kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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" |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 35 | #include "codegen.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 36 | #include "compilation-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "compiler.h" |
| 38 | #include "cpu.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 39 | #include "dateparser-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 41 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | #include "execution.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 43 | #include "global-handles.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | #include "jsregexp.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 45 | #include "liveedit.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 46 | #include "liveobjectlist-inl.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 47 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | #include "platform.h" |
| 49 | #include "runtime.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 50 | #include "runtime-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | #include "scopeinfo.h" |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 52 | #include "smart-pointer.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 53 | #include "stub-cache.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 54 | #include "v8threads.h" |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 55 | #include "string-search.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 57 | namespace v8 { |
| 58 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | |
| 60 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 61 | #define RUNTIME_ASSERT(value) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 62 | if (!(value)) return isolate->ThrowIllegalOperation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
| 64 | // Cast the given object to a value of the specified type and store |
| 65 | // it in a variable with the given name. If the object is not of the |
| 66 | // expected type call IllegalOperation and return. |
| 67 | #define CONVERT_CHECKED(Type, name, obj) \ |
| 68 | RUNTIME_ASSERT(obj->Is##Type()); \ |
| 69 | Type* name = Type::cast(obj); |
| 70 | |
| 71 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 72 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 73 | Handle<Type> name = args.at<Type>(index); |
| 74 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 75 | // Cast the given object to a boolean and store it in a variable with |
| 76 | // the given name. If the object is not a boolean call IllegalOperation |
| 77 | // and return. |
| 78 | #define CONVERT_BOOLEAN_CHECKED(name, obj) \ |
| 79 | RUNTIME_ASSERT(obj->IsBoolean()); \ |
| 80 | bool name = (obj)->IsTrue(); |
| 81 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 82 | // Cast the given object to a Smi and store its value in an int variable |
| 83 | // with the given name. If the object is not a Smi call IllegalOperation |
| 84 | // and return. |
| 85 | #define CONVERT_SMI_CHECKED(name, obj) \ |
| 86 | RUNTIME_ASSERT(obj->IsSmi()); \ |
| 87 | int name = Smi::cast(obj)->value(); |
| 88 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | // Cast the given object to a double and store it in a variable with |
| 90 | // the given name. If the object is not a number (as opposed to |
| 91 | // the number not-a-number) call IllegalOperation and return. |
| 92 | #define CONVERT_DOUBLE_CHECKED(name, obj) \ |
| 93 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 94 | double name = (obj)->Number(); |
| 95 | |
| 96 | // Call the specified converter on the object *comand store the result in |
| 97 | // a variable of the specified type with the given name. If the |
| 98 | // object is not a Number call IllegalOperation and return. |
| 99 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 100 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 101 | type name = NumberTo##Type(obj); |
| 102 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 104 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 105 | JSObject* boilerplate) { |
| 106 | StackLimitCheck check(isolate); |
| 107 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 109 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 110 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 111 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 112 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 113 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 114 | JSObject* copy = JSObject::cast(result); |
| 115 | |
| 116 | // Deep copy local properties. |
| 117 | if (copy->HasFastProperties()) { |
| 118 | FixedArray* properties = copy->properties(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 119 | for (int i = 0; i < properties->length(); i++) { |
| 120 | Object* value = properties->get(i); |
| 121 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 122 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 123 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 124 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 125 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 126 | properties->set(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 129 | int nof = copy->map()->inobject_properties(); |
| 130 | for (int i = 0; i < nof; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 131 | Object* value = copy->InObjectPropertyAt(i); |
| 132 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 133 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 134 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 135 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 136 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 137 | copy->InObjectPropertyAtPut(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 141 | { MaybeObject* maybe_result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 142 | heap->AllocateFixedArray(copy->NumberOfLocalProperties(NONE)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 143 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 144 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 145 | FixedArray* names = FixedArray::cast(result); |
| 146 | copy->GetLocalPropertyNames(names, 0); |
| 147 | for (int i = 0; i < names->length(); i++) { |
| 148 | ASSERT(names->get(i)->IsString()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 149 | String* key_string = String::cast(names->get(i)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 150 | PropertyAttributes attributes = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 151 | copy->GetLocalPropertyAttribute(key_string); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 152 | // Only deep copy fields from the object literal expression. |
| 153 | // In particular, don't try to copy the length attribute of |
| 154 | // an array. |
| 155 | if (attributes != NONE) continue; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 156 | Object* value = |
| 157 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 158 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 159 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 160 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 161 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 162 | } |
| 163 | { MaybeObject* maybe_result = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 164 | // Creating object copy for literals. No strict mode needed. |
| 165 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 166 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 167 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Deep copy local elements. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 173 | // Pixel elements cannot be created using an object literal. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 174 | ASSERT(!copy->HasExternalArrayElements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 175 | switch (copy->GetElementsKind()) { |
| 176 | case JSObject::FAST_ELEMENTS: { |
| 177 | FixedArray* elements = FixedArray::cast(copy->elements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 178 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 179 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 180 | #ifdef DEBUG |
| 181 | for (int i = 0; i < elements->length(); i++) { |
| 182 | ASSERT(!elements->get(i)->IsJSObject()); |
| 183 | } |
| 184 | #endif |
| 185 | } else { |
| 186 | for (int i = 0; i < elements->length(); i++) { |
| 187 | Object* value = elements->get(i); |
| 188 | if (value->IsJSObject()) { |
| 189 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 190 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 191 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 192 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 193 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 194 | elements->set(i, result); |
| 195 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 198 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 199 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 200 | case JSObject::DICTIONARY_ELEMENTS: { |
| 201 | NumberDictionary* element_dictionary = copy->element_dictionary(); |
| 202 | int capacity = element_dictionary->Capacity(); |
| 203 | for (int i = 0; i < capacity; i++) { |
| 204 | Object* k = element_dictionary->KeyAt(i); |
| 205 | if (element_dictionary->IsKey(k)) { |
| 206 | Object* value = element_dictionary->ValueAt(i); |
| 207 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 208 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 209 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 210 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 211 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 212 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 213 | element_dictionary->ValueAtPut(i, result); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | } |
| 219 | default: |
| 220 | UNREACHABLE(); |
| 221 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 222 | } |
| 223 | return copy; |
| 224 | } |
| 225 | |
| 226 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 227 | static MaybeObject* Runtime_CloneLiteralBoilerplate( |
| 228 | RUNTIME_CALLING_CONVENTION) { |
| 229 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 230 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 231 | return DeepCopyBoilerplate(isolate, boilerplate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 235 | static MaybeObject* Runtime_CloneShallowLiteralBoilerplate( |
| 236 | RUNTIME_CALLING_CONVENTION) { |
| 237 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 238 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 239 | return isolate->heap()->CopyJSObject(boilerplate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 243 | static Handle<Map> ComputeObjectLiteralMap( |
| 244 | Handle<Context> context, |
| 245 | Handle<FixedArray> constant_properties, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 246 | bool* is_result_from_cache) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 247 | Isolate* isolate = context->GetIsolate(); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 248 | int properties_length = constant_properties->length(); |
| 249 | int number_of_properties = properties_length / 2; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 250 | if (FLAG_canonicalize_object_literal_maps) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 251 | // Check that there are only symbols and array indices among keys. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 252 | int number_of_symbol_keys = 0; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 253 | for (int p = 0; p != properties_length; p += 2) { |
| 254 | Object* key = constant_properties->get(p); |
| 255 | uint32_t element_index = 0; |
| 256 | if (key->IsSymbol()) { |
| 257 | number_of_symbol_keys++; |
| 258 | } else if (key->ToArrayIndex(&element_index)) { |
| 259 | // An index key does not require space in the property backing store. |
| 260 | number_of_properties--; |
| 261 | } else { |
| 262 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 263 | // ASSERT to make sure that the if condition after the loop is false. |
| 264 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 265 | break; |
| 266 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 267 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 268 | // If we only have symbols and array indices among keys then we can |
| 269 | // use the map cache in the global context. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 270 | const int kMaxKeys = 10; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 271 | if ((number_of_symbol_keys == number_of_properties) && |
| 272 | (number_of_symbol_keys < kMaxKeys)) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 273 | // Create the fixed array with the key. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 274 | Handle<FixedArray> keys = |
| 275 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 276 | if (number_of_symbol_keys > 0) { |
| 277 | int index = 0; |
| 278 | for (int p = 0; p < properties_length; p += 2) { |
| 279 | Object* key = constant_properties->get(p); |
| 280 | if (key->IsSymbol()) { |
| 281 | keys->set(index++, key); |
| 282 | } |
| 283 | } |
| 284 | ASSERT(index == number_of_symbol_keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 285 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 286 | *is_result_from_cache = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 287 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 290 | *is_result_from_cache = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 291 | return isolate->factory()->CopyMap( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 292 | Handle<Map>(context->object_function()->initial_map()), |
| 293 | number_of_properties); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 297 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 298 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 299 | Handle<FixedArray> literals, |
| 300 | Handle<FixedArray> constant_properties); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 301 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 302 | |
| 303 | static Handle<Object> CreateObjectLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 304 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 305 | Handle<FixedArray> literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 306 | Handle<FixedArray> constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 307 | bool should_have_fast_elements, |
| 308 | bool has_function_literal) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 309 | // Get the global context from the literals array. This is the |
| 310 | // context in which the function was created and we use the object |
| 311 | // function from this context to create the object literal. We do |
| 312 | // not use the object function from the current global context |
| 313 | // because this might be the object function from another context |
| 314 | // which we should not have access to. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 315 | Handle<Context> context = |
| 316 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 317 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 318 | // In case we have function literals, we want the object to be in |
| 319 | // slow properties mode for now. We don't go in the map cache because |
| 320 | // maps with constant functions can't be shared if the functions are |
| 321 | // not the same (which is the common case). |
| 322 | bool is_result_from_cache = false; |
| 323 | Handle<Map> map = has_function_literal |
| 324 | ? Handle<Map>(context->object_function()->initial_map()) |
| 325 | : ComputeObjectLiteralMap(context, |
| 326 | constant_properties, |
| 327 | &is_result_from_cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 328 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 329 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 330 | |
| 331 | // Normalize the elements of the boilerplate to save space if needed. |
| 332 | if (!should_have_fast_elements) NormalizeElements(boilerplate); |
| 333 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 334 | // Add the constant properties to the boilerplate. |
| 335 | int length = constant_properties->length(); |
| 336 | bool should_transform = |
| 337 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 338 | if (should_transform || has_function_literal) { |
| 339 | // Normalize the properties of object to avoid n^2 behavior |
| 340 | // when extending the object multiple properties. Indicate the number of |
| 341 | // properties to be added. |
| 342 | NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
| 343 | } |
| 344 | |
| 345 | for (int index = 0; index < length; index +=2) { |
| 346 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 347 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 348 | if (value->IsFixedArray()) { |
| 349 | // The value contains the constant_properties of a |
| 350 | // simple object or array literal. |
| 351 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 352 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 353 | if (value.is_null()) return value; |
| 354 | } |
| 355 | Handle<Object> result; |
| 356 | uint32_t element_index = 0; |
| 357 | if (key->IsSymbol()) { |
| 358 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 359 | // Array index as string (uint32). |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 360 | result = SetOwnElement(boilerplate, |
| 361 | element_index, |
| 362 | value, |
| 363 | kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 364 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 365 | Handle<String> name(String::cast(*key)); |
| 366 | ASSERT(!name->AsArrayIndex(&element_index)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 367 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 368 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 370 | } else if (key->ToArrayIndex(&element_index)) { |
| 371 | // Array index (uint32). |
| 372 | result = SetOwnElement(boilerplate, |
| 373 | element_index, |
| 374 | value, |
| 375 | kNonStrictMode); |
| 376 | } else { |
| 377 | // Non-uint32 number. |
| 378 | ASSERT(key->IsNumber()); |
| 379 | double num = key->Number(); |
| 380 | char arr[100]; |
| 381 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 382 | const char* str = DoubleToCString(num, buffer); |
| 383 | Handle<String> name = |
| 384 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
| 385 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 386 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 387 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 388 | // If setting the property on the boilerplate throws an |
| 389 | // exception, the exception is converted to an empty handle in |
| 390 | // the handle based operations. In that case, we need to |
| 391 | // convert back to an exception. |
| 392 | if (result.is_null()) return result; |
| 393 | } |
| 394 | |
| 395 | // Transform to fast properties if necessary. For object literals with |
| 396 | // containing function literals we defer this operation until after all |
| 397 | // computed properties have been assigned so that we can generate |
| 398 | // constant function properties. |
| 399 | if (should_transform && !has_function_literal) { |
| 400 | TransformToFastProperties(boilerplate, |
| 401 | boilerplate->map()->unused_property_fields()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | } |
| 403 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 404 | return boilerplate; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 408 | static Handle<Object> CreateArrayLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 409 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 410 | Handle<FixedArray> literals, |
| 411 | Handle<FixedArray> elements) { |
| 412 | // Create the JSArray. |
| 413 | Handle<JSFunction> constructor( |
| 414 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 415 | Handle<Object> object = isolate->factory()->NewJSObject(constructor); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 416 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 417 | const bool is_cow = |
| 418 | (elements->map() == isolate->heap()->fixed_cow_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 419 | Handle<FixedArray> copied_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 420 | is_cow ? elements : isolate->factory()->CopyFixedArray(elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 421 | |
| 422 | Handle<FixedArray> content = Handle<FixedArray>::cast(copied_elements); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 423 | if (is_cow) { |
| 424 | #ifdef DEBUG |
| 425 | // Copy-on-write arrays must be shallow (and simple). |
| 426 | for (int i = 0; i < content->length(); i++) { |
| 427 | ASSERT(!content->get(i)->IsFixedArray()); |
| 428 | } |
| 429 | #endif |
| 430 | } else { |
| 431 | for (int i = 0; i < content->length(); i++) { |
| 432 | if (content->get(i)->IsFixedArray()) { |
| 433 | // The value contains the constant_properties of a |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 434 | // simple object or array literal. |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 435 | Handle<FixedArray> fa(FixedArray::cast(content->get(i))); |
| 436 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 437 | CreateLiteralBoilerplate(isolate, literals, fa); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 438 | if (result.is_null()) return result; |
| 439 | content->set(i, *result); |
| 440 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | // Set the elements. |
| 445 | Handle<JSArray>::cast(object)->SetContent(*content); |
| 446 | return object; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 451 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 452 | Handle<FixedArray> literals, |
| 453 | Handle<FixedArray> array) { |
| 454 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 455 | const bool kHasNoFunctionLiteral = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 456 | switch (CompileTimeValue::GetType(array)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 457 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 458 | return CreateObjectLiteralBoilerplate(isolate, |
| 459 | literals, |
| 460 | elements, |
| 461 | true, |
| 462 | kHasNoFunctionLiteral); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 463 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 464 | return CreateObjectLiteralBoilerplate(isolate, |
| 465 | literals, |
| 466 | elements, |
| 467 | false, |
| 468 | kHasNoFunctionLiteral); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 469 | case CompileTimeValue::ARRAY_LITERAL: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 470 | return CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 471 | default: |
| 472 | UNREACHABLE(); |
| 473 | return Handle<Object>::null(); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 478 | static MaybeObject* Runtime_CreateArrayLiteralBoilerplate( |
| 479 | RUNTIME_CALLING_CONVENTION) { |
| 480 | RUNTIME_GET_ISOLATE; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 481 | // Takes a FixedArray of elements containing the literal elements of |
| 482 | // the array literal and produces JSArray with those elements. |
| 483 | // Additionally takes the literals array of the surrounding function |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 484 | // which contains the context from which to get the Array function |
| 485 | // to use for creating the array literal. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 486 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 487 | ASSERT(args.length() == 3); |
| 488 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 489 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 490 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 492 | Handle<Object> object = |
| 493 | CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 494 | if (object.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 495 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 496 | // Update the functions literal and return the boilerplate. |
| 497 | literals->set(literals_index, *object); |
| 498 | return *object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 502 | static MaybeObject* Runtime_CreateObjectLiteral(RUNTIME_CALLING_CONVENTION) { |
| 503 | RUNTIME_GET_ISOLATE; |
| 504 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 505 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 506 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 507 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 508 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 509 | CONVERT_SMI_CHECKED(flags, args[3]); |
| 510 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 511 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 512 | |
| 513 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 514 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 515 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 516 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 517 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 518 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 519 | should_have_fast_elements, |
| 520 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 521 | if (boilerplate.is_null()) return Failure::Exception(); |
| 522 | // Update the functions literal and return the boilerplate. |
| 523 | literals->set(literals_index, *boilerplate); |
| 524 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 525 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 529 | static MaybeObject* Runtime_CreateObjectLiteralShallow( |
| 530 | RUNTIME_CALLING_CONVENTION) { |
| 531 | RUNTIME_GET_ISOLATE; |
| 532 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 533 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 534 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 535 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 536 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 537 | CONVERT_SMI_CHECKED(flags, args[3]); |
| 538 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 539 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 540 | |
| 541 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 542 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 543 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 544 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 545 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 546 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 547 | should_have_fast_elements, |
| 548 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 549 | if (boilerplate.is_null()) return Failure::Exception(); |
| 550 | // Update the functions literal and return the boilerplate. |
| 551 | literals->set(literals_index, *boilerplate); |
| 552 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 553 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 557 | static MaybeObject* Runtime_CreateArrayLiteral(RUNTIME_CALLING_CONVENTION) { |
| 558 | RUNTIME_GET_ISOLATE; |
| 559 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 560 | ASSERT(args.length() == 3); |
| 561 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 562 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 563 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 564 | |
| 565 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 566 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 567 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 568 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 569 | if (boilerplate.is_null()) return Failure::Exception(); |
| 570 | // Update the functions literal and return the boilerplate. |
| 571 | literals->set(literals_index, *boilerplate); |
| 572 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 573 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 577 | static MaybeObject* Runtime_CreateArrayLiteralShallow( |
| 578 | RUNTIME_CALLING_CONVENTION) { |
| 579 | RUNTIME_GET_ISOLATE; |
| 580 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 581 | ASSERT(args.length() == 3); |
| 582 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 583 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 584 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 585 | |
| 586 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 587 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 588 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 589 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 590 | if (boilerplate.is_null()) return Failure::Exception(); |
| 591 | // Update the functions literal and return the boilerplate. |
| 592 | literals->set(literals_index, *boilerplate); |
| 593 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 594 | if (JSObject::cast(*boilerplate)->elements()->map() == |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 595 | isolate->heap()->fixed_cow_array_map()) { |
| 596 | COUNTERS->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 597 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 598 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 602 | static MaybeObject* Runtime_CreateCatchExtensionObject( |
| 603 | RUNTIME_CALLING_CONVENTION) { |
| 604 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 605 | ASSERT(args.length() == 2); |
| 606 | CONVERT_CHECKED(String, key, args[0]); |
| 607 | Object* value = args[1]; |
| 608 | // Create a catch context extension object. |
| 609 | JSFunction* constructor = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 610 | isolate->context()->global_context()-> |
| 611 | context_extension_function(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 612 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 613 | { MaybeObject* maybe_object = isolate->heap()->AllocateJSObject(constructor); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 614 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 615 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 616 | // Assign the exception value to the catch variable and make sure |
| 617 | // that the catch variable is DontDelete. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 618 | { MaybeObject* maybe_value = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 619 | // Passing non-strict per ECMA-262 5th Ed. 12.14. Catch, bullet #4. |
| 620 | JSObject::cast(object)->SetProperty( |
| 621 | key, value, DONT_DELETE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 622 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 623 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 624 | return object; |
| 625 | } |
| 626 | |
| 627 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 628 | static MaybeObject* Runtime_ClassOf(RUNTIME_CALLING_CONVENTION) { |
| 629 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 630 | NoHandleAllocation ha; |
| 631 | ASSERT(args.length() == 1); |
| 632 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 633 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 634 | return JSObject::cast(obj)->class_name(); |
| 635 | } |
| 636 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 637 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 638 | static MaybeObject* Runtime_IsInPrototypeChain(RUNTIME_CALLING_CONVENTION) { |
| 639 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | NoHandleAllocation ha; |
| 641 | ASSERT(args.length() == 2); |
| 642 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 643 | Object* O = args[0]; |
| 644 | Object* V = args[1]; |
| 645 | while (true) { |
| 646 | Object* prototype = V->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 647 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 648 | if (O == prototype) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | V = prototype; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 654 | // Inserts an object as the hidden prototype of another object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 655 | static MaybeObject* Runtime_SetHiddenPrototype(RUNTIME_CALLING_CONVENTION) { |
| 656 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 657 | NoHandleAllocation ha; |
| 658 | ASSERT(args.length() == 2); |
| 659 | CONVERT_CHECKED(JSObject, jsobject, args[0]); |
| 660 | CONVERT_CHECKED(JSObject, proto, args[1]); |
| 661 | |
| 662 | // Sanity checks. The old prototype (that we are replacing) could |
| 663 | // theoretically be null, but if it is not null then check that we |
| 664 | // didn't already install a hidden prototype here. |
| 665 | RUNTIME_ASSERT(!jsobject->GetPrototype()->IsHeapObject() || |
| 666 | !HeapObject::cast(jsobject->GetPrototype())->map()->is_hidden_prototype()); |
| 667 | RUNTIME_ASSERT(!proto->map()->is_hidden_prototype()); |
| 668 | |
| 669 | // Allocate up front before we start altering state in case we get a GC. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 670 | Object* map_or_failure; |
| 671 | { MaybeObject* maybe_map_or_failure = proto->map()->CopyDropTransitions(); |
| 672 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 673 | return maybe_map_or_failure; |
| 674 | } |
| 675 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 676 | Map* new_proto_map = Map::cast(map_or_failure); |
| 677 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 678 | { MaybeObject* maybe_map_or_failure = jsobject->map()->CopyDropTransitions(); |
| 679 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 680 | return maybe_map_or_failure; |
| 681 | } |
| 682 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 683 | Map* new_map = Map::cast(map_or_failure); |
| 684 | |
| 685 | // Set proto's prototype to be the old prototype of the object. |
| 686 | new_proto_map->set_prototype(jsobject->GetPrototype()); |
| 687 | proto->set_map(new_proto_map); |
| 688 | new_proto_map->set_is_hidden_prototype(); |
| 689 | |
| 690 | // Set the object's prototype to proto. |
| 691 | new_map->set_prototype(proto); |
| 692 | jsobject->set_map(new_map); |
| 693 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 694 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 698 | static MaybeObject* Runtime_IsConstructCall(RUNTIME_CALLING_CONVENTION) { |
| 699 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 701 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | JavaScriptFrameIterator it; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 703 | return isolate->heap()->ToBoolean(it.frame()->IsConstructor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 707 | // Recursively traverses hidden prototypes if property is not found |
| 708 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 709 | String* name, |
| 710 | LookupResult* result) { |
| 711 | obj->LocalLookupRealNamedProperty(name, result); |
| 712 | |
| 713 | if (!result->IsProperty()) { |
| 714 | Object* proto = obj->GetPrototype(); |
| 715 | if (proto->IsJSObject() && |
| 716 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 717 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 718 | name, result); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 723 | static bool CheckAccessException(LookupResult* result, |
| 724 | v8::AccessType access_type) { |
| 725 | if (result->type() == CALLBACKS) { |
| 726 | Object* callback = result->GetCallbackObject(); |
| 727 | if (callback->IsAccessorInfo()) { |
| 728 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 729 | bool can_access = |
| 730 | (access_type == v8::ACCESS_HAS && |
| 731 | (info->all_can_read() || info->all_can_write())) || |
| 732 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 733 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 734 | return can_access; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | static bool CheckAccess(JSObject* obj, |
| 743 | String* name, |
| 744 | LookupResult* result, |
| 745 | v8::AccessType access_type) { |
| 746 | ASSERT(result->IsProperty()); |
| 747 | |
| 748 | JSObject* holder = result->holder(); |
| 749 | JSObject* current = obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 750 | Isolate* isolate = obj->GetIsolate(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 751 | while (true) { |
| 752 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 753 | !isolate->MayNamedAccess(current, name, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 754 | // Access check callback denied the access, but some properties |
| 755 | // can have a special permissions which override callbacks descision |
| 756 | // (currently see v8::AccessControl). |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | if (current == holder) { |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | current = JSObject::cast(current->GetPrototype()); |
| 765 | } |
| 766 | |
| 767 | // API callbacks can have per callback access exceptions. |
| 768 | switch (result->type()) { |
| 769 | case CALLBACKS: { |
| 770 | if (CheckAccessException(result, access_type)) { |
| 771 | return true; |
| 772 | } |
| 773 | break; |
| 774 | } |
| 775 | case INTERCEPTOR: { |
| 776 | // If the object has an interceptor, try real named properties. |
| 777 | // Overwrite the result to fetch the correct property later. |
| 778 | holder->LookupRealNamedProperty(name, result); |
| 779 | if (result->IsProperty()) { |
| 780 | if (CheckAccessException(result, access_type)) { |
| 781 | return true; |
| 782 | } |
| 783 | } |
| 784 | break; |
| 785 | } |
| 786 | default: |
| 787 | break; |
| 788 | } |
| 789 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 790 | isolate->ReportFailedAccessCheck(current, access_type); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 796 | static bool CheckElementAccess(JSObject* obj, |
| 797 | uint32_t index, |
| 798 | v8::AccessType access_type) { |
| 799 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 800 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 801 | return false; |
| 802 | } |
| 803 | |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 808 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 809 | enum PropertyDescriptorIndices { |
| 810 | IS_ACCESSOR_INDEX, |
| 811 | VALUE_INDEX, |
| 812 | GETTER_INDEX, |
| 813 | SETTER_INDEX, |
| 814 | WRITABLE_INDEX, |
| 815 | ENUMERABLE_INDEX, |
| 816 | CONFIGURABLE_INDEX, |
| 817 | DESCRIPTOR_SIZE |
| 818 | }; |
| 819 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 820 | // Returns an array with the property description: |
| 821 | // if args[1] is not a property on args[0] |
| 822 | // returns undefined |
| 823 | // if args[1] is a data property on args[0] |
| 824 | // [false, value, Writeable, Enumerable, Configurable] |
| 825 | // if args[1] is an accessor on args[0] |
| 826 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 827 | static MaybeObject* Runtime_GetOwnProperty(RUNTIME_CALLING_CONVENTION) { |
| 828 | RUNTIME_GET_ISOLATE; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 829 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 830 | Heap* heap = isolate->heap(); |
| 831 | HandleScope scope(isolate); |
| 832 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 833 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 834 | LookupResult result; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 835 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 836 | CONVERT_ARG_CHECKED(String, name, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 837 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 838 | // This could be an element. |
| 839 | uint32_t index; |
| 840 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 841 | switch (obj->HasLocalElement(index)) { |
| 842 | case JSObject::UNDEFINED_ELEMENT: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 843 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 844 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 845 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 846 | // Special handling of string objects according to ECMAScript 5 |
| 847 | // 15.5.5.2. Note that this might be a string object with elements |
| 848 | // other than the actual string value. This is covered by the |
| 849 | // subsequent cases. |
| 850 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 851 | Handle<String> str(String::cast(js_value->value())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 852 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 853 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 854 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 855 | elms->set(VALUE_INDEX, *substr); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 856 | elms->set(WRITABLE_INDEX, heap->false_value()); |
| 857 | elms->set(ENUMERABLE_INDEX, heap->false_value()); |
| 858 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 859 | return *desc; |
| 860 | } |
| 861 | |
| 862 | case JSObject::INTERCEPTED_ELEMENT: |
| 863 | case JSObject::FAST_ELEMENT: { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 864 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 865 | Handle<Object> value = GetElement(obj, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 866 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 867 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 868 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 869 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 870 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 871 | return *desc; |
| 872 | } |
| 873 | |
| 874 | case JSObject::DICTIONARY_ELEMENT: { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 875 | Handle<JSObject> holder = obj; |
| 876 | if (obj->IsJSGlobalProxy()) { |
| 877 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 878 | if (proto->IsNull()) return heap->undefined_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 879 | ASSERT(proto->IsJSGlobalObject()); |
| 880 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 881 | } |
| 882 | NumberDictionary* dictionary = holder->element_dictionary(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 883 | int entry = dictionary->FindEntry(index); |
| 884 | ASSERT(entry != NumberDictionary::kNotFound); |
| 885 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 886 | switch (details.type()) { |
| 887 | case CALLBACKS: { |
| 888 | // This is an accessor property with getter and/or setter. |
| 889 | FixedArray* callbacks = |
| 890 | FixedArray::cast(dictionary->ValueAt(entry)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 891 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 892 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
| 893 | elms->set(GETTER_INDEX, callbacks->get(0)); |
| 894 | } |
| 895 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
| 896 | elms->set(SETTER_INDEX, callbacks->get(1)); |
| 897 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 898 | break; |
| 899 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 900 | case NORMAL: { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 901 | // This is a data property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 902 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 903 | Handle<Object> value = GetElement(obj, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 904 | ASSERT(!value.is_null()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 905 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 906 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 907 | break; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 908 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 909 | default: |
| 910 | UNREACHABLE(); |
| 911 | break; |
| 912 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 913 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 914 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 915 | return *desc; |
| 916 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 920 | // Use recursive implementation to also traverse hidden prototypes |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 921 | GetOwnPropertyImplementation(*obj, *name, &result); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 922 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 923 | if (!result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 924 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 925 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 926 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 927 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 928 | return heap->false_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 929 | } |
| 930 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 931 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 932 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 933 | |
| 934 | bool is_js_accessor = (result.type() == CALLBACKS) && |
| 935 | (result.GetCallbackObject()->IsFixedArray()); |
| 936 | |
| 937 | if (is_js_accessor) { |
| 938 | // __defineGetter__/__defineSetter__ callback. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 939 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 940 | |
| 941 | FixedArray* structure = FixedArray::cast(result.GetCallbackObject()); |
| 942 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
| 943 | elms->set(GETTER_INDEX, structure->get(0)); |
| 944 | } |
| 945 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
| 946 | elms->set(SETTER_INDEX, structure->get(1)); |
| 947 | } |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 948 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 949 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 950 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 951 | |
| 952 | PropertyAttributes attrs; |
| 953 | Object* value; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 954 | // GetProperty will check access and report any violations. |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 955 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 956 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 957 | } |
| 958 | elms->set(VALUE_INDEX, value); |
| 959 | } |
| 960 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 961 | return *desc; |
| 962 | } |
| 963 | |
| 964 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 965 | static MaybeObject* Runtime_PreventExtensions(RUNTIME_CALLING_CONVENTION) { |
| 966 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 967 | ASSERT(args.length() == 1); |
| 968 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 969 | return obj->PreventExtensions(); |
| 970 | } |
| 971 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 972 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 973 | static MaybeObject* Runtime_IsExtensible(RUNTIME_CALLING_CONVENTION) { |
| 974 | RUNTIME_GET_ISOLATE; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 975 | ASSERT(args.length() == 1); |
| 976 | CONVERT_CHECKED(JSObject, obj, args[0]); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 977 | if (obj->IsJSGlobalProxy()) { |
| 978 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 979 | if (proto->IsNull()) return isolate->heap()->false_value(); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 980 | ASSERT(proto->IsJSGlobalObject()); |
| 981 | obj = JSObject::cast(proto); |
| 982 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 983 | return obj->map()->is_extensible() ? isolate->heap()->true_value() |
| 984 | : isolate->heap()->false_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 988 | static MaybeObject* Runtime_RegExpCompile(RUNTIME_CALLING_CONVENTION) { |
| 989 | RUNTIME_GET_ISOLATE; |
| 990 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 991 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 992 | CONVERT_ARG_CHECKED(JSRegExp, re, 0); |
| 993 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 994 | CONVERT_ARG_CHECKED(String, flags, 2); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 995 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 996 | if (result.is_null()) return Failure::Exception(); |
| 997 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1001 | static MaybeObject* Runtime_CreateApiFunction(RUNTIME_CALLING_CONVENTION) { |
| 1002 | RUNTIME_GET_ISOLATE; |
| 1003 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1004 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1005 | CONVERT_ARG_CHECKED(FunctionTemplateInfo, data, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1006 | return *isolate->factory()->CreateApiFunction(data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1010 | static MaybeObject* Runtime_IsTemplate(RUNTIME_CALLING_CONVENTION) { |
| 1011 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1012 | ASSERT(args.length() == 1); |
| 1013 | Object* arg = args[0]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1014 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1015 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1019 | static MaybeObject* Runtime_GetTemplateField(RUNTIME_CALLING_CONVENTION) { |
| 1020 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1021 | ASSERT(args.length() == 2); |
| 1022 | CONVERT_CHECKED(HeapObject, templ, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1023 | CONVERT_CHECKED(Smi, field, args[1]); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1024 | int index = field->value(); |
| 1025 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1026 | InstanceType type = templ->map()->instance_type(); |
| 1027 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1028 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1029 | RUNTIME_ASSERT(offset > 0); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1030 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1031 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1032 | } else { |
| 1033 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1034 | } |
| 1035 | return *HeapObject::RawField(templ, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1039 | static MaybeObject* Runtime_DisableAccessChecks(RUNTIME_CALLING_CONVENTION) { |
| 1040 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1041 | ASSERT(args.length() == 1); |
| 1042 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1043 | Map* old_map = object->map(); |
| 1044 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1045 | if (needs_access_checks) { |
| 1046 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1047 | Object* new_map; |
| 1048 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1049 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1050 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1051 | |
| 1052 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1053 | object->set_map(Map::cast(new_map)); |
| 1054 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1055 | return needs_access_checks ? isolate->heap()->true_value() |
| 1056 | : isolate->heap()->false_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1060 | static MaybeObject* Runtime_EnableAccessChecks(RUNTIME_CALLING_CONVENTION) { |
| 1061 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1062 | ASSERT(args.length() == 1); |
| 1063 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1064 | Map* old_map = object->map(); |
| 1065 | if (!old_map->is_access_check_needed()) { |
| 1066 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1067 | Object* new_map; |
| 1068 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1069 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1070 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1071 | |
| 1072 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1073 | object->set_map(Map::cast(new_map)); |
| 1074 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1075 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1079 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1080 | const char* type, |
| 1081 | Handle<String> name) { |
| 1082 | HandleScope scope(isolate); |
| 1083 | Handle<Object> type_handle = |
| 1084 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1085 | Handle<Object> args[2] = { type_handle, name }; |
| 1086 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1087 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1088 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1092 | static MaybeObject* Runtime_DeclareGlobals(RUNTIME_CALLING_CONVENTION) { |
| 1093 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1094 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1095 | HandleScope scope(isolate); |
| 1096 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1097 | isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1099 | Handle<Context> context = args.at<Context>(0); |
| 1100 | CONVERT_ARG_CHECKED(FixedArray, pairs, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | bool is_eval = Smi::cast(args[2])->value() == 1; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1102 | StrictModeFlag strict_mode = |
| 1103 | static_cast<StrictModeFlag>(Smi::cast(args[3])->value()); |
| 1104 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1105 | |
| 1106 | // Compute the property attributes. According to ECMA-262, section |
| 1107 | // 13, page 71, the property must be read-only and |
| 1108 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1109 | // property as read-only, so we don't either. |
| 1110 | PropertyAttributes base = is_eval ? NONE : DONT_DELETE; |
| 1111 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1112 | // Traverse the name/value pairs and set the properties. |
| 1113 | int length = pairs->length(); |
| 1114 | for (int i = 0; i < length; i += 2) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1115 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1116 | Handle<String> name(String::cast(pairs->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1117 | Handle<Object> value(pairs->get(i + 1), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | |
| 1119 | // We have to declare a global const property. To capture we only |
| 1120 | // assign to it when evaluating the assignment for "const x = |
| 1121 | // <expr>" the initial value is the hole. |
| 1122 | bool is_const_property = value->IsTheHole(); |
| 1123 | |
| 1124 | if (value->IsUndefined() || is_const_property) { |
| 1125 | // Lookup the property in the global object, and don't set the |
| 1126 | // value of the variable if the property is already there. |
| 1127 | LookupResult lookup; |
| 1128 | global->Lookup(*name, &lookup); |
| 1129 | if (lookup.IsProperty()) { |
| 1130 | // Determine if the property is local by comparing the holder |
| 1131 | // against the global object. The information will be used to |
| 1132 | // avoid throwing re-declaration errors when declaring |
| 1133 | // variables or constants that exist in the prototype chain. |
| 1134 | bool is_local = (*global == lookup.holder()); |
| 1135 | // Get the property attributes and determine if the property is |
| 1136 | // read-only. |
| 1137 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1138 | bool is_read_only = (attributes & READ_ONLY) != 0; |
| 1139 | if (lookup.type() == INTERCEPTOR) { |
| 1140 | // If the interceptor says the property is there, we |
| 1141 | // just return undefined without overwriting the property. |
| 1142 | // Otherwise, we continue to setting the property. |
| 1143 | if (attributes != ABSENT) { |
| 1144 | // Check if the existing property conflicts with regards to const. |
| 1145 | if (is_local && (is_read_only || is_const_property)) { |
| 1146 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1147 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1148 | }; |
| 1149 | // The property already exists without conflicting: Go to |
| 1150 | // the next declaration. |
| 1151 | continue; |
| 1152 | } |
| 1153 | // Fall-through and introduce the absent property by using |
| 1154 | // SetProperty. |
| 1155 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1156 | // For const properties, we treat a callback with this name |
| 1157 | // even in the prototype as a conflicting declaration. |
| 1158 | if (is_const_property && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1159 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1160 | } |
| 1161 | // Otherwise, we check for locally conflicting declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1162 | if (is_local && (is_read_only || is_const_property)) { |
| 1163 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1164 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1165 | } |
| 1166 | // The property already exists without conflicting: Go to |
| 1167 | // the next declaration. |
| 1168 | continue; |
| 1169 | } |
| 1170 | } |
| 1171 | } else { |
| 1172 | // Copy the function and update its context. Use it as value. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1173 | Handle<SharedFunctionInfo> shared = |
| 1174 | Handle<SharedFunctionInfo>::cast(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1175 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1176 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1177 | context, |
| 1178 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1179 | value = function; |
| 1180 | } |
| 1181 | |
| 1182 | LookupResult lookup; |
| 1183 | global->LocalLookup(*name, &lookup); |
| 1184 | |
| 1185 | PropertyAttributes attributes = is_const_property |
| 1186 | ? static_cast<PropertyAttributes>(base | READ_ONLY) |
| 1187 | : base; |
| 1188 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1189 | // There's a local property that we need to overwrite because |
| 1190 | // we're either declaring a function or there's an interceptor |
| 1191 | // that claims the property is absent. |
| 1192 | // |
| 1193 | // Check for conflicting re-declarations. We cannot have |
| 1194 | // conflicting types in case of intercepted properties because |
| 1195 | // they are absent. |
| 1196 | if (lookup.IsProperty() && |
| 1197 | (lookup.type() != INTERCEPTOR) && |
| 1198 | (lookup.IsReadOnly() || is_const_property)) { |
| 1199 | const char* type = (lookup.IsReadOnly()) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1200 | return ThrowRedeclarationError(isolate, type, name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1201 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1202 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1203 | // Safari does not allow the invocation of callback setters for |
| 1204 | // function declarations. To mimic this behavior, we do not allow |
| 1205 | // the invocation of setters for function values. This makes a |
| 1206 | // difference for global functions with the same names as event |
| 1207 | // handlers such as "function onload() {}". Firefox does call the |
| 1208 | // onload setter in those case and Safari does not. We follow |
| 1209 | // Safari for compatibility. |
| 1210 | if (value->IsJSFunction()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1211 | // Do not change DONT_DELETE to false from true. |
| 1212 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
| 1213 | attributes = static_cast<PropertyAttributes>( |
| 1214 | attributes | (lookup.GetAttributes() & DONT_DELETE)); |
| 1215 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1216 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1217 | SetLocalPropertyIgnoreAttributes(global, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1218 | name, |
| 1219 | value, |
| 1220 | attributes)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1222 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1223 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1224 | name, |
| 1225 | value, |
| 1226 | attributes, |
| 1227 | strict_mode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1230 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1231 | ASSERT(!isolate->has_pending_exception()); |
| 1232 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1236 | static MaybeObject* Runtime_DeclareContextSlot(RUNTIME_CALLING_CONVENTION) { |
| 1237 | RUNTIME_GET_ISOLATE; |
| 1238 | HandleScope scope(isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1239 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1241 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 1242 | Handle<String> name(String::cast(args[1])); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1243 | PropertyAttributes mode = |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1244 | static_cast<PropertyAttributes>(Smi::cast(args[2])->value()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1245 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1246 | Handle<Object> initial_value(args[3], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Declarations are always done in the function context. |
| 1249 | context = Handle<Context>(context->fcontext()); |
| 1250 | |
| 1251 | int index; |
| 1252 | PropertyAttributes attributes; |
| 1253 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1254 | Handle<Object> holder = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1255 | context->Lookup(name, flags, &index, &attributes); |
| 1256 | |
| 1257 | if (attributes != ABSENT) { |
| 1258 | // The name was declared before; check for conflicting |
| 1259 | // re-declarations: This is similar to the code in parser.cc in |
| 1260 | // the AstBuildingParser::Declare function. |
| 1261 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1262 | // Functions are not read-only. |
| 1263 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1264 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1265 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | // Initialize it if necessary. |
| 1269 | if (*initial_value != NULL) { |
| 1270 | if (index >= 0) { |
| 1271 | // The variable or constant context slot should always be in |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1272 | // the function context or the arguments object. |
| 1273 | if (holder->IsContext()) { |
| 1274 | ASSERT(holder.is_identical_to(context)); |
| 1275 | if (((attributes & READ_ONLY) == 0) || |
| 1276 | context->get(index)->IsTheHole()) { |
| 1277 | context->set(index, *initial_value); |
| 1278 | } |
| 1279 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1280 | // The holder is an arguments object. |
| 1281 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1282 | Handle<Object> result = SetElement(arguments, index, initial_value, |
| 1283 | kNonStrictMode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1284 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } else { |
| 1287 | // Slow case: The property is not in the FixedArray part of the context. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1288 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1289 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1290 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1291 | SetProperty(context_ext, name, initial_value, |
| 1292 | mode, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1295 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1296 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1297 | // The property is not in the function context. It needs to be |
| 1298 | // "declared" in the function context's extension context, or in the |
| 1299 | // global context. |
| 1300 | Handle<JSObject> context_ext; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1301 | if (context->has_extension()) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1302 | // The function context's extension context exists - use it. |
| 1303 | context_ext = Handle<JSObject>(context->extension()); |
| 1304 | } else { |
| 1305 | // The function context's extension context does not exists - allocate |
| 1306 | // it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1307 | context_ext = isolate->factory()->NewJSObject( |
| 1308 | isolate->context_extension_function()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1309 | // And store it in the extension slot. |
| 1310 | context->set_extension(*context_ext); |
| 1311 | } |
| 1312 | ASSERT(*context_ext != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1313 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1314 | // Declare the property by setting it to the initial value if provided, |
| 1315 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1316 | // constant declarations). |
| 1317 | ASSERT(!context_ext->HasLocalProperty(*name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1318 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1319 | if (*initial_value != NULL) value = initial_value; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1320 | // Declaring a const context slot is a conflicting declaration if |
| 1321 | // there is a callback with that name in a prototype. It is |
| 1322 | // allowed to introduce const variables in |
| 1323 | // JSContextExtensionObjects. They are treated specially in |
| 1324 | // SetProperty and no setters are invoked for those since they are |
| 1325 | // not real JSObjects. |
| 1326 | if (initial_value->IsTheHole() && |
| 1327 | !context_ext->IsJSContextExtensionObject()) { |
| 1328 | LookupResult lookup; |
| 1329 | context_ext->Lookup(*name, &lookup); |
| 1330 | if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1331 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1334 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1335 | SetProperty(context_ext, name, value, mode, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1336 | kNonStrictMode)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1339 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1343 | static MaybeObject* Runtime_InitializeVarGlobal(RUNTIME_CALLING_CONVENTION) { |
| 1344 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | NoHandleAllocation nha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1346 | // args[0] == name |
| 1347 | // args[1] == strict_mode |
| 1348 | // args[2] == value (optional) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1349 | |
| 1350 | // Determine if we need to assign to the variable if it already |
| 1351 | // exists (based on the number of arguments). |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1352 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1353 | bool assign = args.length() == 3; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1354 | |
| 1355 | CONVERT_ARG_CHECKED(String, name, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1356 | GlobalObject* global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1357 | RUNTIME_ASSERT(args[1]->IsSmi()); |
| 1358 | StrictModeFlag strict_mode = |
| 1359 | static_cast<StrictModeFlag>(Smi::cast(args[1])->value()); |
| 1360 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1361 | |
| 1362 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1363 | // not be deletable. |
| 1364 | PropertyAttributes attributes = DONT_DELETE; |
| 1365 | |
| 1366 | // Lookup the property locally in the global object. If it isn't |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1367 | // there, there is a property with this name in the prototype chain. |
| 1368 | // We follow Safari and Firefox behavior and only set the property |
| 1369 | // locally if there is an explicit initialization value that we have |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1370 | // to assign to the property. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1371 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1372 | // the whole chain of hidden prototypes to do a 'local' lookup. |
| 1373 | JSObject* real_holder = global; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1374 | LookupResult lookup; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1375 | while (true) { |
| 1376 | real_holder->LocalLookup(*name, &lookup); |
| 1377 | if (lookup.IsProperty()) { |
| 1378 | // Determine if this is a redeclaration of something read-only. |
| 1379 | if (lookup.IsReadOnly()) { |
| 1380 | // If we found readonly property on one of hidden prototypes, |
| 1381 | // just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1382 | if (real_holder != isolate->context()->global()) break; |
| 1383 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | // Determine if this is a redeclaration of an intercepted read-only |
| 1387 | // property and figure out if the property exists at all. |
| 1388 | bool found = true; |
| 1389 | PropertyType type = lookup.type(); |
| 1390 | if (type == INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1391 | HandleScope handle_scope(isolate); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1392 | Handle<JSObject> holder(real_holder); |
| 1393 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1394 | real_holder = *holder; |
| 1395 | if (intercepted == ABSENT) { |
| 1396 | // The interceptor claims the property isn't there. We need to |
| 1397 | // make sure to introduce it. |
| 1398 | found = false; |
| 1399 | } else if ((intercepted & READ_ONLY) != 0) { |
| 1400 | // The property is present, but read-only. Since we're trying to |
| 1401 | // overwrite it with a variable declaration we must throw a |
| 1402 | // re-declaration error. However if we found readonly property |
| 1403 | // on one of hidden prototypes, just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1404 | if (real_holder != isolate->context()->global()) break; |
| 1405 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | if (found && !assign) { |
| 1410 | // The global property is there and we're not assigning any value |
| 1411 | // to it. Just return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1412 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | // Assign the value (or undefined) to the property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1416 | Object* value = (assign) ? args[2] : isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1417 | return real_holder->SetProperty( |
| 1418 | &lookup, *name, value, attributes, strict_mode); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1419 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1420 | |
| 1421 | Object* proto = real_holder->GetPrototype(); |
| 1422 | if (!proto->IsJSObject()) |
| 1423 | break; |
| 1424 | |
| 1425 | if (!JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 1426 | break; |
| 1427 | |
| 1428 | real_holder = JSObject::cast(proto); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1431 | global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1432 | if (assign) { |
| 1433 | return global->SetProperty(*name, args[2], attributes, strict_mode); |
| 1434 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1435 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1439 | static MaybeObject* Runtime_InitializeConstGlobal(RUNTIME_CALLING_CONVENTION) { |
| 1440 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | // All constants are declared with an initial value. The name |
| 1442 | // of the constant is the first argument and the initial value |
| 1443 | // is the second. |
| 1444 | RUNTIME_ASSERT(args.length() == 2); |
| 1445 | CONVERT_ARG_CHECKED(String, name, 0); |
| 1446 | Handle<Object> value = args.at<Object>(1); |
| 1447 | |
| 1448 | // Get the current global object from top. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1449 | GlobalObject* global = isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1450 | |
| 1451 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1452 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1453 | PropertyAttributes attributes = |
| 1454 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1455 | |
| 1456 | // Lookup the property locally in the global object. If it isn't |
| 1457 | // there, we add the property and take special precautions to always |
| 1458 | // add it as a local property even in case of callbacks in the |
| 1459 | // prototype chain (this rules out using SetProperty). |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1460 | // We use SetLocalPropertyIgnoreAttributes instead |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1461 | LookupResult lookup; |
| 1462 | global->LocalLookup(*name, &lookup); |
| 1463 | if (!lookup.IsProperty()) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1464 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1465 | *value, |
| 1466 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | // Determine if this is a redeclaration of something not |
| 1470 | // read-only. In case the result is hidden behind an interceptor we |
| 1471 | // need to ask it for the property attributes. |
| 1472 | if (!lookup.IsReadOnly()) { |
| 1473 | if (lookup.type() != INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1474 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | PropertyAttributes intercepted = global->GetPropertyAttribute(*name); |
| 1478 | |
| 1479 | // Throw re-declaration error if the intercepted property is present |
| 1480 | // but not read-only. |
| 1481 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1482 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | // Restore global object from context (in case of GC) and continue |
| 1486 | // with setting the value because the property is either absent or |
| 1487 | // read-only. We also have to do redo the lookup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1488 | HandleScope handle_scope(isolate); |
| 1489 | Handle<GlobalObject> global(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1490 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1491 | // BUG 1213575: Handle the case where we have to set a read-only |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1492 | // property through an interceptor and only do it if it's |
| 1493 | // uninitialized, e.g. the hole. Nirk... |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1494 | // Passing non-strict mode because the property is writable. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1495 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1496 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1497 | name, |
| 1498 | value, |
| 1499 | attributes, |
| 1500 | kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1501 | return *value; |
| 1502 | } |
| 1503 | |
| 1504 | // Set the value, but only we're assigning the initial value to a |
| 1505 | // constant. For now, we determine this by checking if the |
| 1506 | // current value is the hole. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1507 | // Strict mode handling not needed (const disallowed in strict mode). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1508 | PropertyType type = lookup.type(); |
| 1509 | if (type == FIELD) { |
| 1510 | FixedArray* properties = global->properties(); |
| 1511 | int index = lookup.GetFieldIndex(); |
| 1512 | if (properties->get(index)->IsTheHole()) { |
| 1513 | properties->set(index, *value); |
| 1514 | } |
| 1515 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1516 | if (global->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1517 | global->SetNormalizedProperty(&lookup, *value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1518 | } |
| 1519 | } else { |
| 1520 | // Ignore re-initialization of constants that have already been |
| 1521 | // assigned a function value. |
| 1522 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1523 | } |
| 1524 | |
| 1525 | // Use the set value as the result of the operation. |
| 1526 | return *value; |
| 1527 | } |
| 1528 | |
| 1529 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1530 | static MaybeObject* Runtime_InitializeConstContextSlot( |
| 1531 | RUNTIME_CALLING_CONVENTION) { |
| 1532 | RUNTIME_GET_ISOLATE; |
| 1533 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1534 | ASSERT(args.length() == 3); |
| 1535 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1536 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1537 | ASSERT(!value->IsTheHole()); |
| 1538 | CONVERT_ARG_CHECKED(Context, context, 1); |
| 1539 | Handle<String> name(String::cast(args[2])); |
| 1540 | |
| 1541 | // Initializations are always done in the function context. |
| 1542 | context = Handle<Context>(context->fcontext()); |
| 1543 | |
| 1544 | int index; |
| 1545 | PropertyAttributes attributes; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1546 | ContextLookupFlags flags = FOLLOW_CHAINS; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1547 | Handle<Object> holder = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1548 | context->Lookup(name, flags, &index, &attributes); |
| 1549 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1550 | // In most situations, the property introduced by the const |
| 1551 | // declaration should be present in the context extension object. |
| 1552 | // However, because declaration and initialization are separate, the |
| 1553 | // property might have been deleted (if it was introduced by eval) |
| 1554 | // before we reach the initialization point. |
| 1555 | // |
| 1556 | // Example: |
| 1557 | // |
| 1558 | // function f() { eval("delete x; const x;"); } |
| 1559 | // |
| 1560 | // In that case, the initialization behaves like a normal assignment |
| 1561 | // to property 'x'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1562 | if (index >= 0) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1563 | // Property was found in a context. |
| 1564 | if (holder->IsContext()) { |
| 1565 | // The holder cannot be the function context. If it is, there |
| 1566 | // should have been a const redeclaration error when declaring |
| 1567 | // the const property. |
| 1568 | ASSERT(!holder.is_identical_to(context)); |
| 1569 | if ((attributes & READ_ONLY) == 0) { |
| 1570 | Handle<Context>::cast(holder)->set(index, *value); |
| 1571 | } |
| 1572 | } else { |
| 1573 | // The holder is an arguments object. |
| 1574 | ASSERT((attributes & READ_ONLY) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1575 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1576 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1577 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1578 | SetElement(arguments, index, value, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1579 | } |
| 1580 | return *value; |
| 1581 | } |
| 1582 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1583 | // The property could not be found, we introduce it in the global |
| 1584 | // context. |
| 1585 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1586 | Handle<JSObject> global = Handle<JSObject>( |
| 1587 | isolate->context()->global()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1588 | // Strict mode not needed (const disallowed in strict mode). |
| 1589 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1590 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1591 | SetProperty(global, name, value, NONE, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1592 | return *value; |
| 1593 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1595 | // The property was present in a context extension object. |
| 1596 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1597 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1598 | if (*context_ext == context->extension()) { |
| 1599 | // This is the property that was introduced by the const |
| 1600 | // declaration. Set it if it hasn't been set before. NOTE: We |
| 1601 | // cannot use GetProperty() to get the current value as it |
| 1602 | // 'unholes' the value. |
| 1603 | LookupResult lookup; |
| 1604 | context_ext->LocalLookupRealNamedProperty(*name, &lookup); |
| 1605 | ASSERT(lookup.IsProperty()); // the property was declared |
| 1606 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1607 | |
| 1608 | PropertyType type = lookup.type(); |
| 1609 | if (type == FIELD) { |
| 1610 | FixedArray* properties = context_ext->properties(); |
| 1611 | int index = lookup.GetFieldIndex(); |
| 1612 | if (properties->get(index)->IsTheHole()) { |
| 1613 | properties->set(index, *value); |
| 1614 | } |
| 1615 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1616 | if (context_ext->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1617 | context_ext->SetNormalizedProperty(&lookup, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1618 | } |
| 1619 | } else { |
| 1620 | // We should not reach here. Any real, named property should be |
| 1621 | // either a field or a dictionary slot. |
| 1622 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } else { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1625 | // The property was found in a different context extension object. |
| 1626 | // Set it if it is not a read-only property. |
| 1627 | if ((attributes & READ_ONLY) == 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1628 | // Strict mode not needed (const disallowed in strict mode). |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1629 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1630 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1631 | SetProperty(context_ext, name, value, attributes, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1632 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1633 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1634 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1635 | return *value; |
| 1636 | } |
| 1637 | |
| 1638 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1639 | static MaybeObject* Runtime_OptimizeObjectForAddingMultipleProperties( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1640 | RUNTIME_CALLING_CONVENTION) { |
| 1641 | RUNTIME_GET_ISOLATE; |
| 1642 | HandleScope scope(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1643 | ASSERT(args.length() == 2); |
| 1644 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 1645 | CONVERT_SMI_CHECKED(properties, args[1]); |
| 1646 | if (object->HasFastProperties()) { |
| 1647 | NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
| 1648 | } |
| 1649 | return *object; |
| 1650 | } |
| 1651 | |
| 1652 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1653 | static MaybeObject* Runtime_RegExpExec(RUNTIME_CALLING_CONVENTION) { |
| 1654 | RUNTIME_GET_ISOLATE; |
| 1655 | HandleScope scope(isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1656 | ASSERT(args.length() == 4); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1657 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1658 | CONVERT_ARG_CHECKED(String, subject, 1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1659 | // Due to the way the JS calls are constructed this must be less than the |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1660 | // length of a string, i.e. it is always a Smi. We check anyway for security. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1661 | CONVERT_SMI_CHECKED(index, args[2]); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1662 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1663 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1664 | RUNTIME_ASSERT(index >= 0); |
| 1665 | RUNTIME_ASSERT(index <= subject->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1666 | isolate->counters()->regexp_entry_runtime()->Increment(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1667 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1668 | subject, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1669 | index, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1670 | last_match_info); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1671 | if (result.is_null()) return Failure::Exception(); |
| 1672 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1676 | static MaybeObject* Runtime_RegExpConstructResult(RUNTIME_CALLING_CONVENTION) { |
| 1677 | RUNTIME_GET_ISOLATE; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1678 | ASSERT(args.length() == 3); |
| 1679 | CONVERT_SMI_CHECKED(elements_count, args[0]); |
| 1680 | if (elements_count > JSArray::kMaxFastElementsLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1681 | return isolate->ThrowIllegalOperation(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1682 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1683 | Object* new_object; |
| 1684 | { MaybeObject* maybe_new_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1685 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1686 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1687 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1688 | FixedArray* elements = FixedArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1689 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1690 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1691 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1692 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1693 | { |
| 1694 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1695 | HandleScope scope(isolate); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1696 | reinterpret_cast<HeapObject*>(new_object)-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1697 | set_map(isolate->global_context()->regexp_result_map()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1698 | } |
| 1699 | JSArray* array = JSArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1700 | array->set_properties(isolate->heap()->empty_fixed_array()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1701 | array->set_elements(elements); |
| 1702 | array->set_length(Smi::FromInt(elements_count)); |
| 1703 | // Write in-object properties after the length of the array. |
| 1704 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1705 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1706 | return array; |
| 1707 | } |
| 1708 | |
| 1709 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1710 | static MaybeObject* Runtime_RegExpInitializeObject(RUNTIME_CALLING_CONVENTION) { |
| 1711 | RUNTIME_GET_ISOLATE; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1712 | AssertNoAllocation no_alloc; |
| 1713 | ASSERT(args.length() == 5); |
| 1714 | CONVERT_CHECKED(JSRegExp, regexp, args[0]); |
| 1715 | CONVERT_CHECKED(String, source, args[1]); |
| 1716 | |
| 1717 | Object* global = args[2]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1718 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1719 | |
| 1720 | Object* ignoreCase = args[3]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1721 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1722 | |
| 1723 | Object* multiline = args[4]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1724 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1725 | |
| 1726 | Map* map = regexp->map(); |
| 1727 | Object* constructor = map->constructor(); |
| 1728 | if (constructor->IsJSFunction() && |
| 1729 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1730 | // If we still have the original map, set in-object properties directly. |
| 1731 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
| 1732 | // TODO(lrn): Consider skipping write barrier on booleans as well. |
| 1733 | // Both true and false should be in oldspace at all times. |
| 1734 | regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, global); |
| 1735 | regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, ignoreCase); |
| 1736 | regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, multiline); |
| 1737 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1738 | Smi::FromInt(0), |
| 1739 | SKIP_WRITE_BARRIER); |
| 1740 | return regexp; |
| 1741 | } |
| 1742 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1743 | // Map has changed, so use generic, but slower, method. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1744 | PropertyAttributes final = |
| 1745 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1746 | PropertyAttributes writable = |
| 1747 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1748 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1749 | MaybeObject* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1750 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1751 | source, |
| 1752 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1753 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1754 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1755 | global, |
| 1756 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1757 | ASSERT(!result->IsFailure()); |
| 1758 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1759 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1760 | ignoreCase, |
| 1761 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1762 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1763 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1764 | multiline, |
| 1765 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1766 | ASSERT(!result->IsFailure()); |
| 1767 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1768 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1769 | Smi::FromInt(0), |
| 1770 | writable); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1771 | ASSERT(!result->IsFailure()); |
| 1772 | USE(result); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1773 | return regexp; |
| 1774 | } |
| 1775 | |
| 1776 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1777 | static MaybeObject* Runtime_FinishArrayPrototypeSetup( |
| 1778 | RUNTIME_CALLING_CONVENTION) { |
| 1779 | RUNTIME_GET_ISOLATE; |
| 1780 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1781 | ASSERT(args.length() == 1); |
| 1782 | CONVERT_ARG_CHECKED(JSArray, prototype, 0); |
| 1783 | // This is necessary to enable fast checks for absence of elements |
| 1784 | // on Array.prototype and below. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1785 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1786 | return Smi::FromInt(0); |
| 1787 | } |
| 1788 | |
| 1789 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1790 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1791 | Handle<JSObject> holder, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1792 | const char* name, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1793 | Builtins::Name builtin_name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1794 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1795 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1796 | Handle<JSFunction> optimized = |
| 1797 | isolate->factory()->NewFunction(key, |
| 1798 | JS_OBJECT_TYPE, |
| 1799 | JSObject::kHeaderSize, |
| 1800 | code, |
| 1801 | false); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1802 | optimized->shared()->DontAdaptArguments(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1803 | SetProperty(holder, key, optimized, NONE, kStrictMode); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1804 | return optimized; |
| 1805 | } |
| 1806 | |
| 1807 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1808 | static MaybeObject* Runtime_SpecialArrayFunctions(RUNTIME_CALLING_CONVENTION) { |
| 1809 | RUNTIME_GET_ISOLATE; |
| 1810 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1811 | ASSERT(args.length() == 1); |
| 1812 | CONVERT_ARG_CHECKED(JSObject, holder, 0); |
| 1813 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1814 | InstallBuiltin(isolate, holder, "pop", Builtins::ArrayPop); |
| 1815 | InstallBuiltin(isolate, holder, "push", Builtins::ArrayPush); |
| 1816 | InstallBuiltin(isolate, holder, "shift", Builtins::ArrayShift); |
| 1817 | InstallBuiltin(isolate, holder, "unshift", Builtins::ArrayUnshift); |
| 1818 | InstallBuiltin(isolate, holder, "slice", Builtins::ArraySlice); |
| 1819 | InstallBuiltin(isolate, holder, "splice", Builtins::ArraySplice); |
| 1820 | InstallBuiltin(isolate, holder, "concat", Builtins::ArrayConcat); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1821 | |
| 1822 | return *holder; |
| 1823 | } |
| 1824 | |
| 1825 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1826 | static MaybeObject* Runtime_GetGlobalReceiver(RUNTIME_CALLING_CONVENTION) { |
| 1827 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1828 | // Returns a real global receiver, not one of builtins object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1829 | Context* global_context = |
| 1830 | isolate->context()->global()->global_context(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1831 | return global_context->global()->global_receiver(); |
| 1832 | } |
| 1833 | |
| 1834 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1835 | static MaybeObject* Runtime_MaterializeRegExpLiteral( |
| 1836 | RUNTIME_CALLING_CONVENTION) { |
| 1837 | RUNTIME_GET_ISOLATE; |
| 1838 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1839 | ASSERT(args.length() == 4); |
| 1840 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 1841 | int index = Smi::cast(args[1])->value(); |
| 1842 | Handle<String> pattern = args.at<String>(2); |
| 1843 | Handle<String> flags = args.at<String>(3); |
| 1844 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1845 | // Get the RegExp function from the context in the literals array. |
| 1846 | // This is the RegExp function from the context in which the |
| 1847 | // function was created. We do not use the RegExp function from the |
| 1848 | // current global context because this might be the RegExp function |
| 1849 | // from another context which we should not have access to. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1850 | Handle<JSFunction> constructor = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1851 | Handle<JSFunction>( |
| 1852 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1853 | // Compute the regular expression literal. |
| 1854 | bool has_pending_exception; |
| 1855 | Handle<Object> regexp = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1856 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1857 | &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1858 | if (has_pending_exception) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1859 | ASSERT(isolate->has_pending_exception()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1860 | return Failure::Exception(); |
| 1861 | } |
| 1862 | literals->set(index, *regexp); |
| 1863 | return *regexp; |
| 1864 | } |
| 1865 | |
| 1866 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1867 | static MaybeObject* Runtime_FunctionGetName(RUNTIME_CALLING_CONVENTION) { |
| 1868 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1869 | NoHandleAllocation ha; |
| 1870 | ASSERT(args.length() == 1); |
| 1871 | |
| 1872 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1873 | return f->shared()->name(); |
| 1874 | } |
| 1875 | |
| 1876 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1877 | static MaybeObject* Runtime_FunctionSetName(RUNTIME_CALLING_CONVENTION) { |
| 1878 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1879 | NoHandleAllocation ha; |
| 1880 | ASSERT(args.length() == 2); |
| 1881 | |
| 1882 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1883 | CONVERT_CHECKED(String, name, args[1]); |
| 1884 | f->shared()->set_name(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1885 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1889 | static MaybeObject* Runtime_FunctionRemovePrototype( |
| 1890 | RUNTIME_CALLING_CONVENTION) { |
| 1891 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1892 | NoHandleAllocation ha; |
| 1893 | ASSERT(args.length() == 1); |
| 1894 | |
| 1895 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1896 | Object* obj = f->RemovePrototype(); |
| 1897 | if (obj->IsFailure()) return obj; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1898 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1899 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1903 | static MaybeObject* Runtime_FunctionGetScript(RUNTIME_CALLING_CONVENTION) { |
| 1904 | RUNTIME_GET_ISOLATE; |
| 1905 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1906 | ASSERT(args.length() == 1); |
| 1907 | |
| 1908 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1909 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 1910 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1911 | |
| 1912 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 1913 | } |
| 1914 | |
| 1915 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1916 | static MaybeObject* Runtime_FunctionGetSourceCode(RUNTIME_CALLING_CONVENTION) { |
| 1917 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1918 | NoHandleAllocation ha; |
| 1919 | ASSERT(args.length() == 1); |
| 1920 | |
| 1921 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1922 | return f->shared()->GetSourceCode(); |
| 1923 | } |
| 1924 | |
| 1925 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1926 | static MaybeObject* Runtime_FunctionGetScriptSourcePosition( |
| 1927 | RUNTIME_CALLING_CONVENTION) { |
| 1928 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1929 | NoHandleAllocation ha; |
| 1930 | ASSERT(args.length() == 1); |
| 1931 | |
| 1932 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1933 | int pos = fun->shared()->start_position(); |
| 1934 | return Smi::FromInt(pos); |
| 1935 | } |
| 1936 | |
| 1937 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1938 | static MaybeObject* Runtime_FunctionGetPositionForOffset( |
| 1939 | RUNTIME_CALLING_CONVENTION) { |
| 1940 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1941 | ASSERT(args.length() == 2); |
| 1942 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1943 | CONVERT_CHECKED(Code, code, args[0]); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1944 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 1945 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1946 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 1947 | |
| 1948 | Address pc = code->address() + offset; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1949 | return Smi::FromInt(code->SourcePosition(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1953 | static MaybeObject* Runtime_FunctionSetInstanceClassName( |
| 1954 | RUNTIME_CALLING_CONVENTION) { |
| 1955 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1956 | NoHandleAllocation ha; |
| 1957 | ASSERT(args.length() == 2); |
| 1958 | |
| 1959 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1960 | CONVERT_CHECKED(String, name, args[1]); |
| 1961 | fun->SetInstanceClassName(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1962 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1966 | static MaybeObject* Runtime_FunctionSetLength(RUNTIME_CALLING_CONVENTION) { |
| 1967 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1968 | NoHandleAllocation ha; |
| 1969 | ASSERT(args.length() == 2); |
| 1970 | |
| 1971 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1972 | CONVERT_CHECKED(Smi, length, args[1]); |
| 1973 | fun->shared()->set_length(length->value()); |
| 1974 | return length; |
| 1975 | } |
| 1976 | |
| 1977 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1978 | static MaybeObject* Runtime_FunctionSetPrototype(RUNTIME_CALLING_CONVENTION) { |
| 1979 | RUNTIME_GET_ISOLATE; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1980 | NoHandleAllocation ha; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1981 | ASSERT(args.length() == 2); |
| 1982 | |
| 1983 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1984 | ASSERT(fun->should_have_prototype()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1985 | Object* obj; |
| 1986 | { MaybeObject* maybe_obj = |
| 1987 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 1988 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1989 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1990 | return args[0]; // return TOS |
| 1991 | } |
| 1992 | |
| 1993 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 1994 | static MaybeObject* Runtime_FunctionIsAPIFunction(RUNTIME_CALLING_CONVENTION) { |
| 1995 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1996 | NoHandleAllocation ha; |
| 1997 | ASSERT(args.length() == 1); |
| 1998 | |
| 1999 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2000 | return f->shared()->IsApiFunction() ? isolate->heap()->true_value() |
| 2001 | : isolate->heap()->false_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2004 | |
| 2005 | static MaybeObject* Runtime_FunctionIsBuiltin(RUNTIME_CALLING_CONVENTION) { |
| 2006 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 2007 | NoHandleAllocation ha; |
| 2008 | ASSERT(args.length() == 1); |
| 2009 | |
| 2010 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2011 | return f->IsBuiltin() ? isolate->heap()->true_value() : |
| 2012 | isolate->heap()->false_value(); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2015 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2016 | static MaybeObject* Runtime_SetCode(RUNTIME_CALLING_CONVENTION) { |
| 2017 | RUNTIME_GET_ISOLATE; |
| 2018 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2019 | ASSERT(args.length() == 2); |
| 2020 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2021 | CONVERT_ARG_CHECKED(JSFunction, target, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2022 | Handle<Object> code = args.at<Object>(1); |
| 2023 | |
| 2024 | Handle<Context> context(target->context()); |
| 2025 | |
| 2026 | if (!code->IsNull()) { |
| 2027 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 2028 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2029 | Handle<SharedFunctionInfo> shared(fun->shared()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2030 | |
| 2031 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2032 | return Failure::Exception(); |
| 2033 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2034 | // Since we don't store the source for this we should never |
| 2035 | // optimize this. |
| 2036 | shared->code()->set_optimizable(false); |
| 2037 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2038 | // Set the code, scope info, formal parameter count, |
| 2039 | // and the length of the target function. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2040 | target->shared()->set_code(shared->code()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2041 | target->ReplaceCode(shared->code()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2042 | target->shared()->set_scope_info(shared->scope_info()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2043 | target->shared()->set_length(shared->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2044 | target->shared()->set_formal_parameter_count( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2045 | shared->formal_parameter_count()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2046 | // Set the source code of the target function to undefined. |
| 2047 | // SetCode is only used for built-in constructors like String, |
| 2048 | // Array, and Object, and some web code |
| 2049 | // doesn't like seeing source code for constructors. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2050 | target->shared()->set_script(isolate->heap()->undefined_value()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2051 | target->shared()->code()->set_optimizable(false); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2052 | // Clear the optimization hints related to the compiled code as these are no |
| 2053 | // longer valid when the code is overwritten. |
| 2054 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2055 | context = Handle<Context>(fun->context()); |
| 2056 | |
| 2057 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2058 | // cross context contamination. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2059 | int number_of_literals = fun->NumberOfLiterals(); |
| 2060 | Handle<FixedArray> literals = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2061 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2062 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2063 | // Insert the object, regexp and array functions in the literals |
| 2064 | // array prefix. These are the functions that will be used when |
| 2065 | // creating object, regexp and array literals. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2066 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2067 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2069 | // It's okay to skip the write barrier here because the literals |
| 2070 | // are guaranteed to be in old space. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2071 | target->set_literals(*literals, SKIP_WRITE_BARRIER); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2072 | target->set_next_function_link(isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | target->set_context(*context); |
| 2076 | return *target; |
| 2077 | } |
| 2078 | |
| 2079 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2080 | static MaybeObject* Runtime_SetExpectedNumberOfProperties( |
| 2081 | RUNTIME_CALLING_CONVENTION) { |
| 2082 | RUNTIME_GET_ISOLATE; |
| 2083 | HandleScope scope(isolate); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2084 | ASSERT(args.length() == 2); |
| 2085 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 2086 | CONVERT_SMI_CHECKED(num, args[1]); |
| 2087 | RUNTIME_ASSERT(num >= 0); |
| 2088 | SetExpectedNofProperties(function, num); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2089 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2093 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2094 | Object* char_code) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2095 | uint32_t code; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2096 | if (char_code->ToArrayIndex(&code)) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2097 | if (code <= 0xffff) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2098 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2099 | } |
| 2100 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2101 | return isolate->heap()->empty_string(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2105 | static MaybeObject* Runtime_StringCharCodeAt(RUNTIME_CALLING_CONVENTION) { |
| 2106 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2107 | NoHandleAllocation ha; |
| 2108 | ASSERT(args.length() == 2); |
| 2109 | |
| 2110 | CONVERT_CHECKED(String, subject, args[0]); |
| 2111 | Object* index = args[1]; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2112 | RUNTIME_ASSERT(index->IsNumber()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2114 | uint32_t i = 0; |
| 2115 | if (index->IsSmi()) { |
| 2116 | int value = Smi::cast(index)->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2117 | if (value < 0) return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2118 | i = value; |
| 2119 | } else { |
| 2120 | ASSERT(index->IsHeapNumber()); |
| 2121 | double value = HeapNumber::cast(index)->value(); |
| 2122 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
kasperl@chromium.org | 74e4e5e | 2010-01-25 10:15:52 +0000 | [diff] [blame] | 2123 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2124 | |
| 2125 | // Flatten the string. If someone wants to get a char at an index |
| 2126 | // in a cons string, it is likely that more indices will be |
| 2127 | // accessed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2128 | Object* flat; |
| 2129 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2130 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2131 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2132 | subject = String::cast(flat); |
| 2133 | |
| 2134 | if (i >= static_cast<uint32_t>(subject->length())) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2135 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | return Smi::FromInt(subject->Get(i)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2142 | static MaybeObject* Runtime_CharFromCode(RUNTIME_CALLING_CONVENTION) { |
| 2143 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2144 | NoHandleAllocation ha; |
| 2145 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2146 | return CharFromCode(isolate, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2149 | |
| 2150 | class FixedArrayBuilder { |
| 2151 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2152 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2153 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2154 | length_(0) { |
| 2155 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2156 | // extend the array will work. |
| 2157 | ASSERT(initial_capacity > 0); |
| 2158 | } |
| 2159 | |
| 2160 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2161 | : array_(backing_store), |
| 2162 | length_(0) { |
| 2163 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2164 | // extend the array will work. |
| 2165 | ASSERT(backing_store->length() > 0); |
| 2166 | } |
| 2167 | |
| 2168 | bool HasCapacity(int elements) { |
| 2169 | int length = array_->length(); |
| 2170 | int required_length = length_ + elements; |
| 2171 | return (length >= required_length); |
| 2172 | } |
| 2173 | |
| 2174 | void EnsureCapacity(int elements) { |
| 2175 | int length = array_->length(); |
| 2176 | int required_length = length_ + elements; |
| 2177 | if (length < required_length) { |
| 2178 | int new_length = length; |
| 2179 | do { |
| 2180 | new_length *= 2; |
| 2181 | } while (new_length < required_length); |
| 2182 | Handle<FixedArray> extended_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2183 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2184 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2185 | array_ = extended_array; |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | void Add(Object* value) { |
| 2190 | ASSERT(length_ < capacity()); |
| 2191 | array_->set(length_, value); |
| 2192 | length_++; |
| 2193 | } |
| 2194 | |
| 2195 | void Add(Smi* value) { |
| 2196 | ASSERT(length_ < capacity()); |
| 2197 | array_->set(length_, value); |
| 2198 | length_++; |
| 2199 | } |
| 2200 | |
| 2201 | Handle<FixedArray> array() { |
| 2202 | return array_; |
| 2203 | } |
| 2204 | |
| 2205 | int length() { |
| 2206 | return length_; |
| 2207 | } |
| 2208 | |
| 2209 | int capacity() { |
| 2210 | return array_->length(); |
| 2211 | } |
| 2212 | |
| 2213 | Handle<JSArray> ToJSArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2214 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2215 | result_array->set_length(Smi::FromInt(length_)); |
| 2216 | return result_array; |
| 2217 | } |
| 2218 | |
| 2219 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
| 2220 | target_array->set_elements(*array_); |
| 2221 | target_array->set_length(Smi::FromInt(length_)); |
| 2222 | return target_array; |
| 2223 | } |
| 2224 | |
| 2225 | private: |
| 2226 | Handle<FixedArray> array_; |
| 2227 | int length_; |
| 2228 | }; |
| 2229 | |
| 2230 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2231 | // Forward declarations. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2232 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2233 | const int kStringBuilderConcatHelperPositionBits = 19; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2234 | |
| 2235 | template <typename schar> |
| 2236 | static inline void StringBuilderConcatHelper(String*, |
| 2237 | schar*, |
| 2238 | FixedArray*, |
| 2239 | int); |
| 2240 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2241 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2242 | StringBuilderSubstringLength; |
| 2243 | typedef BitField<int, |
| 2244 | kStringBuilderConcatHelperLengthBits, |
| 2245 | kStringBuilderConcatHelperPositionBits> |
| 2246 | StringBuilderSubstringPosition; |
| 2247 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2248 | |
| 2249 | class ReplacementStringBuilder { |
| 2250 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2251 | ReplacementStringBuilder(Heap* heap, |
| 2252 | Handle<String> subject, |
| 2253 | int estimated_part_count) |
| 2254 | : heap_(heap), |
| 2255 | array_builder_(heap->isolate(), estimated_part_count), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2256 | subject_(subject), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2257 | character_count_(0), |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2258 | is_ascii_(subject->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2259 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2260 | // extend the array will work. |
| 2261 | ASSERT(estimated_part_count > 0); |
| 2262 | } |
| 2263 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2264 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2265 | int from, |
| 2266 | int to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2267 | ASSERT(from >= 0); |
| 2268 | int length = to - from; |
| 2269 | ASSERT(length > 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2270 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2271 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2272 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2273 | StringBuilderSubstringPosition::encode(from); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2274 | builder->Add(Smi::FromInt(encoded_slice)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2275 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2276 | // Otherwise encode as two smis. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2277 | builder->Add(Smi::FromInt(-length)); |
| 2278 | builder->Add(Smi::FromInt(from)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2279 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | void EnsureCapacity(int elements) { |
| 2284 | array_builder_.EnsureCapacity(elements); |
| 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | void AddSubjectSlice(int from, int to) { |
| 2289 | AddSubjectSlice(&array_builder_, from, to); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2290 | IncrementCharacterCount(to - from); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | |
| 2294 | void AddString(Handle<String> string) { |
| 2295 | int length = string->length(); |
| 2296 | ASSERT(length > 0); |
| 2297 | AddElement(*string); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2298 | if (!string->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2299 | is_ascii_ = false; |
| 2300 | } |
| 2301 | IncrementCharacterCount(length); |
| 2302 | } |
| 2303 | |
| 2304 | |
| 2305 | Handle<String> ToString() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2306 | if (array_builder_.length() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2307 | return heap_->isolate()->factory()->empty_string(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | Handle<String> joined_string; |
| 2311 | if (is_ascii_) { |
| 2312 | joined_string = NewRawAsciiString(character_count_); |
| 2313 | AssertNoAllocation no_alloc; |
| 2314 | SeqAsciiString* seq = SeqAsciiString::cast(*joined_string); |
| 2315 | char* char_buffer = seq->GetChars(); |
| 2316 | StringBuilderConcatHelper(*subject_, |
| 2317 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2318 | *array_builder_.array(), |
| 2319 | array_builder_.length()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2320 | } else { |
| 2321 | // Non-ASCII. |
| 2322 | joined_string = NewRawTwoByteString(character_count_); |
| 2323 | AssertNoAllocation no_alloc; |
| 2324 | SeqTwoByteString* seq = SeqTwoByteString::cast(*joined_string); |
| 2325 | uc16* char_buffer = seq->GetChars(); |
| 2326 | StringBuilderConcatHelper(*subject_, |
| 2327 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2328 | *array_builder_.array(), |
| 2329 | array_builder_.length()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2330 | } |
| 2331 | return joined_string; |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | void IncrementCharacterCount(int by) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2336 | if (character_count_ > String::kMaxLength - by) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2337 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2338 | } |
| 2339 | character_count_ += by; |
| 2340 | } |
| 2341 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2342 | Handle<JSArray> GetParts() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2343 | return array_builder_.ToJSArray(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2344 | } |
kmillikin@chromium.org | d982519 | 2010-03-30 08:36:16 +0000 | [diff] [blame] | 2345 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2346 | private: |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2347 | Handle<String> NewRawAsciiString(int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2348 | CALL_HEAP_FUNCTION(heap_->isolate(), |
| 2349 | heap_->AllocateRawAsciiString(size), String); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | Handle<String> NewRawTwoByteString(int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2354 | CALL_HEAP_FUNCTION(heap_->isolate(), |
| 2355 | heap_->AllocateRawTwoByteString(size), String); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | void AddElement(Object* element) { |
| 2360 | ASSERT(element->IsSmi() || element->IsString()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2361 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2362 | array_builder_.Add(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2365 | Heap* heap_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2366 | FixedArrayBuilder array_builder_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2367 | Handle<String> subject_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2368 | int character_count_; |
| 2369 | bool is_ascii_; |
| 2370 | }; |
| 2371 | |
| 2372 | |
| 2373 | class CompiledReplacement { |
| 2374 | public: |
| 2375 | CompiledReplacement() |
| 2376 | : parts_(1), replacement_substrings_(0) {} |
| 2377 | |
| 2378 | void Compile(Handle<String> replacement, |
| 2379 | int capture_count, |
| 2380 | int subject_length); |
| 2381 | |
| 2382 | void Apply(ReplacementStringBuilder* builder, |
| 2383 | int match_from, |
| 2384 | int match_to, |
| 2385 | Handle<JSArray> last_match_info); |
| 2386 | |
| 2387 | // Number of distinct parts of the replacement pattern. |
| 2388 | int parts() { |
| 2389 | return parts_.length(); |
| 2390 | } |
| 2391 | private: |
| 2392 | enum PartType { |
| 2393 | SUBJECT_PREFIX = 1, |
| 2394 | SUBJECT_SUFFIX, |
| 2395 | SUBJECT_CAPTURE, |
| 2396 | REPLACEMENT_SUBSTRING, |
| 2397 | REPLACEMENT_STRING, |
| 2398 | |
| 2399 | NUMBER_OF_PART_TYPES |
| 2400 | }; |
| 2401 | |
| 2402 | struct ReplacementPart { |
| 2403 | static inline ReplacementPart SubjectMatch() { |
| 2404 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2405 | } |
| 2406 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2407 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2408 | } |
| 2409 | static inline ReplacementPart SubjectPrefix() { |
| 2410 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2411 | } |
| 2412 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2413 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2414 | } |
| 2415 | static inline ReplacementPart ReplacementString() { |
| 2416 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2417 | } |
| 2418 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2419 | ASSERT(from >= 0); |
| 2420 | ASSERT(to > from); |
| 2421 | return ReplacementPart(-from, to); |
| 2422 | } |
| 2423 | |
| 2424 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2425 | // the replacement pattern, otherwise it's a value from PartType. |
| 2426 | ReplacementPart(int tag, int data) |
| 2427 | : tag(tag), data(data) { |
| 2428 | // Must be non-positive or a PartType value. |
| 2429 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2430 | } |
| 2431 | // Either a value of PartType or a non-positive number that is |
| 2432 | // the negation of an index into the replacement string. |
| 2433 | int tag; |
| 2434 | // The data value's interpretation depends on the value of tag: |
| 2435 | // tag == SUBJECT_PREFIX || |
| 2436 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2437 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2438 | // tag == REPLACEMENT_SUBSTRING || |
| 2439 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2440 | // of the replacement string. |
| 2441 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2442 | // string ranging over -tag .. data. |
| 2443 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2444 | // substring objects. |
| 2445 | int data; |
| 2446 | }; |
| 2447 | |
| 2448 | template<typename Char> |
| 2449 | static void ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
| 2450 | Vector<Char> characters, |
| 2451 | int capture_count, |
| 2452 | int subject_length) { |
| 2453 | int length = characters.length(); |
| 2454 | int last = 0; |
| 2455 | for (int i = 0; i < length; i++) { |
| 2456 | Char c = characters[i]; |
| 2457 | if (c == '$') { |
| 2458 | int next_index = i + 1; |
| 2459 | if (next_index == length) { // No next character! |
| 2460 | break; |
| 2461 | } |
| 2462 | Char c2 = characters[next_index]; |
| 2463 | switch (c2) { |
| 2464 | case '$': |
| 2465 | if (i > last) { |
| 2466 | // There is a substring before. Include the first "$". |
| 2467 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2468 | last = next_index + 1; // Continue after the second "$". |
| 2469 | } else { |
| 2470 | // Let the next substring start with the second "$". |
| 2471 | last = next_index; |
| 2472 | } |
| 2473 | i = next_index; |
| 2474 | break; |
| 2475 | case '`': |
| 2476 | if (i > last) { |
| 2477 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2478 | } |
| 2479 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2480 | i = next_index; |
| 2481 | last = i + 1; |
| 2482 | break; |
| 2483 | case '\'': |
| 2484 | if (i > last) { |
| 2485 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2486 | } |
| 2487 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2488 | i = next_index; |
| 2489 | last = i + 1; |
| 2490 | break; |
| 2491 | case '&': |
| 2492 | if (i > last) { |
| 2493 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2494 | } |
| 2495 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2496 | i = next_index; |
| 2497 | last = i + 1; |
| 2498 | break; |
| 2499 | case '0': |
| 2500 | case '1': |
| 2501 | case '2': |
| 2502 | case '3': |
| 2503 | case '4': |
| 2504 | case '5': |
| 2505 | case '6': |
| 2506 | case '7': |
| 2507 | case '8': |
| 2508 | case '9': { |
| 2509 | int capture_ref = c2 - '0'; |
| 2510 | if (capture_ref > capture_count) { |
| 2511 | i = next_index; |
| 2512 | continue; |
| 2513 | } |
| 2514 | int second_digit_index = next_index + 1; |
| 2515 | if (second_digit_index < length) { |
| 2516 | // Peek ahead to see if we have two digits. |
| 2517 | Char c3 = characters[second_digit_index]; |
| 2518 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2519 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2520 | if (double_digit_ref <= capture_count) { |
| 2521 | next_index = second_digit_index; |
| 2522 | capture_ref = double_digit_ref; |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | if (capture_ref > 0) { |
| 2527 | if (i > last) { |
| 2528 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2529 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2530 | ASSERT(capture_ref <= capture_count); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2531 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2532 | last = next_index + 1; |
| 2533 | } |
| 2534 | i = next_index; |
| 2535 | break; |
| 2536 | } |
| 2537 | default: |
| 2538 | i = next_index; |
| 2539 | break; |
| 2540 | } |
| 2541 | } |
| 2542 | } |
| 2543 | if (length > last) { |
| 2544 | if (last == 0) { |
| 2545 | parts->Add(ReplacementPart::ReplacementString()); |
| 2546 | } else { |
| 2547 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2548 | } |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | ZoneList<ReplacementPart> parts_; |
| 2553 | ZoneList<Handle<String> > replacement_substrings_; |
| 2554 | }; |
| 2555 | |
| 2556 | |
| 2557 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2558 | int capture_count, |
| 2559 | int subject_length) { |
| 2560 | ASSERT(replacement->IsFlat()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2561 | if (replacement->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2562 | AssertNoAllocation no_alloc; |
| 2563 | ParseReplacementPattern(&parts_, |
| 2564 | replacement->ToAsciiVector(), |
| 2565 | capture_count, |
| 2566 | subject_length); |
| 2567 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2568 | ASSERT(replacement->IsTwoByteRepresentation()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2569 | AssertNoAllocation no_alloc; |
| 2570 | |
| 2571 | ParseReplacementPattern(&parts_, |
| 2572 | replacement->ToUC16Vector(), |
| 2573 | capture_count, |
| 2574 | subject_length); |
| 2575 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2576 | Isolate* isolate = replacement->GetIsolate(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2577 | // Find substrings of replacement string and create them as String objects. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2578 | int substring_index = 0; |
| 2579 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2580 | int tag = parts_[i].tag; |
| 2581 | if (tag <= 0) { // A replacement string slice. |
| 2582 | int from = -tag; |
| 2583 | int to = parts_[i].data; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2584 | replacement_substrings_.Add( |
| 2585 | isolate->factory()->NewSubString(replacement, from, to)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2586 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2587 | parts_[i].data = substring_index; |
| 2588 | substring_index++; |
| 2589 | } else if (tag == REPLACEMENT_STRING) { |
| 2590 | replacement_substrings_.Add(replacement); |
| 2591 | parts_[i].data = substring_index; |
| 2592 | substring_index++; |
| 2593 | } |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | |
| 2598 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2599 | int match_from, |
| 2600 | int match_to, |
| 2601 | Handle<JSArray> last_match_info) { |
| 2602 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2603 | ReplacementPart part = parts_[i]; |
| 2604 | switch (part.tag) { |
| 2605 | case SUBJECT_PREFIX: |
| 2606 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2607 | break; |
| 2608 | case SUBJECT_SUFFIX: { |
| 2609 | int subject_length = part.data; |
| 2610 | if (match_to < subject_length) { |
| 2611 | builder->AddSubjectSlice(match_to, subject_length); |
| 2612 | } |
| 2613 | break; |
| 2614 | } |
| 2615 | case SUBJECT_CAPTURE: { |
| 2616 | int capture = part.data; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2617 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2618 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2619 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2620 | if (from >= 0 && to > from) { |
| 2621 | builder->AddSubjectSlice(from, to); |
| 2622 | } |
| 2623 | break; |
| 2624 | } |
| 2625 | case REPLACEMENT_SUBSTRING: |
| 2626 | case REPLACEMENT_STRING: |
| 2627 | builder->AddString(replacement_substrings_[part.data]); |
| 2628 | break; |
| 2629 | default: |
| 2630 | UNREACHABLE(); |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | |
| 2636 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2637 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2638 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2639 | String* subject, |
| 2640 | JSRegExp* regexp, |
| 2641 | String* replacement, |
| 2642 | JSArray* last_match_info) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2643 | ASSERT(subject->IsFlat()); |
| 2644 | ASSERT(replacement->IsFlat()); |
| 2645 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2646 | HandleScope handles(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2647 | |
| 2648 | int length = subject->length(); |
| 2649 | Handle<String> subject_handle(subject); |
| 2650 | Handle<JSRegExp> regexp_handle(regexp); |
| 2651 | Handle<String> replacement_handle(replacement); |
| 2652 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2653 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2654 | subject_handle, |
| 2655 | 0, |
| 2656 | last_match_info_handle); |
| 2657 | if (match.is_null()) { |
| 2658 | return Failure::Exception(); |
| 2659 | } |
| 2660 | if (match->IsNull()) { |
| 2661 | return *subject_handle; |
| 2662 | } |
| 2663 | |
| 2664 | int capture_count = regexp_handle->CaptureCount(); |
| 2665 | |
| 2666 | // CompiledReplacement uses zone allocation. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2667 | CompilationZoneScope zone(DELETE_ON_EXIT); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2668 | CompiledReplacement compiled_replacement; |
| 2669 | compiled_replacement.Compile(replacement_handle, |
| 2670 | capture_count, |
| 2671 | length); |
| 2672 | |
| 2673 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 2674 | |
| 2675 | // Guessing the number of parts that the final result string is built |
| 2676 | // from. Global regexps can match any number of times, so we guess |
| 2677 | // conservatively. |
| 2678 | int expected_parts = |
| 2679 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2680 | ReplacementStringBuilder builder(isolate->heap(), |
| 2681 | subject_handle, |
| 2682 | expected_parts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2683 | |
| 2684 | // Index of end of last match. |
| 2685 | int prev = 0; |
| 2686 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2687 | // Number of parts added by compiled replacement plus preceeding |
| 2688 | // string and possibly suffix after last match. It is possible for |
| 2689 | // all components to use two elements when encoded as two smis. |
| 2690 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2691 | bool matched = true; |
| 2692 | do { |
| 2693 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2694 | // Increase the capacity of the builder before entering local handle-scope, |
| 2695 | // so its internal buffer can safely allocate a new handle if it grows. |
| 2696 | builder.EnsureCapacity(parts_added_per_loop); |
| 2697 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2698 | HandleScope loop_scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2699 | int start, end; |
| 2700 | { |
| 2701 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2702 | FixedArray* match_info_array = |
| 2703 | FixedArray::cast(last_match_info_handle->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2704 | |
| 2705 | ASSERT_EQ(capture_count * 2 + 2, |
| 2706 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 2707 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2708 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2709 | } |
| 2710 | |
| 2711 | if (prev < start) { |
| 2712 | builder.AddSubjectSlice(prev, start); |
| 2713 | } |
| 2714 | compiled_replacement.Apply(&builder, |
| 2715 | start, |
| 2716 | end, |
| 2717 | last_match_info_handle); |
| 2718 | prev = end; |
| 2719 | |
| 2720 | // Only continue checking for global regexps. |
| 2721 | if (!is_global) break; |
| 2722 | |
| 2723 | // Continue from where the match ended, unless it was an empty match. |
| 2724 | int next = end; |
| 2725 | if (start == end) { |
| 2726 | next = end + 1; |
| 2727 | if (next > length) break; |
| 2728 | } |
| 2729 | |
| 2730 | match = RegExpImpl::Exec(regexp_handle, |
| 2731 | subject_handle, |
| 2732 | next, |
| 2733 | last_match_info_handle); |
| 2734 | if (match.is_null()) { |
| 2735 | return Failure::Exception(); |
| 2736 | } |
| 2737 | matched = !match->IsNull(); |
| 2738 | } while (matched); |
| 2739 | |
| 2740 | if (prev < length) { |
| 2741 | builder.AddSubjectSlice(prev, length); |
| 2742 | } |
| 2743 | |
| 2744 | return *(builder.ToString()); |
| 2745 | } |
| 2746 | |
| 2747 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2748 | template <typename ResultSeqString> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2749 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2750 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2751 | String* subject, |
| 2752 | JSRegExp* regexp, |
| 2753 | JSArray* last_match_info) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2754 | ASSERT(subject->IsFlat()); |
| 2755 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2756 | HandleScope handles(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2757 | |
| 2758 | Handle<String> subject_handle(subject); |
| 2759 | Handle<JSRegExp> regexp_handle(regexp); |
| 2760 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2761 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2762 | subject_handle, |
| 2763 | 0, |
| 2764 | last_match_info_handle); |
| 2765 | if (match.is_null()) return Failure::Exception(); |
| 2766 | if (match->IsNull()) return *subject_handle; |
| 2767 | |
| 2768 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2769 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2770 | int start, end; |
| 2771 | { |
| 2772 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 2773 | FixedArray* match_info_array = |
| 2774 | FixedArray::cast(last_match_info_handle->elements()); |
| 2775 | |
| 2776 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2777 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2778 | } |
| 2779 | |
| 2780 | int length = subject->length(); |
| 2781 | int new_length = length - (end - start); |
| 2782 | if (new_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2783 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2784 | } |
| 2785 | Handle<ResultSeqString> answer; |
| 2786 | if (ResultSeqString::kHasAsciiEncoding) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2787 | answer = Handle<ResultSeqString>::cast( |
| 2788 | isolate->factory()->NewRawAsciiString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2789 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2790 | answer = Handle<ResultSeqString>::cast( |
| 2791 | isolate->factory()->NewRawTwoByteString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | // If the regexp isn't global, only match once. |
| 2795 | if (!regexp_handle->GetFlags().is_global()) { |
| 2796 | if (start > 0) { |
| 2797 | String::WriteToFlat(*subject_handle, |
| 2798 | answer->GetChars(), |
| 2799 | 0, |
| 2800 | start); |
| 2801 | } |
| 2802 | if (end < length) { |
| 2803 | String::WriteToFlat(*subject_handle, |
| 2804 | answer->GetChars() + start, |
| 2805 | end, |
| 2806 | length); |
| 2807 | } |
| 2808 | return *answer; |
| 2809 | } |
| 2810 | |
| 2811 | int prev = 0; // Index of end of last match. |
| 2812 | int next = 0; // Start of next search (prev unless last match was empty). |
| 2813 | int position = 0; |
| 2814 | |
| 2815 | do { |
| 2816 | if (prev < start) { |
| 2817 | // Add substring subject[prev;start] to answer string. |
| 2818 | String::WriteToFlat(*subject_handle, |
| 2819 | answer->GetChars() + position, |
| 2820 | prev, |
| 2821 | start); |
| 2822 | position += start - prev; |
| 2823 | } |
| 2824 | prev = end; |
| 2825 | next = end; |
| 2826 | // Continue from where the match ended, unless it was an empty match. |
| 2827 | if (start == end) { |
| 2828 | next++; |
| 2829 | if (next > length) break; |
| 2830 | } |
| 2831 | match = RegExpImpl::Exec(regexp_handle, |
| 2832 | subject_handle, |
| 2833 | next, |
| 2834 | last_match_info_handle); |
| 2835 | if (match.is_null()) return Failure::Exception(); |
| 2836 | if (match->IsNull()) break; |
| 2837 | |
| 2838 | ASSERT(last_match_info_handle->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2839 | HandleScope loop_scope(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2840 | { |
| 2841 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 2842 | FixedArray* match_info_array = |
| 2843 | FixedArray::cast(last_match_info_handle->elements()); |
| 2844 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2845 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2846 | } |
| 2847 | } while (true); |
| 2848 | |
| 2849 | if (prev < length) { |
| 2850 | // Add substring subject[prev;length] to answer string. |
| 2851 | String::WriteToFlat(*subject_handle, |
| 2852 | answer->GetChars() + position, |
| 2853 | prev, |
| 2854 | length); |
| 2855 | position += length - prev; |
| 2856 | } |
| 2857 | |
| 2858 | if (position == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2859 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | // Shorten string and fill |
| 2863 | int string_size = ResultSeqString::SizeFor(position); |
| 2864 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 2865 | int delta = allocated_string_size - string_size; |
| 2866 | |
| 2867 | answer->set_length(position); |
| 2868 | if (delta == 0) return *answer; |
| 2869 | |
| 2870 | Address end_of_string = answer->address() + string_size; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2871 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2872 | |
| 2873 | return *answer; |
| 2874 | } |
| 2875 | |
| 2876 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2877 | static MaybeObject* Runtime_StringReplaceRegExpWithString( |
| 2878 | RUNTIME_CALLING_CONVENTION) { |
| 2879 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2880 | ASSERT(args.length() == 4); |
| 2881 | |
| 2882 | CONVERT_CHECKED(String, subject, args[0]); |
| 2883 | if (!subject->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2884 | Object* flat_subject; |
| 2885 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 2886 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 2887 | return maybe_flat_subject; |
| 2888 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2889 | } |
| 2890 | subject = String::cast(flat_subject); |
| 2891 | } |
| 2892 | |
| 2893 | CONVERT_CHECKED(String, replacement, args[2]); |
| 2894 | if (!replacement->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2895 | Object* flat_replacement; |
| 2896 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 2897 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 2898 | return maybe_flat_replacement; |
| 2899 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2900 | } |
| 2901 | replacement = String::cast(flat_replacement); |
| 2902 | } |
| 2903 | |
| 2904 | CONVERT_CHECKED(JSRegExp, regexp, args[1]); |
| 2905 | CONVERT_CHECKED(JSArray, last_match_info, args[3]); |
| 2906 | |
| 2907 | ASSERT(last_match_info->HasFastElements()); |
| 2908 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2909 | if (replacement->length() == 0) { |
| 2910 | if (subject->HasOnlyAsciiChars()) { |
| 2911 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2912 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2913 | } else { |
| 2914 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2915 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2919 | return StringReplaceRegExpWithString(isolate, |
| 2920 | subject, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2921 | regexp, |
| 2922 | replacement, |
| 2923 | last_match_info); |
| 2924 | } |
| 2925 | |
| 2926 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2927 | // Perform string match of pattern on subject, starting at start index. |
| 2928 | // Caller must ensure that 0 <= start_index <= sub->length(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2929 | // and should check that pat->length() + start_index <= sub->length(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2930 | int Runtime::StringMatch(Isolate* isolate, |
| 2931 | Handle<String> sub, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2932 | Handle<String> pat, |
| 2933 | int start_index) { |
| 2934 | ASSERT(0 <= start_index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2935 | ASSERT(start_index <= sub->length()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2936 | |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 2937 | int pattern_length = pat->length(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2938 | if (pattern_length == 0) return start_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2939 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2940 | int subject_length = sub->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2941 | if (start_index + pattern_length > subject_length) return -1; |
| 2942 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2943 | if (!sub->IsFlat()) FlattenString(sub); |
| 2944 | if (!pat->IsFlat()) FlattenString(pat); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2945 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2946 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2947 | // Extract flattened substrings of cons strings before determining asciiness. |
| 2948 | String* seq_sub = *sub; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2949 | if (seq_sub->IsConsString()) seq_sub = ConsString::cast(seq_sub)->first(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2950 | String* seq_pat = *pat; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2951 | if (seq_pat->IsConsString()) seq_pat = ConsString::cast(seq_pat)->first(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2952 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2953 | // dispatch on type of strings |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2954 | if (seq_pat->IsAsciiRepresentation()) { |
| 2955 | Vector<const char> pat_vector = seq_pat->ToAsciiVector(); |
| 2956 | if (seq_sub->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2957 | return SearchString(isolate, |
| 2958 | seq_sub->ToAsciiVector(), |
| 2959 | pat_vector, |
| 2960 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2961 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2962 | return SearchString(isolate, |
| 2963 | seq_sub->ToUC16Vector(), |
| 2964 | pat_vector, |
| 2965 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2966 | } |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2967 | Vector<const uc16> pat_vector = seq_pat->ToUC16Vector(); |
| 2968 | if (seq_sub->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2969 | return SearchString(isolate, |
| 2970 | seq_sub->ToAsciiVector(), |
| 2971 | pat_vector, |
| 2972 | start_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2973 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2974 | return SearchString(isolate, |
| 2975 | seq_sub->ToUC16Vector(), |
| 2976 | pat_vector, |
| 2977 | start_index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2981 | static MaybeObject* Runtime_StringIndexOf(RUNTIME_CALLING_CONVENTION) { |
| 2982 | RUNTIME_GET_ISOLATE; |
| 2983 | HandleScope scope(isolate); // create a new handle scope |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2984 | ASSERT(args.length() == 3); |
| 2985 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2986 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 2987 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 2988 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2989 | Object* index = args[2]; |
| 2990 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2991 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2992 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2993 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 2994 | int position = |
| 2995 | Runtime::StringMatch(isolate, sub, pat, start_index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2996 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3000 | template <typename schar, typename pchar> |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3001 | static int StringMatchBackwards(Vector<const schar> subject, |
| 3002 | Vector<const pchar> pattern, |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3003 | int idx) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3004 | int pattern_length = pattern.length(); |
| 3005 | ASSERT(pattern_length >= 1); |
| 3006 | ASSERT(idx + pattern_length <= subject.length()); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3007 | |
| 3008 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3009 | for (int i = 0; i < pattern_length; i++) { |
| 3010 | uc16 c = pattern[i]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3011 | if (c > String::kMaxAsciiCharCode) { |
| 3012 | return -1; |
| 3013 | } |
| 3014 | } |
| 3015 | } |
| 3016 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3017 | pchar pattern_first_char = pattern[0]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3018 | for (int i = idx; i >= 0; i--) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3019 | if (subject[i] != pattern_first_char) continue; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3020 | int j = 1; |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3021 | while (j < pattern_length) { |
| 3022 | if (pattern[j] != subject[i+j]) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3023 | break; |
| 3024 | } |
| 3025 | j++; |
| 3026 | } |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3027 | if (j == pattern_length) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3028 | return i; |
| 3029 | } |
| 3030 | } |
| 3031 | return -1; |
| 3032 | } |
| 3033 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3034 | static MaybeObject* Runtime_StringLastIndexOf(RUNTIME_CALLING_CONVENTION) { |
| 3035 | RUNTIME_GET_ISOLATE; |
| 3036 | HandleScope scope(isolate); // create a new handle scope |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3037 | ASSERT(args.length() == 3); |
| 3038 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3039 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 3040 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 3041 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3042 | Object* index = args[2]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3043 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3044 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3045 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3046 | uint32_t pat_length = pat->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3047 | uint32_t sub_length = sub->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3048 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3049 | if (start_index + pat_length > sub_length) { |
| 3050 | start_index = sub_length - pat_length; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 3051 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3052 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3053 | if (pat_length == 0) { |
| 3054 | return Smi::FromInt(start_index); |
| 3055 | } |
| 3056 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3057 | if (!sub->IsFlat()) FlattenString(sub); |
| 3058 | if (!pat->IsFlat()) FlattenString(pat); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3059 | |
| 3060 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3061 | |
| 3062 | int position = -1; |
| 3063 | |
| 3064 | if (pat->IsAsciiRepresentation()) { |
| 3065 | Vector<const char> pat_vector = pat->ToAsciiVector(); |
| 3066 | if (sub->IsAsciiRepresentation()) { |
| 3067 | position = StringMatchBackwards(sub->ToAsciiVector(), |
| 3068 | pat_vector, |
| 3069 | start_index); |
| 3070 | } else { |
| 3071 | position = StringMatchBackwards(sub->ToUC16Vector(), |
| 3072 | pat_vector, |
| 3073 | start_index); |
| 3074 | } |
| 3075 | } else { |
| 3076 | Vector<const uc16> pat_vector = pat->ToUC16Vector(); |
| 3077 | if (sub->IsAsciiRepresentation()) { |
| 3078 | position = StringMatchBackwards(sub->ToAsciiVector(), |
| 3079 | pat_vector, |
| 3080 | start_index); |
| 3081 | } else { |
| 3082 | position = StringMatchBackwards(sub->ToUC16Vector(), |
| 3083 | pat_vector, |
| 3084 | start_index); |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3092 | static MaybeObject* Runtime_StringLocaleCompare(RUNTIME_CALLING_CONVENTION) { |
| 3093 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3094 | NoHandleAllocation ha; |
| 3095 | ASSERT(args.length() == 2); |
| 3096 | |
| 3097 | CONVERT_CHECKED(String, str1, args[0]); |
| 3098 | CONVERT_CHECKED(String, str2, args[1]); |
| 3099 | |
| 3100 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3101 | int str1_length = str1->length(); |
| 3102 | int str2_length = str2->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3103 | |
| 3104 | // Decide trivial cases without flattening. |
| 3105 | if (str1_length == 0) { |
| 3106 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3107 | return Smi::FromInt(-str2_length); |
| 3108 | } else { |
| 3109 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3110 | } |
| 3111 | |
| 3112 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3113 | |
| 3114 | // No need to flatten if we are going to find the answer on the first |
| 3115 | // character. At this point we know there is at least one character |
| 3116 | // in each string, due to the trivial case handling above. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3117 | int d = str1->Get(0) - str2->Get(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3118 | if (d != 0) return Smi::FromInt(d); |
| 3119 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3120 | str1->TryFlatten(); |
| 3121 | str2->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3122 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3123 | StringInputBuffer& buf1 = |
| 3124 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3125 | StringInputBuffer& buf2 = |
| 3126 | *isolate->runtime_state()->string_locale_compare_buf2(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3127 | |
| 3128 | buf1.Reset(str1); |
| 3129 | buf2.Reset(str2); |
| 3130 | |
| 3131 | for (int i = 0; i < end; i++) { |
| 3132 | uint16_t char1 = buf1.GetNext(); |
| 3133 | uint16_t char2 = buf2.GetNext(); |
| 3134 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3135 | } |
| 3136 | |
| 3137 | return Smi::FromInt(str1_length - str2_length); |
| 3138 | } |
| 3139 | |
| 3140 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3141 | static MaybeObject* Runtime_SubString(RUNTIME_CALLING_CONVENTION) { |
| 3142 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3143 | NoHandleAllocation ha; |
| 3144 | ASSERT(args.length() == 3); |
| 3145 | |
| 3146 | CONVERT_CHECKED(String, value, args[0]); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3147 | Object* from = args[1]; |
| 3148 | Object* to = args[2]; |
| 3149 | int start, end; |
| 3150 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3151 | // the common case where from and to are Smis. |
| 3152 | if (from->IsSmi() && to->IsSmi()) { |
| 3153 | start = Smi::cast(from)->value(); |
| 3154 | end = Smi::cast(to)->value(); |
| 3155 | } else { |
| 3156 | CONVERT_DOUBLE_CHECKED(from_number, from); |
| 3157 | CONVERT_DOUBLE_CHECKED(to_number, to); |
| 3158 | start = FastD2I(from_number); |
| 3159 | end = FastD2I(to_number); |
| 3160 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3161 | RUNTIME_ASSERT(end >= start); |
| 3162 | RUNTIME_ASSERT(start >= 0); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3163 | RUNTIME_ASSERT(end <= value->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3164 | isolate->counters()->sub_string_runtime()->Increment(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3165 | return value->SubString(start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
| 3168 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3169 | static MaybeObject* Runtime_StringMatch(RUNTIME_CALLING_CONVENTION) { |
| 3170 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3171 | ASSERT_EQ(3, args.length()); |
| 3172 | |
| 3173 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 3174 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3175 | CONVERT_ARG_CHECKED(JSArray, regexp_info, 2); |
| 3176 | HandleScope handles; |
| 3177 | |
| 3178 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3179 | |
| 3180 | if (match.is_null()) { |
| 3181 | return Failure::Exception(); |
| 3182 | } |
| 3183 | if (match->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3184 | return isolate->heap()->null_value(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3185 | } |
| 3186 | int length = subject->length(); |
| 3187 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3188 | CompilationZoneScope zone_space(DELETE_ON_EXIT); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3189 | ZoneList<int> offsets(8); |
| 3190 | do { |
| 3191 | int start; |
| 3192 | int end; |
| 3193 | { |
| 3194 | AssertNoAllocation no_alloc; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3195 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3196 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3197 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3198 | } |
| 3199 | offsets.Add(start); |
| 3200 | offsets.Add(end); |
| 3201 | int index = start < end ? end : end + 1; |
| 3202 | if (index > length) break; |
| 3203 | match = RegExpImpl::Exec(regexp, subject, index, regexp_info); |
| 3204 | if (match.is_null()) { |
| 3205 | return Failure::Exception(); |
| 3206 | } |
| 3207 | } while (!match->IsNull()); |
| 3208 | int matches = offsets.length() / 2; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3209 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3210 | for (int i = 0; i < matches ; i++) { |
| 3211 | int from = offsets.at(i * 2); |
| 3212 | int to = offsets.at(i * 2 + 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3213 | Handle<String> match = isolate->factory()->NewSubString(subject, from, to); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3214 | elements->set(i, *match); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3215 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3216 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3217 | result->set_length(Smi::FromInt(matches)); |
| 3218 | return *result; |
| 3219 | } |
| 3220 | |
| 3221 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3222 | // Two smis before and after the match, for very long strings. |
| 3223 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3224 | |
| 3225 | |
| 3226 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3227 | Handle<JSArray> last_match_info, |
| 3228 | int match_start, |
| 3229 | int match_end) { |
| 3230 | // Fill last_match_info with a single capture. |
| 3231 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3232 | AssertNoAllocation no_gc; |
| 3233 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3234 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3235 | RegExpImpl::SetLastInput(elements, *subject); |
| 3236 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3237 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3238 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3239 | } |
| 3240 | |
| 3241 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3242 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3243 | static bool SearchStringMultiple(Isolate* isolate, |
| 3244 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3245 | Vector<const PatternChar> pattern, |
| 3246 | String* pattern_string, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3247 | FixedArrayBuilder* builder, |
| 3248 | int* match_pos) { |
| 3249 | int pos = *match_pos; |
| 3250 | int subject_length = subject.length(); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3251 | int pattern_length = pattern.length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3252 | int max_search_start = subject_length - pattern_length; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3253 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3254 | while (pos <= max_search_start) { |
| 3255 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3256 | *match_pos = pos; |
| 3257 | return false; |
| 3258 | } |
| 3259 | // Position of end of previous match. |
| 3260 | int match_end = pos + pattern_length; |
| 3261 | int new_pos = search.Search(subject, match_end); |
| 3262 | if (new_pos >= 0) { |
| 3263 | // A match. |
| 3264 | if (new_pos > match_end) { |
| 3265 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3266 | match_end, |
| 3267 | new_pos); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3268 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3269 | pos = new_pos; |
| 3270 | builder->Add(pattern_string); |
| 3271 | } else { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3272 | break; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3273 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3274 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3275 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3276 | if (pos < max_search_start) { |
| 3277 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3278 | pos + pattern_length, |
| 3279 | subject_length); |
| 3280 | } |
| 3281 | *match_pos = pos; |
| 3282 | return true; |
| 3283 | } |
| 3284 | |
| 3285 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3286 | static bool SearchStringMultiple(Isolate* isolate, |
| 3287 | Handle<String> subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3288 | Handle<String> pattern, |
| 3289 | Handle<JSArray> last_match_info, |
| 3290 | FixedArrayBuilder* builder) { |
| 3291 | ASSERT(subject->IsFlat()); |
| 3292 | ASSERT(pattern->IsFlat()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Treating as if a previous match was before first character. |
| 3295 | int match_pos = -pattern->length(); |
| 3296 | |
| 3297 | for (;;) { // Break when search complete. |
| 3298 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3299 | AssertNoAllocation no_gc; |
| 3300 | if (subject->IsAsciiRepresentation()) { |
| 3301 | Vector<const char> subject_vector = subject->ToAsciiVector(); |
| 3302 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3303 | if (SearchStringMultiple(isolate, |
| 3304 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3305 | pattern->ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3306 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3307 | builder, |
| 3308 | &match_pos)) break; |
| 3309 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3310 | if (SearchStringMultiple(isolate, |
| 3311 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3312 | pattern->ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3313 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3314 | builder, |
| 3315 | &match_pos)) break; |
| 3316 | } |
| 3317 | } else { |
| 3318 | Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
| 3319 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3320 | if (SearchStringMultiple(isolate, |
| 3321 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3322 | pattern->ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3323 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3324 | builder, |
| 3325 | &match_pos)) break; |
| 3326 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3327 | if (SearchStringMultiple(isolate, |
| 3328 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3329 | pattern->ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3330 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3331 | builder, |
| 3332 | &match_pos)) break; |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | if (match_pos >= 0) { |
| 3338 | SetLastMatchInfoNoCaptures(subject, |
| 3339 | last_match_info, |
| 3340 | match_pos, |
| 3341 | match_pos + pattern->length()); |
| 3342 | return true; |
| 3343 | } |
| 3344 | return false; // No matches at all. |
| 3345 | } |
| 3346 | |
| 3347 | |
| 3348 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3349 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3350 | Handle<String> subject, |
| 3351 | Handle<JSRegExp> regexp, |
| 3352 | Handle<JSArray> last_match_array, |
| 3353 | FixedArrayBuilder* builder) { |
| 3354 | ASSERT(subject->IsFlat()); |
| 3355 | int match_start = -1; |
| 3356 | int match_end = 0; |
| 3357 | int pos = 0; |
| 3358 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3359 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3360 | |
| 3361 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3362 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3363 | int subject_length = subject->length(); |
| 3364 | |
| 3365 | for (;;) { // Break on failure, return on exception. |
| 3366 | RegExpImpl::IrregexpResult result = |
| 3367 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3368 | subject, |
| 3369 | pos, |
| 3370 | register_vector); |
| 3371 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3372 | match_start = register_vector[0]; |
| 3373 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3374 | if (match_end < match_start) { |
| 3375 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3376 | match_end, |
| 3377 | match_start); |
| 3378 | } |
| 3379 | match_end = register_vector[1]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3380 | HandleScope loop_scope(isolate); |
| 3381 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3382 | match_start, |
| 3383 | match_end)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3384 | if (match_start != match_end) { |
| 3385 | pos = match_end; |
| 3386 | } else { |
| 3387 | pos = match_end + 1; |
| 3388 | if (pos > subject_length) break; |
| 3389 | } |
| 3390 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3391 | break; |
| 3392 | } else { |
| 3393 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3394 | return result; |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | if (match_start >= 0) { |
| 3399 | if (match_end < subject_length) { |
| 3400 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3401 | match_end, |
| 3402 | subject_length); |
| 3403 | } |
| 3404 | SetLastMatchInfoNoCaptures(subject, |
| 3405 | last_match_array, |
| 3406 | match_start, |
| 3407 | match_end); |
| 3408 | return RegExpImpl::RE_SUCCESS; |
| 3409 | } else { |
| 3410 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3411 | } |
| 3412 | } |
| 3413 | |
| 3414 | |
| 3415 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3416 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3417 | Handle<String> subject, |
| 3418 | Handle<JSRegExp> regexp, |
| 3419 | Handle<JSArray> last_match_array, |
| 3420 | FixedArrayBuilder* builder) { |
| 3421 | |
| 3422 | ASSERT(subject->IsFlat()); |
| 3423 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3424 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3425 | |
| 3426 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3427 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3428 | |
| 3429 | RegExpImpl::IrregexpResult result = |
| 3430 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3431 | subject, |
| 3432 | 0, |
| 3433 | register_vector); |
| 3434 | |
| 3435 | int capture_count = regexp->CaptureCount(); |
| 3436 | int subject_length = subject->length(); |
| 3437 | |
| 3438 | // Position to search from. |
| 3439 | int pos = 0; |
| 3440 | // End of previous match. Differs from pos if match was empty. |
| 3441 | int match_end = 0; |
| 3442 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3443 | // Need to keep a copy of the previous match for creating last_match_info |
| 3444 | // at the end, so we have two vectors that we swap between. |
| 3445 | OffsetsVector registers2(required_registers); |
| 3446 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
| 3447 | |
| 3448 | do { |
| 3449 | int match_start = register_vector[0]; |
| 3450 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3451 | if (match_end < match_start) { |
| 3452 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3453 | match_end, |
| 3454 | match_start); |
| 3455 | } |
| 3456 | match_end = register_vector[1]; |
| 3457 | |
| 3458 | { |
| 3459 | // Avoid accumulating new handles inside loop. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3460 | HandleScope temp_scope(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3461 | // Arguments array to replace function is match, captures, index and |
| 3462 | // subject, i.e., 3 + capture count in total. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3463 | Handle<FixedArray> elements = |
| 3464 | isolate->factory()->NewFixedArray(3 + capture_count); |
| 3465 | Handle<String> match = isolate->factory()->NewSubString(subject, |
| 3466 | match_start, |
| 3467 | match_end); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3468 | elements->set(0, *match); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3469 | for (int i = 1; i <= capture_count; i++) { |
| 3470 | int start = register_vector[i * 2]; |
| 3471 | if (start >= 0) { |
| 3472 | int end = register_vector[i * 2 + 1]; |
| 3473 | ASSERT(start <= end); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3474 | Handle<String> substring = isolate->factory()->NewSubString(subject, |
| 3475 | start, |
| 3476 | end); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3477 | elements->set(i, *substring); |
| 3478 | } else { |
| 3479 | ASSERT(register_vector[i * 2 + 1] < 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3480 | elements->set(i, isolate->heap()->undefined_value()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3481 | } |
| 3482 | } |
| 3483 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3484 | elements->set(capture_count + 2, *subject); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3485 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3486 | } |
| 3487 | // Swap register vectors, so the last successful match is in |
| 3488 | // prev_register_vector. |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3489 | Vector<int32_t> tmp = prev_register_vector; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3490 | prev_register_vector = register_vector; |
| 3491 | register_vector = tmp; |
| 3492 | |
| 3493 | if (match_end > match_start) { |
| 3494 | pos = match_end; |
| 3495 | } else { |
| 3496 | pos = match_end + 1; |
| 3497 | if (pos > subject_length) { |
| 3498 | break; |
| 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3503 | subject, |
| 3504 | pos, |
| 3505 | register_vector); |
| 3506 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3507 | |
| 3508 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3509 | // Finished matching, with at least one match. |
| 3510 | if (match_end < subject_length) { |
| 3511 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3512 | match_end, |
| 3513 | subject_length); |
| 3514 | } |
| 3515 | |
| 3516 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3517 | int last_match_array_size = |
| 3518 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3519 | last_match_array->EnsureSize(last_match_array_size); |
| 3520 | AssertNoAllocation no_gc; |
| 3521 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3522 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3523 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3524 | RegExpImpl::SetLastInput(elements, *subject); |
| 3525 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3526 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3527 | } |
| 3528 | return RegExpImpl::RE_SUCCESS; |
| 3529 | } |
| 3530 | } |
| 3531 | // No matches at all, return failure or exception result directly. |
| 3532 | return result; |
| 3533 | } |
| 3534 | |
| 3535 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3536 | static MaybeObject* Runtime_RegExpExecMultiple(RUNTIME_CALLING_CONVENTION) { |
| 3537 | RUNTIME_GET_ISOLATE; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3538 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3539 | HandleScope handles(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3540 | |
| 3541 | CONVERT_ARG_CHECKED(String, subject, 1); |
| 3542 | if (!subject->IsFlat()) { FlattenString(subject); } |
| 3543 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 3544 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 2); |
| 3545 | CONVERT_ARG_CHECKED(JSArray, result_array, 3); |
| 3546 | |
| 3547 | ASSERT(last_match_info->HasFastElements()); |
| 3548 | ASSERT(regexp->GetFlags().is_global()); |
| 3549 | Handle<FixedArray> result_elements; |
| 3550 | if (result_array->HasFastElements()) { |
| 3551 | result_elements = |
| 3552 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
| 3553 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3554 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3555 | } |
| 3556 | FixedArrayBuilder builder(result_elements); |
| 3557 | |
| 3558 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 3559 | Handle<String> pattern( |
| 3560 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3561 | ASSERT(pattern->IsFlat()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3562 | if (SearchStringMultiple(isolate, subject, pattern, |
| 3563 | last_match_info, &builder)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3564 | return *builder.ToJSArray(result_array); |
| 3565 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3566 | return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 3570 | |
| 3571 | RegExpImpl::IrregexpResult result; |
| 3572 | if (regexp->CaptureCount() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3573 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 3574 | subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3575 | regexp, |
| 3576 | last_match_info, |
| 3577 | &builder); |
| 3578 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3579 | result = SearchRegExpMultiple(isolate, |
| 3580 | subject, |
| 3581 | regexp, |
| 3582 | last_match_info, |
| 3583 | &builder); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3584 | } |
| 3585 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3586 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3587 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3588 | return Failure::Exception(); |
| 3589 | } |
| 3590 | |
| 3591 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3592 | static MaybeObject* Runtime_NumberToRadixString(RUNTIME_CALLING_CONVENTION) { |
| 3593 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | NoHandleAllocation ha; |
| 3595 | ASSERT(args.length() == 2); |
| 3596 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3597 | // Fast case where the result is a one character string. |
| 3598 | if (args[0]->IsSmi() && args[1]->IsSmi()) { |
| 3599 | int value = Smi::cast(args[0])->value(); |
| 3600 | int radix = Smi::cast(args[1])->value(); |
| 3601 | if (value >= 0 && value < radix) { |
| 3602 | RUNTIME_ASSERT(radix <= 36); |
| 3603 | // Character array used for conversion. |
| 3604 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3605 | return isolate->heap()-> |
| 3606 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | // Slow case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3611 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3612 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3613 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3614 | } |
| 3615 | if (isinf(value)) { |
| 3616 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3617 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3618 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3619 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3620 | } |
| 3621 | CONVERT_DOUBLE_CHECKED(radix_number, args[1]); |
| 3622 | int radix = FastD2I(radix_number); |
| 3623 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
| 3624 | char* str = DoubleToRadixCString(value, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3625 | MaybeObject* result = |
| 3626 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3627 | DeleteArray(str); |
| 3628 | return result; |
| 3629 | } |
| 3630 | |
| 3631 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3632 | static MaybeObject* Runtime_NumberToFixed(RUNTIME_CALLING_CONVENTION) { |
| 3633 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3634 | NoHandleAllocation ha; |
| 3635 | ASSERT(args.length() == 2); |
| 3636 | |
| 3637 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3638 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3639 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3640 | } |
| 3641 | if (isinf(value)) { |
| 3642 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3643 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3644 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3645 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3646 | } |
| 3647 | CONVERT_DOUBLE_CHECKED(f_number, args[1]); |
| 3648 | int f = FastD2I(f_number); |
| 3649 | RUNTIME_ASSERT(f >= 0); |
| 3650 | char* str = DoubleToFixedCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3651 | MaybeObject* res = |
| 3652 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3653 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3654 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3658 | static MaybeObject* Runtime_NumberToExponential(RUNTIME_CALLING_CONVENTION) { |
| 3659 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3660 | NoHandleAllocation ha; |
| 3661 | ASSERT(args.length() == 2); |
| 3662 | |
| 3663 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3664 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3665 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3666 | } |
| 3667 | if (isinf(value)) { |
| 3668 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3669 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3670 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3671 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3672 | } |
| 3673 | CONVERT_DOUBLE_CHECKED(f_number, args[1]); |
| 3674 | int f = FastD2I(f_number); |
| 3675 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 3676 | char* str = DoubleToExponentialCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3677 | MaybeObject* res = |
| 3678 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3679 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3680 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3684 | static MaybeObject* Runtime_NumberToPrecision(RUNTIME_CALLING_CONVENTION) { |
| 3685 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3686 | NoHandleAllocation ha; |
| 3687 | ASSERT(args.length() == 2); |
| 3688 | |
| 3689 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3690 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3691 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3692 | } |
| 3693 | if (isinf(value)) { |
| 3694 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3695 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3696 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3697 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3698 | } |
| 3699 | CONVERT_DOUBLE_CHECKED(f_number, args[1]); |
| 3700 | int f = FastD2I(f_number); |
| 3701 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 3702 | char* str = DoubleToPrecisionCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3703 | MaybeObject* res = |
| 3704 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3705 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3706 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | |
| 3710 | // Returns a single character string where first character equals |
| 3711 | // string->Get(index). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3712 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3713 | if (index < static_cast<uint32_t>(string->length())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3714 | string->TryFlatten(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3715 | return LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3716 | string->Get(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3717 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3718 | return Execution::CharAt(string, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3722 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 3723 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3724 | uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3725 | // Handle [] indexing on Strings |
| 3726 | if (object->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3727 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 3728 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | // Handle [] indexing on String objects |
| 3732 | if (object->IsStringObjectWithCharacterAt(index)) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3733 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 3734 | Handle<Object> result = |
| 3735 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 3736 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3740 | Handle<Object> prototype = GetPrototype(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3741 | return prototype->GetElement(index); |
| 3742 | } |
| 3743 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3744 | return GetElement(object, index); |
| 3745 | } |
| 3746 | |
| 3747 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3748 | MaybeObject* Runtime::GetElement(Handle<Object> object, uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3749 | return object->GetElement(index); |
| 3750 | } |
| 3751 | |
| 3752 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3753 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 3754 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3755 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3756 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3757 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3758 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3759 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3760 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3761 | isolate->factory()->NewTypeError("non_object_property_load", |
| 3762 | HandleVector(args, 2)); |
| 3763 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | // Check if the given key is an array index. |
| 3767 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3768 | if (key->ToArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3769 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | // Convert the key to a string - possibly by calling back into JavaScript. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3773 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3774 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3775 | name = Handle<String>::cast(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3776 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3777 | bool has_pending_exception = false; |
| 3778 | Handle<Object> converted = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3779 | Execution::ToString(key, &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3780 | if (has_pending_exception) return Failure::Exception(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3781 | name = Handle<String>::cast(converted); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3784 | // Check if the name is trivially convertible to an index and get |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3785 | // the element if so. |
| 3786 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3787 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | } else { |
| 3789 | PropertyAttributes attr; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3790 | return object->GetProperty(*name, &attr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3795 | static MaybeObject* Runtime_GetProperty(RUNTIME_CALLING_CONVENTION) { |
| 3796 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3797 | NoHandleAllocation ha; |
| 3798 | ASSERT(args.length() == 2); |
| 3799 | |
| 3800 | Handle<Object> object = args.at<Object>(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3801 | Handle<Object> key = args.at<Object>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3802 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3803 | return Runtime::GetObjectProperty(isolate, object, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3807 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3808 | static MaybeObject* Runtime_KeyedGetProperty(RUNTIME_CALLING_CONVENTION) { |
| 3809 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3810 | NoHandleAllocation ha; |
| 3811 | ASSERT(args.length() == 2); |
| 3812 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3813 | // Fast cases for getting named properties of the receiver JSObject |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3814 | // itself. |
| 3815 | // |
| 3816 | // The global proxy objects has to be excluded since LocalLookup on |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3817 | // the global proxy object can return a valid result even though the |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3818 | // global proxy object never has properties. This is the case |
| 3819 | // because the global proxy object forwards everything to its hidden |
| 3820 | // prototype including local lookups. |
| 3821 | // |
| 3822 | // Additionally, we need to make sure that we do not cache results |
| 3823 | // for objects that require access checks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3824 | if (args[0]->IsJSObject() && |
| 3825 | !args[0]->IsJSGlobalProxy() && |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3826 | !args[0]->IsAccessCheckNeeded() && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3827 | args[1]->IsString()) { |
| 3828 | JSObject* receiver = JSObject::cast(args[0]); |
| 3829 | String* key = String::cast(args[1]); |
| 3830 | if (receiver->HasFastProperties()) { |
| 3831 | // Attempt to use lookup cache. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3832 | Map* receiver_map = receiver->map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3833 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 3834 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3835 | if (offset != -1) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3836 | Object* value = receiver->FastPropertyAt(offset); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3837 | return value->IsTheHole() ? isolate->heap()->undefined_value() : value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3838 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3839 | // Lookup cache miss. Perform lookup and update the cache if appropriate. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3840 | LookupResult result; |
| 3841 | receiver->LocalLookup(key, &result); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 3842 | if (result.IsProperty() && result.type() == FIELD) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3843 | int offset = result.GetFieldIndex(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3844 | keyed_lookup_cache->Update(receiver_map, key, offset); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3845 | return receiver->FastPropertyAt(offset); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3846 | } |
| 3847 | } else { |
| 3848 | // Attempt dictionary lookup. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3849 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 3850 | int entry = dictionary->FindEntry(key); |
| 3851 | if ((entry != StringDictionary::kNotFound) && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3852 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3853 | Object* value = dictionary->ValueAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3854 | if (!receiver->IsGlobalObject()) return value; |
| 3855 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 3856 | if (!value->IsTheHole()) return value; |
| 3857 | // If value is the hole do the general lookup. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3858 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3859 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3860 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 3861 | // Fast case for string indexing using [] with a smi index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3862 | HandleScope scope(isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3863 | Handle<String> str = args.at<String>(0); |
| 3864 | int index = Smi::cast(args[1])->value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3865 | if (index >= 0 && index < str->length()) { |
| 3866 | Handle<Object> result = GetCharAt(str, index); |
| 3867 | return *result; |
| 3868 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3869 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3870 | |
| 3871 | // Fall back to GetObjectProperty. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3872 | return Runtime::GetObjectProperty(isolate, |
| 3873 | args.at<Object>(0), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3874 | args.at<Object>(1)); |
| 3875 | } |
| 3876 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3877 | // Implements part of 8.12.9 DefineOwnProperty. |
| 3878 | // There are 3 cases that lead here: |
| 3879 | // Step 4b - define a new accessor property. |
| 3880 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 3881 | // Step 12 - update an existing accessor property with an accessor or generic |
| 3882 | // descriptor. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3883 | static MaybeObject* Runtime_DefineOrRedefineAccessorProperty( |
| 3884 | RUNTIME_CALLING_CONVENTION) { |
| 3885 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3886 | ASSERT(args.length() == 5); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3887 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3888 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 3889 | CONVERT_CHECKED(String, name, args[1]); |
| 3890 | CONVERT_CHECKED(Smi, flag_setter, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3891 | Object* fun = args[3]; |
| 3892 | RUNTIME_ASSERT(fun->IsJSFunction() || fun->IsUndefined()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3893 | CONVERT_CHECKED(Smi, flag_attr, args[4]); |
| 3894 | int unchecked = flag_attr->value(); |
| 3895 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 3896 | RUNTIME_ASSERT(!obj->IsNull()); |
| 3897 | LookupResult result; |
| 3898 | obj->LocalLookupRealNamedProperty(name, &result); |
| 3899 | |
| 3900 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 3901 | // If an existing property is either FIELD, NORMAL or CONSTANT_FUNCTION |
| 3902 | // delete it to avoid running into trouble in DefineAccessor, which |
| 3903 | // handles this incorrectly if the property is readonly (does nothing) |
| 3904 | if (result.IsProperty() && |
| 3905 | (result.type() == FIELD || result.type() == NORMAL |
| 3906 | || result.type() == CONSTANT_FUNCTION)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3907 | Object* ok; |
| 3908 | { MaybeObject* maybe_ok = |
| 3909 | obj->DeleteProperty(name, JSObject::NORMAL_DELETION); |
| 3910 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3911 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3912 | } |
| 3913 | return obj->DefineAccessor(name, flag_setter->value() == 0, fun, attr); |
| 3914 | } |
| 3915 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3916 | // Implements part of 8.12.9 DefineOwnProperty. |
| 3917 | // There are 3 cases that lead here: |
| 3918 | // Step 4a - define a new data property. |
| 3919 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 3920 | // Step 12 - update an existing data property with a data or generic |
| 3921 | // descriptor. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3922 | static MaybeObject* Runtime_DefineOrRedefineDataProperty( |
| 3923 | RUNTIME_CALLING_CONVENTION) { |
| 3924 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3925 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3926 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3927 | CONVERT_ARG_CHECKED(JSObject, js_object, 0); |
| 3928 | CONVERT_ARG_CHECKED(String, name, 1); |
| 3929 | Handle<Object> obj_value = args.at<Object>(2); |
| 3930 | |
| 3931 | CONVERT_CHECKED(Smi, flag, args[3]); |
| 3932 | int unchecked = flag->value(); |
| 3933 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 3934 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3935 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 3936 | |
| 3937 | // Check if this is an element. |
| 3938 | uint32_t index; |
| 3939 | bool is_element = name->AsArrayIndex(&index); |
| 3940 | |
| 3941 | // Special case for elements if any of the flags are true. |
| 3942 | // If elements are in fast case we always implicitly assume that: |
| 3943 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 3944 | if (((unchecked & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) && |
| 3945 | is_element) { |
| 3946 | // Normalize the elements to enable attributes on the property. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3947 | if (js_object->IsJSGlobalProxy()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3948 | // We do not need to do access checks here since these has already |
| 3949 | // been performed by the call to GetOwnProperty. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3950 | Handle<Object> proto(js_object->GetPrototype()); |
| 3951 | // If proxy is detached, ignore the assignment. Alternatively, |
| 3952 | // we could throw an exception. |
| 3953 | if (proto->IsNull()) return *obj_value; |
| 3954 | js_object = Handle<JSObject>::cast(proto); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3955 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3956 | NormalizeElements(js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3957 | Handle<NumberDictionary> dictionary(js_object->element_dictionary()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3958 | // Make sure that we never go back to fast case. |
| 3959 | dictionary->set_requires_slow_elements(); |
| 3960 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3961 | NumberDictionarySet(dictionary, index, obj_value, details); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3962 | return *obj_value; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3965 | LookupResult result; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3966 | js_object->LookupRealNamedProperty(*name, &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3967 | |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 3968 | // To be compatible with safari we do not change the value on API objects |
| 3969 | // in defineProperty. Firefox disagrees here, and actually changes the value. |
| 3970 | if (result.IsProperty() && |
| 3971 | (result.type() == CALLBACKS) && |
| 3972 | result.GetCallbackObject()->IsAccessorInfo()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3973 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3976 | // Take special care when attributes are different and there is already |
| 3977 | // a property. For simplicity we normalize the property which enables us |
| 3978 | // to not worry about changing the instance_descriptor and creating a new |
| 3979 | // map. The current version of SetObjectProperty does not handle attributes |
| 3980 | // correctly in the case where a property is a field and is reset with |
| 3981 | // new attributes. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3982 | if (result.IsProperty() && |
| 3983 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3984 | // New attributes - normalize to avoid writing to instance descriptor |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3985 | if (js_object->IsJSGlobalProxy()) { |
| 3986 | // Since the result is a property, the prototype will exist so |
| 3987 | // we don't have to check for null. |
| 3988 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3989 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3990 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3991 | // Use IgnoreAttributes version since a readonly property may be |
| 3992 | // overridden and SetProperty does not allow this. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3993 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 3994 | *obj_value, |
| 3995 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3996 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3997 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 3998 | return Runtime::ForceSetObjectProperty(isolate, |
| 3999 | js_object, |
| 4000 | name, |
| 4001 | obj_value, |
| 4002 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4006 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 4007 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4008 | Handle<Object> key, |
| 4009 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4010 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4011 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4012 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4013 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4015 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4016 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4017 | isolate->factory()->NewTypeError("non_object_property_store", |
| 4018 | HandleVector(args, 2)); |
| 4019 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | // If the object isn't a JavaScript object, we ignore the store. |
| 4023 | if (!object->IsJSObject()) return *value; |
| 4024 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4025 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 4026 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4027 | // Check if the given key is an array index. |
| 4028 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4029 | if (key->ToArrayIndex(&index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4030 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4031 | // of a string using [] notation. We need to support this too in |
| 4032 | // JavaScript. |
| 4033 | // In the case of a String object we just need to redirect the assignment to |
| 4034 | // the underlying string if the index is in range. Since the underlying |
| 4035 | // string does nothing with the assignment then we can ignore such |
| 4036 | // assignments. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4037 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4038 | return *value; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4039 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4040 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4041 | Handle<Object> result = SetElement(js_object, index, value, strict_mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4042 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4043 | return *value; |
| 4044 | } |
| 4045 | |
| 4046 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4047 | Handle<Object> result; |
| 4048 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4049 | result = SetElement(js_object, index, value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4050 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4051 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4052 | key_string->TryFlatten(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4053 | result = SetProperty(js_object, key_string, value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4055 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4056 | return *value; |
| 4057 | } |
| 4058 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4059 | // Call-back into JavaScript to convert the key to a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4060 | bool has_pending_exception = false; |
| 4061 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4062 | if (has_pending_exception) return Failure::Exception(); |
| 4063 | Handle<String> name = Handle<String>::cast(converted); |
| 4064 | |
| 4065 | if (name->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4066 | return js_object->SetElement(index, *value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4067 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4068 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4073 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4074 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4075 | Handle<Object> key, |
| 4076 | Handle<Object> value, |
| 4077 | PropertyAttributes attr) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4078 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4079 | |
| 4080 | // Check if the given key is an array index. |
| 4081 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4082 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4083 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4084 | // of a string using [] notation. We need to support this too in |
| 4085 | // JavaScript. |
| 4086 | // In the case of a String object we just need to redirect the assignment to |
| 4087 | // the underlying string if the index is in range. Since the underlying |
| 4088 | // string does nothing with the assignment then we can ignore such |
| 4089 | // assignments. |
| 4090 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4091 | return *value; |
| 4092 | } |
| 4093 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4094 | return js_object->SetElement(index, *value, kNonStrictMode); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | if (key->IsString()) { |
| 4098 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4099 | return js_object->SetElement(index, *value, kNonStrictMode); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4100 | } else { |
| 4101 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4102 | key_string->TryFlatten(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4103 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4104 | *value, |
| 4105 | attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | // Call-back into JavaScript to convert the key to a string. |
| 4110 | bool has_pending_exception = false; |
| 4111 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4112 | if (has_pending_exception) return Failure::Exception(); |
| 4113 | Handle<String> name = Handle<String>::cast(converted); |
| 4114 | |
| 4115 | if (name->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4116 | return js_object->SetElement(index, *value, kNonStrictMode); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4117 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4118 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4123 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
| 4124 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4125 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4126 | HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4127 | |
| 4128 | // Check if the given key is an array index. |
| 4129 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4130 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4131 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4132 | // characters of a string using [] notation. In the case of a |
| 4133 | // String object we just need to redirect the deletion to the |
| 4134 | // underlying string if the index is in range. Since the |
| 4135 | // underlying string does nothing with the deletion, we can ignore |
| 4136 | // such deletions. |
| 4137 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4138 | return isolate->heap()->true_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | return js_object->DeleteElement(index, JSObject::FORCE_DELETION); |
| 4142 | } |
| 4143 | |
| 4144 | Handle<String> key_string; |
| 4145 | if (key->IsString()) { |
| 4146 | key_string = Handle<String>::cast(key); |
| 4147 | } else { |
| 4148 | // Call-back into JavaScript to convert the key to a string. |
| 4149 | bool has_pending_exception = false; |
| 4150 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4151 | if (has_pending_exception) return Failure::Exception(); |
| 4152 | key_string = Handle<String>::cast(converted); |
| 4153 | } |
| 4154 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4155 | key_string->TryFlatten(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4156 | return js_object->DeleteProperty(*key_string, JSObject::FORCE_DELETION); |
| 4157 | } |
| 4158 | |
| 4159 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4160 | static MaybeObject* Runtime_SetProperty(RUNTIME_CALLING_CONVENTION) { |
| 4161 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4162 | NoHandleAllocation ha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4163 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4164 | |
| 4165 | Handle<Object> object = args.at<Object>(0); |
| 4166 | Handle<Object> key = args.at<Object>(1); |
| 4167 | Handle<Object> value = args.at<Object>(2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4168 | CONVERT_SMI_CHECKED(unchecked_attributes, args[3]); |
| 4169 | RUNTIME_ASSERT( |
| 4170 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4171 | // Compute attributes. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4172 | PropertyAttributes attributes = |
| 4173 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4174 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4175 | StrictModeFlag strict_mode = kNonStrictMode; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4176 | if (args.length() == 5) { |
| 4177 | CONVERT_SMI_CHECKED(strict_unchecked, args[4]); |
| 4178 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 4179 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4180 | strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4181 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4182 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4183 | return Runtime::SetObjectProperty(isolate, |
| 4184 | object, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4185 | key, |
| 4186 | value, |
| 4187 | attributes, |
| 4188 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | |
| 4192 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4193 | // exist, it will be added with attributes NONE. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4194 | static MaybeObject* Runtime_IgnoreAttributesAndSetProperty( |
| 4195 | RUNTIME_CALLING_CONVENTION) { |
| 4196 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | NoHandleAllocation ha; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4198 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4199 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4200 | CONVERT_CHECKED(String, name, args[1]); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4201 | // Compute attributes. |
| 4202 | PropertyAttributes attributes = NONE; |
| 4203 | if (args.length() == 4) { |
| 4204 | CONVERT_CHECKED(Smi, value_obj, args[3]); |
| 4205 | int unchecked_value = value_obj->value(); |
| 4206 | // Only attribute bits should be set. |
| 4207 | RUNTIME_ASSERT( |
| 4208 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4209 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4210 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4211 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4212 | return object-> |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4213 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4217 | static MaybeObject* Runtime_DeleteProperty(RUNTIME_CALLING_CONVENTION) { |
| 4218 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4219 | NoHandleAllocation ha; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4220 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4221 | |
| 4222 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4223 | CONVERT_CHECKED(String, key, args[1]); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4224 | CONVERT_SMI_CHECKED(strict, args[2]); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4225 | return object->DeleteProperty(key, (strict == kStrictMode) |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4226 | ? JSObject::STRICT_DELETION |
| 4227 | : JSObject::NORMAL_DELETION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4231 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4232 | Handle<JSObject> object, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4233 | Handle<String> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4234 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4235 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4236 | // then we have to check it for properties, because they are supposed to |
| 4237 | // look like they are on this object. |
| 4238 | Handle<Object> proto(object->GetPrototype()); |
| 4239 | if (proto->IsJSObject() && |
| 4240 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4241 | return HasLocalPropertyImplementation(isolate, |
| 4242 | Handle<JSObject>::cast(proto), |
| 4243 | key); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4244 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4245 | return isolate->heap()->false_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4249 | static MaybeObject* Runtime_HasLocalProperty(RUNTIME_CALLING_CONVENTION) { |
| 4250 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4251 | NoHandleAllocation ha; |
| 4252 | ASSERT(args.length() == 2); |
| 4253 | CONVERT_CHECKED(String, key, args[1]); |
| 4254 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4255 | Object* obj = args[0]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4256 | // Only JS objects can have properties. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4257 | if (obj->IsJSObject()) { |
| 4258 | JSObject* object = JSObject::cast(obj); |
| 4259 | // Fast case - no interceptors. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4260 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4261 | // Slow case. Either it's not there or we have an interceptor. We should |
| 4262 | // have handles for this kind of deal. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4263 | HandleScope scope(isolate); |
| 4264 | return HasLocalPropertyImplementation(isolate, |
| 4265 | Handle<JSObject>(object), |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4266 | Handle<String>(key)); |
| 4267 | } else if (obj->IsString()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4268 | // Well, there is one exception: Handle [] on strings. |
| 4269 | uint32_t index; |
| 4270 | if (key->AsArrayIndex(&index)) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4271 | String* string = String::cast(obj); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 4272 | if (index < static_cast<uint32_t>(string->length())) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4273 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4274 | } |
| 4275 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4276 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4280 | static MaybeObject* Runtime_HasProperty(RUNTIME_CALLING_CONVENTION) { |
| 4281 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4282 | NoHandleAllocation na; |
| 4283 | ASSERT(args.length() == 2); |
| 4284 | |
| 4285 | // Only JS objects can have properties. |
| 4286 | if (args[0]->IsJSObject()) { |
| 4287 | JSObject* object = JSObject::cast(args[0]); |
| 4288 | CONVERT_CHECKED(String, key, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4289 | if (object->HasProperty(key)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4290 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4291 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4295 | static MaybeObject* Runtime_HasElement(RUNTIME_CALLING_CONVENTION) { |
| 4296 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4297 | NoHandleAllocation na; |
| 4298 | ASSERT(args.length() == 2); |
| 4299 | |
| 4300 | // Only JS objects can have elements. |
| 4301 | if (args[0]->IsJSObject()) { |
| 4302 | JSObject* object = JSObject::cast(args[0]); |
| 4303 | CONVERT_CHECKED(Smi, index_obj, args[1]); |
| 4304 | uint32_t index = index_obj->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4305 | if (object->HasElement(index)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4306 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4307 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4311 | static MaybeObject* Runtime_IsPropertyEnumerable(RUNTIME_CALLING_CONVENTION) { |
| 4312 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4313 | NoHandleAllocation ha; |
| 4314 | ASSERT(args.length() == 2); |
| 4315 | |
| 4316 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4317 | CONVERT_CHECKED(String, key, args[1]); |
| 4318 | |
| 4319 | uint32_t index; |
| 4320 | if (key->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4321 | return isolate->heap()->ToBoolean(object->HasElement(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4322 | } |
| 4323 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4324 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4325 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4329 | static MaybeObject* Runtime_GetPropertyNames(RUNTIME_CALLING_CONVENTION) { |
| 4330 | RUNTIME_GET_ISOLATE; |
| 4331 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4332 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4333 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4334 | return *GetKeysFor(object); |
| 4335 | } |
| 4336 | |
| 4337 | |
| 4338 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4339 | // or, if the given object has an enum cache that contains |
| 4340 | // all enumerable properties of the object and its prototypes |
| 4341 | // have none, the map of the object. This is used to speed up |
| 4342 | // the check for deletions during a for-in. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4343 | static MaybeObject* Runtime_GetPropertyNamesFast(RUNTIME_CALLING_CONVENTION) { |
| 4344 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4345 | ASSERT(args.length() == 1); |
| 4346 | |
| 4347 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
| 4348 | |
| 4349 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4350 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4351 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4352 | Handle<JSObject> object(raw_object); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4353 | Handle<FixedArray> content = GetKeysInFixedArrayFor(object, |
| 4354 | INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4355 | |
| 4356 | // Test again, since cache may have been built by preceding call. |
| 4357 | if (object->IsSimpleEnum()) return object->map(); |
| 4358 | |
| 4359 | return *content; |
| 4360 | } |
| 4361 | |
| 4362 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4363 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4364 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4365 | // is prototype for. |
| 4366 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4367 | int count = 1; |
| 4368 | Object* proto = obj->GetPrototype(); |
| 4369 | while (proto->IsJSObject() && |
| 4370 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4371 | count++; |
| 4372 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4373 | } |
| 4374 | return count; |
| 4375 | } |
| 4376 | |
| 4377 | |
| 4378 | // Return the names of the local named properties. |
| 4379 | // args[0]: object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4380 | static MaybeObject* Runtime_GetLocalPropertyNames(RUNTIME_CALLING_CONVENTION) { |
| 4381 | RUNTIME_GET_ISOLATE; |
| 4382 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4383 | ASSERT(args.length() == 1); |
| 4384 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4385 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4386 | } |
| 4387 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4388 | |
| 4389 | // Skip the global proxy as it has no properties and always delegates to the |
| 4390 | // real global object. |
| 4391 | if (obj->IsJSGlobalProxy()) { |
| 4392 | // Only collect names if access is permitted. |
| 4393 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4394 | !isolate->MayNamedAccess(*obj, |
| 4395 | isolate->heap()->undefined_value(), |
| 4396 | v8::ACCESS_KEYS)) { |
| 4397 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4398 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4399 | } |
| 4400 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4401 | } |
| 4402 | |
| 4403 | // Find the number of objects making up this. |
| 4404 | int length = LocalPrototypeChainLength(*obj); |
| 4405 | |
| 4406 | // Find the number of local properties for each of the objects. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 4407 | ScopedVector<int> local_property_count(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4408 | int total_property_count = 0; |
| 4409 | Handle<JSObject> jsproto = obj; |
| 4410 | for (int i = 0; i < length; i++) { |
| 4411 | // Only collect names if access is permitted. |
| 4412 | if (jsproto->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4413 | !isolate->MayNamedAccess(*jsproto, |
| 4414 | isolate->heap()->undefined_value(), |
| 4415 | v8::ACCESS_KEYS)) { |
| 4416 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 4417 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4418 | } |
| 4419 | int n; |
| 4420 | n = jsproto->NumberOfLocalProperties(static_cast<PropertyAttributes>(NONE)); |
| 4421 | local_property_count[i] = n; |
| 4422 | total_property_count += n; |
| 4423 | if (i < length - 1) { |
| 4424 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | // Allocate an array with storage for all the property names. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4429 | Handle<FixedArray> names = |
| 4430 | isolate->factory()->NewFixedArray(total_property_count); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4431 | |
| 4432 | // Get the property names. |
| 4433 | jsproto = obj; |
| 4434 | int proto_with_hidden_properties = 0; |
| 4435 | for (int i = 0; i < length; i++) { |
| 4436 | jsproto->GetLocalPropertyNames(*names, |
| 4437 | i == 0 ? 0 : local_property_count[i - 1]); |
| 4438 | if (!GetHiddenProperties(jsproto, false)->IsUndefined()) { |
| 4439 | proto_with_hidden_properties++; |
| 4440 | } |
| 4441 | if (i < length - 1) { |
| 4442 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | // Filter out name of hidden propeties object. |
| 4447 | if (proto_with_hidden_properties > 0) { |
| 4448 | Handle<FixedArray> old_names = names; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4449 | names = isolate->factory()->NewFixedArray( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4450 | names->length() - proto_with_hidden_properties); |
| 4451 | int dest_pos = 0; |
| 4452 | for (int i = 0; i < total_property_count; i++) { |
| 4453 | Object* name = old_names->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4454 | if (name == isolate->heap()->hidden_symbol()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4455 | continue; |
| 4456 | } |
| 4457 | names->set(dest_pos++, name); |
| 4458 | } |
| 4459 | } |
| 4460 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4461 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
| 4464 | |
| 4465 | // Return the names of the local indexed properties. |
| 4466 | // args[0]: object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4467 | static MaybeObject* Runtime_GetLocalElementNames(RUNTIME_CALLING_CONVENTION) { |
| 4468 | RUNTIME_GET_ISOLATE; |
| 4469 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4470 | ASSERT(args.length() == 1); |
| 4471 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4472 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4473 | } |
| 4474 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4475 | |
| 4476 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4477 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4478 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4479 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | |
| 4483 | // Return information on whether an object has a named or indexed interceptor. |
| 4484 | // args[0]: object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4485 | static MaybeObject* Runtime_GetInterceptorInfo(RUNTIME_CALLING_CONVENTION) { |
| 4486 | RUNTIME_GET_ISOLATE; |
| 4487 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4488 | ASSERT(args.length() == 1); |
| 4489 | if (!args[0]->IsJSObject()) { |
| 4490 | return Smi::FromInt(0); |
| 4491 | } |
| 4492 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4493 | |
| 4494 | int result = 0; |
| 4495 | if (obj->HasNamedInterceptor()) result |= 2; |
| 4496 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 4497 | |
| 4498 | return Smi::FromInt(result); |
| 4499 | } |
| 4500 | |
| 4501 | |
| 4502 | // Return property names from named interceptor. |
| 4503 | // args[0]: object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4504 | static MaybeObject* Runtime_GetNamedInterceptorPropertyNames( |
| 4505 | RUNTIME_CALLING_CONVENTION) { |
| 4506 | RUNTIME_GET_ISOLATE; |
| 4507 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4508 | ASSERT(args.length() == 1); |
| 4509 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4510 | |
| 4511 | if (obj->HasNamedInterceptor()) { |
| 4512 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 4513 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4514 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4515 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | |
| 4519 | // Return element names from indexed interceptor. |
| 4520 | // args[0]: object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4521 | static MaybeObject* Runtime_GetIndexedInterceptorElementNames( |
| 4522 | RUNTIME_CALLING_CONVENTION) { |
| 4523 | RUNTIME_GET_ISOLATE; |
| 4524 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4525 | ASSERT(args.length() == 1); |
| 4526 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4527 | |
| 4528 | if (obj->HasIndexedInterceptor()) { |
| 4529 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 4530 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4531 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4532 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4536 | static MaybeObject* Runtime_LocalKeys(RUNTIME_CALLING_CONVENTION) { |
| 4537 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4538 | ASSERT_EQ(args.length(), 1); |
| 4539 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4540 | HandleScope scope(isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4541 | Handle<JSObject> object(raw_object); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4542 | |
| 4543 | if (object->IsJSGlobalProxy()) { |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4544 | // Do access checks before going to the global object. |
| 4545 | if (object->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4546 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4547 | v8::ACCESS_KEYS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4548 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 4549 | return *isolate->factory()->NewJSArray(0); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4552 | Handle<Object> proto(object->GetPrototype()); |
| 4553 | // If proxy is detached we simply return an empty array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4554 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4555 | object = Handle<JSObject>::cast(proto); |
| 4556 | } |
| 4557 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4558 | Handle<FixedArray> contents = GetKeysInFixedArrayFor(object, |
| 4559 | LOCAL_ONLY); |
| 4560 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 4561 | // property array and since the result is mutable we have to create |
| 4562 | // a fresh clone on each invocation. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4563 | int length = contents->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4564 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4565 | for (int i = 0; i < length; i++) { |
| 4566 | Object* entry = contents->get(i); |
| 4567 | if (entry->IsString()) { |
| 4568 | copy->set(i, entry); |
| 4569 | } else { |
| 4570 | ASSERT(entry->IsNumber()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4571 | HandleScope scope(isolate); |
| 4572 | Handle<Object> entry_handle(entry, isolate); |
| 4573 | Handle<Object> entry_str = |
| 4574 | isolate->factory()->NumberToString(entry_handle); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4575 | copy->set(i, *entry_str); |
| 4576 | } |
| 4577 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4578 | return *isolate->factory()->NewJSArrayWithElements(copy); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4582 | static MaybeObject* Runtime_GetArgumentsProperty(RUNTIME_CALLING_CONVENTION) { |
| 4583 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4584 | NoHandleAllocation ha; |
| 4585 | ASSERT(args.length() == 1); |
| 4586 | |
| 4587 | // Compute the frame holding the arguments. |
| 4588 | JavaScriptFrameIterator it; |
| 4589 | it.AdvanceToArgumentsFrame(); |
| 4590 | JavaScriptFrame* frame = it.frame(); |
| 4591 | |
| 4592 | // Get the actual number of provided arguments. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4593 | const uint32_t n = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4594 | |
| 4595 | // Try to convert the key to an index. If successful and within |
| 4596 | // index return the the argument from the frame. |
| 4597 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4598 | if (args[0]->ToArrayIndex(&index) && index < n) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4599 | return frame->GetParameter(index); |
| 4600 | } |
| 4601 | |
| 4602 | // Convert the key to a string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4603 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4604 | bool exception = false; |
| 4605 | Handle<Object> converted = |
| 4606 | Execution::ToString(args.at<Object>(0), &exception); |
| 4607 | if (exception) return Failure::Exception(); |
| 4608 | Handle<String> key = Handle<String>::cast(converted); |
| 4609 | |
| 4610 | // Try to convert the string key into an array index. |
| 4611 | if (key->AsArrayIndex(&index)) { |
| 4612 | if (index < n) { |
| 4613 | return frame->GetParameter(index); |
| 4614 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4615 | return isolate->initial_object_prototype()->GetElement(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4616 | } |
| 4617 | } |
| 4618 | |
| 4619 | // Handle special arguments properties. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4620 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 4621 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 4622 | Object* function = frame->function(); |
| 4623 | if (function->IsJSFunction() && |
| 4624 | JSFunction::cast(function)->shared()->strict_mode()) { |
| 4625 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 4626 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 4627 | } |
| 4628 | return function; |
| 4629 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4630 | |
| 4631 | // Lookup in the initial Object.prototype object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4632 | return isolate->initial_object_prototype()->GetProperty(*key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4636 | static MaybeObject* Runtime_ToFastProperties(RUNTIME_CALLING_CONVENTION) { |
| 4637 | RUNTIME_GET_ISOLATE; |
| 4638 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4639 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4640 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4641 | Handle<Object> object = args.at<Object>(0); |
| 4642 | if (object->IsJSObject()) { |
| 4643 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4644 | if (!js_object->HasFastProperties() && !js_object->IsGlobalObject()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4645 | MaybeObject* ok = js_object->TransformToFastProperties(0); |
| 4646 | if (ok->IsRetryAfterGC()) return ok; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4647 | } |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4648 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4649 | return *object; |
| 4650 | } |
| 4651 | |
| 4652 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4653 | static MaybeObject* Runtime_ToSlowProperties(RUNTIME_CALLING_CONVENTION) { |
| 4654 | RUNTIME_GET_ISOLATE; |
| 4655 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4656 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4657 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4658 | Handle<Object> object = args.at<Object>(0); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4659 | if (object->IsJSObject() && !object->IsJSGlobalProxy()) { |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4660 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4661 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4662 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4663 | return *object; |
| 4664 | } |
| 4665 | |
| 4666 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4667 | static MaybeObject* Runtime_ToBool(RUNTIME_CALLING_CONVENTION) { |
| 4668 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4669 | NoHandleAllocation ha; |
| 4670 | ASSERT(args.length() == 1); |
| 4671 | |
| 4672 | return args[0]->ToBoolean(); |
| 4673 | } |
| 4674 | |
| 4675 | |
| 4676 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 4677 | // Possible optimizations: put the type string into the oddballs. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4678 | static MaybeObject* Runtime_Typeof(RUNTIME_CALLING_CONVENTION) { |
| 4679 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4680 | NoHandleAllocation ha; |
| 4681 | |
| 4682 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4683 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4684 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 4685 | |
| 4686 | // typeof an undetectable object is 'undefined' |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4687 | if (heap_obj->map()->is_undetectable()) { |
| 4688 | return isolate->heap()->undefined_symbol(); |
| 4689 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4690 | |
| 4691 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 4692 | if (instance_type < FIRST_NONSTRING_TYPE) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4693 | return isolate->heap()->string_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | switch (instance_type) { |
| 4697 | case ODDBALL_TYPE: |
| 4698 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4699 | return isolate->heap()->boolean_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4700 | } |
| 4701 | if (heap_obj->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4702 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4703 | } |
| 4704 | ASSERT(heap_obj->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4705 | return isolate->heap()->undefined_symbol(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 4706 | case JS_FUNCTION_TYPE: case JS_REGEXP_TYPE: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4707 | return isolate->heap()->function_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4708 | default: |
| 4709 | // For any kind of object not handled above, the spec rule for |
| 4710 | // host objects gives that it is okay to return "object" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4711 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4712 | } |
| 4713 | } |
| 4714 | |
| 4715 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4716 | static bool AreDigits(const char*s, int from, int to) { |
| 4717 | for (int i = from; i < to; i++) { |
| 4718 | if (s[i] < '0' || s[i] > '9') return false; |
| 4719 | } |
| 4720 | |
| 4721 | return true; |
| 4722 | } |
| 4723 | |
| 4724 | |
| 4725 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 4726 | ASSERT(to - from < 10); // Overflow is not possible. |
| 4727 | ASSERT(from < to); |
| 4728 | int d = s[from] - '0'; |
| 4729 | |
| 4730 | for (int i = from + 1; i < to; i++) { |
| 4731 | d = 10 * d + (s[i] - '0'); |
| 4732 | } |
| 4733 | |
| 4734 | return d; |
| 4735 | } |
| 4736 | |
| 4737 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4738 | static MaybeObject* Runtime_StringToNumber(RUNTIME_CALLING_CONVENTION) { |
| 4739 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4740 | NoHandleAllocation ha; |
| 4741 | ASSERT(args.length() == 1); |
| 4742 | CONVERT_CHECKED(String, subject, args[0]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4743 | subject->TryFlatten(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4744 | |
| 4745 | // Fast case: short integer or some sorts of junk values. |
| 4746 | int len = subject->length(); |
| 4747 | if (subject->IsSeqAsciiString()) { |
| 4748 | if (len == 0) return Smi::FromInt(0); |
| 4749 | |
| 4750 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 4751 | bool minus = (data[0] == '-'); |
| 4752 | int start_pos = (minus ? 1 : 0); |
| 4753 | |
| 4754 | if (start_pos == len) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4755 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4756 | } else if (data[start_pos] > '9') { |
| 4757 | // Fast check for a junk value. A valid string may start from a |
| 4758 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 4759 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 4760 | // '9' except 'I'. |
| 4761 | if (data[start_pos] != 'I') { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4762 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4763 | } |
| 4764 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 4765 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 4766 | // know it will fit into the smi-data type. |
| 4767 | int d = ParseDecimalInteger(data, start_pos, len); |
| 4768 | if (minus) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4769 | if (d == 0) return isolate->heap()->minus_zero_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4770 | d = -d; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4771 | } else if (!subject->HasHashCode() && |
| 4772 | len <= String::kMaxArrayIndexSize && |
| 4773 | (len == 1 || data[0] != '0')) { |
| 4774 | // String hash is not calculated yet but all the data are present. |
| 4775 | // Update the hash field to speed up sequential convertions. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4776 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4777 | #ifdef DEBUG |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4778 | subject->Hash(); // Force hash calculation. |
| 4779 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 4780 | static_cast<int>(hash)); |
| 4781 | #endif |
| 4782 | subject->set_hash_field(hash); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4783 | } |
| 4784 | return Smi::FromInt(d); |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | // Slower case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4789 | return isolate->heap()->NumberFromDouble(StringToDouble(subject, ALLOW_HEX)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4793 | static MaybeObject* Runtime_StringFromCharCodeArray( |
| 4794 | RUNTIME_CALLING_CONVENTION) { |
| 4795 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4796 | NoHandleAllocation ha; |
| 4797 | ASSERT(args.length() == 1); |
| 4798 | |
| 4799 | CONVERT_CHECKED(JSArray, codes, args[0]); |
| 4800 | int length = Smi::cast(codes->length())->value(); |
| 4801 | |
| 4802 | // Check if the string can be ASCII. |
| 4803 | int i; |
| 4804 | for (i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4805 | Object* element; |
| 4806 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 4807 | // We probably can't get an exception here, but just in order to enforce |
| 4808 | // the checking of inputs in the runtime calls we check here. |
| 4809 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 4810 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4811 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 4812 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 4813 | break; |
| 4814 | } |
| 4815 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4816 | MaybeObject* maybe_object = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4817 | if (i == length) { // The string is ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4818 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4819 | } else { // The string is not ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4820 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4823 | Object* object = NULL; |
| 4824 | if (!maybe_object->ToObject(&object)) return maybe_object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4825 | String* result = String::cast(object); |
| 4826 | for (int i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4827 | Object* element; |
| 4828 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 4829 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 4830 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4831 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4832 | result->Set(i, chr & 0xffff); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4833 | } |
| 4834 | return result; |
| 4835 | } |
| 4836 | |
| 4837 | |
| 4838 | // kNotEscaped is generated by the following: |
| 4839 | // |
| 4840 | // #!/bin/perl |
| 4841 | // for (my $i = 0; $i < 256; $i++) { |
| 4842 | // print "\n" if $i % 16 == 0; |
| 4843 | // my $c = chr($i); |
| 4844 | // my $escaped = 1; |
| 4845 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 4846 | // print $escaped ? "0, " : "1, "; |
| 4847 | // } |
| 4848 | |
| 4849 | |
| 4850 | static bool IsNotEscaped(uint16_t character) { |
| 4851 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 4852 | ASSERT(character < 256); |
| 4853 | static const char kNotEscaped[256] = { |
| 4854 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4855 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4856 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 4857 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 4858 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4859 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 4860 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4861 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 4862 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4863 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4864 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4865 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4866 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4867 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4868 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4869 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4870 | }; |
| 4871 | return kNotEscaped[character] != 0; |
| 4872 | } |
| 4873 | |
| 4874 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4875 | static MaybeObject* Runtime_URIEscape(RUNTIME_CALLING_CONVENTION) { |
| 4876 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4877 | const char hex_chars[] = "0123456789ABCDEF"; |
| 4878 | NoHandleAllocation ha; |
| 4879 | ASSERT(args.length() == 1); |
| 4880 | CONVERT_CHECKED(String, source, args[0]); |
| 4881 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4882 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4883 | |
| 4884 | int escaped_length = 0; |
| 4885 | int length = source->length(); |
| 4886 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4887 | Access<StringInputBuffer> buffer( |
| 4888 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4889 | buffer->Reset(source); |
| 4890 | while (buffer->has_more()) { |
| 4891 | uint16_t character = buffer->GetNext(); |
| 4892 | if (character >= 256) { |
| 4893 | escaped_length += 6; |
| 4894 | } else if (IsNotEscaped(character)) { |
| 4895 | escaped_length++; |
| 4896 | } else { |
| 4897 | escaped_length += 3; |
| 4898 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4899 | // We don't allow strings that are longer than a maximal length. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4900 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4901 | if (escaped_length > String::kMaxLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4902 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4903 | return Failure::OutOfMemoryException(); |
| 4904 | } |
| 4905 | } |
| 4906 | } |
| 4907 | // No length change implies no change. Return original string if no change. |
| 4908 | if (escaped_length == length) { |
| 4909 | return source; |
| 4910 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4911 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4912 | { MaybeObject* maybe_o = |
| 4913 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4914 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 4915 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4916 | String* destination = String::cast(o); |
| 4917 | int dest_position = 0; |
| 4918 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4919 | Access<StringInputBuffer> buffer( |
| 4920 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4921 | buffer->Rewind(); |
| 4922 | while (buffer->has_more()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4923 | uint16_t chr = buffer->GetNext(); |
| 4924 | if (chr >= 256) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4925 | destination->Set(dest_position, '%'); |
| 4926 | destination->Set(dest_position+1, 'u'); |
| 4927 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 4928 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 4929 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 4930 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4931 | dest_position += 6; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4932 | } else if (IsNotEscaped(chr)) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4933 | destination->Set(dest_position, chr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4934 | dest_position++; |
| 4935 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4936 | destination->Set(dest_position, '%'); |
| 4937 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 4938 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4939 | dest_position += 3; |
| 4940 | } |
| 4941 | } |
| 4942 | return destination; |
| 4943 | } |
| 4944 | |
| 4945 | |
| 4946 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 4947 | static const signed char kHexValue['g'] = { |
| 4948 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4949 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4950 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4951 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 4952 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4953 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4954 | -1, 10, 11, 12, 13, 14, 15 }; |
| 4955 | |
| 4956 | if (character1 > 'f') return -1; |
| 4957 | int hi = kHexValue[character1]; |
| 4958 | if (hi == -1) return -1; |
| 4959 | if (character2 > 'f') return -1; |
| 4960 | int lo = kHexValue[character2]; |
| 4961 | if (lo == -1) return -1; |
| 4962 | return (hi << 4) + lo; |
| 4963 | } |
| 4964 | |
| 4965 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4966 | static inline int Unescape(String* source, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4967 | int i, |
| 4968 | int length, |
| 4969 | int* step) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4970 | uint16_t character = source->Get(i); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4971 | int32_t hi = 0; |
| 4972 | int32_t lo = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4973 | if (character == '%' && |
| 4974 | i <= length - 6 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4975 | source->Get(i + 1) == 'u' && |
| 4976 | (hi = TwoDigitHex(source->Get(i + 2), |
| 4977 | source->Get(i + 3))) != -1 && |
| 4978 | (lo = TwoDigitHex(source->Get(i + 4), |
| 4979 | source->Get(i + 5))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4980 | *step = 6; |
| 4981 | return (hi << 8) + lo; |
| 4982 | } else if (character == '%' && |
| 4983 | i <= length - 3 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4984 | (lo = TwoDigitHex(source->Get(i + 1), |
| 4985 | source->Get(i + 2))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4986 | *step = 3; |
| 4987 | return lo; |
| 4988 | } else { |
| 4989 | *step = 1; |
| 4990 | return character; |
| 4991 | } |
| 4992 | } |
| 4993 | |
| 4994 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 4995 | static MaybeObject* Runtime_URIUnescape(RUNTIME_CALLING_CONVENTION) { |
| 4996 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4997 | NoHandleAllocation ha; |
| 4998 | ASSERT(args.length() == 1); |
| 4999 | CONVERT_CHECKED(String, source, args[0]); |
| 5000 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5001 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5002 | |
| 5003 | bool ascii = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5004 | int length = source->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5005 | |
| 5006 | int unescaped_length = 0; |
| 5007 | for (int i = 0; i < length; unescaped_length++) { |
| 5008 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5009 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5010 | ascii = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5011 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5012 | i += step; |
| 5013 | } |
| 5014 | |
| 5015 | // No length change implies no change. Return original string if no change. |
| 5016 | if (unescaped_length == length) |
| 5017 | return source; |
| 5018 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5019 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5020 | { MaybeObject* maybe_o = |
| 5021 | ascii ? |
| 5022 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 5023 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5024 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5025 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5026 | String* destination = String::cast(o); |
| 5027 | |
| 5028 | int dest_position = 0; |
| 5029 | for (int i = 0; i < length; dest_position++) { |
| 5030 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5031 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5032 | i += step; |
| 5033 | } |
| 5034 | return destination; |
| 5035 | } |
| 5036 | |
| 5037 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5038 | static const unsigned int kQuoteTableLength = 128u; |
| 5039 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5040 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 5041 | static const char* const JsonQuotes = |
| 5042 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 5043 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 5044 | "\\b \\t \\n \\u000b " |
| 5045 | "\\f \\r \\u000e \\u000f " |
| 5046 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 5047 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 5048 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 5049 | "\\u001c \\u001d \\u001e \\u001f " |
| 5050 | " ! \\\" # " |
| 5051 | "$ % & ' " |
| 5052 | "( ) * + " |
| 5053 | ", - . / " |
| 5054 | "0 1 2 3 " |
| 5055 | "4 5 6 7 " |
| 5056 | "8 9 : ; " |
| 5057 | "< = > ? " |
| 5058 | "@ A B C " |
| 5059 | "D E F G " |
| 5060 | "H I J K " |
| 5061 | "L M N O " |
| 5062 | "P Q R S " |
| 5063 | "T U V W " |
| 5064 | "X Y Z [ " |
| 5065 | "\\\\ ] ^ _ " |
| 5066 | "` a b c " |
| 5067 | "d e f g " |
| 5068 | "h i j k " |
| 5069 | "l m n o " |
| 5070 | "p q r s " |
| 5071 | "t u v w " |
| 5072 | "x y z { " |
| 5073 | "| } ~ \177 "; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5074 | |
| 5075 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5076 | // For a string that is less than 32k characters it should always be |
| 5077 | // possible to allocate it in new space. |
| 5078 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5079 | |
| 5080 | |
| 5081 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5082 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5083 | |
| 5084 | |
| 5085 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5086 | // quoting). |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5087 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5088 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5089 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5090 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5091 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5092 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5093 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5094 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5095 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5096 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5097 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5098 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5099 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5100 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5101 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5102 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5103 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5104 | }; |
| 5105 | |
| 5106 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5107 | template <typename StringType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5108 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5109 | |
| 5110 | |
| 5111 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5112 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5113 | return isolate->heap()->AllocateRawTwoByteString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | |
| 5117 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5118 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5119 | return isolate->heap()->AllocateRawAsciiString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5123 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5124 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5125 | Vector<const Char> characters) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5126 | int length = characters.length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5127 | const Char* read_cursor = characters.start(); |
| 5128 | const Char* end = read_cursor + length; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5129 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5130 | int quoted_length = kSpaceForQuotes; |
| 5131 | while (read_cursor < end) { |
| 5132 | Char c = *(read_cursor++); |
| 5133 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5134 | quoted_length++; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5135 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5136 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5137 | } |
| 5138 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5139 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5140 | quoted_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5141 | Object* new_object; |
| 5142 | if (!new_alloc->ToObject(&new_object)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5143 | return new_alloc; |
| 5144 | } |
| 5145 | StringType* new_string = StringType::cast(new_object); |
| 5146 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5147 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5148 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5149 | if (comma) *(write_cursor++) = ','; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5150 | *(write_cursor++) = '"'; |
| 5151 | |
| 5152 | read_cursor = characters.start(); |
| 5153 | while (read_cursor < end) { |
| 5154 | Char c = *(read_cursor++); |
| 5155 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5156 | *(write_cursor++) = c; |
| 5157 | } else { |
| 5158 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5159 | const char* replacement = JsonQuotes + |
| 5160 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5161 | for (int i = 0; i < len; i++) { |
| 5162 | *write_cursor++ = *replacement++; |
| 5163 | } |
| 5164 | } |
| 5165 | } |
| 5166 | *(write_cursor++) = '"'; |
| 5167 | return new_string; |
| 5168 | } |
| 5169 | |
| 5170 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5171 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5172 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5173 | Vector<const Char> characters) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5174 | int length = characters.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5175 | isolate->counters()->quote_json_char_count()->Increment(length); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5176 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5177 | int worst_case_length = length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; |
| 5178 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5179 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5180 | } |
| 5181 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5182 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5183 | worst_case_length); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5184 | Object* new_object; |
| 5185 | if (!new_alloc->ToObject(&new_object)) { |
| 5186 | return new_alloc; |
| 5187 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5188 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5189 | // Even if our string is small enough to fit in new space we still have to |
| 5190 | // handle it being allocated in old space as may happen in the third |
| 5191 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5192 | // CEntryStub::GenerateCore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5193 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5194 | } |
| 5195 | StringType* new_string = StringType::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5196 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5197 | |
| 5198 | STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
| 5199 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5200 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5201 | if (comma) *(write_cursor++) = ','; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5202 | *(write_cursor++) = '"'; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5203 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5204 | const Char* read_cursor = characters.start(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5205 | const Char* end = read_cursor + length; |
| 5206 | while (read_cursor < end) { |
| 5207 | Char c = *(read_cursor++); |
| 5208 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5209 | *(write_cursor++) = c; |
| 5210 | } else { |
| 5211 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5212 | const char* replacement = JsonQuotes + |
| 5213 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5214 | write_cursor[0] = replacement[0]; |
| 5215 | if (len > 1) { |
| 5216 | write_cursor[1] = replacement[1]; |
| 5217 | if (len > 2) { |
| 5218 | ASSERT(len == 6); |
| 5219 | write_cursor[2] = replacement[2]; |
| 5220 | write_cursor[3] = replacement[3]; |
| 5221 | write_cursor[4] = replacement[4]; |
| 5222 | write_cursor[5] = replacement[5]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5223 | } |
| 5224 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5225 | write_cursor += len; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5226 | } |
| 5227 | } |
| 5228 | *(write_cursor++) = '"'; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5229 | |
| 5230 | int final_length = static_cast<int>( |
| 5231 | write_cursor - reinterpret_cast<Char*>( |
| 5232 | new_string->address() + SeqAsciiString::kHeaderSize)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5233 | isolate->heap()->new_space()->ShrinkStringAtAllocationBoundary<StringType>( |
| 5234 | new_string, final_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5235 | return new_string; |
| 5236 | } |
| 5237 | |
| 5238 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5239 | static MaybeObject* Runtime_QuoteJSONString(RUNTIME_CALLING_CONVENTION) { |
| 5240 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5241 | NoHandleAllocation ha; |
| 5242 | CONVERT_CHECKED(String, str, args[0]); |
| 5243 | if (!str->IsFlat()) { |
| 5244 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5245 | Object* flat; |
| 5246 | if (!try_flatten->ToObject(&flat)) { |
| 5247 | return try_flatten; |
| 5248 | } |
| 5249 | str = String::cast(flat); |
| 5250 | ASSERT(str->IsFlat()); |
| 5251 | } |
| 5252 | if (str->IsTwoByteRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5253 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
| 5254 | str->ToUC16Vector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5255 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5256 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
| 5257 | str->ToAsciiVector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5262 | static MaybeObject* Runtime_QuoteJSONStringComma(RUNTIME_CALLING_CONVENTION) { |
| 5263 | RUNTIME_GET_ISOLATE; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5264 | NoHandleAllocation ha; |
| 5265 | CONVERT_CHECKED(String, str, args[0]); |
| 5266 | if (!str->IsFlat()) { |
| 5267 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5268 | Object* flat; |
| 5269 | if (!try_flatten->ToObject(&flat)) { |
| 5270 | return try_flatten; |
| 5271 | } |
| 5272 | str = String::cast(flat); |
| 5273 | ASSERT(str->IsFlat()); |
| 5274 | } |
| 5275 | if (str->IsTwoByteRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5276 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
| 5277 | str->ToUC16Vector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5278 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5279 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
| 5280 | str->ToAsciiVector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5284 | static MaybeObject* Runtime_StringParseInt(RUNTIME_CALLING_CONVENTION) { |
| 5285 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5286 | NoHandleAllocation ha; |
| 5287 | |
| 5288 | CONVERT_CHECKED(String, s, args[0]); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5289 | CONVERT_SMI_CHECKED(radix, args[1]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5290 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5291 | s->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5292 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5293 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
| 5294 | double value = StringToInt(s, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5295 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5299 | static MaybeObject* Runtime_StringParseFloat(RUNTIME_CALLING_CONVENTION) { |
| 5300 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5301 | NoHandleAllocation ha; |
| 5302 | CONVERT_CHECKED(String, str, args[0]); |
| 5303 | |
| 5304 | // ECMA-262 section 15.1.2.3, empty string is NaN |
| 5305 | double value = StringToDouble(str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
| 5306 | |
| 5307 | // Create a number object from the value. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5308 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5312 | template <class Converter> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5313 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5314 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5315 | String* s, |
| 5316 | int length, |
| 5317 | int input_string_length, |
| 5318 | unibrow::Mapping<Converter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5319 | // We try this twice, once with the assumption that the result is no longer |
| 5320 | // than the input and, if that assumption breaks, again with the exact |
| 5321 | // length. This may not be pretty, but it is nicer than what was here before |
| 5322 | // and I hereby claim my vaffel-is. |
| 5323 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5324 | // Allocate the resulting string. |
| 5325 | // |
| 5326 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5327 | // character is also ascii. This is currently the case, but it |
| 5328 | // might break in the future if we implement more context and locale |
| 5329 | // dependent upper/lower conversions. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5330 | Object* o; |
| 5331 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5332 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 5333 | : isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5334 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5335 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5336 | String* result = String::cast(o); |
| 5337 | bool has_changed_character = false; |
| 5338 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5339 | // Convert all characters to upper case, assuming that they will fit |
| 5340 | // in the buffer |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5341 | Access<StringInputBuffer> buffer( |
| 5342 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5343 | buffer->Reset(s); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5344 | unibrow::uchar chars[Converter::kMaxWidth]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5345 | // We can assume that the string is not empty |
| 5346 | uc32 current = buffer->GetNext(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5347 | for (int i = 0; i < length;) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5348 | bool has_next = buffer->has_more(); |
| 5349 | uc32 next = has_next ? buffer->GetNext() : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5350 | int char_length = mapping->get(current, next, chars); |
| 5351 | if (char_length == 0) { |
| 5352 | // The case conversion of this character is the character itself. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5353 | result->Set(i, current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5354 | i++; |
| 5355 | } else if (char_length == 1) { |
| 5356 | // Common case: converting the letter resulted in one character. |
| 5357 | ASSERT(static_cast<uc32>(chars[0]) != current); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5358 | result->Set(i, chars[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5359 | has_changed_character = true; |
| 5360 | i++; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5361 | } else if (length == input_string_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5362 | // We've assumed that the result would be as long as the |
| 5363 | // input but here is a character that converts to several |
| 5364 | // characters. No matter, we calculate the exact length |
| 5365 | // of the result and try the whole thing again. |
| 5366 | // |
| 5367 | // Note that this leaves room for optimization. We could just |
| 5368 | // memcpy what we already have to the result string. Also, |
| 5369 | // the result string is the last object allocated we could |
| 5370 | // "realloc" it and probably, in the vast majority of cases, |
| 5371 | // extend the existing string to be able to hold the full |
| 5372 | // result. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5373 | int next_length = 0; |
| 5374 | if (has_next) { |
| 5375 | next_length = mapping->get(next, 0, chars); |
| 5376 | if (next_length == 0) next_length = 1; |
| 5377 | } |
| 5378 | int current_length = i + char_length + next_length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5379 | while (buffer->has_more()) { |
| 5380 | current = buffer->GetNext(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5381 | // NOTE: we use 0 as the next character here because, while |
| 5382 | // the next character may affect what a character converts to, |
| 5383 | // it does not in any case affect the length of what it convert |
| 5384 | // to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5385 | int char_length = mapping->get(current, 0, chars); |
| 5386 | if (char_length == 0) char_length = 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5387 | current_length += char_length; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5388 | if (current_length > Smi::kMaxValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5389 | isolate->context()->mark_out_of_memory(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5390 | return Failure::OutOfMemoryException(); |
| 5391 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5392 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5393 | // Try again with the real length. |
| 5394 | return Smi::FromInt(current_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5395 | } else { |
| 5396 | for (int j = 0; j < char_length; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5397 | result->Set(i, chars[j]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5398 | i++; |
| 5399 | } |
| 5400 | has_changed_character = true; |
| 5401 | } |
| 5402 | current = next; |
| 5403 | } |
| 5404 | if (has_changed_character) { |
| 5405 | return result; |
| 5406 | } else { |
| 5407 | // If we didn't actually change anything in doing the conversion |
| 5408 | // we simple return the result and let the converted string |
| 5409 | // become garbage; there is no reason to keep two identical strings |
| 5410 | // alive. |
| 5411 | return s; |
| 5412 | } |
| 5413 | } |
| 5414 | |
| 5415 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5416 | namespace { |
| 5417 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5418 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 5419 | |
| 5420 | |
| 5421 | // Given a word and two range boundaries returns a word with high bit |
| 5422 | // set in every byte iff the corresponding input byte was strictly in |
| 5423 | // the range (m, n). All the other bits in the result are cleared. |
| 5424 | // This function is only useful when it can be inlined and the |
| 5425 | // boundaries are statically known. |
| 5426 | // Requires: all bytes in the input word and the boundaries must be |
| 5427 | // ascii (less than 0x7F). |
| 5428 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
| 5429 | // Every byte in an ascii string is less than or equal to 0x7F. |
| 5430 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 5431 | // Use strict inequalities since in edge cases the function could be |
| 5432 | // further simplified. |
| 5433 | ASSERT(0 < m && m < n && n < 0x7F); |
| 5434 | // Has high bit set in every w byte less than n. |
| 5435 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 5436 | // Has high bit set in every w byte greater than m. |
| 5437 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 5438 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | enum AsciiCaseConversion { |
| 5443 | ASCII_TO_LOWER, |
| 5444 | ASCII_TO_UPPER |
| 5445 | }; |
| 5446 | |
| 5447 | |
| 5448 | template <AsciiCaseConversion dir> |
| 5449 | struct FastAsciiConverter { |
| 5450 | static bool Convert(char* dst, char* src, int length) { |
| 5451 | #ifdef DEBUG |
| 5452 | char* saved_dst = dst; |
| 5453 | char* saved_src = src; |
| 5454 | #endif |
| 5455 | // We rely on the distance between upper and lower case letters |
| 5456 | // being a known power of 2. |
| 5457 | ASSERT('a' - 'A' == (1 << 5)); |
| 5458 | // Boundaries for the range of input characters than require conversion. |
| 5459 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 5460 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 5461 | bool changed = false; |
| 5462 | char* const limit = src + length; |
| 5463 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 5464 | // Process the prefix of the input that requires no conversion one |
| 5465 | // (machine) word at a time. |
| 5466 | while (src <= limit - sizeof(uintptr_t)) { |
| 5467 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 5468 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 5469 | changed = true; |
| 5470 | break; |
| 5471 | } |
| 5472 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 5473 | src += sizeof(uintptr_t); |
| 5474 | dst += sizeof(uintptr_t); |
| 5475 | } |
| 5476 | // Process the remainder of the input performing conversion when |
| 5477 | // required one word at a time. |
| 5478 | while (src <= limit - sizeof(uintptr_t)) { |
| 5479 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 5480 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 5481 | // The mask has high (7th) bit set in every byte that needs |
| 5482 | // conversion and we know that the distance between cases is |
| 5483 | // 1 << 5. |
| 5484 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 5485 | src += sizeof(uintptr_t); |
| 5486 | dst += sizeof(uintptr_t); |
| 5487 | } |
| 5488 | #endif |
| 5489 | // Process the last few bytes of the input (or the whole input if |
| 5490 | // unaligned access is not supported). |
| 5491 | while (src < limit) { |
| 5492 | char c = *src; |
| 5493 | if (lo < c && c < hi) { |
| 5494 | c ^= (1 << 5); |
| 5495 | changed = true; |
| 5496 | } |
| 5497 | *dst = c; |
| 5498 | ++src; |
| 5499 | ++dst; |
| 5500 | } |
| 5501 | #ifdef DEBUG |
| 5502 | CheckConvert(saved_dst, saved_src, length, changed); |
| 5503 | #endif |
| 5504 | return changed; |
| 5505 | } |
| 5506 | |
| 5507 | #ifdef DEBUG |
| 5508 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 5509 | bool expected_changed = false; |
| 5510 | for (int i = 0; i < length; i++) { |
| 5511 | if (dst[i] == src[i]) continue; |
| 5512 | expected_changed = true; |
| 5513 | if (dir == ASCII_TO_LOWER) { |
| 5514 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 5515 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 5516 | } else { |
| 5517 | ASSERT(dir == ASCII_TO_UPPER); |
| 5518 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 5519 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 5520 | } |
| 5521 | } |
| 5522 | ASSERT(expected_changed == changed); |
| 5523 | } |
| 5524 | #endif |
| 5525 | }; |
| 5526 | |
| 5527 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5528 | struct ToLowerTraits { |
| 5529 | typedef unibrow::ToLowercase UnibrowConverter; |
| 5530 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5531 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5532 | }; |
| 5533 | |
| 5534 | |
| 5535 | struct ToUpperTraits { |
| 5536 | typedef unibrow::ToUppercase UnibrowConverter; |
| 5537 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5538 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5539 | }; |
| 5540 | |
| 5541 | } // namespace |
| 5542 | |
| 5543 | |
| 5544 | template <typename ConvertTraits> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5545 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5546 | Arguments args, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5547 | Isolate* isolate, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5548 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5549 | NoHandleAllocation ha; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5550 | CONVERT_CHECKED(String, s, args[0]); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5551 | s = s->TryFlattenGetString(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5552 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5553 | const int length = s->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5554 | // Assume that the string is not empty; we need this assumption later |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5555 | if (length == 0) return s; |
| 5556 | |
| 5557 | // Simpler handling of ascii strings. |
| 5558 | // |
| 5559 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5560 | // character is also ascii. This is currently the case, but it |
| 5561 | // might break in the future if we implement more context and locale |
| 5562 | // dependent upper/lower conversions. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5563 | if (s->IsSeqAsciiString()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5564 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5565 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5566 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5567 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5568 | SeqAsciiString* result = SeqAsciiString::cast(o); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5569 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5570 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5571 | return has_changed_character ? result : s; |
| 5572 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5573 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5574 | Object* answer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5575 | { MaybeObject* maybe_answer = |
| 5576 | ConvertCaseHelper(isolate, s, length, length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5577 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 5578 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5579 | if (answer->IsSmi()) { |
| 5580 | // Retry with correct length. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5581 | { MaybeObject* maybe_answer = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5582 | ConvertCaseHelper(isolate, |
| 5583 | s, Smi::cast(answer)->value(), length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5584 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 5585 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5586 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5587 | return answer; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5591 | static MaybeObject* Runtime_StringToLowerCase(RUNTIME_CALLING_CONVENTION) { |
| 5592 | RUNTIME_GET_ISOLATE; |
| 5593 | return ConvertCase<ToLowerTraits>( |
| 5594 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5598 | static MaybeObject* Runtime_StringToUpperCase(RUNTIME_CALLING_CONVENTION) { |
| 5599 | RUNTIME_GET_ISOLATE; |
| 5600 | return ConvertCase<ToUpperTraits>( |
| 5601 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5604 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5605 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
| 5606 | return unibrow::WhiteSpace::Is(c) || c == 0x200b; |
| 5607 | } |
| 5608 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5609 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5610 | static MaybeObject* Runtime_StringTrim(RUNTIME_CALLING_CONVENTION) { |
| 5611 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5612 | NoHandleAllocation ha; |
| 5613 | ASSERT(args.length() == 3); |
| 5614 | |
| 5615 | CONVERT_CHECKED(String, s, args[0]); |
| 5616 | CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); |
| 5617 | CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); |
| 5618 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5619 | s->TryFlatten(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5620 | int length = s->length(); |
| 5621 | |
| 5622 | int left = 0; |
| 5623 | if (trimLeft) { |
| 5624 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 5625 | left++; |
| 5626 | } |
| 5627 | } |
| 5628 | |
| 5629 | int right = length; |
| 5630 | if (trimRight) { |
| 5631 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 5632 | right--; |
| 5633 | } |
| 5634 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5635 | return s->SubString(left, right); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5636 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5637 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5638 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5639 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5640 | void FindStringIndices(Isolate* isolate, |
| 5641 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5642 | Vector<const PatternChar> pattern, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5643 | ZoneList<int>* indices, |
| 5644 | unsigned int limit) { |
| 5645 | ASSERT(limit > 0); |
| 5646 | // Collect indices of pattern in subject, and the end-of-string index. |
| 5647 | // Stop after finding at most limit values. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5648 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5649 | int pattern_length = pattern.length(); |
| 5650 | int index = 0; |
| 5651 | while (limit > 0) { |
| 5652 | index = search.Search(subject, index); |
| 5653 | if (index < 0) return; |
| 5654 | indices->Add(index); |
| 5655 | index += pattern_length; |
| 5656 | limit--; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5657 | } |
| 5658 | } |
| 5659 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5660 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5661 | static MaybeObject* Runtime_StringSplit(RUNTIME_CALLING_CONVENTION) { |
| 5662 | RUNTIME_GET_ISOLATE; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5663 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5664 | HandleScope handle_scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5665 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 5666 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 5667 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 5668 | |
| 5669 | int subject_length = subject->length(); |
| 5670 | int pattern_length = pattern->length(); |
| 5671 | RUNTIME_ASSERT(pattern_length > 0); |
| 5672 | |
| 5673 | // The limit can be very large (0xffffffffu), but since the pattern |
| 5674 | // isn't empty, we can never create more parts than ~half the length |
| 5675 | // of the subject. |
| 5676 | |
| 5677 | if (!subject->IsFlat()) FlattenString(subject); |
| 5678 | |
| 5679 | static const int kMaxInitialListCapacity = 16; |
| 5680 | |
| 5681 | ZoneScope scope(DELETE_ON_EXIT); |
| 5682 | |
| 5683 | // Find (up to limit) indices of separator and end-of-string in subject |
| 5684 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 5685 | ZoneList<int> indices(initial_capacity); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5686 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 5687 | |
| 5688 | // No allocation block. |
| 5689 | { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5690 | AssertNoAllocation nogc; |
| 5691 | if (subject->IsAsciiRepresentation()) { |
| 5692 | Vector<const char> subject_vector = subject->ToAsciiVector(); |
| 5693 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5694 | FindStringIndices(isolate, |
| 5695 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5696 | pattern->ToAsciiVector(), |
| 5697 | &indices, |
| 5698 | limit); |
| 5699 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5700 | FindStringIndices(isolate, |
| 5701 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5702 | pattern->ToUC16Vector(), |
| 5703 | &indices, |
| 5704 | limit); |
| 5705 | } |
| 5706 | } else { |
| 5707 | Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
| 5708 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5709 | FindStringIndices(isolate, |
| 5710 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5711 | pattern->ToAsciiVector(), |
| 5712 | &indices, |
| 5713 | limit); |
| 5714 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5715 | FindStringIndices(isolate, |
| 5716 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5717 | pattern->ToUC16Vector(), |
| 5718 | &indices, |
| 5719 | limit); |
| 5720 | } |
| 5721 | } |
| 5722 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5723 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5724 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 5725 | indices.Add(subject_length); |
| 5726 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5727 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5728 | // The list indices now contains the end of each part to create. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5729 | |
| 5730 | // Create JSArray of substrings separated by separator. |
| 5731 | int part_count = indices.length(); |
| 5732 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5733 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5734 | result->set_length(Smi::FromInt(part_count)); |
| 5735 | |
| 5736 | ASSERT(result->HasFastElements()); |
| 5737 | |
| 5738 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 5739 | FixedArray::cast(result->elements())->set(0, *subject); |
| 5740 | return *result; |
| 5741 | } |
| 5742 | |
| 5743 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 5744 | int part_start = 0; |
| 5745 | for (int i = 0; i < part_count; i++) { |
| 5746 | HandleScope local_loop_handle; |
| 5747 | int part_end = indices.at(i); |
| 5748 | Handle<String> substring = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5749 | isolate->factory()->NewSubString(subject, part_start, part_end); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5750 | elements->set(i, *substring); |
| 5751 | part_start = part_end + pattern_length; |
| 5752 | } |
| 5753 | |
| 5754 | return *result; |
| 5755 | } |
| 5756 | |
| 5757 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5758 | // Copies ascii characters to the given fixed array looking up |
| 5759 | // one-char strings in the cache. Gives up on the first char that is |
| 5760 | // not in the cache and fills the remainder with smi zeros. Returns |
| 5761 | // the length of the successfully copied prefix. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5762 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 5763 | const char* chars, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5764 | FixedArray* elements, |
| 5765 | int length) { |
| 5766 | AssertNoAllocation nogc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5767 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 5768 | Object* undefined = heap->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5769 | int i; |
| 5770 | for (i = 0; i < length; ++i) { |
| 5771 | Object* value = ascii_cache->get(chars[i]); |
| 5772 | if (value == undefined) break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5773 | ASSERT(!heap->InNewSpace(value)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5774 | elements->set(i, value, SKIP_WRITE_BARRIER); |
| 5775 | } |
| 5776 | if (i < length) { |
| 5777 | ASSERT(Smi::FromInt(0) == 0); |
| 5778 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 5779 | } |
| 5780 | #ifdef DEBUG |
| 5781 | for (int j = 0; j < length; ++j) { |
| 5782 | Object* element = elements->get(j); |
| 5783 | ASSERT(element == Smi::FromInt(0) || |
| 5784 | (element->IsString() && String::cast(element)->LooksValid())); |
| 5785 | } |
| 5786 | #endif |
| 5787 | return i; |
| 5788 | } |
| 5789 | |
| 5790 | |
| 5791 | // Converts a String to JSArray. |
| 5792 | // For example, "foo" => ["f", "o", "o"]. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5793 | static MaybeObject* Runtime_StringToArray(RUNTIME_CALLING_CONVENTION) { |
| 5794 | RUNTIME_GET_ISOLATE; |
| 5795 | HandleScope scope(isolate); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5796 | ASSERT(args.length() == 2); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5797 | CONVERT_ARG_CHECKED(String, s, 0); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5798 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5799 | |
| 5800 | s->TryFlatten(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5801 | const int length = static_cast<int>(Min<uint32_t>(s->length(), limit)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5802 | |
| 5803 | Handle<FixedArray> elements; |
| 5804 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5805 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5806 | { MaybeObject* maybe_obj = |
| 5807 | isolate->heap()->AllocateUninitializedFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5808 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 5809 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5810 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5811 | |
| 5812 | Vector<const char> chars = s->ToAsciiVector(); |
| 5813 | // Note, this will initialize all elements (not only the prefix) |
| 5814 | // to prevent GC from seeing partially initialized array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5815 | int num_copied_from_cache = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 5816 | chars.start(), |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5817 | *elements, |
| 5818 | length); |
| 5819 | |
| 5820 | for (int i = num_copied_from_cache; i < length; ++i) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5821 | Handle<Object> str = LookupSingleCharacterStringFromCode(chars[i]); |
| 5822 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5823 | } |
| 5824 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5825 | elements = isolate->factory()->NewFixedArray(length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5826 | for (int i = 0; i < length; ++i) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5827 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 5828 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5829 | } |
| 5830 | } |
| 5831 | |
| 5832 | #ifdef DEBUG |
| 5833 | for (int i = 0; i < length; ++i) { |
| 5834 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 5835 | } |
| 5836 | #endif |
| 5837 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5838 | return *isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5842 | static MaybeObject* Runtime_NewStringWrapper(RUNTIME_CALLING_CONVENTION) { |
| 5843 | RUNTIME_GET_ISOLATE; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5844 | NoHandleAllocation ha; |
| 5845 | ASSERT(args.length() == 1); |
| 5846 | CONVERT_CHECKED(String, value, args[0]); |
| 5847 | return value->ToObject(); |
| 5848 | } |
| 5849 | |
| 5850 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5851 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 5852 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5853 | int char_length = runtime_state->to_upper_mapping()->get(ch, 0, chars); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 5854 | return char_length == 0; |
| 5855 | } |
| 5856 | |
| 5857 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5858 | static MaybeObject* Runtime_NumberToString(RUNTIME_CALLING_CONVENTION) { |
| 5859 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5860 | NoHandleAllocation ha; |
| 5861 | ASSERT(args.length() == 1); |
| 5862 | |
| 5863 | Object* number = args[0]; |
| 5864 | RUNTIME_ASSERT(number->IsNumber()); |
| 5865 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5866 | return isolate->heap()->NumberToString(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5870 | static MaybeObject* Runtime_NumberToStringSkipCache( |
| 5871 | RUNTIME_CALLING_CONVENTION) { |
| 5872 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5873 | NoHandleAllocation ha; |
| 5874 | ASSERT(args.length() == 1); |
| 5875 | |
| 5876 | Object* number = args[0]; |
| 5877 | RUNTIME_ASSERT(number->IsNumber()); |
| 5878 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5879 | return isolate->heap()->NumberToString(number, false); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5883 | static MaybeObject* Runtime_NumberToInteger(RUNTIME_CALLING_CONVENTION) { |
| 5884 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5885 | NoHandleAllocation ha; |
| 5886 | ASSERT(args.length() == 1); |
| 5887 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5888 | CONVERT_DOUBLE_CHECKED(number, args[0]); |
| 5889 | |
| 5890 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5891 | if (number > 0 && number <= Smi::kMaxValue) { |
| 5892 | return Smi::FromInt(static_cast<int>(number)); |
| 5893 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5894 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5895 | } |
| 5896 | |
| 5897 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5898 | static MaybeObject* Runtime_NumberToIntegerMapMinusZero( |
| 5899 | RUNTIME_CALLING_CONVENTION) { |
| 5900 | RUNTIME_GET_ISOLATE; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5901 | NoHandleAllocation ha; |
| 5902 | ASSERT(args.length() == 1); |
| 5903 | |
| 5904 | CONVERT_DOUBLE_CHECKED(number, args[0]); |
| 5905 | |
| 5906 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5907 | if (number > 0 && number <= Smi::kMaxValue) { |
| 5908 | return Smi::FromInt(static_cast<int>(number)); |
| 5909 | } |
| 5910 | |
| 5911 | double double_value = DoubleToInteger(number); |
| 5912 | // Map both -0 and +0 to +0. |
| 5913 | if (double_value == 0) double_value = 0; |
| 5914 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5915 | return isolate->heap()->NumberFromDouble(double_value); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5919 | static MaybeObject* Runtime_NumberToJSUint32(RUNTIME_CALLING_CONVENTION) { |
| 5920 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5921 | NoHandleAllocation ha; |
| 5922 | ASSERT(args.length() == 1); |
| 5923 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5924 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5925 | return isolate->heap()->NumberFromUint32(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5929 | static MaybeObject* Runtime_NumberToJSInt32(RUNTIME_CALLING_CONVENTION) { |
| 5930 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5931 | NoHandleAllocation ha; |
| 5932 | ASSERT(args.length() == 1); |
| 5933 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5934 | CONVERT_DOUBLE_CHECKED(number, args[0]); |
| 5935 | |
| 5936 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5937 | if (number > 0 && number <= Smi::kMaxValue) { |
| 5938 | return Smi::FromInt(static_cast<int>(number)); |
| 5939 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5940 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5944 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 5945 | // a small integer. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5946 | static MaybeObject* Runtime_NumberToSmi(RUNTIME_CALLING_CONVENTION) { |
| 5947 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5948 | NoHandleAllocation ha; |
| 5949 | ASSERT(args.length() == 1); |
| 5950 | |
| 5951 | Object* obj = args[0]; |
| 5952 | if (obj->IsSmi()) { |
| 5953 | return obj; |
| 5954 | } |
| 5955 | if (obj->IsHeapNumber()) { |
| 5956 | double value = HeapNumber::cast(obj)->value(); |
| 5957 | int int_value = FastD2I(value); |
| 5958 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 5959 | return Smi::FromInt(int_value); |
| 5960 | } |
| 5961 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5962 | return isolate->heap()->nan_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5963 | } |
| 5964 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5965 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5966 | static MaybeObject* Runtime_AllocateHeapNumber(RUNTIME_CALLING_CONVENTION) { |
| 5967 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5968 | NoHandleAllocation ha; |
| 5969 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5970 | return isolate->heap()->AllocateHeapNumber(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5971 | } |
| 5972 | |
| 5973 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5974 | static MaybeObject* Runtime_NumberAdd(RUNTIME_CALLING_CONVENTION) { |
| 5975 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5976 | NoHandleAllocation ha; |
| 5977 | ASSERT(args.length() == 2); |
| 5978 | |
| 5979 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5980 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5981 | return isolate->heap()->NumberFromDouble(x + y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5985 | static MaybeObject* Runtime_NumberSub(RUNTIME_CALLING_CONVENTION) { |
| 5986 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5987 | NoHandleAllocation ha; |
| 5988 | ASSERT(args.length() == 2); |
| 5989 | |
| 5990 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5991 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5992 | return isolate->heap()->NumberFromDouble(x - y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
| 5995 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 5996 | static MaybeObject* Runtime_NumberMul(RUNTIME_CALLING_CONVENTION) { |
| 5997 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5998 | NoHandleAllocation ha; |
| 5999 | ASSERT(args.length() == 2); |
| 6000 | |
| 6001 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6002 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6003 | return isolate->heap()->NumberFromDouble(x * y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6007 | static MaybeObject* Runtime_NumberUnaryMinus(RUNTIME_CALLING_CONVENTION) { |
| 6008 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6009 | NoHandleAllocation ha; |
| 6010 | ASSERT(args.length() == 1); |
| 6011 | |
| 6012 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6013 | return isolate->heap()->NumberFromDouble(-x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
| 6016 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6017 | static MaybeObject* Runtime_NumberAlloc(RUNTIME_CALLING_CONVENTION) { |
| 6018 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6019 | NoHandleAllocation ha; |
| 6020 | ASSERT(args.length() == 0); |
| 6021 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6022 | return isolate->heap()->NumberFromDouble(9876543210.0); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6026 | static MaybeObject* Runtime_NumberDiv(RUNTIME_CALLING_CONVENTION) { |
| 6027 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6028 | NoHandleAllocation ha; |
| 6029 | ASSERT(args.length() == 2); |
| 6030 | |
| 6031 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6032 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6033 | return isolate->heap()->NumberFromDouble(x / y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6037 | static MaybeObject* Runtime_NumberMod(RUNTIME_CALLING_CONVENTION) { |
| 6038 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6039 | NoHandleAllocation ha; |
| 6040 | ASSERT(args.length() == 2); |
| 6041 | |
| 6042 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6043 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6044 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6045 | x = modulo(x, y); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6046 | // NumberFromDouble may return a Smi instead of a Number object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6047 | return isolate->heap()->NumberFromDouble(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6051 | static MaybeObject* Runtime_StringAdd(RUNTIME_CALLING_CONVENTION) { |
| 6052 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6053 | NoHandleAllocation ha; |
| 6054 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6055 | CONVERT_CHECKED(String, str1, args[0]); |
| 6056 | CONVERT_CHECKED(String, str2, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6057 | isolate->counters()->string_add_runtime()->Increment(); |
| 6058 | return isolate->heap()->AllocateConsString(str1, str2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6062 | template <typename sinkchar> |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6063 | static inline void StringBuilderConcatHelper(String* special, |
| 6064 | sinkchar* sink, |
| 6065 | FixedArray* fixed_array, |
| 6066 | int array_length) { |
| 6067 | int position = 0; |
| 6068 | for (int i = 0; i < array_length; i++) { |
| 6069 | Object* element = fixed_array->get(i); |
| 6070 | if (element->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6071 | // Smi encoding of position and length. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6072 | int encoded_slice = Smi::cast(element)->value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6073 | int pos; |
| 6074 | int len; |
| 6075 | if (encoded_slice > 0) { |
| 6076 | // Position and length encoded in one smi. |
| 6077 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 6078 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 6079 | } else { |
| 6080 | // Position and length encoded in two smis. |
| 6081 | Object* obj = fixed_array->get(++i); |
| 6082 | ASSERT(obj->IsSmi()); |
| 6083 | pos = Smi::cast(obj)->value(); |
| 6084 | len = -encoded_slice; |
| 6085 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6086 | String::WriteToFlat(special, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6087 | sink + position, |
| 6088 | pos, |
| 6089 | pos + len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6090 | position += len; |
| 6091 | } else { |
| 6092 | String* string = String::cast(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6093 | int element_length = string->length(); |
| 6094 | String::WriteToFlat(string, sink + position, 0, element_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6095 | position += element_length; |
| 6096 | } |
| 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6101 | static MaybeObject* Runtime_StringBuilderConcat(RUNTIME_CALLING_CONVENTION) { |
| 6102 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6103 | NoHandleAllocation ha; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6104 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6105 | CONVERT_CHECKED(JSArray, array, args[0]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6106 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6107 | isolate->context()->mark_out_of_memory(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6108 | return Failure::OutOfMemoryException(); |
| 6109 | } |
| 6110 | int array_length = Smi::cast(args[1])->value(); |
| 6111 | CONVERT_CHECKED(String, special, args[2]); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6112 | |
| 6113 | // This assumption is used by the slice encoding in one or two smis. |
| 6114 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6115 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6116 | int special_length = special->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6117 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6118 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6119 | } |
| 6120 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6121 | if (fixed_array->length() < array_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6122 | array_length = fixed_array->length(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6123 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6124 | |
| 6125 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6126 | return isolate->heap()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6127 | } else if (array_length == 1) { |
| 6128 | Object* first = fixed_array->get(0); |
| 6129 | if (first->IsString()) return first; |
| 6130 | } |
| 6131 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6132 | bool ascii = special->HasOnlyAsciiChars(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6133 | int position = 0; |
| 6134 | for (int i = 0; i < array_length; i++) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6135 | int increment = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6136 | Object* elt = fixed_array->get(i); |
| 6137 | if (elt->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6138 | // Smi encoding of position and length. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6139 | int smi_value = Smi::cast(elt)->value(); |
| 6140 | int pos; |
| 6141 | int len; |
| 6142 | if (smi_value > 0) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6143 | // Position and length encoded in one smi. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6144 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6145 | len = StringBuilderSubstringLength::decode(smi_value); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6146 | } else { |
| 6147 | // Position and length encoded in two smis. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6148 | len = -smi_value; |
| 6149 | // Get the position and check that it is a positive smi. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6150 | i++; |
| 6151 | if (i >= array_length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6152 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6153 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6154 | Object* next_smi = fixed_array->get(i); |
| 6155 | if (!next_smi->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6156 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6157 | } |
| 6158 | pos = Smi::cast(next_smi)->value(); |
| 6159 | if (pos < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6160 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6161 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6162 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6163 | ASSERT(pos >= 0); |
| 6164 | ASSERT(len >= 0); |
| 6165 | if (pos > special_length || len > special_length - pos) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6166 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6167 | } |
| 6168 | increment = len; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6169 | } else if (elt->IsString()) { |
| 6170 | String* element = String::cast(elt); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6171 | int element_length = element->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6172 | increment = element_length; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6173 | if (ascii && !element->HasOnlyAsciiChars()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6174 | ascii = false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6175 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6176 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6177 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6178 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6179 | if (increment > String::kMaxLength - position) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6180 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6181 | return Failure::OutOfMemoryException(); |
| 6182 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6183 | position += increment; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | int length = position; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6187 | Object* object; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6188 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6189 | if (ascii) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6190 | { MaybeObject* maybe_object = |
| 6191 | isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6192 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6193 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6194 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6195 | StringBuilderConcatHelper(special, |
| 6196 | answer->GetChars(), |
| 6197 | fixed_array, |
| 6198 | array_length); |
| 6199 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6200 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6201 | { MaybeObject* maybe_object = |
| 6202 | isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6203 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6204 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6205 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6206 | StringBuilderConcatHelper(special, |
| 6207 | answer->GetChars(), |
| 6208 | fixed_array, |
| 6209 | array_length); |
| 6210 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6211 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
| 6214 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6215 | static MaybeObject* Runtime_StringBuilderJoin(RUNTIME_CALLING_CONVENTION) { |
| 6216 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6217 | NoHandleAllocation ha; |
| 6218 | ASSERT(args.length() == 3); |
| 6219 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 6220 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6221 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6222 | return Failure::OutOfMemoryException(); |
| 6223 | } |
| 6224 | int array_length = Smi::cast(args[1])->value(); |
| 6225 | CONVERT_CHECKED(String, separator, args[2]); |
| 6226 | |
| 6227 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6228 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6229 | } |
| 6230 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6231 | if (fixed_array->length() < array_length) { |
| 6232 | array_length = fixed_array->length(); |
| 6233 | } |
| 6234 | |
| 6235 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6236 | return isolate->heap()->empty_string(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6237 | } else if (array_length == 1) { |
| 6238 | Object* first = fixed_array->get(0); |
| 6239 | if (first->IsString()) return first; |
| 6240 | } |
| 6241 | |
| 6242 | int separator_length = separator->length(); |
| 6243 | int max_nof_separators = |
| 6244 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6245 | if (max_nof_separators < (array_length - 1)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6246 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6247 | return Failure::OutOfMemoryException(); |
| 6248 | } |
| 6249 | int length = (array_length - 1) * separator_length; |
| 6250 | for (int i = 0; i < array_length; i++) { |
| 6251 | Object* element_obj = fixed_array->get(i); |
| 6252 | if (!element_obj->IsString()) { |
| 6253 | // TODO(1161): handle this case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6254 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6255 | } |
| 6256 | String* element = String::cast(element_obj); |
| 6257 | int increment = element->length(); |
| 6258 | if (increment > String::kMaxLength - length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6259 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6260 | return Failure::OutOfMemoryException(); |
| 6261 | } |
| 6262 | length += increment; |
| 6263 | } |
| 6264 | |
| 6265 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6266 | { MaybeObject* maybe_object = |
| 6267 | isolate->heap()->AllocateRawTwoByteString(length); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6268 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6269 | } |
| 6270 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6271 | |
| 6272 | uc16* sink = answer->GetChars(); |
| 6273 | #ifdef DEBUG |
| 6274 | uc16* end = sink + length; |
| 6275 | #endif |
| 6276 | |
| 6277 | String* first = String::cast(fixed_array->get(0)); |
| 6278 | int first_length = first->length(); |
| 6279 | String::WriteToFlat(first, sink, 0, first_length); |
| 6280 | sink += first_length; |
| 6281 | |
| 6282 | for (int i = 1; i < array_length; i++) { |
| 6283 | ASSERT(sink + separator_length <= end); |
| 6284 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6285 | sink += separator_length; |
| 6286 | |
| 6287 | String* element = String::cast(fixed_array->get(i)); |
| 6288 | int element_length = element->length(); |
| 6289 | ASSERT(sink + element_length <= end); |
| 6290 | String::WriteToFlat(element, sink, 0, element_length); |
| 6291 | sink += element_length; |
| 6292 | } |
| 6293 | ASSERT(sink == end); |
| 6294 | |
| 6295 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6296 | return answer; |
| 6297 | } |
| 6298 | |
| 6299 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6300 | static MaybeObject* Runtime_NumberOr(RUNTIME_CALLING_CONVENTION) { |
| 6301 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6302 | NoHandleAllocation ha; |
| 6303 | ASSERT(args.length() == 2); |
| 6304 | |
| 6305 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6306 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6307 | return isolate->heap()->NumberFromInt32(x | y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
| 6310 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6311 | static MaybeObject* Runtime_NumberAnd(RUNTIME_CALLING_CONVENTION) { |
| 6312 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6313 | NoHandleAllocation ha; |
| 6314 | ASSERT(args.length() == 2); |
| 6315 | |
| 6316 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6317 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6318 | return isolate->heap()->NumberFromInt32(x & y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6319 | } |
| 6320 | |
| 6321 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6322 | static MaybeObject* Runtime_NumberXor(RUNTIME_CALLING_CONVENTION) { |
| 6323 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6324 | NoHandleAllocation ha; |
| 6325 | ASSERT(args.length() == 2); |
| 6326 | |
| 6327 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6328 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6329 | return isolate->heap()->NumberFromInt32(x ^ y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6333 | static MaybeObject* Runtime_NumberNot(RUNTIME_CALLING_CONVENTION) { |
| 6334 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6335 | NoHandleAllocation ha; |
| 6336 | ASSERT(args.length() == 1); |
| 6337 | |
| 6338 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6339 | return isolate->heap()->NumberFromInt32(~x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6343 | static MaybeObject* Runtime_NumberShl(RUNTIME_CALLING_CONVENTION) { |
| 6344 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6345 | NoHandleAllocation ha; |
| 6346 | ASSERT(args.length() == 2); |
| 6347 | |
| 6348 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6349 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6350 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
| 6353 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6354 | static MaybeObject* Runtime_NumberShr(RUNTIME_CALLING_CONVENTION) { |
| 6355 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6356 | NoHandleAllocation ha; |
| 6357 | ASSERT(args.length() == 2); |
| 6358 | |
| 6359 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 6360 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6361 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
| 6364 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6365 | static MaybeObject* Runtime_NumberSar(RUNTIME_CALLING_CONVENTION) { |
| 6366 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6367 | NoHandleAllocation ha; |
| 6368 | ASSERT(args.length() == 2); |
| 6369 | |
| 6370 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6371 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6372 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6376 | static MaybeObject* Runtime_NumberEquals(RUNTIME_CALLING_CONVENTION) { |
| 6377 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6378 | NoHandleAllocation ha; |
| 6379 | ASSERT(args.length() == 2); |
| 6380 | |
| 6381 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6382 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6383 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 6384 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 6385 | if (x == y) return Smi::FromInt(EQUAL); |
| 6386 | Object* result; |
| 6387 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 6388 | result = Smi::FromInt(EQUAL); |
| 6389 | } else { |
| 6390 | result = Smi::FromInt(NOT_EQUAL); |
| 6391 | } |
| 6392 | return result; |
| 6393 | } |
| 6394 | |
| 6395 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6396 | static MaybeObject* Runtime_StringEquals(RUNTIME_CALLING_CONVENTION) { |
| 6397 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6398 | NoHandleAllocation ha; |
| 6399 | ASSERT(args.length() == 2); |
| 6400 | |
| 6401 | CONVERT_CHECKED(String, x, args[0]); |
| 6402 | CONVERT_CHECKED(String, y, args[1]); |
| 6403 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6404 | bool not_equal = !x->Equals(y); |
| 6405 | // This is slightly convoluted because the value that signifies |
| 6406 | // equality is 0 and inequality is 1 so we have to negate the result |
| 6407 | // from String::Equals. |
| 6408 | ASSERT(not_equal == 0 || not_equal == 1); |
| 6409 | STATIC_CHECK(EQUAL == 0); |
| 6410 | STATIC_CHECK(NOT_EQUAL == 1); |
| 6411 | return Smi::FromInt(not_equal); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6412 | } |
| 6413 | |
| 6414 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6415 | static MaybeObject* Runtime_NumberCompare(RUNTIME_CALLING_CONVENTION) { |
| 6416 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6417 | NoHandleAllocation ha; |
| 6418 | ASSERT(args.length() == 3); |
| 6419 | |
| 6420 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6421 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6422 | if (isnan(x) || isnan(y)) return args[2]; |
| 6423 | if (x == y) return Smi::FromInt(EQUAL); |
| 6424 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 6425 | return Smi::FromInt(GREATER); |
| 6426 | } |
| 6427 | |
| 6428 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6429 | // Compare two Smis as if they were converted to strings and then |
| 6430 | // compared lexicographically. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6431 | static MaybeObject* Runtime_SmiLexicographicCompare( |
| 6432 | RUNTIME_CALLING_CONVENTION) { |
| 6433 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6434 | NoHandleAllocation ha; |
| 6435 | ASSERT(args.length() == 2); |
| 6436 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6437 | // Extract the integer values from the Smis. |
| 6438 | CONVERT_CHECKED(Smi, x, args[0]); |
| 6439 | CONVERT_CHECKED(Smi, y, args[1]); |
| 6440 | int x_value = x->value(); |
| 6441 | int y_value = y->value(); |
| 6442 | |
| 6443 | // If the integers are equal so are the string representations. |
| 6444 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 6445 | |
| 6446 | // If one of the integers are zero the normal integer order is the |
| 6447 | // same as the lexicographic order of the string representations. |
| 6448 | if (x_value == 0 || y_value == 0) return Smi::FromInt(x_value - y_value); |
| 6449 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6450 | // If only one of the integers is negative the negative number is |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6451 | // smallest because the char code of '-' is less than the char code |
| 6452 | // of any digit. Otherwise, we make both values positive. |
| 6453 | if (x_value < 0 || y_value < 0) { |
| 6454 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 6455 | if (x_value >= 0) return Smi::FromInt(GREATER); |
| 6456 | x_value = -x_value; |
| 6457 | y_value = -y_value; |
| 6458 | } |
| 6459 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6460 | // Arrays for the individual characters of the two Smis. Smis are |
| 6461 | // 31 bit integers and 10 decimal digits are therefore enough. |
| 6462 | // TODO(isolates): maybe we should simply allocate 20 bytes on the stack. |
| 6463 | int* x_elms = isolate->runtime_state()->smi_lexicographic_compare_x_elms(); |
| 6464 | int* y_elms = isolate->runtime_state()->smi_lexicographic_compare_y_elms(); |
| 6465 | |
| 6466 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6467 | // Convert the integers to arrays of their decimal digits. |
| 6468 | int x_index = 0; |
| 6469 | int y_index = 0; |
| 6470 | while (x_value > 0) { |
| 6471 | x_elms[x_index++] = x_value % 10; |
| 6472 | x_value /= 10; |
| 6473 | } |
| 6474 | while (y_value > 0) { |
| 6475 | y_elms[y_index++] = y_value % 10; |
| 6476 | y_value /= 10; |
| 6477 | } |
| 6478 | |
| 6479 | // Loop through the arrays of decimal digits finding the first place |
| 6480 | // where they differ. |
| 6481 | while (--x_index >= 0 && --y_index >= 0) { |
| 6482 | int diff = x_elms[x_index] - y_elms[y_index]; |
| 6483 | if (diff != 0) return Smi::FromInt(diff); |
| 6484 | } |
| 6485 | |
| 6486 | // If one array is a suffix of the other array, the longest array is |
| 6487 | // the representation of the largest of the Smis in the |
| 6488 | // lexicographic ordering. |
| 6489 | return Smi::FromInt(x_index - y_index); |
| 6490 | } |
| 6491 | |
| 6492 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6493 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 6494 | String* x, |
| 6495 | String* y) { |
| 6496 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 6497 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6498 | bufx.Reset(x); |
| 6499 | bufy.Reset(y); |
| 6500 | while (bufx.has_more() && bufy.has_more()) { |
| 6501 | int d = bufx.GetNext() - bufy.GetNext(); |
| 6502 | if (d < 0) return Smi::FromInt(LESS); |
| 6503 | else if (d > 0) return Smi::FromInt(GREATER); |
| 6504 | } |
| 6505 | |
| 6506 | // x is (non-trivial) prefix of y: |
| 6507 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 6508 | // y is prefix of x: |
| 6509 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 6510 | } |
| 6511 | |
| 6512 | |
| 6513 | static Object* FlatStringCompare(String* x, String* y) { |
| 6514 | ASSERT(x->IsFlat()); |
| 6515 | ASSERT(y->IsFlat()); |
| 6516 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 6517 | int prefix_length = x->length(); |
| 6518 | if (y->length() < prefix_length) { |
| 6519 | prefix_length = y->length(); |
| 6520 | equal_prefix_result = Smi::FromInt(GREATER); |
| 6521 | } else if (y->length() > prefix_length) { |
| 6522 | equal_prefix_result = Smi::FromInt(LESS); |
| 6523 | } |
| 6524 | int r; |
| 6525 | if (x->IsAsciiRepresentation()) { |
| 6526 | Vector<const char> x_chars = x->ToAsciiVector(); |
| 6527 | if (y->IsAsciiRepresentation()) { |
| 6528 | Vector<const char> y_chars = y->ToAsciiVector(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6529 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6530 | } else { |
| 6531 | Vector<const uc16> y_chars = y->ToUC16Vector(); |
| 6532 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6533 | } |
| 6534 | } else { |
| 6535 | Vector<const uc16> x_chars = x->ToUC16Vector(); |
| 6536 | if (y->IsAsciiRepresentation()) { |
| 6537 | Vector<const char> y_chars = y->ToAsciiVector(); |
| 6538 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6539 | } else { |
| 6540 | Vector<const uc16> y_chars = y->ToUC16Vector(); |
| 6541 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6542 | } |
| 6543 | } |
| 6544 | Object* result; |
| 6545 | if (r == 0) { |
| 6546 | result = equal_prefix_result; |
| 6547 | } else { |
| 6548 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 6549 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6550 | ASSERT(result == |
| 6551 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6552 | return result; |
| 6553 | } |
| 6554 | |
| 6555 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6556 | static MaybeObject* Runtime_StringCompare(RUNTIME_CALLING_CONVENTION) { |
| 6557 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6558 | NoHandleAllocation ha; |
| 6559 | ASSERT(args.length() == 2); |
| 6560 | |
| 6561 | CONVERT_CHECKED(String, x, args[0]); |
| 6562 | CONVERT_CHECKED(String, y, args[1]); |
| 6563 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6564 | isolate->counters()->string_compare_runtime()->Increment(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6565 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6566 | // A few fast case tests before we flatten. |
| 6567 | if (x == y) return Smi::FromInt(EQUAL); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6568 | if (y->length() == 0) { |
| 6569 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6570 | return Smi::FromInt(GREATER); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6571 | } else if (x->length() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6572 | return Smi::FromInt(LESS); |
| 6573 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6574 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6575 | int d = x->Get(0) - y->Get(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6576 | if (d < 0) return Smi::FromInt(LESS); |
| 6577 | else if (d > 0) return Smi::FromInt(GREATER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6578 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6579 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6580 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6581 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6582 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6583 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6584 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6585 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6586 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6587 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6588 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
| 6591 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6592 | static MaybeObject* Runtime_Math_acos(RUNTIME_CALLING_CONVENTION) { |
| 6593 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6594 | NoHandleAllocation ha; |
| 6595 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6596 | isolate->counters()->math_acos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6597 | |
| 6598 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6599 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6603 | static MaybeObject* Runtime_Math_asin(RUNTIME_CALLING_CONVENTION) { |
| 6604 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6605 | NoHandleAllocation ha; |
| 6606 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6607 | isolate->counters()->math_asin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6608 | |
| 6609 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6610 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6614 | static MaybeObject* Runtime_Math_atan(RUNTIME_CALLING_CONVENTION) { |
| 6615 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6616 | NoHandleAllocation ha; |
| 6617 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6618 | isolate->counters()->math_atan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6619 | |
| 6620 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6621 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6625 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 6626 | |
| 6627 | |
| 6628 | static MaybeObject* Runtime_Math_atan2(RUNTIME_CALLING_CONVENTION) { |
| 6629 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6630 | NoHandleAllocation ha; |
| 6631 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6632 | isolate->counters()->math_atan2()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6633 | |
| 6634 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6635 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6636 | double result; |
| 6637 | if (isinf(x) && isinf(y)) { |
| 6638 | // Make sure that the result in case of two infinite arguments |
| 6639 | // is a multiple of Pi / 4. The sign of the result is determined |
| 6640 | // by the first argument (x) and the sign of the second argument |
| 6641 | // determines the multiplier: one or three. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6642 | int multiplier = (x < 0) ? -1 : 1; |
| 6643 | if (y < 0) multiplier *= 3; |
| 6644 | result = multiplier * kPiDividedBy4; |
| 6645 | } else { |
| 6646 | result = atan2(x, y); |
| 6647 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6648 | return isolate->heap()->AllocateHeapNumber(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6652 | static MaybeObject* Runtime_Math_ceil(RUNTIME_CALLING_CONVENTION) { |
| 6653 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6654 | NoHandleAllocation ha; |
| 6655 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6656 | isolate->counters()->math_ceil()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6657 | |
| 6658 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6659 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
| 6662 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6663 | static MaybeObject* Runtime_Math_cos(RUNTIME_CALLING_CONVENTION) { |
| 6664 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6665 | NoHandleAllocation ha; |
| 6666 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6667 | isolate->counters()->math_cos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6668 | |
| 6669 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6670 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
| 6673 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6674 | static MaybeObject* Runtime_Math_exp(RUNTIME_CALLING_CONVENTION) { |
| 6675 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6676 | NoHandleAllocation ha; |
| 6677 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6678 | isolate->counters()->math_exp()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6679 | |
| 6680 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6681 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6685 | static MaybeObject* Runtime_Math_floor(RUNTIME_CALLING_CONVENTION) { |
| 6686 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6687 | NoHandleAllocation ha; |
| 6688 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6689 | isolate->counters()->math_floor()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6690 | |
| 6691 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6692 | return isolate->heap()->NumberFromDouble(floor(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6693 | } |
| 6694 | |
| 6695 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6696 | static MaybeObject* Runtime_Math_log(RUNTIME_CALLING_CONVENTION) { |
| 6697 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6698 | NoHandleAllocation ha; |
| 6699 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6700 | isolate->counters()->math_log()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6701 | |
| 6702 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6703 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6707 | static MaybeObject* Runtime_Math_pow(RUNTIME_CALLING_CONVENTION) { |
| 6708 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6709 | NoHandleAllocation ha; |
| 6710 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6711 | isolate->counters()->math_pow()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6712 | |
| 6713 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6714 | |
| 6715 | // If the second argument is a smi, it is much faster to call the |
| 6716 | // custom powi() function than the generic pow(). |
| 6717 | if (args[1]->IsSmi()) { |
| 6718 | int y = Smi::cast(args[1])->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6719 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6720 | } |
| 6721 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6722 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6723 | return isolate->heap()->AllocateHeapNumber(power_double_double(x, y)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6726 | // Fast version of Math.pow if we know that y is not an integer and |
| 6727 | // y is not -0.5 or 0.5. Used as slowcase from codegen. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6728 | static MaybeObject* Runtime_Math_pow_cfunction(RUNTIME_CALLING_CONVENTION) { |
| 6729 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6730 | NoHandleAllocation ha; |
| 6731 | ASSERT(args.length() == 2); |
| 6732 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6733 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6734 | if (y == 0) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6735 | return Smi::FromInt(1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6736 | } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6737 | return isolate->heap()->nan_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6738 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6739 | return isolate->heap()->AllocateHeapNumber(pow(x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6740 | } |
| 6741 | } |
| 6742 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6743 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6744 | static MaybeObject* Runtime_RoundNumber(RUNTIME_CALLING_CONVENTION) { |
| 6745 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6746 | NoHandleAllocation ha; |
| 6747 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6748 | isolate->counters()->math_round()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6749 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 6750 | if (!args[0]->IsHeapNumber()) { |
| 6751 | // Must be smi. Return the argument unchanged for all the other types |
| 6752 | // to make fuzz-natives test happy. |
| 6753 | return args[0]; |
| 6754 | } |
| 6755 | |
| 6756 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 6757 | |
| 6758 | double value = number->value(); |
| 6759 | int exponent = number->get_exponent(); |
| 6760 | int sign = number->get_sign(); |
| 6761 | |
| 6762 | // We compare with kSmiValueSize - 3 because (2^30 - 0.1) has exponent 29 and |
| 6763 | // should be rounded to 2^30, which is not smi. |
| 6764 | if (!sign && exponent <= kSmiValueSize - 3) { |
| 6765 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 6766 | } |
| 6767 | |
| 6768 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 6769 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 6770 | if (exponent >= 52) { |
| 6771 | return number; |
| 6772 | } |
| 6773 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6774 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 6775 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6776 | // Do not call NumberFromDouble() to avoid extra checks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6777 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6781 | static MaybeObject* Runtime_Math_sin(RUNTIME_CALLING_CONVENTION) { |
| 6782 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6783 | NoHandleAllocation ha; |
| 6784 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6785 | isolate->counters()->math_sin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6786 | |
| 6787 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6788 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6792 | static MaybeObject* Runtime_Math_sqrt(RUNTIME_CALLING_CONVENTION) { |
| 6793 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6794 | NoHandleAllocation ha; |
| 6795 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6796 | isolate->counters()->math_sqrt()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6797 | |
| 6798 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6799 | return isolate->heap()->AllocateHeapNumber(sqrt(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6803 | static MaybeObject* Runtime_Math_tan(RUNTIME_CALLING_CONVENTION) { |
| 6804 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6805 | NoHandleAllocation ha; |
| 6806 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6807 | isolate->counters()->math_tan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6808 | |
| 6809 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6810 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
| 6813 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6814 | static int MakeDay(int year, int month, int day) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6815 | static const int day_from_month[] = {0, 31, 59, 90, 120, 151, |
| 6816 | 181, 212, 243, 273, 304, 334}; |
| 6817 | static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152, |
| 6818 | 182, 213, 244, 274, 305, 335}; |
| 6819 | |
| 6820 | year += month / 12; |
| 6821 | month %= 12; |
| 6822 | if (month < 0) { |
| 6823 | year--; |
| 6824 | month += 12; |
| 6825 | } |
| 6826 | |
| 6827 | ASSERT(month >= 0); |
| 6828 | ASSERT(month < 12); |
| 6829 | |
| 6830 | // year_delta is an arbitrary number such that: |
| 6831 | // a) year_delta = -1 (mod 400) |
| 6832 | // b) year + year_delta > 0 for years in the range defined by |
| 6833 | // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
| 6834 | // Jan 1 1970. This is required so that we don't run into integer |
| 6835 | // division of negative numbers. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6836 | // c) there shouldn't be an overflow for 32-bit integers in the following |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6837 | // operations. |
| 6838 | static const int year_delta = 399999; |
| 6839 | static const int base_day = 365 * (1970 + year_delta) + |
| 6840 | (1970 + year_delta) / 4 - |
| 6841 | (1970 + year_delta) / 100 + |
| 6842 | (1970 + year_delta) / 400; |
| 6843 | |
| 6844 | int year1 = year + year_delta; |
| 6845 | int day_from_year = 365 * year1 + |
| 6846 | year1 / 4 - |
| 6847 | year1 / 100 + |
| 6848 | year1 / 400 - |
| 6849 | base_day; |
| 6850 | |
| 6851 | if (year % 4 || (year % 100 == 0 && year % 400 != 0)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6852 | return day_from_year + day_from_month[month] + day - 1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6853 | } |
| 6854 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6855 | return day_from_year + day_from_month_leap[month] + day - 1; |
| 6856 | } |
| 6857 | |
| 6858 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 6859 | static MaybeObject* Runtime_DateMakeDay(RUNTIME_CALLING_CONVENTION) { |
| 6860 | RUNTIME_GET_ISOLATE; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6861 | NoHandleAllocation ha; |
| 6862 | ASSERT(args.length() == 3); |
| 6863 | |
| 6864 | CONVERT_SMI_CHECKED(year, args[0]); |
| 6865 | CONVERT_SMI_CHECKED(month, args[1]); |
| 6866 | CONVERT_SMI_CHECKED(date, args[2]); |
| 6867 | |
| 6868 | return Smi::FromInt(MakeDay(year, month, date)); |
| 6869 | } |
| 6870 | |
| 6871 | |
| 6872 | static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1}; |
| 6873 | static const int kDaysIn4Years = 4 * 365 + 1; |
| 6874 | static const int kDaysIn100Years = 25 * kDaysIn4Years - 1; |
| 6875 | static const int kDaysIn400Years = 4 * kDaysIn100Years + 1; |
| 6876 | static const int kDays1970to2000 = 30 * 365 + 7; |
| 6877 | static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - |
| 6878 | kDays1970to2000; |
| 6879 | static const int kYearsOffset = 400000; |
| 6880 | |
| 6881 | static const char kDayInYear[] = { |
| 6882 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6883 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6884 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6885 | 22, 23, 24, 25, 26, 27, 28, |
| 6886 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6887 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6888 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6889 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6890 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6891 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6892 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6893 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6894 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6895 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6896 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6897 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6898 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6899 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6900 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6901 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6902 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6903 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6904 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6905 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6906 | |
| 6907 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6908 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6909 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6910 | 22, 23, 24, 25, 26, 27, 28, |
| 6911 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6912 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6913 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6914 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6915 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6916 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6917 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6918 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6919 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6920 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6921 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6922 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6923 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6924 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6925 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6926 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6927 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6928 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6929 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6930 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6931 | |
| 6932 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6933 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6934 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6935 | 22, 23, 24, 25, 26, 27, 28, 29, |
| 6936 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6937 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6938 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6939 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6940 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6941 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6942 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6943 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6944 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6945 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6946 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6947 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6948 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6949 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6950 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6951 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6952 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6953 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6954 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6955 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6956 | |
| 6957 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6958 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6959 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6960 | 22, 23, 24, 25, 26, 27, 28, |
| 6961 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6962 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6963 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6964 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6965 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6966 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6967 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6968 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6969 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6970 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6971 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6972 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6973 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6974 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6975 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6976 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6977 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6978 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6979 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6980 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 6981 | |
| 6982 | static const char kMonthInYear[] = { |
| 6983 | 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, |
| 6984 | 0, 0, 0, 0, 0, 0, |
| 6985 | 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, |
| 6986 | 1, 1, 1, |
| 6987 | 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, |
| 6988 | 2, 2, 2, 2, 2, 2, |
| 6989 | 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, |
| 6990 | 3, 3, 3, 3, 3, |
| 6991 | 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, |
| 6992 | 4, 4, 4, 4, 4, 4, |
| 6993 | 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, |
| 6994 | 5, 5, 5, 5, 5, |
| 6995 | 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, |
| 6996 | 6, 6, 6, 6, 6, 6, |
| 6997 | 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, |
| 6998 | 7, 7, 7, 7, 7, 7, |
| 6999 | 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, |
| 7000 | 8, 8, 8, 8, 8, |
| 7001 | 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, |
| 7002 | 9, 9, 9, 9, 9, 9, |
| 7003 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7004 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7005 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7006 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7007 | |
| 7008 | 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, |
| 7009 | 0, 0, 0, 0, 0, 0, |
| 7010 | 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, |
| 7011 | 1, 1, 1, |
| 7012 | 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, |
| 7013 | 2, 2, 2, 2, 2, 2, |
| 7014 | 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, |
| 7015 | 3, 3, 3, 3, 3, |
| 7016 | 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, |
| 7017 | 4, 4, 4, 4, 4, 4, |
| 7018 | 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, |
| 7019 | 5, 5, 5, 5, 5, |
| 7020 | 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, |
| 7021 | 6, 6, 6, 6, 6, 6, |
| 7022 | 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, |
| 7023 | 7, 7, 7, 7, 7, 7, |
| 7024 | 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, |
| 7025 | 8, 8, 8, 8, 8, |
| 7026 | 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, |
| 7027 | 9, 9, 9, 9, 9, 9, |
| 7028 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7029 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7030 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7031 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7032 | |
| 7033 | 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, |
| 7034 | 0, 0, 0, 0, 0, 0, |
| 7035 | 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, |
| 7036 | 1, 1, 1, 1, |
| 7037 | 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, |
| 7038 | 2, 2, 2, 2, 2, 2, |
| 7039 | 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, |
| 7040 | 3, 3, 3, 3, 3, |
| 7041 | 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, |
| 7042 | 4, 4, 4, 4, 4, 4, |
| 7043 | 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, |
| 7044 | 5, 5, 5, 5, 5, |
| 7045 | 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, |
| 7046 | 6, 6, 6, 6, 6, 6, |
| 7047 | 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, |
| 7048 | 7, 7, 7, 7, 7, 7, |
| 7049 | 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, |
| 7050 | 8, 8, 8, 8, 8, |
| 7051 | 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, |
| 7052 | 9, 9, 9, 9, 9, 9, |
| 7053 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7054 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7055 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7056 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7057 | |
| 7058 | 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, |
| 7059 | 0, 0, 0, 0, 0, 0, |
| 7060 | 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, |
| 7061 | 1, 1, 1, |
| 7062 | 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, |
| 7063 | 2, 2, 2, 2, 2, 2, |
| 7064 | 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, |
| 7065 | 3, 3, 3, 3, 3, |
| 7066 | 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, |
| 7067 | 4, 4, 4, 4, 4, 4, |
| 7068 | 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, |
| 7069 | 5, 5, 5, 5, 5, |
| 7070 | 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, |
| 7071 | 6, 6, 6, 6, 6, 6, |
| 7072 | 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, |
| 7073 | 7, 7, 7, 7, 7, 7, |
| 7074 | 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, |
| 7075 | 8, 8, 8, 8, 8, |
| 7076 | 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, |
| 7077 | 9, 9, 9, 9, 9, 9, |
| 7078 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7079 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7080 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7081 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; |
| 7082 | |
| 7083 | |
| 7084 | // This function works for dates from 1970 to 2099. |
| 7085 | static inline void DateYMDFromTimeAfter1970(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7086 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7087 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7088 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7089 | #endif |
| 7090 | |
| 7091 | year = 1970 + (4 * date + 2) / kDaysIn4Years; |
| 7092 | date %= kDaysIn4Years; |
| 7093 | |
| 7094 | month = kMonthInYear[date]; |
| 7095 | day = kDayInYear[date]; |
| 7096 | |
| 7097 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7098 | } |
| 7099 | |
| 7100 | |
| 7101 | static inline void DateYMDFromTimeSlow(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7102 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7103 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7104 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7105 | #endif |
| 7106 | |
| 7107 | date += kDaysOffset; |
| 7108 | year = 400 * (date / kDaysIn400Years) - kYearsOffset; |
| 7109 | date %= kDaysIn400Years; |
| 7110 | |
| 7111 | ASSERT(MakeDay(year, 0, 1) + date == save_date); |
| 7112 | |
| 7113 | date--; |
| 7114 | int yd1 = date / kDaysIn100Years; |
| 7115 | date %= kDaysIn100Years; |
| 7116 | year += 100 * yd1; |
| 7117 | |
| 7118 | date++; |
| 7119 | int yd2 = date / kDaysIn4Years; |
| 7120 | date %= kDaysIn4Years; |
| 7121 | year += 4 * yd2; |
| 7122 | |
| 7123 | date--; |
| 7124 | int yd3 = date / 365; |
| 7125 | date %= 365; |
| 7126 | year += yd3; |
| 7127 | |
| 7128 | bool is_leap = (!yd1 || yd2) && !yd3; |
| 7129 | |
| 7130 | ASSERT(date >= -1); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7131 | ASSERT(is_leap || (date >= 0)); |
| 7132 | ASSERT((date < 365) || (is_leap && (date < 366))); |
| 7133 | ASSERT(is_leap == ((year % 4 == 0) && (year % 100 || (year % 400 == 0)))); |
| 7134 | ASSERT(is_leap || ((MakeDay(year, 0, 1) + date) == save_date)); |
| 7135 | ASSERT(!is_leap || ((MakeDay(year, 0, 1) + date + 1) == save_date)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7136 | |
| 7137 | if (is_leap) { |
| 7138 | day = kDayInYear[2*365 + 1 + date]; |
| 7139 | month = kMonthInYear[2*365 + 1 + date]; |
| 7140 | } else { |
| 7141 | day = kDayInYear[date]; |
| 7142 | month = kMonthInYear[date]; |
| 7143 | } |
| 7144 | |
| 7145 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7146 | } |
| 7147 | |
| 7148 | |
| 7149 | static inline void DateYMDFromTime(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7150 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7151 | if (date >= 0 && date < 32 * kDaysIn4Years) { |
| 7152 | DateYMDFromTimeAfter1970(date, year, month, day); |
| 7153 | } else { |
| 7154 | DateYMDFromTimeSlow(date, year, month, day); |
| 7155 | } |
| 7156 | } |
| 7157 | |
| 7158 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7159 | static MaybeObject* Runtime_DateYMDFromTime(RUNTIME_CALLING_CONVENTION) { |
| 7160 | RUNTIME_GET_ISOLATE; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7161 | NoHandleAllocation ha; |
| 7162 | ASSERT(args.length() == 2); |
| 7163 | |
| 7164 | CONVERT_DOUBLE_CHECKED(t, args[0]); |
| 7165 | CONVERT_CHECKED(JSArray, res_array, args[1]); |
| 7166 | |
| 7167 | int year, month, day; |
| 7168 | DateYMDFromTime(static_cast<int>(floor(t / 86400000)), year, month, day); |
| 7169 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7170 | RUNTIME_ASSERT(res_array->elements()->map() == |
| 7171 | isolate->heap()->fixed_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 7172 | FixedArray* elms = FixedArray::cast(res_array->elements()); |
| 7173 | RUNTIME_ASSERT(elms->length() == 3); |
| 7174 | |
| 7175 | elms->set(0, Smi::FromInt(year)); |
| 7176 | elms->set(1, Smi::FromInt(month)); |
| 7177 | elms->set(2, Smi::FromInt(day)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7178 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7179 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7183 | static MaybeObject* Runtime_NewArgumentsFast(RUNTIME_CALLING_CONVENTION) { |
| 7184 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7185 | NoHandleAllocation ha; |
| 7186 | ASSERT(args.length() == 3); |
| 7187 | |
| 7188 | JSFunction* callee = JSFunction::cast(args[0]); |
| 7189 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7190 | const int length = Smi::cast(args[2])->value(); |
| 7191 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7192 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7193 | { MaybeObject* maybe_result = |
| 7194 | isolate->heap()->AllocateArgumentsObject(callee, length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7195 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7196 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7197 | // Allocate the elements if needed. |
| 7198 | if (length > 0) { |
| 7199 | // Allocate the fixed array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7200 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7201 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7202 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7203 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7204 | |
| 7205 | AssertNoAllocation no_gc; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 7206 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7207 | array->set_map(isolate->heap()->fixed_array_map()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7208 | array->set_length(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7209 | |
| 7210 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7211 | for (int i = 0; i < length; i++) { |
| 7212 | array->set(i, *--parameters, mode); |
| 7213 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7214 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7215 | } |
| 7216 | return result; |
| 7217 | } |
| 7218 | |
| 7219 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7220 | static MaybeObject* Runtime_NewClosure(RUNTIME_CALLING_CONVENTION) { |
| 7221 | RUNTIME_GET_ISOLATE; |
| 7222 | HandleScope scope(isolate); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7223 | ASSERT(args.length() == 3); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 7224 | CONVERT_ARG_CHECKED(Context, context, 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7225 | CONVERT_ARG_CHECKED(SharedFunctionInfo, shared, 1); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7226 | CONVERT_BOOLEAN_CHECKED(pretenure, args[2]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7227 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7228 | // Allocate global closures in old space and allocate local closures |
| 7229 | // in new space. Additionally pretenure closures that are assigned |
| 7230 | // directly to properties. |
| 7231 | pretenure = pretenure || (context->global_context() == *context); |
| 7232 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7233 | Handle<JSFunction> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7234 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 7235 | context, |
| 7236 | pretenure_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7237 | return *result; |
| 7238 | } |
| 7239 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7240 | static MaybeObject* Runtime_NewObjectFromBound(RUNTIME_CALLING_CONVENTION) { |
| 7241 | RUNTIME_GET_ISOLATE; |
| 7242 | HandleScope scope(isolate); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7243 | ASSERT(args.length() == 2); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7244 | // First argument is a function to use as a constructor. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7245 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7246 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7247 | // Second argument is either null or an array of bound arguments. |
| 7248 | FixedArray* bound_args = NULL; |
| 7249 | int bound_argc = 0; |
| 7250 | if (!args[1]->IsNull()) { |
| 7251 | CONVERT_ARG_CHECKED(JSArray, params, 1); |
| 7252 | RUNTIME_ASSERT(params->HasFastElements()); |
| 7253 | bound_args = FixedArray::cast(params->elements()); |
| 7254 | bound_argc = Smi::cast(params->length())->value(); |
| 7255 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7256 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7257 | // Find frame containing arguments passed to the caller. |
| 7258 | JavaScriptFrameIterator it; |
| 7259 | JavaScriptFrame* frame = it.frame(); |
| 7260 | ASSERT(!frame->is_optimized()); |
| 7261 | it.AdvanceToArgumentsFrame(); |
| 7262 | frame = it.frame(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 7263 | int argc = frame->ComputeParametersCount(); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7264 | |
| 7265 | // Prepend bound arguments to caller's arguments. |
| 7266 | int total_argc = bound_argc + argc; |
| 7267 | SmartPointer<Object**> param_data(NewArray<Object**>(total_argc)); |
| 7268 | for (int i = 0; i < bound_argc; i++) { |
| 7269 | Handle<Object> val = Handle<Object>(bound_args->get(i)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7270 | param_data[i] = val.location(); |
| 7271 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7272 | for (int i = 0; i < argc; i++) { |
| 7273 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
| 7274 | param_data[bound_argc + i] = val.location(); |
| 7275 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7276 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7277 | bool exception = false; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7278 | Handle<Object> result = |
| 7279 | Execution::New(function, total_argc, *param_data, &exception); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7280 | if (exception) { |
| 7281 | return Failure::Exception(); |
| 7282 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7283 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7284 | ASSERT(!result.is_null()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7285 | return *result; |
| 7286 | } |
| 7287 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7288 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7289 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 7290 | Handle<JSFunction> function) { |
| 7291 | Handle<Object> prototype = isolate->factory()->null_value(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7292 | if (function->has_instance_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7293 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7294 | } |
| 7295 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7296 | ConstructStubCompiler compiler; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 7297 | MaybeObject* code = compiler.CompileConstructStub(*function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7298 | if (!code->IsFailure()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7299 | function->shared()->set_construct_stub( |
| 7300 | Code::cast(code->ToObjectUnchecked())); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7301 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7302 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7306 | static MaybeObject* Runtime_NewObject(RUNTIME_CALLING_CONVENTION) { |
| 7307 | RUNTIME_GET_ISOLATE; |
| 7308 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7309 | ASSERT(args.length() == 1); |
| 7310 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7311 | Handle<Object> constructor = args.at<Object>(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7312 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7313 | // If the constructor isn't a proper function we throw a type error. |
| 7314 | if (!constructor->IsJSFunction()) { |
| 7315 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7316 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7317 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7318 | return isolate->Throw(*type_error); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7319 | } |
| 7320 | |
| 7321 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7322 | |
| 7323 | // If function should not have prototype, construction is not allowed. In this |
| 7324 | // case generated code bailouts here, since function has no initial_map. |
| 7325 | if (!function->should_have_prototype()) { |
| 7326 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7327 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7328 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7329 | return isolate->Throw(*type_error); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7330 | } |
| 7331 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7332 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7333 | Debug* debug = isolate->debug(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7334 | // Handle stepping into constructors if step into is active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7335 | if (debug->StepInActive()) { |
| 7336 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7337 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7338 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7339 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7340 | if (function->has_initial_map()) { |
| 7341 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7342 | // The 'Function' function ignores the receiver object when |
| 7343 | // called using 'new' and creates a new JSFunction object that |
| 7344 | // is returned. The receiver object is only used for error |
| 7345 | // reporting if an error occurs when constructing the new |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7346 | // JSFunction. FACTORY->NewJSObject() should not be used to |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7347 | // allocate JSFunctions since it does not properly initialize |
| 7348 | // the shared part of the function. Since the receiver is |
| 7349 | // ignored anyway, we use the global object as the receiver |
| 7350 | // instead of a new JSFunction object. This way, errors are |
| 7351 | // reported the same way whether or not 'Function' is called |
| 7352 | // using 'new'. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7353 | return isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7354 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7357 | // The function should be compiled for the optimization hints to be |
| 7358 | // available. We cannot use EnsureCompiled because that forces a |
| 7359 | // compilation through the shared function info which makes it |
| 7360 | // impossible for us to optimize. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7361 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7362 | if (!function->is_compiled()) CompileLazy(function, CLEAR_EXCEPTION); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7363 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7364 | if (!function->has_initial_map() && |
| 7365 | shared->IsInobjectSlackTrackingInProgress()) { |
| 7366 | // The tracking is already in progress for another function. We can only |
| 7367 | // track one initial_map at a time, so we force the completion before the |
| 7368 | // function is called as a constructor for the first time. |
| 7369 | shared->CompleteInobjectSlackTracking(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7370 | } |
| 7371 | |
| 7372 | bool first_allocation = !shared->live_objects_may_exist(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7373 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 7374 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7375 | // Delay setting the stub if inobject slack tracking is in progress. |
| 7376 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7377 | TrySettingInlineConstructStub(isolate, function); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7378 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7379 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7380 | isolate->counters()->constructed_objects()->Increment(); |
| 7381 | isolate->counters()->constructed_objects_runtime()->Increment(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7382 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7383 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7384 | } |
| 7385 | |
| 7386 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7387 | static MaybeObject* Runtime_FinalizeInstanceSize(RUNTIME_CALLING_CONVENTION) { |
| 7388 | RUNTIME_GET_ISOLATE; |
| 7389 | HandleScope scope(isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7390 | ASSERT(args.length() == 1); |
| 7391 | |
| 7392 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7393 | function->shared()->CompleteInobjectSlackTracking(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7394 | TrySettingInlineConstructStub(isolate, function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7395 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7396 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7397 | } |
| 7398 | |
| 7399 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7400 | static MaybeObject* Runtime_LazyCompile(RUNTIME_CALLING_CONVENTION) { |
| 7401 | RUNTIME_GET_ISOLATE; |
| 7402 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7403 | ASSERT(args.length() == 1); |
| 7404 | |
| 7405 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 7406 | #ifdef DEBUG |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7407 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7408 | PrintF("[lazy: "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7409 | function->PrintName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7410 | PrintF("]\n"); |
| 7411 | } |
| 7412 | #endif |
| 7413 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 7414 | // Compile the target function. Here we compile using CompileLazyInLoop in |
| 7415 | // order to get the optimized version. This helps code like delta-blue |
| 7416 | // that calls performance-critical routines through constructors. A |
| 7417 | // constructor call doesn't use a CallIC, it uses a LoadIC followed by a |
| 7418 | // direct call. Since the in-loop tracking takes place through CallICs |
| 7419 | // this means that things called through constructors are never known to |
| 7420 | // be in loops. We compile them as if they are in loops here just in case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7421 | ASSERT(!function->is_compiled()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 7422 | if (!CompileLazyInLoop(function, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7423 | return Failure::Exception(); |
| 7424 | } |
| 7425 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7426 | // All done. Return the compiled code. |
| 7427 | ASSERT(function->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7428 | return function->code(); |
| 7429 | } |
| 7430 | |
| 7431 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7432 | static MaybeObject* Runtime_LazyRecompile(RUNTIME_CALLING_CONVENTION) { |
| 7433 | RUNTIME_GET_ISOLATE; |
| 7434 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7435 | ASSERT(args.length() == 1); |
| 7436 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 7437 | // If the function is not optimizable or debugger is active continue using the |
| 7438 | // code from the full compiler. |
| 7439 | if (!function->shared()->code()->optimizable() || |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7440 | isolate->debug()->has_break_points()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7441 | if (FLAG_trace_opt) { |
| 7442 | PrintF("[failed to optimize "); |
| 7443 | function->PrintName(); |
| 7444 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 7445 | function->shared()->code()->optimizable() ? "T" : "F", |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7446 | isolate->debug()->has_break_points() ? "T" : "F"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7447 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7448 | function->ReplaceCode(function->shared()->code()); |
| 7449 | return function->code(); |
| 7450 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7451 | if (CompileOptimized(function, AstNode::kNoNumber, CLEAR_EXCEPTION)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7452 | return function->code(); |
| 7453 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7454 | if (FLAG_trace_opt) { |
| 7455 | PrintF("[failed to optimize "); |
| 7456 | function->PrintName(); |
| 7457 | PrintF(": optimized compilation failed]\n"); |
| 7458 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7459 | function->ReplaceCode(function->shared()->code()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7460 | return function->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
| 7463 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7464 | static MaybeObject* Runtime_NotifyDeoptimized(RUNTIME_CALLING_CONVENTION) { |
| 7465 | RUNTIME_GET_ISOLATE; |
| 7466 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7467 | ASSERT(args.length() == 1); |
| 7468 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 7469 | Deoptimizer::BailoutType type = |
| 7470 | static_cast<Deoptimizer::BailoutType>(Smi::cast(args[0])->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7471 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 7472 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7473 | int frames = deoptimizer->output_count(); |
| 7474 | |
| 7475 | JavaScriptFrameIterator it; |
| 7476 | JavaScriptFrame* frame = NULL; |
| 7477 | for (int i = 0; i < frames; i++) { |
| 7478 | if (i != 0) it.Advance(); |
| 7479 | frame = it.frame(); |
| 7480 | deoptimizer->InsertHeapNumberValues(frames - i - 1, frame); |
| 7481 | } |
| 7482 | delete deoptimizer; |
| 7483 | |
| 7484 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7485 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7486 | Handle<Object> arguments; |
| 7487 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7488 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7489 | if (arguments.is_null()) { |
| 7490 | // FunctionGetArguments can't throw an exception, so cast away the |
| 7491 | // doubt with an assert. |
| 7492 | arguments = Handle<Object>( |
| 7493 | Accessors::FunctionGetArguments(*function, |
| 7494 | NULL)->ToObjectUnchecked()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7495 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 7496 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7497 | } |
| 7498 | frame->SetExpression(i, *arguments); |
| 7499 | } |
| 7500 | } |
| 7501 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7502 | isolate->compilation_cache()->MarkForLazyOptimizing(function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7503 | if (type == Deoptimizer::EAGER) { |
| 7504 | RUNTIME_ASSERT(function->IsOptimized()); |
| 7505 | } else { |
| 7506 | RUNTIME_ASSERT(!function->IsOptimized()); |
| 7507 | } |
| 7508 | |
| 7509 | // Avoid doing too much work when running with --always-opt and keep |
| 7510 | // the optimized code around. |
| 7511 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7512 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
| 7515 | // Count the number of optimized activations of the function. |
| 7516 | int activations = 0; |
| 7517 | while (!it.done()) { |
| 7518 | JavaScriptFrame* frame = it.frame(); |
| 7519 | if (frame->is_optimized() && frame->function() == *function) { |
| 7520 | activations++; |
| 7521 | } |
| 7522 | it.Advance(); |
| 7523 | } |
| 7524 | |
| 7525 | // TODO(kasperl): For now, we cannot support removing the optimized |
| 7526 | // code when we have recursive invocations of the same function. |
| 7527 | if (activations == 0) { |
| 7528 | if (FLAG_trace_deopt) { |
| 7529 | PrintF("[removing optimized code for: "); |
| 7530 | function->PrintName(); |
| 7531 | PrintF("]\n"); |
| 7532 | } |
| 7533 | function->ReplaceCode(function->shared()->code()); |
| 7534 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7535 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7536 | } |
| 7537 | |
| 7538 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7539 | static MaybeObject* Runtime_NotifyOSR(RUNTIME_CALLING_CONVENTION) { |
| 7540 | RUNTIME_GET_ISOLATE; |
| 7541 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7542 | delete deoptimizer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7543 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7544 | } |
| 7545 | |
| 7546 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7547 | static MaybeObject* Runtime_DeoptimizeFunction(RUNTIME_CALLING_CONVENTION) { |
| 7548 | RUNTIME_GET_ISOLATE; |
| 7549 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7550 | ASSERT(args.length() == 1); |
| 7551 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7552 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7553 | |
| 7554 | Deoptimizer::DeoptimizeFunction(*function); |
| 7555 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7556 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7557 | } |
| 7558 | |
| 7559 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7560 | static MaybeObject* Runtime_CompileForOnStackReplacement( |
| 7561 | RUNTIME_CALLING_CONVENTION) { |
| 7562 | RUNTIME_GET_ISOLATE; |
| 7563 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7564 | ASSERT(args.length() == 1); |
| 7565 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7566 | |
| 7567 | // We're not prepared to handle a function with arguments object. |
| 7568 | ASSERT(!function->shared()->scope_info()->HasArgumentsShadow()); |
| 7569 | |
| 7570 | // We have hit a back edge in an unoptimized frame for a function that was |
| 7571 | // selected for on-stack replacement. Find the unoptimized code object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7572 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7573 | // Keep track of whether we've succeeded in optimizing. |
| 7574 | bool succeeded = unoptimized->optimizable(); |
| 7575 | if (succeeded) { |
| 7576 | // If we are trying to do OSR when there are already optimized |
| 7577 | // activations of the function, it means (a) the function is directly or |
| 7578 | // indirectly recursive and (b) an optimized invocation has been |
| 7579 | // deoptimized so that we are currently in an unoptimized activation. |
| 7580 | // Check for optimized activations of this function. |
| 7581 | JavaScriptFrameIterator it; |
| 7582 | while (succeeded && !it.done()) { |
| 7583 | JavaScriptFrame* frame = it.frame(); |
| 7584 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 7585 | it.Advance(); |
| 7586 | } |
| 7587 | } |
| 7588 | |
| 7589 | int ast_id = AstNode::kNoNumber; |
| 7590 | if (succeeded) { |
| 7591 | // The top JS function is this one, the PC is somewhere in the |
| 7592 | // unoptimized code. |
| 7593 | JavaScriptFrameIterator it; |
| 7594 | JavaScriptFrame* frame = it.frame(); |
| 7595 | ASSERT(frame->function() == *function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7596 | ASSERT(frame->LookupCode(isolate) == *unoptimized); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7597 | ASSERT(unoptimized->contains(frame->pc())); |
| 7598 | |
| 7599 | // Use linear search of the unoptimized code's stack check table to find |
| 7600 | // the AST id matching the PC. |
| 7601 | Address start = unoptimized->instruction_start(); |
| 7602 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 7603 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7604 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 7605 | table_cursor += kIntSize; |
| 7606 | for (unsigned i = 0; i < table_length; ++i) { |
| 7607 | // Table entries are (AST id, pc offset) pairs. |
| 7608 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 7609 | if (pc_offset == target_pc_offset) { |
| 7610 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 7611 | break; |
| 7612 | } |
| 7613 | table_cursor += 2 * kIntSize; |
| 7614 | } |
| 7615 | ASSERT(ast_id != AstNode::kNoNumber); |
| 7616 | if (FLAG_trace_osr) { |
| 7617 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 7618 | function->PrintName(); |
| 7619 | PrintF("]\n"); |
| 7620 | } |
| 7621 | |
| 7622 | // Try to compile the optimized code. A true return value from |
| 7623 | // CompileOptimized means that compilation succeeded, not necessarily |
| 7624 | // that optimization succeeded. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7625 | if (CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
| 7626 | function->IsOptimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7627 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 7628 | function->code()->deoptimization_data()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7629 | if (data->OsrPcOffset()->value() >= 0) { |
| 7630 | if (FLAG_trace_osr) { |
| 7631 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7632 | data->OsrPcOffset()->value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7633 | } |
| 7634 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 7635 | } else { |
| 7636 | // We may never generate the desired OSR entry if we emit an |
| 7637 | // early deoptimize. |
| 7638 | succeeded = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7639 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7640 | } else { |
| 7641 | succeeded = false; |
| 7642 | } |
| 7643 | } |
| 7644 | |
| 7645 | // Revert to the original stack checks in the original unoptimized code. |
| 7646 | if (FLAG_trace_osr) { |
| 7647 | PrintF("[restoring original stack checks in "); |
| 7648 | function->PrintName(); |
| 7649 | PrintF("]\n"); |
| 7650 | } |
| 7651 | StackCheckStub check_stub; |
| 7652 | Handle<Code> check_code = check_stub.GetCode(); |
| 7653 | Handle<Code> replacement_code( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7654 | isolate->builtins()->builtin(Builtins::OnStackReplacement)); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7655 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 7656 | *check_code, |
| 7657 | *replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7658 | |
| 7659 | // Allow OSR only at nesting level zero again. |
| 7660 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 7661 | |
| 7662 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 7663 | // smi. This tells the builtin that we need to translate the unoptimized |
| 7664 | // frame to an optimized one. |
| 7665 | if (succeeded) { |
| 7666 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 7667 | return Smi::FromInt(ast_id); |
| 7668 | } else { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7669 | if (function->IsMarkedForLazyRecompilation()) { |
| 7670 | function->ReplaceCode(function->shared()->code()); |
| 7671 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7672 | return Smi::FromInt(-1); |
| 7673 | } |
| 7674 | } |
| 7675 | |
| 7676 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7677 | static MaybeObject* Runtime_GetFunctionDelegate(RUNTIME_CALLING_CONVENTION) { |
| 7678 | RUNTIME_GET_ISOLATE; |
| 7679 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7680 | ASSERT(args.length() == 1); |
| 7681 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 7682 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 7683 | } |
| 7684 | |
| 7685 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7686 | static MaybeObject* Runtime_GetConstructorDelegate(RUNTIME_CALLING_CONVENTION) { |
| 7687 | RUNTIME_GET_ISOLATE; |
| 7688 | HandleScope scope(isolate); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 7689 | ASSERT(args.length() == 1); |
| 7690 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 7691 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 7692 | } |
| 7693 | |
| 7694 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7695 | static MaybeObject* Runtime_NewContext(RUNTIME_CALLING_CONVENTION) { |
| 7696 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7697 | NoHandleAllocation ha; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7698 | ASSERT(args.length() == 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7699 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7700 | CONVERT_CHECKED(JSFunction, function, args[0]); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7701 | int length = function->shared()->scope_info()->NumberOfContextSlots(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7702 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7703 | { MaybeObject* maybe_result = |
| 7704 | isolate->heap()->AllocateFunctionContext(length, function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7705 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7706 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7707 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7708 | isolate->set_context(Context::cast(result)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7709 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7710 | return result; // non-failure |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7713 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7714 | MUST_USE_RESULT static MaybeObject* PushContextHelper(Isolate* isolate, |
| 7715 | Object* object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7716 | bool is_catch_context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7717 | // Convert the object to a proper JavaScript object. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7718 | Object* js_object = object; |
| 7719 | if (!js_object->IsJSObject()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7720 | MaybeObject* maybe_js_object = js_object->ToObject(); |
| 7721 | if (!maybe_js_object->ToObject(&js_object)) { |
| 7722 | if (!Failure::cast(maybe_js_object)->IsInternalError()) { |
| 7723 | return maybe_js_object; |
| 7724 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7725 | HandleScope scope(isolate); |
| 7726 | Handle<Object> handle(object, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7727 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7728 | isolate->factory()->NewTypeError("with_expression", |
| 7729 | HandleVector(&handle, 1)); |
| 7730 | return isolate->Throw(*result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7731 | } |
| 7732 | } |
| 7733 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7734 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7735 | { MaybeObject* maybe_result = isolate->heap()->AllocateWithContext( |
| 7736 | isolate->context(), JSObject::cast(js_object), is_catch_context); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7737 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7738 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7739 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7740 | Context* context = Context::cast(result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7741 | isolate->set_context(context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7742 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7743 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7744 | } |
| 7745 | |
| 7746 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7747 | static MaybeObject* Runtime_PushContext(RUNTIME_CALLING_CONVENTION) { |
| 7748 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7749 | NoHandleAllocation ha; |
| 7750 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7751 | return PushContextHelper(isolate, args[0], false); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7755 | static MaybeObject* Runtime_PushCatchContext(RUNTIME_CALLING_CONVENTION) { |
| 7756 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7757 | NoHandleAllocation ha; |
| 7758 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7759 | return PushContextHelper(isolate, args[0], true); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7760 | } |
| 7761 | |
| 7762 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7763 | static MaybeObject* Runtime_DeleteContextSlot(RUNTIME_CALLING_CONVENTION) { |
| 7764 | RUNTIME_GET_ISOLATE; |
| 7765 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7766 | ASSERT(args.length() == 2); |
| 7767 | |
| 7768 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 7769 | CONVERT_ARG_CHECKED(String, name, 1); |
| 7770 | |
| 7771 | int index; |
| 7772 | PropertyAttributes attributes; |
| 7773 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7774 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7775 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7776 | // If the slot was not found the result is true. |
| 7777 | if (holder.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7778 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7779 | } |
| 7780 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7781 | // If the slot was found in a context, it should be DONT_DELETE. |
| 7782 | if (holder->IsContext()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7783 | return isolate->heap()->false_value(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7784 | } |
| 7785 | |
| 7786 | // The slot was found in a JSObject, either a context extension object, |
| 7787 | // the global object, or an arguments object. Try to delete it |
| 7788 | // (respecting DONT_DELETE). For consistency with V8's usual behavior, |
| 7789 | // which allows deleting all parameters in functions that mention |
| 7790 | // 'arguments', we do this even for the case of slots found on an |
| 7791 | // arguments object. The slot was found on an arguments object if the |
| 7792 | // index is non-negative. |
| 7793 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 7794 | if (index >= 0) { |
| 7795 | return object->DeleteElement(index, JSObject::NORMAL_DELETION); |
| 7796 | } else { |
| 7797 | return object->DeleteProperty(*name, JSObject::NORMAL_DELETION); |
| 7798 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7799 | } |
| 7800 | |
| 7801 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7802 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 7803 | // How this is achieved is calling convention-dependent. |
| 7804 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 7805 | // variants where a 64-bit value is returned in two 32-bit registers |
| 7806 | // (edx:eax on ia32, r1:r0 on ARM). |
| 7807 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 7808 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 7809 | // allocated by the caller, and passed as a pointer in a hidden first parameter. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7810 | #ifdef V8_HOST_ARCH_64_BIT |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7811 | struct ObjectPair { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7812 | MaybeObject* x; |
| 7813 | MaybeObject* y; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7814 | }; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7815 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7816 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7817 | ObjectPair result = {x, y}; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7818 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 7819 | // In Win64 they are assigned to a hidden first argument. |
| 7820 | return result; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7821 | } |
| 7822 | #else |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7823 | typedef uint64_t ObjectPair; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7824 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7825 | return reinterpret_cast<uint32_t>(x) | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7826 | (reinterpret_cast<ObjectPair>(y) << 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7827 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7828 | #endif |
| 7829 | |
| 7830 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7831 | static inline MaybeObject* Unhole(Heap* heap, |
| 7832 | MaybeObject* x, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7833 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7834 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 7835 | USE(attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7836 | return x->IsTheHole() ? heap->undefined_value() : x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7837 | } |
| 7838 | |
| 7839 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7840 | static JSObject* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 7841 | JSObject* holder) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7842 | ASSERT(!holder->IsGlobalObject()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7843 | Context* top = isolate->context(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7844 | // Get the context extension function. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7845 | JSFunction* context_extension_function = |
| 7846 | top->global_context()->context_extension_function(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7847 | // If the holder isn't a context extension object, we just return it |
| 7848 | // as the receiver. This allows arguments objects to be used as |
| 7849 | // receivers, but only if they are put in the context scope chain |
| 7850 | // explicitly via a with-statement. |
| 7851 | Object* constructor = holder->map()->constructor(); |
| 7852 | if (constructor != context_extension_function) return holder; |
| 7853 | // Fall back to using the global object as the receiver if the |
| 7854 | // property turns out to be a local variable allocated in a context |
| 7855 | // extension object - introduced via eval. |
| 7856 | return top->global()->global_receiver(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7857 | } |
| 7858 | |
| 7859 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7860 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 7861 | Isolate* isolate, |
| 7862 | bool throw_error) { |
| 7863 | HandleScope scope(isolate); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7864 | ASSERT_EQ(2, args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7865 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7866 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7867 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7868 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7869 | Handle<Context> context = args.at<Context>(0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7870 | Handle<String> name = args.at<String>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7871 | |
| 7872 | int index; |
| 7873 | PropertyAttributes attributes; |
| 7874 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7875 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7876 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7877 | // If the index is non-negative, the slot has been found in a local |
| 7878 | // variable or a parameter. Read it from the context object or the |
| 7879 | // arguments object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7880 | if (index >= 0) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7881 | // If the "property" we were looking for is a local variable or an |
| 7882 | // argument in a context, the receiver is the global object; see |
| 7883 | // ECMA-262, 3rd., 10.1.6 and 10.2.3. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7884 | JSObject* receiver = |
| 7885 | isolate->context()->global()->global_receiver(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7886 | MaybeObject* value = (holder->IsContext()) |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7887 | ? Context::cast(*holder)->get(index) |
| 7888 | : JSObject::cast(*holder)->GetElement(index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7889 | return MakePair(Unhole(isolate->heap(), value, attributes), receiver); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7890 | } |
| 7891 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7892 | // If the holder is found, we read the property from it. |
| 7893 | if (!holder.is_null() && holder->IsJSObject()) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7894 | ASSERT(Handle<JSObject>::cast(holder)->HasProperty(*name)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7895 | JSObject* object = JSObject::cast(*holder); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7896 | JSObject* receiver; |
| 7897 | if (object->IsGlobalObject()) { |
| 7898 | receiver = GlobalObject::cast(object)->global_receiver(); |
| 7899 | } else if (context->is_exception_holder(*holder)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7900 | receiver = isolate->context()->global()->global_receiver(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7901 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7902 | receiver = ComputeReceiverForNonGlobal(isolate, object); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7903 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7904 | // No need to unhole the value here. This is taken care of by the |
| 7905 | // GetProperty function. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7906 | MaybeObject* value = object->GetProperty(*name); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7907 | return MakePair(value, receiver); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7908 | } |
| 7909 | |
| 7910 | if (throw_error) { |
| 7911 | // The property doesn't exist - throw exception. |
| 7912 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7913 | isolate->factory()->NewReferenceError("not_defined", |
| 7914 | HandleVector(&name, 1)); |
| 7915 | return MakePair(isolate->Throw(*reference_error), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7916 | } else { |
| 7917 | // The property doesn't exist - return undefined |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7918 | return MakePair(isolate->heap()->undefined_value(), |
| 7919 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7920 | } |
| 7921 | } |
| 7922 | |
| 7923 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7924 | static ObjectPair Runtime_LoadContextSlot(RUNTIME_CALLING_CONVENTION) { |
| 7925 | RUNTIME_GET_ISOLATE; |
| 7926 | return LoadContextSlotHelper(args, isolate, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7930 | static ObjectPair Runtime_LoadContextSlotNoReferenceError( |
| 7931 | RUNTIME_CALLING_CONVENTION) { |
| 7932 | RUNTIME_GET_ISOLATE; |
| 7933 | return LoadContextSlotHelper(args, isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7934 | } |
| 7935 | |
| 7936 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7937 | static MaybeObject* Runtime_StoreContextSlot(RUNTIME_CALLING_CONVENTION) { |
| 7938 | RUNTIME_GET_ISOLATE; |
| 7939 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 7940 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7941 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7942 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7943 | CONVERT_ARG_CHECKED(Context, context, 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7944 | CONVERT_ARG_CHECKED(String, name, 2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 7945 | CONVERT_SMI_CHECKED(strict_unchecked, args[3]); |
| 7946 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 7947 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7948 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7949 | |
| 7950 | int index; |
| 7951 | PropertyAttributes attributes; |
| 7952 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7953 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7954 | |
| 7955 | if (index >= 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7956 | if (holder->IsContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7957 | // Ignore if read_only variable. |
| 7958 | if ((attributes & READ_ONLY) == 0) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7959 | // Context is a fixed array and set cannot fail. |
| 7960 | Context::cast(*holder)->set(index, *value); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7961 | } else if (strict_mode == kStrictMode) { |
| 7962 | // Setting read only property in strict mode. |
| 7963 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7964 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 7965 | HandleVector(&name, 1)); |
| 7966 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7967 | } |
| 7968 | } else { |
| 7969 | ASSERT((attributes & READ_ONLY) == 0); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7970 | Handle<Object> result = |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7971 | SetElement(Handle<JSObject>::cast(holder), index, value, strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7972 | if (result.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7973 | ASSERT(isolate->has_pending_exception()); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7974 | return Failure::Exception(); |
| 7975 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7976 | } |
| 7977 | return *value; |
| 7978 | } |
| 7979 | |
| 7980 | // Slow case: The property is not in a FixedArray context. |
| 7981 | // It is either in an JSObject extension context or it was not found. |
| 7982 | Handle<JSObject> context_ext; |
| 7983 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7984 | if (!holder.is_null()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7985 | // The property exists in the extension context. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7986 | context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7987 | } else { |
| 7988 | // The property was not found. It needs to be stored in the global context. |
| 7989 | ASSERT(attributes == ABSENT); |
| 7990 | attributes = NONE; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7991 | context_ext = Handle<JSObject>(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7992 | } |
| 7993 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7994 | // Set the property, but ignore if read_only variable on the context |
| 7995 | // extension object itself. |
| 7996 | if ((attributes & READ_ONLY) == 0 || |
| 7997 | (context_ext->GetLocalPropertyAttribute(*name) == ABSENT)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7998 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 7999 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8000 | SetProperty(context_ext, name, value, NONE, strict_mode)); |
| 8001 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8002 | // Setting read only property in strict mode. |
| 8003 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8004 | isolate->factory()->NewTypeError( |
| 8005 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 8006 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8007 | } |
| 8008 | return *value; |
| 8009 | } |
| 8010 | |
| 8011 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8012 | static MaybeObject* Runtime_Throw(RUNTIME_CALLING_CONVENTION) { |
| 8013 | RUNTIME_GET_ISOLATE; |
| 8014 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8015 | ASSERT(args.length() == 1); |
| 8016 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8017 | return isolate->Throw(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
| 8020 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8021 | static MaybeObject* Runtime_ReThrow(RUNTIME_CALLING_CONVENTION) { |
| 8022 | RUNTIME_GET_ISOLATE; |
| 8023 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8024 | ASSERT(args.length() == 1); |
| 8025 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8026 | return isolate->ReThrow(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8027 | } |
| 8028 | |
| 8029 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8030 | static MaybeObject* Runtime_PromoteScheduledException( |
| 8031 | RUNTIME_CALLING_CONVENTION) { |
| 8032 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8033 | ASSERT_EQ(0, args.length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8034 | return isolate->PromoteScheduledException(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8035 | } |
| 8036 | |
| 8037 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8038 | static MaybeObject* Runtime_ThrowReferenceError(RUNTIME_CALLING_CONVENTION) { |
| 8039 | RUNTIME_GET_ISOLATE; |
| 8040 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8041 | ASSERT(args.length() == 1); |
| 8042 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8043 | Handle<Object> name(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8044 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8045 | isolate->factory()->NewReferenceError("not_defined", |
| 8046 | HandleVector(&name, 1)); |
| 8047 | return isolate->Throw(*reference_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
| 8050 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8051 | static MaybeObject* Runtime_StackGuard(RUNTIME_CALLING_CONVENTION) { |
| 8052 | RUNTIME_GET_ISOLATE; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 8053 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8054 | |
| 8055 | // First check if this is a real stack overflow. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8056 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 8057 | NoHandleAllocation na; |
| 8058 | return isolate->StackOverflow(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 8059 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8060 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8061 | return Execution::HandleStackGuardInterrupt(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8062 | } |
| 8063 | |
| 8064 | |
| 8065 | // NOTE: These PrintXXX functions are defined for all builds (not just |
| 8066 | // DEBUG builds) because we may want to be able to trace function |
| 8067 | // calls in all modes. |
| 8068 | static void PrintString(String* str) { |
| 8069 | // not uncommon to have empty strings |
| 8070 | if (str->length() > 0) { |
| 8071 | SmartPointer<char> s = |
| 8072 | str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 8073 | PrintF("%s", *s); |
| 8074 | } |
| 8075 | } |
| 8076 | |
| 8077 | |
| 8078 | static void PrintObject(Object* obj) { |
| 8079 | if (obj->IsSmi()) { |
| 8080 | PrintF("%d", Smi::cast(obj)->value()); |
| 8081 | } else if (obj->IsString() || obj->IsSymbol()) { |
| 8082 | PrintString(String::cast(obj)); |
| 8083 | } else if (obj->IsNumber()) { |
| 8084 | PrintF("%g", obj->Number()); |
| 8085 | } else if (obj->IsFailure()) { |
| 8086 | PrintF("<failure>"); |
| 8087 | } else if (obj->IsUndefined()) { |
| 8088 | PrintF("<undefined>"); |
| 8089 | } else if (obj->IsNull()) { |
| 8090 | PrintF("<null>"); |
| 8091 | } else if (obj->IsTrue()) { |
| 8092 | PrintF("<true>"); |
| 8093 | } else if (obj->IsFalse()) { |
| 8094 | PrintF("<false>"); |
| 8095 | } else { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 8096 | PrintF("%p", reinterpret_cast<void*>(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8097 | } |
| 8098 | } |
| 8099 | |
| 8100 | |
| 8101 | static int StackSize() { |
| 8102 | int n = 0; |
| 8103 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 8104 | return n; |
| 8105 | } |
| 8106 | |
| 8107 | |
| 8108 | static void PrintTransition(Object* result) { |
| 8109 | // indentation |
| 8110 | { const int nmax = 80; |
| 8111 | int n = StackSize(); |
| 8112 | if (n <= nmax) |
| 8113 | PrintF("%4d:%*s", n, n, ""); |
| 8114 | else |
| 8115 | PrintF("%4d:%*s", n, nmax, "..."); |
| 8116 | } |
| 8117 | |
| 8118 | if (result == NULL) { |
| 8119 | // constructor calls |
| 8120 | JavaScriptFrameIterator it; |
| 8121 | JavaScriptFrame* frame = it.frame(); |
| 8122 | if (frame->IsConstructor()) PrintF("new "); |
| 8123 | // function name |
| 8124 | Object* fun = frame->function(); |
| 8125 | if (fun->IsJSFunction()) { |
| 8126 | PrintObject(JSFunction::cast(fun)->shared()->name()); |
| 8127 | } else { |
| 8128 | PrintObject(fun); |
| 8129 | } |
| 8130 | // function arguments |
| 8131 | // (we are intentionally only printing the actually |
| 8132 | // supplied parameters, not all parameters required) |
| 8133 | PrintF("(this="); |
| 8134 | PrintObject(frame->receiver()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8135 | const int length = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8136 | for (int i = 0; i < length; i++) { |
| 8137 | PrintF(", "); |
| 8138 | PrintObject(frame->GetParameter(i)); |
| 8139 | } |
| 8140 | PrintF(") {\n"); |
| 8141 | |
| 8142 | } else { |
| 8143 | // function result |
| 8144 | PrintF("} -> "); |
| 8145 | PrintObject(result); |
| 8146 | PrintF("\n"); |
| 8147 | } |
| 8148 | } |
| 8149 | |
| 8150 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8151 | static MaybeObject* Runtime_TraceEnter(RUNTIME_CALLING_CONVENTION) { |
| 8152 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8153 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8154 | NoHandleAllocation ha; |
| 8155 | PrintTransition(NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8156 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8157 | } |
| 8158 | |
| 8159 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8160 | static MaybeObject* Runtime_TraceExit(RUNTIME_CALLING_CONVENTION) { |
| 8161 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8162 | NoHandleAllocation ha; |
| 8163 | PrintTransition(args[0]); |
| 8164 | return args[0]; // return TOS |
| 8165 | } |
| 8166 | |
| 8167 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8168 | static MaybeObject* Runtime_DebugPrint(RUNTIME_CALLING_CONVENTION) { |
| 8169 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8170 | NoHandleAllocation ha; |
| 8171 | ASSERT(args.length() == 1); |
| 8172 | |
| 8173 | #ifdef DEBUG |
| 8174 | if (args[0]->IsString()) { |
| 8175 | // If we have a string, assume it's a code "marker" |
| 8176 | // and print some interesting cpu debugging info. |
| 8177 | JavaScriptFrameIterator it; |
| 8178 | JavaScriptFrame* frame = it.frame(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8179 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 8180 | frame->fp(), frame->sp(), frame->caller_sp()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8181 | } else { |
| 8182 | PrintF("DebugPrint: "); |
| 8183 | } |
| 8184 | args[0]->Print(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8185 | if (args[0]->IsHeapObject()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8186 | PrintF("\n"); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8187 | HeapObject::cast(args[0])->map()->Print(); |
| 8188 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8189 | #else |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8190 | // ShortPrint is available in release mode. Print is not. |
| 8191 | args[0]->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8192 | #endif |
| 8193 | PrintF("\n"); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8194 | Flush(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8195 | |
| 8196 | return args[0]; // return TOS |
| 8197 | } |
| 8198 | |
| 8199 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8200 | static MaybeObject* Runtime_DebugTrace(RUNTIME_CALLING_CONVENTION) { |
| 8201 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8202 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8203 | NoHandleAllocation ha; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8204 | isolate->PrintStack(); |
| 8205 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
| 8208 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8209 | static MaybeObject* Runtime_DateCurrentTime(RUNTIME_CALLING_CONVENTION) { |
| 8210 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8211 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 8212 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8213 | |
| 8214 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 8215 | // the number in a Date object representing a particular instant in |
| 8216 | // time is milliseconds. Therefore, we floor the result of getting |
| 8217 | // the OS time. |
| 8218 | double millis = floor(OS::TimeCurrentMillis()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8219 | return isolate->heap()->NumberFromDouble(millis); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8220 | } |
| 8221 | |
| 8222 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8223 | static MaybeObject* Runtime_DateParseString(RUNTIME_CALLING_CONVENTION) { |
| 8224 | RUNTIME_GET_ISOLATE; |
| 8225 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8226 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8227 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8228 | CONVERT_ARG_CHECKED(String, str, 0); |
| 8229 | FlattenString(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8230 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8231 | CONVERT_ARG_CHECKED(JSArray, output, 1); |
| 8232 | RUNTIME_ASSERT(output->HasFastElements()); |
| 8233 | |
| 8234 | AssertNoAllocation no_allocation; |
| 8235 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8236 | FixedArray* output_array = FixedArray::cast(output->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8237 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 8238 | bool result; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 8239 | if (str->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8240 | result = DateParser::Parse(str->ToAsciiVector(), output_array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8241 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 8242 | ASSERT(str->IsTwoByteRepresentation()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8243 | result = DateParser::Parse(str->ToUC16Vector(), output_array); |
| 8244 | } |
| 8245 | |
| 8246 | if (result) { |
| 8247 | return *output; |
| 8248 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8249 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8250 | } |
| 8251 | } |
| 8252 | |
| 8253 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8254 | static MaybeObject* Runtime_DateLocalTimezone(RUNTIME_CALLING_CONVENTION) { |
| 8255 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8256 | NoHandleAllocation ha; |
| 8257 | ASSERT(args.length() == 1); |
| 8258 | |
| 8259 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 8260 | const char* zone = OS::LocalTimezone(x); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8261 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8262 | } |
| 8263 | |
| 8264 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8265 | static MaybeObject* Runtime_DateLocalTimeOffset(RUNTIME_CALLING_CONVENTION) { |
| 8266 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8267 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 8268 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8269 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8270 | return isolate->heap()->NumberFromDouble(OS::LocalTimeOffset()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
| 8273 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8274 | static MaybeObject* Runtime_DateDaylightSavingsOffset( |
| 8275 | RUNTIME_CALLING_CONVENTION) { |
| 8276 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8277 | NoHandleAllocation ha; |
| 8278 | ASSERT(args.length() == 1); |
| 8279 | |
| 8280 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8281 | return isolate->heap()->NumberFromDouble(OS::DaylightSavingsOffset(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8282 | } |
| 8283 | |
| 8284 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8285 | static MaybeObject* Runtime_GlobalReceiver(RUNTIME_CALLING_CONVENTION) { |
| 8286 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8287 | ASSERT(args.length() == 1); |
| 8288 | Object* global = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8289 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8290 | return JSGlobalObject::cast(global)->global_receiver(); |
| 8291 | } |
| 8292 | |
| 8293 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8294 | static MaybeObject* Runtime_ParseJson(RUNTIME_CALLING_CONVENTION) { |
| 8295 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8296 | ASSERT_EQ(1, args.length()); |
| 8297 | CONVERT_ARG_CHECKED(String, source, 0); |
| 8298 | |
| 8299 | Handle<Object> result = JsonParser::Parse(source); |
| 8300 | if (result.is_null()) { |
| 8301 | // Syntax error or stack overflow in scanner. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8302 | ASSERT(isolate->has_pending_exception()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8303 | return Failure::Exception(); |
| 8304 | } |
| 8305 | return *result; |
| 8306 | } |
| 8307 | |
| 8308 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8309 | static MaybeObject* Runtime_CompileString(RUNTIME_CALLING_CONVENTION) { |
| 8310 | RUNTIME_GET_ISOLATE; |
| 8311 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8312 | ASSERT_EQ(1, args.length()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 8313 | CONVERT_ARG_CHECKED(String, source, 0); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8314 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 8315 | // Compile source string in the global context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8316 | Handle<Context> context(isolate->context()->global_context()); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8317 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval(source, |
| 8318 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8319 | true, |
| 8320 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8321 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8322 | Handle<JSFunction> fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8323 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 8324 | context, |
| 8325 | NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8326 | return *fun; |
| 8327 | } |
| 8328 | |
| 8329 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8330 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 8331 | Handle<String> source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8332 | Handle<Object> receiver, |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 8333 | StrictModeFlag strict_mode) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8334 | // Deal with a normal eval call with a string argument. Compile it |
| 8335 | // and return the compiled function bound in the local context. |
| 8336 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 8337 | source, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8338 | Handle<Context>(isolate->context()), |
| 8339 | isolate->context()->IsGlobalContext(), |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 8340 | strict_mode); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8341 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8342 | Handle<JSFunction> compiled = |
| 8343 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 8344 | shared, Handle<Context>(isolate->context()), NOT_TENURED); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8345 | return MakePair(*compiled, *receiver); |
| 8346 | } |
| 8347 | |
| 8348 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8349 | static ObjectPair Runtime_ResolvePossiblyDirectEval( |
| 8350 | RUNTIME_CALLING_CONVENTION) { |
| 8351 | RUNTIME_GET_ISOLATE; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8352 | ASSERT(args.length() == 4); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8353 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8354 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8355 | Handle<Object> callee = args.at<Object>(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8356 | Handle<Object> receiver; // Will be overwritten. |
| 8357 | |
| 8358 | // Compute the calling context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8359 | Handle<Context> context = Handle<Context>(isolate->context(), isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8360 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8361 | // Make sure Isolate::context() agrees with the old code that traversed |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8362 | // the stack frames to compute the context. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8363 | StackFrameLocator locator; |
| 8364 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8365 | ASSERT(Context::cast(frame->context()) == *context); |
| 8366 | #endif |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8367 | |
| 8368 | // Find where the 'eval' symbol is bound. It is unaliased only if |
| 8369 | // it is bound in the global context. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8370 | int index = -1; |
| 8371 | PropertyAttributes attributes = ABSENT; |
| 8372 | while (true) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8373 | receiver = context->Lookup(isolate->factory()->eval_symbol(), |
| 8374 | FOLLOW_PROTOTYPE_CHAIN, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8375 | &index, &attributes); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8376 | // Stop search when eval is found or when the global context is |
| 8377 | // reached. |
| 8378 | if (attributes != ABSENT || context->IsGlobalContext()) break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8379 | if (context->is_function_context()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8380 | context = Handle<Context>(Context::cast(context->closure()->context()), |
| 8381 | isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8382 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8383 | context = Handle<Context>(context->previous(), isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8384 | } |
| 8385 | } |
| 8386 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8387 | // If eval could not be resolved, it has been deleted and we need to |
| 8388 | // throw a reference error. |
| 8389 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8390 | Handle<Object> name = isolate->factory()->eval_symbol(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8391 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8392 | isolate->factory()->NewReferenceError("not_defined", |
| 8393 | HandleVector(&name, 1)); |
| 8394 | return MakePair(isolate->Throw(*reference_error), NULL); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8395 | } |
| 8396 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8397 | if (!context->IsGlobalContext()) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8398 | // 'eval' is not bound in the global context. Just call the function |
| 8399 | // with the given arguments. This is not necessarily the global eval. |
| 8400 | if (receiver->IsContext()) { |
| 8401 | context = Handle<Context>::cast(receiver); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8402 | receiver = Handle<Object>(context->get(index), isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8403 | } else if (receiver->IsJSContextExtensionObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8404 | receiver = Handle<JSObject>( |
| 8405 | isolate->context()->global()->global_receiver(), isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8406 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8407 | return MakePair(*callee, *receiver); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8408 | } |
| 8409 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8410 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 8411 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8412 | if (*callee != isolate->global_context()->global_eval_fun() || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8413 | !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8414 | return MakePair(*callee, |
| 8415 | isolate->context()->global()->global_receiver()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8416 | } |
| 8417 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8418 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8419 | return CompileGlobalEval(isolate, |
| 8420 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8421 | args.at<Object>(2), |
| 8422 | static_cast<StrictModeFlag>( |
| 8423 | Smi::cast(args[3])->value())); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8424 | } |
| 8425 | |
| 8426 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8427 | static ObjectPair Runtime_ResolvePossiblyDirectEvalNoLookup( |
| 8428 | RUNTIME_CALLING_CONVENTION) { |
| 8429 | RUNTIME_GET_ISOLATE; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8430 | ASSERT(args.length() == 4); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8431 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8432 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8433 | Handle<Object> callee = args.at<Object>(0); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8434 | |
| 8435 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 8436 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8437 | if (*callee != isolate->global_context()->global_eval_fun() || |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8438 | !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8439 | return MakePair(*callee, |
| 8440 | isolate->context()->global()->global_receiver()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8441 | } |
| 8442 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8443 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8444 | return CompileGlobalEval(isolate, |
| 8445 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8446 | args.at<Object>(2), |
| 8447 | static_cast<StrictModeFlag>( |
| 8448 | Smi::cast(args[3])->value())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8449 | } |
| 8450 | |
| 8451 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8452 | static MaybeObject* Runtime_SetNewFunctionAttributes( |
| 8453 | RUNTIME_CALLING_CONVENTION) { |
| 8454 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8455 | // This utility adjusts the property attributes for newly created Function |
| 8456 | // object ("new Function(...)") by changing the map. |
| 8457 | // All it does is changing the prototype property to enumerable |
| 8458 | // as specified in ECMA262, 15.3.5.2. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8459 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8460 | ASSERT(args.length() == 1); |
| 8461 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8462 | |
| 8463 | Handle<Map> map = func->shared()->strict_mode() |
| 8464 | ? isolate->strict_mode_function_instance_map() |
| 8465 | : isolate->function_instance_map(); |
| 8466 | |
| 8467 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 8468 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 8469 | func->set_map(*map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8470 | return *func; |
| 8471 | } |
| 8472 | |
| 8473 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8474 | static MaybeObject* Runtime_AllocateInNewSpace(RUNTIME_CALLING_CONVENTION) { |
| 8475 | RUNTIME_GET_ISOLATE; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8476 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 8477 | // Use as fallback for allocation in generated code when NewSpace |
| 8478 | // is full. |
| 8479 | ASSERT(args.length() == 1); |
| 8480 | CONVERT_ARG_CHECKED(Smi, size_smi, 0); |
| 8481 | int size = size_smi->value(); |
| 8482 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 8483 | RUNTIME_ASSERT(size > 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8484 | Heap* heap = isolate->heap(); |
| 8485 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8486 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8487 | Object* allocation; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8488 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8489 | if (maybe_allocation->ToObject(&allocation)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8490 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8491 | } |
| 8492 | return maybe_allocation; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8493 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
| 8496 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8497 | // Push an object unto an array of objects if it is not already in the |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8498 | // array. Returns true if the element was pushed on the stack and |
| 8499 | // false otherwise. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8500 | static MaybeObject* Runtime_PushIfAbsent(RUNTIME_CALLING_CONVENTION) { |
| 8501 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8502 | ASSERT(args.length() == 2); |
| 8503 | CONVERT_CHECKED(JSArray, array, args[0]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8504 | CONVERT_CHECKED(JSObject, element, args[1]); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8505 | RUNTIME_ASSERT(array->HasFastElements()); |
| 8506 | int length = Smi::cast(array->length())->value(); |
| 8507 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 8508 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8509 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8510 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8511 | Object* obj; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8512 | // Strict not needed. Used for cycle detection in Array join implementation. |
| 8513 | { MaybeObject* maybe_obj = array->SetFastElement(length, element, |
| 8514 | kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8515 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8516 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8517 | return isolate->heap()->true_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8518 | } |
| 8519 | |
| 8520 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8521 | /** |
| 8522 | * A simple visitor visits every element of Array's. |
| 8523 | * The backend storage can be a fixed array for fast elements case, |
| 8524 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 8525 | * of FixedArray, the class can be used by both fast and slow cases. |
| 8526 | * The second parameter of the constructor, fast_elements, specifies |
| 8527 | * whether the storage is a FixedArray or Dictionary. |
| 8528 | * |
| 8529 | * An index limit is used to deal with the situation that a result array |
| 8530 | * length overflows 32-bit non-negative integer. |
| 8531 | */ |
| 8532 | class ArrayConcatVisitor { |
| 8533 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8534 | ArrayConcatVisitor(Isolate* isolate, |
| 8535 | Handle<FixedArray> storage, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8536 | bool fast_elements) : |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8537 | isolate_(isolate), |
| 8538 | storage_(Handle<FixedArray>::cast( |
| 8539 | isolate->global_handles()->Create(*storage))), |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8540 | index_offset_(0u), |
| 8541 | fast_elements_(fast_elements) { } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8542 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8543 | ~ArrayConcatVisitor() { |
| 8544 | clear_storage(); |
| 8545 | } |
| 8546 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8547 | void visit(uint32_t i, Handle<Object> elm) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8548 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8549 | uint32_t index = index_offset_ + i; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8550 | |
| 8551 | if (fast_elements_) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8552 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 8553 | storage_->set(index, *elm); |
| 8554 | return; |
| 8555 | } |
| 8556 | // Our initial estimate of length was foiled, possibly by |
| 8557 | // getters on the arrays increasing the length of later arrays |
| 8558 | // during iteration. |
| 8559 | // This shouldn't happen in anything but pathological cases. |
| 8560 | SetDictionaryMode(index); |
| 8561 | // Fall-through to dictionary mode. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8562 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8563 | ASSERT(!fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8564 | Handle<NumberDictionary> dict(NumberDictionary::cast(*storage_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8565 | Handle<NumberDictionary> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8566 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8567 | if (!result.is_identical_to(dict)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8568 | // Dictionary needed to grow. |
| 8569 | clear_storage(); |
| 8570 | set_storage(*result); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8571 | } |
| 8572 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8573 | |
| 8574 | void increase_index_offset(uint32_t delta) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8575 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 8576 | index_offset_ = JSObject::kMaxElementCount; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8577 | } else { |
| 8578 | index_offset_ += delta; |
| 8579 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8580 | } |
| 8581 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8582 | Handle<JSArray> ToArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8583 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8584 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8585 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8586 | Handle<Map> map; |
| 8587 | if (fast_elements_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8588 | map = isolate_->factory()->GetFastElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8589 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8590 | map = isolate_->factory()->GetSlowElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8591 | } |
| 8592 | array->set_map(*map); |
| 8593 | array->set_length(*length); |
| 8594 | array->set_elements(*storage_); |
| 8595 | return array; |
| 8596 | } |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 8597 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8598 | private: |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8599 | // Convert storage to dictionary mode. |
| 8600 | void SetDictionaryMode(uint32_t index) { |
| 8601 | ASSERT(fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8602 | Handle<FixedArray> current_storage(*storage_); |
| 8603 | Handle<NumberDictionary> slow_storage( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8604 | isolate_->factory()->NewNumberDictionary(current_storage->length())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8605 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 8606 | for (uint32_t i = 0; i < current_length; i++) { |
| 8607 | HandleScope loop_scope; |
| 8608 | Handle<Object> element(current_storage->get(i)); |
| 8609 | if (!element->IsTheHole()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8610 | Handle<NumberDictionary> new_storage = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8611 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8612 | if (!new_storage.is_identical_to(slow_storage)) { |
| 8613 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 8614 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8615 | } |
| 8616 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8617 | clear_storage(); |
| 8618 | set_storage(*slow_storage); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8619 | fast_elements_ = false; |
| 8620 | } |
| 8621 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8622 | inline void clear_storage() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8623 | isolate_->global_handles()->Destroy( |
| 8624 | Handle<Object>::cast(storage_).location()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8625 | } |
| 8626 | |
| 8627 | inline void set_storage(FixedArray* storage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8628 | storage_ = Handle<FixedArray>::cast( |
| 8629 | isolate_->global_handles()->Create(storage)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8630 | } |
| 8631 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8632 | Isolate* isolate_; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8633 | Handle<FixedArray> storage_; // Always a global handle. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8634 | // Index after last seen index. Always less than or equal to |
| 8635 | // JSObject::kMaxElementCount. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8636 | uint32_t index_offset_; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8637 | bool fast_elements_; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8638 | }; |
| 8639 | |
| 8640 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8641 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 8642 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 8643 | int element_count = 0; |
| 8644 | switch (array->GetElementsKind()) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8645 | case JSObject::FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8646 | // Fast elements can't have lengths that are not representable by |
| 8647 | // a 32-bit signed integer. |
| 8648 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 8649 | int fast_length = static_cast<int>(length); |
| 8650 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 8651 | for (int i = 0; i < fast_length; i++) { |
| 8652 | if (!elements->get(i)->IsTheHole()) element_count++; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8653 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 8654 | break; |
| 8655 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8656 | case JSObject::DICTIONARY_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8657 | Handle<NumberDictionary> dictionary( |
| 8658 | NumberDictionary::cast(array->elements())); |
| 8659 | int capacity = dictionary->Capacity(); |
| 8660 | for (int i = 0; i < capacity; i++) { |
| 8661 | Handle<Object> key(dictionary->KeyAt(i)); |
| 8662 | if (dictionary->IsKey(*key)) { |
| 8663 | element_count++; |
| 8664 | } |
| 8665 | } |
| 8666 | break; |
| 8667 | } |
| 8668 | default: |
| 8669 | // External arrays are always dense. |
| 8670 | return length; |
| 8671 | } |
| 8672 | // As an estimate, we assume that the prototype doesn't contain any |
| 8673 | // inherited elements. |
| 8674 | return element_count; |
| 8675 | } |
| 8676 | |
| 8677 | |
| 8678 | |
| 8679 | template<class ExternalArrayClass, class ElementType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8680 | static void IterateExternalArrayElements(Isolate* isolate, |
| 8681 | Handle<JSObject> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8682 | bool elements_are_ints, |
| 8683 | bool elements_are_guaranteed_smis, |
| 8684 | ArrayConcatVisitor* visitor) { |
| 8685 | Handle<ExternalArrayClass> array( |
| 8686 | ExternalArrayClass::cast(receiver->elements())); |
| 8687 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 8688 | |
| 8689 | ASSERT(visitor != NULL); |
| 8690 | if (elements_are_ints) { |
| 8691 | if (elements_are_guaranteed_smis) { |
| 8692 | for (uint32_t j = 0; j < len; j++) { |
| 8693 | HandleScope loop_scope; |
| 8694 | Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get(j)))); |
| 8695 | visitor->visit(j, e); |
| 8696 | } |
| 8697 | } else { |
| 8698 | for (uint32_t j = 0; j < len; j++) { |
| 8699 | HandleScope loop_scope; |
| 8700 | int64_t val = static_cast<int64_t>(array->get(j)); |
| 8701 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 8702 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 8703 | visitor->visit(j, e); |
| 8704 | } else { |
| 8705 | Handle<Object> e = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8706 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8707 | visitor->visit(j, e); |
| 8708 | } |
| 8709 | } |
| 8710 | } |
| 8711 | } else { |
| 8712 | for (uint32_t j = 0; j < len; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8713 | HandleScope loop_scope(isolate); |
| 8714 | Handle<Object> e = isolate->factory()->NewNumber(array->get(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8715 | visitor->visit(j, e); |
| 8716 | } |
| 8717 | } |
| 8718 | } |
| 8719 | |
| 8720 | |
| 8721 | // Used for sorting indices in a List<uint32_t>. |
| 8722 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 8723 | uint32_t a = *ap; |
| 8724 | uint32_t b = *bp; |
| 8725 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 8726 | } |
| 8727 | |
| 8728 | |
| 8729 | static void CollectElementIndices(Handle<JSObject> object, |
| 8730 | uint32_t range, |
| 8731 | List<uint32_t>* indices) { |
| 8732 | JSObject::ElementsKind kind = object->GetElementsKind(); |
| 8733 | switch (kind) { |
| 8734 | case JSObject::FAST_ELEMENTS: { |
| 8735 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 8736 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 8737 | if (range < length) length = range; |
| 8738 | for (uint32_t i = 0; i < length; i++) { |
| 8739 | if (!elements->get(i)->IsTheHole()) { |
| 8740 | indices->Add(i); |
| 8741 | } |
| 8742 | } |
| 8743 | break; |
| 8744 | } |
| 8745 | case JSObject::DICTIONARY_ELEMENTS: { |
| 8746 | Handle<NumberDictionary> dict(NumberDictionary::cast(object->elements())); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8747 | uint32_t capacity = dict->Capacity(); |
| 8748 | for (uint32_t j = 0; j < capacity; j++) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8749 | HandleScope loop_scope; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8750 | Handle<Object> k(dict->KeyAt(j)); |
| 8751 | if (dict->IsKey(*k)) { |
| 8752 | ASSERT(k->IsNumber()); |
| 8753 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 8754 | if (index < range) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8755 | indices->Add(index); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8756 | } |
| 8757 | } |
| 8758 | } |
| 8759 | break; |
| 8760 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8761 | default: { |
| 8762 | int dense_elements_length; |
| 8763 | switch (kind) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8764 | case JSObject::EXTERNAL_PIXEL_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8765 | dense_elements_length = |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8766 | ExternalPixelArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8767 | break; |
| 8768 | } |
| 8769 | case JSObject::EXTERNAL_BYTE_ELEMENTS: { |
| 8770 | dense_elements_length = |
| 8771 | ExternalByteArray::cast(object->elements())->length(); |
| 8772 | break; |
| 8773 | } |
| 8774 | case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 8775 | dense_elements_length = |
| 8776 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
| 8777 | break; |
| 8778 | } |
| 8779 | case JSObject::EXTERNAL_SHORT_ELEMENTS: { |
| 8780 | dense_elements_length = |
| 8781 | ExternalShortArray::cast(object->elements())->length(); |
| 8782 | break; |
| 8783 | } |
| 8784 | case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 8785 | dense_elements_length = |
| 8786 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
| 8787 | break; |
| 8788 | } |
| 8789 | case JSObject::EXTERNAL_INT_ELEMENTS: { |
| 8790 | dense_elements_length = |
| 8791 | ExternalIntArray::cast(object->elements())->length(); |
| 8792 | break; |
| 8793 | } |
| 8794 | case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 8795 | dense_elements_length = |
| 8796 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
| 8797 | break; |
| 8798 | } |
| 8799 | case JSObject::EXTERNAL_FLOAT_ELEMENTS: { |
| 8800 | dense_elements_length = |
| 8801 | ExternalFloatArray::cast(object->elements())->length(); |
| 8802 | break; |
| 8803 | } |
| 8804 | default: |
| 8805 | UNREACHABLE(); |
| 8806 | dense_elements_length = 0; |
| 8807 | break; |
| 8808 | } |
| 8809 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 8810 | if (range <= length) { |
| 8811 | length = range; |
| 8812 | // We will add all indices, so we might as well clear it first |
| 8813 | // and avoid duplicates. |
| 8814 | indices->Clear(); |
| 8815 | } |
| 8816 | for (uint32_t i = 0; i < length; i++) { |
| 8817 | indices->Add(i); |
| 8818 | } |
| 8819 | if (length == range) return; // All indices accounted for already. |
| 8820 | break; |
| 8821 | } |
| 8822 | } |
| 8823 | |
| 8824 | Handle<Object> prototype(object->GetPrototype()); |
| 8825 | if (prototype->IsJSObject()) { |
| 8826 | // The prototype will usually have no inherited element indices, |
| 8827 | // but we have to check. |
| 8828 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 8829 | } |
| 8830 | } |
| 8831 | |
| 8832 | |
| 8833 | /** |
| 8834 | * A helper function that visits elements of a JSArray in numerical |
| 8835 | * order. |
| 8836 | * |
| 8837 | * The visitor argument called for each existing element in the array |
| 8838 | * with the element index and the element's value. |
| 8839 | * Afterwards it increments the base-index of the visitor by the array |
| 8840 | * length. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8841 | * Returns false if any access threw an exception, otherwise true. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8842 | */ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8843 | static bool IterateElements(Isolate* isolate, |
| 8844 | Handle<JSArray> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8845 | ArrayConcatVisitor* visitor) { |
| 8846 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 8847 | switch (receiver->GetElementsKind()) { |
| 8848 | case JSObject::FAST_ELEMENTS: { |
| 8849 | // Run through the elements FixedArray and use HasElement and GetElement |
| 8850 | // to check the prototype for missing elements. |
| 8851 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 8852 | int fast_length = static_cast<int>(length); |
| 8853 | ASSERT(fast_length <= elements->length()); |
| 8854 | for (int j = 0; j < fast_length; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8855 | HandleScope loop_scope(isolate); |
| 8856 | Handle<Object> element_value(elements->get(j), isolate); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8857 | if (!element_value->IsTheHole()) { |
| 8858 | visitor->visit(j, element_value); |
| 8859 | } else if (receiver->HasElement(j)) { |
| 8860 | // Call GetElement on receiver, not its prototype, or getters won't |
| 8861 | // have the correct receiver. |
| 8862 | element_value = GetElement(receiver, j); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8863 | if (element_value.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8864 | visitor->visit(j, element_value); |
| 8865 | } |
| 8866 | } |
| 8867 | break; |
| 8868 | } |
| 8869 | case JSObject::DICTIONARY_ELEMENTS: { |
| 8870 | Handle<NumberDictionary> dict(receiver->element_dictionary()); |
| 8871 | List<uint32_t> indices(dict->Capacity() / 2); |
| 8872 | // Collect all indices in the object and the prototypes less |
| 8873 | // than length. This might introduce duplicates in the indices list. |
| 8874 | CollectElementIndices(receiver, length, &indices); |
| 8875 | indices.Sort(&compareUInt32); |
| 8876 | int j = 0; |
| 8877 | int n = indices.length(); |
| 8878 | while (j < n) { |
| 8879 | HandleScope loop_scope; |
| 8880 | uint32_t index = indices[j]; |
| 8881 | Handle<Object> element = GetElement(receiver, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8882 | if (element.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8883 | visitor->visit(index, element); |
| 8884 | // Skip to next different index (i.e., omit duplicates). |
| 8885 | do { |
| 8886 | j++; |
| 8887 | } while (j < n && indices[j] == index); |
| 8888 | } |
| 8889 | break; |
| 8890 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8891 | case JSObject::EXTERNAL_PIXEL_ELEMENTS: { |
| 8892 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 8893 | receiver->elements())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8894 | for (uint32_t j = 0; j < length; j++) { |
| 8895 | Handle<Smi> e(Smi::FromInt(pixels->get(j))); |
| 8896 | visitor->visit(j, e); |
| 8897 | } |
| 8898 | break; |
| 8899 | } |
| 8900 | case JSObject::EXTERNAL_BYTE_ELEMENTS: { |
| 8901 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8902 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8903 | break; |
| 8904 | } |
| 8905 | case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 8906 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8907 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8908 | break; |
| 8909 | } |
| 8910 | case JSObject::EXTERNAL_SHORT_ELEMENTS: { |
| 8911 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8912 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8913 | break; |
| 8914 | } |
| 8915 | case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 8916 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8917 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8918 | break; |
| 8919 | } |
| 8920 | case JSObject::EXTERNAL_INT_ELEMENTS: { |
| 8921 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8922 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8923 | break; |
| 8924 | } |
| 8925 | case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 8926 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8927 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8928 | break; |
| 8929 | } |
| 8930 | case JSObject::EXTERNAL_FLOAT_ELEMENTS: { |
| 8931 | IterateExternalArrayElements<ExternalFloatArray, float>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8932 | isolate, receiver, false, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8933 | break; |
| 8934 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8935 | default: |
| 8936 | UNREACHABLE(); |
| 8937 | break; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8938 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8939 | visitor->increase_index_offset(length); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8940 | return true; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8941 | } |
| 8942 | |
| 8943 | |
| 8944 | /** |
| 8945 | * Array::concat implementation. |
| 8946 | * See ECMAScript 262, 15.4.4.4. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8947 | * TODO(581): Fix non-compliance for very large concatenations and update to |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8948 | * following the ECMAScript 5 specification. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8949 | */ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8950 | static MaybeObject* Runtime_ArrayConcat(RUNTIME_CALLING_CONVENTION) { |
| 8951 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8952 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 8953 | HandleScope handle_scope(isolate); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8954 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8955 | CONVERT_ARG_CHECKED(JSArray, arguments, 0); |
| 8956 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 8957 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 8958 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8959 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8960 | // Pass 1: estimate the length and number of elements of the result. |
| 8961 | // The actual length can be larger if any of the arguments have getters |
| 8962 | // that mutate other arguments (but will otherwise be precise). |
| 8963 | // The number of elements is precise if there are no inherited elements. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8964 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8965 | uint32_t estimate_result_length = 0; |
| 8966 | uint32_t estimate_nof_elements = 0; |
| 8967 | { |
| 8968 | for (int i = 0; i < argument_count; i++) { |
| 8969 | HandleScope loop_scope; |
| 8970 | Handle<Object> obj(elements->get(i)); |
| 8971 | uint32_t length_estimate; |
| 8972 | uint32_t element_estimate; |
| 8973 | if (obj->IsJSArray()) { |
| 8974 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
| 8975 | length_estimate = |
| 8976 | static_cast<uint32_t>(array->length()->Number()); |
| 8977 | element_estimate = |
| 8978 | EstimateElementCount(array); |
| 8979 | } else { |
| 8980 | length_estimate = 1; |
| 8981 | element_estimate = 1; |
| 8982 | } |
| 8983 | // Avoid overflows by capping at kMaxElementCount. |
| 8984 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 8985 | length_estimate) { |
| 8986 | estimate_result_length = JSObject::kMaxElementCount; |
| 8987 | } else { |
| 8988 | estimate_result_length += length_estimate; |
| 8989 | } |
| 8990 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 8991 | element_estimate) { |
| 8992 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 8993 | } else { |
| 8994 | estimate_nof_elements += element_estimate; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8995 | } |
| 8996 | } |
| 8997 | } |
| 8998 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8999 | // If estimated number of elements is more than half of length, a |
| 9000 | // fixed array (fast case) is more time and space-efficient than a |
| 9001 | // dictionary. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9002 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9003 | |
| 9004 | Handle<FixedArray> storage; |
| 9005 | if (fast_case) { |
| 9006 | // The backing storage array must have non-existing elements to |
| 9007 | // preserve holes across concat operations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9008 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 9009 | estimate_result_length); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9010 | } else { |
| 9011 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 9012 | uint32_t at_least_space_for = estimate_nof_elements + |
| 9013 | (estimate_nof_elements >> 2); |
| 9014 | storage = Handle<FixedArray>::cast( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9015 | isolate->factory()->NewNumberDictionary(at_least_space_for)); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9016 | } |
| 9017 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9018 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9019 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9020 | for (int i = 0; i < argument_count; i++) { |
| 9021 | Handle<Object> obj(elements->get(i)); |
| 9022 | if (obj->IsJSArray()) { |
| 9023 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9024 | if (!IterateElements(isolate, array, &visitor)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9025 | return Failure::Exception(); |
| 9026 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9027 | } else { |
| 9028 | visitor.visit(0, obj); |
| 9029 | visitor.increase_index_offset(1); |
| 9030 | } |
| 9031 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9032 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9033 | return *visitor.ToArray(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9034 | } |
| 9035 | |
| 9036 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9037 | // This will not allocate (flatten the string), but it may run |
| 9038 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9039 | static MaybeObject* Runtime_GlobalPrint(RUNTIME_CALLING_CONVENTION) { |
| 9040 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9041 | NoHandleAllocation ha; |
| 9042 | ASSERT(args.length() == 1); |
| 9043 | |
| 9044 | CONVERT_CHECKED(String, string, args[0]); |
| 9045 | StringInputBuffer buffer(string); |
| 9046 | while (buffer.has_more()) { |
| 9047 | uint16_t character = buffer.GetNext(); |
| 9048 | PrintF("%c", character); |
| 9049 | } |
| 9050 | return string; |
| 9051 | } |
| 9052 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9053 | // Moves all own elements of an object, that are below a limit, to positions |
| 9054 | // starting at zero. All undefined values are placed after non-undefined values, |
| 9055 | // and are followed by non-existing element. Does not change the length |
| 9056 | // property. |
| 9057 | // Returns the number of non-undefined elements collected. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9058 | static MaybeObject* Runtime_RemoveArrayHoles(RUNTIME_CALLING_CONVENTION) { |
| 9059 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9060 | ASSERT(args.length() == 2); |
| 9061 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9062 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 9063 | return object->PrepareElementsForSort(limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9064 | } |
| 9065 | |
| 9066 | |
| 9067 | // Move contents of argument 0 (an array) to argument 1 (an array) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9068 | static MaybeObject* Runtime_MoveArrayContents(RUNTIME_CALLING_CONVENTION) { |
| 9069 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9070 | ASSERT(args.length() == 2); |
| 9071 | CONVERT_CHECKED(JSArray, from, args[0]); |
| 9072 | CONVERT_CHECKED(JSArray, to, args[1]); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9073 | HeapObject* new_elements = from->elements(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9074 | MaybeObject* maybe_new_map; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9075 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 9076 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9077 | maybe_new_map = to->map()->GetFastElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9078 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9079 | maybe_new_map = to->map()->GetSlowElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9080 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9081 | Object* new_map; |
| 9082 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9083 | to->set_map(Map::cast(new_map)); |
| 9084 | to->set_elements(new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9085 | to->set_length(from->length()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9086 | Object* obj; |
| 9087 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 9088 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9089 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9090 | from->set_length(Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9091 | return to; |
| 9092 | } |
| 9093 | |
| 9094 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9095 | // How many elements does this object/array have? |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9096 | static MaybeObject* Runtime_EstimateNumberOfElements( |
| 9097 | RUNTIME_CALLING_CONVENTION) { |
| 9098 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9099 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9100 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9101 | HeapObject* elements = object->elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9102 | if (elements->IsDictionary()) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9103 | return Smi::FromInt(NumberDictionary::cast(elements)->NumberOfElements()); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9104 | } else if (object->IsJSArray()) { |
| 9105 | return JSArray::cast(object)->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9106 | } else { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9107 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9108 | } |
| 9109 | } |
| 9110 | |
| 9111 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9112 | static MaybeObject* Runtime_SwapElements(RUNTIME_CALLING_CONVENTION) { |
| 9113 | RUNTIME_GET_ISOLATE; |
| 9114 | HandleScope handle_scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9115 | |
| 9116 | ASSERT_EQ(3, args.length()); |
| 9117 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9118 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9119 | Handle<Object> key1 = args.at<Object>(1); |
| 9120 | Handle<Object> key2 = args.at<Object>(2); |
| 9121 | |
| 9122 | uint32_t index1, index2; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9123 | if (!key1->ToArrayIndex(&index1) |
| 9124 | || !key2->ToArrayIndex(&index2)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9125 | return isolate->ThrowIllegalOperation(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9126 | } |
| 9127 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9128 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
| 9129 | Handle<Object> tmp1 = GetElement(jsobject, index1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9130 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9131 | Handle<Object> tmp2 = GetElement(jsobject, index2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9132 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9133 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9134 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 9135 | SetElement(jsobject, index1, tmp2, kStrictMode)); |
| 9136 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 9137 | SetElement(jsobject, index2, tmp1, kStrictMode)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9138 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9139 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9140 | } |
| 9141 | |
| 9142 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9143 | // Returns an array that tells you where in the [0, length) interval an array |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9144 | // might have elements. Can either return keys (positive integers) or |
| 9145 | // intervals (pair of a negative integer (-start-1) followed by a |
| 9146 | // positive (length)) or undefined values. |
| 9147 | // Intervals can span over some keys that are not in the object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9148 | static MaybeObject* Runtime_GetArrayKeys(RUNTIME_CALLING_CONVENTION) { |
| 9149 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9150 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9151 | HandleScope scope(isolate); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9152 | CONVERT_ARG_CHECKED(JSObject, array, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9153 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9154 | if (array->elements()->IsDictionary()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9155 | // Create an array and get all the keys into it, then remove all the |
| 9156 | // keys that are not integers in the range 0 to length-1. |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9157 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(array, INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9158 | int keys_length = keys->length(); |
| 9159 | for (int i = 0; i < keys_length; i++) { |
| 9160 | Object* key = keys->get(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9161 | uint32_t index = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9162 | if (!key->ToArrayIndex(&index) || index >= length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9163 | // Zap invalid keys. |
| 9164 | keys->set_undefined(i); |
| 9165 | } |
| 9166 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9167 | return *isolate->factory()->NewJSArrayWithElements(keys); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9168 | } else { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9169 | ASSERT(array->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9170 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9171 | // -1 means start of array. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9172 | single_interval->set(0, Smi::FromInt(-1)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9173 | uint32_t actual_length = |
| 9174 | static_cast<uint32_t>(FixedArray::cast(array->elements())->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9175 | uint32_t min_length = actual_length < length ? actual_length : length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9176 | Handle<Object> length_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9177 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9178 | single_interval->set(1, *length_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9179 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9180 | } |
| 9181 | } |
| 9182 | |
| 9183 | |
| 9184 | // DefineAccessor takes an optional final argument which is the |
| 9185 | // property attributes (eg, DONT_ENUM, DONT_DELETE). IMPORTANT: due |
| 9186 | // to the way accessors are implemented, it is set for both the getter |
| 9187 | // and setter on the first call to DefineAccessor and ignored on |
| 9188 | // subsequent calls. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9189 | static MaybeObject* Runtime_DefineAccessor(RUNTIME_CALLING_CONVENTION) { |
| 9190 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9191 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
| 9192 | // Compute attributes. |
| 9193 | PropertyAttributes attributes = NONE; |
| 9194 | if (args.length() == 5) { |
| 9195 | CONVERT_CHECKED(Smi, attrs, args[4]); |
| 9196 | int value = attrs->value(); |
| 9197 | // Only attribute bits should be set. |
| 9198 | ASSERT((value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 9199 | attributes = static_cast<PropertyAttributes>(value); |
| 9200 | } |
| 9201 | |
| 9202 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 9203 | CONVERT_CHECKED(String, name, args[1]); |
| 9204 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 9205 | CONVERT_CHECKED(JSFunction, fun, args[3]); |
| 9206 | return obj->DefineAccessor(name, flag->value() == 0, fun, attributes); |
| 9207 | } |
| 9208 | |
| 9209 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9210 | static MaybeObject* Runtime_LookupAccessor(RUNTIME_CALLING_CONVENTION) { |
| 9211 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9212 | ASSERT(args.length() == 3); |
| 9213 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 9214 | CONVERT_CHECKED(String, name, args[1]); |
| 9215 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 9216 | return obj->LookupAccessor(name, flag->value() == 0); |
| 9217 | } |
| 9218 | |
| 9219 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9220 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9221 | static MaybeObject* Runtime_DebugBreak(RUNTIME_CALLING_CONVENTION) { |
| 9222 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9223 | ASSERT(args.length() == 0); |
| 9224 | return Execution::DebugBreakHelper(); |
| 9225 | } |
| 9226 | |
| 9227 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9228 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 9229 | static Smi* WrapFrameId(StackFrame::Id id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 9230 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9231 | return Smi::FromInt(id >> 2); |
| 9232 | } |
| 9233 | |
| 9234 | |
| 9235 | static StackFrame::Id UnwrapFrameId(Smi* wrapped) { |
| 9236 | return static_cast<StackFrame::Id>(wrapped->value() << 2); |
| 9237 | } |
| 9238 | |
| 9239 | |
| 9240 | // Adds a JavaScript function as a debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9241 | // args[0]: debug event listener function to set or null or undefined for |
| 9242 | // clearing the event listener function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9243 | // args[1]: object supplied during callback |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9244 | static MaybeObject* Runtime_SetDebugEventListener(RUNTIME_CALLING_CONVENTION) { |
| 9245 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9246 | ASSERT(args.length() == 2); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9247 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 9248 | args[0]->IsUndefined() || |
| 9249 | args[0]->IsNull()); |
| 9250 | Handle<Object> callback = args.at<Object>(0); |
| 9251 | Handle<Object> data = args.at<Object>(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9252 | isolate->debugger()->SetEventListener(callback, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9253 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9254 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9255 | } |
| 9256 | |
| 9257 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9258 | static MaybeObject* Runtime_Break(RUNTIME_CALLING_CONVENTION) { |
| 9259 | RUNTIME_GET_ISOLATE; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9260 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9261 | isolate->stack_guard()->DebugBreak(); |
| 9262 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9263 | } |
| 9264 | |
| 9265 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9266 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 9267 | Object* receiver, |
| 9268 | String* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9269 | LookupResult* result, |
| 9270 | bool* caught_exception) { |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9271 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9272 | switch (result->type()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9273 | case NORMAL: |
| 9274 | value = result->holder()->GetNormalizedProperty(result); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9275 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9276 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9277 | } |
| 9278 | return value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9279 | case FIELD: |
| 9280 | value = |
| 9281 | JSObject::cast( |
| 9282 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 9283 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9284 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9285 | } |
| 9286 | return value; |
| 9287 | case CONSTANT_FUNCTION: |
| 9288 | return result->GetConstantFunction(); |
| 9289 | case CALLBACKS: { |
| 9290 | Object* structure = result->GetCallbackObject(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 9291 | if (structure->IsProxy() || structure->IsAccessorInfo()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9292 | MaybeObject* maybe_value = receiver->GetPropertyWithCallback( |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9293 | receiver, structure, name, result->holder()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9294 | if (!maybe_value->ToObject(&value)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9295 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9296 | ASSERT(maybe_value->IsException()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9297 | maybe_value = heap->isolate()->pending_exception(); |
| 9298 | heap->isolate()->clear_pending_exception(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9299 | if (caught_exception != NULL) { |
| 9300 | *caught_exception = true; |
| 9301 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9302 | return maybe_value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9303 | } |
| 9304 | return value; |
| 9305 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9306 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9307 | } |
| 9308 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9309 | case INTERCEPTOR: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9310 | case MAP_TRANSITION: |
| 9311 | case CONSTANT_TRANSITION: |
| 9312 | case NULL_DESCRIPTOR: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9313 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9314 | default: |
| 9315 | UNREACHABLE(); |
| 9316 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9317 | UNREACHABLE(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9318 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9319 | } |
| 9320 | |
| 9321 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 9322 | // Get debugger related details for an object property. |
| 9323 | // args[0]: object holding property |
| 9324 | // args[1]: name of the property |
| 9325 | // |
| 9326 | // The array returned contains the following information: |
| 9327 | // 0: Property value |
| 9328 | // 1: Property details |
| 9329 | // 2: Property value is exception |
| 9330 | // 3: Getter function if defined |
| 9331 | // 4: Setter function if defined |
| 9332 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 9333 | // defined through __defineGetter__ and/or __defineSetter__. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9334 | static MaybeObject* Runtime_DebugGetPropertyDetails( |
| 9335 | RUNTIME_CALLING_CONVENTION) { |
| 9336 | RUNTIME_GET_ISOLATE; |
| 9337 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9338 | |
| 9339 | ASSERT(args.length() == 2); |
| 9340 | |
| 9341 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9342 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9343 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9344 | // Make sure to set the current context to the context before the debugger was |
| 9345 | // entered (if the debugger is entered). The reason for switching context here |
| 9346 | // is that for some property lookups (accessors and interceptors) callbacks |
| 9347 | // into the embedding application can occour, and the embedding application |
| 9348 | // could have the assumption that its own global context is the current |
| 9349 | // context and not some internal debugger context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9350 | SaveContext save(isolate); |
| 9351 | if (isolate->debug()->InDebugger()) { |
| 9352 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9353 | } |
| 9354 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9355 | // Skip the global proxy as it has no properties and always delegates to the |
| 9356 | // real global object. |
| 9357 | if (obj->IsJSGlobalProxy()) { |
| 9358 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 9359 | } |
| 9360 | |
| 9361 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9362 | // Check if the name is trivially convertible to an index and get the element |
| 9363 | // if so. |
| 9364 | uint32_t index; |
| 9365 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9366 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9367 | Object* element_or_char; |
| 9368 | { MaybeObject* maybe_element_or_char = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9369 | Runtime::GetElementOrCharAt(isolate, obj, index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9370 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 9371 | return maybe_element_or_char; |
| 9372 | } |
| 9373 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9374 | details->set(0, element_or_char); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9375 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9376 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9377 | } |
| 9378 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9379 | // Find the number of objects making up this. |
| 9380 | int length = LocalPrototypeChainLength(*obj); |
| 9381 | |
| 9382 | // Try local lookup on each of the objects. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9383 | Handle<JSObject> jsproto = obj; |
| 9384 | for (int i = 0; i < length; i++) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9385 | LookupResult result; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9386 | jsproto->LocalLookup(*name, &result); |
| 9387 | if (result.IsProperty()) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9388 | // LookupResult is not GC safe as it holds raw object pointers. |
| 9389 | // GC can happen later in this code so put the required fields into |
| 9390 | // local variables using handles when required for later use. |
| 9391 | PropertyType result_type = result.type(); |
| 9392 | Handle<Object> result_callback_obj; |
| 9393 | if (result_type == CALLBACKS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9394 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 9395 | isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9396 | } |
| 9397 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 9398 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 9399 | // to be copied to handles before this. |
| 9400 | bool caught_exception = false; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9401 | Object* raw_value; |
| 9402 | { MaybeObject* maybe_raw_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9403 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 9404 | &result, &caught_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9405 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 9406 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9407 | Handle<Object> value(raw_value, isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9408 | |
| 9409 | // If the callback object is a fixed array then it contains JavaScript |
| 9410 | // getter and/or setter. |
| 9411 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
| 9412 | result_callback_obj->IsFixedArray(); |
| 9413 | Handle<FixedArray> details = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9414 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9415 | details->set(0, *value); |
| 9416 | details->set(1, property_details); |
| 9417 | if (hasJavaScriptAccessors) { |
| 9418 | details->set(2, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9419 | caught_exception ? isolate->heap()->true_value() |
| 9420 | : isolate->heap()->false_value()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9421 | details->set(3, FixedArray::cast(*result_callback_obj)->get(0)); |
| 9422 | details->set(4, FixedArray::cast(*result_callback_obj)->get(1)); |
| 9423 | } |
| 9424 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9425 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9426 | } |
| 9427 | if (i < length - 1) { |
| 9428 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 9429 | } |
| 9430 | } |
| 9431 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9432 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9433 | } |
| 9434 | |
| 9435 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9436 | static MaybeObject* Runtime_DebugGetProperty(RUNTIME_CALLING_CONVENTION) { |
| 9437 | RUNTIME_GET_ISOLATE; |
| 9438 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9439 | |
| 9440 | ASSERT(args.length() == 2); |
| 9441 | |
| 9442 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9443 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9444 | |
| 9445 | LookupResult result; |
| 9446 | obj->Lookup(*name, &result); |
| 9447 | if (result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9448 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9449 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9450 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9451 | } |
| 9452 | |
| 9453 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9454 | // Return the property type calculated from the property details. |
| 9455 | // args[0]: smi with property details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9456 | static MaybeObject* Runtime_DebugPropertyTypeFromDetails( |
| 9457 | RUNTIME_CALLING_CONVENTION) { |
| 9458 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9459 | ASSERT(args.length() == 1); |
| 9460 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9461 | PropertyType type = PropertyDetails(details).type(); |
| 9462 | return Smi::FromInt(static_cast<int>(type)); |
| 9463 | } |
| 9464 | |
| 9465 | |
| 9466 | // Return the property attribute calculated from the property details. |
| 9467 | // args[0]: smi with property details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9468 | static MaybeObject* Runtime_DebugPropertyAttributesFromDetails( |
| 9469 | RUNTIME_CALLING_CONVENTION) { |
| 9470 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9471 | ASSERT(args.length() == 1); |
| 9472 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9473 | PropertyAttributes attributes = PropertyDetails(details).attributes(); |
| 9474 | return Smi::FromInt(static_cast<int>(attributes)); |
| 9475 | } |
| 9476 | |
| 9477 | |
| 9478 | // Return the property insertion index calculated from the property details. |
| 9479 | // args[0]: smi with property details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9480 | static MaybeObject* Runtime_DebugPropertyIndexFromDetails( |
| 9481 | RUNTIME_CALLING_CONVENTION) { |
| 9482 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9483 | ASSERT(args.length() == 1); |
| 9484 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9485 | int index = PropertyDetails(details).index(); |
| 9486 | return Smi::FromInt(index); |
| 9487 | } |
| 9488 | |
| 9489 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9490 | // Return property value from named interceptor. |
| 9491 | // args[0]: object |
| 9492 | // args[1]: property name |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9493 | static MaybeObject* Runtime_DebugNamedInterceptorPropertyValue( |
| 9494 | RUNTIME_CALLING_CONVENTION) { |
| 9495 | RUNTIME_GET_ISOLATE; |
| 9496 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9497 | ASSERT(args.length() == 2); |
| 9498 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9499 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
| 9500 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9501 | |
| 9502 | PropertyAttributes attributes; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9503 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9504 | } |
| 9505 | |
| 9506 | |
| 9507 | // Return element value from indexed interceptor. |
| 9508 | // args[0]: object |
| 9509 | // args[1]: index |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9510 | static MaybeObject* Runtime_DebugIndexedInterceptorElementValue( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9511 | RUNTIME_CALLING_CONVENTION) { |
| 9512 | RUNTIME_GET_ISOLATE; |
| 9513 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9514 | ASSERT(args.length() == 2); |
| 9515 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9516 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 9517 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 9518 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9519 | return obj->GetElementWithInterceptor(*obj, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9523 | static MaybeObject* Runtime_CheckExecutionState(RUNTIME_CALLING_CONVENTION) { |
| 9524 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9525 | ASSERT(args.length() >= 1); |
| 9526 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9527 | // Check that the break id is valid. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9528 | if (isolate->debug()->break_id() == 0 || |
| 9529 | break_id != isolate->debug()->break_id()) { |
| 9530 | return isolate->Throw( |
| 9531 | isolate->heap()->illegal_execution_state_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9532 | } |
| 9533 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9534 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9535 | } |
| 9536 | |
| 9537 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9538 | static MaybeObject* Runtime_GetFrameCount(RUNTIME_CALLING_CONVENTION) { |
| 9539 | RUNTIME_GET_ISOLATE; |
| 9540 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9541 | ASSERT(args.length() == 1); |
| 9542 | |
| 9543 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9544 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9545 | { MaybeObject* maybe_result = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9546 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 9547 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9548 | |
| 9549 | // Count all frames which are relevant to debugging stack trace. |
| 9550 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9551 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9552 | if (id == StackFrame::NO_ID) { |
| 9553 | // If there is no JavaScript stack frame count is 0. |
| 9554 | return Smi::FromInt(0); |
| 9555 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9556 | for (JavaScriptFrameIterator it(id); !it.done(); it.Advance()) n++; |
| 9557 | return Smi::FromInt(n); |
| 9558 | } |
| 9559 | |
| 9560 | |
| 9561 | static const int kFrameDetailsFrameIdIndex = 0; |
| 9562 | static const int kFrameDetailsReceiverIndex = 1; |
| 9563 | static const int kFrameDetailsFunctionIndex = 2; |
| 9564 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 9565 | static const int kFrameDetailsLocalCountIndex = 4; |
| 9566 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 9567 | static const int kFrameDetailsConstructCallIndex = 6; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9568 | static const int kFrameDetailsAtReturnIndex = 7; |
| 9569 | static const int kFrameDetailsDebuggerFrameIndex = 8; |
| 9570 | static const int kFrameDetailsFirstDynamicIndex = 9; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9571 | |
| 9572 | // Return an array with frame details |
| 9573 | // args[0]: number: break id |
| 9574 | // args[1]: number: frame index |
| 9575 | // |
| 9576 | // The array returned contains the following information: |
| 9577 | // 0: Frame id |
| 9578 | // 1: Receiver |
| 9579 | // 2: Function |
| 9580 | // 3: Argument count |
| 9581 | // 4: Local count |
| 9582 | // 5: Source position |
| 9583 | // 6: Constructor call |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9584 | // 7: Is at return |
| 9585 | // 8: Debugger frame |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9586 | // Arguments name, value |
| 9587 | // Locals name, value |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9588 | // Return value if any |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9589 | static MaybeObject* Runtime_GetFrameDetails(RUNTIME_CALLING_CONVENTION) { |
| 9590 | RUNTIME_GET_ISOLATE; |
| 9591 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9592 | ASSERT(args.length() == 2); |
| 9593 | |
| 9594 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9595 | Object* check; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9596 | { MaybeObject* maybe_check = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9597 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 9598 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9599 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9600 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9601 | |
| 9602 | // Find the relevant frame with the requested index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9603 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9604 | if (id == StackFrame::NO_ID) { |
| 9605 | // If there are no JavaScript stack frames return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9606 | return heap->undefined_value(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9607 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9608 | int count = 0; |
| 9609 | JavaScriptFrameIterator it(id); |
| 9610 | for (; !it.done(); it.Advance()) { |
| 9611 | if (count == index) break; |
| 9612 | count++; |
| 9613 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9614 | if (it.done()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9615 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9616 | bool is_optimized_frame = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9617 | it.frame()->LookupCode(isolate)->kind() == Code::OPTIMIZED_FUNCTION; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9618 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9619 | // Traverse the saved contexts chain to find the active context for the |
| 9620 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9621 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9622 | while (save != NULL && !save->below(it.frame())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9623 | save = save->prev(); |
| 9624 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9625 | ASSERT(save != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9626 | |
| 9627 | // Get the frame id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9628 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9629 | |
| 9630 | // Find source position. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9631 | int position = |
| 9632 | it.frame()->LookupCode(isolate)->SourcePosition(it.frame()->pc()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9633 | |
| 9634 | // Check for constructor frame. |
| 9635 | bool constructor = it.frame()->IsConstructor(); |
| 9636 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9637 | // Get scope info and read from it for local variable information. |
| 9638 | Handle<JSFunction> function(JSFunction::cast(it.frame()->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9639 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9640 | ScopeInfo<> info(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9641 | |
| 9642 | // Get the context. |
| 9643 | Handle<Context> context(Context::cast(it.frame()->context())); |
| 9644 | |
| 9645 | // Get the locals names and values into a temporary array. |
| 9646 | // |
| 9647 | // TODO(1240907): Hide compiler-introduced stack variables |
| 9648 | // (e.g. .result)? For users of the debugger, they will probably be |
| 9649 | // confusing. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9650 | Handle<FixedArray> locals = |
| 9651 | isolate->factory()->NewFixedArray(info.NumberOfLocals() * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9652 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9653 | // Fill in the names of the locals. |
| 9654 | for (int i = 0; i < info.NumberOfLocals(); i++) { |
| 9655 | locals->set(i * 2, *info.LocalName(i)); |
| 9656 | } |
| 9657 | |
| 9658 | // Fill in the values of the locals. |
| 9659 | for (int i = 0; i < info.NumberOfLocals(); i++) { |
| 9660 | if (is_optimized_frame) { |
| 9661 | // If we are inspecting an optimized frame use undefined as the |
| 9662 | // value for all locals. |
| 9663 | // |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9664 | // TODO(1140): We should be able to get the correct values |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9665 | // for locals in optimized frames. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9666 | locals->set(i * 2 + 1, isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9667 | } else if (i < info.number_of_stack_slots()) { |
| 9668 | // Get the value from the stack. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9669 | locals->set(i * 2 + 1, it.frame()->GetExpression(i)); |
| 9670 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9671 | // Traverse the context chain to the function context as all local |
| 9672 | // variables stored in the context will be on the function context. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9673 | Handle<String> name = info.LocalName(i); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9674 | while (!context->is_function_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9675 | context = Handle<Context>(context->previous()); |
| 9676 | } |
| 9677 | ASSERT(context->is_function_context()); |
| 9678 | locals->set(i * 2 + 1, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9679 | context->get(scope_info->ContextSlotIndex(*name, NULL))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9680 | } |
| 9681 | } |
| 9682 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9683 | // Check whether this frame is positioned at return. If not top |
| 9684 | // frame or if the frame is optimized it cannot be at a return. |
| 9685 | bool at_return = false; |
| 9686 | if (!is_optimized_frame && index == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9687 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9688 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9689 | |
| 9690 | // If positioned just before return find the value to be returned and add it |
| 9691 | // to the frame information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9692 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9693 | if (at_return) { |
| 9694 | StackFrameIterator it2; |
| 9695 | Address internal_frame_sp = NULL; |
| 9696 | while (!it2.done()) { |
| 9697 | if (it2.frame()->is_internal()) { |
| 9698 | internal_frame_sp = it2.frame()->sp(); |
| 9699 | } else { |
| 9700 | if (it2.frame()->is_java_script()) { |
| 9701 | if (it2.frame()->id() == it.frame()->id()) { |
| 9702 | // The internal frame just before the JavaScript frame contains the |
| 9703 | // value to return on top. A debug break at return will create an |
| 9704 | // internal frame to store the return value (eax/rax/r0) before |
| 9705 | // entering the debug break exit frame. |
| 9706 | if (internal_frame_sp != NULL) { |
| 9707 | return_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9708 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 9709 | isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9710 | break; |
| 9711 | } |
| 9712 | } |
| 9713 | } |
| 9714 | |
| 9715 | // Indicate that the previous frame was not an internal frame. |
| 9716 | internal_frame_sp = NULL; |
| 9717 | } |
| 9718 | it2.Advance(); |
| 9719 | } |
| 9720 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9721 | |
| 9722 | // Now advance to the arguments adapter frame (if any). It contains all |
| 9723 | // the provided parameters whereas the function frame always have the number |
| 9724 | // of arguments matching the functions parameters. The rest of the |
| 9725 | // information (except for what is collected above) is the same. |
| 9726 | it.AdvanceToArgumentsFrame(); |
| 9727 | |
| 9728 | // Find the number of arguments to fill. At least fill the number of |
| 9729 | // parameters for the function and fill more if more parameters are provided. |
| 9730 | int argument_count = info.number_of_parameters(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9731 | if (argument_count < it.frame()->ComputeParametersCount()) { |
| 9732 | argument_count = it.frame()->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9733 | } |
| 9734 | |
| 9735 | // Calculate the size of the result. |
| 9736 | int details_size = kFrameDetailsFirstDynamicIndex + |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9737 | 2 * (argument_count + info.NumberOfLocals()) + |
| 9738 | (at_return ? 1 : 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9739 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9740 | |
| 9741 | // Add the frame id. |
| 9742 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 9743 | |
| 9744 | // Add the function (same as in function frame). |
| 9745 | details->set(kFrameDetailsFunctionIndex, it.frame()->function()); |
| 9746 | |
| 9747 | // Add the arguments count. |
| 9748 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 9749 | |
| 9750 | // Add the locals count |
| 9751 | details->set(kFrameDetailsLocalCountIndex, |
| 9752 | Smi::FromInt(info.NumberOfLocals())); |
| 9753 | |
| 9754 | // Add the source position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9755 | if (position != RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9756 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 9757 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9758 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9759 | } |
| 9760 | |
| 9761 | // Add the constructor information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9762 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9763 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9764 | // Add the at return information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9765 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9766 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9767 | // Add information on whether this frame is invoked in the debugger context. |
| 9768 | details->set(kFrameDetailsDebuggerFrameIndex, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9769 | heap->ToBoolean(*save->context() == |
| 9770 | *isolate->debug()->debug_context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9771 | |
| 9772 | // Fill the dynamic part. |
| 9773 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 9774 | |
| 9775 | // Add arguments name and value. |
| 9776 | for (int i = 0; i < argument_count; i++) { |
| 9777 | // Name of the argument. |
| 9778 | if (i < info.number_of_parameters()) { |
| 9779 | details->set(details_index++, *info.parameter_name(i)); |
| 9780 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9781 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9782 | } |
| 9783 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9784 | // Parameter value. If we are inspecting an optimized frame, use |
| 9785 | // undefined as the value. |
| 9786 | // |
| 9787 | // TODO(3141533): We should be able to get the actual parameter |
| 9788 | // value for optimized frames. |
| 9789 | if (!is_optimized_frame && |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9790 | (i < it.frame()->ComputeParametersCount())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9791 | details->set(details_index++, it.frame()->GetParameter(i)); |
| 9792 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9793 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9794 | } |
| 9795 | } |
| 9796 | |
| 9797 | // Add locals name and value from the temporary copy from the function frame. |
| 9798 | for (int i = 0; i < info.NumberOfLocals() * 2; i++) { |
| 9799 | details->set(details_index++, locals->get(i)); |
| 9800 | } |
| 9801 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9802 | // Add the value being returned. |
| 9803 | if (at_return) { |
| 9804 | details->set(details_index++, *return_value); |
| 9805 | } |
| 9806 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9807 | // Add the receiver (same as in function frame). |
| 9808 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 9809 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9810 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9811 | if (!receiver->IsJSObject()) { |
| 9812 | // If the receiver is NOT a JSObject we have hit an optimization |
| 9813 | // where a value object is not converted into a wrapped JS objects. |
| 9814 | // To hide this optimization from the debugger, we wrap the receiver |
| 9815 | // by creating correct wrapper object based on the calling frame's |
| 9816 | // global context. |
| 9817 | it.Advance(); |
| 9818 | Handle<Context> calling_frames_global_context( |
| 9819 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9820 | receiver = |
| 9821 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9822 | } |
| 9823 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 9824 | |
| 9825 | ASSERT_EQ(details_size, details_index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9826 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9827 | } |
| 9828 | |
| 9829 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9830 | // Copy all the context locals into an object used to materialize a scope. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9831 | static bool CopyContextLocalsToScopeObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9832 | Isolate* isolate, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9833 | Handle<SerializedScopeInfo> serialized_scope_info, |
| 9834 | ScopeInfo<>& scope_info, |
| 9835 | Handle<Context> context, |
| 9836 | Handle<JSObject> scope_object) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9837 | // Fill all context locals to the context extension. |
| 9838 | for (int i = Context::MIN_CONTEXT_SLOTS; |
| 9839 | i < scope_info.number_of_context_slots(); |
| 9840 | i++) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9841 | int context_index = serialized_scope_info->ContextSlotIndex( |
| 9842 | *scope_info.context_slot_name(i), NULL); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9843 | |
| 9844 | // Don't include the arguments shadow (.arguments) context variable. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9845 | if (*scope_info.context_slot_name(i) != |
| 9846 | isolate->heap()->arguments_shadow_symbol()) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9847 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9848 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9849 | SetProperty(scope_object, |
| 9850 | scope_info.context_slot_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9851 | Handle<Object>(context->get(context_index), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9852 | NONE, |
| 9853 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9854 | false); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9855 | } |
| 9856 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9857 | |
| 9858 | return true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9859 | } |
| 9860 | |
| 9861 | |
| 9862 | // Create a plain JSObject which materializes the local scope for the specified |
| 9863 | // frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9864 | static Handle<JSObject> MaterializeLocalScope(Isolate* isolate, |
| 9865 | JavaScriptFrame* frame) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9866 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9867 | Handle<SharedFunctionInfo> shared(function->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9868 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 9869 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9870 | |
| 9871 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 9872 | // heap locals and extension properties of the debugged function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9873 | Handle<JSObject> local_scope = |
| 9874 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9875 | |
| 9876 | // First fill all parameters. |
| 9877 | for (int i = 0; i < scope_info.number_of_parameters(); ++i) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9878 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9879 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9880 | SetProperty(local_scope, |
| 9881 | scope_info.parameter_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9882 | Handle<Object>(frame->GetParameter(i), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9883 | NONE, |
| 9884 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9885 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
| 9888 | // Second fill all stack locals. |
| 9889 | for (int i = 0; i < scope_info.number_of_stack_slots(); i++) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9890 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9891 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9892 | SetProperty(local_scope, |
| 9893 | scope_info.stack_slot_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9894 | Handle<Object>(frame->GetExpression(i), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9895 | NONE, |
| 9896 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9897 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9898 | } |
| 9899 | |
| 9900 | // Third fill all context locals. |
| 9901 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 9902 | Handle<Context> function_context(frame_context->fcontext()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9903 | if (!CopyContextLocalsToScopeObject(isolate, |
| 9904 | serialized_scope_info, scope_info, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9905 | function_context, local_scope)) { |
| 9906 | return Handle<JSObject>(); |
| 9907 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9908 | |
| 9909 | // Finally copy any properties from the function context extension. This will |
| 9910 | // be variables introduced by eval. |
| 9911 | if (function_context->closure() == *function) { |
| 9912 | if (function_context->has_extension() && |
| 9913 | !function_context->IsGlobalContext()) { |
| 9914 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9915 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9916 | for (int i = 0; i < keys->length(); i++) { |
| 9917 | // Names of variables introduced by eval are strings. |
| 9918 | ASSERT(keys->get(i)->IsString()); |
| 9919 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9920 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9921 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9922 | SetProperty(local_scope, |
| 9923 | key, |
| 9924 | GetProperty(ext, key), |
| 9925 | NONE, |
| 9926 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9927 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9928 | } |
| 9929 | } |
| 9930 | } |
| 9931 | return local_scope; |
| 9932 | } |
| 9933 | |
| 9934 | |
| 9935 | // Create a plain JSObject which materializes the closure content for the |
| 9936 | // context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9937 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 9938 | Handle<Context> context) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9939 | ASSERT(context->is_function_context()); |
| 9940 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9941 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9942 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 9943 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9944 | |
| 9945 | // Allocate and initialize a JSObject with all the content of theis function |
| 9946 | // closure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9947 | Handle<JSObject> closure_scope = |
| 9948 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9949 | |
| 9950 | // Check whether the arguments shadow object exists. |
| 9951 | int arguments_shadow_index = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9952 | shared->scope_info()->ContextSlotIndex( |
| 9953 | isolate->heap()->arguments_shadow_symbol(), NULL); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9954 | if (arguments_shadow_index >= 0) { |
| 9955 | // In this case all the arguments are available in the arguments shadow |
| 9956 | // object. |
| 9957 | Handle<JSObject> arguments_shadow( |
| 9958 | JSObject::cast(context->get(arguments_shadow_index))); |
| 9959 | for (int i = 0; i < scope_info.number_of_parameters(); ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9960 | // We don't expect exception-throwing getters on the arguments shadow. |
| 9961 | Object* element = arguments_shadow->GetElement(i)->ToObjectUnchecked(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9962 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9963 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9964 | SetProperty(closure_scope, |
| 9965 | scope_info.parameter_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9966 | Handle<Object>(element, isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9967 | NONE, |
| 9968 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9969 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9970 | } |
| 9971 | } |
| 9972 | |
| 9973 | // Fill all context locals to the context extension. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9974 | if (!CopyContextLocalsToScopeObject(isolate, |
| 9975 | serialized_scope_info, scope_info, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9976 | context, closure_scope)) { |
| 9977 | return Handle<JSObject>(); |
| 9978 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9979 | |
| 9980 | // Finally copy any properties from the function context extension. This will |
| 9981 | // be variables introduced by eval. |
| 9982 | if (context->has_extension()) { |
| 9983 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9984 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9985 | for (int i = 0; i < keys->length(); i++) { |
| 9986 | // Names of variables introduced by eval are strings. |
| 9987 | ASSERT(keys->get(i)->IsString()); |
| 9988 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 9989 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 9990 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9991 | SetProperty(closure_scope, |
| 9992 | key, |
| 9993 | GetProperty(ext, key), |
| 9994 | NONE, |
| 9995 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9996 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9997 | } |
| 9998 | } |
| 9999 | |
| 10000 | return closure_scope; |
| 10001 | } |
| 10002 | |
| 10003 | |
| 10004 | // Iterate over the actual scopes visible from a stack frame. All scopes are |
| 10005 | // backed by an actual context except the local scope, which is inserted |
| 10006 | // "artifically" in the context chain. |
| 10007 | class ScopeIterator { |
| 10008 | public: |
| 10009 | enum ScopeType { |
| 10010 | ScopeTypeGlobal = 0, |
| 10011 | ScopeTypeLocal, |
| 10012 | ScopeTypeWith, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10013 | ScopeTypeClosure, |
| 10014 | // Every catch block contains an implicit with block (its parameter is |
| 10015 | // a JSContextExtensionObject) that extends current scope with a variable |
| 10016 | // holding exception object. Such with blocks are treated as scopes of their |
| 10017 | // own type. |
| 10018 | ScopeTypeCatch |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10019 | }; |
| 10020 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10021 | ScopeIterator(Isolate* isolate, JavaScriptFrame* frame) |
| 10022 | : isolate_(isolate), |
| 10023 | frame_(frame), |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10024 | function_(JSFunction::cast(frame->function())), |
| 10025 | context_(Context::cast(frame->context())), |
| 10026 | local_done_(false), |
| 10027 | at_local_(false) { |
| 10028 | |
| 10029 | // Check whether the first scope is actually a local scope. |
| 10030 | if (context_->IsGlobalContext()) { |
| 10031 | // If there is a stack slot for .result then this local scope has been |
| 10032 | // created for evaluating top level code and it is not a real local scope. |
| 10033 | // Checking for the existence of .result seems fragile, but the scope info |
| 10034 | // saved with the code object does not otherwise have that information. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10035 | int index = function_->shared()->scope_info()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10036 | StackSlotIndex(isolate_->heap()->result_symbol()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10037 | at_local_ = index < 0; |
| 10038 | } else if (context_->is_function_context()) { |
| 10039 | at_local_ = true; |
| 10040 | } |
| 10041 | } |
| 10042 | |
| 10043 | // More scopes? |
| 10044 | bool Done() { return context_.is_null(); } |
| 10045 | |
| 10046 | // Move to the next scope. |
| 10047 | void Next() { |
| 10048 | // If at a local scope mark the local scope as passed. |
| 10049 | if (at_local_) { |
| 10050 | at_local_ = false; |
| 10051 | local_done_ = true; |
| 10052 | |
| 10053 | // If the current context is not associated with the local scope the |
| 10054 | // current context is the next real scope, so don't move to the next |
| 10055 | // context in this case. |
| 10056 | if (context_->closure() != *function_) { |
| 10057 | return; |
| 10058 | } |
| 10059 | } |
| 10060 | |
| 10061 | // The global scope is always the last in the chain. |
| 10062 | if (context_->IsGlobalContext()) { |
| 10063 | context_ = Handle<Context>(); |
| 10064 | return; |
| 10065 | } |
| 10066 | |
| 10067 | // Move to the next context. |
| 10068 | if (context_->is_function_context()) { |
| 10069 | context_ = Handle<Context>(Context::cast(context_->closure()->context())); |
| 10070 | } else { |
| 10071 | context_ = Handle<Context>(context_->previous()); |
| 10072 | } |
| 10073 | |
| 10074 | // If passing the local scope indicate that the current scope is now the |
| 10075 | // local scope. |
| 10076 | if (!local_done_ && |
| 10077 | (context_->IsGlobalContext() || (context_->is_function_context()))) { |
| 10078 | at_local_ = true; |
| 10079 | } |
| 10080 | } |
| 10081 | |
| 10082 | // Return the type of the current scope. |
| 10083 | int Type() { |
| 10084 | if (at_local_) { |
| 10085 | return ScopeTypeLocal; |
| 10086 | } |
| 10087 | if (context_->IsGlobalContext()) { |
| 10088 | ASSERT(context_->global()->IsGlobalObject()); |
| 10089 | return ScopeTypeGlobal; |
| 10090 | } |
| 10091 | if (context_->is_function_context()) { |
| 10092 | return ScopeTypeClosure; |
| 10093 | } |
| 10094 | ASSERT(context_->has_extension()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10095 | // Current scope is either an explicit with statement or a with statement |
| 10096 | // implicitely generated for a catch block. |
| 10097 | // If the extension object here is a JSContextExtensionObject then |
| 10098 | // current with statement is one frome a catch block otherwise it's a |
| 10099 | // regular with statement. |
| 10100 | if (context_->extension()->IsJSContextExtensionObject()) { |
| 10101 | return ScopeTypeCatch; |
| 10102 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10103 | return ScopeTypeWith; |
| 10104 | } |
| 10105 | |
| 10106 | // Return the JavaScript object with the content of the current scope. |
| 10107 | Handle<JSObject> ScopeObject() { |
| 10108 | switch (Type()) { |
| 10109 | case ScopeIterator::ScopeTypeGlobal: |
| 10110 | return Handle<JSObject>(CurrentContext()->global()); |
| 10111 | break; |
| 10112 | case ScopeIterator::ScopeTypeLocal: |
| 10113 | // Materialize the content of the local scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10114 | return MaterializeLocalScope(isolate_, frame_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10115 | break; |
| 10116 | case ScopeIterator::ScopeTypeWith: |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10117 | case ScopeIterator::ScopeTypeCatch: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10118 | // Return the with object. |
| 10119 | return Handle<JSObject>(CurrentContext()->extension()); |
| 10120 | break; |
| 10121 | case ScopeIterator::ScopeTypeClosure: |
| 10122 | // Materialize the content of the closure scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10123 | return MaterializeClosure(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10124 | break; |
| 10125 | } |
| 10126 | UNREACHABLE(); |
| 10127 | return Handle<JSObject>(); |
| 10128 | } |
| 10129 | |
| 10130 | // Return the context for this scope. For the local context there might not |
| 10131 | // be an actual context. |
| 10132 | Handle<Context> CurrentContext() { |
| 10133 | if (at_local_ && context_->closure() != *function_) { |
| 10134 | return Handle<Context>(); |
| 10135 | } |
| 10136 | return context_; |
| 10137 | } |
| 10138 | |
| 10139 | #ifdef DEBUG |
| 10140 | // Debug print of the content of the current scope. |
| 10141 | void DebugPrint() { |
| 10142 | switch (Type()) { |
| 10143 | case ScopeIterator::ScopeTypeGlobal: |
| 10144 | PrintF("Global:\n"); |
| 10145 | CurrentContext()->Print(); |
| 10146 | break; |
| 10147 | |
| 10148 | case ScopeIterator::ScopeTypeLocal: { |
| 10149 | PrintF("Local:\n"); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10150 | ScopeInfo<> scope_info(function_->shared()->scope_info()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10151 | scope_info.Print(); |
| 10152 | if (!CurrentContext().is_null()) { |
| 10153 | CurrentContext()->Print(); |
| 10154 | if (CurrentContext()->has_extension()) { |
| 10155 | Handle<JSObject> extension = |
| 10156 | Handle<JSObject>(CurrentContext()->extension()); |
| 10157 | if (extension->IsJSContextExtensionObject()) { |
| 10158 | extension->Print(); |
| 10159 | } |
| 10160 | } |
| 10161 | } |
| 10162 | break; |
| 10163 | } |
| 10164 | |
| 10165 | case ScopeIterator::ScopeTypeWith: { |
| 10166 | PrintF("With:\n"); |
| 10167 | Handle<JSObject> extension = |
| 10168 | Handle<JSObject>(CurrentContext()->extension()); |
| 10169 | extension->Print(); |
| 10170 | break; |
| 10171 | } |
| 10172 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10173 | case ScopeIterator::ScopeTypeCatch: { |
| 10174 | PrintF("Catch:\n"); |
| 10175 | Handle<JSObject> extension = |
| 10176 | Handle<JSObject>(CurrentContext()->extension()); |
| 10177 | extension->Print(); |
| 10178 | break; |
| 10179 | } |
| 10180 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10181 | case ScopeIterator::ScopeTypeClosure: { |
| 10182 | PrintF("Closure:\n"); |
| 10183 | CurrentContext()->Print(); |
| 10184 | if (CurrentContext()->has_extension()) { |
| 10185 | Handle<JSObject> extension = |
| 10186 | Handle<JSObject>(CurrentContext()->extension()); |
| 10187 | if (extension->IsJSContextExtensionObject()) { |
| 10188 | extension->Print(); |
| 10189 | } |
| 10190 | } |
| 10191 | break; |
| 10192 | } |
| 10193 | |
| 10194 | default: |
| 10195 | UNREACHABLE(); |
| 10196 | } |
| 10197 | PrintF("\n"); |
| 10198 | } |
| 10199 | #endif |
| 10200 | |
| 10201 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10202 | Isolate* isolate_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10203 | JavaScriptFrame* frame_; |
| 10204 | Handle<JSFunction> function_; |
| 10205 | Handle<Context> context_; |
| 10206 | bool local_done_; |
| 10207 | bool at_local_; |
| 10208 | |
| 10209 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 10210 | }; |
| 10211 | |
| 10212 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10213 | static MaybeObject* Runtime_GetScopeCount(RUNTIME_CALLING_CONVENTION) { |
| 10214 | RUNTIME_GET_ISOLATE; |
| 10215 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10216 | ASSERT(args.length() == 2); |
| 10217 | |
| 10218 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10219 | Object* check; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10220 | { MaybeObject* maybe_check = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10221 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10222 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10223 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10224 | |
| 10225 | // Get the frame where the debugging is performed. |
| 10226 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
| 10227 | JavaScriptFrameIterator it(id); |
| 10228 | JavaScriptFrame* frame = it.frame(); |
| 10229 | |
| 10230 | // Count the visible scopes. |
| 10231 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10232 | for (ScopeIterator it(isolate, frame); !it.Done(); it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10233 | n++; |
| 10234 | } |
| 10235 | |
| 10236 | return Smi::FromInt(n); |
| 10237 | } |
| 10238 | |
| 10239 | |
| 10240 | static const int kScopeDetailsTypeIndex = 0; |
| 10241 | static const int kScopeDetailsObjectIndex = 1; |
| 10242 | static const int kScopeDetailsSize = 2; |
| 10243 | |
| 10244 | // Return an array with scope details |
| 10245 | // args[0]: number: break id |
| 10246 | // args[1]: number: frame index |
| 10247 | // args[2]: number: scope index |
| 10248 | // |
| 10249 | // The array returned contains the following information: |
| 10250 | // 0: Scope type |
| 10251 | // 1: Scope object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10252 | static MaybeObject* Runtime_GetScopeDetails(RUNTIME_CALLING_CONVENTION) { |
| 10253 | RUNTIME_GET_ISOLATE; |
| 10254 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10255 | ASSERT(args.length() == 3); |
| 10256 | |
| 10257 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10258 | Object* check; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10259 | { MaybeObject* maybe_check = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10260 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10261 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10262 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10263 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[2]); |
| 10264 | |
| 10265 | // Get the frame where the debugging is performed. |
| 10266 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
| 10267 | JavaScriptFrameIterator frame_it(id); |
| 10268 | JavaScriptFrame* frame = frame_it.frame(); |
| 10269 | |
| 10270 | // Find the requested scope. |
| 10271 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10272 | ScopeIterator it(isolate, frame); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10273 | for (; !it.Done() && n < index; it.Next()) { |
| 10274 | n++; |
| 10275 | } |
| 10276 | if (it.Done()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10277 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10278 | } |
| 10279 | |
| 10280 | // Calculate the size of the result. |
| 10281 | int details_size = kScopeDetailsSize; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10282 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10283 | |
| 10284 | // Fill in scope details. |
| 10285 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10286 | Handle<JSObject> scope_object = it.ScopeObject(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10287 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10288 | details->set(kScopeDetailsObjectIndex, *scope_object); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10289 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10290 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10291 | } |
| 10292 | |
| 10293 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10294 | static MaybeObject* Runtime_DebugPrintScopes(RUNTIME_CALLING_CONVENTION) { |
| 10295 | RUNTIME_GET_ISOLATE; |
| 10296 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10297 | ASSERT(args.length() == 0); |
| 10298 | |
| 10299 | #ifdef DEBUG |
| 10300 | // Print the scopes for the top frame. |
| 10301 | StackFrameLocator locator; |
| 10302 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10303 | for (ScopeIterator it(isolate, frame); !it.Done(); it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10304 | it.DebugPrint(); |
| 10305 | } |
| 10306 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10307 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10308 | } |
| 10309 | |
| 10310 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10311 | static MaybeObject* Runtime_GetThreadCount(RUNTIME_CALLING_CONVENTION) { |
| 10312 | RUNTIME_GET_ISOLATE; |
| 10313 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10314 | ASSERT(args.length() == 1); |
| 10315 | |
| 10316 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10317 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10318 | { MaybeObject* maybe_result = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10319 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10320 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10321 | |
| 10322 | // Count all archived V8 threads. |
| 10323 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10324 | for (ThreadState* thread = |
| 10325 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10326 | thread != NULL; |
| 10327 | thread = thread->Next()) { |
| 10328 | n++; |
| 10329 | } |
| 10330 | |
| 10331 | // Total number of threads is current thread and archived threads. |
| 10332 | return Smi::FromInt(n + 1); |
| 10333 | } |
| 10334 | |
| 10335 | |
| 10336 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 10337 | static const int kThreadDetailsThreadIdIndex = 1; |
| 10338 | static const int kThreadDetailsSize = 2; |
| 10339 | |
| 10340 | // Return an array with thread details |
| 10341 | // args[0]: number: break id |
| 10342 | // args[1]: number: thread index |
| 10343 | // |
| 10344 | // The array returned contains the following information: |
| 10345 | // 0: Is current thread? |
| 10346 | // 1: Thread id |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10347 | static MaybeObject* Runtime_GetThreadDetails(RUNTIME_CALLING_CONVENTION) { |
| 10348 | RUNTIME_GET_ISOLATE; |
| 10349 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10350 | ASSERT(args.length() == 2); |
| 10351 | |
| 10352 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10353 | Object* check; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10354 | { MaybeObject* maybe_check = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10355 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10356 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10357 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 10358 | |
| 10359 | // Allocate array for result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10360 | Handle<FixedArray> details = |
| 10361 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10362 | |
| 10363 | // Thread index 0 is current thread. |
| 10364 | if (index == 0) { |
| 10365 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10366 | details->set(kThreadDetailsCurrentThreadIndex, |
| 10367 | isolate->heap()->true_value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10368 | details->set(kThreadDetailsThreadIdIndex, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10369 | Smi::FromInt( |
| 10370 | isolate->thread_manager()->CurrentId())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10371 | } else { |
| 10372 | // Find the thread with the requested index. |
| 10373 | int n = 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10374 | ThreadState* thread = |
| 10375 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10376 | while (index != n && thread != NULL) { |
| 10377 | thread = thread->Next(); |
| 10378 | n++; |
| 10379 | } |
| 10380 | if (thread == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10381 | return isolate->heap()->undefined_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10382 | } |
| 10383 | |
| 10384 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10385 | details->set(kThreadDetailsCurrentThreadIndex, |
| 10386 | isolate->heap()->false_value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10387 | details->set(kThreadDetailsThreadIdIndex, Smi::FromInt(thread->id())); |
| 10388 | } |
| 10389 | |
| 10390 | // Convert to JS array and return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10391 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10392 | } |
| 10393 | |
| 10394 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10395 | // Sets the disable break state |
| 10396 | // args[0]: disable break state |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10397 | static MaybeObject* Runtime_SetDisableBreak(RUNTIME_CALLING_CONVENTION) { |
| 10398 | RUNTIME_GET_ISOLATE; |
| 10399 | HandleScope scope(isolate); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10400 | ASSERT(args.length() == 1); |
| 10401 | CONVERT_BOOLEAN_CHECKED(disable_break, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10402 | isolate->debug()->set_disable_break(disable_break); |
| 10403 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10404 | } |
| 10405 | |
| 10406 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10407 | static MaybeObject* Runtime_GetBreakLocations(RUNTIME_CALLING_CONVENTION) { |
| 10408 | RUNTIME_GET_ISOLATE; |
| 10409 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10410 | ASSERT(args.length() == 1); |
| 10411 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10412 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 10413 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10414 | // Find the number of break points |
| 10415 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10416 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10417 | // Return array as JS array |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10418 | return *isolate->factory()->NewJSArrayWithElements( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10419 | Handle<FixedArray>::cast(break_locations)); |
| 10420 | } |
| 10421 | |
| 10422 | |
| 10423 | // Set a break point in a function |
| 10424 | // args[0]: function |
| 10425 | // args[1]: number: break source position (within the function source) |
| 10426 | // args[2]: number: break point object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10427 | static MaybeObject* Runtime_SetFunctionBreakPoint(RUNTIME_CALLING_CONVENTION) { |
| 10428 | RUNTIME_GET_ISOLATE; |
| 10429 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10430 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10431 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 10432 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10433 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 10434 | RUNTIME_ASSERT(source_position >= 0); |
| 10435 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 10436 | |
| 10437 | // Set break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10438 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 10439 | &source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10440 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 10441 | return Smi::FromInt(source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10442 | } |
| 10443 | |
| 10444 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10445 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 10446 | Handle<Script> script, |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 10447 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10448 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 10449 | // script. The inner most SharedFunctionInfo containing the source position |
| 10450 | // for the requested break point is found. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10451 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10452 | // which is found is not compiled it is compiled and the heap is iterated |
| 10453 | // again as the compilation might create inner functions from the newly |
| 10454 | // compiled function and the actual requested break point might be in one of |
| 10455 | // these functions. |
| 10456 | bool done = false; |
| 10457 | // The current candidate for the source position: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10458 | int target_start_position = RelocInfo::kNoPosition; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10459 | Handle<SharedFunctionInfo> target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10460 | while (!done) { |
| 10461 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10462 | for (HeapObject* obj = iterator.next(); |
| 10463 | obj != NULL; obj = iterator.next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10464 | if (obj->IsSharedFunctionInfo()) { |
| 10465 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 10466 | if (shared->script() == *script) { |
| 10467 | // If the SharedFunctionInfo found has the requested script data and |
| 10468 | // contains the source position it is a candidate. |
| 10469 | int start_position = shared->function_token_position(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10470 | if (start_position == RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10471 | start_position = shared->start_position(); |
| 10472 | } |
| 10473 | if (start_position <= position && |
| 10474 | position <= shared->end_position()) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10475 | // If there is no candidate or this function is within the current |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10476 | // candidate this is the new candidate. |
| 10477 | if (target.is_null()) { |
| 10478 | target_start_position = start_position; |
| 10479 | target = shared; |
| 10480 | } else { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10481 | if (target_start_position == start_position && |
| 10482 | shared->end_position() == target->end_position()) { |
| 10483 | // If a top-level function contain only one function |
| 10484 | // declartion the source for the top-level and the function is |
| 10485 | // the same. In that case prefer the non top-level function. |
| 10486 | if (!shared->is_toplevel()) { |
| 10487 | target_start_position = start_position; |
| 10488 | target = shared; |
| 10489 | } |
| 10490 | } else if (target_start_position <= start_position && |
| 10491 | shared->end_position() <= target->end_position()) { |
| 10492 | // This containment check includes equality as a function inside |
| 10493 | // a top-level function can share either start or end position |
| 10494 | // with the top-level function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10495 | target_start_position = start_position; |
| 10496 | target = shared; |
| 10497 | } |
| 10498 | } |
| 10499 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10500 | } |
| 10501 | } |
| 10502 | } |
| 10503 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10504 | if (target.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10505 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10506 | } |
| 10507 | |
| 10508 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 10509 | // compilation of inner functions is introduced some additional checking |
| 10510 | // needs to be done here to compile inner functions. |
| 10511 | done = target->is_compiled(); |
| 10512 | if (!done) { |
| 10513 | // If the candidate is not compiled compile it to reveal any inner |
| 10514 | // functions which might contain the requested source position. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10515 | CompileLazyShared(target, KEEP_EXCEPTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10516 | } |
| 10517 | } |
| 10518 | |
| 10519 | return *target; |
| 10520 | } |
| 10521 | |
| 10522 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10523 | // Changes the state of a break point in a script and returns source position |
| 10524 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 10525 | // GetScriptFromScriptData. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10526 | // args[0]: script to set break point in |
| 10527 | // args[1]: number: break source position (within the script source) |
| 10528 | // args[2]: number: break point object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10529 | static MaybeObject* Runtime_SetScriptBreakPoint(RUNTIME_CALLING_CONVENTION) { |
| 10530 | RUNTIME_GET_ISOLATE; |
| 10531 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10532 | ASSERT(args.length() == 3); |
| 10533 | CONVERT_ARG_CHECKED(JSValue, wrapper, 0); |
| 10534 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 10535 | RUNTIME_ASSERT(source_position >= 0); |
| 10536 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 10537 | |
| 10538 | // Get the script from the script wrapper. |
| 10539 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 10540 | Handle<Script> script(Script::cast(wrapper->value())); |
| 10541 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 10542 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10543 | isolate, script, source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10544 | if (!result->IsUndefined()) { |
| 10545 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 10546 | // Find position within function. The script position might be before the |
| 10547 | // source position of the first function. |
| 10548 | int position; |
| 10549 | if (shared->start_position() > source_position) { |
| 10550 | position = 0; |
| 10551 | } else { |
| 10552 | position = source_position - shared->start_position(); |
| 10553 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10554 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10555 | position += shared->start_position(); |
| 10556 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10557 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10558 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10559 | } |
| 10560 | |
| 10561 | |
| 10562 | // Clear a break point |
| 10563 | // args[0]: number: break point object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10564 | static MaybeObject* Runtime_ClearBreakPoint(RUNTIME_CALLING_CONVENTION) { |
| 10565 | RUNTIME_GET_ISOLATE; |
| 10566 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10567 | ASSERT(args.length() == 1); |
| 10568 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 10569 | |
| 10570 | // Clear break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10571 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10572 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10573 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10574 | } |
| 10575 | |
| 10576 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10577 | // Change the state of break on exceptions. |
| 10578 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 10579 | // args[1]: Boolean indicating on/off. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10580 | static MaybeObject* Runtime_ChangeBreakOnException(RUNTIME_CALLING_CONVENTION) { |
| 10581 | RUNTIME_GET_ISOLATE; |
| 10582 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10583 | ASSERT(args.length() == 2); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10584 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 10585 | CONVERT_BOOLEAN_CHECKED(enable, args[1]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10586 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10587 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 10588 | // function will default to affecting caught exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10589 | ExceptionBreakType type = |
| 10590 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10591 | // Update break point state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10592 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 10593 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10594 | } |
| 10595 | |
| 10596 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10597 | // Returns the state of break on exceptions |
| 10598 | // args[0]: boolean indicating uncaught exceptions |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10599 | static MaybeObject* Runtime_IsBreakOnException(RUNTIME_CALLING_CONVENTION) { |
| 10600 | RUNTIME_GET_ISOLATE; |
| 10601 | HandleScope scope(isolate); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10602 | ASSERT(args.length() == 1); |
| 10603 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 10604 | |
| 10605 | ExceptionBreakType type = |
| 10606 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10607 | bool result = isolate->debug()->IsBreakOnException(type); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10608 | return Smi::FromInt(result); |
| 10609 | } |
| 10610 | |
| 10611 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10612 | // Prepare for stepping |
| 10613 | // args[0]: break id for checking execution state |
| 10614 | // args[1]: step action from the enumeration StepAction |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10615 | // args[2]: number of times to perform the step, for step out it is the number |
| 10616 | // of frames to step down. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10617 | static MaybeObject* Runtime_PrepareStep(RUNTIME_CALLING_CONVENTION) { |
| 10618 | RUNTIME_GET_ISOLATE; |
| 10619 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10620 | ASSERT(args.length() == 3); |
| 10621 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10622 | Object* check; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10623 | { MaybeObject* maybe_check = Runtime_CheckExecutionState(args, isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10624 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10625 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10626 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10627 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10628 | } |
| 10629 | |
| 10630 | // Get the step action and check validity. |
| 10631 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 10632 | if (step_action != StepIn && |
| 10633 | step_action != StepNext && |
| 10634 | step_action != StepOut && |
| 10635 | step_action != StepInMin && |
| 10636 | step_action != StepMin) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10637 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10638 | } |
| 10639 | |
| 10640 | // Get the number of steps. |
| 10641 | int step_count = NumberToInt32(args[2]); |
| 10642 | if (step_count < 1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10643 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10644 | } |
| 10645 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10646 | // Clear all current stepping setup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10647 | isolate->debug()->ClearStepping(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10648 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10649 | // Prepare step. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10650 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 10651 | step_count); |
| 10652 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10653 | } |
| 10654 | |
| 10655 | |
| 10656 | // Clear all stepping set by PrepareStep. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10657 | static MaybeObject* Runtime_ClearStepping(RUNTIME_CALLING_CONVENTION) { |
| 10658 | RUNTIME_GET_ISOLATE; |
| 10659 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10660 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10661 | isolate->debug()->ClearStepping(); |
| 10662 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10663 | } |
| 10664 | |
| 10665 | |
| 10666 | // Creates a copy of the with context chain. The copy of the context chain is |
| 10667 | // is linked to the function context supplied. |
| 10668 | static Handle<Context> CopyWithContextChain(Handle<Context> context_chain, |
| 10669 | Handle<Context> function_context) { |
| 10670 | // At the bottom of the chain. Return the function context to link to. |
| 10671 | if (context_chain->is_function_context()) { |
| 10672 | return function_context; |
| 10673 | } |
| 10674 | |
| 10675 | // Recursively copy the with contexts. |
| 10676 | Handle<Context> previous(context_chain->previous()); |
| 10677 | Handle<JSObject> extension(JSObject::cast(context_chain->extension())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10678 | Handle<Context> context = CopyWithContextChain(function_context, previous); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10679 | return context->GetIsolate()->factory()->NewWithContext( |
| 10680 | context, extension, context_chain->IsCatchContext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10681 | } |
| 10682 | |
| 10683 | |
| 10684 | // Helper function to find or create the arguments object for |
| 10685 | // Runtime_DebugEvaluate. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10686 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 10687 | JavaScriptFrame* frame, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10688 | Handle<JSFunction> function, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10689 | Handle<SerializedScopeInfo> scope_info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10690 | const ScopeInfo<>* sinfo, |
| 10691 | Handle<Context> function_context) { |
| 10692 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 10693 | // found (that is the debugged function does not reference 'arguments' and |
| 10694 | // does not support eval) then create an 'arguments' object. |
| 10695 | int index; |
| 10696 | if (sinfo->number_of_stack_slots() > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10697 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10698 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10699 | return Handle<Object>(frame->GetExpression(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10700 | } |
| 10701 | } |
| 10702 | |
| 10703 | if (sinfo->number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10704 | index = scope_info->ContextSlotIndex(isolate->heap()->arguments_symbol(), |
| 10705 | NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10706 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10707 | return Handle<Object>(function_context->get(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10708 | } |
| 10709 | } |
| 10710 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 10711 | const int length = frame->ComputeParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10712 | Handle<JSObject> arguments = |
| 10713 | isolate->factory()->NewArgumentsObject(function, length); |
| 10714 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10715 | |
| 10716 | AssertNoAllocation no_gc; |
| 10717 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10718 | for (int i = 0; i < length; i++) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 10719 | array->set(i, frame->GetParameter(i), mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10720 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 10721 | arguments->set_elements(*array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10722 | return arguments; |
| 10723 | } |
| 10724 | |
| 10725 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10726 | static const char kSourceStr[] = |
| 10727 | "(function(arguments,__source__){return eval(__source__);})"; |
| 10728 | |
| 10729 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10730 | // Evaluate a piece of JavaScript in the context of a stack frame for |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10731 | // debugging. This is accomplished by creating a new context which in its |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10732 | // extension part has all the parameters and locals of the function on the |
| 10733 | // stack frame. A function which calls eval with the code to evaluate is then |
| 10734 | // compiled in this context and called in this context. As this context |
| 10735 | // replaces the context of the function on the stack frame a new (empty) |
| 10736 | // function is created as well to be used as the closure for the context. |
| 10737 | // This function and the context acts as replacements for the function on the |
| 10738 | // stack frame presenting the same view of the values of parameters and |
| 10739 | // local variables as if the piece of JavaScript was evaluated at the point |
| 10740 | // where the function on the stack frame is currently stopped. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10741 | static MaybeObject* Runtime_DebugEvaluate(RUNTIME_CALLING_CONVENTION) { |
| 10742 | RUNTIME_GET_ISOLATE; |
| 10743 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10744 | |
| 10745 | // Check the execution state and decode arguments frame and source to be |
| 10746 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10747 | ASSERT(args.length() == 5); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10748 | Object* check_result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10749 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState(args, |
| 10750 | isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10751 | if (!maybe_check_result->ToObject(&check_result)) { |
| 10752 | return maybe_check_result; |
| 10753 | } |
| 10754 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10755 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10756 | CONVERT_ARG_CHECKED(String, source, 2); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10757 | CONVERT_BOOLEAN_CHECKED(disable_break, args[3]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10758 | Handle<Object> additional_context(args[4]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10759 | |
| 10760 | // Handle the processing of break. |
| 10761 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10762 | |
| 10763 | // Get the frame where the debugging is performed. |
| 10764 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
| 10765 | JavaScriptFrameIterator it(id); |
| 10766 | JavaScriptFrame* frame = it.frame(); |
| 10767 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10768 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10769 | ScopeInfo<> sinfo(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10770 | |
| 10771 | // Traverse the saved contexts chain to find the active context for the |
| 10772 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10773 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10774 | while (save != NULL && !save->below(frame)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10775 | save = save->prev(); |
| 10776 | } |
| 10777 | ASSERT(save != NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10778 | SaveContext savex(isolate); |
| 10779 | isolate->set_context(*(save->context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10780 | |
| 10781 | // Create the (empty) function replacing the function on the stack frame for |
| 10782 | // the purpose of evaluating in the context created below. It is important |
| 10783 | // that this function does not describe any parameters and local variables |
| 10784 | // in the context. If it does then this will cause problems with the lookup |
| 10785 | // in Context::Lookup, where context slots for parameters and local variables |
| 10786 | // are looked at before the extension object. |
| 10787 | Handle<JSFunction> go_between = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10788 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 10789 | isolate->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10790 | go_between->set_context(function->context()); |
| 10791 | #ifdef DEBUG |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10792 | ScopeInfo<> go_between_sinfo(go_between->shared()->scope_info()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10793 | ASSERT(go_between_sinfo.number_of_parameters() == 0); |
| 10794 | ASSERT(go_between_sinfo.number_of_context_slots() == 0); |
| 10795 | #endif |
| 10796 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10797 | // Materialize the content of the local scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10798 | Handle<JSObject> local_scope = MaterializeLocalScope(isolate, frame); |
| 10799 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10800 | |
| 10801 | // Allocate a new context for the debug evaluation and set the extension |
| 10802 | // object build. |
| 10803 | Handle<Context> context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10804 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 10805 | go_between); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10806 | context->set_extension(*local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10807 | // Copy any with contexts present and chain them in front of this context. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10808 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 10809 | Handle<Context> function_context(frame_context->fcontext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10810 | context = CopyWithContextChain(frame_context, context); |
| 10811 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10812 | if (additional_context->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10813 | context = isolate->factory()->NewWithContext(context, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10814 | Handle<JSObject>::cast(additional_context), false); |
| 10815 | } |
| 10816 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10817 | // Wrap the evaluation statement in a new function compiled in the newly |
| 10818 | // created context. The function has one parameter which has to be called |
| 10819 | // 'arguments'. This it to have access to what would have been 'arguments' in |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10820 | // the function being debugged. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10821 | // function(arguments,__source__) {return eval(__source__);} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10822 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10823 | Handle<String> function_source = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10824 | isolate->factory()->NewStringFromAscii( |
| 10825 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10826 | |
| 10827 | // Currently, the eval code will be executed in non-strict mode, |
| 10828 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10829 | Handle<SharedFunctionInfo> shared = |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 10830 | Compiler::CompileEval(function_source, |
| 10831 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10832 | context->IsGlobalContext(), |
| 10833 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10834 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10835 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10836 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10837 | |
| 10838 | // Invoke the result of the compilation to get the evaluation function. |
| 10839 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10840 | Handle<Object> receiver(frame->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10841 | Handle<Object> evaluation_function = |
| 10842 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 10843 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10844 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10845 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10846 | Handle<Object> arguments = GetArgumentsObject(isolate, frame, |
| 10847 | function, scope_info, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10848 | &sinfo, function_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10849 | |
| 10850 | // Invoke the evaluation function and return the result. |
| 10851 | const int argc = 2; |
| 10852 | Object** argv[argc] = { arguments.location(), |
| 10853 | Handle<Object>::cast(source).location() }; |
| 10854 | Handle<Object> result = |
| 10855 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), receiver, |
| 10856 | argc, argv, &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10857 | if (has_pending_exception) return Failure::Exception(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10858 | |
| 10859 | // Skip the global proxy as it has no properties and always delegates to the |
| 10860 | // real global object. |
| 10861 | if (result->IsJSGlobalProxy()) { |
| 10862 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 10863 | } |
| 10864 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10865 | return *result; |
| 10866 | } |
| 10867 | |
| 10868 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10869 | static MaybeObject* Runtime_DebugEvaluateGlobal(RUNTIME_CALLING_CONVENTION) { |
| 10870 | RUNTIME_GET_ISOLATE; |
| 10871 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10872 | |
| 10873 | // Check the execution state and decode arguments frame and source to be |
| 10874 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10875 | ASSERT(args.length() == 4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10876 | Object* check_result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10877 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState(args, |
| 10878 | isolate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10879 | if (!maybe_check_result->ToObject(&check_result)) { |
| 10880 | return maybe_check_result; |
| 10881 | } |
| 10882 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10883 | CONVERT_ARG_CHECKED(String, source, 1); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10884 | CONVERT_BOOLEAN_CHECKED(disable_break, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10885 | Handle<Object> additional_context(args[3]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10886 | |
| 10887 | // Handle the processing of break. |
| 10888 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10889 | |
| 10890 | // Enter the top context from before the debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10891 | SaveContext save(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10892 | SaveContext* top = &save; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10893 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10894 | top = top->prev(); |
| 10895 | } |
| 10896 | if (top != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10897 | isolate->set_context(*top->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10898 | } |
| 10899 | |
| 10900 | // Get the global context now set to the top context from before the |
| 10901 | // debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10902 | Handle<Context> context = isolate->global_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10903 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10904 | bool is_global = true; |
| 10905 | |
| 10906 | if (additional_context->IsJSObject()) { |
| 10907 | // Create a function context first, than put 'with' context on top of it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10908 | Handle<JSFunction> go_between = isolate->factory()->NewFunction( |
| 10909 | isolate->factory()->empty_string(), |
| 10910 | isolate->factory()->undefined_value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10911 | go_between->set_context(*context); |
| 10912 | context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10913 | isolate->factory()->NewFunctionContext( |
| 10914 | Context::MIN_CONTEXT_SLOTS, go_between); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10915 | context->set_extension(JSObject::cast(*additional_context)); |
| 10916 | is_global = false; |
| 10917 | } |
| 10918 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10919 | // Compile the source to be evaluated. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10920 | // Currently, the eval code will be executed in non-strict mode, |
| 10921 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10922 | Handle<SharedFunctionInfo> shared = |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10923 | Compiler::CompileEval(source, context, is_global, kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10924 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10925 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10926 | Handle<JSFunction>( |
| 10927 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 10928 | context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10929 | |
| 10930 | // Invoke the result of the compilation to get the evaluation function. |
| 10931 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10932 | Handle<Object> receiver = isolate->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10933 | Handle<Object> result = |
| 10934 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 10935 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10936 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10937 | return *result; |
| 10938 | } |
| 10939 | |
| 10940 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10941 | static MaybeObject* Runtime_DebugGetLoadedScripts(RUNTIME_CALLING_CONVENTION) { |
| 10942 | RUNTIME_GET_ISOLATE; |
| 10943 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10944 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10945 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10946 | // Fill the script objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10947 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10948 | |
| 10949 | // Convert the script objects to proper JS objects. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 10950 | for (int i = 0; i < instances->length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10951 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 10952 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 10953 | // because using |
| 10954 | // instances->set(i, *GetScriptWrapper(script)) |
| 10955 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
| 10956 | // already have deferenced the instances handle. |
| 10957 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 10958 | instances->set(i, *wrapper); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10959 | } |
| 10960 | |
| 10961 | // Return result as a JS array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 10962 | Handle<JSObject> result = |
| 10963 | isolate->factory()->NewJSObject(isolate->array_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10964 | Handle<JSArray>::cast(result)->SetContent(*instances); |
| 10965 | return *result; |
| 10966 | } |
| 10967 | |
| 10968 | |
| 10969 | // Helper function used by Runtime_DebugReferencedBy below. |
| 10970 | static int DebugReferencedBy(JSObject* target, |
| 10971 | Object* instance_filter, int max_references, |
| 10972 | FixedArray* instances, int instances_size, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10973 | JSFunction* arguments_function) { |
| 10974 | NoHandleAllocation ha; |
| 10975 | AssertNoAllocation no_alloc; |
| 10976 | |
| 10977 | // Iterate the heap. |
| 10978 | int count = 0; |
| 10979 | JSObject* last = NULL; |
| 10980 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10981 | HeapObject* heap_obj = NULL; |
| 10982 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10983 | (max_references == 0 || count < max_references)) { |
| 10984 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10985 | if (heap_obj->IsJSObject()) { |
| 10986 | // Skip context extension objects and argument arrays as these are |
| 10987 | // checked in the context of functions using them. |
| 10988 | JSObject* obj = JSObject::cast(heap_obj); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 10989 | if (obj->IsJSContextExtensionObject() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10990 | obj->map()->constructor() == arguments_function) { |
| 10991 | continue; |
| 10992 | } |
| 10993 | |
| 10994 | // Check if the JS object has a reference to the object looked for. |
| 10995 | if (obj->ReferencesObject(target)) { |
| 10996 | // Check instance filter if supplied. This is normally used to avoid |
| 10997 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 10998 | if (!instance_filter->IsUndefined()) { |
| 10999 | Object* V = obj; |
| 11000 | while (true) { |
| 11001 | Object* prototype = V->GetPrototype(); |
| 11002 | if (prototype->IsNull()) { |
| 11003 | break; |
| 11004 | } |
| 11005 | if (instance_filter == prototype) { |
| 11006 | obj = NULL; // Don't add this object. |
| 11007 | break; |
| 11008 | } |
| 11009 | V = prototype; |
| 11010 | } |
| 11011 | } |
| 11012 | |
| 11013 | if (obj != NULL) { |
| 11014 | // Valid reference found add to instance array if supplied an update |
| 11015 | // count. |
| 11016 | if (instances != NULL && count < instances_size) { |
| 11017 | instances->set(count, obj); |
| 11018 | } |
| 11019 | last = obj; |
| 11020 | count++; |
| 11021 | } |
| 11022 | } |
| 11023 | } |
| 11024 | } |
| 11025 | |
| 11026 | // Check for circular reference only. This can happen when the object is only |
| 11027 | // referenced from mirrors and has a circular reference in which case the |
| 11028 | // object is not really alive and would have been garbage collected if not |
| 11029 | // referenced from the mirror. |
| 11030 | if (count == 1 && last == target) { |
| 11031 | count = 0; |
| 11032 | } |
| 11033 | |
| 11034 | // Return the number of referencing objects found. |
| 11035 | return count; |
| 11036 | } |
| 11037 | |
| 11038 | |
| 11039 | // Scan the heap for objects with direct references to an object |
| 11040 | // args[0]: the object to find references to |
| 11041 | // args[1]: constructor function for instances to exclude (Mirror) |
| 11042 | // args[2]: the the maximum number of objects to return |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11043 | static MaybeObject* Runtime_DebugReferencedBy(RUNTIME_CALLING_CONVENTION) { |
| 11044 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11045 | ASSERT(args.length() == 3); |
| 11046 | |
| 11047 | // First perform a full GC in order to avoid references from dead objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11048 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11049 | |
| 11050 | // Check parameters. |
| 11051 | CONVERT_CHECKED(JSObject, target, args[0]); |
| 11052 | Object* instance_filter = args[1]; |
| 11053 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 11054 | instance_filter->IsJSObject()); |
| 11055 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 11056 | RUNTIME_ASSERT(max_references >= 0); |
| 11057 | |
| 11058 | // Get the constructor function for context extension and arguments array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11059 | JSObject* arguments_boilerplate = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11060 | isolate->context()->global_context()->arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11061 | JSFunction* arguments_function = |
| 11062 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 11063 | |
| 11064 | // Get the number of referencing objects. |
| 11065 | int count; |
| 11066 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11067 | NULL, 0, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11068 | |
| 11069 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11070 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11071 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11072 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 11073 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11074 | FixedArray* instances = FixedArray::cast(object); |
| 11075 | |
| 11076 | // Fill the referencing objects. |
| 11077 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11078 | instances, count, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11079 | |
| 11080 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11081 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11082 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 11083 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11084 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11085 | } |
| 11086 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11087 | return result; |
| 11088 | } |
| 11089 | |
| 11090 | |
| 11091 | // Helper function used by Runtime_DebugConstructedBy below. |
| 11092 | static int DebugConstructedBy(JSFunction* constructor, int max_references, |
| 11093 | FixedArray* instances, int instances_size) { |
| 11094 | AssertNoAllocation no_alloc; |
| 11095 | |
| 11096 | // Iterate the heap. |
| 11097 | int count = 0; |
| 11098 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11099 | HeapObject* heap_obj = NULL; |
| 11100 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11101 | (max_references == 0 || count < max_references)) { |
| 11102 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11103 | if (heap_obj->IsJSObject()) { |
| 11104 | JSObject* obj = JSObject::cast(heap_obj); |
| 11105 | if (obj->map()->constructor() == constructor) { |
| 11106 | // Valid reference found add to instance array if supplied an update |
| 11107 | // count. |
| 11108 | if (instances != NULL && count < instances_size) { |
| 11109 | instances->set(count, obj); |
| 11110 | } |
| 11111 | count++; |
| 11112 | } |
| 11113 | } |
| 11114 | } |
| 11115 | |
| 11116 | // Return the number of referencing objects found. |
| 11117 | return count; |
| 11118 | } |
| 11119 | |
| 11120 | |
| 11121 | // Scan the heap for objects constructed by a specific function. |
| 11122 | // args[0]: the constructor to find instances of |
| 11123 | // args[1]: the the maximum number of objects to return |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11124 | static MaybeObject* Runtime_DebugConstructedBy(RUNTIME_CALLING_CONVENTION) { |
| 11125 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11126 | ASSERT(args.length() == 2); |
| 11127 | |
| 11128 | // First perform a full GC in order to avoid dead objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11129 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11130 | |
| 11131 | // Check parameters. |
| 11132 | CONVERT_CHECKED(JSFunction, constructor, args[0]); |
| 11133 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 11134 | RUNTIME_ASSERT(max_references >= 0); |
| 11135 | |
| 11136 | // Get the number of referencing objects. |
| 11137 | int count; |
| 11138 | count = DebugConstructedBy(constructor, max_references, NULL, 0); |
| 11139 | |
| 11140 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11141 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11142 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11143 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 11144 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11145 | FixedArray* instances = FixedArray::cast(object); |
| 11146 | |
| 11147 | // Fill the referencing objects. |
| 11148 | count = DebugConstructedBy(constructor, max_references, instances, count); |
| 11149 | |
| 11150 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11151 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11152 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 11153 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11154 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11155 | } |
| 11156 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11157 | return result; |
| 11158 | } |
| 11159 | |
| 11160 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 11161 | // Find the effective prototype object as returned by __proto__. |
| 11162 | // args[0]: the object to find the prototype for. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11163 | static MaybeObject* Runtime_DebugGetPrototype(RUNTIME_CALLING_CONVENTION) { |
| 11164 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11165 | ASSERT(args.length() == 1); |
| 11166 | |
| 11167 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 11168 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 11169 | // Use the __proto__ accessor. |
| 11170 | return Accessors::ObjectPrototype.getter(obj, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11171 | } |
| 11172 | |
| 11173 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11174 | static MaybeObject* Runtime_SystemBreak(RUNTIME_CALLING_CONVENTION) { |
| 11175 | RUNTIME_GET_ISOLATE; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11176 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11177 | CPU::DebugBreak(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11178 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11179 | } |
| 11180 | |
| 11181 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11182 | static MaybeObject* Runtime_DebugDisassembleFunction( |
| 11183 | RUNTIME_CALLING_CONVENTION) { |
| 11184 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11185 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11186 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11187 | ASSERT(args.length() == 1); |
| 11188 | // Get the function and make sure it is compiled. |
| 11189 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11190 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 11191 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11192 | return Failure::Exception(); |
| 11193 | } |
| 11194 | func->code()->PrintLn(); |
| 11195 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11196 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11197 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11198 | |
| 11199 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11200 | static MaybeObject* Runtime_DebugDisassembleConstructor( |
| 11201 | RUNTIME_CALLING_CONVENTION) { |
| 11202 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11203 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11204 | HandleScope scope(isolate); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11205 | ASSERT(args.length() == 1); |
| 11206 | // Get the function and make sure it is compiled. |
| 11207 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11208 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 11209 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11210 | return Failure::Exception(); |
| 11211 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11212 | shared->construct_stub()->PrintLn(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11213 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11214 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11215 | } |
| 11216 | |
| 11217 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11218 | static MaybeObject* Runtime_FunctionGetInferredName( |
| 11219 | RUNTIME_CALLING_CONVENTION) { |
| 11220 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11221 | NoHandleAllocation ha; |
| 11222 | ASSERT(args.length() == 1); |
| 11223 | |
| 11224 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 11225 | return f->shared()->inferred_name(); |
| 11226 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11227 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11228 | |
| 11229 | static int FindSharedFunctionInfosForScript(Script* script, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11230 | FixedArray* buffer) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11231 | AssertNoAllocation no_allocations; |
| 11232 | |
| 11233 | int counter = 0; |
| 11234 | int buffer_size = buffer->length(); |
| 11235 | HeapIterator iterator; |
| 11236 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 11237 | ASSERT(obj != NULL); |
| 11238 | if (!obj->IsSharedFunctionInfo()) { |
| 11239 | continue; |
| 11240 | } |
| 11241 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 11242 | if (shared->script() != script) { |
| 11243 | continue; |
| 11244 | } |
| 11245 | if (counter < buffer_size) { |
| 11246 | buffer->set(counter, shared); |
| 11247 | } |
| 11248 | counter++; |
| 11249 | } |
| 11250 | return counter; |
| 11251 | } |
| 11252 | |
| 11253 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 11254 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 11255 | // in OpaqueReferences. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11256 | static MaybeObject* Runtime_LiveEditFindSharedFunctionInfosForScript( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11257 | RUNTIME_CALLING_CONVENTION) { |
| 11258 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11259 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11260 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11261 | CONVERT_CHECKED(JSValue, script_value, args[0]); |
| 11262 | |
| 11263 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 11264 | |
| 11265 | const int kBufferSize = 32; |
| 11266 | |
| 11267 | Handle<FixedArray> array; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11268 | array = isolate->factory()->NewFixedArray(kBufferSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11269 | int number = FindSharedFunctionInfosForScript(*script, *array); |
| 11270 | if (number > kBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11271 | array = isolate->factory()->NewFixedArray(number); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11272 | FindSharedFunctionInfosForScript(*script, *array); |
| 11273 | } |
| 11274 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11275 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11276 | result->set_length(Smi::FromInt(number)); |
| 11277 | |
| 11278 | LiveEdit::WrapSharedFunctionInfos(result); |
| 11279 | |
| 11280 | return *result; |
| 11281 | } |
| 11282 | |
| 11283 | // For a script calculates compilation information about all its functions. |
| 11284 | // The script source is explicitly specified by the second argument. |
| 11285 | // The source of the actual script is not used, however it is important that |
| 11286 | // all generated code keeps references to this particular instance of script. |
| 11287 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 11288 | // each function with all its descendant is always stored in a continues range |
| 11289 | // with the function itself going first. The root function is a script function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11290 | static MaybeObject* Runtime_LiveEditGatherCompileInfo( |
| 11291 | RUNTIME_CALLING_CONVENTION) { |
| 11292 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11293 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11294 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11295 | CONVERT_CHECKED(JSValue, script, args[0]); |
| 11296 | CONVERT_ARG_CHECKED(String, source, 1); |
| 11297 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 11298 | |
| 11299 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 11300 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11301 | if (isolate->has_pending_exception()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11302 | return Failure::Exception(); |
| 11303 | } |
| 11304 | |
| 11305 | return result; |
| 11306 | } |
| 11307 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11308 | // Changes the source of the script to a new_source. |
| 11309 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 11310 | // the script with its original source and sends notification to debugger. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11311 | static MaybeObject* Runtime_LiveEditReplaceScript(RUNTIME_CALLING_CONVENTION) { |
| 11312 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11313 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11314 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11315 | CONVERT_CHECKED(JSValue, original_script_value, args[0]); |
| 11316 | CONVERT_ARG_CHECKED(String, new_source, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11317 | Handle<Object> old_script_name(args[2], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11318 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11319 | CONVERT_CHECKED(Script, original_script_pointer, |
| 11320 | original_script_value->value()); |
| 11321 | Handle<Script> original_script(original_script_pointer); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11322 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11323 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 11324 | new_source, |
| 11325 | old_script_name); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11326 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11327 | if (old_script->IsScript()) { |
| 11328 | Handle<Script> script_handle(Script::cast(old_script)); |
| 11329 | return *(GetScriptWrapper(script_handle)); |
| 11330 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11331 | return isolate->heap()->null_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11332 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11333 | } |
| 11334 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11335 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11336 | static MaybeObject* Runtime_LiveEditFunctionSourceUpdated( |
| 11337 | RUNTIME_CALLING_CONVENTION) { |
| 11338 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11339 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11340 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11341 | CONVERT_ARG_CHECKED(JSArray, shared_info, 0); |
| 11342 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 11343 | } |
| 11344 | |
| 11345 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11346 | // Replaces code of SharedFunctionInfo with a new one. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11347 | static MaybeObject* Runtime_LiveEditReplaceFunctionCode( |
| 11348 | RUNTIME_CALLING_CONVENTION) { |
| 11349 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11350 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11351 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11352 | CONVERT_ARG_CHECKED(JSArray, new_compile_info, 0); |
| 11353 | CONVERT_ARG_CHECKED(JSArray, shared_info, 1); |
| 11354 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11355 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11356 | } |
| 11357 | |
| 11358 | // Connects SharedFunctionInfo to another script. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11359 | static MaybeObject* Runtime_LiveEditFunctionSetScript( |
| 11360 | RUNTIME_CALLING_CONVENTION) { |
| 11361 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11362 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11363 | HandleScope scope(isolate); |
| 11364 | Handle<Object> function_object(args[0], isolate); |
| 11365 | Handle<Object> script_object(args[1], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11366 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11367 | if (function_object->IsJSValue()) { |
| 11368 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 11369 | if (script_object->IsJSValue()) { |
| 11370 | CONVERT_CHECKED(Script, script, JSValue::cast(*script_object)->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11371 | script_object = Handle<Object>(script, isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11372 | } |
| 11373 | |
| 11374 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 11375 | } else { |
| 11376 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 11377 | // and we check it in this function. |
| 11378 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11379 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11380 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11381 | } |
| 11382 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11383 | |
| 11384 | // In a code of a parent function replaces original function as embedded object |
| 11385 | // with a substitution one. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11386 | static MaybeObject* Runtime_LiveEditReplaceRefToNestedFunction( |
| 11387 | RUNTIME_CALLING_CONVENTION) { |
| 11388 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11389 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11390 | HandleScope scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11391 | |
| 11392 | CONVERT_ARG_CHECKED(JSValue, parent_wrapper, 0); |
| 11393 | CONVERT_ARG_CHECKED(JSValue, orig_wrapper, 1); |
| 11394 | CONVERT_ARG_CHECKED(JSValue, subst_wrapper, 2); |
| 11395 | |
| 11396 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 11397 | subst_wrapper); |
| 11398 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11399 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11400 | } |
| 11401 | |
| 11402 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11403 | // Updates positions of a shared function info (first parameter) according |
| 11404 | // to script source change. Text change is described in second parameter as |
| 11405 | // array of groups of 3 numbers: |
| 11406 | // (change_begin, change_end, change_end_new_position). |
| 11407 | // Each group describes a change in text; groups are sorted by change_begin. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11408 | static MaybeObject* Runtime_LiveEditPatchFunctionPositions( |
| 11409 | RUNTIME_CALLING_CONVENTION) { |
| 11410 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11411 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11412 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11413 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
| 11414 | CONVERT_ARG_CHECKED(JSArray, position_change_array, 1); |
| 11415 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11416 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11417 | } |
| 11418 | |
| 11419 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11420 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 11421 | // checks that none of them have activations on stacks (of any thread). |
| 11422 | // Returns array of the same length with corresponding results of |
| 11423 | // LiveEdit::FunctionPatchabilityStatus type. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11424 | static MaybeObject* Runtime_LiveEditCheckAndDropActivations( |
| 11425 | RUNTIME_CALLING_CONVENTION) { |
| 11426 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11427 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11428 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11429 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11430 | CONVERT_BOOLEAN_CHECKED(do_drop, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11431 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11432 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11433 | } |
| 11434 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11435 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 11436 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 11437 | // of diff chunks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11438 | static MaybeObject* Runtime_LiveEditCompareStrings(RUNTIME_CALLING_CONVENTION) { |
| 11439 | RUNTIME_GET_ISOLATE; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11440 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11441 | HandleScope scope(isolate); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11442 | CONVERT_ARG_CHECKED(String, s1, 0); |
| 11443 | CONVERT_ARG_CHECKED(String, s2, 1); |
| 11444 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11445 | return *LiveEdit::CompareStrings(s1, s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11446 | } |
| 11447 | |
| 11448 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11449 | // A testing entry. Returns statement position which is the closest to |
| 11450 | // source_position. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11451 | static MaybeObject* Runtime_GetFunctionCodePositionFromSource( |
| 11452 | RUNTIME_CALLING_CONVENTION) { |
| 11453 | RUNTIME_GET_ISOLATE; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11454 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11455 | HandleScope scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11456 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 11457 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11458 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11459 | Handle<Code> code(function->code(), isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11460 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11461 | if (code->kind() != Code::FUNCTION && |
| 11462 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11463 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11464 | } |
| 11465 | |
| 11466 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11467 | int closest_pc = 0; |
| 11468 | int distance = kMaxInt; |
| 11469 | while (!it.done()) { |
| 11470 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 11471 | // Check if this break point is closer that what was previously found. |
| 11472 | if (source_position <= statement_position && |
| 11473 | statement_position - source_position < distance) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 11474 | closest_pc = |
| 11475 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11476 | distance = statement_position - source_position; |
| 11477 | // Check whether we can't get any closer. |
| 11478 | if (distance == 0) break; |
| 11479 | } |
| 11480 | it.next(); |
| 11481 | } |
| 11482 | |
| 11483 | return Smi::FromInt(closest_pc); |
| 11484 | } |
| 11485 | |
| 11486 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11487 | // Calls specified function with or without entering the debugger. |
| 11488 | // This is used in unit tests to run code as if debugger is entered or simply |
| 11489 | // to have a stack with C++ frame in the middle. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11490 | static MaybeObject* Runtime_ExecuteInDebugContext(RUNTIME_CALLING_CONVENTION) { |
| 11491 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11492 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11493 | HandleScope scope(isolate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11494 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 11495 | CONVERT_BOOLEAN_CHECKED(without_debugger, args[1]); |
| 11496 | |
| 11497 | Handle<Object> result; |
| 11498 | bool pending_exception; |
| 11499 | { |
| 11500 | if (without_debugger) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11501 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11502 | &pending_exception); |
| 11503 | } else { |
| 11504 | EnterDebugger enter_debugger; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11505 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11506 | &pending_exception); |
| 11507 | } |
| 11508 | } |
| 11509 | if (!pending_exception) { |
| 11510 | return *result; |
| 11511 | } else { |
| 11512 | return Failure::Exception(); |
| 11513 | } |
| 11514 | } |
| 11515 | |
| 11516 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11517 | // Sets a v8 flag. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11518 | static MaybeObject* Runtime_SetFlags(RUNTIME_CALLING_CONVENTION) { |
| 11519 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11520 | CONVERT_CHECKED(String, arg, args[0]); |
| 11521 | SmartPointer<char> flags = |
| 11522 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 11523 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11524 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11525 | } |
| 11526 | |
| 11527 | |
| 11528 | // Performs a GC. |
| 11529 | // Presently, it only does a full GC. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11530 | static MaybeObject* Runtime_CollectGarbage(RUNTIME_CALLING_CONVENTION) { |
| 11531 | RUNTIME_GET_ISOLATE; |
| 11532 | isolate->heap()->CollectAllGarbage(true); |
| 11533 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11534 | } |
| 11535 | |
| 11536 | |
| 11537 | // Gets the current heap usage. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11538 | static MaybeObject* Runtime_GetHeapUsage(RUNTIME_CALLING_CONVENTION) { |
| 11539 | RUNTIME_GET_ISOLATE; |
| 11540 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11541 | if (!Smi::IsValid(usage)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11542 | return *isolate->factory()->NewNumberFromInt(usage); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11543 | } |
| 11544 | return Smi::FromInt(usage); |
| 11545 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11546 | |
| 11547 | |
| 11548 | // Captures a live object list from the present heap. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11549 | static MaybeObject* Runtime_HasLOLEnabled(RUNTIME_CALLING_CONVENTION) { |
| 11550 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11551 | #ifdef LIVE_OBJECT_LIST |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11552 | return isolate->heap()->true_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11553 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11554 | return isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11555 | #endif |
| 11556 | } |
| 11557 | |
| 11558 | |
| 11559 | // Captures a live object list from the present heap. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11560 | static MaybeObject* Runtime_CaptureLOL(RUNTIME_CALLING_CONVENTION) { |
| 11561 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11562 | #ifdef LIVE_OBJECT_LIST |
| 11563 | return LiveObjectList::Capture(); |
| 11564 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11565 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11566 | #endif |
| 11567 | } |
| 11568 | |
| 11569 | |
| 11570 | // Deletes the specified live object list. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11571 | static MaybeObject* Runtime_DeleteLOL(RUNTIME_CALLING_CONVENTION) { |
| 11572 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11573 | #ifdef LIVE_OBJECT_LIST |
| 11574 | CONVERT_SMI_CHECKED(id, args[0]); |
| 11575 | bool success = LiveObjectList::Delete(id); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11576 | return success ? isolate->heap()->true_value() : |
| 11577 | isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11578 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11579 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11580 | #endif |
| 11581 | } |
| 11582 | |
| 11583 | |
| 11584 | // Generates the response to a debugger request for a dump of the objects |
| 11585 | // contained in the difference between the captured live object lists |
| 11586 | // specified by id1 and id2. |
| 11587 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 11588 | // dumped. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11589 | static MaybeObject* Runtime_DumpLOL(RUNTIME_CALLING_CONVENTION) { |
| 11590 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11591 | #ifdef LIVE_OBJECT_LIST |
| 11592 | HandleScope scope; |
| 11593 | CONVERT_SMI_CHECKED(id1, args[0]); |
| 11594 | CONVERT_SMI_CHECKED(id2, args[1]); |
| 11595 | CONVERT_SMI_CHECKED(start, args[2]); |
| 11596 | CONVERT_SMI_CHECKED(count, args[3]); |
| 11597 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 4); |
| 11598 | EnterDebugger enter_debugger; |
| 11599 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 11600 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11601 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11602 | #endif |
| 11603 | } |
| 11604 | |
| 11605 | |
| 11606 | // Gets the specified object as requested by the debugger. |
| 11607 | // This is only used for obj ids shown in live object lists. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11608 | static MaybeObject* Runtime_GetLOLObj(RUNTIME_CALLING_CONVENTION) { |
| 11609 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11610 | #ifdef LIVE_OBJECT_LIST |
| 11611 | CONVERT_SMI_CHECKED(obj_id, args[0]); |
| 11612 | Object* result = LiveObjectList::GetObj(obj_id); |
| 11613 | return result; |
| 11614 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11615 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11616 | #endif |
| 11617 | } |
| 11618 | |
| 11619 | |
| 11620 | // Gets the obj id for the specified address if valid. |
| 11621 | // This is only used for obj ids shown in live object lists. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11622 | static MaybeObject* Runtime_GetLOLObjId(RUNTIME_CALLING_CONVENTION) { |
| 11623 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11624 | #ifdef LIVE_OBJECT_LIST |
| 11625 | HandleScope scope; |
| 11626 | CONVERT_ARG_CHECKED(String, address, 0); |
| 11627 | Object* result = LiveObjectList::GetObjId(address); |
| 11628 | return result; |
| 11629 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11630 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11631 | #endif |
| 11632 | } |
| 11633 | |
| 11634 | |
| 11635 | // Gets the retainers that references the specified object alive. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11636 | static MaybeObject* Runtime_GetLOLObjRetainers(RUNTIME_CALLING_CONVENTION) { |
| 11637 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11638 | #ifdef LIVE_OBJECT_LIST |
| 11639 | HandleScope scope; |
| 11640 | CONVERT_SMI_CHECKED(obj_id, args[0]); |
| 11641 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 11642 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 11643 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 11644 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
| 11645 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 5); |
| 11646 | |
| 11647 | Handle<JSObject> instance_filter; |
| 11648 | if (args[1]->IsJSObject()) { |
| 11649 | instance_filter = args.at<JSObject>(1); |
| 11650 | } |
| 11651 | bool verbose = false; |
| 11652 | if (args[2]->IsBoolean()) { |
| 11653 | verbose = args[2]->IsTrue(); |
| 11654 | } |
| 11655 | int start = 0; |
| 11656 | if (args[3]->IsSmi()) { |
| 11657 | start = Smi::cast(args[3])->value(); |
| 11658 | } |
| 11659 | int limit = Smi::kMaxValue; |
| 11660 | if (args[4]->IsSmi()) { |
| 11661 | limit = Smi::cast(args[4])->value(); |
| 11662 | } |
| 11663 | |
| 11664 | return LiveObjectList::GetObjRetainers(obj_id, |
| 11665 | instance_filter, |
| 11666 | verbose, |
| 11667 | start, |
| 11668 | limit, |
| 11669 | filter_obj); |
| 11670 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11671 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11672 | #endif |
| 11673 | } |
| 11674 | |
| 11675 | |
| 11676 | // Gets the reference path between 2 objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11677 | static MaybeObject* Runtime_GetLOLPath(RUNTIME_CALLING_CONVENTION) { |
| 11678 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11679 | #ifdef LIVE_OBJECT_LIST |
| 11680 | HandleScope scope; |
| 11681 | CONVERT_SMI_CHECKED(obj_id1, args[0]); |
| 11682 | CONVERT_SMI_CHECKED(obj_id2, args[1]); |
| 11683 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 11684 | |
| 11685 | Handle<JSObject> instance_filter; |
| 11686 | if (args[2]->IsJSObject()) { |
| 11687 | instance_filter = args.at<JSObject>(2); |
| 11688 | } |
| 11689 | |
| 11690 | Object* result = |
| 11691 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 11692 | return result; |
| 11693 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11694 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11695 | #endif |
| 11696 | } |
| 11697 | |
| 11698 | |
| 11699 | // Generates the response to a debugger request for a list of all |
| 11700 | // previously captured live object lists. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11701 | static MaybeObject* Runtime_InfoLOL(RUNTIME_CALLING_CONVENTION) { |
| 11702 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11703 | #ifdef LIVE_OBJECT_LIST |
| 11704 | CONVERT_SMI_CHECKED(start, args[0]); |
| 11705 | CONVERT_SMI_CHECKED(count, args[1]); |
| 11706 | return LiveObjectList::Info(start, count); |
| 11707 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11708 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11709 | #endif |
| 11710 | } |
| 11711 | |
| 11712 | |
| 11713 | // Gets a dump of the specified object as requested by the debugger. |
| 11714 | // This is only used for obj ids shown in live object lists. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11715 | static MaybeObject* Runtime_PrintLOLObj(RUNTIME_CALLING_CONVENTION) { |
| 11716 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11717 | #ifdef LIVE_OBJECT_LIST |
| 11718 | HandleScope scope; |
| 11719 | CONVERT_SMI_CHECKED(obj_id, args[0]); |
| 11720 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 11721 | return result; |
| 11722 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11723 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11724 | #endif |
| 11725 | } |
| 11726 | |
| 11727 | |
| 11728 | // Resets and releases all previously captured live object lists. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11729 | static MaybeObject* Runtime_ResetLOL(RUNTIME_CALLING_CONVENTION) { |
| 11730 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11731 | #ifdef LIVE_OBJECT_LIST |
| 11732 | LiveObjectList::Reset(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11733 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11734 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11735 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11736 | #endif |
| 11737 | } |
| 11738 | |
| 11739 | |
| 11740 | // Generates the response to a debugger request for a summary of the types |
| 11741 | // of objects in the difference between the captured live object lists |
| 11742 | // specified by id1 and id2. |
| 11743 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 11744 | // summarized. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11745 | static MaybeObject* Runtime_SummarizeLOL(RUNTIME_CALLING_CONVENTION) { |
| 11746 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11747 | #ifdef LIVE_OBJECT_LIST |
| 11748 | HandleScope scope; |
| 11749 | CONVERT_SMI_CHECKED(id1, args[0]); |
| 11750 | CONVERT_SMI_CHECKED(id2, args[1]); |
| 11751 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 2); |
| 11752 | |
| 11753 | EnterDebugger enter_debugger; |
| 11754 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 11755 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11756 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11757 | #endif |
| 11758 | } |
| 11759 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11760 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 11761 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11762 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11763 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11764 | static MaybeObject* Runtime_ProfilerResume(RUNTIME_CALLING_CONVENTION) { |
| 11765 | RUNTIME_GET_ISOLATE; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11766 | NoHandleAllocation ha; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11767 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11768 | |
| 11769 | CONVERT_CHECKED(Smi, smi_modules, args[0]); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11770 | CONVERT_CHECKED(Smi, smi_tag, args[1]); |
| 11771 | v8::V8::ResumeProfilerEx(smi_modules->value(), smi_tag->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11772 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11773 | } |
| 11774 | |
| 11775 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11776 | static MaybeObject* Runtime_ProfilerPause(RUNTIME_CALLING_CONVENTION) { |
| 11777 | RUNTIME_GET_ISOLATE; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11778 | NoHandleAllocation ha; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11779 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11780 | |
| 11781 | CONVERT_CHECKED(Smi, smi_modules, args[0]); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11782 | CONVERT_CHECKED(Smi, smi_tag, args[1]); |
| 11783 | v8::V8::PauseProfilerEx(smi_modules->value(), smi_tag->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11784 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11785 | } |
| 11786 | |
| 11787 | #endif // ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11788 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11789 | // Finds the script object from the script data. NOTE: This operation uses |
| 11790 | // heap traversal to find the function generated for the source position |
| 11791 | // for the requested break point. For lazily compiled functions several heap |
| 11792 | // traversals might be required rendering this operation as a rather slow |
| 11793 | // operation. However for setting break points which is normally done through |
| 11794 | // some kind of user interaction the performance is not crucial. |
| 11795 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 11796 | Handle<String> script_name) { |
| 11797 | // Scan the heap for Script objects to find the script with the requested |
| 11798 | // script data. |
| 11799 | Handle<Script> script; |
| 11800 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11801 | HeapObject* obj = NULL; |
| 11802 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11803 | // If a script is found check if it has the script data requested. |
| 11804 | if (obj->IsScript()) { |
| 11805 | if (Script::cast(obj)->name()->IsString()) { |
| 11806 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 11807 | script = Handle<Script>(Script::cast(obj)); |
| 11808 | } |
| 11809 | } |
| 11810 | } |
| 11811 | } |
| 11812 | |
| 11813 | // If no script with the requested script data is found return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11814 | if (script.is_null()) return FACTORY->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11815 | |
| 11816 | // Return the script found. |
| 11817 | return GetScriptWrapper(script); |
| 11818 | } |
| 11819 | |
| 11820 | |
| 11821 | // Get the script object from script data. NOTE: Regarding performance |
| 11822 | // see the NOTE for GetScriptFromScriptData. |
| 11823 | // args[0]: script data for the script to find the source for |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11824 | static MaybeObject* Runtime_GetScript(RUNTIME_CALLING_CONVENTION) { |
| 11825 | RUNTIME_GET_ISOLATE; |
| 11826 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11827 | |
| 11828 | ASSERT(args.length() == 1); |
| 11829 | |
| 11830 | CONVERT_CHECKED(String, script_name, args[0]); |
| 11831 | |
| 11832 | // Find the requested script. |
| 11833 | Handle<Object> result = |
| 11834 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 11835 | return *result; |
| 11836 | } |
| 11837 | |
| 11838 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11839 | // Determines whether the given stack frame should be displayed in |
| 11840 | // a stack trace. The caller is the error constructor that asked |
| 11841 | // for the stack trace to be collected. The first time a construct |
| 11842 | // call to this function is encountered it is skipped. The seen_caller |
| 11843 | // in/out parameter is used to remember if the caller has been seen |
| 11844 | // yet. |
| 11845 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, Object* caller, |
| 11846 | bool* seen_caller) { |
| 11847 | // Only display JS frames. |
| 11848 | if (!raw_frame->is_java_script()) |
| 11849 | return false; |
| 11850 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 11851 | Object* raw_fun = frame->function(); |
| 11852 | // Not sure when this can happen but skip it just in case. |
| 11853 | if (!raw_fun->IsJSFunction()) |
| 11854 | return false; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11855 | if ((raw_fun == caller) && !(*seen_caller)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11856 | *seen_caller = true; |
| 11857 | return false; |
| 11858 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11859 | // Skip all frames until we've seen the caller. Also, skip the most |
| 11860 | // obvious builtin calls. Some builtin calls (such as Number.ADD |
| 11861 | // which is invoked using 'call') are very difficult to recognize |
| 11862 | // so we're leaving them in for now. |
| 11863 | return *seen_caller && !frame->receiver()->IsJSBuiltinsObject(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11864 | } |
| 11865 | |
| 11866 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11867 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 11868 | // element segments each containing a receiver, function, code and |
| 11869 | // native code offset. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11870 | static MaybeObject* Runtime_CollectStackTrace(RUNTIME_CALLING_CONVENTION) { |
| 11871 | RUNTIME_GET_ISOLATE; |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11872 | ASSERT_EQ(args.length(), 2); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11873 | Handle<Object> caller = args.at<Object>(0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11874 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[1]); |
| 11875 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11876 | HandleScope scope(isolate); |
| 11877 | Factory* factory = isolate->factory(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11878 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 11879 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 11880 | int initial_size = Min(limit, 10); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11881 | Handle<FixedArray> elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11882 | factory->NewFixedArrayWithHoles(initial_size * 4); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11883 | |
| 11884 | StackFrameIterator iter; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11885 | // If the caller parameter is a function we skip frames until we're |
| 11886 | // under it before starting to collect. |
| 11887 | bool seen_caller = !caller->IsJSFunction(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11888 | int cursor = 0; |
| 11889 | int frames_seen = 0; |
| 11890 | while (!iter.done() && frames_seen < limit) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11891 | StackFrame* raw_frame = iter.frame(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11892 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11893 | frames_seen++; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11894 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11895 | List<FrameSummary> frames(3); // Max 2 levels of inlining. |
| 11896 | frame->Summarize(&frames); |
| 11897 | for (int i = frames.length() - 1; i >= 0; i--) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11898 | if (cursor + 4 > elements->length()) { |
| 11899 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 11900 | Handle<FixedArray> new_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11901 | factory->NewFixedArrayWithHoles(new_capacity); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11902 | for (int i = 0; i < cursor; i++) { |
| 11903 | new_elements->set(i, elements->get(i)); |
| 11904 | } |
| 11905 | elements = new_elements; |
| 11906 | } |
| 11907 | ASSERT(cursor + 4 <= elements->length()); |
| 11908 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11909 | Handle<Object> recv = frames[i].receiver(); |
| 11910 | Handle<JSFunction> fun = frames[i].function(); |
| 11911 | Handle<Code> code = frames[i].code(); |
| 11912 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11913 | elements->set(cursor++, *recv); |
| 11914 | elements->set(cursor++, *fun); |
| 11915 | elements->set(cursor++, *code); |
| 11916 | elements->set(cursor++, *offset); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11917 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11918 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11919 | iter.Advance(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11920 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11921 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11922 | result->set_length(Smi::FromInt(cursor)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11923 | return *result; |
| 11924 | } |
| 11925 | |
| 11926 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11927 | // Returns V8 version as a string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11928 | static MaybeObject* Runtime_GetV8Version(RUNTIME_CALLING_CONVENTION) { |
| 11929 | RUNTIME_GET_ISOLATE; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11930 | ASSERT_EQ(args.length(), 0); |
| 11931 | |
| 11932 | NoHandleAllocation ha; |
| 11933 | |
| 11934 | const char* version_string = v8::V8::GetVersion(); |
| 11935 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11936 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 11937 | NOT_TENURED); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11938 | } |
| 11939 | |
| 11940 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11941 | static MaybeObject* Runtime_Abort(RUNTIME_CALLING_CONVENTION) { |
| 11942 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11943 | ASSERT(args.length() == 2); |
| 11944 | OS::PrintError("abort: %s\n", reinterpret_cast<char*>(args[0]) + |
| 11945 | Smi::cast(args[1])->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11946 | isolate->PrintStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11947 | OS::Abort(); |
| 11948 | UNREACHABLE(); |
| 11949 | return NULL; |
| 11950 | } |
| 11951 | |
| 11952 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11953 | static MaybeObject* Runtime_GetFromCache(RUNTIME_CALLING_CONVENTION) { |
| 11954 | RUNTIME_GET_ISOLATE; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11955 | // This is only called from codegen, so checks might be more lax. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11956 | CONVERT_CHECKED(JSFunctionResultCache, cache, args[0]); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11957 | Object* key = args[1]; |
| 11958 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11959 | int finger_index = cache->finger_index(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11960 | Object* o = cache->get(finger_index); |
| 11961 | if (o == key) { |
| 11962 | // The fastest case: hit the same place again. |
| 11963 | return cache->get(finger_index + 1); |
| 11964 | } |
| 11965 | |
| 11966 | for (int i = finger_index - 2; |
| 11967 | i >= JSFunctionResultCache::kEntriesIndex; |
| 11968 | i -= 2) { |
| 11969 | o = cache->get(i); |
| 11970 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11971 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11972 | return cache->get(i + 1); |
| 11973 | } |
| 11974 | } |
| 11975 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11976 | int size = cache->size(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11977 | ASSERT(size <= cache->length()); |
| 11978 | |
| 11979 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 11980 | o = cache->get(i); |
| 11981 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11982 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11983 | return cache->get(i + 1); |
| 11984 | } |
| 11985 | } |
| 11986 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11987 | // There is no value in the cache. Invoke the function and cache result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11988 | HandleScope scope(isolate); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11989 | |
| 11990 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 11991 | Handle<Object> key_handle(key); |
| 11992 | Handle<Object> value; |
| 11993 | { |
| 11994 | Handle<JSFunction> factory(JSFunction::cast( |
| 11995 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 11996 | // TODO(antonm): consider passing a receiver when constructing a cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 11997 | Handle<Object> receiver(isolate->global_context()->global()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11998 | // This handle is nor shared, nor used later, so it's safe. |
| 11999 | Object** argv[] = { key_handle.location() }; |
| 12000 | bool pending_exception = false; |
| 12001 | value = Execution::Call(factory, |
| 12002 | receiver, |
| 12003 | 1, |
| 12004 | argv, |
| 12005 | &pending_exception); |
| 12006 | if (pending_exception) return Failure::Exception(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12007 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12008 | |
| 12009 | #ifdef DEBUG |
| 12010 | cache_handle->JSFunctionResultCacheVerify(); |
| 12011 | #endif |
| 12012 | |
| 12013 | // Function invocation may have cleared the cache. Reread all the data. |
| 12014 | finger_index = cache_handle->finger_index(); |
| 12015 | size = cache_handle->size(); |
| 12016 | |
| 12017 | // If we have spare room, put new data into it, otherwise evict post finger |
| 12018 | // entry which is likely to be the least recently used. |
| 12019 | int index = -1; |
| 12020 | if (size < cache_handle->length()) { |
| 12021 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 12022 | index = size; |
| 12023 | } else { |
| 12024 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 12025 | if (index == cache_handle->length()) { |
| 12026 | index = JSFunctionResultCache::kEntriesIndex; |
| 12027 | } |
| 12028 | } |
| 12029 | |
| 12030 | ASSERT(index % 2 == 0); |
| 12031 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 12032 | ASSERT(index < cache_handle->length()); |
| 12033 | |
| 12034 | cache_handle->set(index, *key_handle); |
| 12035 | cache_handle->set(index + 1, *value); |
| 12036 | cache_handle->set_finger_index(index); |
| 12037 | |
| 12038 | #ifdef DEBUG |
| 12039 | cache_handle->JSFunctionResultCacheVerify(); |
| 12040 | #endif |
| 12041 | |
| 12042 | return *value; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12043 | } |
| 12044 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12045 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12046 | static MaybeObject* Runtime_NewMessageObject(RUNTIME_CALLING_CONVENTION) { |
| 12047 | RUNTIME_GET_ISOLATE; |
| 12048 | HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12049 | CONVERT_ARG_CHECKED(String, type, 0); |
| 12050 | CONVERT_ARG_CHECKED(JSArray, arguments, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12051 | return *isolate->factory()->NewJSMessageObject( |
| 12052 | type, |
| 12053 | arguments, |
| 12054 | 0, |
| 12055 | 0, |
| 12056 | isolate->factory()->undefined_value(), |
| 12057 | isolate->factory()->undefined_value(), |
| 12058 | isolate->factory()->undefined_value()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12059 | } |
| 12060 | |
| 12061 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12062 | static MaybeObject* Runtime_MessageGetType(RUNTIME_CALLING_CONVENTION) { |
| 12063 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12064 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12065 | return message->type(); |
| 12066 | } |
| 12067 | |
| 12068 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12069 | static MaybeObject* Runtime_MessageGetArguments(RUNTIME_CALLING_CONVENTION) { |
| 12070 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12071 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12072 | return message->arguments(); |
| 12073 | } |
| 12074 | |
| 12075 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12076 | static MaybeObject* Runtime_MessageGetStartPosition( |
| 12077 | RUNTIME_CALLING_CONVENTION) { |
| 12078 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12079 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12080 | return Smi::FromInt(message->start_position()); |
| 12081 | } |
| 12082 | |
| 12083 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12084 | static MaybeObject* Runtime_MessageGetScript(RUNTIME_CALLING_CONVENTION) { |
| 12085 | RUNTIME_GET_ISOLATE; |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12086 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12087 | return message->script(); |
| 12088 | } |
| 12089 | |
| 12090 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 12091 | #ifdef DEBUG |
| 12092 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 12093 | // Exclude the code in release mode. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12094 | static MaybeObject* Runtime_ListNatives(RUNTIME_CALLING_CONVENTION) { |
| 12095 | RUNTIME_GET_ISOLATE; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12096 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12097 | HandleScope scope; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12098 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 12099 | int entry_count = 0 |
| 12100 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 12101 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 12102 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 12103 | #undef COUNT_ENTRY |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12104 | Factory* factory = isolate->factory(); |
| 12105 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12106 | int index = 0; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12107 | bool inline_runtime_functions = false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 12108 | #define ADD_ENTRY(Name, argc, ressize) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12109 | { \ |
| 12110 | HandleScope inner; \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12111 | Handle<String> name; \ |
| 12112 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 12113 | if (inline_runtime_functions) { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12114 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12115 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 12116 | } else { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12117 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12118 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 12119 | } \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12120 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12121 | pair_elements->set(0, *name); \ |
| 12122 | pair_elements->set(1, Smi::FromInt(argc)); \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12123 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12124 | elements->set(index++, *pair); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12125 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12126 | inline_runtime_functions = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12127 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12128 | inline_runtime_functions = true; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12129 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12130 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12131 | #undef ADD_ENTRY |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12132 | ASSERT_EQ(index, entry_count); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12133 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12134 | return *result; |
| 12135 | } |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 12136 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12137 | |
| 12138 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12139 | static MaybeObject* Runtime_Log(RUNTIME_CALLING_CONVENTION) { |
| 12140 | RUNTIME_GET_ISOLATE; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12141 | ASSERT(args.length() == 2); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12142 | CONVERT_CHECKED(String, format, args[0]); |
| 12143 | CONVERT_CHECKED(JSArray, elms, args[1]); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12144 | Vector<const char> chars = format->ToAsciiVector(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12145 | LOGGER->LogRuntime(chars, elms); |
| 12146 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12147 | } |
| 12148 | |
| 12149 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12150 | static MaybeObject* Runtime_IS_VAR(RUNTIME_CALLING_CONVENTION) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12151 | UNREACHABLE(); // implemented as macro in the parser |
| 12152 | return NULL; |
| 12153 | } |
| 12154 | |
| 12155 | |
| 12156 | // ---------------------------------------------------------------------------- |
| 12157 | // Implementation of Runtime |
| 12158 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12159 | #define F(name, number_of_args, result_size) \ |
| 12160 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 12161 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12162 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12163 | |
| 12164 | #define I(name, number_of_args, result_size) \ |
| 12165 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 12166 | "_" #name, NULL, number_of_args, result_size }, |
| 12167 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12168 | static const Runtime::Function kIntrinsicFunctions[] = { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12169 | RUNTIME_FUNCTION_LIST(F) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12170 | INLINE_FUNCTION_LIST(I) |
| 12171 | INLINE_RUNTIME_FUNCTION_LIST(I) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12172 | }; |
| 12173 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12174 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12175 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 12176 | Object* dictionary) { |
| 12177 | ASSERT(Isolate::Current()->heap() == heap); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12178 | ASSERT(dictionary != NULL); |
| 12179 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 12180 | for (int i = 0; i < kNumFunctions; ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12181 | Object* name_symbol; |
| 12182 | { MaybeObject* maybe_name_symbol = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12183 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12184 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 12185 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12186 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12187 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 12188 | String::cast(name_symbol), |
| 12189 | Smi::FromInt(i), |
| 12190 | PropertyDetails(NONE, NORMAL)); |
| 12191 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 12192 | // Non-recoverable failure. Calling code must restart heap |
| 12193 | // initialization. |
| 12194 | return maybe_dictionary; |
| 12195 | } |
| 12196 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12197 | } |
| 12198 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12199 | } |
| 12200 | |
| 12201 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12202 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 12203 | Heap* heap = name->GetHeap(); |
| 12204 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12205 | if (entry != kNotFound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12206 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12207 | int function_index = Smi::cast(smi_index)->value(); |
| 12208 | return &(kIntrinsicFunctions[function_index]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12209 | } |
| 12210 | return NULL; |
| 12211 | } |
| 12212 | |
| 12213 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12214 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12215 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 12216 | } |
| 12217 | |
| 12218 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12219 | void Runtime::PerformGC(Object* result) { |
| 12220 | Failure* failure = Failure::cast(result); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12221 | if (failure->IsRetryAfterGC()) { |
| 12222 | // Try to do a garbage collection; ignore it if it fails. The C |
| 12223 | // entry stub will throw an out-of-memory exception in that case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12224 | HEAP->CollectGarbage(failure->allocation_space()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12225 | } else { |
| 12226 | // Handle last resort GC and make sure to allow future allocations |
| 12227 | // to grow the heap without causing GCs (if possible). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 12228 | COUNTERS->gc_last_resort_from_js()->Increment(); |
| 12229 | HEAP->CollectAllGarbage(false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12230 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12231 | } |
| 12232 | |
| 12233 | |
| 12234 | } } // namespace v8::internal |