ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1 | // Copyright 2011 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" |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 35 | #include "bootstrapper.h" |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 36 | #include "codegen.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 37 | #include "compilation-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | #include "compiler.h" |
| 39 | #include "cpu.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 40 | #include "dateparser-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 42 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | #include "execution.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 44 | #include "global-handles.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 45 | #include "jsregexp.h" |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 46 | #include "json-parser.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 47 | #include "liveedit.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 48 | #include "liveobjectlist-inl.h" |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 49 | #include "misc-intrinsics.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 50 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 52 | #include "runtime-profiler.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 53 | #include "runtime.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | #include "scopeinfo.h" |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 55 | #include "smart-array-pointer.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 56 | #include "string-search.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 57 | #include "stub-cache.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 58 | #include "v8threads.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 59 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 61 | namespace v8 { |
| 62 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
| 64 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 65 | #define RUNTIME_ASSERT(value) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 66 | if (!(value)) return isolate->ThrowIllegalOperation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 67 | |
| 68 | // Cast the given object to a value of the specified type and store |
| 69 | // it in a variable with the given name. If the object is not of the |
| 70 | // expected type call IllegalOperation and return. |
| 71 | #define CONVERT_CHECKED(Type, name, obj) \ |
| 72 | RUNTIME_ASSERT(obj->Is##Type()); \ |
| 73 | Type* name = Type::cast(obj); |
| 74 | |
| 75 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 76 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 77 | Handle<Type> name = args.at<Type>(index); |
| 78 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 79 | // Cast the given object to a boolean and store it in a variable with |
| 80 | // the given name. If the object is not a boolean call IllegalOperation |
| 81 | // and return. |
| 82 | #define CONVERT_BOOLEAN_CHECKED(name, obj) \ |
| 83 | RUNTIME_ASSERT(obj->IsBoolean()); \ |
| 84 | bool name = (obj)->IsTrue(); |
| 85 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 86 | // Cast the given argument to a Smi and store its value in an int variable |
| 87 | // with the given name. If the argument is not a Smi call IllegalOperation |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 88 | // and return. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 89 | #define CONVERT_SMI_ARG_CHECKED(name, index) \ |
| 90 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 91 | int name = args.smi_at(index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 92 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 93 | // Cast the given argument to a double and store it in a variable with |
| 94 | // the given name. If the argument is not a number (as opposed to |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | // the number not-a-number) call IllegalOperation and return. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 96 | #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \ |
| 97 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 98 | double name = args.number_at(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | |
| 100 | // Call the specified converter on the object *comand store the result in |
| 101 | // a variable of the specified type with the given name. If the |
| 102 | // object is not a Number call IllegalOperation and return. |
| 103 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 104 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 105 | type name = NumberTo##Type(obj); |
| 106 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 108 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 109 | JSObject* boilerplate) { |
| 110 | StackLimitCheck check(isolate); |
| 111 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 112 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 113 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 114 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 115 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 116 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 117 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 118 | JSObject* copy = JSObject::cast(result); |
| 119 | |
| 120 | // Deep copy local properties. |
| 121 | if (copy->HasFastProperties()) { |
| 122 | FixedArray* properties = copy->properties(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 123 | for (int i = 0; i < properties->length(); i++) { |
| 124 | Object* value = properties->get(i); |
| 125 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 126 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 127 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 128 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 129 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 130 | properties->set(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 133 | int nof = copy->map()->inobject_properties(); |
| 134 | for (int i = 0; i < nof; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 135 | Object* value = copy->InObjectPropertyAt(i); |
| 136 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 137 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 138 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 139 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 140 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 141 | copy->InObjectPropertyAtPut(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 145 | { MaybeObject* maybe_result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 146 | heap->AllocateFixedArray(copy->NumberOfLocalProperties(NONE)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 147 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 148 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 149 | FixedArray* names = FixedArray::cast(result); |
| 150 | copy->GetLocalPropertyNames(names, 0); |
| 151 | for (int i = 0; i < names->length(); i++) { |
| 152 | ASSERT(names->get(i)->IsString()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 153 | String* key_string = String::cast(names->get(i)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 154 | PropertyAttributes attributes = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 155 | copy->GetLocalPropertyAttribute(key_string); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 156 | // Only deep copy fields from the object literal expression. |
| 157 | // In particular, don't try to copy the length attribute of |
| 158 | // an array. |
| 159 | if (attributes != NONE) continue; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 160 | Object* value = |
| 161 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 162 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 163 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 164 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 165 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 166 | } |
| 167 | { MaybeObject* maybe_result = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 168 | // Creating object copy for literals. No strict mode needed. |
| 169 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 170 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 171 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Deep copy local elements. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 177 | // Pixel elements cannot be created using an object literal. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 178 | ASSERT(!copy->HasExternalArrayElements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 179 | switch (copy->GetElementsKind()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 180 | case FAST_ELEMENTS: { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 181 | FixedArray* elements = FixedArray::cast(copy->elements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 182 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 183 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 184 | #ifdef DEBUG |
| 185 | for (int i = 0; i < elements->length(); i++) { |
| 186 | ASSERT(!elements->get(i)->IsJSObject()); |
| 187 | } |
| 188 | #endif |
| 189 | } else { |
| 190 | for (int i = 0; i < elements->length(); i++) { |
| 191 | Object* value = elements->get(i); |
| 192 | if (value->IsJSObject()) { |
| 193 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 194 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 195 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 196 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 197 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 198 | elements->set(i, result); |
| 199 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 202 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 203 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 204 | case DICTIONARY_ELEMENTS: { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 205 | NumberDictionary* element_dictionary = copy->element_dictionary(); |
| 206 | int capacity = element_dictionary->Capacity(); |
| 207 | for (int i = 0; i < capacity; i++) { |
| 208 | Object* k = element_dictionary->KeyAt(i); |
| 209 | if (element_dictionary->IsKey(k)) { |
| 210 | Object* value = element_dictionary->ValueAt(i); |
| 211 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 212 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 213 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 214 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 215 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 216 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 217 | element_dictionary->ValueAtPut(i, result); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | break; |
| 222 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 223 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 224 | UNIMPLEMENTED(); |
| 225 | break; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 226 | case EXTERNAL_PIXEL_ELEMENTS: |
| 227 | case EXTERNAL_BYTE_ELEMENTS: |
| 228 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 229 | case EXTERNAL_SHORT_ELEMENTS: |
| 230 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 231 | case EXTERNAL_INT_ELEMENTS: |
| 232 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 233 | case EXTERNAL_FLOAT_ELEMENTS: |
| 234 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 235 | case FAST_DOUBLE_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 236 | // No contained objects, nothing to do. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 237 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 238 | } |
| 239 | return copy; |
| 240 | } |
| 241 | |
| 242 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 243 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CloneLiteralBoilerplate) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 244 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 245 | return DeepCopyBoilerplate(isolate, boilerplate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 249 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CloneShallowLiteralBoilerplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 250 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 251 | return isolate->heap()->CopyJSObject(boilerplate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 255 | static Handle<Map> ComputeObjectLiteralMap( |
| 256 | Handle<Context> context, |
| 257 | Handle<FixedArray> constant_properties, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 258 | bool* is_result_from_cache) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 259 | Isolate* isolate = context->GetIsolate(); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 260 | int properties_length = constant_properties->length(); |
| 261 | int number_of_properties = properties_length / 2; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 262 | if (FLAG_canonicalize_object_literal_maps) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 263 | // Check that there are only symbols and array indices among keys. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 264 | int number_of_symbol_keys = 0; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 265 | for (int p = 0; p != properties_length; p += 2) { |
| 266 | Object* key = constant_properties->get(p); |
| 267 | uint32_t element_index = 0; |
| 268 | if (key->IsSymbol()) { |
| 269 | number_of_symbol_keys++; |
| 270 | } else if (key->ToArrayIndex(&element_index)) { |
| 271 | // An index key does not require space in the property backing store. |
| 272 | number_of_properties--; |
| 273 | } else { |
| 274 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 275 | // ASSERT to make sure that the if condition after the loop is false. |
| 276 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 277 | break; |
| 278 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 279 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 280 | // If we only have symbols and array indices among keys then we can |
| 281 | // use the map cache in the global context. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 282 | const int kMaxKeys = 10; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 283 | if ((number_of_symbol_keys == number_of_properties) && |
| 284 | (number_of_symbol_keys < kMaxKeys)) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 285 | // Create the fixed array with the key. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 286 | Handle<FixedArray> keys = |
| 287 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 288 | if (number_of_symbol_keys > 0) { |
| 289 | int index = 0; |
| 290 | for (int p = 0; p < properties_length; p += 2) { |
| 291 | Object* key = constant_properties->get(p); |
| 292 | if (key->IsSymbol()) { |
| 293 | keys->set(index++, key); |
| 294 | } |
| 295 | } |
| 296 | ASSERT(index == number_of_symbol_keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 297 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 298 | *is_result_from_cache = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 299 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 302 | *is_result_from_cache = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 303 | return isolate->factory()->CopyMap( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 304 | Handle<Map>(context->object_function()->initial_map()), |
| 305 | number_of_properties); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 309 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 310 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 311 | Handle<FixedArray> literals, |
| 312 | Handle<FixedArray> constant_properties); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 313 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 314 | |
| 315 | static Handle<Object> CreateObjectLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 316 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 317 | Handle<FixedArray> literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 318 | Handle<FixedArray> constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 319 | bool should_have_fast_elements, |
| 320 | bool has_function_literal) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 321 | // Get the global context from the literals array. This is the |
| 322 | // context in which the function was created and we use the object |
| 323 | // function from this context to create the object literal. We do |
| 324 | // not use the object function from the current global context |
| 325 | // because this might be the object function from another context |
| 326 | // which we should not have access to. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 327 | Handle<Context> context = |
| 328 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 329 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 330 | // In case we have function literals, we want the object to be in |
| 331 | // slow properties mode for now. We don't go in the map cache because |
| 332 | // maps with constant functions can't be shared if the functions are |
| 333 | // not the same (which is the common case). |
| 334 | bool is_result_from_cache = false; |
| 335 | Handle<Map> map = has_function_literal |
| 336 | ? Handle<Map>(context->object_function()->initial_map()) |
| 337 | : ComputeObjectLiteralMap(context, |
| 338 | constant_properties, |
| 339 | &is_result_from_cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 340 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 341 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 342 | |
| 343 | // Normalize the elements of the boilerplate to save space if needed. |
| 344 | if (!should_have_fast_elements) NormalizeElements(boilerplate); |
| 345 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 346 | // Add the constant properties to the boilerplate. |
| 347 | int length = constant_properties->length(); |
| 348 | bool should_transform = |
| 349 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 350 | if (should_transform || has_function_literal) { |
| 351 | // Normalize the properties of object to avoid n^2 behavior |
| 352 | // when extending the object multiple properties. Indicate the number of |
| 353 | // properties to be added. |
| 354 | NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
| 355 | } |
| 356 | |
| 357 | for (int index = 0; index < length; index +=2) { |
| 358 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 359 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 360 | if (value->IsFixedArray()) { |
| 361 | // The value contains the constant_properties of a |
| 362 | // simple object or array literal. |
| 363 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 364 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 365 | if (value.is_null()) return value; |
| 366 | } |
| 367 | Handle<Object> result; |
| 368 | uint32_t element_index = 0; |
| 369 | if (key->IsSymbol()) { |
| 370 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 371 | // Array index as string (uint32). |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 372 | result = SetOwnElement(boilerplate, |
| 373 | element_index, |
| 374 | value, |
| 375 | kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 376 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 377 | Handle<String> name(String::cast(*key)); |
| 378 | ASSERT(!name->AsArrayIndex(&element_index)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 379 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 380 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 382 | } else if (key->ToArrayIndex(&element_index)) { |
| 383 | // Array index (uint32). |
| 384 | result = SetOwnElement(boilerplate, |
| 385 | element_index, |
| 386 | value, |
| 387 | kNonStrictMode); |
| 388 | } else { |
| 389 | // Non-uint32 number. |
| 390 | ASSERT(key->IsNumber()); |
| 391 | double num = key->Number(); |
| 392 | char arr[100]; |
| 393 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 394 | const char* str = DoubleToCString(num, buffer); |
| 395 | Handle<String> name = |
| 396 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
| 397 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 398 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 400 | // If setting the property on the boilerplate throws an |
| 401 | // exception, the exception is converted to an empty handle in |
| 402 | // the handle based operations. In that case, we need to |
| 403 | // convert back to an exception. |
| 404 | if (result.is_null()) return result; |
| 405 | } |
| 406 | |
| 407 | // Transform to fast properties if necessary. For object literals with |
| 408 | // containing function literals we defer this operation until after all |
| 409 | // computed properties have been assigned so that we can generate |
| 410 | // constant function properties. |
| 411 | if (should_transform && !has_function_literal) { |
| 412 | TransformToFastProperties(boilerplate, |
| 413 | boilerplate->map()->unused_property_fields()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 414 | } |
| 415 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 416 | return boilerplate; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 420 | static Handle<Object> CreateArrayLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 421 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 422 | Handle<FixedArray> literals, |
| 423 | Handle<FixedArray> elements) { |
| 424 | // Create the JSArray. |
| 425 | Handle<JSFunction> constructor( |
| 426 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 427 | Handle<Object> object = isolate->factory()->NewJSObject(constructor); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 428 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 429 | const bool is_cow = |
| 430 | (elements->map() == isolate->heap()->fixed_cow_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 431 | Handle<FixedArray> copied_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 432 | is_cow ? elements : isolate->factory()->CopyFixedArray(elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 433 | |
| 434 | Handle<FixedArray> content = Handle<FixedArray>::cast(copied_elements); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 435 | if (is_cow) { |
| 436 | #ifdef DEBUG |
| 437 | // Copy-on-write arrays must be shallow (and simple). |
| 438 | for (int i = 0; i < content->length(); i++) { |
| 439 | ASSERT(!content->get(i)->IsFixedArray()); |
| 440 | } |
| 441 | #endif |
| 442 | } else { |
| 443 | for (int i = 0; i < content->length(); i++) { |
| 444 | if (content->get(i)->IsFixedArray()) { |
| 445 | // The value contains the constant_properties of a |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 446 | // simple object or array literal. |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 447 | Handle<FixedArray> fa(FixedArray::cast(content->get(i))); |
| 448 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 449 | CreateLiteralBoilerplate(isolate, literals, fa); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 450 | if (result.is_null()) return result; |
| 451 | content->set(i, *result); |
| 452 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | // Set the elements. |
| 457 | Handle<JSArray>::cast(object)->SetContent(*content); |
| 458 | return object; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 463 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 464 | Handle<FixedArray> literals, |
| 465 | Handle<FixedArray> array) { |
| 466 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 467 | const bool kHasNoFunctionLiteral = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 468 | switch (CompileTimeValue::GetType(array)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 469 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 470 | return CreateObjectLiteralBoilerplate(isolate, |
| 471 | literals, |
| 472 | elements, |
| 473 | true, |
| 474 | kHasNoFunctionLiteral); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 475 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 476 | return CreateObjectLiteralBoilerplate(isolate, |
| 477 | literals, |
| 478 | elements, |
| 479 | false, |
| 480 | kHasNoFunctionLiteral); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 481 | case CompileTimeValue::ARRAY_LITERAL: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 482 | return CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 483 | default: |
| 484 | UNREACHABLE(); |
| 485 | return Handle<Object>::null(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 490 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralBoilerplate) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 491 | // Takes a FixedArray of elements containing the literal elements of |
| 492 | // the array literal and produces JSArray with those elements. |
| 493 | // Additionally takes the literals array of the surrounding function |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 494 | // which contains the context from which to get the Array function |
| 495 | // to use for creating the array literal. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 496 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 497 | ASSERT(args.length() == 3); |
| 498 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 499 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 500 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 502 | Handle<Object> object = |
| 503 | CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 504 | if (object.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 505 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 506 | // Update the functions literal and return the boilerplate. |
| 507 | literals->set(literals_index, *object); |
| 508 | return *object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 513 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 514 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 515 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 516 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 517 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 518 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 519 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 520 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 521 | |
| 522 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 523 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 524 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 525 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 526 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 527 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 528 | should_have_fast_elements, |
| 529 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 530 | if (boilerplate.is_null()) return Failure::Exception(); |
| 531 | // Update the functions literal and return the boilerplate. |
| 532 | literals->set(literals_index, *boilerplate); |
| 533 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 534 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 538 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 539 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 540 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 541 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 542 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 543 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 544 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 545 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 546 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 547 | |
| 548 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 549 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 550 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 551 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 552 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 553 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 554 | should_have_fast_elements, |
| 555 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 556 | if (boilerplate.is_null()) return Failure::Exception(); |
| 557 | // Update the functions literal and return the boilerplate. |
| 558 | literals->set(literals_index, *boilerplate); |
| 559 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 560 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 564 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 565 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 566 | ASSERT(args.length() == 3); |
| 567 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 568 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 569 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 570 | |
| 571 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 572 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 573 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 574 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 575 | if (boilerplate.is_null()) return Failure::Exception(); |
| 576 | // Update the functions literal and return the boilerplate. |
| 577 | literals->set(literals_index, *boilerplate); |
| 578 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 579 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 583 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 584 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 585 | ASSERT(args.length() == 3); |
| 586 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 587 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 588 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 589 | |
| 590 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 591 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 592 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 593 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 594 | if (boilerplate.is_null()) return Failure::Exception(); |
| 595 | // Update the functions literal and return the boilerplate. |
| 596 | literals->set(literals_index, *boilerplate); |
| 597 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 598 | if (JSObject::cast(*boilerplate)->elements()->map() == |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 599 | isolate->heap()->fixed_cow_array_map()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 600 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 601 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 602 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 606 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSProxy) { |
| 607 | ASSERT(args.length() == 2); |
| 608 | Object* handler = args[0]; |
| 609 | Object* prototype = args[1]; |
| 610 | Object* used_prototype = |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 611 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 612 | return isolate->heap()->AllocateJSProxy(handler, used_prototype); |
| 613 | } |
| 614 | |
| 615 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 616 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSFunctionProxy) { |
| 617 | ASSERT(args.length() == 4); |
| 618 | Object* handler = args[0]; |
| 619 | Object* call_trap = args[1]; |
| 620 | Object* construct_trap = args[2]; |
| 621 | Object* prototype = args[3]; |
| 622 | Object* used_prototype = |
| 623 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
| 624 | return isolate->heap()->AllocateJSFunctionProxy( |
| 625 | handler, call_trap, construct_trap, used_prototype); |
| 626 | } |
| 627 | |
| 628 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 629 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSProxy) { |
| 630 | ASSERT(args.length() == 1); |
| 631 | Object* obj = args[0]; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 632 | return isolate->heap()->ToBoolean(obj->IsJSProxy()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 636 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSFunctionProxy) { |
| 637 | ASSERT(args.length() == 1); |
| 638 | Object* obj = args[0]; |
| 639 | return isolate->heap()->ToBoolean(obj->IsJSFunctionProxy()); |
| 640 | } |
| 641 | |
| 642 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 643 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHandler) { |
| 644 | ASSERT(args.length() == 1); |
| 645 | CONVERT_CHECKED(JSProxy, proxy, args[0]); |
| 646 | return proxy->handler(); |
| 647 | } |
| 648 | |
| 649 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 650 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetCallTrap) { |
| 651 | ASSERT(args.length() == 1); |
| 652 | CONVERT_CHECKED(JSFunctionProxy, proxy, args[0]); |
| 653 | return proxy->call_trap(); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructTrap) { |
| 658 | ASSERT(args.length() == 1); |
| 659 | CONVERT_CHECKED(JSFunctionProxy, proxy, args[0]); |
| 660 | return proxy->construct_trap(); |
| 661 | } |
| 662 | |
| 663 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 664 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { |
| 665 | ASSERT(args.length() == 1); |
| 666 | CONVERT_CHECKED(JSProxy, proxy, args[0]); |
| 667 | proxy->Fix(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 668 | return isolate->heap()->undefined_value(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 672 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapInitialize) { |
| 673 | HandleScope scope(isolate); |
| 674 | ASSERT(args.length() == 1); |
| 675 | CONVERT_ARG_CHECKED(JSWeakMap, weakmap, 0); |
| 676 | ASSERT(weakmap->map()->inobject_properties() == 0); |
| 677 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 678 | weakmap->set_table(*table); |
| 679 | weakmap->set_next(Smi::FromInt(0)); |
| 680 | return *weakmap; |
| 681 | } |
| 682 | |
| 683 | |
| 684 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapGet) { |
| 685 | NoHandleAllocation ha; |
| 686 | ASSERT(args.length() == 2); |
| 687 | CONVERT_ARG_CHECKED(JSWeakMap, weakmap, 0); |
| 688 | // TODO(mstarzinger): Currently we cannot use JSProxy objects as keys |
| 689 | // because they cannot be cast to JSObject to get an identity hash code. |
| 690 | CONVERT_ARG_CHECKED(JSObject, key, 1); |
| 691 | return weakmap->table()->Lookup(*key); |
| 692 | } |
| 693 | |
| 694 | |
| 695 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapSet) { |
| 696 | HandleScope scope(isolate); |
| 697 | ASSERT(args.length() == 3); |
| 698 | CONVERT_ARG_CHECKED(JSWeakMap, weakmap, 0); |
| 699 | // TODO(mstarzinger): See Runtime_WeakMapGet above. |
| 700 | CONVERT_ARG_CHECKED(JSObject, key, 1); |
| 701 | Handle<Object> value(args[2]); |
| 702 | Handle<ObjectHashTable> table(weakmap->table()); |
| 703 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 704 | weakmap->set_table(*new_table); |
| 705 | return *value; |
| 706 | } |
| 707 | |
| 708 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 709 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClassOf) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 710 | NoHandleAllocation ha; |
| 711 | ASSERT(args.length() == 1); |
| 712 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 713 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 714 | return JSObject::cast(obj)->class_name(); |
| 715 | } |
| 716 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 717 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 718 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPrototype) { |
| 719 | NoHandleAllocation ha; |
| 720 | ASSERT(args.length() == 1); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 721 | CONVERT_CHECKED(JSReceiver, input_obj, args[0]); |
| 722 | Object* obj = input_obj; |
| 723 | // We don't expect access checks to be needed on JSProxy objects. |
| 724 | ASSERT(!obj->IsAccessCheckNeeded() || obj->IsJSObject()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 725 | do { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 726 | if (obj->IsAccessCheckNeeded() && |
| 727 | !isolate->MayNamedAccess(JSObject::cast(obj), |
| 728 | isolate->heap()->Proto_symbol(), |
| 729 | v8::ACCESS_GET)) { |
| 730 | isolate->ReportFailedAccessCheck(JSObject::cast(obj), v8::ACCESS_GET); |
| 731 | return isolate->heap()->undefined_value(); |
| 732 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 733 | obj = obj->GetPrototype(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 734 | } while (obj->IsJSObject() && |
| 735 | JSObject::cast(obj)->map()->is_hidden_prototype()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 736 | return obj; |
| 737 | } |
| 738 | |
| 739 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 740 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsInPrototypeChain) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 741 | NoHandleAllocation ha; |
| 742 | ASSERT(args.length() == 2); |
| 743 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 744 | Object* O = args[0]; |
| 745 | Object* V = args[1]; |
| 746 | while (true) { |
| 747 | Object* prototype = V->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 748 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 749 | if (O == prototype) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 750 | V = prototype; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 755 | // Inserts an object as the hidden prototype of another object. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 756 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetHiddenPrototype) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 757 | NoHandleAllocation ha; |
| 758 | ASSERT(args.length() == 2); |
| 759 | CONVERT_CHECKED(JSObject, jsobject, args[0]); |
| 760 | CONVERT_CHECKED(JSObject, proto, args[1]); |
| 761 | |
| 762 | // Sanity checks. The old prototype (that we are replacing) could |
| 763 | // theoretically be null, but if it is not null then check that we |
| 764 | // didn't already install a hidden prototype here. |
| 765 | RUNTIME_ASSERT(!jsobject->GetPrototype()->IsHeapObject() || |
| 766 | !HeapObject::cast(jsobject->GetPrototype())->map()->is_hidden_prototype()); |
| 767 | RUNTIME_ASSERT(!proto->map()->is_hidden_prototype()); |
| 768 | |
| 769 | // 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] | 770 | Object* map_or_failure; |
| 771 | { MaybeObject* maybe_map_or_failure = proto->map()->CopyDropTransitions(); |
| 772 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 773 | return maybe_map_or_failure; |
| 774 | } |
| 775 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 776 | Map* new_proto_map = Map::cast(map_or_failure); |
| 777 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 778 | { MaybeObject* maybe_map_or_failure = jsobject->map()->CopyDropTransitions(); |
| 779 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 780 | return maybe_map_or_failure; |
| 781 | } |
| 782 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 783 | Map* new_map = Map::cast(map_or_failure); |
| 784 | |
| 785 | // Set proto's prototype to be the old prototype of the object. |
| 786 | new_proto_map->set_prototype(jsobject->GetPrototype()); |
| 787 | proto->set_map(new_proto_map); |
| 788 | new_proto_map->set_is_hidden_prototype(); |
| 789 | |
| 790 | // Set the object's prototype to proto. |
| 791 | new_map->set_prototype(proto); |
| 792 | jsobject->set_map(new_map); |
| 793 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 794 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 798 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsConstructCall) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 799 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 800 | ASSERT(args.length() == 0); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 801 | JavaScriptFrameIterator it(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 802 | return isolate->heap()->ToBoolean(it.frame()->IsConstructor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 806 | // Recursively traverses hidden prototypes if property is not found |
| 807 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 808 | String* name, |
| 809 | LookupResult* result) { |
| 810 | obj->LocalLookupRealNamedProperty(name, result); |
| 811 | |
| 812 | if (!result->IsProperty()) { |
| 813 | Object* proto = obj->GetPrototype(); |
| 814 | if (proto->IsJSObject() && |
| 815 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 816 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 817 | name, result); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 822 | static bool CheckAccessException(LookupResult* result, |
| 823 | v8::AccessType access_type) { |
| 824 | if (result->type() == CALLBACKS) { |
| 825 | Object* callback = result->GetCallbackObject(); |
| 826 | if (callback->IsAccessorInfo()) { |
| 827 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 828 | bool can_access = |
| 829 | (access_type == v8::ACCESS_HAS && |
| 830 | (info->all_can_read() || info->all_can_write())) || |
| 831 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 832 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 833 | return can_access; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | |
| 841 | static bool CheckAccess(JSObject* obj, |
| 842 | String* name, |
| 843 | LookupResult* result, |
| 844 | v8::AccessType access_type) { |
| 845 | ASSERT(result->IsProperty()); |
| 846 | |
| 847 | JSObject* holder = result->holder(); |
| 848 | JSObject* current = obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 849 | Isolate* isolate = obj->GetIsolate(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 850 | while (true) { |
| 851 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 852 | !isolate->MayNamedAccess(current, name, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 853 | // Access check callback denied the access, but some properties |
| 854 | // can have a special permissions which override callbacks descision |
| 855 | // (currently see v8::AccessControl). |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | if (current == holder) { |
| 860 | return true; |
| 861 | } |
| 862 | |
| 863 | current = JSObject::cast(current->GetPrototype()); |
| 864 | } |
| 865 | |
| 866 | // API callbacks can have per callback access exceptions. |
| 867 | switch (result->type()) { |
| 868 | case CALLBACKS: { |
| 869 | if (CheckAccessException(result, access_type)) { |
| 870 | return true; |
| 871 | } |
| 872 | break; |
| 873 | } |
| 874 | case INTERCEPTOR: { |
| 875 | // If the object has an interceptor, try real named properties. |
| 876 | // Overwrite the result to fetch the correct property later. |
| 877 | holder->LookupRealNamedProperty(name, result); |
| 878 | if (result->IsProperty()) { |
| 879 | if (CheckAccessException(result, access_type)) { |
| 880 | return true; |
| 881 | } |
| 882 | } |
| 883 | break; |
| 884 | } |
| 885 | default: |
| 886 | break; |
| 887 | } |
| 888 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 889 | isolate->ReportFailedAccessCheck(current, access_type); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 890 | return false; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 895 | static bool CheckElementAccess(JSObject* obj, |
| 896 | uint32_t index, |
| 897 | v8::AccessType access_type) { |
| 898 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 899 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 900 | return false; |
| 901 | } |
| 902 | |
| 903 | return true; |
| 904 | } |
| 905 | |
| 906 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 907 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 908 | enum PropertyDescriptorIndices { |
| 909 | IS_ACCESSOR_INDEX, |
| 910 | VALUE_INDEX, |
| 911 | GETTER_INDEX, |
| 912 | SETTER_INDEX, |
| 913 | WRITABLE_INDEX, |
| 914 | ENUMERABLE_INDEX, |
| 915 | CONFIGURABLE_INDEX, |
| 916 | DESCRIPTOR_SIZE |
| 917 | }; |
| 918 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 919 | // Returns an array with the property description: |
| 920 | // if args[1] is not a property on args[0] |
| 921 | // returns undefined |
| 922 | // if args[1] is a data property on args[0] |
| 923 | // [false, value, Writeable, Enumerable, Configurable] |
| 924 | // if args[1] is an accessor on args[0] |
| 925 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 926 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOwnProperty) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 927 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 928 | Heap* heap = isolate->heap(); |
| 929 | HandleScope scope(isolate); |
| 930 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 931 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 932 | LookupResult result; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 933 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 934 | CONVERT_ARG_CHECKED(String, name, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 935 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 936 | // This could be an element. |
| 937 | uint32_t index; |
| 938 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 939 | switch (obj->HasLocalElement(index)) { |
| 940 | case JSObject::UNDEFINED_ELEMENT: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 941 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 942 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 943 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 944 | // Special handling of string objects according to ECMAScript 5 |
| 945 | // 15.5.5.2. Note that this might be a string object with elements |
| 946 | // other than the actual string value. This is covered by the |
| 947 | // subsequent cases. |
| 948 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 949 | Handle<String> str(String::cast(js_value->value())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 950 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 951 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 952 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 953 | elms->set(VALUE_INDEX, *substr); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 954 | elms->set(WRITABLE_INDEX, heap->false_value()); |
| 955 | elms->set(ENUMERABLE_INDEX, heap->false_value()); |
| 956 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 957 | return *desc; |
| 958 | } |
| 959 | |
| 960 | case JSObject::INTERCEPTED_ELEMENT: |
| 961 | case JSObject::FAST_ELEMENT: { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 962 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 963 | Handle<Object> value = GetElement(obj, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 964 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 965 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 966 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 967 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 968 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 969 | return *desc; |
| 970 | } |
| 971 | |
| 972 | case JSObject::DICTIONARY_ELEMENT: { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 973 | Handle<JSObject> holder = obj; |
| 974 | if (obj->IsJSGlobalProxy()) { |
| 975 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 976 | if (proto->IsNull()) return heap->undefined_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 977 | ASSERT(proto->IsJSGlobalObject()); |
| 978 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 979 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 980 | FixedArray* elements = FixedArray::cast(holder->elements()); |
| 981 | NumberDictionary* dictionary = NULL; |
| 982 | if (elements->map() == heap->non_strict_arguments_elements_map()) { |
| 983 | dictionary = NumberDictionary::cast(elements->get(1)); |
| 984 | } else { |
| 985 | dictionary = NumberDictionary::cast(elements); |
| 986 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 987 | int entry = dictionary->FindEntry(index); |
| 988 | ASSERT(entry != NumberDictionary::kNotFound); |
| 989 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 990 | switch (details.type()) { |
| 991 | case CALLBACKS: { |
| 992 | // This is an accessor property with getter and/or setter. |
| 993 | FixedArray* callbacks = |
| 994 | FixedArray::cast(dictionary->ValueAt(entry)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 995 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 996 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
| 997 | elms->set(GETTER_INDEX, callbacks->get(0)); |
| 998 | } |
| 999 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
| 1000 | elms->set(SETTER_INDEX, callbacks->get(1)); |
| 1001 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1004 | case NORMAL: { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1005 | // This is a data property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1006 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1007 | Handle<Object> value = GetElement(obj, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1008 | ASSERT(!value.is_null()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1009 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1010 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1011 | break; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1012 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1013 | default: |
| 1014 | UNREACHABLE(); |
| 1015 | break; |
| 1016 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1017 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 1018 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1019 | return *desc; |
| 1020 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1024 | // Use recursive implementation to also traverse hidden prototypes |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1025 | GetOwnPropertyImplementation(*obj, *name, &result); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1026 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1027 | if (!result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1028 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1029 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1030 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1031 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1032 | return heap->false_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1035 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 1036 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1037 | |
| 1038 | bool is_js_accessor = (result.type() == CALLBACKS) && |
| 1039 | (result.GetCallbackObject()->IsFixedArray()); |
| 1040 | |
| 1041 | if (is_js_accessor) { |
| 1042 | // __defineGetter__/__defineSetter__ callback. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1043 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1044 | |
| 1045 | FixedArray* structure = FixedArray::cast(result.GetCallbackObject()); |
| 1046 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
| 1047 | elms->set(GETTER_INDEX, structure->get(0)); |
| 1048 | } |
| 1049 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
| 1050 | elms->set(SETTER_INDEX, structure->get(1)); |
| 1051 | } |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1052 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1053 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 1054 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1055 | |
| 1056 | PropertyAttributes attrs; |
| 1057 | Object* value; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1058 | // GetProperty will check access and report any violations. |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1059 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 1060 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 1061 | } |
| 1062 | elms->set(VALUE_INDEX, value); |
| 1063 | } |
| 1064 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1065 | return *desc; |
| 1066 | } |
| 1067 | |
| 1068 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1069 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PreventExtensions) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1070 | ASSERT(args.length() == 1); |
| 1071 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 1072 | return obj->PreventExtensions(); |
| 1073 | } |
| 1074 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1075 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1076 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsExtensible) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1077 | ASSERT(args.length() == 1); |
| 1078 | CONVERT_CHECKED(JSObject, obj, args[0]); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1079 | if (obj->IsJSGlobalProxy()) { |
| 1080 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1081 | if (proto->IsNull()) return isolate->heap()->false_value(); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1082 | ASSERT(proto->IsJSGlobalObject()); |
| 1083 | obj = JSObject::cast(proto); |
| 1084 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1085 | return isolate->heap()->ToBoolean(obj->map()->is_extensible()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1089 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1090 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1091 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1092 | CONVERT_ARG_CHECKED(JSRegExp, re, 0); |
| 1093 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 1094 | CONVERT_ARG_CHECKED(String, flags, 2); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1095 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 1096 | if (result.is_null()) return Failure::Exception(); |
| 1097 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1101 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateApiFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1102 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1103 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1104 | CONVERT_ARG_CHECKED(FunctionTemplateInfo, data, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1105 | return *isolate->factory()->CreateApiFunction(data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1109 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsTemplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1110 | ASSERT(args.length() == 1); |
| 1111 | Object* arg = args[0]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1112 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1113 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1117 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetTemplateField) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1118 | ASSERT(args.length() == 2); |
| 1119 | CONVERT_CHECKED(HeapObject, templ, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1120 | CONVERT_CHECKED(Smi, field, args[1]); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1121 | int index = field->value(); |
| 1122 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1123 | InstanceType type = templ->map()->instance_type(); |
| 1124 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1125 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1126 | RUNTIME_ASSERT(offset > 0); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1127 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1128 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1129 | } else { |
| 1130 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1131 | } |
| 1132 | return *HeapObject::RawField(templ, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1136 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DisableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1137 | ASSERT(args.length() == 1); |
| 1138 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1139 | Map* old_map = object->map(); |
| 1140 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1141 | if (needs_access_checks) { |
| 1142 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1143 | Object* new_map; |
| 1144 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1145 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1146 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1147 | |
| 1148 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1149 | object->set_map(Map::cast(new_map)); |
| 1150 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1151 | return isolate->heap()->ToBoolean(needs_access_checks); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1155 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EnableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1156 | ASSERT(args.length() == 1); |
| 1157 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1158 | Map* old_map = object->map(); |
| 1159 | if (!old_map->is_access_check_needed()) { |
| 1160 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1161 | Object* new_map; |
| 1162 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1163 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1164 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1165 | |
| 1166 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1167 | object->set_map(Map::cast(new_map)); |
| 1168 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1169 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1173 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1174 | const char* type, |
| 1175 | Handle<String> name) { |
| 1176 | HandleScope scope(isolate); |
| 1177 | Handle<Object> type_handle = |
| 1178 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1179 | Handle<Object> args[2] = { type_handle, name }; |
| 1180 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1181 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1182 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1186 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareGlobals) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1187 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1188 | HandleScope scope(isolate); |
| 1189 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1190 | isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1191 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1192 | Handle<Context> context = args.at<Context>(0); |
| 1193 | CONVERT_ARG_CHECKED(FixedArray, pairs, 1); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1194 | CONVERT_SMI_ARG_CHECKED(flags, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1196 | // Traverse the name/value pairs and set the properties. |
| 1197 | int length = pairs->length(); |
| 1198 | for (int i = 0; i < length; i += 2) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1199 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1200 | Handle<String> name(String::cast(pairs->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1201 | Handle<Object> value(pairs->get(i + 1), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1202 | |
| 1203 | // We have to declare a global const property. To capture we only |
| 1204 | // assign to it when evaluating the assignment for "const x = |
| 1205 | // <expr>" the initial value is the hole. |
| 1206 | bool is_const_property = value->IsTheHole(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1207 | bool is_function_declaration = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1208 | if (value->IsUndefined() || is_const_property) { |
| 1209 | // Lookup the property in the global object, and don't set the |
| 1210 | // value of the variable if the property is already there. |
| 1211 | LookupResult lookup; |
| 1212 | global->Lookup(*name, &lookup); |
| 1213 | if (lookup.IsProperty()) { |
| 1214 | // Determine if the property is local by comparing the holder |
| 1215 | // against the global object. The information will be used to |
| 1216 | // avoid throwing re-declaration errors when declaring |
| 1217 | // variables or constants that exist in the prototype chain. |
| 1218 | bool is_local = (*global == lookup.holder()); |
| 1219 | // Get the property attributes and determine if the property is |
| 1220 | // read-only. |
| 1221 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1222 | bool is_read_only = (attributes & READ_ONLY) != 0; |
| 1223 | if (lookup.type() == INTERCEPTOR) { |
| 1224 | // If the interceptor says the property is there, we |
| 1225 | // just return undefined without overwriting the property. |
| 1226 | // Otherwise, we continue to setting the property. |
| 1227 | if (attributes != ABSENT) { |
| 1228 | // Check if the existing property conflicts with regards to const. |
| 1229 | if (is_local && (is_read_only || is_const_property)) { |
| 1230 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1231 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1232 | }; |
| 1233 | // The property already exists without conflicting: Go to |
| 1234 | // the next declaration. |
| 1235 | continue; |
| 1236 | } |
| 1237 | // Fall-through and introduce the absent property by using |
| 1238 | // SetProperty. |
| 1239 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1240 | // For const properties, we treat a callback with this name |
| 1241 | // even in the prototype as a conflicting declaration. |
| 1242 | if (is_const_property && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1243 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1244 | } |
| 1245 | // Otherwise, we check for locally conflicting declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1246 | if (is_local && (is_read_only || is_const_property)) { |
| 1247 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1248 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1249 | } |
| 1250 | // The property already exists without conflicting: Go to |
| 1251 | // the next declaration. |
| 1252 | continue; |
| 1253 | } |
| 1254 | } |
| 1255 | } else { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1256 | is_function_declaration = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | // Copy the function and update its context. Use it as value. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1258 | Handle<SharedFunctionInfo> shared = |
| 1259 | Handle<SharedFunctionInfo>::cast(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1260 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1261 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1262 | context, |
| 1263 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1264 | value = function; |
| 1265 | } |
| 1266 | |
| 1267 | LookupResult lookup; |
| 1268 | global->LocalLookup(*name, &lookup); |
| 1269 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1270 | // There's a local property that we need to overwrite because |
| 1271 | // we're either declaring a function or there's an interceptor |
| 1272 | // that claims the property is absent. |
| 1273 | // |
| 1274 | // Check for conflicting re-declarations. We cannot have |
| 1275 | // conflicting types in case of intercepted properties because |
| 1276 | // they are absent. |
| 1277 | if (lookup.IsProperty() && |
| 1278 | (lookup.type() != INTERCEPTOR) && |
| 1279 | (lookup.IsReadOnly() || is_const_property)) { |
| 1280 | const char* type = (lookup.IsReadOnly()) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1281 | return ThrowRedeclarationError(isolate, type, name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1282 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1283 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1284 | // Compute the property attributes. According to ECMA-262, section |
| 1285 | // 13, page 71, the property must be read-only and |
| 1286 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1287 | // property as read-only, so we don't either. |
| 1288 | int attr = NONE; |
| 1289 | if ((flags & kDeclareGlobalsEvalFlag) == 0) { |
| 1290 | attr |= DONT_DELETE; |
| 1291 | } |
| 1292 | bool is_native = (flags & kDeclareGlobalsNativeFlag) != 0; |
| 1293 | if (is_const_property || (is_native && is_function_declaration)) { |
| 1294 | attr |= READ_ONLY; |
| 1295 | } |
| 1296 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1297 | // Safari does not allow the invocation of callback setters for |
| 1298 | // function declarations. To mimic this behavior, we do not allow |
| 1299 | // the invocation of setters for function values. This makes a |
| 1300 | // difference for global functions with the same names as event |
| 1301 | // handlers such as "function onload() {}". Firefox does call the |
| 1302 | // onload setter in those case and Safari does not. We follow |
| 1303 | // Safari for compatibility. |
| 1304 | if (value->IsJSFunction()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1305 | // Do not change DONT_DELETE to false from true. |
| 1306 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1307 | attr |= lookup.GetAttributes() & DONT_DELETE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1308 | } |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1309 | PropertyAttributes attributes = static_cast<PropertyAttributes>(attr); |
| 1310 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1311 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1312 | SetLocalPropertyIgnoreAttributes(global, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1313 | name, |
| 1314 | value, |
| 1315 | attributes)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1316 | } else { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1317 | StrictModeFlag strict_mode = |
| 1318 | ((flags & kDeclareGlobalsStrictModeFlag) != 0) ? kStrictMode |
| 1319 | : kNonStrictMode; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1320 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1321 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1322 | name, |
| 1323 | value, |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1324 | static_cast<PropertyAttributes>(attr), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1325 | strict_mode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1328 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1329 | ASSERT(!isolate->has_pending_exception()); |
| 1330 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1334 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1335 | HandleScope scope(isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1336 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1338 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 1339 | Handle<String> name(String::cast(args[1])); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1340 | PropertyAttributes mode = static_cast<PropertyAttributes>(args.smi_at(2)); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1341 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1342 | Handle<Object> initial_value(args[3], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1344 | // Declarations are always done in a function or global context. |
| 1345 | context = Handle<Context>(context->declaration_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | |
| 1347 | int index; |
| 1348 | PropertyAttributes attributes; |
| 1349 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1350 | BindingFlags binding_flags; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1351 | Handle<Object> holder = |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1352 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (attributes != ABSENT) { |
| 1355 | // The name was declared before; check for conflicting |
| 1356 | // re-declarations: This is similar to the code in parser.cc in |
| 1357 | // the AstBuildingParser::Declare function. |
| 1358 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1359 | // Functions are not read-only. |
| 1360 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1361 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1362 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | // Initialize it if necessary. |
| 1366 | if (*initial_value != NULL) { |
| 1367 | if (index >= 0) { |
| 1368 | // The variable or constant context slot should always be in |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1369 | // the function context or the arguments object. |
| 1370 | if (holder->IsContext()) { |
| 1371 | ASSERT(holder.is_identical_to(context)); |
| 1372 | if (((attributes & READ_ONLY) == 0) || |
| 1373 | context->get(index)->IsTheHole()) { |
| 1374 | context->set(index, *initial_value); |
| 1375 | } |
| 1376 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1377 | // The holder is an arguments object. |
| 1378 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1379 | Handle<Object> result = SetElement(arguments, index, initial_value, |
| 1380 | kNonStrictMode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1381 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } else { |
| 1384 | // 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] | 1385 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1386 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1387 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1388 | SetProperty(context_ext, name, initial_value, |
| 1389 | mode, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | } |
| 1391 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1392 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1393 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1394 | // The property is not in the function context. It needs to be |
| 1395 | // "declared" in the function context's extension context, or in the |
| 1396 | // global context. |
| 1397 | Handle<JSObject> context_ext; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1398 | if (context->has_extension()) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1399 | // The function context's extension context exists - use it. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1400 | context_ext = Handle<JSObject>(JSObject::cast(context->extension())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1401 | } else { |
| 1402 | // The function context's extension context does not exists - allocate |
| 1403 | // it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1404 | context_ext = isolate->factory()->NewJSObject( |
| 1405 | isolate->context_extension_function()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1406 | // And store it in the extension slot. |
| 1407 | context->set_extension(*context_ext); |
| 1408 | } |
| 1409 | ASSERT(*context_ext != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1410 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1411 | // Declare the property by setting it to the initial value if provided, |
| 1412 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1413 | // constant declarations). |
| 1414 | ASSERT(!context_ext->HasLocalProperty(*name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1415 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1416 | if (*initial_value != NULL) value = initial_value; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1417 | // Declaring a const context slot is a conflicting declaration if |
| 1418 | // there is a callback with that name in a prototype. It is |
| 1419 | // allowed to introduce const variables in |
| 1420 | // JSContextExtensionObjects. They are treated specially in |
| 1421 | // SetProperty and no setters are invoked for those since they are |
| 1422 | // not real JSObjects. |
| 1423 | if (initial_value->IsTheHole() && |
| 1424 | !context_ext->IsJSContextExtensionObject()) { |
| 1425 | LookupResult lookup; |
| 1426 | context_ext->Lookup(*name, &lookup); |
| 1427 | if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1428 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1431 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1432 | SetProperty(context_ext, name, value, mode, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1433 | kNonStrictMode)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1436 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1440 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeVarGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | NoHandleAllocation nha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1442 | // args[0] == name |
| 1443 | // args[1] == strict_mode |
| 1444 | // args[2] == value (optional) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Determine if we need to assign to the variable if it already |
| 1447 | // exists (based on the number of arguments). |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1448 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1449 | bool assign = args.length() == 3; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1450 | |
| 1451 | CONVERT_ARG_CHECKED(String, name, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1452 | GlobalObject* global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1453 | RUNTIME_ASSERT(args[1]->IsSmi()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1454 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(args.smi_at(1)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1455 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1456 | |
| 1457 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1458 | // not be deletable. |
| 1459 | PropertyAttributes attributes = DONT_DELETE; |
| 1460 | |
| 1461 | // 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] | 1462 | // there, there is a property with this name in the prototype chain. |
| 1463 | // We follow Safari and Firefox behavior and only set the property |
| 1464 | // locally if there is an explicit initialization value that we have |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1465 | // to assign to the property. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1466 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1467 | // the whole chain of hidden prototypes to do a 'local' lookup. |
| 1468 | JSObject* real_holder = global; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1469 | LookupResult lookup; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1470 | while (true) { |
| 1471 | real_holder->LocalLookup(*name, &lookup); |
| 1472 | if (lookup.IsProperty()) { |
| 1473 | // Determine if this is a redeclaration of something read-only. |
| 1474 | if (lookup.IsReadOnly()) { |
| 1475 | // If we found readonly property on one of hidden prototypes, |
| 1476 | // just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1477 | if (real_holder != isolate->context()->global()) break; |
| 1478 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | // Determine if this is a redeclaration of an intercepted read-only |
| 1482 | // property and figure out if the property exists at all. |
| 1483 | bool found = true; |
| 1484 | PropertyType type = lookup.type(); |
| 1485 | if (type == INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1486 | HandleScope handle_scope(isolate); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1487 | Handle<JSObject> holder(real_holder); |
| 1488 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1489 | real_holder = *holder; |
| 1490 | if (intercepted == ABSENT) { |
| 1491 | // The interceptor claims the property isn't there. We need to |
| 1492 | // make sure to introduce it. |
| 1493 | found = false; |
| 1494 | } else if ((intercepted & READ_ONLY) != 0) { |
| 1495 | // The property is present, but read-only. Since we're trying to |
| 1496 | // overwrite it with a variable declaration we must throw a |
| 1497 | // re-declaration error. However if we found readonly property |
| 1498 | // on one of hidden prototypes, just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1499 | if (real_holder != isolate->context()->global()) break; |
| 1500 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | if (found && !assign) { |
| 1505 | // The global property is there and we're not assigning any value |
| 1506 | // to it. Just return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1507 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | // Assign the value (or undefined) to the property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1511 | Object* value = (assign) ? args[2] : isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1512 | return real_holder->SetProperty( |
| 1513 | &lookup, *name, value, attributes, strict_mode); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1514 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1515 | |
| 1516 | Object* proto = real_holder->GetPrototype(); |
| 1517 | if (!proto->IsJSObject()) |
| 1518 | break; |
| 1519 | |
| 1520 | if (!JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 1521 | break; |
| 1522 | |
| 1523 | real_holder = JSObject::cast(proto); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1526 | global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1527 | if (assign) { |
| 1528 | return global->SetProperty(*name, args[2], attributes, strict_mode); |
| 1529 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1530 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1534 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1535 | // All constants are declared with an initial value. The name |
| 1536 | // of the constant is the first argument and the initial value |
| 1537 | // is the second. |
| 1538 | RUNTIME_ASSERT(args.length() == 2); |
| 1539 | CONVERT_ARG_CHECKED(String, name, 0); |
| 1540 | Handle<Object> value = args.at<Object>(1); |
| 1541 | |
| 1542 | // Get the current global object from top. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1543 | GlobalObject* global = isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1544 | |
| 1545 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1546 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1547 | PropertyAttributes attributes = |
| 1548 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1549 | |
| 1550 | // Lookup the property locally in the global object. If it isn't |
| 1551 | // there, we add the property and take special precautions to always |
| 1552 | // add it as a local property even in case of callbacks in the |
| 1553 | // prototype chain (this rules out using SetProperty). |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1554 | // We use SetLocalPropertyIgnoreAttributes instead |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1555 | LookupResult lookup; |
| 1556 | global->LocalLookup(*name, &lookup); |
| 1557 | if (!lookup.IsProperty()) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1558 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1559 | *value, |
| 1560 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | // Determine if this is a redeclaration of something not |
| 1564 | // read-only. In case the result is hidden behind an interceptor we |
| 1565 | // need to ask it for the property attributes. |
| 1566 | if (!lookup.IsReadOnly()) { |
| 1567 | if (lookup.type() != INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1568 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | PropertyAttributes intercepted = global->GetPropertyAttribute(*name); |
| 1572 | |
| 1573 | // Throw re-declaration error if the intercepted property is present |
| 1574 | // but not read-only. |
| 1575 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1576 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | // Restore global object from context (in case of GC) and continue |
| 1580 | // with setting the value because the property is either absent or |
| 1581 | // read-only. We also have to do redo the lookup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1582 | HandleScope handle_scope(isolate); |
| 1583 | Handle<GlobalObject> global(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1584 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1585 | // 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] | 1586 | // property through an interceptor and only do it if it's |
| 1587 | // uninitialized, e.g. the hole. Nirk... |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1588 | // Passing non-strict mode because the property is writable. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1589 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1590 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1591 | name, |
| 1592 | value, |
| 1593 | attributes, |
| 1594 | kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1595 | return *value; |
| 1596 | } |
| 1597 | |
| 1598 | // Set the value, but only we're assigning the initial value to a |
| 1599 | // constant. For now, we determine this by checking if the |
| 1600 | // current value is the hole. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1601 | // Strict mode handling not needed (const disallowed in strict mode). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1602 | PropertyType type = lookup.type(); |
| 1603 | if (type == FIELD) { |
| 1604 | FixedArray* properties = global->properties(); |
| 1605 | int index = lookup.GetFieldIndex(); |
| 1606 | if (properties->get(index)->IsTheHole()) { |
| 1607 | properties->set(index, *value); |
| 1608 | } |
| 1609 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1610 | if (global->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1611 | global->SetNormalizedProperty(&lookup, *value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1612 | } |
| 1613 | } else { |
| 1614 | // Ignore re-initialization of constants that have already been |
| 1615 | // assigned a function value. |
| 1616 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1617 | } |
| 1618 | |
| 1619 | // Use the set value as the result of the operation. |
| 1620 | return *value; |
| 1621 | } |
| 1622 | |
| 1623 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1624 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1625 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1626 | ASSERT(args.length() == 3); |
| 1627 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1628 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1629 | ASSERT(!value->IsTheHole()); |
| 1630 | CONVERT_ARG_CHECKED(Context, context, 1); |
| 1631 | Handle<String> name(String::cast(args[2])); |
| 1632 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1633 | // Initializations are always done in a function or global context. |
| 1634 | context = Handle<Context>(context->declaration_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1635 | |
| 1636 | int index; |
| 1637 | PropertyAttributes attributes; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1638 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1639 | BindingFlags binding_flags; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1640 | Handle<Object> holder = |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1641 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1642 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1643 | // In most situations, the property introduced by the const |
| 1644 | // declaration should be present in the context extension object. |
| 1645 | // However, because declaration and initialization are separate, the |
| 1646 | // property might have been deleted (if it was introduced by eval) |
| 1647 | // before we reach the initialization point. |
| 1648 | // |
| 1649 | // Example: |
| 1650 | // |
| 1651 | // function f() { eval("delete x; const x;"); } |
| 1652 | // |
| 1653 | // In that case, the initialization behaves like a normal assignment |
| 1654 | // to property 'x'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1655 | if (index >= 0) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1656 | if (holder->IsContext()) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1657 | // Property was found in a context. Perform the assignment if we |
| 1658 | // found some non-constant or an uninitialized constant. |
| 1659 | Handle<Context> context = Handle<Context>::cast(holder); |
| 1660 | if ((attributes & READ_ONLY) == 0 || context->get(index)->IsTheHole()) { |
| 1661 | context->set(index, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1662 | } |
| 1663 | } else { |
| 1664 | // The holder is an arguments object. |
| 1665 | ASSERT((attributes & READ_ONLY) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1666 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1667 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1668 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1669 | SetElement(arguments, index, value, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1670 | } |
| 1671 | return *value; |
| 1672 | } |
| 1673 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1674 | // The property could not be found, we introduce it in the global |
| 1675 | // context. |
| 1676 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1677 | Handle<JSObject> global = Handle<JSObject>( |
| 1678 | isolate->context()->global()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1679 | // Strict mode not needed (const disallowed in strict mode). |
| 1680 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1681 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1682 | SetProperty(global, name, value, NONE, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1683 | return *value; |
| 1684 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1685 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1686 | // The property was present in a context extension object. |
| 1687 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1688 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1689 | if (*context_ext == context->extension()) { |
| 1690 | // This is the property that was introduced by the const |
| 1691 | // declaration. Set it if it hasn't been set before. NOTE: We |
| 1692 | // cannot use GetProperty() to get the current value as it |
| 1693 | // 'unholes' the value. |
| 1694 | LookupResult lookup; |
| 1695 | context_ext->LocalLookupRealNamedProperty(*name, &lookup); |
| 1696 | ASSERT(lookup.IsProperty()); // the property was declared |
| 1697 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1698 | |
| 1699 | PropertyType type = lookup.type(); |
| 1700 | if (type == FIELD) { |
| 1701 | FixedArray* properties = context_ext->properties(); |
| 1702 | int index = lookup.GetFieldIndex(); |
| 1703 | if (properties->get(index)->IsTheHole()) { |
| 1704 | properties->set(index, *value); |
| 1705 | } |
| 1706 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1707 | if (context_ext->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1708 | context_ext->SetNormalizedProperty(&lookup, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1709 | } |
| 1710 | } else { |
| 1711 | // We should not reach here. Any real, named property should be |
| 1712 | // either a field or a dictionary slot. |
| 1713 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } else { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1716 | // The property was found in a different context extension object. |
| 1717 | // Set it if it is not a read-only property. |
| 1718 | if ((attributes & READ_ONLY) == 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1719 | // Strict mode not needed (const disallowed in strict mode). |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1720 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1721 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1722 | SetProperty(context_ext, name, value, attributes, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1723 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1724 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1725 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1726 | return *value; |
| 1727 | } |
| 1728 | |
| 1729 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1730 | RUNTIME_FUNCTION(MaybeObject*, |
| 1731 | Runtime_OptimizeObjectForAddingMultipleProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1732 | HandleScope scope(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1733 | ASSERT(args.length() == 2); |
| 1734 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1735 | CONVERT_SMI_ARG_CHECKED(properties, 1); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1736 | if (object->HasFastProperties()) { |
| 1737 | NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
| 1738 | } |
| 1739 | return *object; |
| 1740 | } |
| 1741 | |
| 1742 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1743 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExec) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1744 | HandleScope scope(isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1745 | ASSERT(args.length() == 4); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1746 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1747 | CONVERT_ARG_CHECKED(String, subject, 1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1748 | // 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] | 1749 | // length of a string, i.e. it is always a Smi. We check anyway for security. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1750 | CONVERT_SMI_ARG_CHECKED(index, 2); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1751 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1752 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1753 | RUNTIME_ASSERT(index >= 0); |
| 1754 | RUNTIME_ASSERT(index <= subject->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1755 | isolate->counters()->regexp_entry_runtime()->Increment(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1756 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1757 | subject, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1758 | index, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1759 | last_match_info); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1760 | if (result.is_null()) return Failure::Exception(); |
| 1761 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1765 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpConstructResult) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1766 | ASSERT(args.length() == 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1767 | CONVERT_SMI_ARG_CHECKED(elements_count, 0); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1768 | if (elements_count < 0 || |
| 1769 | elements_count > FixedArray::kMaxLength || |
| 1770 | !Smi::IsValid(elements_count)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1771 | return isolate->ThrowIllegalOperation(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1772 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1773 | Object* new_object; |
| 1774 | { MaybeObject* maybe_new_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1775 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1776 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1777 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1778 | FixedArray* elements = FixedArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1779 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1780 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1781 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1782 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1783 | { |
| 1784 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1785 | HandleScope scope(isolate); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1786 | reinterpret_cast<HeapObject*>(new_object)-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1787 | set_map(isolate->global_context()->regexp_result_map()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1788 | } |
| 1789 | JSArray* array = JSArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1790 | array->set_properties(isolate->heap()->empty_fixed_array()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1791 | array->set_elements(elements); |
| 1792 | array->set_length(Smi::FromInt(elements_count)); |
| 1793 | // Write in-object properties after the length of the array. |
| 1794 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1795 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1796 | return array; |
| 1797 | } |
| 1798 | |
| 1799 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1800 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpInitializeObject) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1801 | AssertNoAllocation no_alloc; |
| 1802 | ASSERT(args.length() == 5); |
| 1803 | CONVERT_CHECKED(JSRegExp, regexp, args[0]); |
| 1804 | CONVERT_CHECKED(String, source, args[1]); |
| 1805 | |
| 1806 | Object* global = args[2]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1807 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1808 | |
| 1809 | Object* ignoreCase = args[3]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1810 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1811 | |
| 1812 | Object* multiline = args[4]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1813 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1814 | |
| 1815 | Map* map = regexp->map(); |
| 1816 | Object* constructor = map->constructor(); |
| 1817 | if (constructor->IsJSFunction() && |
| 1818 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1819 | // If we still have the original map, set in-object properties directly. |
| 1820 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
| 1821 | // TODO(lrn): Consider skipping write barrier on booleans as well. |
| 1822 | // Both true and false should be in oldspace at all times. |
| 1823 | regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, global); |
| 1824 | regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, ignoreCase); |
| 1825 | regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, multiline); |
| 1826 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1827 | Smi::FromInt(0), |
| 1828 | SKIP_WRITE_BARRIER); |
| 1829 | return regexp; |
| 1830 | } |
| 1831 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1832 | // Map has changed, so use generic, but slower, method. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1833 | PropertyAttributes final = |
| 1834 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1835 | PropertyAttributes writable = |
| 1836 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1837 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1838 | MaybeObject* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1839 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1840 | source, |
| 1841 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1842 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1843 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1844 | global, |
| 1845 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1846 | ASSERT(!result->IsFailure()); |
| 1847 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1848 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1849 | ignoreCase, |
| 1850 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1851 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1852 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1853 | multiline, |
| 1854 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1855 | ASSERT(!result->IsFailure()); |
| 1856 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1857 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1858 | Smi::FromInt(0), |
| 1859 | writable); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1860 | ASSERT(!result->IsFailure()); |
| 1861 | USE(result); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1862 | return regexp; |
| 1863 | } |
| 1864 | |
| 1865 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1866 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinishArrayPrototypeSetup) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1867 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1868 | ASSERT(args.length() == 1); |
| 1869 | CONVERT_ARG_CHECKED(JSArray, prototype, 0); |
| 1870 | // This is necessary to enable fast checks for absence of elements |
| 1871 | // on Array.prototype and below. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1872 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1873 | return Smi::FromInt(0); |
| 1874 | } |
| 1875 | |
| 1876 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1877 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1878 | Handle<JSObject> holder, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1879 | const char* name, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1880 | Builtins::Name builtin_name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1881 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1882 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1883 | Handle<JSFunction> optimized = |
| 1884 | isolate->factory()->NewFunction(key, |
| 1885 | JS_OBJECT_TYPE, |
| 1886 | JSObject::kHeaderSize, |
| 1887 | code, |
| 1888 | false); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1889 | optimized->shared()->DontAdaptArguments(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1890 | SetProperty(holder, key, optimized, NONE, kStrictMode); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1891 | return optimized; |
| 1892 | } |
| 1893 | |
| 1894 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1895 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1896 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1897 | ASSERT(args.length() == 1); |
| 1898 | CONVERT_ARG_CHECKED(JSObject, holder, 0); |
| 1899 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1900 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
| 1901 | InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
| 1902 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 1903 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 1904 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 1905 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
| 1906 | InstallBuiltin(isolate, holder, "concat", Builtins::kArrayConcat); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1907 | |
| 1908 | return *holder; |
| 1909 | } |
| 1910 | |
| 1911 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1912 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetDefaultReceiver) { |
| 1913 | NoHandleAllocation handle_free; |
| 1914 | ASSERT(args.length() == 1); |
| 1915 | CONVERT_CHECKED(JSFunction, function, args[0]); |
| 1916 | SharedFunctionInfo* shared = function->shared(); |
| 1917 | if (shared->native() || shared->strict_mode()) { |
| 1918 | return isolate->heap()->undefined_value(); |
| 1919 | } |
| 1920 | // Returns undefined for strict or native functions, or |
| 1921 | // the associated global receiver for "normal" functions. |
| 1922 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1923 | Context* global_context = |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1924 | function->context()->global()->global_context(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1925 | return global_context->global()->global_receiver(); |
| 1926 | } |
| 1927 | |
| 1928 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MaterializeRegExpLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1930 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1931 | ASSERT(args.length() == 4); |
| 1932 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1933 | int index = args.smi_at(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1934 | Handle<String> pattern = args.at<String>(2); |
| 1935 | Handle<String> flags = args.at<String>(3); |
| 1936 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1937 | // Get the RegExp function from the context in the literals array. |
| 1938 | // This is the RegExp function from the context in which the |
| 1939 | // function was created. We do not use the RegExp function from the |
| 1940 | // current global context because this might be the RegExp function |
| 1941 | // 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] | 1942 | Handle<JSFunction> constructor = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1943 | Handle<JSFunction>( |
| 1944 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1945 | // Compute the regular expression literal. |
| 1946 | bool has_pending_exception; |
| 1947 | Handle<Object> regexp = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1948 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1949 | &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | if (has_pending_exception) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1951 | ASSERT(isolate->has_pending_exception()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1952 | return Failure::Exception(); |
| 1953 | } |
| 1954 | literals->set(index, *regexp); |
| 1955 | return *regexp; |
| 1956 | } |
| 1957 | |
| 1958 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1959 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1960 | NoHandleAllocation ha; |
| 1961 | ASSERT(args.length() == 1); |
| 1962 | |
| 1963 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1964 | return f->shared()->name(); |
| 1965 | } |
| 1966 | |
| 1967 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1968 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetName) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1969 | NoHandleAllocation ha; |
| 1970 | ASSERT(args.length() == 2); |
| 1971 | |
| 1972 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1973 | CONVERT_CHECKED(String, name, args[1]); |
| 1974 | f->shared()->set_name(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1975 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1979 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionNameShouldPrintAsAnonymous) { |
| 1980 | NoHandleAllocation ha; |
| 1981 | ASSERT(args.length() == 1); |
| 1982 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1983 | return isolate->heap()->ToBoolean( |
| 1984 | f->shared()->name_should_print_as_anonymous()); |
| 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionMarkNameShouldPrintAsAnonymous) { |
| 1989 | NoHandleAllocation ha; |
| 1990 | ASSERT(args.length() == 1); |
| 1991 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1992 | f->shared()->set_name_should_print_as_anonymous(true); |
| 1993 | return isolate->heap()->undefined_value(); |
| 1994 | } |
| 1995 | |
| 1996 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1997 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetBound) { |
| 1998 | HandleScope scope(isolate); |
| 1999 | ASSERT(args.length() == 1); |
| 2000 | |
| 2001 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2002 | fun->shared()->set_bound(true); |
| 2003 | return isolate->heap()->undefined_value(); |
| 2004 | } |
| 2005 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2006 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +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 | Object* obj = f->RemovePrototype(); |
| 2012 | if (obj->IsFailure()) return obj; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2013 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2014 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2018 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2019 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2020 | ASSERT(args.length() == 1); |
| 2021 | |
| 2022 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2023 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 2024 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2025 | |
| 2026 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 2027 | } |
| 2028 | |
| 2029 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2030 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetSourceCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2031 | NoHandleAllocation ha; |
| 2032 | ASSERT(args.length() == 1); |
| 2033 | |
| 2034 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 2035 | return f->shared()->GetSourceCode(); |
| 2036 | } |
| 2037 | |
| 2038 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2039 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScriptSourcePosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2040 | NoHandleAllocation ha; |
| 2041 | ASSERT(args.length() == 1); |
| 2042 | |
| 2043 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2044 | int pos = fun->shared()->start_position(); |
| 2045 | return Smi::FromInt(pos); |
| 2046 | } |
| 2047 | |
| 2048 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2049 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetPositionForOffset) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2050 | ASSERT(args.length() == 2); |
| 2051 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2052 | CONVERT_CHECKED(Code, code, args[0]); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2053 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 2054 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2055 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 2056 | |
| 2057 | Address pc = code->address() + offset; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2058 | return Smi::FromInt(code->SourcePosition(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2062 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetInstanceClassName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2063 | NoHandleAllocation ha; |
| 2064 | ASSERT(args.length() == 2); |
| 2065 | |
| 2066 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2067 | CONVERT_CHECKED(String, name, args[1]); |
| 2068 | fun->SetInstanceClassName(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2069 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2073 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2074 | NoHandleAllocation ha; |
| 2075 | ASSERT(args.length() == 2); |
| 2076 | |
| 2077 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2078 | CONVERT_CHECKED(Smi, length, args[1]); |
| 2079 | fun->shared()->set_length(length->value()); |
| 2080 | return length; |
| 2081 | } |
| 2082 | |
| 2083 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2084 | // Creates a local, readonly, property called length with the correct |
| 2085 | // length (when read by the user). This effectively overwrites the |
| 2086 | // interceptor used to normally provide the length. |
| 2087 | RUNTIME_FUNCTION(MaybeObject*, Runtime_BoundFunctionSetLength) { |
| 2088 | NoHandleAllocation ha; |
| 2089 | ASSERT(args.length() == 2); |
| 2090 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 2091 | CONVERT_CHECKED(Smi, length, args[1]); |
| 2092 | MaybeObject* maybe_name = |
| 2093 | isolate->heap()->AllocateStringFromAscii(CStrVector("length")); |
| 2094 | String* name; |
| 2095 | if (!maybe_name->To(&name)) return maybe_name; |
| 2096 | PropertyAttributes attr = |
| 2097 | static_cast<PropertyAttributes>(DONT_DELETE | DONT_ENUM | READ_ONLY); |
| 2098 | return fun->AddProperty(name, length, attr, kNonStrictMode); |
| 2099 | } |
| 2100 | |
| 2101 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2102 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetPrototype) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2103 | NoHandleAllocation ha; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2104 | ASSERT(args.length() == 2); |
| 2105 | |
| 2106 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2107 | ASSERT(fun->should_have_prototype()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2108 | Object* obj; |
| 2109 | { MaybeObject* maybe_obj = |
| 2110 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 2111 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2112 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2113 | return args[0]; // return TOS |
| 2114 | } |
| 2115 | |
| 2116 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 2117 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetReadOnlyPrototype) { |
| 2118 | NoHandleAllocation ha; |
| 2119 | RUNTIME_ASSERT(args.length() == 1); |
| 2120 | CONVERT_CHECKED(JSFunction, function, args[0]); |
| 2121 | |
| 2122 | MaybeObject* maybe_name = |
| 2123 | isolate->heap()->AllocateStringFromAscii(CStrVector("prototype")); |
| 2124 | String* name; |
| 2125 | if (!maybe_name->To(&name)) return maybe_name; |
| 2126 | |
| 2127 | if (function->HasFastProperties()) { |
| 2128 | // Construct a new field descriptor with updated attributes. |
| 2129 | DescriptorArray* instance_desc = function->map()->instance_descriptors(); |
| 2130 | int index = instance_desc->Search(name); |
| 2131 | ASSERT(index != DescriptorArray::kNotFound); |
| 2132 | PropertyDetails details(instance_desc->GetDetails(index)); |
| 2133 | CallbacksDescriptor new_desc(name, |
| 2134 | instance_desc->GetValue(index), |
| 2135 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2136 | details.index()); |
| 2137 | // Construct a new field descriptors array containing the new descriptor. |
| 2138 | Object* descriptors_unchecked; |
| 2139 | { MaybeObject* maybe_descriptors_unchecked = |
| 2140 | instance_desc->CopyInsert(&new_desc, REMOVE_TRANSITIONS); |
| 2141 | if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { |
| 2142 | return maybe_descriptors_unchecked; |
| 2143 | } |
| 2144 | } |
| 2145 | DescriptorArray* new_descriptors = |
| 2146 | DescriptorArray::cast(descriptors_unchecked); |
| 2147 | // Create a new map featuring the new field descriptors array. |
| 2148 | Object* map_unchecked; |
| 2149 | { MaybeObject* maybe_map_unchecked = function->map()->CopyDropDescriptors(); |
| 2150 | if (!maybe_map_unchecked->ToObject(&map_unchecked)) { |
| 2151 | return maybe_map_unchecked; |
| 2152 | } |
| 2153 | } |
| 2154 | Map* new_map = Map::cast(map_unchecked); |
| 2155 | new_map->set_instance_descriptors(new_descriptors); |
| 2156 | function->set_map(new_map); |
| 2157 | } else { // Dictionary properties. |
| 2158 | // Directly manipulate the property details. |
| 2159 | int entry = function->property_dictionary()->FindEntry(name); |
| 2160 | ASSERT(entry != StringDictionary::kNotFound); |
| 2161 | PropertyDetails details = function->property_dictionary()->DetailsAt(entry); |
| 2162 | PropertyDetails new_details( |
| 2163 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2164 | details.type(), |
| 2165 | details.index()); |
| 2166 | function->property_dictionary()->DetailsAtPut(entry, new_details); |
| 2167 | } |
| 2168 | return function; |
| 2169 | } |
| 2170 | |
| 2171 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2172 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsAPIFunction) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2173 | NoHandleAllocation ha; |
| 2174 | ASSERT(args.length() == 1); |
| 2175 | |
| 2176 | CONVERT_CHECKED(JSFunction, f, args[0]); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2177 | return isolate->heap()->ToBoolean(f->shared()->IsApiFunction()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2180 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2181 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsBuiltin) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 2182 | NoHandleAllocation ha; |
| 2183 | ASSERT(args.length() == 1); |
| 2184 | |
| 2185 | CONVERT_CHECKED(JSFunction, f, args[0]); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2186 | return isolate->heap()->ToBoolean(f->IsBuiltin()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2189 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2190 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetCode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2191 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2192 | ASSERT(args.length() == 2); |
| 2193 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2194 | CONVERT_ARG_CHECKED(JSFunction, target, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | Handle<Object> code = args.at<Object>(1); |
| 2196 | |
| 2197 | Handle<Context> context(target->context()); |
| 2198 | |
| 2199 | if (!code->IsNull()) { |
| 2200 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 2201 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2202 | Handle<SharedFunctionInfo> shared(fun->shared()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2203 | |
| 2204 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2205 | return Failure::Exception(); |
| 2206 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2207 | // Since we don't store the source for this we should never |
| 2208 | // optimize this. |
| 2209 | shared->code()->set_optimizable(false); |
| 2210 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2211 | // Set the code, scope info, formal parameter count, |
| 2212 | // and the length of the target function. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2213 | target->shared()->set_code(shared->code()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2214 | target->ReplaceCode(shared->code()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2215 | target->shared()->set_scope_info(shared->scope_info()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2216 | target->shared()->set_length(shared->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2217 | target->shared()->set_formal_parameter_count( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2218 | shared->formal_parameter_count()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2219 | // Set the source code of the target function to undefined. |
| 2220 | // SetCode is only used for built-in constructors like String, |
| 2221 | // Array, and Object, and some web code |
| 2222 | // doesn't like seeing source code for constructors. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2223 | target->shared()->set_script(isolate->heap()->undefined_value()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2224 | target->shared()->code()->set_optimizable(false); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2225 | // Clear the optimization hints related to the compiled code as these are no |
| 2226 | // longer valid when the code is overwritten. |
| 2227 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2228 | context = Handle<Context>(fun->context()); |
| 2229 | |
| 2230 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2231 | // cross context contamination. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2232 | int number_of_literals = fun->NumberOfLiterals(); |
| 2233 | Handle<FixedArray> literals = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2234 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2235 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2236 | // Insert the object, regexp and array functions in the literals |
| 2237 | // array prefix. These are the functions that will be used when |
| 2238 | // creating object, regexp and array literals. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2239 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2240 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2241 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2242 | // It's okay to skip the write barrier here because the literals |
| 2243 | // are guaranteed to be in old space. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2244 | target->set_literals(*literals, SKIP_WRITE_BARRIER); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2245 | target->set_next_function_link(isolate->heap()->undefined_value()); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 2246 | |
| 2247 | if (isolate->logger()->is_logging() || CpuProfiler::is_profiling(isolate)) { |
| 2248 | isolate->logger()->LogExistingFunction( |
| 2249 | shared, Handle<Code>(shared->code())); |
| 2250 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | target->set_context(*context); |
| 2254 | return *target; |
| 2255 | } |
| 2256 | |
| 2257 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2258 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2259 | HandleScope scope(isolate); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2260 | ASSERT(args.length() == 2); |
| 2261 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2262 | CONVERT_SMI_ARG_CHECKED(num, 1); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2263 | RUNTIME_ASSERT(num >= 0); |
| 2264 | SetExpectedNofProperties(function, num); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2265 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2269 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2270 | Object* char_code) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2271 | uint32_t code; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2272 | if (char_code->ToArrayIndex(&code)) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2273 | if (code <= 0xffff) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2274 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2275 | } |
| 2276 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2277 | return isolate->heap()->empty_string(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2281 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCharCodeAt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2282 | NoHandleAllocation ha; |
| 2283 | ASSERT(args.length() == 2); |
| 2284 | |
| 2285 | CONVERT_CHECKED(String, subject, args[0]); |
| 2286 | Object* index = args[1]; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2287 | RUNTIME_ASSERT(index->IsNumber()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2288 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2289 | uint32_t i = 0; |
| 2290 | if (index->IsSmi()) { |
| 2291 | int value = Smi::cast(index)->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2292 | if (value < 0) return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2293 | i = value; |
| 2294 | } else { |
| 2295 | ASSERT(index->IsHeapNumber()); |
| 2296 | double value = HeapNumber::cast(index)->value(); |
| 2297 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
kasperl@chromium.org | 74e4e5e | 2010-01-25 10:15:52 +0000 | [diff] [blame] | 2298 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2299 | |
| 2300 | // Flatten the string. If someone wants to get a char at an index |
| 2301 | // in a cons string, it is likely that more indices will be |
| 2302 | // accessed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2303 | Object* flat; |
| 2304 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2305 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2306 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2307 | subject = String::cast(flat); |
| 2308 | |
| 2309 | if (i >= static_cast<uint32_t>(subject->length())) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2310 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | return Smi::FromInt(subject->Get(i)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2317 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CharFromCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2318 | NoHandleAllocation ha; |
| 2319 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2320 | return CharFromCode(isolate, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2323 | |
| 2324 | class FixedArrayBuilder { |
| 2325 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2326 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2327 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2328 | length_(0) { |
| 2329 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2330 | // extend the array will work. |
| 2331 | ASSERT(initial_capacity > 0); |
| 2332 | } |
| 2333 | |
| 2334 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2335 | : array_(backing_store), |
| 2336 | length_(0) { |
| 2337 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2338 | // extend the array will work. |
| 2339 | ASSERT(backing_store->length() > 0); |
| 2340 | } |
| 2341 | |
| 2342 | bool HasCapacity(int elements) { |
| 2343 | int length = array_->length(); |
| 2344 | int required_length = length_ + elements; |
| 2345 | return (length >= required_length); |
| 2346 | } |
| 2347 | |
| 2348 | void EnsureCapacity(int elements) { |
| 2349 | int length = array_->length(); |
| 2350 | int required_length = length_ + elements; |
| 2351 | if (length < required_length) { |
| 2352 | int new_length = length; |
| 2353 | do { |
| 2354 | new_length *= 2; |
| 2355 | } while (new_length < required_length); |
| 2356 | Handle<FixedArray> extended_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2357 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2358 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2359 | array_ = extended_array; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | void Add(Object* value) { |
| 2364 | ASSERT(length_ < capacity()); |
| 2365 | array_->set(length_, value); |
| 2366 | length_++; |
| 2367 | } |
| 2368 | |
| 2369 | void Add(Smi* value) { |
| 2370 | ASSERT(length_ < capacity()); |
| 2371 | array_->set(length_, value); |
| 2372 | length_++; |
| 2373 | } |
| 2374 | |
| 2375 | Handle<FixedArray> array() { |
| 2376 | return array_; |
| 2377 | } |
| 2378 | |
| 2379 | int length() { |
| 2380 | return length_; |
| 2381 | } |
| 2382 | |
| 2383 | int capacity() { |
| 2384 | return array_->length(); |
| 2385 | } |
| 2386 | |
| 2387 | Handle<JSArray> ToJSArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2388 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2389 | result_array->set_length(Smi::FromInt(length_)); |
| 2390 | return result_array; |
| 2391 | } |
| 2392 | |
| 2393 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
| 2394 | target_array->set_elements(*array_); |
| 2395 | target_array->set_length(Smi::FromInt(length_)); |
| 2396 | return target_array; |
| 2397 | } |
| 2398 | |
| 2399 | private: |
| 2400 | Handle<FixedArray> array_; |
| 2401 | int length_; |
| 2402 | }; |
| 2403 | |
| 2404 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2405 | // Forward declarations. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2406 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2407 | const int kStringBuilderConcatHelperPositionBits = 19; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2408 | |
| 2409 | template <typename schar> |
| 2410 | static inline void StringBuilderConcatHelper(String*, |
| 2411 | schar*, |
| 2412 | FixedArray*, |
| 2413 | int); |
| 2414 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2415 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2416 | StringBuilderSubstringLength; |
| 2417 | typedef BitField<int, |
| 2418 | kStringBuilderConcatHelperLengthBits, |
| 2419 | kStringBuilderConcatHelperPositionBits> |
| 2420 | StringBuilderSubstringPosition; |
| 2421 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2422 | |
| 2423 | class ReplacementStringBuilder { |
| 2424 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2425 | ReplacementStringBuilder(Heap* heap, |
| 2426 | Handle<String> subject, |
| 2427 | int estimated_part_count) |
| 2428 | : heap_(heap), |
| 2429 | array_builder_(heap->isolate(), estimated_part_count), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2430 | subject_(subject), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2431 | character_count_(0), |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2432 | is_ascii_(subject->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2433 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2434 | // extend the array will work. |
| 2435 | ASSERT(estimated_part_count > 0); |
| 2436 | } |
| 2437 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2438 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2439 | int from, |
| 2440 | int to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2441 | ASSERT(from >= 0); |
| 2442 | int length = to - from; |
| 2443 | ASSERT(length > 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2444 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2445 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2446 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2447 | StringBuilderSubstringPosition::encode(from); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2448 | builder->Add(Smi::FromInt(encoded_slice)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2449 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2450 | // Otherwise encode as two smis. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2451 | builder->Add(Smi::FromInt(-length)); |
| 2452 | builder->Add(Smi::FromInt(from)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2453 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | |
| 2457 | void EnsureCapacity(int elements) { |
| 2458 | array_builder_.EnsureCapacity(elements); |
| 2459 | } |
| 2460 | |
| 2461 | |
| 2462 | void AddSubjectSlice(int from, int to) { |
| 2463 | AddSubjectSlice(&array_builder_, from, to); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2464 | IncrementCharacterCount(to - from); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | |
| 2468 | void AddString(Handle<String> string) { |
| 2469 | int length = string->length(); |
| 2470 | ASSERT(length > 0); |
| 2471 | AddElement(*string); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2472 | if (!string->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2473 | is_ascii_ = false; |
| 2474 | } |
| 2475 | IncrementCharacterCount(length); |
| 2476 | } |
| 2477 | |
| 2478 | |
| 2479 | Handle<String> ToString() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2480 | if (array_builder_.length() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2481 | return heap_->isolate()->factory()->empty_string(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | Handle<String> joined_string; |
| 2485 | if (is_ascii_) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2486 | Handle<SeqAsciiString> seq = NewRawAsciiString(character_count_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2487 | AssertNoAllocation no_alloc; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2488 | char* char_buffer = seq->GetChars(); |
| 2489 | StringBuilderConcatHelper(*subject_, |
| 2490 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2491 | *array_builder_.array(), |
| 2492 | array_builder_.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2493 | joined_string = Handle<String>::cast(seq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2494 | } else { |
| 2495 | // Non-ASCII. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2496 | Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2497 | AssertNoAllocation no_alloc; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2498 | uc16* char_buffer = seq->GetChars(); |
| 2499 | StringBuilderConcatHelper(*subject_, |
| 2500 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2501 | *array_builder_.array(), |
| 2502 | array_builder_.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2503 | joined_string = Handle<String>::cast(seq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2504 | } |
| 2505 | return joined_string; |
| 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | void IncrementCharacterCount(int by) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2510 | if (character_count_ > String::kMaxLength - by) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2511 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2512 | } |
| 2513 | character_count_ += by; |
| 2514 | } |
| 2515 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2516 | Handle<JSArray> GetParts() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2517 | return array_builder_.ToJSArray(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2518 | } |
kmillikin@chromium.org | d982519 | 2010-03-30 08:36:16 +0000 | [diff] [blame] | 2519 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2520 | private: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2521 | Handle<SeqAsciiString> NewRawAsciiString(int length) { |
| 2522 | return heap_->isolate()->factory()->NewRawAsciiString(length); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2526 | Handle<SeqTwoByteString> NewRawTwoByteString(int length) { |
| 2527 | return heap_->isolate()->factory()->NewRawTwoByteString(length); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | |
| 2531 | void AddElement(Object* element) { |
| 2532 | ASSERT(element->IsSmi() || element->IsString()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2533 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2534 | array_builder_.Add(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2537 | Heap* heap_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2538 | FixedArrayBuilder array_builder_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2539 | Handle<String> subject_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2540 | int character_count_; |
| 2541 | bool is_ascii_; |
| 2542 | }; |
| 2543 | |
| 2544 | |
| 2545 | class CompiledReplacement { |
| 2546 | public: |
| 2547 | CompiledReplacement() |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2548 | : parts_(1), replacement_substrings_(0), simple_hint_(false) {} |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2549 | |
| 2550 | void Compile(Handle<String> replacement, |
| 2551 | int capture_count, |
| 2552 | int subject_length); |
| 2553 | |
| 2554 | void Apply(ReplacementStringBuilder* builder, |
| 2555 | int match_from, |
| 2556 | int match_to, |
| 2557 | Handle<JSArray> last_match_info); |
| 2558 | |
| 2559 | // Number of distinct parts of the replacement pattern. |
| 2560 | int parts() { |
| 2561 | return parts_.length(); |
| 2562 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2563 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2564 | bool simple_hint() { |
| 2565 | return simple_hint_; |
| 2566 | } |
| 2567 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2568 | private: |
| 2569 | enum PartType { |
| 2570 | SUBJECT_PREFIX = 1, |
| 2571 | SUBJECT_SUFFIX, |
| 2572 | SUBJECT_CAPTURE, |
| 2573 | REPLACEMENT_SUBSTRING, |
| 2574 | REPLACEMENT_STRING, |
| 2575 | |
| 2576 | NUMBER_OF_PART_TYPES |
| 2577 | }; |
| 2578 | |
| 2579 | struct ReplacementPart { |
| 2580 | static inline ReplacementPart SubjectMatch() { |
| 2581 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2582 | } |
| 2583 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2584 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2585 | } |
| 2586 | static inline ReplacementPart SubjectPrefix() { |
| 2587 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2588 | } |
| 2589 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2590 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2591 | } |
| 2592 | static inline ReplacementPart ReplacementString() { |
| 2593 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2594 | } |
| 2595 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2596 | ASSERT(from >= 0); |
| 2597 | ASSERT(to > from); |
| 2598 | return ReplacementPart(-from, to); |
| 2599 | } |
| 2600 | |
| 2601 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2602 | // the replacement pattern, otherwise it's a value from PartType. |
| 2603 | ReplacementPart(int tag, int data) |
| 2604 | : tag(tag), data(data) { |
| 2605 | // Must be non-positive or a PartType value. |
| 2606 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2607 | } |
| 2608 | // Either a value of PartType or a non-positive number that is |
| 2609 | // the negation of an index into the replacement string. |
| 2610 | int tag; |
| 2611 | // The data value's interpretation depends on the value of tag: |
| 2612 | // tag == SUBJECT_PREFIX || |
| 2613 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2614 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2615 | // tag == REPLACEMENT_SUBSTRING || |
| 2616 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2617 | // of the replacement string. |
| 2618 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2619 | // string ranging over -tag .. data. |
| 2620 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2621 | // substring objects. |
| 2622 | int data; |
| 2623 | }; |
| 2624 | |
| 2625 | template<typename Char> |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2626 | static bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2627 | Vector<Char> characters, |
| 2628 | int capture_count, |
| 2629 | int subject_length) { |
| 2630 | int length = characters.length(); |
| 2631 | int last = 0; |
| 2632 | for (int i = 0; i < length; i++) { |
| 2633 | Char c = characters[i]; |
| 2634 | if (c == '$') { |
| 2635 | int next_index = i + 1; |
| 2636 | if (next_index == length) { // No next character! |
| 2637 | break; |
| 2638 | } |
| 2639 | Char c2 = characters[next_index]; |
| 2640 | switch (c2) { |
| 2641 | case '$': |
| 2642 | if (i > last) { |
| 2643 | // There is a substring before. Include the first "$". |
| 2644 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2645 | last = next_index + 1; // Continue after the second "$". |
| 2646 | } else { |
| 2647 | // Let the next substring start with the second "$". |
| 2648 | last = next_index; |
| 2649 | } |
| 2650 | i = next_index; |
| 2651 | break; |
| 2652 | case '`': |
| 2653 | if (i > last) { |
| 2654 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2655 | } |
| 2656 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2657 | i = next_index; |
| 2658 | last = i + 1; |
| 2659 | break; |
| 2660 | case '\'': |
| 2661 | if (i > last) { |
| 2662 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2663 | } |
| 2664 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2665 | i = next_index; |
| 2666 | last = i + 1; |
| 2667 | break; |
| 2668 | case '&': |
| 2669 | if (i > last) { |
| 2670 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2671 | } |
| 2672 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2673 | i = next_index; |
| 2674 | last = i + 1; |
| 2675 | break; |
| 2676 | case '0': |
| 2677 | case '1': |
| 2678 | case '2': |
| 2679 | case '3': |
| 2680 | case '4': |
| 2681 | case '5': |
| 2682 | case '6': |
| 2683 | case '7': |
| 2684 | case '8': |
| 2685 | case '9': { |
| 2686 | int capture_ref = c2 - '0'; |
| 2687 | if (capture_ref > capture_count) { |
| 2688 | i = next_index; |
| 2689 | continue; |
| 2690 | } |
| 2691 | int second_digit_index = next_index + 1; |
| 2692 | if (second_digit_index < length) { |
| 2693 | // Peek ahead to see if we have two digits. |
| 2694 | Char c3 = characters[second_digit_index]; |
| 2695 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2696 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2697 | if (double_digit_ref <= capture_count) { |
| 2698 | next_index = second_digit_index; |
| 2699 | capture_ref = double_digit_ref; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | if (capture_ref > 0) { |
| 2704 | if (i > last) { |
| 2705 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2706 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2707 | ASSERT(capture_ref <= capture_count); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2708 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2709 | last = next_index + 1; |
| 2710 | } |
| 2711 | i = next_index; |
| 2712 | break; |
| 2713 | } |
| 2714 | default: |
| 2715 | i = next_index; |
| 2716 | break; |
| 2717 | } |
| 2718 | } |
| 2719 | } |
| 2720 | if (length > last) { |
| 2721 | if (last == 0) { |
| 2722 | parts->Add(ReplacementPart::ReplacementString()); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2723 | return true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2724 | } else { |
| 2725 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2726 | } |
| 2727 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2728 | return false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | ZoneList<ReplacementPart> parts_; |
| 2732 | ZoneList<Handle<String> > replacement_substrings_; |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2733 | bool simple_hint_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2734 | }; |
| 2735 | |
| 2736 | |
| 2737 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2738 | int capture_count, |
| 2739 | int subject_length) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2740 | { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2741 | AssertNoAllocation no_alloc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2742 | String::FlatContent content = replacement->GetFlatContent(); |
| 2743 | ASSERT(content.IsFlat()); |
| 2744 | if (content.IsAscii()) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2745 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2746 | content.ToAsciiVector(), |
| 2747 | capture_count, |
| 2748 | subject_length); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2749 | } else { |
| 2750 | ASSERT(content.IsTwoByte()); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2751 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2752 | content.ToUC16Vector(), |
| 2753 | capture_count, |
| 2754 | subject_length); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2755 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2756 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2757 | Isolate* isolate = replacement->GetIsolate(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2758 | // Find substrings of replacement string and create them as String objects. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2759 | int substring_index = 0; |
| 2760 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2761 | int tag = parts_[i].tag; |
| 2762 | if (tag <= 0) { // A replacement string slice. |
| 2763 | int from = -tag; |
| 2764 | int to = parts_[i].data; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2765 | replacement_substrings_.Add( |
| 2766 | isolate->factory()->NewSubString(replacement, from, to)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2767 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2768 | parts_[i].data = substring_index; |
| 2769 | substring_index++; |
| 2770 | } else if (tag == REPLACEMENT_STRING) { |
| 2771 | replacement_substrings_.Add(replacement); |
| 2772 | parts_[i].data = substring_index; |
| 2773 | substring_index++; |
| 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | |
| 2779 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2780 | int match_from, |
| 2781 | int match_to, |
| 2782 | Handle<JSArray> last_match_info) { |
| 2783 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2784 | ReplacementPart part = parts_[i]; |
| 2785 | switch (part.tag) { |
| 2786 | case SUBJECT_PREFIX: |
| 2787 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2788 | break; |
| 2789 | case SUBJECT_SUFFIX: { |
| 2790 | int subject_length = part.data; |
| 2791 | if (match_to < subject_length) { |
| 2792 | builder->AddSubjectSlice(match_to, subject_length); |
| 2793 | } |
| 2794 | break; |
| 2795 | } |
| 2796 | case SUBJECT_CAPTURE: { |
| 2797 | int capture = part.data; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2798 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2799 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2800 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2801 | if (from >= 0 && to > from) { |
| 2802 | builder->AddSubjectSlice(from, to); |
| 2803 | } |
| 2804 | break; |
| 2805 | } |
| 2806 | case REPLACEMENT_SUBSTRING: |
| 2807 | case REPLACEMENT_STRING: |
| 2808 | builder->AddString(replacement_substrings_[part.data]); |
| 2809 | break; |
| 2810 | default: |
| 2811 | UNREACHABLE(); |
| 2812 | } |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2817 | void FindAsciiStringIndices(Vector<const char> subject, |
| 2818 | char pattern, |
| 2819 | ZoneList<int>* indices, |
| 2820 | unsigned int limit) { |
| 2821 | ASSERT(limit > 0); |
| 2822 | // Collect indices of pattern in subject using memchr. |
| 2823 | // Stop after finding at most limit values. |
| 2824 | const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 2825 | const char* subject_end = subject_start + subject.length(); |
| 2826 | const char* pos = subject_start; |
| 2827 | while (limit > 0) { |
| 2828 | pos = reinterpret_cast<const char*>( |
| 2829 | memchr(pos, pattern, subject_end - pos)); |
| 2830 | if (pos == NULL) return; |
| 2831 | indices->Add(static_cast<int>(pos - subject_start)); |
| 2832 | pos++; |
| 2833 | limit--; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | |
| 2838 | template <typename SubjectChar, typename PatternChar> |
| 2839 | void FindStringIndices(Isolate* isolate, |
| 2840 | Vector<const SubjectChar> subject, |
| 2841 | Vector<const PatternChar> pattern, |
| 2842 | ZoneList<int>* indices, |
| 2843 | unsigned int limit) { |
| 2844 | ASSERT(limit > 0); |
| 2845 | // Collect indices of pattern in subject. |
| 2846 | // Stop after finding at most limit values. |
| 2847 | int pattern_length = pattern.length(); |
| 2848 | int index = 0; |
| 2849 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
| 2850 | while (limit > 0) { |
| 2851 | index = search.Search(subject, index); |
| 2852 | if (index < 0) return; |
| 2853 | indices->Add(index); |
| 2854 | index += pattern_length; |
| 2855 | limit--; |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | |
| 2860 | void FindStringIndicesDispatch(Isolate* isolate, |
| 2861 | String* subject, |
| 2862 | String* pattern, |
| 2863 | ZoneList<int>* indices, |
| 2864 | unsigned int limit) { |
| 2865 | { |
| 2866 | AssertNoAllocation no_gc; |
| 2867 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 2868 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 2869 | ASSERT(subject_content.IsFlat()); |
| 2870 | ASSERT(pattern_content.IsFlat()); |
| 2871 | if (subject_content.IsAscii()) { |
| 2872 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 2873 | if (pattern_content.IsAscii()) { |
| 2874 | Vector<const char> pattern_vector = pattern_content.ToAsciiVector(); |
| 2875 | if (pattern_vector.length() == 1) { |
| 2876 | FindAsciiStringIndices(subject_vector, |
| 2877 | pattern_vector[0], |
| 2878 | indices, |
| 2879 | limit); |
| 2880 | } else { |
| 2881 | FindStringIndices(isolate, |
| 2882 | subject_vector, |
| 2883 | pattern_vector, |
| 2884 | indices, |
| 2885 | limit); |
| 2886 | } |
| 2887 | } else { |
| 2888 | FindStringIndices(isolate, |
| 2889 | subject_vector, |
| 2890 | pattern_content.ToUC16Vector(), |
| 2891 | indices, |
| 2892 | limit); |
| 2893 | } |
| 2894 | } else { |
| 2895 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
| 2896 | if (pattern->IsAsciiRepresentation()) { |
| 2897 | FindStringIndices(isolate, |
| 2898 | subject_vector, |
| 2899 | pattern_content.ToAsciiVector(), |
| 2900 | indices, |
| 2901 | limit); |
| 2902 | } else { |
| 2903 | FindStringIndices(isolate, |
| 2904 | subject_vector, |
| 2905 | pattern_content.ToUC16Vector(), |
| 2906 | indices, |
| 2907 | limit); |
| 2908 | } |
| 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | |
| 2914 | template<typename ResultSeqString> |
| 2915 | MUST_USE_RESULT static MaybeObject* StringReplaceStringWithString( |
| 2916 | Isolate* isolate, |
| 2917 | Handle<String> subject, |
| 2918 | Handle<JSRegExp> pattern_regexp, |
| 2919 | Handle<String> replacement) { |
| 2920 | ASSERT(subject->IsFlat()); |
| 2921 | ASSERT(replacement->IsFlat()); |
| 2922 | |
| 2923 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
| 2924 | ZoneList<int> indices(8); |
| 2925 | ASSERT_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); |
| 2926 | String* pattern = |
| 2927 | String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
| 2928 | int subject_len = subject->length(); |
| 2929 | int pattern_len = pattern->length(); |
| 2930 | int replacement_len = replacement->length(); |
| 2931 | |
| 2932 | FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff); |
| 2933 | |
| 2934 | int matches = indices.length(); |
| 2935 | if (matches == 0) return *subject; |
| 2936 | |
| 2937 | int result_len = (replacement_len - pattern_len) * matches + subject_len; |
| 2938 | int subject_pos = 0; |
| 2939 | int result_pos = 0; |
| 2940 | |
| 2941 | Handle<ResultSeqString> result; |
| 2942 | if (ResultSeqString::kHasAsciiEncoding) { |
| 2943 | result = Handle<ResultSeqString>::cast( |
| 2944 | isolate->factory()->NewRawAsciiString(result_len)); |
| 2945 | } else { |
| 2946 | result = Handle<ResultSeqString>::cast( |
| 2947 | isolate->factory()->NewRawTwoByteString(result_len)); |
| 2948 | } |
| 2949 | |
| 2950 | for (int i = 0; i < matches; i++) { |
| 2951 | // Copy non-matched subject content. |
| 2952 | if (subject_pos < indices.at(i)) { |
| 2953 | String::WriteToFlat(*subject, |
| 2954 | result->GetChars() + result_pos, |
| 2955 | subject_pos, |
| 2956 | indices.at(i)); |
| 2957 | result_pos += indices.at(i) - subject_pos; |
| 2958 | } |
| 2959 | |
| 2960 | // Replace match. |
| 2961 | if (replacement_len > 0) { |
| 2962 | String::WriteToFlat(*replacement, |
| 2963 | result->GetChars() + result_pos, |
| 2964 | 0, |
| 2965 | replacement_len); |
| 2966 | result_pos += replacement_len; |
| 2967 | } |
| 2968 | |
| 2969 | subject_pos = indices.at(i) + pattern_len; |
| 2970 | } |
| 2971 | // Add remaining subject content at the end. |
| 2972 | if (subject_pos < subject_len) { |
| 2973 | String::WriteToFlat(*subject, |
| 2974 | result->GetChars() + result_pos, |
| 2975 | subject_pos, |
| 2976 | subject_len); |
| 2977 | } |
| 2978 | return *result; |
| 2979 | } |
| 2980 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2981 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2982 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2983 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2984 | String* subject, |
| 2985 | JSRegExp* regexp, |
| 2986 | String* replacement, |
| 2987 | JSArray* last_match_info) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2988 | ASSERT(subject->IsFlat()); |
| 2989 | ASSERT(replacement->IsFlat()); |
| 2990 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2991 | HandleScope handles(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2992 | |
| 2993 | int length = subject->length(); |
| 2994 | Handle<String> subject_handle(subject); |
| 2995 | Handle<JSRegExp> regexp_handle(regexp); |
| 2996 | Handle<String> replacement_handle(replacement); |
| 2997 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2998 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2999 | subject_handle, |
| 3000 | 0, |
| 3001 | last_match_info_handle); |
| 3002 | if (match.is_null()) { |
| 3003 | return Failure::Exception(); |
| 3004 | } |
| 3005 | if (match->IsNull()) { |
| 3006 | return *subject_handle; |
| 3007 | } |
| 3008 | |
| 3009 | int capture_count = regexp_handle->CaptureCount(); |
| 3010 | |
| 3011 | // CompiledReplacement uses zone allocation. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 3012 | ZoneScope zone(isolate, DELETE_ON_EXIT); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3013 | CompiledReplacement compiled_replacement; |
| 3014 | compiled_replacement.Compile(replacement_handle, |
| 3015 | capture_count, |
| 3016 | length); |
| 3017 | |
| 3018 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 3019 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 3020 | // Shortcut for simple non-regexp global replacements |
| 3021 | if (is_global && |
| 3022 | regexp->TypeTag() == JSRegExp::ATOM && |
| 3023 | compiled_replacement.simple_hint()) { |
| 3024 | if (subject_handle->HasOnlyAsciiChars() && |
| 3025 | replacement_handle->HasOnlyAsciiChars()) { |
| 3026 | return StringReplaceStringWithString<SeqAsciiString>( |
| 3027 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 3028 | } else { |
| 3029 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 3030 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 3031 | } |
| 3032 | } |
| 3033 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3034 | // Guessing the number of parts that the final result string is built |
| 3035 | // from. Global regexps can match any number of times, so we guess |
| 3036 | // conservatively. |
| 3037 | int expected_parts = |
| 3038 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3039 | ReplacementStringBuilder builder(isolate->heap(), |
| 3040 | subject_handle, |
| 3041 | expected_parts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3042 | |
| 3043 | // Index of end of last match. |
| 3044 | int prev = 0; |
| 3045 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3046 | // Number of parts added by compiled replacement plus preceeding |
| 3047 | // string and possibly suffix after last match. It is possible for |
| 3048 | // all components to use two elements when encoded as two smis. |
| 3049 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3050 | bool matched = true; |
| 3051 | do { |
| 3052 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3053 | // Increase the capacity of the builder before entering local handle-scope, |
| 3054 | // so its internal buffer can safely allocate a new handle if it grows. |
| 3055 | builder.EnsureCapacity(parts_added_per_loop); |
| 3056 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3057 | HandleScope loop_scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3058 | int start, end; |
| 3059 | { |
| 3060 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3061 | FixedArray* match_info_array = |
| 3062 | FixedArray::cast(last_match_info_handle->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3063 | |
| 3064 | ASSERT_EQ(capture_count * 2 + 2, |
| 3065 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 3066 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3067 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3068 | } |
| 3069 | |
| 3070 | if (prev < start) { |
| 3071 | builder.AddSubjectSlice(prev, start); |
| 3072 | } |
| 3073 | compiled_replacement.Apply(&builder, |
| 3074 | start, |
| 3075 | end, |
| 3076 | last_match_info_handle); |
| 3077 | prev = end; |
| 3078 | |
| 3079 | // Only continue checking for global regexps. |
| 3080 | if (!is_global) break; |
| 3081 | |
| 3082 | // Continue from where the match ended, unless it was an empty match. |
| 3083 | int next = end; |
| 3084 | if (start == end) { |
| 3085 | next = end + 1; |
| 3086 | if (next > length) break; |
| 3087 | } |
| 3088 | |
| 3089 | match = RegExpImpl::Exec(regexp_handle, |
| 3090 | subject_handle, |
| 3091 | next, |
| 3092 | last_match_info_handle); |
| 3093 | if (match.is_null()) { |
| 3094 | return Failure::Exception(); |
| 3095 | } |
| 3096 | matched = !match->IsNull(); |
| 3097 | } while (matched); |
| 3098 | |
| 3099 | if (prev < length) { |
| 3100 | builder.AddSubjectSlice(prev, length); |
| 3101 | } |
| 3102 | |
| 3103 | return *(builder.ToString()); |
| 3104 | } |
| 3105 | |
| 3106 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3107 | template <typename ResultSeqString> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3108 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3109 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3110 | String* subject, |
| 3111 | JSRegExp* regexp, |
| 3112 | JSArray* last_match_info) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3113 | ASSERT(subject->IsFlat()); |
| 3114 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3115 | HandleScope handles(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3116 | |
| 3117 | Handle<String> subject_handle(subject); |
| 3118 | Handle<JSRegExp> regexp_handle(regexp); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 3119 | |
| 3120 | // Shortcut for simple non-regexp global replacements |
| 3121 | if (regexp_handle->GetFlags().is_global() && |
| 3122 | regexp_handle->TypeTag() == JSRegExp::ATOM) { |
| 3123 | Handle<String> empty_string_handle(HEAP->empty_string()); |
| 3124 | if (subject_handle->HasOnlyAsciiChars()) { |
| 3125 | return StringReplaceStringWithString<SeqAsciiString>( |
| 3126 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3127 | } else { |
| 3128 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 3129 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3130 | } |
| 3131 | } |
| 3132 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3133 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 3134 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 3135 | subject_handle, |
| 3136 | 0, |
| 3137 | last_match_info_handle); |
| 3138 | if (match.is_null()) return Failure::Exception(); |
| 3139 | if (match->IsNull()) return *subject_handle; |
| 3140 | |
| 3141 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3142 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3143 | int start, end; |
| 3144 | { |
| 3145 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3146 | FixedArray* match_info_array = |
| 3147 | FixedArray::cast(last_match_info_handle->elements()); |
| 3148 | |
| 3149 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3150 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3151 | } |
| 3152 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 3153 | int length = subject_handle->length(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3154 | int new_length = length - (end - start); |
| 3155 | if (new_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3156 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3157 | } |
| 3158 | Handle<ResultSeqString> answer; |
| 3159 | if (ResultSeqString::kHasAsciiEncoding) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3160 | answer = Handle<ResultSeqString>::cast( |
| 3161 | isolate->factory()->NewRawAsciiString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3162 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3163 | answer = Handle<ResultSeqString>::cast( |
| 3164 | isolate->factory()->NewRawTwoByteString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | // If the regexp isn't global, only match once. |
| 3168 | if (!regexp_handle->GetFlags().is_global()) { |
| 3169 | if (start > 0) { |
| 3170 | String::WriteToFlat(*subject_handle, |
| 3171 | answer->GetChars(), |
| 3172 | 0, |
| 3173 | start); |
| 3174 | } |
| 3175 | if (end < length) { |
| 3176 | String::WriteToFlat(*subject_handle, |
| 3177 | answer->GetChars() + start, |
| 3178 | end, |
| 3179 | length); |
| 3180 | } |
| 3181 | return *answer; |
| 3182 | } |
| 3183 | |
| 3184 | int prev = 0; // Index of end of last match. |
| 3185 | int next = 0; // Start of next search (prev unless last match was empty). |
| 3186 | int position = 0; |
| 3187 | |
| 3188 | do { |
| 3189 | if (prev < start) { |
| 3190 | // Add substring subject[prev;start] to answer string. |
| 3191 | String::WriteToFlat(*subject_handle, |
| 3192 | answer->GetChars() + position, |
| 3193 | prev, |
| 3194 | start); |
| 3195 | position += start - prev; |
| 3196 | } |
| 3197 | prev = end; |
| 3198 | next = end; |
| 3199 | // Continue from where the match ended, unless it was an empty match. |
| 3200 | if (start == end) { |
| 3201 | next++; |
| 3202 | if (next > length) break; |
| 3203 | } |
| 3204 | match = RegExpImpl::Exec(regexp_handle, |
| 3205 | subject_handle, |
| 3206 | next, |
| 3207 | last_match_info_handle); |
| 3208 | if (match.is_null()) return Failure::Exception(); |
| 3209 | if (match->IsNull()) break; |
| 3210 | |
| 3211 | ASSERT(last_match_info_handle->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3212 | HandleScope loop_scope(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3213 | { |
| 3214 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3215 | FixedArray* match_info_array = |
| 3216 | FixedArray::cast(last_match_info_handle->elements()); |
| 3217 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3218 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3219 | } |
| 3220 | } while (true); |
| 3221 | |
| 3222 | if (prev < length) { |
| 3223 | // Add substring subject[prev;length] to answer string. |
| 3224 | String::WriteToFlat(*subject_handle, |
| 3225 | answer->GetChars() + position, |
| 3226 | prev, |
| 3227 | length); |
| 3228 | position += length - prev; |
| 3229 | } |
| 3230 | |
| 3231 | if (position == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3232 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | // Shorten string and fill |
| 3236 | int string_size = ResultSeqString::SizeFor(position); |
| 3237 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 3238 | int delta = allocated_string_size - string_size; |
| 3239 | |
| 3240 | answer->set_length(position); |
| 3241 | if (delta == 0) return *answer; |
| 3242 | |
| 3243 | Address end_of_string = answer->address() + string_size; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3244 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3245 | |
| 3246 | return *answer; |
| 3247 | } |
| 3248 | |
| 3249 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3250 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceRegExpWithString) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3251 | ASSERT(args.length() == 4); |
| 3252 | |
| 3253 | CONVERT_CHECKED(String, subject, args[0]); |
| 3254 | if (!subject->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3255 | Object* flat_subject; |
| 3256 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 3257 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 3258 | return maybe_flat_subject; |
| 3259 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3260 | } |
| 3261 | subject = String::cast(flat_subject); |
| 3262 | } |
| 3263 | |
| 3264 | CONVERT_CHECKED(String, replacement, args[2]); |
| 3265 | if (!replacement->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3266 | Object* flat_replacement; |
| 3267 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 3268 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 3269 | return maybe_flat_replacement; |
| 3270 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3271 | } |
| 3272 | replacement = String::cast(flat_replacement); |
| 3273 | } |
| 3274 | |
| 3275 | CONVERT_CHECKED(JSRegExp, regexp, args[1]); |
| 3276 | CONVERT_CHECKED(JSArray, last_match_info, args[3]); |
| 3277 | |
| 3278 | ASSERT(last_match_info->HasFastElements()); |
| 3279 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3280 | if (replacement->length() == 0) { |
| 3281 | if (subject->HasOnlyAsciiChars()) { |
| 3282 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3283 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3284 | } else { |
| 3285 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3286 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3287 | } |
| 3288 | } |
| 3289 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3290 | return StringReplaceRegExpWithString(isolate, |
| 3291 | subject, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3292 | regexp, |
| 3293 | replacement, |
| 3294 | last_match_info); |
| 3295 | } |
| 3296 | |
| 3297 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3298 | // Perform string match of pattern on subject, starting at start index. |
| 3299 | // Caller must ensure that 0 <= start_index <= sub->length(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3300 | // and should check that pat->length() + start_index <= sub->length(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3301 | int Runtime::StringMatch(Isolate* isolate, |
| 3302 | Handle<String> sub, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3303 | Handle<String> pat, |
| 3304 | int start_index) { |
| 3305 | ASSERT(0 <= start_index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3306 | ASSERT(start_index <= sub->length()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3307 | |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3308 | int pattern_length = pat->length(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3309 | if (pattern_length == 0) return start_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3310 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3311 | int subject_length = sub->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3312 | if (start_index + pattern_length > subject_length) return -1; |
| 3313 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3314 | if (!sub->IsFlat()) FlattenString(sub); |
| 3315 | if (!pat->IsFlat()) FlattenString(pat); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 3316 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3317 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 3318 | // Extract flattened substrings of cons strings before determining asciiness. |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3319 | String::FlatContent seq_sub = sub->GetFlatContent(); |
| 3320 | String::FlatContent seq_pat = pat->GetFlatContent(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 3321 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3322 | // dispatch on type of strings |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3323 | if (seq_pat.IsAscii()) { |
| 3324 | Vector<const char> pat_vector = seq_pat.ToAsciiVector(); |
| 3325 | if (seq_sub.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3326 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3327 | seq_sub.ToAsciiVector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3328 | pat_vector, |
| 3329 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 3330 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3331 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3332 | seq_sub.ToUC16Vector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3333 | pat_vector, |
| 3334 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 3335 | } |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3336 | Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); |
| 3337 | if (seq_sub.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3338 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3339 | seq_sub.ToAsciiVector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3340 | pat_vector, |
| 3341 | start_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3342 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3343 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3344 | seq_sub.ToUC16Vector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3345 | pat_vector, |
| 3346 | start_index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3350 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3351 | HandleScope scope(isolate); // create a new handle scope |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3352 | ASSERT(args.length() == 3); |
| 3353 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3354 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 3355 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 3356 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3357 | Object* index = args[2]; |
| 3358 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3359 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3360 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3361 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3362 | int position = |
| 3363 | Runtime::StringMatch(isolate, sub, pat, start_index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3364 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3368 | template <typename schar, typename pchar> |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3369 | static int StringMatchBackwards(Vector<const schar> subject, |
| 3370 | Vector<const pchar> pattern, |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3371 | int idx) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3372 | int pattern_length = pattern.length(); |
| 3373 | ASSERT(pattern_length >= 1); |
| 3374 | ASSERT(idx + pattern_length <= subject.length()); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3375 | |
| 3376 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3377 | for (int i = 0; i < pattern_length; i++) { |
| 3378 | uc16 c = pattern[i]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3379 | if (c > String::kMaxAsciiCharCode) { |
| 3380 | return -1; |
| 3381 | } |
| 3382 | } |
| 3383 | } |
| 3384 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3385 | pchar pattern_first_char = pattern[0]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3386 | for (int i = idx; i >= 0; i--) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3387 | if (subject[i] != pattern_first_char) continue; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3388 | int j = 1; |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3389 | while (j < pattern_length) { |
| 3390 | if (pattern[j] != subject[i+j]) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3391 | break; |
| 3392 | } |
| 3393 | j++; |
| 3394 | } |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3395 | if (j == pattern_length) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3396 | return i; |
| 3397 | } |
| 3398 | } |
| 3399 | return -1; |
| 3400 | } |
| 3401 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3402 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLastIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3403 | HandleScope scope(isolate); // create a new handle scope |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3404 | ASSERT(args.length() == 3); |
| 3405 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3406 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 3407 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 3408 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3409 | Object* index = args[2]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3410 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3411 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3412 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3413 | uint32_t pat_length = pat->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3414 | uint32_t sub_length = sub->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3415 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3416 | if (start_index + pat_length > sub_length) { |
| 3417 | start_index = sub_length - pat_length; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 3418 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3419 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3420 | if (pat_length == 0) { |
| 3421 | return Smi::FromInt(start_index); |
| 3422 | } |
| 3423 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3424 | if (!sub->IsFlat()) FlattenString(sub); |
| 3425 | if (!pat->IsFlat()) FlattenString(pat); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3426 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3427 | int position = -1; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3428 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3429 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3430 | String::FlatContent sub_content = sub->GetFlatContent(); |
| 3431 | String::FlatContent pat_content = pat->GetFlatContent(); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3432 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3433 | if (pat_content.IsAscii()) { |
| 3434 | Vector<const char> pat_vector = pat_content.ToAsciiVector(); |
| 3435 | if (sub_content.IsAscii()) { |
| 3436 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3437 | pat_vector, |
| 3438 | start_index); |
| 3439 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3440 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3441 | pat_vector, |
| 3442 | start_index); |
| 3443 | } |
| 3444 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3445 | Vector<const uc16> pat_vector = pat_content.ToUC16Vector(); |
| 3446 | if (sub_content.IsAscii()) { |
| 3447 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3448 | pat_vector, |
| 3449 | start_index); |
| 3450 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3451 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3452 | pat_vector, |
| 3453 | start_index); |
| 3454 | } |
| 3455 | } |
| 3456 | |
| 3457 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3461 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLocaleCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3462 | NoHandleAllocation ha; |
| 3463 | ASSERT(args.length() == 2); |
| 3464 | |
| 3465 | CONVERT_CHECKED(String, str1, args[0]); |
| 3466 | CONVERT_CHECKED(String, str2, args[1]); |
| 3467 | |
| 3468 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3469 | int str1_length = str1->length(); |
| 3470 | int str2_length = str2->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3471 | |
| 3472 | // Decide trivial cases without flattening. |
| 3473 | if (str1_length == 0) { |
| 3474 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3475 | return Smi::FromInt(-str2_length); |
| 3476 | } else { |
| 3477 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3478 | } |
| 3479 | |
| 3480 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3481 | |
| 3482 | // No need to flatten if we are going to find the answer on the first |
| 3483 | // character. At this point we know there is at least one character |
| 3484 | // in each string, due to the trivial case handling above. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3485 | int d = str1->Get(0) - str2->Get(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3486 | if (d != 0) return Smi::FromInt(d); |
| 3487 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3488 | str1->TryFlatten(); |
| 3489 | str2->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3490 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3491 | StringInputBuffer& buf1 = |
| 3492 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3493 | StringInputBuffer& buf2 = |
| 3494 | *isolate->runtime_state()->string_locale_compare_buf2(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3495 | |
| 3496 | buf1.Reset(str1); |
| 3497 | buf2.Reset(str2); |
| 3498 | |
| 3499 | for (int i = 0; i < end; i++) { |
| 3500 | uint16_t char1 = buf1.GetNext(); |
| 3501 | uint16_t char2 = buf2.GetNext(); |
| 3502 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3503 | } |
| 3504 | |
| 3505 | return Smi::FromInt(str1_length - str2_length); |
| 3506 | } |
| 3507 | |
| 3508 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3509 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SubString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3510 | NoHandleAllocation ha; |
| 3511 | ASSERT(args.length() == 3); |
| 3512 | |
| 3513 | CONVERT_CHECKED(String, value, args[0]); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3514 | int start, end; |
| 3515 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3516 | // the common case where from and to are Smis. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3517 | if (args[1]->IsSmi() && args[2]->IsSmi()) { |
| 3518 | CONVERT_SMI_ARG_CHECKED(from_number, 1); |
| 3519 | CONVERT_SMI_ARG_CHECKED(to_number, 2); |
| 3520 | start = from_number; |
| 3521 | end = to_number; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3522 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3523 | CONVERT_DOUBLE_ARG_CHECKED(from_number, 1); |
| 3524 | CONVERT_DOUBLE_ARG_CHECKED(to_number, 2); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3525 | start = FastD2I(from_number); |
| 3526 | end = FastD2I(to_number); |
| 3527 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3528 | RUNTIME_ASSERT(end >= start); |
| 3529 | RUNTIME_ASSERT(start >= 0); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3530 | RUNTIME_ASSERT(end <= value->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3531 | isolate->counters()->sub_string_runtime()->Increment(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3532 | return value->SubString(start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3536 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringMatch) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3537 | ASSERT_EQ(3, args.length()); |
| 3538 | |
| 3539 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 3540 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3541 | CONVERT_ARG_CHECKED(JSArray, regexp_info, 2); |
| 3542 | HandleScope handles; |
| 3543 | |
| 3544 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3545 | |
| 3546 | if (match.is_null()) { |
| 3547 | return Failure::Exception(); |
| 3548 | } |
| 3549 | if (match->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3550 | return isolate->heap()->null_value(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3551 | } |
| 3552 | int length = subject->length(); |
| 3553 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 3554 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3555 | ZoneList<int> offsets(8); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3556 | int start; |
| 3557 | int end; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3558 | do { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3559 | { |
| 3560 | AssertNoAllocation no_alloc; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3561 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3562 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3563 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3564 | } |
| 3565 | offsets.Add(start); |
| 3566 | offsets.Add(end); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3567 | if (start == end) if (++end > length) break; |
| 3568 | match = RegExpImpl::Exec(regexp, subject, end, regexp_info); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3569 | if (match.is_null()) { |
| 3570 | return Failure::Exception(); |
| 3571 | } |
| 3572 | } while (!match->IsNull()); |
| 3573 | int matches = offsets.length() / 2; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3574 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3575 | Handle<String> substring = isolate->factory()-> |
| 3576 | NewSubString(subject, offsets.at(0), offsets.at(1)); |
| 3577 | elements->set(0, *substring); |
| 3578 | for (int i = 1; i < matches ; i++) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3579 | int from = offsets.at(i * 2); |
| 3580 | int to = offsets.at(i * 2 + 1); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3581 | Handle<String> substring = isolate->factory()-> |
| 3582 | NewProperSubString(subject, from, to); |
| 3583 | elements->set(i, *substring); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3584 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3585 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3586 | result->set_length(Smi::FromInt(matches)); |
| 3587 | return *result; |
| 3588 | } |
| 3589 | |
| 3590 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3591 | // Two smis before and after the match, for very long strings. |
| 3592 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3593 | |
| 3594 | |
| 3595 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3596 | Handle<JSArray> last_match_info, |
| 3597 | int match_start, |
| 3598 | int match_end) { |
| 3599 | // Fill last_match_info with a single capture. |
| 3600 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3601 | AssertNoAllocation no_gc; |
| 3602 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3603 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3604 | RegExpImpl::SetLastInput(elements, *subject); |
| 3605 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3606 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3607 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3608 | } |
| 3609 | |
| 3610 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3611 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3612 | static bool SearchStringMultiple(Isolate* isolate, |
| 3613 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3614 | Vector<const PatternChar> pattern, |
| 3615 | String* pattern_string, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3616 | FixedArrayBuilder* builder, |
| 3617 | int* match_pos) { |
| 3618 | int pos = *match_pos; |
| 3619 | int subject_length = subject.length(); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3620 | int pattern_length = pattern.length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3621 | int max_search_start = subject_length - pattern_length; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3622 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3623 | while (pos <= max_search_start) { |
| 3624 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3625 | *match_pos = pos; |
| 3626 | return false; |
| 3627 | } |
| 3628 | // Position of end of previous match. |
| 3629 | int match_end = pos + pattern_length; |
| 3630 | int new_pos = search.Search(subject, match_end); |
| 3631 | if (new_pos >= 0) { |
| 3632 | // A match. |
| 3633 | if (new_pos > match_end) { |
| 3634 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3635 | match_end, |
| 3636 | new_pos); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3637 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3638 | pos = new_pos; |
| 3639 | builder->Add(pattern_string); |
| 3640 | } else { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3641 | break; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3642 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3643 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3644 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3645 | if (pos < max_search_start) { |
| 3646 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3647 | pos + pattern_length, |
| 3648 | subject_length); |
| 3649 | } |
| 3650 | *match_pos = pos; |
| 3651 | return true; |
| 3652 | } |
| 3653 | |
| 3654 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3655 | static bool SearchStringMultiple(Isolate* isolate, |
| 3656 | Handle<String> subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3657 | Handle<String> pattern, |
| 3658 | Handle<JSArray> last_match_info, |
| 3659 | FixedArrayBuilder* builder) { |
| 3660 | ASSERT(subject->IsFlat()); |
| 3661 | ASSERT(pattern->IsFlat()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3662 | |
| 3663 | // Treating as if a previous match was before first character. |
| 3664 | int match_pos = -pattern->length(); |
| 3665 | |
| 3666 | for (;;) { // Break when search complete. |
| 3667 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3668 | AssertNoAllocation no_gc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3669 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 3670 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 3671 | if (subject_content.IsAscii()) { |
| 3672 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 3673 | if (pattern_content.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3674 | if (SearchStringMultiple(isolate, |
| 3675 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3676 | pattern_content.ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3677 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3678 | builder, |
| 3679 | &match_pos)) break; |
| 3680 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3681 | if (SearchStringMultiple(isolate, |
| 3682 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3683 | pattern_content.ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3684 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3685 | builder, |
| 3686 | &match_pos)) break; |
| 3687 | } |
| 3688 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3689 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
| 3690 | if (pattern_content.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3691 | if (SearchStringMultiple(isolate, |
| 3692 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3693 | pattern_content.ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3694 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3695 | builder, |
| 3696 | &match_pos)) break; |
| 3697 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3698 | if (SearchStringMultiple(isolate, |
| 3699 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3700 | pattern_content.ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3701 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3702 | builder, |
| 3703 | &match_pos)) break; |
| 3704 | } |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | if (match_pos >= 0) { |
| 3709 | SetLastMatchInfoNoCaptures(subject, |
| 3710 | last_match_info, |
| 3711 | match_pos, |
| 3712 | match_pos + pattern->length()); |
| 3713 | return true; |
| 3714 | } |
| 3715 | return false; // No matches at all. |
| 3716 | } |
| 3717 | |
| 3718 | |
| 3719 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3720 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3721 | Handle<String> subject, |
| 3722 | Handle<JSRegExp> regexp, |
| 3723 | Handle<JSArray> last_match_array, |
| 3724 | FixedArrayBuilder* builder) { |
| 3725 | ASSERT(subject->IsFlat()); |
| 3726 | int match_start = -1; |
| 3727 | int match_end = 0; |
| 3728 | int pos = 0; |
| 3729 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3730 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3731 | |
| 3732 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3733 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3734 | int subject_length = subject->length(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3735 | bool first = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3736 | |
| 3737 | for (;;) { // Break on failure, return on exception. |
| 3738 | RegExpImpl::IrregexpResult result = |
| 3739 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3740 | subject, |
| 3741 | pos, |
| 3742 | register_vector); |
| 3743 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3744 | match_start = register_vector[0]; |
| 3745 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3746 | if (match_end < match_start) { |
| 3747 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3748 | match_end, |
| 3749 | match_start); |
| 3750 | } |
| 3751 | match_end = register_vector[1]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3752 | HandleScope loop_scope(isolate); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3753 | if (!first) { |
| 3754 | builder->Add(*isolate->factory()->NewProperSubString(subject, |
| 3755 | match_start, |
| 3756 | match_end)); |
| 3757 | } else { |
| 3758 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3759 | match_start, |
| 3760 | match_end)); |
| 3761 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3762 | if (match_start != match_end) { |
| 3763 | pos = match_end; |
| 3764 | } else { |
| 3765 | pos = match_end + 1; |
| 3766 | if (pos > subject_length) break; |
| 3767 | } |
| 3768 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3769 | break; |
| 3770 | } else { |
| 3771 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3772 | return result; |
| 3773 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3774 | first = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | if (match_start >= 0) { |
| 3778 | if (match_end < subject_length) { |
| 3779 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3780 | match_end, |
| 3781 | subject_length); |
| 3782 | } |
| 3783 | SetLastMatchInfoNoCaptures(subject, |
| 3784 | last_match_array, |
| 3785 | match_start, |
| 3786 | match_end); |
| 3787 | return RegExpImpl::RE_SUCCESS; |
| 3788 | } else { |
| 3789 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | |
| 3794 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3795 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3796 | Handle<String> subject, |
| 3797 | Handle<JSRegExp> regexp, |
| 3798 | Handle<JSArray> last_match_array, |
| 3799 | FixedArrayBuilder* builder) { |
| 3800 | |
| 3801 | ASSERT(subject->IsFlat()); |
| 3802 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3803 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3804 | |
| 3805 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3806 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3807 | |
| 3808 | RegExpImpl::IrregexpResult result = |
| 3809 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3810 | subject, |
| 3811 | 0, |
| 3812 | register_vector); |
| 3813 | |
| 3814 | int capture_count = regexp->CaptureCount(); |
| 3815 | int subject_length = subject->length(); |
| 3816 | |
| 3817 | // Position to search from. |
| 3818 | int pos = 0; |
| 3819 | // End of previous match. Differs from pos if match was empty. |
| 3820 | int match_end = 0; |
| 3821 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3822 | // Need to keep a copy of the previous match for creating last_match_info |
| 3823 | // at the end, so we have two vectors that we swap between. |
| 3824 | OffsetsVector registers2(required_registers); |
| 3825 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3826 | bool first = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3827 | do { |
| 3828 | int match_start = register_vector[0]; |
| 3829 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3830 | if (match_end < match_start) { |
| 3831 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3832 | match_end, |
| 3833 | match_start); |
| 3834 | } |
| 3835 | match_end = register_vector[1]; |
| 3836 | |
| 3837 | { |
| 3838 | // Avoid accumulating new handles inside loop. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3839 | HandleScope temp_scope(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3840 | // Arguments array to replace function is match, captures, index and |
| 3841 | // subject, i.e., 3 + capture count in total. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3842 | Handle<FixedArray> elements = |
| 3843 | isolate->factory()->NewFixedArray(3 + capture_count); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3844 | Handle<String> match; |
| 3845 | if (!first) { |
| 3846 | match = isolate->factory()->NewProperSubString(subject, |
| 3847 | match_start, |
| 3848 | match_end); |
| 3849 | } else { |
| 3850 | match = isolate->factory()->NewSubString(subject, |
| 3851 | match_start, |
| 3852 | match_end); |
| 3853 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3854 | elements->set(0, *match); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3855 | for (int i = 1; i <= capture_count; i++) { |
| 3856 | int start = register_vector[i * 2]; |
| 3857 | if (start >= 0) { |
| 3858 | int end = register_vector[i * 2 + 1]; |
| 3859 | ASSERT(start <= end); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3860 | Handle<String> substring; |
| 3861 | if (!first) { |
| 3862 | substring = isolate->factory()->NewProperSubString(subject, |
| 3863 | start, |
| 3864 | end); |
| 3865 | } else { |
| 3866 | substring = isolate->factory()->NewSubString(subject, start, end); |
| 3867 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3868 | elements->set(i, *substring); |
| 3869 | } else { |
| 3870 | ASSERT(register_vector[i * 2 + 1] < 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3871 | elements->set(i, isolate->heap()->undefined_value()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3872 | } |
| 3873 | } |
| 3874 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3875 | elements->set(capture_count + 2, *subject); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3876 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3877 | } |
| 3878 | // Swap register vectors, so the last successful match is in |
| 3879 | // prev_register_vector. |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3880 | Vector<int32_t> tmp = prev_register_vector; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3881 | prev_register_vector = register_vector; |
| 3882 | register_vector = tmp; |
| 3883 | |
| 3884 | if (match_end > match_start) { |
| 3885 | pos = match_end; |
| 3886 | } else { |
| 3887 | pos = match_end + 1; |
| 3888 | if (pos > subject_length) { |
| 3889 | break; |
| 3890 | } |
| 3891 | } |
| 3892 | |
| 3893 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3894 | subject, |
| 3895 | pos, |
| 3896 | register_vector); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3897 | first = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3898 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3899 | |
| 3900 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3901 | // Finished matching, with at least one match. |
| 3902 | if (match_end < subject_length) { |
| 3903 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3904 | match_end, |
| 3905 | subject_length); |
| 3906 | } |
| 3907 | |
| 3908 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3909 | int last_match_array_size = |
| 3910 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3911 | last_match_array->EnsureSize(last_match_array_size); |
| 3912 | AssertNoAllocation no_gc; |
| 3913 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3914 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3915 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3916 | RegExpImpl::SetLastInput(elements, *subject); |
| 3917 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3918 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3919 | } |
| 3920 | return RegExpImpl::RE_SUCCESS; |
| 3921 | } |
| 3922 | } |
| 3923 | // No matches at all, return failure or exception result directly. |
| 3924 | return result; |
| 3925 | } |
| 3926 | |
| 3927 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3928 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3929 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3930 | HandleScope handles(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3931 | |
| 3932 | CONVERT_ARG_CHECKED(String, subject, 1); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 3933 | if (!subject->IsFlat()) FlattenString(subject); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3934 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 3935 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 2); |
| 3936 | CONVERT_ARG_CHECKED(JSArray, result_array, 3); |
| 3937 | |
| 3938 | ASSERT(last_match_info->HasFastElements()); |
| 3939 | ASSERT(regexp->GetFlags().is_global()); |
| 3940 | Handle<FixedArray> result_elements; |
| 3941 | if (result_array->HasFastElements()) { |
| 3942 | result_elements = |
| 3943 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3944 | } |
| 3945 | if (result_elements.is_null() || result_elements->length() < 16) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3946 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3947 | } |
| 3948 | FixedArrayBuilder builder(result_elements); |
| 3949 | |
| 3950 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 3951 | Handle<String> pattern( |
| 3952 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3953 | ASSERT(pattern->IsFlat()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3954 | if (SearchStringMultiple(isolate, subject, pattern, |
| 3955 | last_match_info, &builder)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3956 | return *builder.ToJSArray(result_array); |
| 3957 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3958 | return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 3962 | |
| 3963 | RegExpImpl::IrregexpResult result; |
| 3964 | if (regexp->CaptureCount() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3965 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 3966 | subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3967 | regexp, |
| 3968 | last_match_info, |
| 3969 | &builder); |
| 3970 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3971 | result = SearchRegExpMultiple(isolate, |
| 3972 | subject, |
| 3973 | regexp, |
| 3974 | last_match_info, |
| 3975 | &builder); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3976 | } |
| 3977 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3978 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3979 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3980 | return Failure::Exception(); |
| 3981 | } |
| 3982 | |
| 3983 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3984 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToRadixString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3985 | NoHandleAllocation ha; |
| 3986 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3987 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3988 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3989 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3990 | // Fast case where the result is a one character string. |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3991 | if (args[0]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3992 | int value = args.smi_at(0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3993 | if (value >= 0 && value < radix) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3994 | // Character array used for conversion. |
| 3995 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3996 | return isolate->heap()-> |
| 3997 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | // Slow case. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4002 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4003 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4004 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4005 | } |
| 4006 | if (isinf(value)) { |
| 4007 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4008 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4009 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4010 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4011 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4012 | char* str = DoubleToRadixCString(value, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4013 | MaybeObject* result = |
| 4014 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4015 | DeleteArray(str); |
| 4016 | return result; |
| 4017 | } |
| 4018 | |
| 4019 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4020 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToFixed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | NoHandleAllocation ha; |
| 4022 | ASSERT(args.length() == 2); |
| 4023 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4024 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4025 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4026 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4027 | } |
| 4028 | if (isinf(value)) { |
| 4029 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4030 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4031 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4032 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4033 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4034 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4035 | int f = FastD2I(f_number); |
| 4036 | RUNTIME_ASSERT(f >= 0); |
| 4037 | char* str = DoubleToFixedCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4038 | MaybeObject* res = |
| 4039 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4040 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4041 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4045 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToExponential) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4046 | NoHandleAllocation ha; |
| 4047 | ASSERT(args.length() == 2); |
| 4048 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4049 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4050 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4051 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4052 | } |
| 4053 | if (isinf(value)) { |
| 4054 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4055 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4056 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4057 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4058 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4059 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4060 | int f = FastD2I(f_number); |
| 4061 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 4062 | char* str = DoubleToExponentialCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4063 | MaybeObject* res = |
| 4064 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4065 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4066 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4070 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPrecision) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4071 | NoHandleAllocation ha; |
| 4072 | ASSERT(args.length() == 2); |
| 4073 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4074 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4075 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4076 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4077 | } |
| 4078 | if (isinf(value)) { |
| 4079 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4080 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4081 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4082 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4083 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4084 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4085 | int f = FastD2I(f_number); |
| 4086 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 4087 | char* str = DoubleToPrecisionCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4088 | MaybeObject* res = |
| 4089 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4090 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4091 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | |
| 4095 | // Returns a single character string where first character equals |
| 4096 | // string->Get(index). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4097 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4098 | if (index < static_cast<uint32_t>(string->length())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4099 | string->TryFlatten(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4100 | return LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4101 | string->Get(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4102 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4103 | return Execution::CharAt(string, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4107 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 4108 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4109 | uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4110 | // Handle [] indexing on Strings |
| 4111 | if (object->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4112 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 4113 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | // Handle [] indexing on String objects |
| 4117 | if (object->IsStringObjectWithCharacterAt(index)) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4118 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 4119 | Handle<Object> result = |
| 4120 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 4121 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4125 | Handle<Object> prototype = GetPrototype(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4126 | return prototype->GetElement(index); |
| 4127 | } |
| 4128 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4129 | return GetElement(object, index); |
| 4130 | } |
| 4131 | |
| 4132 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4133 | MaybeObject* Runtime::GetElement(Handle<Object> object, uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4134 | return object->GetElement(index); |
| 4135 | } |
| 4136 | |
| 4137 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4138 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 4139 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4140 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4141 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4142 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4143 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4144 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4145 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4146 | isolate->factory()->NewTypeError("non_object_property_load", |
| 4147 | HandleVector(args, 2)); |
| 4148 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | // Check if the given key is an array index. |
| 4152 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4153 | if (key->ToArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4154 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | // 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] | 4158 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4159 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4160 | name = Handle<String>::cast(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4161 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4162 | bool has_pending_exception = false; |
| 4163 | Handle<Object> converted = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4164 | Execution::ToString(key, &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4165 | if (has_pending_exception) return Failure::Exception(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4166 | name = Handle<String>::cast(converted); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4169 | // 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] | 4170 | // the element if so. |
| 4171 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4172 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4173 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4174 | return object->GetProperty(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
| 4178 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4179 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4180 | NoHandleAllocation ha; |
| 4181 | ASSERT(args.length() == 2); |
| 4182 | |
| 4183 | Handle<Object> object = args.at<Object>(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4184 | Handle<Object> key = args.at<Object>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4185 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4186 | return Runtime::GetObjectProperty(isolate, object, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4190 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4191 | RUNTIME_FUNCTION(MaybeObject*, Runtime_KeyedGetProperty) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4192 | NoHandleAllocation ha; |
| 4193 | ASSERT(args.length() == 2); |
| 4194 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4195 | // Fast cases for getting named properties of the receiver JSObject |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4196 | // itself. |
| 4197 | // |
| 4198 | // The global proxy objects has to be excluded since LocalLookup on |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4199 | // 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] | 4200 | // global proxy object never has properties. This is the case |
| 4201 | // because the global proxy object forwards everything to its hidden |
| 4202 | // prototype including local lookups. |
| 4203 | // |
| 4204 | // Additionally, we need to make sure that we do not cache results |
| 4205 | // for objects that require access checks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4206 | if (args[0]->IsJSObject() && |
| 4207 | !args[0]->IsJSGlobalProxy() && |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4208 | !args[0]->IsAccessCheckNeeded() && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4209 | args[1]->IsString()) { |
| 4210 | JSObject* receiver = JSObject::cast(args[0]); |
| 4211 | String* key = String::cast(args[1]); |
| 4212 | if (receiver->HasFastProperties()) { |
| 4213 | // Attempt to use lookup cache. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4214 | Map* receiver_map = receiver->map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4215 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 4216 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4217 | if (offset != -1) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4218 | Object* value = receiver->FastPropertyAt(offset); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4219 | return value->IsTheHole() ? isolate->heap()->undefined_value() : value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4220 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4221 | // Lookup cache miss. Perform lookup and update the cache if appropriate. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4222 | LookupResult result; |
| 4223 | receiver->LocalLookup(key, &result); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 4224 | if (result.IsProperty() && result.type() == FIELD) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4225 | int offset = result.GetFieldIndex(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4226 | keyed_lookup_cache->Update(receiver_map, key, offset); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4227 | return receiver->FastPropertyAt(offset); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4228 | } |
| 4229 | } else { |
| 4230 | // Attempt dictionary lookup. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 4231 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 4232 | int entry = dictionary->FindEntry(key); |
| 4233 | if ((entry != StringDictionary::kNotFound) && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4234 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 4235 | Object* value = dictionary->ValueAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4236 | if (!receiver->IsGlobalObject()) return value; |
| 4237 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 4238 | if (!value->IsTheHole()) return value; |
| 4239 | // If value is the hole do the general lookup. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4240 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4241 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4242 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 4243 | // Fast case for string indexing using [] with a smi index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4244 | HandleScope scope(isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4245 | Handle<String> str = args.at<String>(0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4246 | int index = args.smi_at(1); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4247 | if (index >= 0 && index < str->length()) { |
| 4248 | Handle<Object> result = GetCharAt(str, index); |
| 4249 | return *result; |
| 4250 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4251 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4252 | |
| 4253 | // Fall back to GetObjectProperty. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4254 | return Runtime::GetObjectProperty(isolate, |
| 4255 | args.at<Object>(0), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4256 | args.at<Object>(1)); |
| 4257 | } |
| 4258 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4259 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4260 | // There are 3 cases that lead here: |
| 4261 | // Step 4b - define a new accessor property. |
| 4262 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 4263 | // Step 12 - update an existing accessor property with an accessor or generic |
| 4264 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4265 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineAccessorProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4266 | ASSERT(args.length() == 5); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4267 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4268 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4269 | CONVERT_CHECKED(String, name, args[1]); |
| 4270 | CONVERT_CHECKED(Smi, flag_setter, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4271 | Object* fun = args[3]; |
| 4272 | RUNTIME_ASSERT(fun->IsJSFunction() || fun->IsUndefined()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4273 | CONVERT_CHECKED(Smi, flag_attr, args[4]); |
| 4274 | int unchecked = flag_attr->value(); |
| 4275 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4276 | RUNTIME_ASSERT(!obj->IsNull()); |
| 4277 | LookupResult result; |
| 4278 | obj->LocalLookupRealNamedProperty(name, &result); |
| 4279 | |
| 4280 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 4281 | // If an existing property is either FIELD, NORMAL or CONSTANT_FUNCTION |
| 4282 | // delete it to avoid running into trouble in DefineAccessor, which |
| 4283 | // handles this incorrectly if the property is readonly (does nothing) |
| 4284 | if (result.IsProperty() && |
| 4285 | (result.type() == FIELD || result.type() == NORMAL |
| 4286 | || result.type() == CONSTANT_FUNCTION)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4287 | Object* ok; |
| 4288 | { MaybeObject* maybe_ok = |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4289 | obj->DeleteProperty(name, JSReceiver::NORMAL_DELETION); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4290 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 4291 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4292 | } |
| 4293 | return obj->DefineAccessor(name, flag_setter->value() == 0, fun, attr); |
| 4294 | } |
| 4295 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4296 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4297 | // There are 3 cases that lead here: |
| 4298 | // Step 4a - define a new data property. |
| 4299 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 4300 | // Step 12 - update an existing data property with a data or generic |
| 4301 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4302 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineDataProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4303 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4304 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4305 | CONVERT_ARG_CHECKED(JSObject, js_object, 0); |
| 4306 | CONVERT_ARG_CHECKED(String, name, 1); |
| 4307 | Handle<Object> obj_value = args.at<Object>(2); |
| 4308 | |
| 4309 | CONVERT_CHECKED(Smi, flag, args[3]); |
| 4310 | int unchecked = flag->value(); |
| 4311 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4312 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4313 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 4314 | |
| 4315 | // Check if this is an element. |
| 4316 | uint32_t index; |
| 4317 | bool is_element = name->AsArrayIndex(&index); |
| 4318 | |
| 4319 | // Special case for elements if any of the flags are true. |
| 4320 | // If elements are in fast case we always implicitly assume that: |
| 4321 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 4322 | if (((unchecked & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) && |
| 4323 | is_element) { |
| 4324 | // Normalize the elements to enable attributes on the property. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4325 | if (js_object->IsJSGlobalProxy()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4326 | // We do not need to do access checks here since these has already |
| 4327 | // been performed by the call to GetOwnProperty. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4328 | Handle<Object> proto(js_object->GetPrototype()); |
| 4329 | // If proxy is detached, ignore the assignment. Alternatively, |
| 4330 | // we could throw an exception. |
| 4331 | if (proto->IsNull()) return *obj_value; |
| 4332 | js_object = Handle<JSObject>::cast(proto); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4333 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4334 | |
| 4335 | // Don't allow element properties to be redefined on objects with external |
| 4336 | // array elements. |
| 4337 | if (js_object->HasExternalArrayElements()) { |
| 4338 | Handle<Object> args[2] = { js_object, name }; |
| 4339 | Handle<Object> error = |
| 4340 | isolate->factory()->NewTypeError("redef_external_array_element", |
| 4341 | HandleVector(args, 2)); |
| 4342 | return isolate->Throw(*error); |
| 4343 | } |
| 4344 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4345 | Handle<NumberDictionary> dictionary = NormalizeElements(js_object); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4346 | // Make sure that we never go back to fast case. |
| 4347 | dictionary->set_requires_slow_elements(); |
| 4348 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4349 | Handle<NumberDictionary> extended_dictionary = |
| 4350 | NumberDictionarySet(dictionary, index, obj_value, details); |
| 4351 | if (*extended_dictionary != *dictionary) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4352 | if (js_object->GetElementsKind() == NON_STRICT_ARGUMENTS_ELEMENTS) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4353 | FixedArray::cast(js_object->elements())->set(1, *extended_dictionary); |
| 4354 | } else { |
| 4355 | js_object->set_elements(*extended_dictionary); |
| 4356 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4357 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4358 | return *obj_value; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4361 | LookupResult result; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4362 | js_object->LocalLookupRealNamedProperty(*name, &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4363 | |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 4364 | // To be compatible with safari we do not change the value on API objects |
| 4365 | // in defineProperty. Firefox disagrees here, and actually changes the value. |
| 4366 | if (result.IsProperty() && |
| 4367 | (result.type() == CALLBACKS) && |
| 4368 | result.GetCallbackObject()->IsAccessorInfo()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4369 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4372 | // Take special care when attributes are different and there is already |
| 4373 | // a property. For simplicity we normalize the property which enables us |
| 4374 | // to not worry about changing the instance_descriptor and creating a new |
| 4375 | // map. The current version of SetObjectProperty does not handle attributes |
| 4376 | // correctly in the case where a property is a field and is reset with |
| 4377 | // new attributes. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4378 | if (result.IsProperty() && |
| 4379 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4380 | // New attributes - normalize to avoid writing to instance descriptor |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4381 | if (js_object->IsJSGlobalProxy()) { |
| 4382 | // Since the result is a property, the prototype will exist so |
| 4383 | // we don't have to check for null. |
| 4384 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4385 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4386 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4387 | // Use IgnoreAttributes version since a readonly property may be |
| 4388 | // overridden and SetProperty does not allow this. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4389 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 4390 | *obj_value, |
| 4391 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4392 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4393 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4394 | return Runtime::ForceSetObjectProperty(isolate, |
| 4395 | js_object, |
| 4396 | name, |
| 4397 | obj_value, |
| 4398 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4402 | // Special case for elements if any of the flags are true. |
| 4403 | // If elements are in fast case we always implicitly assume that: |
| 4404 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 4405 | static MaybeObject* NormalizeObjectSetElement(Isolate* isolate, |
| 4406 | Handle<JSObject> js_object, |
| 4407 | uint32_t index, |
| 4408 | Handle<Object> value, |
| 4409 | PropertyAttributes attr) { |
| 4410 | // Normalize the elements to enable attributes on the property. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4411 | Handle<NumberDictionary> dictionary = NormalizeElements(js_object); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4412 | // Make sure that we never go back to fast case. |
| 4413 | dictionary->set_requires_slow_elements(); |
| 4414 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
| 4415 | Handle<NumberDictionary> extended_dictionary = |
| 4416 | NumberDictionarySet(dictionary, index, value, details); |
| 4417 | if (*extended_dictionary != *dictionary) { |
| 4418 | js_object->set_elements(*extended_dictionary); |
| 4419 | } |
| 4420 | return *value; |
| 4421 | } |
| 4422 | |
| 4423 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4424 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 4425 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4426 | Handle<Object> key, |
| 4427 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4428 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4429 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4430 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4431 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4432 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4433 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4434 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4435 | isolate->factory()->NewTypeError("non_object_property_store", |
| 4436 | HandleVector(args, 2)); |
| 4437 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | // If the object isn't a JavaScript object, we ignore the store. |
| 4441 | if (!object->IsJSObject()) return *value; |
| 4442 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4443 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 4444 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4445 | // Check if the given key is an array index. |
| 4446 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4447 | if (key->ToArrayIndex(&index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4448 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4449 | // of a string using [] notation. We need to support this too in |
| 4450 | // JavaScript. |
| 4451 | // In the case of a String object we just need to redirect the assignment to |
| 4452 | // the underlying string if the index is in range. Since the underlying |
| 4453 | // string does nothing with the assignment then we can ignore such |
| 4454 | // assignments. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4455 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4456 | return *value; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4457 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4458 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4459 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4460 | return NormalizeObjectSetElement(isolate, js_object, index, value, attr); |
| 4461 | } |
| 4462 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4463 | 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] | 4464 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4465 | return *value; |
| 4466 | } |
| 4467 | |
| 4468 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4469 | Handle<Object> result; |
| 4470 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4471 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4472 | return NormalizeObjectSetElement(isolate, |
| 4473 | js_object, |
| 4474 | index, |
| 4475 | value, |
| 4476 | attr); |
| 4477 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4478 | result = SetElement(js_object, index, value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4479 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4480 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4481 | key_string->TryFlatten(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4482 | result = SetProperty(js_object, key_string, value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4483 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4484 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4485 | return *value; |
| 4486 | } |
| 4487 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4488 | // Call-back into JavaScript to convert the key to a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4489 | bool has_pending_exception = false; |
| 4490 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4491 | if (has_pending_exception) return Failure::Exception(); |
| 4492 | Handle<String> name = Handle<String>::cast(converted); |
| 4493 | |
| 4494 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4495 | return js_object->SetElement(index, *value, strict_mode, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4496 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4497 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
| 4500 | |
| 4501 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4502 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4503 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4504 | Handle<Object> key, |
| 4505 | Handle<Object> value, |
| 4506 | PropertyAttributes attr) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4507 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4508 | |
| 4509 | // Check if the given key is an array index. |
| 4510 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4511 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4512 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4513 | // of a string using [] notation. We need to support this too in |
| 4514 | // JavaScript. |
| 4515 | // In the case of a String object we just need to redirect the assignment to |
| 4516 | // the underlying string if the index is in range. Since the underlying |
| 4517 | // string does nothing with the assignment then we can ignore such |
| 4518 | // assignments. |
| 4519 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4520 | return *value; |
| 4521 | } |
| 4522 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4523 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | if (key->IsString()) { |
| 4527 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4528 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4529 | } else { |
| 4530 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4531 | key_string->TryFlatten(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4532 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4533 | *value, |
| 4534 | attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4535 | } |
| 4536 | } |
| 4537 | |
| 4538 | // Call-back into JavaScript to convert the key to a string. |
| 4539 | bool has_pending_exception = false; |
| 4540 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4541 | if (has_pending_exception) return Failure::Exception(); |
| 4542 | Handle<String> name = Handle<String>::cast(converted); |
| 4543 | |
| 4544 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4545 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4546 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4547 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4552 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4553 | Handle<JSReceiver> receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4554 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4555 | HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4556 | |
| 4557 | // Check if the given key is an array index. |
| 4558 | uint32_t index; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4559 | if (receiver->IsJSObject() && key->ToArrayIndex(&index)) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4560 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4561 | // characters of a string using [] notation. In the case of a |
| 4562 | // String object we just need to redirect the deletion to the |
| 4563 | // underlying string if the index is in range. Since the |
| 4564 | // underlying string does nothing with the deletion, we can ignore |
| 4565 | // such deletions. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4566 | if (receiver->IsStringObjectWithCharacterAt(index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4567 | return isolate->heap()->true_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4570 | return JSObject::cast(*receiver)->DeleteElement( |
| 4571 | index, JSReceiver::FORCE_DELETION); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | Handle<String> key_string; |
| 4575 | if (key->IsString()) { |
| 4576 | key_string = Handle<String>::cast(key); |
| 4577 | } else { |
| 4578 | // Call-back into JavaScript to convert the key to a string. |
| 4579 | bool has_pending_exception = false; |
| 4580 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4581 | if (has_pending_exception) return Failure::Exception(); |
| 4582 | key_string = Handle<String>::cast(converted); |
| 4583 | } |
| 4584 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4585 | key_string->TryFlatten(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4586 | return receiver->DeleteProperty(*key_string, JSReceiver::FORCE_DELETION); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4590 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4591 | NoHandleAllocation ha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4592 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4593 | |
| 4594 | Handle<Object> object = args.at<Object>(0); |
| 4595 | Handle<Object> key = args.at<Object>(1); |
| 4596 | Handle<Object> value = args.at<Object>(2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4597 | CONVERT_SMI_ARG_CHECKED(unchecked_attributes, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4598 | RUNTIME_ASSERT( |
| 4599 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4600 | // Compute attributes. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4601 | PropertyAttributes attributes = |
| 4602 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4603 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4604 | StrictModeFlag strict_mode = kNonStrictMode; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4605 | if (args.length() == 5) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4606 | CONVERT_SMI_ARG_CHECKED(strict_unchecked, 4); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4607 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 4608 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4609 | strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4610 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4611 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4612 | return Runtime::SetObjectProperty(isolate, |
| 4613 | object, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4614 | key, |
| 4615 | value, |
| 4616 | attributes, |
| 4617 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4621 | // Set the native flag on the function. |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4622 | // This is used to decide if we should transform null and undefined |
| 4623 | // into the global object when doing call and apply. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4624 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNativeFlag) { |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4625 | NoHandleAllocation ha; |
| 4626 | RUNTIME_ASSERT(args.length() == 1); |
| 4627 | |
| 4628 | Handle<Object> object = args.at<Object>(0); |
| 4629 | |
| 4630 | if (object->IsJSFunction()) { |
| 4631 | JSFunction* func = JSFunction::cast(*object); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4632 | func->shared()->set_native(true); |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4633 | } |
| 4634 | return isolate->heap()->undefined_value(); |
| 4635 | } |
| 4636 | |
| 4637 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4638 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4639 | // exist, it will be added with attributes NONE. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4640 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4641 | NoHandleAllocation ha; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4642 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4643 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4644 | CONVERT_CHECKED(String, name, args[1]); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4645 | // Compute attributes. |
| 4646 | PropertyAttributes attributes = NONE; |
| 4647 | if (args.length() == 4) { |
| 4648 | CONVERT_CHECKED(Smi, value_obj, args[3]); |
| 4649 | int unchecked_value = value_obj->value(); |
| 4650 | // Only attribute bits should be set. |
| 4651 | RUNTIME_ASSERT( |
| 4652 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4653 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4654 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4655 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4656 | return object-> |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4657 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4661 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4662 | NoHandleAllocation ha; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4663 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4664 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4665 | CONVERT_CHECKED(JSReceiver, object, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4666 | CONVERT_CHECKED(String, key, args[1]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4667 | CONVERT_SMI_ARG_CHECKED(strict, 2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4668 | return object->DeleteProperty(key, (strict == kStrictMode) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4669 | ? JSReceiver::STRICT_DELETION |
| 4670 | : JSReceiver::NORMAL_DELETION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4674 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4675 | Handle<JSObject> object, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4676 | Handle<String> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4677 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4678 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4679 | // then we have to check it for properties, because they are supposed to |
| 4680 | // look like they are on this object. |
| 4681 | Handle<Object> proto(object->GetPrototype()); |
| 4682 | if (proto->IsJSObject() && |
| 4683 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4684 | return HasLocalPropertyImplementation(isolate, |
| 4685 | Handle<JSObject>::cast(proto), |
| 4686 | key); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4687 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4688 | return isolate->heap()->false_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4692 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLocalProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4693 | NoHandleAllocation ha; |
| 4694 | ASSERT(args.length() == 2); |
| 4695 | CONVERT_CHECKED(String, key, args[1]); |
| 4696 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4697 | uint32_t index; |
| 4698 | const bool key_is_array_index = key->AsArrayIndex(&index); |
| 4699 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4700 | Object* obj = args[0]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4701 | // Only JS objects can have properties. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4702 | if (obj->IsJSObject()) { |
| 4703 | JSObject* object = JSObject::cast(obj); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4704 | // Fast case: either the key is a real named property or it is not |
| 4705 | // an array index and there are no interceptors or hidden |
| 4706 | // prototypes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4707 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4708 | Map* map = object->map(); |
| 4709 | if (!key_is_array_index && |
| 4710 | !map->has_named_interceptor() && |
| 4711 | !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) { |
| 4712 | return isolate->heap()->false_value(); |
| 4713 | } |
| 4714 | // Slow case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4715 | HandleScope scope(isolate); |
| 4716 | return HasLocalPropertyImplementation(isolate, |
| 4717 | Handle<JSObject>(object), |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4718 | Handle<String>(key)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4719 | } else if (obj->IsString() && key_is_array_index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4720 | // Well, there is one exception: Handle [] on strings. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4721 | String* string = String::cast(obj); |
| 4722 | if (index < static_cast<uint32_t>(string->length())) { |
| 4723 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4726 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4730 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4731 | NoHandleAllocation na; |
| 4732 | ASSERT(args.length() == 2); |
| 4733 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 4734 | // Only JS receivers can have properties. |
| 4735 | if (args[0]->IsJSReceiver()) { |
| 4736 | JSReceiver* receiver = JSReceiver::cast(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4737 | CONVERT_CHECKED(String, key, args[1]); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 4738 | if (receiver->HasProperty(key)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4739 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4740 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4744 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasElement) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4745 | NoHandleAllocation na; |
| 4746 | ASSERT(args.length() == 2); |
| 4747 | |
| 4748 | // Only JS objects can have elements. |
| 4749 | if (args[0]->IsJSObject()) { |
| 4750 | JSObject* object = JSObject::cast(args[0]); |
| 4751 | CONVERT_CHECKED(Smi, index_obj, args[1]); |
| 4752 | uint32_t index = index_obj->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4753 | if (object->HasElement(index)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4754 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4755 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4759 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsPropertyEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4760 | NoHandleAllocation ha; |
| 4761 | ASSERT(args.length() == 2); |
| 4762 | |
| 4763 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4764 | CONVERT_CHECKED(String, key, args[1]); |
| 4765 | |
| 4766 | uint32_t index; |
| 4767 | if (key->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4768 | return isolate->heap()->ToBoolean(object->HasElement(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4771 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4772 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4776 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4777 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4778 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4779 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4780 | return *GetKeysFor(object); |
| 4781 | } |
| 4782 | |
| 4783 | |
| 4784 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4785 | // or, if the given object has an enum cache that contains |
| 4786 | // all enumerable properties of the object and its prototypes |
| 4787 | // have none, the map of the object. This is used to speed up |
| 4788 | // the check for deletions during a for-in. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4789 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNamesFast) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4790 | ASSERT(args.length() == 1); |
| 4791 | |
| 4792 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
| 4793 | |
| 4794 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4795 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4796 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4797 | Handle<JSObject> object(raw_object); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4798 | Handle<FixedArray> content = GetKeysInFixedArrayFor(object, |
| 4799 | INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4800 | |
| 4801 | // Test again, since cache may have been built by preceding call. |
| 4802 | if (object->IsSimpleEnum()) return object->map(); |
| 4803 | |
| 4804 | return *content; |
| 4805 | } |
| 4806 | |
| 4807 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4808 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4809 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4810 | // is prototype for. |
| 4811 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4812 | int count = 1; |
| 4813 | Object* proto = obj->GetPrototype(); |
| 4814 | while (proto->IsJSObject() && |
| 4815 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4816 | count++; |
| 4817 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4818 | } |
| 4819 | return count; |
| 4820 | } |
| 4821 | |
| 4822 | |
| 4823 | // Return the names of the local named properties. |
| 4824 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4825 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4826 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4827 | ASSERT(args.length() == 1); |
| 4828 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4829 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4830 | } |
| 4831 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4832 | |
| 4833 | // Skip the global proxy as it has no properties and always delegates to the |
| 4834 | // real global object. |
| 4835 | if (obj->IsJSGlobalProxy()) { |
| 4836 | // Only collect names if access is permitted. |
| 4837 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4838 | !isolate->MayNamedAccess(*obj, |
| 4839 | isolate->heap()->undefined_value(), |
| 4840 | v8::ACCESS_KEYS)) { |
| 4841 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4842 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4843 | } |
| 4844 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4845 | } |
| 4846 | |
| 4847 | // Find the number of objects making up this. |
| 4848 | int length = LocalPrototypeChainLength(*obj); |
| 4849 | |
| 4850 | // Find the number of local properties for each of the objects. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 4851 | ScopedVector<int> local_property_count(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4852 | int total_property_count = 0; |
| 4853 | Handle<JSObject> jsproto = obj; |
| 4854 | for (int i = 0; i < length; i++) { |
| 4855 | // Only collect names if access is permitted. |
| 4856 | if (jsproto->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4857 | !isolate->MayNamedAccess(*jsproto, |
| 4858 | isolate->heap()->undefined_value(), |
| 4859 | v8::ACCESS_KEYS)) { |
| 4860 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 4861 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4862 | } |
| 4863 | int n; |
| 4864 | n = jsproto->NumberOfLocalProperties(static_cast<PropertyAttributes>(NONE)); |
| 4865 | local_property_count[i] = n; |
| 4866 | total_property_count += n; |
| 4867 | if (i < length - 1) { |
| 4868 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | // Allocate an array with storage for all the property names. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4873 | Handle<FixedArray> names = |
| 4874 | isolate->factory()->NewFixedArray(total_property_count); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4875 | |
| 4876 | // Get the property names. |
| 4877 | jsproto = obj; |
| 4878 | int proto_with_hidden_properties = 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4879 | int next_copy_index = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4880 | for (int i = 0; i < length; i++) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 4881 | jsproto->GetLocalPropertyNames(*names, next_copy_index); |
| 4882 | next_copy_index += local_property_count[i]; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 4883 | if (jsproto->HasHiddenProperties()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4884 | proto_with_hidden_properties++; |
| 4885 | } |
| 4886 | if (i < length - 1) { |
| 4887 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | // Filter out name of hidden propeties object. |
| 4892 | if (proto_with_hidden_properties > 0) { |
| 4893 | Handle<FixedArray> old_names = names; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4894 | names = isolate->factory()->NewFixedArray( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4895 | names->length() - proto_with_hidden_properties); |
| 4896 | int dest_pos = 0; |
| 4897 | for (int i = 0; i < total_property_count; i++) { |
| 4898 | Object* name = old_names->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4899 | if (name == isolate->heap()->hidden_symbol()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4900 | continue; |
| 4901 | } |
| 4902 | names->set(dest_pos++, name); |
| 4903 | } |
| 4904 | } |
| 4905 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4906 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | |
| 4910 | // Return the names of the local indexed properties. |
| 4911 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4912 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4913 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4914 | ASSERT(args.length() == 1); |
| 4915 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4916 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4917 | } |
| 4918 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4919 | |
| 4920 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4921 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4922 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4923 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | |
| 4927 | // Return information on whether an object has a named or indexed interceptor. |
| 4928 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetInterceptorInfo) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4930 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4931 | ASSERT(args.length() == 1); |
| 4932 | if (!args[0]->IsJSObject()) { |
| 4933 | return Smi::FromInt(0); |
| 4934 | } |
| 4935 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4936 | |
| 4937 | int result = 0; |
| 4938 | if (obj->HasNamedInterceptor()) result |= 2; |
| 4939 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 4940 | |
| 4941 | return Smi::FromInt(result); |
| 4942 | } |
| 4943 | |
| 4944 | |
| 4945 | // Return property names from named interceptor. |
| 4946 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4947 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetNamedInterceptorPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4948 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4949 | ASSERT(args.length() == 1); |
| 4950 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4951 | |
| 4952 | if (obj->HasNamedInterceptor()) { |
| 4953 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 4954 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4955 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4956 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | |
| 4960 | // Return element names from indexed interceptor. |
| 4961 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4962 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetIndexedInterceptorElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4963 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4964 | ASSERT(args.length() == 1); |
| 4965 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4966 | |
| 4967 | if (obj->HasIndexedInterceptor()) { |
| 4968 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 4969 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4970 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4971 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4975 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LocalKeys) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4976 | ASSERT_EQ(args.length(), 1); |
| 4977 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4978 | HandleScope scope(isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4979 | Handle<JSObject> object(raw_object); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4980 | |
| 4981 | if (object->IsJSGlobalProxy()) { |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4982 | // Do access checks before going to the global object. |
| 4983 | if (object->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4984 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4985 | v8::ACCESS_KEYS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4986 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 4987 | return *isolate->factory()->NewJSArray(0); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4990 | Handle<Object> proto(object->GetPrototype()); |
| 4991 | // If proxy is detached we simply return an empty array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4992 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4993 | object = Handle<JSObject>::cast(proto); |
| 4994 | } |
| 4995 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4996 | Handle<FixedArray> contents = GetKeysInFixedArrayFor(object, |
| 4997 | LOCAL_ONLY); |
| 4998 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 4999 | // property array and since the result is mutable we have to create |
| 5000 | // a fresh clone on each invocation. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5001 | int length = contents->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5002 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5003 | for (int i = 0; i < length; i++) { |
| 5004 | Object* entry = contents->get(i); |
| 5005 | if (entry->IsString()) { |
| 5006 | copy->set(i, entry); |
| 5007 | } else { |
| 5008 | ASSERT(entry->IsNumber()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5009 | HandleScope scope(isolate); |
| 5010 | Handle<Object> entry_handle(entry, isolate); |
| 5011 | Handle<Object> entry_str = |
| 5012 | isolate->factory()->NumberToString(entry_handle); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5013 | copy->set(i, *entry_str); |
| 5014 | } |
| 5015 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5016 | return *isolate->factory()->NewJSArrayWithElements(copy); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5020 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArgumentsProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5021 | NoHandleAllocation ha; |
| 5022 | ASSERT(args.length() == 1); |
| 5023 | |
| 5024 | // Compute the frame holding the arguments. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5025 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5026 | it.AdvanceToArgumentsFrame(); |
| 5027 | JavaScriptFrame* frame = it.frame(); |
| 5028 | |
| 5029 | // Get the actual number of provided arguments. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5030 | const uint32_t n = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5031 | |
| 5032 | // Try to convert the key to an index. If successful and within |
| 5033 | // index return the the argument from the frame. |
| 5034 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5035 | if (args[0]->ToArrayIndex(&index) && index < n) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5036 | return frame->GetParameter(index); |
| 5037 | } |
| 5038 | |
| 5039 | // Convert the key to a string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5040 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5041 | bool exception = false; |
| 5042 | Handle<Object> converted = |
| 5043 | Execution::ToString(args.at<Object>(0), &exception); |
| 5044 | if (exception) return Failure::Exception(); |
| 5045 | Handle<String> key = Handle<String>::cast(converted); |
| 5046 | |
| 5047 | // Try to convert the string key into an array index. |
| 5048 | if (key->AsArrayIndex(&index)) { |
| 5049 | if (index < n) { |
| 5050 | return frame->GetParameter(index); |
| 5051 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5052 | return isolate->initial_object_prototype()->GetElement(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | // Handle special arguments properties. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5057 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 5058 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 5059 | Object* function = frame->function(); |
| 5060 | if (function->IsJSFunction() && |
| 5061 | JSFunction::cast(function)->shared()->strict_mode()) { |
| 5062 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 5063 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 5064 | } |
| 5065 | return function; |
| 5066 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5067 | |
| 5068 | // Lookup in the initial Object.prototype object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5069 | return isolate->initial_object_prototype()->GetProperty(*key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5073 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToFastProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5074 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5075 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5076 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5077 | Handle<Object> object = args.at<Object>(0); |
| 5078 | if (object->IsJSObject()) { |
| 5079 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5080 | if (!js_object->HasFastProperties() && !js_object->IsGlobalObject()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5081 | MaybeObject* ok = js_object->TransformToFastProperties(0); |
| 5082 | if (ok->IsRetryAfterGC()) return ok; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5083 | } |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5084 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5085 | return *object; |
| 5086 | } |
| 5087 | |
| 5088 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5089 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToSlowProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5090 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5091 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5092 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5093 | Handle<Object> object = args.at<Object>(0); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 5094 | if (object->IsJSObject() && !object->IsJSGlobalProxy()) { |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5095 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5096 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 5097 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5098 | return *object; |
| 5099 | } |
| 5100 | |
| 5101 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5102 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToBool) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5103 | NoHandleAllocation ha; |
| 5104 | ASSERT(args.length() == 1); |
| 5105 | |
| 5106 | return args[0]->ToBoolean(); |
| 5107 | } |
| 5108 | |
| 5109 | |
| 5110 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 5111 | // Possible optimizations: put the type string into the oddballs. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5112 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Typeof) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5113 | NoHandleAllocation ha; |
| 5114 | |
| 5115 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5116 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5117 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 5118 | |
| 5119 | // typeof an undetectable object is 'undefined' |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5120 | if (heap_obj->map()->is_undetectable()) { |
| 5121 | return isolate->heap()->undefined_symbol(); |
| 5122 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5123 | |
| 5124 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 5125 | if (instance_type < FIRST_NONSTRING_TYPE) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5126 | return isolate->heap()->string_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | switch (instance_type) { |
| 5130 | case ODDBALL_TYPE: |
| 5131 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5132 | return isolate->heap()->boolean_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5133 | } |
| 5134 | if (heap_obj->IsNull()) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 5135 | return FLAG_harmony_typeof |
| 5136 | ? isolate->heap()->null_symbol() |
| 5137 | : isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5138 | } |
| 5139 | ASSERT(heap_obj->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5140 | return isolate->heap()->undefined_symbol(); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5141 | case JS_FUNCTION_TYPE: |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 5142 | case JS_FUNCTION_PROXY_TYPE: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5143 | return isolate->heap()->function_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5144 | default: |
| 5145 | // For any kind of object not handled above, the spec rule for |
| 5146 | // host objects gives that it is okay to return "object" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5147 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5152 | static bool AreDigits(const char*s, int from, int to) { |
| 5153 | for (int i = from; i < to; i++) { |
| 5154 | if (s[i] < '0' || s[i] > '9') return false; |
| 5155 | } |
| 5156 | |
| 5157 | return true; |
| 5158 | } |
| 5159 | |
| 5160 | |
| 5161 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 5162 | ASSERT(to - from < 10); // Overflow is not possible. |
| 5163 | ASSERT(from < to); |
| 5164 | int d = s[from] - '0'; |
| 5165 | |
| 5166 | for (int i = from + 1; i < to; i++) { |
| 5167 | d = 10 * d + (s[i] - '0'); |
| 5168 | } |
| 5169 | |
| 5170 | return d; |
| 5171 | } |
| 5172 | |
| 5173 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5174 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5175 | NoHandleAllocation ha; |
| 5176 | ASSERT(args.length() == 1); |
| 5177 | CONVERT_CHECKED(String, subject, args[0]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5178 | subject->TryFlatten(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5179 | |
| 5180 | // Fast case: short integer or some sorts of junk values. |
| 5181 | int len = subject->length(); |
| 5182 | if (subject->IsSeqAsciiString()) { |
| 5183 | if (len == 0) return Smi::FromInt(0); |
| 5184 | |
| 5185 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 5186 | bool minus = (data[0] == '-'); |
| 5187 | int start_pos = (minus ? 1 : 0); |
| 5188 | |
| 5189 | if (start_pos == len) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5190 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5191 | } else if (data[start_pos] > '9') { |
| 5192 | // Fast check for a junk value. A valid string may start from a |
| 5193 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 5194 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 5195 | // '9' except 'I'. |
| 5196 | if (data[start_pos] != 'I') { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5197 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5198 | } |
| 5199 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 5200 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 5201 | // know it will fit into the smi-data type. |
| 5202 | int d = ParseDecimalInteger(data, start_pos, len); |
| 5203 | if (minus) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5204 | if (d == 0) return isolate->heap()->minus_zero_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5205 | d = -d; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5206 | } else if (!subject->HasHashCode() && |
| 5207 | len <= String::kMaxArrayIndexSize && |
| 5208 | (len == 1 || data[0] != '0')) { |
| 5209 | // String hash is not calculated yet but all the data are present. |
| 5210 | // Update the hash field to speed up sequential convertions. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 5211 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5212 | #ifdef DEBUG |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5213 | subject->Hash(); // Force hash calculation. |
| 5214 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 5215 | static_cast<int>(hash)); |
| 5216 | #endif |
| 5217 | subject->set_hash_field(hash); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5218 | } |
| 5219 | return Smi::FromInt(d); |
| 5220 | } |
| 5221 | } |
| 5222 | |
| 5223 | // Slower case. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5224 | return isolate->heap()->NumberFromDouble( |
| 5225 | StringToDouble(isolate->unicode_cache(), subject, ALLOW_HEX)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5226 | } |
| 5227 | |
| 5228 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5229 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringFromCharCodeArray) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5230 | NoHandleAllocation ha; |
| 5231 | ASSERT(args.length() == 1); |
| 5232 | |
| 5233 | CONVERT_CHECKED(JSArray, codes, args[0]); |
| 5234 | int length = Smi::cast(codes->length())->value(); |
| 5235 | |
| 5236 | // Check if the string can be ASCII. |
| 5237 | int i; |
| 5238 | for (i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5239 | Object* element; |
| 5240 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5241 | // We probably can't get an exception here, but just in order to enforce |
| 5242 | // the checking of inputs in the runtime calls we check here. |
| 5243 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5244 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5245 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 5246 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 5247 | break; |
| 5248 | } |
| 5249 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5250 | MaybeObject* maybe_object = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5251 | if (i == length) { // The string is ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5252 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5253 | } else { // The string is not ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5254 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5257 | Object* object = NULL; |
| 5258 | if (!maybe_object->ToObject(&object)) return maybe_object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5259 | String* result = String::cast(object); |
| 5260 | for (int i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5261 | Object* element; |
| 5262 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5263 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5264 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5265 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5266 | result->Set(i, chr & 0xffff); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5267 | } |
| 5268 | return result; |
| 5269 | } |
| 5270 | |
| 5271 | |
| 5272 | // kNotEscaped is generated by the following: |
| 5273 | // |
| 5274 | // #!/bin/perl |
| 5275 | // for (my $i = 0; $i < 256; $i++) { |
| 5276 | // print "\n" if $i % 16 == 0; |
| 5277 | // my $c = chr($i); |
| 5278 | // my $escaped = 1; |
| 5279 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 5280 | // print $escaped ? "0, " : "1, "; |
| 5281 | // } |
| 5282 | |
| 5283 | |
| 5284 | static bool IsNotEscaped(uint16_t character) { |
| 5285 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 5286 | ASSERT(character < 256); |
| 5287 | static const char kNotEscaped[256] = { |
| 5288 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5289 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5290 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 5291 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 5292 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5293 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 5294 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5295 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 5296 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5297 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5298 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5299 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5300 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5301 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5302 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5303 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5304 | }; |
| 5305 | return kNotEscaped[character] != 0; |
| 5306 | } |
| 5307 | |
| 5308 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5309 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5310 | const char hex_chars[] = "0123456789ABCDEF"; |
| 5311 | NoHandleAllocation ha; |
| 5312 | ASSERT(args.length() == 1); |
| 5313 | CONVERT_CHECKED(String, source, args[0]); |
| 5314 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5315 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5316 | |
| 5317 | int escaped_length = 0; |
| 5318 | int length = source->length(); |
| 5319 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5320 | Access<StringInputBuffer> buffer( |
| 5321 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5322 | buffer->Reset(source); |
| 5323 | while (buffer->has_more()) { |
| 5324 | uint16_t character = buffer->GetNext(); |
| 5325 | if (character >= 256) { |
| 5326 | escaped_length += 6; |
| 5327 | } else if (IsNotEscaped(character)) { |
| 5328 | escaped_length++; |
| 5329 | } else { |
| 5330 | escaped_length += 3; |
| 5331 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5332 | // 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] | 5333 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5334 | if (escaped_length > String::kMaxLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5335 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5336 | return Failure::OutOfMemoryException(); |
| 5337 | } |
| 5338 | } |
| 5339 | } |
| 5340 | // No length change implies no change. Return original string if no change. |
| 5341 | if (escaped_length == length) { |
| 5342 | return source; |
| 5343 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5344 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5345 | { MaybeObject* maybe_o = |
| 5346 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5347 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5348 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5349 | String* destination = String::cast(o); |
| 5350 | int dest_position = 0; |
| 5351 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5352 | Access<StringInputBuffer> buffer( |
| 5353 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5354 | buffer->Rewind(); |
| 5355 | while (buffer->has_more()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5356 | uint16_t chr = buffer->GetNext(); |
| 5357 | if (chr >= 256) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5358 | destination->Set(dest_position, '%'); |
| 5359 | destination->Set(dest_position+1, 'u'); |
| 5360 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 5361 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 5362 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 5363 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5364 | dest_position += 6; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5365 | } else if (IsNotEscaped(chr)) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5366 | destination->Set(dest_position, chr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5367 | dest_position++; |
| 5368 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5369 | destination->Set(dest_position, '%'); |
| 5370 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 5371 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5372 | dest_position += 3; |
| 5373 | } |
| 5374 | } |
| 5375 | return destination; |
| 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 5380 | static const signed char kHexValue['g'] = { |
| 5381 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5382 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5383 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5384 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 5385 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5386 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5387 | -1, 10, 11, 12, 13, 14, 15 }; |
| 5388 | |
| 5389 | if (character1 > 'f') return -1; |
| 5390 | int hi = kHexValue[character1]; |
| 5391 | if (hi == -1) return -1; |
| 5392 | if (character2 > 'f') return -1; |
| 5393 | int lo = kHexValue[character2]; |
| 5394 | if (lo == -1) return -1; |
| 5395 | return (hi << 4) + lo; |
| 5396 | } |
| 5397 | |
| 5398 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5399 | static inline int Unescape(String* source, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5400 | int i, |
| 5401 | int length, |
| 5402 | int* step) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5403 | uint16_t character = source->Get(i); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5404 | int32_t hi = 0; |
| 5405 | int32_t lo = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5406 | if (character == '%' && |
| 5407 | i <= length - 6 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5408 | source->Get(i + 1) == 'u' && |
| 5409 | (hi = TwoDigitHex(source->Get(i + 2), |
| 5410 | source->Get(i + 3))) != -1 && |
| 5411 | (lo = TwoDigitHex(source->Get(i + 4), |
| 5412 | source->Get(i + 5))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5413 | *step = 6; |
| 5414 | return (hi << 8) + lo; |
| 5415 | } else if (character == '%' && |
| 5416 | i <= length - 3 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5417 | (lo = TwoDigitHex(source->Get(i + 1), |
| 5418 | source->Get(i + 2))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5419 | *step = 3; |
| 5420 | return lo; |
| 5421 | } else { |
| 5422 | *step = 1; |
| 5423 | return character; |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5428 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5429 | NoHandleAllocation ha; |
| 5430 | ASSERT(args.length() == 1); |
| 5431 | CONVERT_CHECKED(String, source, args[0]); |
| 5432 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5433 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5434 | |
| 5435 | bool ascii = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5436 | int length = source->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5437 | |
| 5438 | int unescaped_length = 0; |
| 5439 | for (int i = 0; i < length; unescaped_length++) { |
| 5440 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5441 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5442 | ascii = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5443 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5444 | i += step; |
| 5445 | } |
| 5446 | |
| 5447 | // No length change implies no change. Return original string if no change. |
| 5448 | if (unescaped_length == length) |
| 5449 | return source; |
| 5450 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5451 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5452 | { MaybeObject* maybe_o = |
| 5453 | ascii ? |
| 5454 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 5455 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5456 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5457 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5458 | String* destination = String::cast(o); |
| 5459 | |
| 5460 | int dest_position = 0; |
| 5461 | for (int i = 0; i < length; dest_position++) { |
| 5462 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5463 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5464 | i += step; |
| 5465 | } |
| 5466 | return destination; |
| 5467 | } |
| 5468 | |
| 5469 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5470 | static const unsigned int kQuoteTableLength = 128u; |
| 5471 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5472 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 5473 | static const char* const JsonQuotes = |
| 5474 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 5475 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 5476 | "\\b \\t \\n \\u000b " |
| 5477 | "\\f \\r \\u000e \\u000f " |
| 5478 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 5479 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 5480 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 5481 | "\\u001c \\u001d \\u001e \\u001f " |
| 5482 | " ! \\\" # " |
| 5483 | "$ % & ' " |
| 5484 | "( ) * + " |
| 5485 | ", - . / " |
| 5486 | "0 1 2 3 " |
| 5487 | "4 5 6 7 " |
| 5488 | "8 9 : ; " |
| 5489 | "< = > ? " |
| 5490 | "@ A B C " |
| 5491 | "D E F G " |
| 5492 | "H I J K " |
| 5493 | "L M N O " |
| 5494 | "P Q R S " |
| 5495 | "T U V W " |
| 5496 | "X Y Z [ " |
| 5497 | "\\\\ ] ^ _ " |
| 5498 | "` a b c " |
| 5499 | "d e f g " |
| 5500 | "h i j k " |
| 5501 | "l m n o " |
| 5502 | "p q r s " |
| 5503 | "t u v w " |
| 5504 | "x y z { " |
| 5505 | "| } ~ \177 "; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5506 | |
| 5507 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5508 | // For a string that is less than 32k characters it should always be |
| 5509 | // possible to allocate it in new space. |
| 5510 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5511 | |
| 5512 | |
| 5513 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5514 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5515 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5516 | static const int kSpaceForQuotesAndComma = 3; |
| 5517 | static const int kSpaceForBrackets = 2; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5518 | |
| 5519 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5520 | // quoting). |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5521 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5522 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5523 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5524 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5525 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5526 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5527 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5528 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5529 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5530 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5531 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5532 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5533 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5534 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5535 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5536 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5537 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5538 | }; |
| 5539 | |
| 5540 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5541 | template <typename StringType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5542 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5543 | |
| 5544 | |
| 5545 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5546 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5547 | return isolate->heap()->AllocateRawTwoByteString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | |
| 5551 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5552 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5553 | return isolate->heap()->AllocateRawAsciiString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5557 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5558 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5559 | Vector<const Char> characters) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5560 | int length = characters.length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5561 | const Char* read_cursor = characters.start(); |
| 5562 | const Char* end = read_cursor + length; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5563 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5564 | int quoted_length = kSpaceForQuotes; |
| 5565 | while (read_cursor < end) { |
| 5566 | Char c = *(read_cursor++); |
| 5567 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5568 | quoted_length++; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5569 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5570 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5571 | } |
| 5572 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5573 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5574 | quoted_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5575 | Object* new_object; |
| 5576 | if (!new_alloc->ToObject(&new_object)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5577 | return new_alloc; |
| 5578 | } |
| 5579 | StringType* new_string = StringType::cast(new_object); |
| 5580 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5581 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5582 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5583 | if (comma) *(write_cursor++) = ','; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5584 | *(write_cursor++) = '"'; |
| 5585 | |
| 5586 | read_cursor = characters.start(); |
| 5587 | while (read_cursor < end) { |
| 5588 | Char c = *(read_cursor++); |
| 5589 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5590 | *(write_cursor++) = c; |
| 5591 | } else { |
| 5592 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5593 | const char* replacement = JsonQuotes + |
| 5594 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5595 | for (int i = 0; i < len; i++) { |
| 5596 | *write_cursor++ = *replacement++; |
| 5597 | } |
| 5598 | } |
| 5599 | } |
| 5600 | *(write_cursor++) = '"'; |
| 5601 | return new_string; |
| 5602 | } |
| 5603 | |
| 5604 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5605 | template <typename SinkChar, typename SourceChar> |
| 5606 | static inline SinkChar* WriteQuoteJsonString( |
| 5607 | Isolate* isolate, |
| 5608 | SinkChar* write_cursor, |
| 5609 | Vector<const SourceChar> characters) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5610 | // SinkChar is only char if SourceChar is guaranteed to be char. |
| 5611 | ASSERT(sizeof(SinkChar) >= sizeof(SourceChar)); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5612 | const SourceChar* read_cursor = characters.start(); |
| 5613 | const SourceChar* end = read_cursor + characters.length(); |
| 5614 | *(write_cursor++) = '"'; |
| 5615 | while (read_cursor < end) { |
| 5616 | SourceChar c = *(read_cursor++); |
| 5617 | if (sizeof(SourceChar) > 1u && |
| 5618 | static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5619 | *(write_cursor++) = static_cast<SinkChar>(c); |
| 5620 | } else { |
| 5621 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5622 | const char* replacement = JsonQuotes + |
| 5623 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5624 | write_cursor[0] = replacement[0]; |
| 5625 | if (len > 1) { |
| 5626 | write_cursor[1] = replacement[1]; |
| 5627 | if (len > 2) { |
| 5628 | ASSERT(len == 6); |
| 5629 | write_cursor[2] = replacement[2]; |
| 5630 | write_cursor[3] = replacement[3]; |
| 5631 | write_cursor[4] = replacement[4]; |
| 5632 | write_cursor[5] = replacement[5]; |
| 5633 | } |
| 5634 | } |
| 5635 | write_cursor += len; |
| 5636 | } |
| 5637 | } |
| 5638 | *(write_cursor++) = '"'; |
| 5639 | return write_cursor; |
| 5640 | } |
| 5641 | |
| 5642 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5643 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5644 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5645 | Vector<const Char> characters) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5646 | int length = characters.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5647 | isolate->counters()->quote_json_char_count()->Increment(length); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5648 | int worst_case_length = |
| 5649 | length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotesAndComma; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5650 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5651 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5654 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5655 | worst_case_length); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5656 | Object* new_object; |
| 5657 | if (!new_alloc->ToObject(&new_object)) { |
| 5658 | return new_alloc; |
| 5659 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5660 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5661 | // Even if our string is small enough to fit in new space we still have to |
| 5662 | // handle it being allocated in old space as may happen in the third |
| 5663 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5664 | // CEntryStub::GenerateCore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5665 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5666 | } |
| 5667 | StringType* new_string = StringType::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5668 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5669 | |
| 5670 | STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
| 5671 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5672 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5673 | if (comma) *(write_cursor++) = ','; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5674 | write_cursor = WriteQuoteJsonString<Char, Char>(isolate, |
| 5675 | write_cursor, |
| 5676 | characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5677 | int final_length = static_cast<int>( |
| 5678 | write_cursor - reinterpret_cast<Char*>( |
| 5679 | new_string->address() + SeqAsciiString::kHeaderSize)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5680 | isolate->heap()->new_space()-> |
| 5681 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5682 | new_string, final_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5683 | return new_string; |
| 5684 | } |
| 5685 | |
| 5686 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5687 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5688 | NoHandleAllocation ha; |
| 5689 | CONVERT_CHECKED(String, str, args[0]); |
| 5690 | if (!str->IsFlat()) { |
| 5691 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5692 | Object* flat; |
| 5693 | if (!try_flatten->ToObject(&flat)) { |
| 5694 | return try_flatten; |
| 5695 | } |
| 5696 | str = String::cast(flat); |
| 5697 | ASSERT(str->IsFlat()); |
| 5698 | } |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5699 | String::FlatContent flat = str->GetFlatContent(); |
| 5700 | ASSERT(flat.IsFlat()); |
| 5701 | if (flat.IsTwoByte()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5702 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5703 | flat.ToUC16Vector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5704 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5705 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5706 | flat.ToAsciiVector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5711 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringComma) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5712 | NoHandleAllocation ha; |
| 5713 | CONVERT_CHECKED(String, str, args[0]); |
| 5714 | if (!str->IsFlat()) { |
| 5715 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5716 | Object* flat; |
| 5717 | if (!try_flatten->ToObject(&flat)) { |
| 5718 | return try_flatten; |
| 5719 | } |
| 5720 | str = String::cast(flat); |
| 5721 | ASSERT(str->IsFlat()); |
| 5722 | } |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5723 | String::FlatContent flat = str->GetFlatContent(); |
| 5724 | if (flat.IsTwoByte()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5725 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5726 | flat.ToUC16Vector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5727 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5728 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5729 | flat.ToAsciiVector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5730 | } |
| 5731 | } |
| 5732 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5733 | |
| 5734 | template <typename Char, typename StringType> |
| 5735 | static MaybeObject* QuoteJsonStringArray(Isolate* isolate, |
| 5736 | FixedArray* array, |
| 5737 | int worst_case_length) { |
| 5738 | int length = array->length(); |
| 5739 | |
| 5740 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5741 | worst_case_length); |
| 5742 | Object* new_object; |
| 5743 | if (!new_alloc->ToObject(&new_object)) { |
| 5744 | return new_alloc; |
| 5745 | } |
| 5746 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
| 5747 | // Even if our string is small enough to fit in new space we still have to |
| 5748 | // handle it being allocated in old space as may happen in the third |
| 5749 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5750 | // CEntryStub::GenerateCore. |
| 5751 | return isolate->heap()->undefined_value(); |
| 5752 | } |
| 5753 | AssertNoAllocation no_gc; |
| 5754 | StringType* new_string = StringType::cast(new_object); |
| 5755 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
| 5756 | |
| 5757 | STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
| 5758 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5759 | new_string->address() + SeqAsciiString::kHeaderSize); |
| 5760 | *(write_cursor++) = '['; |
| 5761 | for (int i = 0; i < length; i++) { |
| 5762 | if (i != 0) *(write_cursor++) = ','; |
| 5763 | String* str = String::cast(array->get(i)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5764 | String::FlatContent content = str->GetFlatContent(); |
| 5765 | ASSERT(content.IsFlat()); |
| 5766 | if (content.IsTwoByte()) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5767 | write_cursor = WriteQuoteJsonString<Char, uc16>(isolate, |
| 5768 | write_cursor, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5769 | content.ToUC16Vector()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5770 | } else { |
| 5771 | write_cursor = WriteQuoteJsonString<Char, char>(isolate, |
| 5772 | write_cursor, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5773 | content.ToAsciiVector()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5774 | } |
| 5775 | } |
| 5776 | *(write_cursor++) = ']'; |
| 5777 | |
| 5778 | int final_length = static_cast<int>( |
| 5779 | write_cursor - reinterpret_cast<Char*>( |
| 5780 | new_string->address() + SeqAsciiString::kHeaderSize)); |
| 5781 | isolate->heap()->new_space()-> |
| 5782 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5783 | new_string, final_length); |
| 5784 | return new_string; |
| 5785 | } |
| 5786 | |
| 5787 | |
| 5788 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringArray) { |
| 5789 | NoHandleAllocation ha; |
| 5790 | ASSERT(args.length() == 1); |
| 5791 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 5792 | |
| 5793 | if (!array->HasFastElements()) return isolate->heap()->undefined_value(); |
| 5794 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 5795 | int n = elements->length(); |
| 5796 | bool ascii = true; |
| 5797 | int total_length = 0; |
| 5798 | |
| 5799 | for (int i = 0; i < n; i++) { |
| 5800 | Object* elt = elements->get(i); |
| 5801 | if (!elt->IsString()) return isolate->heap()->undefined_value(); |
| 5802 | String* element = String::cast(elt); |
| 5803 | if (!element->IsFlat()) return isolate->heap()->undefined_value(); |
| 5804 | total_length += element->length(); |
| 5805 | if (ascii && element->IsTwoByteRepresentation()) { |
| 5806 | ascii = false; |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | int worst_case_length = |
| 5811 | kSpaceForBrackets + n * kSpaceForQuotesAndComma |
| 5812 | + total_length * kJsonQuoteWorstCaseBlowup; |
| 5813 | |
| 5814 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
| 5815 | return isolate->heap()->undefined_value(); |
| 5816 | } |
| 5817 | |
| 5818 | if (ascii) { |
| 5819 | return QuoteJsonStringArray<char, SeqAsciiString>(isolate, |
| 5820 | elements, |
| 5821 | worst_case_length); |
| 5822 | } else { |
| 5823 | return QuoteJsonStringArray<uc16, SeqTwoByteString>(isolate, |
| 5824 | elements, |
| 5825 | worst_case_length); |
| 5826 | } |
| 5827 | } |
| 5828 | |
| 5829 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5830 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseInt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5831 | NoHandleAllocation ha; |
| 5832 | |
| 5833 | CONVERT_CHECKED(String, s, args[0]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5834 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5835 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5836 | s->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5837 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5838 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5839 | double value = StringToInt(isolate->unicode_cache(), s, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5840 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5844 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseFloat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5845 | NoHandleAllocation ha; |
| 5846 | CONVERT_CHECKED(String, str, args[0]); |
| 5847 | |
| 5848 | // ECMA-262 section 15.1.2.3, empty string is NaN |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5849 | double value = StringToDouble(isolate->unicode_cache(), |
| 5850 | str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5851 | |
| 5852 | // Create a number object from the value. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5853 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5857 | template <class Converter> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5858 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5859 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5860 | String* s, |
| 5861 | int length, |
| 5862 | int input_string_length, |
| 5863 | unibrow::Mapping<Converter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5864 | // We try this twice, once with the assumption that the result is no longer |
| 5865 | // than the input and, if that assumption breaks, again with the exact |
| 5866 | // length. This may not be pretty, but it is nicer than what was here before |
| 5867 | // and I hereby claim my vaffel-is. |
| 5868 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5869 | // Allocate the resulting string. |
| 5870 | // |
| 5871 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5872 | // character is also ascii. This is currently the case, but it |
| 5873 | // might break in the future if we implement more context and locale |
| 5874 | // dependent upper/lower conversions. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5875 | Object* o; |
| 5876 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5877 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 5878 | : isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5879 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5880 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5881 | String* result = String::cast(o); |
| 5882 | bool has_changed_character = false; |
| 5883 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5884 | // Convert all characters to upper case, assuming that they will fit |
| 5885 | // in the buffer |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5886 | Access<StringInputBuffer> buffer( |
| 5887 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5888 | buffer->Reset(s); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5889 | unibrow::uchar chars[Converter::kMaxWidth]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5890 | // We can assume that the string is not empty |
| 5891 | uc32 current = buffer->GetNext(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5892 | for (int i = 0; i < length;) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5893 | bool has_next = buffer->has_more(); |
| 5894 | uc32 next = has_next ? buffer->GetNext() : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5895 | int char_length = mapping->get(current, next, chars); |
| 5896 | if (char_length == 0) { |
| 5897 | // The case conversion of this character is the character itself. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5898 | result->Set(i, current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5899 | i++; |
| 5900 | } else if (char_length == 1) { |
| 5901 | // Common case: converting the letter resulted in one character. |
| 5902 | ASSERT(static_cast<uc32>(chars[0]) != current); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5903 | result->Set(i, chars[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5904 | has_changed_character = true; |
| 5905 | i++; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5906 | } else if (length == input_string_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5907 | // We've assumed that the result would be as long as the |
| 5908 | // input but here is a character that converts to several |
| 5909 | // characters. No matter, we calculate the exact length |
| 5910 | // of the result and try the whole thing again. |
| 5911 | // |
| 5912 | // Note that this leaves room for optimization. We could just |
| 5913 | // memcpy what we already have to the result string. Also, |
| 5914 | // the result string is the last object allocated we could |
| 5915 | // "realloc" it and probably, in the vast majority of cases, |
| 5916 | // extend the existing string to be able to hold the full |
| 5917 | // result. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5918 | int next_length = 0; |
| 5919 | if (has_next) { |
| 5920 | next_length = mapping->get(next, 0, chars); |
| 5921 | if (next_length == 0) next_length = 1; |
| 5922 | } |
| 5923 | int current_length = i + char_length + next_length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5924 | while (buffer->has_more()) { |
| 5925 | current = buffer->GetNext(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5926 | // NOTE: we use 0 as the next character here because, while |
| 5927 | // the next character may affect what a character converts to, |
| 5928 | // it does not in any case affect the length of what it convert |
| 5929 | // to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5930 | int char_length = mapping->get(current, 0, chars); |
| 5931 | if (char_length == 0) char_length = 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5932 | current_length += char_length; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5933 | if (current_length > Smi::kMaxValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5934 | isolate->context()->mark_out_of_memory(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5935 | return Failure::OutOfMemoryException(); |
| 5936 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5937 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5938 | // Try again with the real length. |
| 5939 | return Smi::FromInt(current_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5940 | } else { |
| 5941 | for (int j = 0; j < char_length; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5942 | result->Set(i, chars[j]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5943 | i++; |
| 5944 | } |
| 5945 | has_changed_character = true; |
| 5946 | } |
| 5947 | current = next; |
| 5948 | } |
| 5949 | if (has_changed_character) { |
| 5950 | return result; |
| 5951 | } else { |
| 5952 | // If we didn't actually change anything in doing the conversion |
| 5953 | // we simple return the result and let the converted string |
| 5954 | // become garbage; there is no reason to keep two identical strings |
| 5955 | // alive. |
| 5956 | return s; |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5961 | namespace { |
| 5962 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5963 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 5964 | |
| 5965 | |
| 5966 | // Given a word and two range boundaries returns a word with high bit |
| 5967 | // set in every byte iff the corresponding input byte was strictly in |
| 5968 | // the range (m, n). All the other bits in the result are cleared. |
| 5969 | // This function is only useful when it can be inlined and the |
| 5970 | // boundaries are statically known. |
| 5971 | // Requires: all bytes in the input word and the boundaries must be |
| 5972 | // ascii (less than 0x7F). |
| 5973 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
| 5974 | // Every byte in an ascii string is less than or equal to 0x7F. |
| 5975 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 5976 | // Use strict inequalities since in edge cases the function could be |
| 5977 | // further simplified. |
| 5978 | ASSERT(0 < m && m < n && n < 0x7F); |
| 5979 | // Has high bit set in every w byte less than n. |
| 5980 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 5981 | // Has high bit set in every w byte greater than m. |
| 5982 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 5983 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 5984 | } |
| 5985 | |
| 5986 | |
| 5987 | enum AsciiCaseConversion { |
| 5988 | ASCII_TO_LOWER, |
| 5989 | ASCII_TO_UPPER |
| 5990 | }; |
| 5991 | |
| 5992 | |
| 5993 | template <AsciiCaseConversion dir> |
| 5994 | struct FastAsciiConverter { |
| 5995 | static bool Convert(char* dst, char* src, int length) { |
| 5996 | #ifdef DEBUG |
| 5997 | char* saved_dst = dst; |
| 5998 | char* saved_src = src; |
| 5999 | #endif |
| 6000 | // We rely on the distance between upper and lower case letters |
| 6001 | // being a known power of 2. |
| 6002 | ASSERT('a' - 'A' == (1 << 5)); |
| 6003 | // Boundaries for the range of input characters than require conversion. |
| 6004 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 6005 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 6006 | bool changed = false; |
| 6007 | char* const limit = src + length; |
| 6008 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 6009 | // Process the prefix of the input that requires no conversion one |
| 6010 | // (machine) word at a time. |
| 6011 | while (src <= limit - sizeof(uintptr_t)) { |
| 6012 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6013 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 6014 | changed = true; |
| 6015 | break; |
| 6016 | } |
| 6017 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 6018 | src += sizeof(uintptr_t); |
| 6019 | dst += sizeof(uintptr_t); |
| 6020 | } |
| 6021 | // Process the remainder of the input performing conversion when |
| 6022 | // required one word at a time. |
| 6023 | while (src <= limit - sizeof(uintptr_t)) { |
| 6024 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6025 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 6026 | // The mask has high (7th) bit set in every byte that needs |
| 6027 | // conversion and we know that the distance between cases is |
| 6028 | // 1 << 5. |
| 6029 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 6030 | src += sizeof(uintptr_t); |
| 6031 | dst += sizeof(uintptr_t); |
| 6032 | } |
| 6033 | #endif |
| 6034 | // Process the last few bytes of the input (or the whole input if |
| 6035 | // unaligned access is not supported). |
| 6036 | while (src < limit) { |
| 6037 | char c = *src; |
| 6038 | if (lo < c && c < hi) { |
| 6039 | c ^= (1 << 5); |
| 6040 | changed = true; |
| 6041 | } |
| 6042 | *dst = c; |
| 6043 | ++src; |
| 6044 | ++dst; |
| 6045 | } |
| 6046 | #ifdef DEBUG |
| 6047 | CheckConvert(saved_dst, saved_src, length, changed); |
| 6048 | #endif |
| 6049 | return changed; |
| 6050 | } |
| 6051 | |
| 6052 | #ifdef DEBUG |
| 6053 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 6054 | bool expected_changed = false; |
| 6055 | for (int i = 0; i < length; i++) { |
| 6056 | if (dst[i] == src[i]) continue; |
| 6057 | expected_changed = true; |
| 6058 | if (dir == ASCII_TO_LOWER) { |
| 6059 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 6060 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 6061 | } else { |
| 6062 | ASSERT(dir == ASCII_TO_UPPER); |
| 6063 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 6064 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 6065 | } |
| 6066 | } |
| 6067 | ASSERT(expected_changed == changed); |
| 6068 | } |
| 6069 | #endif |
| 6070 | }; |
| 6071 | |
| 6072 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6073 | struct ToLowerTraits { |
| 6074 | typedef unibrow::ToLowercase UnibrowConverter; |
| 6075 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6076 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6077 | }; |
| 6078 | |
| 6079 | |
| 6080 | struct ToUpperTraits { |
| 6081 | typedef unibrow::ToUppercase UnibrowConverter; |
| 6082 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6083 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6084 | }; |
| 6085 | |
| 6086 | } // namespace |
| 6087 | |
| 6088 | |
| 6089 | template <typename ConvertTraits> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6090 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6091 | Arguments args, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6092 | Isolate* isolate, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6093 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6094 | NoHandleAllocation ha; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6095 | CONVERT_CHECKED(String, s, args[0]); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6096 | s = s->TryFlattenGetString(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6097 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6098 | const int length = s->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6099 | // 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] | 6100 | if (length == 0) return s; |
| 6101 | |
| 6102 | // Simpler handling of ascii strings. |
| 6103 | // |
| 6104 | // NOTE: This assumes that the upper/lower case of an ascii |
| 6105 | // character is also ascii. This is currently the case, but it |
| 6106 | // might break in the future if we implement more context and locale |
| 6107 | // dependent upper/lower conversions. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6108 | if (s->IsSeqAsciiString()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6109 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6110 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6111 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 6112 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6113 | SeqAsciiString* result = SeqAsciiString::cast(o); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6114 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6115 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6116 | return has_changed_character ? result : s; |
| 6117 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6118 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6119 | Object* answer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6120 | { MaybeObject* maybe_answer = |
| 6121 | ConvertCaseHelper(isolate, s, length, length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6122 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6123 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6124 | if (answer->IsSmi()) { |
| 6125 | // Retry with correct length. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6126 | { MaybeObject* maybe_answer = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6127 | ConvertCaseHelper(isolate, |
| 6128 | s, Smi::cast(answer)->value(), length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6129 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6130 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6131 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6132 | return answer; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
| 6135 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6136 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6137 | return ConvertCase<ToLowerTraits>( |
| 6138 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6139 | } |
| 6140 | |
| 6141 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6142 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToUpperCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6143 | return ConvertCase<ToUpperTraits>( |
| 6144 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6145 | } |
| 6146 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6147 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6148 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
| 6149 | return unibrow::WhiteSpace::Is(c) || c == 0x200b; |
| 6150 | } |
| 6151 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6152 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6153 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringTrim) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6154 | NoHandleAllocation ha; |
| 6155 | ASSERT(args.length() == 3); |
| 6156 | |
| 6157 | CONVERT_CHECKED(String, s, args[0]); |
| 6158 | CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); |
| 6159 | CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); |
| 6160 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6161 | s->TryFlatten(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6162 | int length = s->length(); |
| 6163 | |
| 6164 | int left = 0; |
| 6165 | if (trimLeft) { |
| 6166 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 6167 | left++; |
| 6168 | } |
| 6169 | } |
| 6170 | |
| 6171 | int right = length; |
| 6172 | if (trimRight) { |
| 6173 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 6174 | right--; |
| 6175 | } |
| 6176 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6177 | return s->SubString(left, right); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6178 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6179 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6180 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6181 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6182 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6183 | HandleScope handle_scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6184 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 6185 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 6186 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 6187 | |
| 6188 | int subject_length = subject->length(); |
| 6189 | int pattern_length = pattern->length(); |
| 6190 | RUNTIME_ASSERT(pattern_length > 0); |
| 6191 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6192 | if (limit == 0xffffffffu) { |
| 6193 | Handle<Object> cached_answer(StringSplitCache::Lookup( |
| 6194 | isolate->heap()->string_split_cache(), |
| 6195 | *subject, |
| 6196 | *pattern)); |
| 6197 | if (*cached_answer != Smi::FromInt(0)) { |
| 6198 | Handle<JSArray> result = |
| 6199 | isolate->factory()->NewJSArrayWithElements( |
| 6200 | Handle<FixedArray>::cast(cached_answer)); |
| 6201 | return *result; |
| 6202 | } |
| 6203 | } |
| 6204 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6205 | // The limit can be very large (0xffffffffu), but since the pattern |
| 6206 | // isn't empty, we can never create more parts than ~half the length |
| 6207 | // of the subject. |
| 6208 | |
| 6209 | if (!subject->IsFlat()) FlattenString(subject); |
| 6210 | |
| 6211 | static const int kMaxInitialListCapacity = 16; |
| 6212 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 6213 | ZoneScope scope(isolate, DELETE_ON_EXIT); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6214 | |
| 6215 | // Find (up to limit) indices of separator and end-of-string in subject |
| 6216 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 6217 | ZoneList<int> indices(initial_capacity); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6218 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 6219 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 6220 | FindStringIndicesDispatch(isolate, *subject, *pattern, &indices, limit); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6221 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6222 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 6223 | indices.Add(subject_length); |
| 6224 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6225 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6226 | // 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] | 6227 | |
| 6228 | // Create JSArray of substrings separated by separator. |
| 6229 | int part_count = indices.length(); |
| 6230 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6231 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6232 | result->set_length(Smi::FromInt(part_count)); |
| 6233 | |
| 6234 | ASSERT(result->HasFastElements()); |
| 6235 | |
| 6236 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 6237 | FixedArray::cast(result->elements())->set(0, *subject); |
| 6238 | return *result; |
| 6239 | } |
| 6240 | |
| 6241 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 6242 | int part_start = 0; |
| 6243 | for (int i = 0; i < part_count; i++) { |
| 6244 | HandleScope local_loop_handle; |
| 6245 | int part_end = indices.at(i); |
| 6246 | Handle<String> substring = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6247 | isolate->factory()->NewProperSubString(subject, part_start, part_end); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6248 | elements->set(i, *substring); |
| 6249 | part_start = part_end + pattern_length; |
| 6250 | } |
| 6251 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6252 | if (limit == 0xffffffffu) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 6253 | if (result->HasFastElements()) { |
| 6254 | StringSplitCache::Enter(isolate->heap(), |
| 6255 | isolate->heap()->string_split_cache(), |
| 6256 | *subject, |
| 6257 | *pattern, |
| 6258 | *elements); |
| 6259 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6260 | } |
| 6261 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6262 | return *result; |
| 6263 | } |
| 6264 | |
| 6265 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6266 | // Copies ascii characters to the given fixed array looking up |
| 6267 | // one-char strings in the cache. Gives up on the first char that is |
| 6268 | // not in the cache and fills the remainder with smi zeros. Returns |
| 6269 | // the length of the successfully copied prefix. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6270 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 6271 | const char* chars, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6272 | FixedArray* elements, |
| 6273 | int length) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6274 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6275 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 6276 | Object* undefined = heap->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6277 | int i; |
| 6278 | for (i = 0; i < length; ++i) { |
| 6279 | Object* value = ascii_cache->get(chars[i]); |
| 6280 | if (value == undefined) break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6281 | ASSERT(!heap->InNewSpace(value)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6282 | elements->set(i, value, SKIP_WRITE_BARRIER); |
| 6283 | } |
| 6284 | if (i < length) { |
| 6285 | ASSERT(Smi::FromInt(0) == 0); |
| 6286 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 6287 | } |
| 6288 | #ifdef DEBUG |
| 6289 | for (int j = 0; j < length; ++j) { |
| 6290 | Object* element = elements->get(j); |
| 6291 | ASSERT(element == Smi::FromInt(0) || |
| 6292 | (element->IsString() && String::cast(element)->LooksValid())); |
| 6293 | } |
| 6294 | #endif |
| 6295 | return i; |
| 6296 | } |
| 6297 | |
| 6298 | |
| 6299 | // Converts a String to JSArray. |
| 6300 | // For example, "foo" => ["f", "o", "o"]. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6301 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToArray) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6302 | HandleScope scope(isolate); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 6303 | ASSERT(args.length() == 2); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6304 | CONVERT_ARG_CHECKED(String, s, 0); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 6305 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6306 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6307 | s = FlattenGetString(s); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 6308 | 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] | 6309 | |
| 6310 | Handle<FixedArray> elements; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6311 | int position = 0; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6312 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6313 | // Try using cached chars where possible. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6314 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6315 | { MaybeObject* maybe_obj = |
| 6316 | isolate->heap()->AllocateUninitializedFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6317 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6318 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6319 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6320 | String::FlatContent content = s->GetFlatContent(); |
| 6321 | if (content.IsAscii()) { |
| 6322 | Vector<const char> chars = content.ToAsciiVector(); |
| 6323 | // Note, this will initialize all elements (not only the prefix) |
| 6324 | // to prevent GC from seeing partially initialized array. |
| 6325 | position = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 6326 | chars.start(), |
| 6327 | *elements, |
| 6328 | length); |
| 6329 | } else { |
| 6330 | MemsetPointer(elements->data_start(), |
| 6331 | isolate->heap()->undefined_value(), |
| 6332 | length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6333 | } |
| 6334 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6335 | elements = isolate->factory()->NewFixedArray(length); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6336 | } |
| 6337 | for (int i = position; i < length; ++i) { |
| 6338 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 6339 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | #ifdef DEBUG |
| 6343 | for (int i = 0; i < length; ++i) { |
| 6344 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 6345 | } |
| 6346 | #endif |
| 6347 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6348 | return *isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6352 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStringWrapper) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6353 | NoHandleAllocation ha; |
| 6354 | ASSERT(args.length() == 1); |
| 6355 | CONVERT_CHECKED(String, value, args[0]); |
| 6356 | return value->ToObject(); |
| 6357 | } |
| 6358 | |
| 6359 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6360 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 6361 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6362 | 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] | 6363 | return char_length == 0; |
| 6364 | } |
| 6365 | |
| 6366 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6367 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6368 | NoHandleAllocation ha; |
| 6369 | ASSERT(args.length() == 1); |
| 6370 | |
| 6371 | Object* number = args[0]; |
| 6372 | RUNTIME_ASSERT(number->IsNumber()); |
| 6373 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6374 | return isolate->heap()->NumberToString(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6378 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToStringSkipCache) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6379 | NoHandleAllocation ha; |
| 6380 | ASSERT(args.length() == 1); |
| 6381 | |
| 6382 | Object* number = args[0]; |
| 6383 | RUNTIME_ASSERT(number->IsNumber()); |
| 6384 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6385 | return isolate->heap()->NumberToString(number, false); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6389 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToInteger) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6390 | NoHandleAllocation ha; |
| 6391 | ASSERT(args.length() == 1); |
| 6392 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6393 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6394 | |
| 6395 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6396 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6397 | return Smi::FromInt(static_cast<int>(number)); |
| 6398 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6399 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6403 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6404 | NoHandleAllocation ha; |
| 6405 | ASSERT(args.length() == 1); |
| 6406 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6407 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6408 | |
| 6409 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6410 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6411 | return Smi::FromInt(static_cast<int>(number)); |
| 6412 | } |
| 6413 | |
| 6414 | double double_value = DoubleToInteger(number); |
| 6415 | // Map both -0 and +0 to +0. |
| 6416 | if (double_value == 0) double_value = 0; |
| 6417 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6418 | return isolate->heap()->NumberFromDouble(double_value); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
| 6421 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6422 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSUint32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6423 | NoHandleAllocation ha; |
| 6424 | ASSERT(args.length() == 1); |
| 6425 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6426 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6427 | return isolate->heap()->NumberFromUint32(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6431 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSInt32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6432 | NoHandleAllocation ha; |
| 6433 | ASSERT(args.length() == 1); |
| 6434 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6435 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6436 | |
| 6437 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6438 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6439 | return Smi::FromInt(static_cast<int>(number)); |
| 6440 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6441 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6445 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 6446 | // a small integer. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6447 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToSmi) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6448 | NoHandleAllocation ha; |
| 6449 | ASSERT(args.length() == 1); |
| 6450 | |
| 6451 | Object* obj = args[0]; |
| 6452 | if (obj->IsSmi()) { |
| 6453 | return obj; |
| 6454 | } |
| 6455 | if (obj->IsHeapNumber()) { |
| 6456 | double value = HeapNumber::cast(obj)->value(); |
| 6457 | int int_value = FastD2I(value); |
| 6458 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 6459 | return Smi::FromInt(int_value); |
| 6460 | } |
| 6461 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6462 | return isolate->heap()->nan_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6465 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6466 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateHeapNumber) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6467 | NoHandleAllocation ha; |
| 6468 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6469 | return isolate->heap()->AllocateHeapNumber(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6470 | } |
| 6471 | |
| 6472 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6473 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6474 | NoHandleAllocation ha; |
| 6475 | ASSERT(args.length() == 2); |
| 6476 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6477 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6478 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6479 | return isolate->heap()->NumberFromDouble(x + y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6483 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSub) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6484 | NoHandleAllocation ha; |
| 6485 | ASSERT(args.length() == 2); |
| 6486 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6487 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6488 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6489 | return isolate->heap()->NumberFromDouble(x - y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6490 | } |
| 6491 | |
| 6492 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6493 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMul) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6494 | NoHandleAllocation ha; |
| 6495 | ASSERT(args.length() == 2); |
| 6496 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6497 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6498 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6499 | return isolate->heap()->NumberFromDouble(x * y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6503 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberUnaryMinus) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6504 | NoHandleAllocation ha; |
| 6505 | ASSERT(args.length() == 1); |
| 6506 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6507 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6508 | return isolate->heap()->NumberFromDouble(-x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
| 6511 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAlloc) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6513 | NoHandleAllocation ha; |
| 6514 | ASSERT(args.length() == 0); |
| 6515 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6516 | return isolate->heap()->NumberFromDouble(9876543210.0); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
| 6519 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6520 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberDiv) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6521 | NoHandleAllocation ha; |
| 6522 | ASSERT(args.length() == 2); |
| 6523 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6524 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6525 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6526 | return isolate->heap()->NumberFromDouble(x / y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6530 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMod) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6531 | NoHandleAllocation ha; |
| 6532 | ASSERT(args.length() == 2); |
| 6533 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6534 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6535 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6536 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6537 | x = modulo(x, y); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6538 | // NumberFromDouble may return a Smi instead of a Number object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6539 | return isolate->heap()->NumberFromDouble(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6543 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6544 | NoHandleAllocation ha; |
| 6545 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6546 | CONVERT_CHECKED(String, str1, args[0]); |
| 6547 | CONVERT_CHECKED(String, str2, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6548 | isolate->counters()->string_add_runtime()->Increment(); |
| 6549 | return isolate->heap()->AllocateConsString(str1, str2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6553 | template <typename sinkchar> |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6554 | static inline void StringBuilderConcatHelper(String* special, |
| 6555 | sinkchar* sink, |
| 6556 | FixedArray* fixed_array, |
| 6557 | int array_length) { |
| 6558 | int position = 0; |
| 6559 | for (int i = 0; i < array_length; i++) { |
| 6560 | Object* element = fixed_array->get(i); |
| 6561 | if (element->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6562 | // Smi encoding of position and length. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6563 | int encoded_slice = Smi::cast(element)->value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6564 | int pos; |
| 6565 | int len; |
| 6566 | if (encoded_slice > 0) { |
| 6567 | // Position and length encoded in one smi. |
| 6568 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 6569 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 6570 | } else { |
| 6571 | // Position and length encoded in two smis. |
| 6572 | Object* obj = fixed_array->get(++i); |
| 6573 | ASSERT(obj->IsSmi()); |
| 6574 | pos = Smi::cast(obj)->value(); |
| 6575 | len = -encoded_slice; |
| 6576 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6577 | String::WriteToFlat(special, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6578 | sink + position, |
| 6579 | pos, |
| 6580 | pos + len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6581 | position += len; |
| 6582 | } else { |
| 6583 | String* string = String::cast(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6584 | int element_length = string->length(); |
| 6585 | String::WriteToFlat(string, sink + position, 0, element_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6586 | position += element_length; |
| 6587 | } |
| 6588 | } |
| 6589 | } |
| 6590 | |
| 6591 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6592 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6593 | NoHandleAllocation ha; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6594 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6595 | CONVERT_CHECKED(JSArray, array, args[0]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6596 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6597 | isolate->context()->mark_out_of_memory(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6598 | return Failure::OutOfMemoryException(); |
| 6599 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6600 | int array_length = args.smi_at(1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6601 | CONVERT_CHECKED(String, special, args[2]); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6602 | |
| 6603 | // This assumption is used by the slice encoding in one or two smis. |
| 6604 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6605 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6606 | int special_length = special->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6607 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6608 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6609 | } |
| 6610 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6611 | if (fixed_array->length() < array_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6612 | array_length = fixed_array->length(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6613 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6614 | |
| 6615 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6616 | return isolate->heap()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6617 | } else if (array_length == 1) { |
| 6618 | Object* first = fixed_array->get(0); |
| 6619 | if (first->IsString()) return first; |
| 6620 | } |
| 6621 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6622 | bool ascii = special->HasOnlyAsciiChars(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6623 | int position = 0; |
| 6624 | for (int i = 0; i < array_length; i++) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6625 | int increment = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6626 | Object* elt = fixed_array->get(i); |
| 6627 | if (elt->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6628 | // Smi encoding of position and length. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6629 | int smi_value = Smi::cast(elt)->value(); |
| 6630 | int pos; |
| 6631 | int len; |
| 6632 | if (smi_value > 0) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6633 | // Position and length encoded in one smi. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6634 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6635 | len = StringBuilderSubstringLength::decode(smi_value); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6636 | } else { |
| 6637 | // Position and length encoded in two smis. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6638 | len = -smi_value; |
| 6639 | // Get the position and check that it is a positive smi. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6640 | i++; |
| 6641 | if (i >= array_length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6642 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6643 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6644 | Object* next_smi = fixed_array->get(i); |
| 6645 | if (!next_smi->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6646 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6647 | } |
| 6648 | pos = Smi::cast(next_smi)->value(); |
| 6649 | if (pos < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6650 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6651 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6652 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6653 | ASSERT(pos >= 0); |
| 6654 | ASSERT(len >= 0); |
| 6655 | if (pos > special_length || len > special_length - pos) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6656 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6657 | } |
| 6658 | increment = len; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6659 | } else if (elt->IsString()) { |
| 6660 | String* element = String::cast(elt); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6661 | int element_length = element->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6662 | increment = element_length; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6663 | if (ascii && !element->HasOnlyAsciiChars()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6664 | ascii = false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6665 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6666 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6667 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6668 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6669 | if (increment > String::kMaxLength - position) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6670 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6671 | return Failure::OutOfMemoryException(); |
| 6672 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6673 | position += increment; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | int length = position; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6677 | Object* object; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6678 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6679 | if (ascii) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6680 | { MaybeObject* maybe_object = |
| 6681 | isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6682 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6683 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6684 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6685 | StringBuilderConcatHelper(special, |
| 6686 | answer->GetChars(), |
| 6687 | fixed_array, |
| 6688 | array_length); |
| 6689 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6690 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6691 | { MaybeObject* maybe_object = |
| 6692 | isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6693 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6694 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6695 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6696 | StringBuilderConcatHelper(special, |
| 6697 | answer->GetChars(), |
| 6698 | fixed_array, |
| 6699 | array_length); |
| 6700 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6701 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6705 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6706 | NoHandleAllocation ha; |
| 6707 | ASSERT(args.length() == 3); |
| 6708 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 6709 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6710 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6711 | return Failure::OutOfMemoryException(); |
| 6712 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6713 | int array_length = args.smi_at(1); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6714 | CONVERT_CHECKED(String, separator, args[2]); |
| 6715 | |
| 6716 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6717 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6718 | } |
| 6719 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6720 | if (fixed_array->length() < array_length) { |
| 6721 | array_length = fixed_array->length(); |
| 6722 | } |
| 6723 | |
| 6724 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6725 | return isolate->heap()->empty_string(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6726 | } else if (array_length == 1) { |
| 6727 | Object* first = fixed_array->get(0); |
| 6728 | if (first->IsString()) return first; |
| 6729 | } |
| 6730 | |
| 6731 | int separator_length = separator->length(); |
| 6732 | int max_nof_separators = |
| 6733 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6734 | if (max_nof_separators < (array_length - 1)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6735 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6736 | return Failure::OutOfMemoryException(); |
| 6737 | } |
| 6738 | int length = (array_length - 1) * separator_length; |
| 6739 | for (int i = 0; i < array_length; i++) { |
| 6740 | Object* element_obj = fixed_array->get(i); |
| 6741 | if (!element_obj->IsString()) { |
| 6742 | // TODO(1161): handle this case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6743 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6744 | } |
| 6745 | String* element = String::cast(element_obj); |
| 6746 | int increment = element->length(); |
| 6747 | if (increment > String::kMaxLength - length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6748 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6749 | return Failure::OutOfMemoryException(); |
| 6750 | } |
| 6751 | length += increment; |
| 6752 | } |
| 6753 | |
| 6754 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6755 | { MaybeObject* maybe_object = |
| 6756 | isolate->heap()->AllocateRawTwoByteString(length); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6757 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6758 | } |
| 6759 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6760 | |
| 6761 | uc16* sink = answer->GetChars(); |
| 6762 | #ifdef DEBUG |
| 6763 | uc16* end = sink + length; |
| 6764 | #endif |
| 6765 | |
| 6766 | String* first = String::cast(fixed_array->get(0)); |
| 6767 | int first_length = first->length(); |
| 6768 | String::WriteToFlat(first, sink, 0, first_length); |
| 6769 | sink += first_length; |
| 6770 | |
| 6771 | for (int i = 1; i < array_length; i++) { |
| 6772 | ASSERT(sink + separator_length <= end); |
| 6773 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6774 | sink += separator_length; |
| 6775 | |
| 6776 | String* element = String::cast(fixed_array->get(i)); |
| 6777 | int element_length = element->length(); |
| 6778 | ASSERT(sink + element_length <= end); |
| 6779 | String::WriteToFlat(element, sink, 0, element_length); |
| 6780 | sink += element_length; |
| 6781 | } |
| 6782 | ASSERT(sink == end); |
| 6783 | |
| 6784 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6785 | return answer; |
| 6786 | } |
| 6787 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6788 | template <typename Char> |
| 6789 | static void JoinSparseArrayWithSeparator(FixedArray* elements, |
| 6790 | int elements_length, |
| 6791 | uint32_t array_length, |
| 6792 | String* separator, |
| 6793 | Vector<Char> buffer) { |
| 6794 | int previous_separator_position = 0; |
| 6795 | int separator_length = separator->length(); |
| 6796 | int cursor = 0; |
| 6797 | for (int i = 0; i < elements_length; i += 2) { |
| 6798 | int position = NumberToInt32(elements->get(i)); |
| 6799 | String* string = String::cast(elements->get(i + 1)); |
| 6800 | int string_length = string->length(); |
| 6801 | if (string->length() > 0) { |
| 6802 | while (previous_separator_position < position) { |
| 6803 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6804 | 0, separator_length); |
| 6805 | cursor += separator_length; |
| 6806 | previous_separator_position++; |
| 6807 | } |
| 6808 | String::WriteToFlat<Char>(string, &buffer[cursor], |
| 6809 | 0, string_length); |
| 6810 | cursor += string->length(); |
| 6811 | } |
| 6812 | } |
| 6813 | if (separator_length > 0) { |
| 6814 | // Array length must be representable as a signed 32-bit number, |
| 6815 | // otherwise the total string length would have been too large. |
| 6816 | ASSERT(array_length <= 0x7fffffff); // Is int32_t. |
| 6817 | int last_array_index = static_cast<int>(array_length - 1); |
| 6818 | while (previous_separator_position < last_array_index) { |
| 6819 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6820 | 0, separator_length); |
| 6821 | cursor += separator_length; |
| 6822 | previous_separator_position++; |
| 6823 | } |
| 6824 | } |
| 6825 | ASSERT(cursor <= buffer.length()); |
| 6826 | } |
| 6827 | |
| 6828 | |
| 6829 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) { |
| 6830 | NoHandleAllocation ha; |
| 6831 | ASSERT(args.length() == 3); |
| 6832 | CONVERT_CHECKED(JSArray, elements_array, args[0]); |
| 6833 | RUNTIME_ASSERT(elements_array->HasFastElements()); |
| 6834 | CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]); |
| 6835 | CONVERT_CHECKED(String, separator, args[2]); |
| 6836 | // elements_array is fast-mode JSarray of alternating positions |
| 6837 | // (increasing order) and strings. |
| 6838 | // array_length is length of original array (used to add separators); |
| 6839 | // separator is string to put between elements. Assumed to be non-empty. |
| 6840 | |
| 6841 | // Find total length of join result. |
| 6842 | int string_length = 0; |
| 6843 | bool is_ascii = true; |
| 6844 | int max_string_length = SeqAsciiString::kMaxLength; |
| 6845 | bool overflow = false; |
| 6846 | CONVERT_NUMBER_CHECKED(int, elements_length, |
| 6847 | Int32, elements_array->length()); |
| 6848 | RUNTIME_ASSERT((elements_length & 1) == 0); // Even length. |
| 6849 | FixedArray* elements = FixedArray::cast(elements_array->elements()); |
| 6850 | for (int i = 0; i < elements_length; i += 2) { |
| 6851 | RUNTIME_ASSERT(elements->get(i)->IsNumber()); |
| 6852 | CONVERT_CHECKED(String, string, elements->get(i + 1)); |
| 6853 | int length = string->length(); |
| 6854 | if (is_ascii && !string->IsAsciiRepresentation()) { |
| 6855 | is_ascii = false; |
| 6856 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6857 | } |
| 6858 | if (length > max_string_length || |
| 6859 | max_string_length - length < string_length) { |
| 6860 | overflow = true; |
| 6861 | break; |
| 6862 | } |
| 6863 | string_length += length; |
| 6864 | } |
| 6865 | int separator_length = separator->length(); |
| 6866 | if (!overflow && separator_length > 0) { |
| 6867 | if (array_length <= 0x7fffffffu) { |
| 6868 | int separator_count = static_cast<int>(array_length) - 1; |
| 6869 | int remaining_length = max_string_length - string_length; |
| 6870 | if ((remaining_length / separator_length) >= separator_count) { |
| 6871 | string_length += separator_length * (array_length - 1); |
| 6872 | } else { |
| 6873 | // Not room for the separators within the maximal string length. |
| 6874 | overflow = true; |
| 6875 | } |
| 6876 | } else { |
| 6877 | // Nonempty separator and at least 2^31-1 separators necessary |
| 6878 | // means that the string is too large to create. |
| 6879 | STATIC_ASSERT(String::kMaxLength < 0x7fffffff); |
| 6880 | overflow = true; |
| 6881 | } |
| 6882 | } |
| 6883 | if (overflow) { |
| 6884 | // Throw OutOfMemory exception for creating too large a string. |
| 6885 | V8::FatalProcessOutOfMemory("Array join result too large."); |
| 6886 | } |
| 6887 | |
| 6888 | if (is_ascii) { |
| 6889 | MaybeObject* result_allocation = |
| 6890 | isolate->heap()->AllocateRawAsciiString(string_length); |
| 6891 | if (result_allocation->IsFailure()) return result_allocation; |
| 6892 | SeqAsciiString* result_string = |
| 6893 | SeqAsciiString::cast(result_allocation->ToObjectUnchecked()); |
| 6894 | JoinSparseArrayWithSeparator<char>(elements, |
| 6895 | elements_length, |
| 6896 | array_length, |
| 6897 | separator, |
| 6898 | Vector<char>(result_string->GetChars(), |
| 6899 | string_length)); |
| 6900 | return result_string; |
| 6901 | } else { |
| 6902 | MaybeObject* result_allocation = |
| 6903 | isolate->heap()->AllocateRawTwoByteString(string_length); |
| 6904 | if (result_allocation->IsFailure()) return result_allocation; |
| 6905 | SeqTwoByteString* result_string = |
| 6906 | SeqTwoByteString::cast(result_allocation->ToObjectUnchecked()); |
| 6907 | JoinSparseArrayWithSeparator<uc16>(elements, |
| 6908 | elements_length, |
| 6909 | array_length, |
| 6910 | separator, |
| 6911 | Vector<uc16>(result_string->GetChars(), |
| 6912 | string_length)); |
| 6913 | return result_string; |
| 6914 | } |
| 6915 | } |
| 6916 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6917 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6918 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberOr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6919 | NoHandleAllocation ha; |
| 6920 | ASSERT(args.length() == 2); |
| 6921 | |
| 6922 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6923 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6924 | return isolate->heap()->NumberFromInt32(x | y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6925 | } |
| 6926 | |
| 6927 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6928 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAnd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6929 | NoHandleAllocation ha; |
| 6930 | ASSERT(args.length() == 2); |
| 6931 | |
| 6932 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6933 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6934 | return isolate->heap()->NumberFromInt32(x & y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6935 | } |
| 6936 | |
| 6937 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6938 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberXor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6939 | NoHandleAllocation ha; |
| 6940 | ASSERT(args.length() == 2); |
| 6941 | |
| 6942 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6943 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6944 | return isolate->heap()->NumberFromInt32(x ^ y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
| 6947 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6948 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberNot) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6949 | NoHandleAllocation ha; |
| 6950 | ASSERT(args.length() == 1); |
| 6951 | |
| 6952 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6953 | return isolate->heap()->NumberFromInt32(~x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6954 | } |
| 6955 | |
| 6956 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6957 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShl) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6958 | NoHandleAllocation ha; |
| 6959 | ASSERT(args.length() == 2); |
| 6960 | |
| 6961 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6962 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6963 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6964 | } |
| 6965 | |
| 6966 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6967 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6968 | NoHandleAllocation ha; |
| 6969 | ASSERT(args.length() == 2); |
| 6970 | |
| 6971 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 6972 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6973 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6974 | } |
| 6975 | |
| 6976 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6977 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSar) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6978 | NoHandleAllocation ha; |
| 6979 | ASSERT(args.length() == 2); |
| 6980 | |
| 6981 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6982 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6983 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6987 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6988 | NoHandleAllocation ha; |
| 6989 | ASSERT(args.length() == 2); |
| 6990 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6991 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6992 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6993 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 6994 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 6995 | if (x == y) return Smi::FromInt(EQUAL); |
| 6996 | Object* result; |
| 6997 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 6998 | result = Smi::FromInt(EQUAL); |
| 6999 | } else { |
| 7000 | result = Smi::FromInt(NOT_EQUAL); |
| 7001 | } |
| 7002 | return result; |
| 7003 | } |
| 7004 | |
| 7005 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7006 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7007 | NoHandleAllocation ha; |
| 7008 | ASSERT(args.length() == 2); |
| 7009 | |
| 7010 | CONVERT_CHECKED(String, x, args[0]); |
| 7011 | CONVERT_CHECKED(String, y, args[1]); |
| 7012 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7013 | bool not_equal = !x->Equals(y); |
| 7014 | // This is slightly convoluted because the value that signifies |
| 7015 | // equality is 0 and inequality is 1 so we have to negate the result |
| 7016 | // from String::Equals. |
| 7017 | ASSERT(not_equal == 0 || not_equal == 1); |
| 7018 | STATIC_CHECK(EQUAL == 0); |
| 7019 | STATIC_CHECK(NOT_EQUAL == 1); |
| 7020 | return Smi::FromInt(not_equal); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7024 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7025 | NoHandleAllocation ha; |
| 7026 | ASSERT(args.length() == 3); |
| 7027 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7028 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7029 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7030 | if (isnan(x) || isnan(y)) return args[2]; |
| 7031 | if (x == y) return Smi::FromInt(EQUAL); |
| 7032 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 7033 | return Smi::FromInt(GREATER); |
| 7034 | } |
| 7035 | |
| 7036 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7037 | // Compare two Smis as if they were converted to strings and then |
| 7038 | // compared lexicographically. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7039 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7040 | NoHandleAllocation ha; |
| 7041 | ASSERT(args.length() == 2); |
| 7042 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7043 | // Extract the integer values from the Smis. |
| 7044 | CONVERT_CHECKED(Smi, x, args[0]); |
| 7045 | CONVERT_CHECKED(Smi, y, args[1]); |
| 7046 | int x_value = x->value(); |
| 7047 | int y_value = y->value(); |
| 7048 | |
| 7049 | // If the integers are equal so are the string representations. |
| 7050 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 7051 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7052 | // If one of the integers is zero the normal integer order is the |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7053 | // same as the lexicographic order of the string representations. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7054 | if (x_value == 0 || y_value == 0) |
| 7055 | return Smi::FromInt(x_value < y_value ? LESS : GREATER); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7056 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7057 | // 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] | 7058 | // smallest because the char code of '-' is less than the char code |
| 7059 | // of any digit. Otherwise, we make both values positive. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7060 | |
| 7061 | // Use unsigned values otherwise the logic is incorrect for -MIN_INT on |
| 7062 | // architectures using 32-bit Smis. |
| 7063 | uint32_t x_scaled = x_value; |
| 7064 | uint32_t y_scaled = y_value; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7065 | if (x_value < 0 || y_value < 0) { |
| 7066 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 7067 | if (x_value >= 0) return Smi::FromInt(GREATER); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7068 | x_scaled = -x_value; |
| 7069 | y_scaled = -y_value; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7072 | static const uint32_t kPowersOf10[] = { |
| 7073 | 1, 10, 100, 1000, 10*1000, 100*1000, |
| 7074 | 1000*1000, 10*1000*1000, 100*1000*1000, |
| 7075 | 1000*1000*1000 |
| 7076 | }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7077 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7078 | // If the integers have the same number of decimal digits they can be |
| 7079 | // compared directly as the numeric order is the same as the |
| 7080 | // lexicographic order. If one integer has fewer digits, it is scaled |
| 7081 | // by some power of 10 to have the same number of digits as the longer |
| 7082 | // integer. If the scaled integers are equal it means the shorter |
| 7083 | // integer comes first in the lexicographic order. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7084 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7085 | // From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
| 7086 | int x_log2 = IntegerLog2(x_scaled); |
| 7087 | int x_log10 = ((x_log2 + 1) * 1233) >> 12; |
| 7088 | x_log10 -= x_scaled < kPowersOf10[x_log10]; |
| 7089 | |
| 7090 | int y_log2 = IntegerLog2(y_scaled); |
| 7091 | int y_log10 = ((y_log2 + 1) * 1233) >> 12; |
| 7092 | y_log10 -= y_scaled < kPowersOf10[y_log10]; |
| 7093 | |
| 7094 | int tie = EQUAL; |
| 7095 | |
| 7096 | if (x_log10 < y_log10) { |
| 7097 | // X has fewer digits. We would like to simply scale up X but that |
| 7098 | // might overflow, e.g when comparing 9 with 1_000_000_000, 9 would |
| 7099 | // be scaled up to 9_000_000_000. So we scale up by the next |
| 7100 | // smallest power and scale down Y to drop one digit. It is OK to |
| 7101 | // drop one digit from the longer integer since the final digit is |
| 7102 | // past the length of the shorter integer. |
| 7103 | x_scaled *= kPowersOf10[y_log10 - x_log10 - 1]; |
| 7104 | y_scaled /= 10; |
| 7105 | tie = LESS; |
| 7106 | } else if (y_log10 < x_log10) { |
| 7107 | y_scaled *= kPowersOf10[x_log10 - y_log10 - 1]; |
| 7108 | x_scaled /= 10; |
| 7109 | tie = GREATER; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7112 | if (x_scaled < y_scaled) return Smi::FromInt(LESS); |
| 7113 | if (x_scaled > y_scaled) return Smi::FromInt(GREATER); |
| 7114 | return Smi::FromInt(tie); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7115 | } |
| 7116 | |
| 7117 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7118 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 7119 | String* x, |
| 7120 | String* y) { |
| 7121 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 7122 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7123 | bufx.Reset(x); |
| 7124 | bufy.Reset(y); |
| 7125 | while (bufx.has_more() && bufy.has_more()) { |
| 7126 | int d = bufx.GetNext() - bufy.GetNext(); |
| 7127 | if (d < 0) return Smi::FromInt(LESS); |
| 7128 | else if (d > 0) return Smi::FromInt(GREATER); |
| 7129 | } |
| 7130 | |
| 7131 | // x is (non-trivial) prefix of y: |
| 7132 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 7133 | // y is prefix of x: |
| 7134 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 7135 | } |
| 7136 | |
| 7137 | |
| 7138 | static Object* FlatStringCompare(String* x, String* y) { |
| 7139 | ASSERT(x->IsFlat()); |
| 7140 | ASSERT(y->IsFlat()); |
| 7141 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 7142 | int prefix_length = x->length(); |
| 7143 | if (y->length() < prefix_length) { |
| 7144 | prefix_length = y->length(); |
| 7145 | equal_prefix_result = Smi::FromInt(GREATER); |
| 7146 | } else if (y->length() > prefix_length) { |
| 7147 | equal_prefix_result = Smi::FromInt(LESS); |
| 7148 | } |
| 7149 | int r; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7150 | String::FlatContent x_content = x->GetFlatContent(); |
| 7151 | String::FlatContent y_content = y->GetFlatContent(); |
| 7152 | if (x_content.IsAscii()) { |
| 7153 | Vector<const char> x_chars = x_content.ToAsciiVector(); |
| 7154 | if (y_content.IsAscii()) { |
| 7155 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7156 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7157 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7158 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7159 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7160 | } |
| 7161 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7162 | Vector<const uc16> x_chars = x_content.ToUC16Vector(); |
| 7163 | if (y_content.IsAscii()) { |
| 7164 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7165 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7166 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7167 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7168 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7169 | } |
| 7170 | } |
| 7171 | Object* result; |
| 7172 | if (r == 0) { |
| 7173 | result = equal_prefix_result; |
| 7174 | } else { |
| 7175 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 7176 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7177 | ASSERT(result == |
| 7178 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7179 | return result; |
| 7180 | } |
| 7181 | |
| 7182 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7183 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7184 | NoHandleAllocation ha; |
| 7185 | ASSERT(args.length() == 2); |
| 7186 | |
| 7187 | CONVERT_CHECKED(String, x, args[0]); |
| 7188 | CONVERT_CHECKED(String, y, args[1]); |
| 7189 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7190 | isolate->counters()->string_compare_runtime()->Increment(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7191 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7192 | // A few fast case tests before we flatten. |
| 7193 | if (x == y) return Smi::FromInt(EQUAL); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7194 | if (y->length() == 0) { |
| 7195 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7196 | return Smi::FromInt(GREATER); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7197 | } else if (x->length() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7198 | return Smi::FromInt(LESS); |
| 7199 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7200 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7201 | int d = x->Get(0) - y->Get(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7202 | if (d < 0) return Smi::FromInt(LESS); |
| 7203 | else if (d > 0) return Smi::FromInt(GREATER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7204 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7205 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7206 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7207 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7208 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7209 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7210 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7211 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7212 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7213 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7214 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
| 7217 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7218 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_acos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7219 | NoHandleAllocation ha; |
| 7220 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7221 | isolate->counters()->math_acos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7222 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7223 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7224 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7225 | } |
| 7226 | |
| 7227 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7228 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_asin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7229 | NoHandleAllocation ha; |
| 7230 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7231 | isolate->counters()->math_asin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7232 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7233 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7234 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7235 | } |
| 7236 | |
| 7237 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7238 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7239 | NoHandleAllocation ha; |
| 7240 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7241 | isolate->counters()->math_atan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7242 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7243 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7244 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7245 | } |
| 7246 | |
| 7247 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7248 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 7249 | |
| 7250 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7251 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7252 | NoHandleAllocation ha; |
| 7253 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7254 | isolate->counters()->math_atan2()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7255 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7256 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7257 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7258 | double result; |
| 7259 | if (isinf(x) && isinf(y)) { |
| 7260 | // Make sure that the result in case of two infinite arguments |
| 7261 | // is a multiple of Pi / 4. The sign of the result is determined |
| 7262 | // by the first argument (x) and the sign of the second argument |
| 7263 | // determines the multiplier: one or three. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7264 | int multiplier = (x < 0) ? -1 : 1; |
| 7265 | if (y < 0) multiplier *= 3; |
| 7266 | result = multiplier * kPiDividedBy4; |
| 7267 | } else { |
| 7268 | result = atan2(x, y); |
| 7269 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7270 | return isolate->heap()->AllocateHeapNumber(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7271 | } |
| 7272 | |
| 7273 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7274 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_ceil) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7275 | NoHandleAllocation ha; |
| 7276 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7277 | isolate->counters()->math_ceil()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7278 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7279 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7280 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7281 | } |
| 7282 | |
| 7283 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7284 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7285 | NoHandleAllocation ha; |
| 7286 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7287 | isolate->counters()->math_cos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7288 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7289 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7290 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7291 | } |
| 7292 | |
| 7293 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7294 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_exp) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7295 | NoHandleAllocation ha; |
| 7296 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7297 | isolate->counters()->math_exp()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7298 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7299 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7300 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7301 | } |
| 7302 | |
| 7303 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7304 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_floor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7305 | NoHandleAllocation ha; |
| 7306 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7307 | isolate->counters()->math_floor()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7308 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7309 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7310 | return isolate->heap()->NumberFromDouble(floor(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7314 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7315 | NoHandleAllocation ha; |
| 7316 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7317 | isolate->counters()->math_log()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7318 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7319 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7320 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7321 | } |
| 7322 | |
| 7323 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7324 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7325 | NoHandleAllocation ha; |
| 7326 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7327 | isolate->counters()->math_pow()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7328 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7329 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7330 | |
| 7331 | // If the second argument is a smi, it is much faster to call the |
| 7332 | // custom powi() function than the generic pow(). |
| 7333 | if (args[1]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7334 | int y = args.smi_at(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7335 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7338 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7339 | return isolate->heap()->AllocateHeapNumber(power_double_double(x, y)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7342 | // Fast version of Math.pow if we know that y is not an integer and |
| 7343 | // y is not -0.5 or 0.5. Used as slowcase from codegen. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7344 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7345 | NoHandleAllocation ha; |
| 7346 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7347 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7348 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7349 | if (y == 0) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7350 | return Smi::FromInt(1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7351 | } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7352 | return isolate->heap()->nan_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7353 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7354 | return isolate->heap()->AllocateHeapNumber(pow(x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7355 | } |
| 7356 | } |
| 7357 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7358 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7359 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7360 | NoHandleAllocation ha; |
| 7361 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7362 | isolate->counters()->math_round()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7363 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7364 | if (!args[0]->IsHeapNumber()) { |
| 7365 | // Must be smi. Return the argument unchanged for all the other types |
| 7366 | // to make fuzz-natives test happy. |
| 7367 | return args[0]; |
| 7368 | } |
| 7369 | |
| 7370 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 7371 | |
| 7372 | double value = number->value(); |
| 7373 | int exponent = number->get_exponent(); |
| 7374 | int sign = number->get_sign(); |
| 7375 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7376 | if (exponent < -1) { |
| 7377 | // Number in range ]-0.5..0.5[. These always round to +/-zero. |
| 7378 | if (sign) return isolate->heap()->minus_zero_value(); |
| 7379 | return Smi::FromInt(0); |
| 7380 | } |
| 7381 | |
| 7382 | // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and |
| 7383 | // should be rounded to 2^30, which is not smi (for 31-bit smis, similar |
| 7384 | // agument holds for 32-bit smis). |
| 7385 | if (!sign && exponent < kSmiValueSize - 2) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7386 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 7387 | } |
| 7388 | |
| 7389 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 7390 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 7391 | if (exponent >= 52) { |
| 7392 | return number; |
| 7393 | } |
| 7394 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7395 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7396 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7397 | // Do not call NumberFromDouble() to avoid extra checks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7398 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7402 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7403 | NoHandleAllocation ha; |
| 7404 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7405 | isolate->counters()->math_sin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7406 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7407 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7408 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
| 7411 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7412 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sqrt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7413 | NoHandleAllocation ha; |
| 7414 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7415 | isolate->counters()->math_sqrt()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7416 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7417 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7418 | return isolate->heap()->AllocateHeapNumber(sqrt(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7419 | } |
| 7420 | |
| 7421 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7422 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_tan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7423 | NoHandleAllocation ha; |
| 7424 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7425 | isolate->counters()->math_tan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7426 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7427 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7428 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7429 | } |
| 7430 | |
| 7431 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7432 | static int MakeDay(int year, int month, int day) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7433 | static const int day_from_month[] = {0, 31, 59, 90, 120, 151, |
| 7434 | 181, 212, 243, 273, 304, 334}; |
| 7435 | static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152, |
| 7436 | 182, 213, 244, 274, 305, 335}; |
| 7437 | |
| 7438 | year += month / 12; |
| 7439 | month %= 12; |
| 7440 | if (month < 0) { |
| 7441 | year--; |
| 7442 | month += 12; |
| 7443 | } |
| 7444 | |
| 7445 | ASSERT(month >= 0); |
| 7446 | ASSERT(month < 12); |
| 7447 | |
| 7448 | // year_delta is an arbitrary number such that: |
| 7449 | // a) year_delta = -1 (mod 400) |
| 7450 | // b) year + year_delta > 0 for years in the range defined by |
| 7451 | // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
| 7452 | // Jan 1 1970. This is required so that we don't run into integer |
| 7453 | // division of negative numbers. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7454 | // 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] | 7455 | // operations. |
| 7456 | static const int year_delta = 399999; |
| 7457 | static const int base_day = 365 * (1970 + year_delta) + |
| 7458 | (1970 + year_delta) / 4 - |
| 7459 | (1970 + year_delta) / 100 + |
| 7460 | (1970 + year_delta) / 400; |
| 7461 | |
| 7462 | int year1 = year + year_delta; |
| 7463 | int day_from_year = 365 * year1 + |
| 7464 | year1 / 4 - |
| 7465 | year1 / 100 + |
| 7466 | year1 / 400 - |
| 7467 | base_day; |
| 7468 | |
| 7469 | if (year % 4 || (year % 100 == 0 && year % 400 != 0)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7470 | return day_from_year + day_from_month[month] + day - 1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7471 | } |
| 7472 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7473 | return day_from_year + day_from_month_leap[month] + day - 1; |
| 7474 | } |
| 7475 | |
| 7476 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7477 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7478 | NoHandleAllocation ha; |
| 7479 | ASSERT(args.length() == 3); |
| 7480 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7481 | CONVERT_SMI_ARG_CHECKED(year, 0); |
| 7482 | CONVERT_SMI_ARG_CHECKED(month, 1); |
| 7483 | CONVERT_SMI_ARG_CHECKED(date, 2); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7484 | |
| 7485 | return Smi::FromInt(MakeDay(year, month, date)); |
| 7486 | } |
| 7487 | |
| 7488 | |
| 7489 | static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1}; |
| 7490 | static const int kDaysIn4Years = 4 * 365 + 1; |
| 7491 | static const int kDaysIn100Years = 25 * kDaysIn4Years - 1; |
| 7492 | static const int kDaysIn400Years = 4 * kDaysIn100Years + 1; |
| 7493 | static const int kDays1970to2000 = 30 * 365 + 7; |
| 7494 | static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - |
| 7495 | kDays1970to2000; |
| 7496 | static const int kYearsOffset = 400000; |
| 7497 | |
| 7498 | static const char kDayInYear[] = { |
| 7499 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7500 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7501 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7502 | 22, 23, 24, 25, 26, 27, 28, |
| 7503 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7504 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7505 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7506 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7507 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7508 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7509 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7510 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7511 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7512 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7513 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7514 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7515 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7516 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7517 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7518 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7519 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7520 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7521 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7522 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7523 | |
| 7524 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7525 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7526 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7527 | 22, 23, 24, 25, 26, 27, 28, |
| 7528 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7529 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7530 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7531 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7532 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7533 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7534 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7535 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7536 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7537 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7538 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7539 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7540 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7541 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7542 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7543 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7544 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7545 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7546 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7547 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7548 | |
| 7549 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7550 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7551 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7552 | 22, 23, 24, 25, 26, 27, 28, 29, |
| 7553 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7554 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7555 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7556 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7557 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7558 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7559 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7560 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7561 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7562 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7563 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7564 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7565 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7566 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7567 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7568 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7569 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7570 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7571 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7572 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7573 | |
| 7574 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7575 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7576 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7577 | 22, 23, 24, 25, 26, 27, 28, |
| 7578 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7579 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7580 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7581 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7582 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7583 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7584 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7585 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7586 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7587 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7588 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7589 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7590 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7591 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7592 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7593 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7594 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7595 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7596 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7597 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 7598 | |
| 7599 | static const char kMonthInYear[] = { |
| 7600 | 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, |
| 7601 | 0, 0, 0, 0, 0, 0, |
| 7602 | 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, |
| 7603 | 1, 1, 1, |
| 7604 | 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, |
| 7605 | 2, 2, 2, 2, 2, 2, |
| 7606 | 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, |
| 7607 | 3, 3, 3, 3, 3, |
| 7608 | 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, |
| 7609 | 4, 4, 4, 4, 4, 4, |
| 7610 | 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, |
| 7611 | 5, 5, 5, 5, 5, |
| 7612 | 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, |
| 7613 | 6, 6, 6, 6, 6, 6, |
| 7614 | 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, |
| 7615 | 7, 7, 7, 7, 7, 7, |
| 7616 | 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, |
| 7617 | 8, 8, 8, 8, 8, |
| 7618 | 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, |
| 7619 | 9, 9, 9, 9, 9, 9, |
| 7620 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7621 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7622 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7623 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7624 | |
| 7625 | 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, |
| 7626 | 0, 0, 0, 0, 0, 0, |
| 7627 | 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, |
| 7628 | 1, 1, 1, |
| 7629 | 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, |
| 7630 | 2, 2, 2, 2, 2, 2, |
| 7631 | 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, |
| 7632 | 3, 3, 3, 3, 3, |
| 7633 | 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, |
| 7634 | 4, 4, 4, 4, 4, 4, |
| 7635 | 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, |
| 7636 | 5, 5, 5, 5, 5, |
| 7637 | 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, |
| 7638 | 6, 6, 6, 6, 6, 6, |
| 7639 | 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, |
| 7640 | 7, 7, 7, 7, 7, 7, |
| 7641 | 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, |
| 7642 | 8, 8, 8, 8, 8, |
| 7643 | 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, |
| 7644 | 9, 9, 9, 9, 9, 9, |
| 7645 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7646 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7647 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7648 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7649 | |
| 7650 | 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, |
| 7651 | 0, 0, 0, 0, 0, 0, |
| 7652 | 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, |
| 7653 | 1, 1, 1, 1, |
| 7654 | 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, |
| 7655 | 2, 2, 2, 2, 2, 2, |
| 7656 | 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, |
| 7657 | 3, 3, 3, 3, 3, |
| 7658 | 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, |
| 7659 | 4, 4, 4, 4, 4, 4, |
| 7660 | 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, |
| 7661 | 5, 5, 5, 5, 5, |
| 7662 | 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, |
| 7663 | 6, 6, 6, 6, 6, 6, |
| 7664 | 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, |
| 7665 | 7, 7, 7, 7, 7, 7, |
| 7666 | 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, |
| 7667 | 8, 8, 8, 8, 8, |
| 7668 | 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, |
| 7669 | 9, 9, 9, 9, 9, 9, |
| 7670 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7671 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7672 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7673 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7674 | |
| 7675 | 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, |
| 7676 | 0, 0, 0, 0, 0, 0, |
| 7677 | 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, |
| 7678 | 1, 1, 1, |
| 7679 | 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, |
| 7680 | 2, 2, 2, 2, 2, 2, |
| 7681 | 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, |
| 7682 | 3, 3, 3, 3, 3, |
| 7683 | 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, |
| 7684 | 4, 4, 4, 4, 4, 4, |
| 7685 | 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, |
| 7686 | 5, 5, 5, 5, 5, |
| 7687 | 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, |
| 7688 | 6, 6, 6, 6, 6, 6, |
| 7689 | 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, |
| 7690 | 7, 7, 7, 7, 7, 7, |
| 7691 | 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, |
| 7692 | 8, 8, 8, 8, 8, |
| 7693 | 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, |
| 7694 | 9, 9, 9, 9, 9, 9, |
| 7695 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7696 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7697 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7698 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; |
| 7699 | |
| 7700 | |
| 7701 | // This function works for dates from 1970 to 2099. |
| 7702 | static inline void DateYMDFromTimeAfter1970(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7703 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7704 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7705 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7706 | #endif |
| 7707 | |
| 7708 | year = 1970 + (4 * date + 2) / kDaysIn4Years; |
| 7709 | date %= kDaysIn4Years; |
| 7710 | |
| 7711 | month = kMonthInYear[date]; |
| 7712 | day = kDayInYear[date]; |
| 7713 | |
| 7714 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7715 | } |
| 7716 | |
| 7717 | |
| 7718 | static inline void DateYMDFromTimeSlow(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7719 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7720 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7721 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7722 | #endif |
| 7723 | |
| 7724 | date += kDaysOffset; |
| 7725 | year = 400 * (date / kDaysIn400Years) - kYearsOffset; |
| 7726 | date %= kDaysIn400Years; |
| 7727 | |
| 7728 | ASSERT(MakeDay(year, 0, 1) + date == save_date); |
| 7729 | |
| 7730 | date--; |
| 7731 | int yd1 = date / kDaysIn100Years; |
| 7732 | date %= kDaysIn100Years; |
| 7733 | year += 100 * yd1; |
| 7734 | |
| 7735 | date++; |
| 7736 | int yd2 = date / kDaysIn4Years; |
| 7737 | date %= kDaysIn4Years; |
| 7738 | year += 4 * yd2; |
| 7739 | |
| 7740 | date--; |
| 7741 | int yd3 = date / 365; |
| 7742 | date %= 365; |
| 7743 | year += yd3; |
| 7744 | |
| 7745 | bool is_leap = (!yd1 || yd2) && !yd3; |
| 7746 | |
| 7747 | ASSERT(date >= -1); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7748 | ASSERT(is_leap || (date >= 0)); |
| 7749 | ASSERT((date < 365) || (is_leap && (date < 366))); |
| 7750 | ASSERT(is_leap == ((year % 4 == 0) && (year % 100 || (year % 400 == 0)))); |
| 7751 | ASSERT(is_leap || ((MakeDay(year, 0, 1) + date) == save_date)); |
| 7752 | ASSERT(!is_leap || ((MakeDay(year, 0, 1) + date + 1) == save_date)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7753 | |
| 7754 | if (is_leap) { |
| 7755 | day = kDayInYear[2*365 + 1 + date]; |
| 7756 | month = kMonthInYear[2*365 + 1 + date]; |
| 7757 | } else { |
| 7758 | day = kDayInYear[date]; |
| 7759 | month = kMonthInYear[date]; |
| 7760 | } |
| 7761 | |
| 7762 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7763 | } |
| 7764 | |
| 7765 | |
| 7766 | static inline void DateYMDFromTime(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7767 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7768 | if (date >= 0 && date < 32 * kDaysIn4Years) { |
| 7769 | DateYMDFromTimeAfter1970(date, year, month, day); |
| 7770 | } else { |
| 7771 | DateYMDFromTimeSlow(date, year, month, day); |
| 7772 | } |
| 7773 | } |
| 7774 | |
| 7775 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7776 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateYMDFromTime) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7777 | NoHandleAllocation ha; |
| 7778 | ASSERT(args.length() == 2); |
| 7779 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7780 | CONVERT_DOUBLE_ARG_CHECKED(t, 0); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7781 | CONVERT_CHECKED(JSArray, res_array, args[1]); |
| 7782 | |
| 7783 | int year, month, day; |
| 7784 | DateYMDFromTime(static_cast<int>(floor(t / 86400000)), year, month, day); |
| 7785 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7786 | RUNTIME_ASSERT(res_array->elements()->map() == |
| 7787 | isolate->heap()->fixed_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 7788 | FixedArray* elms = FixedArray::cast(res_array->elements()); |
| 7789 | RUNTIME_ASSERT(elms->length() == 3); |
| 7790 | |
| 7791 | elms->set(0, Smi::FromInt(year)); |
| 7792 | elms->set(1, Smi::FromInt(month)); |
| 7793 | elms->set(2, Smi::FromInt(day)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7794 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7795 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
| 7798 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7799 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewArgumentsFast) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7800 | HandleScope scope(isolate); |
| 7801 | ASSERT(args.length() == 3); |
| 7802 | |
| 7803 | Handle<JSFunction> callee = args.at<JSFunction>(0); |
| 7804 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7805 | const int argument_count = Smi::cast(args[2])->value(); |
| 7806 | |
| 7807 | Handle<JSObject> result = |
| 7808 | isolate->factory()->NewArgumentsObject(callee, argument_count); |
| 7809 | // Allocate the elements if needed. |
| 7810 | int parameter_count = callee->shared()->formal_parameter_count(); |
| 7811 | if (argument_count > 0) { |
| 7812 | if (parameter_count > 0) { |
| 7813 | int mapped_count = Min(argument_count, parameter_count); |
| 7814 | Handle<FixedArray> parameter_map = |
| 7815 | isolate->factory()->NewFixedArray(mapped_count + 2, NOT_TENURED); |
| 7816 | parameter_map->set_map( |
| 7817 | isolate->heap()->non_strict_arguments_elements_map()); |
| 7818 | |
| 7819 | Handle<Map> old_map(result->map()); |
| 7820 | Handle<Map> new_map = |
| 7821 | isolate->factory()->CopyMapDropTransitions(old_map); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7822 | new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7823 | |
| 7824 | result->set_map(*new_map); |
| 7825 | result->set_elements(*parameter_map); |
| 7826 | |
| 7827 | // Store the context and the arguments array at the beginning of the |
| 7828 | // parameter map. |
| 7829 | Handle<Context> context(isolate->context()); |
| 7830 | Handle<FixedArray> arguments = |
| 7831 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7832 | parameter_map->set(0, *context); |
| 7833 | parameter_map->set(1, *arguments); |
| 7834 | |
| 7835 | // Loop over the actual parameters backwards. |
| 7836 | int index = argument_count - 1; |
| 7837 | while (index >= mapped_count) { |
| 7838 | // These go directly in the arguments array and have no |
| 7839 | // corresponding slot in the parameter map. |
| 7840 | arguments->set(index, *(parameters - index - 1)); |
| 7841 | --index; |
| 7842 | } |
| 7843 | |
| 7844 | ScopeInfo<> scope_info(callee->shared()->scope_info()); |
| 7845 | while (index >= 0) { |
| 7846 | // Detect duplicate names to the right in the parameter list. |
| 7847 | Handle<String> name = scope_info.parameter_name(index); |
| 7848 | int context_slot_count = scope_info.number_of_context_slots(); |
| 7849 | bool duplicate = false; |
| 7850 | for (int j = index + 1; j < parameter_count; ++j) { |
| 7851 | if (scope_info.parameter_name(j).is_identical_to(name)) { |
| 7852 | duplicate = true; |
| 7853 | break; |
| 7854 | } |
| 7855 | } |
| 7856 | |
| 7857 | if (duplicate) { |
| 7858 | // This goes directly in the arguments array with a hole in the |
| 7859 | // parameter map. |
| 7860 | arguments->set(index, *(parameters - index - 1)); |
| 7861 | parameter_map->set_the_hole(index + 2); |
| 7862 | } else { |
| 7863 | // The context index goes in the parameter map with a hole in the |
| 7864 | // arguments array. |
| 7865 | int context_index = -1; |
| 7866 | for (int j = Context::MIN_CONTEXT_SLOTS; |
| 7867 | j < context_slot_count; |
| 7868 | ++j) { |
| 7869 | if (scope_info.context_slot_name(j).is_identical_to(name)) { |
| 7870 | context_index = j; |
| 7871 | break; |
| 7872 | } |
| 7873 | } |
| 7874 | ASSERT(context_index >= 0); |
| 7875 | arguments->set_the_hole(index); |
| 7876 | parameter_map->set(index + 2, Smi::FromInt(context_index)); |
| 7877 | } |
| 7878 | |
| 7879 | --index; |
| 7880 | } |
| 7881 | } else { |
| 7882 | // If there is no aliasing, the arguments object elements are not |
| 7883 | // special in any way. |
| 7884 | Handle<FixedArray> elements = |
| 7885 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7886 | result->set_elements(*elements); |
| 7887 | for (int i = 0; i < argument_count; ++i) { |
| 7888 | elements->set(i, *(parameters - i - 1)); |
| 7889 | } |
| 7890 | } |
| 7891 | } |
| 7892 | return *result; |
| 7893 | } |
| 7894 | |
| 7895 | |
| 7896 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStrictArgumentsFast) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7897 | NoHandleAllocation ha; |
| 7898 | ASSERT(args.length() == 3); |
| 7899 | |
| 7900 | JSFunction* callee = JSFunction::cast(args[0]); |
| 7901 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7902 | const int length = args.smi_at(2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7903 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7904 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7905 | { MaybeObject* maybe_result = |
| 7906 | isolate->heap()->AllocateArgumentsObject(callee, length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7907 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7908 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7909 | // Allocate the elements if needed. |
| 7910 | if (length > 0) { |
| 7911 | // Allocate the fixed array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7912 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7913 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7914 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7915 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7916 | |
| 7917 | AssertNoAllocation no_gc; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 7918 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7919 | array->set_map(isolate->heap()->fixed_array_map()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7920 | array->set_length(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7921 | |
| 7922 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7923 | for (int i = 0; i < length; i++) { |
| 7924 | array->set(i, *--parameters, mode); |
| 7925 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7926 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7927 | } |
| 7928 | return result; |
| 7929 | } |
| 7930 | |
| 7931 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7932 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewClosure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7933 | HandleScope scope(isolate); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7934 | ASSERT(args.length() == 3); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 7935 | CONVERT_ARG_CHECKED(Context, context, 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7936 | CONVERT_ARG_CHECKED(SharedFunctionInfo, shared, 1); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7937 | CONVERT_BOOLEAN_CHECKED(pretenure, args[2]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7938 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7939 | // The caller ensures that we pretenure closures that are assigned |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7940 | // directly to properties. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7941 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7942 | Handle<JSFunction> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7943 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 7944 | context, |
| 7945 | pretenure_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7946 | return *result; |
| 7947 | } |
| 7948 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7949 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7950 | static SmartArrayPointer<Object**> GetNonBoundArguments(int bound_argc, |
| 7951 | int* total_argc) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7952 | // Find frame containing arguments passed to the caller. |
| 7953 | JavaScriptFrameIterator it; |
| 7954 | JavaScriptFrame* frame = it.frame(); |
| 7955 | List<JSFunction*> functions(2); |
| 7956 | frame->GetFunctions(&functions); |
| 7957 | if (functions.length() > 1) { |
| 7958 | int inlined_frame_index = functions.length() - 1; |
| 7959 | JSFunction* inlined_function = functions[inlined_frame_index]; |
| 7960 | int args_count = inlined_function->shared()->formal_parameter_count(); |
| 7961 | ScopedVector<SlotRef> args_slots(args_count); |
| 7962 | SlotRef::ComputeSlotMappingForArguments(frame, |
| 7963 | inlined_frame_index, |
| 7964 | &args_slots); |
| 7965 | |
| 7966 | *total_argc = bound_argc + args_count; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7967 | SmartArrayPointer<Object**> param_data(NewArray<Object**>(*total_argc)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7968 | for (int i = 0; i < args_count; i++) { |
| 7969 | Handle<Object> val = args_slots[i].GetValue(); |
| 7970 | param_data[bound_argc + i] = val.location(); |
| 7971 | } |
| 7972 | return param_data; |
| 7973 | } else { |
| 7974 | it.AdvanceToArgumentsFrame(); |
| 7975 | frame = it.frame(); |
| 7976 | int args_count = frame->ComputeParametersCount(); |
| 7977 | |
| 7978 | *total_argc = bound_argc + args_count; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7979 | SmartArrayPointer<Object**> param_data(NewArray<Object**>(*total_argc)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7980 | for (int i = 0; i < args_count; i++) { |
| 7981 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
| 7982 | param_data[bound_argc + i] = val.location(); |
| 7983 | } |
| 7984 | return param_data; |
| 7985 | } |
| 7986 | } |
| 7987 | |
| 7988 | |
| 7989 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObjectFromBound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7990 | HandleScope scope(isolate); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7991 | ASSERT(args.length() == 2); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7992 | // First argument is a function to use as a constructor. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7993 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7994 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7995 | // Second argument is either null or an array of bound arguments. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7996 | Handle<FixedArray> bound_args; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7997 | int bound_argc = 0; |
| 7998 | if (!args[1]->IsNull()) { |
| 7999 | CONVERT_ARG_CHECKED(JSArray, params, 1); |
| 8000 | RUNTIME_ASSERT(params->HasFastElements()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8001 | bound_args = Handle<FixedArray>(FixedArray::cast(params->elements())); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8002 | bound_argc = Smi::cast(params->length())->value(); |
| 8003 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8004 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8005 | int total_argc = 0; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8006 | SmartArrayPointer<Object**> param_data = |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8007 | GetNonBoundArguments(bound_argc, &total_argc); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8008 | for (int i = 0; i < bound_argc; i++) { |
| 8009 | Handle<Object> val = Handle<Object>(bound_args->get(i)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8010 | param_data[i] = val.location(); |
| 8011 | } |
| 8012 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 8013 | bool exception = false; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8014 | Handle<Object> result = |
| 8015 | Execution::New(function, total_argc, *param_data, &exception); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 8016 | if (exception) { |
| 8017 | return Failure::Exception(); |
| 8018 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8019 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 8020 | ASSERT(!result.is_null()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8021 | return *result; |
| 8022 | } |
| 8023 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8024 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8025 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 8026 | Handle<JSFunction> function) { |
| 8027 | Handle<Object> prototype = isolate->factory()->null_value(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8028 | if (function->has_instance_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8029 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8030 | } |
| 8031 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8032 | ConstructStubCompiler compiler; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 8033 | MaybeObject* code = compiler.CompileConstructStub(*function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8034 | if (!code->IsFailure()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8035 | function->shared()->set_construct_stub( |
| 8036 | Code::cast(code->ToObjectUnchecked())); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8037 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8038 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8039 | } |
| 8040 | |
| 8041 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8042 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8043 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8044 | ASSERT(args.length() == 1); |
| 8045 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8046 | Handle<Object> constructor = args.at<Object>(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8047 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8048 | // If the constructor isn't a proper function we throw a type error. |
| 8049 | if (!constructor->IsJSFunction()) { |
| 8050 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 8051 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8052 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 8053 | return isolate->Throw(*type_error); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8054 | } |
| 8055 | |
| 8056 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 8057 | |
| 8058 | // If function should not have prototype, construction is not allowed. In this |
| 8059 | // case generated code bailouts here, since function has no initial_map. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8060 | if (!function->should_have_prototype() && !function->shared()->bound()) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 8061 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 8062 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8063 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 8064 | return isolate->Throw(*type_error); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 8065 | } |
| 8066 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8067 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8068 | Debug* debug = isolate->debug(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8069 | // Handle stepping into constructors if step into is active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8070 | if (debug->StepInActive()) { |
| 8071 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8072 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8073 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8074 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8075 | if (function->has_initial_map()) { |
| 8076 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8077 | // The 'Function' function ignores the receiver object when |
| 8078 | // called using 'new' and creates a new JSFunction object that |
| 8079 | // is returned. The receiver object is only used for error |
| 8080 | // reporting if an error occurs when constructing the new |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8081 | // JSFunction. FACTORY->NewJSObject() should not be used to |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8082 | // allocate JSFunctions since it does not properly initialize |
| 8083 | // the shared part of the function. Since the receiver is |
| 8084 | // ignored anyway, we use the global object as the receiver |
| 8085 | // instead of a new JSFunction object. This way, errors are |
| 8086 | // reported the same way whether or not 'Function' is called |
| 8087 | // using 'new'. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8088 | return isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8089 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8090 | } |
| 8091 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8092 | // The function should be compiled for the optimization hints to be |
| 8093 | // available. We cannot use EnsureCompiled because that forces a |
| 8094 | // compilation through the shared function info which makes it |
| 8095 | // impossible for us to optimize. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8096 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8097 | if (!function->is_compiled()) CompileLazy(function, CLEAR_EXCEPTION); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8098 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8099 | if (!function->has_initial_map() && |
| 8100 | shared->IsInobjectSlackTrackingInProgress()) { |
| 8101 | // The tracking is already in progress for another function. We can only |
| 8102 | // track one initial_map at a time, so we force the completion before the |
| 8103 | // function is called as a constructor for the first time. |
| 8104 | shared->CompleteInobjectSlackTracking(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8105 | } |
| 8106 | |
| 8107 | bool first_allocation = !shared->live_objects_may_exist(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8108 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 8109 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8110 | // Delay setting the stub if inobject slack tracking is in progress. |
| 8111 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8112 | TrySettingInlineConstructStub(isolate, function); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8113 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8114 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8115 | isolate->counters()->constructed_objects()->Increment(); |
| 8116 | isolate->counters()->constructed_objects_runtime()->Increment(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8117 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8118 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8119 | } |
| 8120 | |
| 8121 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8122 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinalizeInstanceSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8123 | HandleScope scope(isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8124 | ASSERT(args.length() == 1); |
| 8125 | |
| 8126 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8127 | function->shared()->CompleteInobjectSlackTracking(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8128 | TrySettingInlineConstructStub(isolate, function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8129 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8130 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8131 | } |
| 8132 | |
| 8133 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8134 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8135 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8136 | ASSERT(args.length() == 1); |
| 8137 | |
| 8138 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 8139 | #ifdef DEBUG |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 8140 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8141 | PrintF("[lazy: "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8142 | function->PrintName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8143 | PrintF("]\n"); |
| 8144 | } |
| 8145 | #endif |
| 8146 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 8147 | // Compile the target function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8148 | ASSERT(!function->is_compiled()); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 8149 | if (!CompileLazy(function, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8150 | return Failure::Exception(); |
| 8151 | } |
| 8152 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8153 | // All done. Return the compiled code. |
| 8154 | ASSERT(function->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8155 | return function->code(); |
| 8156 | } |
| 8157 | |
| 8158 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8159 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8160 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8161 | ASSERT(args.length() == 1); |
| 8162 | Handle<JSFunction> function = args.at<JSFunction>(0); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 8163 | |
| 8164 | // If the function is not compiled ignore the lazy |
| 8165 | // recompilation. This can happen if the debugger is activated and |
| 8166 | // the function is returned to the not compiled state. |
| 8167 | if (!function->shared()->is_compiled()) { |
| 8168 | function->ReplaceCode(function->shared()->code()); |
| 8169 | return function->code(); |
| 8170 | } |
| 8171 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8172 | // If the function is not optimizable or debugger is active continue using the |
| 8173 | // code from the full compiler. |
| 8174 | if (!function->shared()->code()->optimizable() || |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8175 | isolate->DebuggerHasBreakPoints()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8176 | if (FLAG_trace_opt) { |
| 8177 | PrintF("[failed to optimize "); |
| 8178 | function->PrintName(); |
| 8179 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 8180 | function->shared()->code()->optimizable() ? "T" : "F", |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8181 | isolate->DebuggerHasBreakPoints() ? "T" : "F"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8182 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8183 | function->ReplaceCode(function->shared()->code()); |
| 8184 | return function->code(); |
| 8185 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8186 | if (CompileOptimized(function, AstNode::kNoNumber, CLEAR_EXCEPTION)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8187 | return function->code(); |
| 8188 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8189 | if (FLAG_trace_opt) { |
| 8190 | PrintF("[failed to optimize "); |
| 8191 | function->PrintName(); |
| 8192 | PrintF(": optimized compilation failed]\n"); |
| 8193 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8194 | function->ReplaceCode(function->shared()->code()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8195 | return function->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8196 | } |
| 8197 | |
| 8198 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8199 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyDeoptimized) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8200 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8201 | ASSERT(args.length() == 1); |
| 8202 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 8203 | Deoptimizer::BailoutType type = |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8204 | static_cast<Deoptimizer::BailoutType>(args.smi_at(0)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8205 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 8206 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8207 | int frames = deoptimizer->output_count(); |
| 8208 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8209 | deoptimizer->MaterializeHeapNumbers(); |
| 8210 | delete deoptimizer; |
| 8211 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8212 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8213 | JavaScriptFrame* frame = NULL; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8214 | for (int i = 0; i < frames - 1; i++) it.Advance(); |
| 8215 | frame = it.frame(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8216 | |
| 8217 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8218 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8219 | Handle<Object> arguments; |
| 8220 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8221 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8222 | if (arguments.is_null()) { |
| 8223 | // FunctionGetArguments can't throw an exception, so cast away the |
| 8224 | // doubt with an assert. |
| 8225 | arguments = Handle<Object>( |
| 8226 | Accessors::FunctionGetArguments(*function, |
| 8227 | NULL)->ToObjectUnchecked()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8228 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 8229 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8230 | } |
| 8231 | frame->SetExpression(i, *arguments); |
| 8232 | } |
| 8233 | } |
| 8234 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8235 | if (type == Deoptimizer::EAGER) { |
| 8236 | RUNTIME_ASSERT(function->IsOptimized()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8237 | } |
| 8238 | |
| 8239 | // Avoid doing too much work when running with --always-opt and keep |
| 8240 | // the optimized code around. |
| 8241 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8242 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8243 | } |
| 8244 | |
| 8245 | // Count the number of optimized activations of the function. |
| 8246 | int activations = 0; |
| 8247 | while (!it.done()) { |
| 8248 | JavaScriptFrame* frame = it.frame(); |
| 8249 | if (frame->is_optimized() && frame->function() == *function) { |
| 8250 | activations++; |
| 8251 | } |
| 8252 | it.Advance(); |
| 8253 | } |
| 8254 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8255 | if (activations == 0) { |
| 8256 | if (FLAG_trace_deopt) { |
| 8257 | PrintF("[removing optimized code for: "); |
| 8258 | function->PrintName(); |
| 8259 | PrintF("]\n"); |
| 8260 | } |
| 8261 | function->ReplaceCode(function->shared()->code()); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 8262 | } else { |
| 8263 | Deoptimizer::DeoptimizeFunction(*function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8264 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8265 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
| 8268 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8269 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyOSR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8270 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8271 | delete deoptimizer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8272 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8273 | } |
| 8274 | |
| 8275 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8276 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeoptimizeFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8277 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8278 | ASSERT(args.length() == 1); |
| 8279 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8280 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8281 | |
| 8282 | Deoptimizer::DeoptimizeFunction(*function); |
| 8283 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8284 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8285 | } |
| 8286 | |
| 8287 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8288 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RunningInSimulator) { |
| 8289 | #if defined(USE_SIMULATOR) |
| 8290 | return isolate->heap()->true_value(); |
| 8291 | #else |
| 8292 | return isolate->heap()->false_value(); |
| 8293 | #endif |
| 8294 | } |
| 8295 | |
| 8296 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8297 | RUNTIME_FUNCTION(MaybeObject*, Runtime_OptimizeFunctionOnNextCall) { |
| 8298 | HandleScope scope(isolate); |
| 8299 | ASSERT(args.length() == 1); |
| 8300 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8301 | if (!function->IsOptimizable()) return isolate->heap()->undefined_value(); |
| 8302 | function->MarkForLazyRecompilation(); |
| 8303 | return isolate->heap()->undefined_value(); |
| 8304 | } |
| 8305 | |
| 8306 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8307 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationStatus) { |
| 8308 | HandleScope scope(isolate); |
| 8309 | ASSERT(args.length() == 1); |
| 8310 | if (!V8::UseCrankshaft()) { |
| 8311 | return Smi::FromInt(4); // 4 == "never". |
| 8312 | } |
| 8313 | if (FLAG_always_opt) { |
| 8314 | return Smi::FromInt(3); // 3 == "always". |
| 8315 | } |
| 8316 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8317 | return function->IsOptimized() ? Smi::FromInt(1) // 1 == "yes". |
| 8318 | : Smi::FromInt(2); // 2 == "no". |
| 8319 | } |
| 8320 | |
| 8321 | |
| 8322 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationCount) { |
| 8323 | HandleScope scope(isolate); |
| 8324 | ASSERT(args.length() == 1); |
| 8325 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8326 | return Smi::FromInt(function->shared()->opt_count()); |
| 8327 | } |
| 8328 | |
| 8329 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8330 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileForOnStackReplacement) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8331 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8332 | ASSERT(args.length() == 1); |
| 8333 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 8334 | |
| 8335 | // We're not prepared to handle a function with arguments object. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8336 | ASSERT(!function->shared()->uses_arguments()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8337 | |
| 8338 | // We have hit a back edge in an unoptimized frame for a function that was |
| 8339 | // selected for on-stack replacement. Find the unoptimized code object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8340 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8341 | // Keep track of whether we've succeeded in optimizing. |
| 8342 | bool succeeded = unoptimized->optimizable(); |
| 8343 | if (succeeded) { |
| 8344 | // If we are trying to do OSR when there are already optimized |
| 8345 | // activations of the function, it means (a) the function is directly or |
| 8346 | // indirectly recursive and (b) an optimized invocation has been |
| 8347 | // deoptimized so that we are currently in an unoptimized activation. |
| 8348 | // Check for optimized activations of this function. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8349 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8350 | while (succeeded && !it.done()) { |
| 8351 | JavaScriptFrame* frame = it.frame(); |
| 8352 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 8353 | it.Advance(); |
| 8354 | } |
| 8355 | } |
| 8356 | |
| 8357 | int ast_id = AstNode::kNoNumber; |
| 8358 | if (succeeded) { |
| 8359 | // The top JS function is this one, the PC is somewhere in the |
| 8360 | // unoptimized code. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8361 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8362 | JavaScriptFrame* frame = it.frame(); |
| 8363 | ASSERT(frame->function() == *function); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8364 | ASSERT(frame->LookupCode() == *unoptimized); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8365 | ASSERT(unoptimized->contains(frame->pc())); |
| 8366 | |
| 8367 | // Use linear search of the unoptimized code's stack check table to find |
| 8368 | // the AST id matching the PC. |
| 8369 | Address start = unoptimized->instruction_start(); |
| 8370 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8371 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8372 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 8373 | table_cursor += kIntSize; |
| 8374 | for (unsigned i = 0; i < table_length; ++i) { |
| 8375 | // Table entries are (AST id, pc offset) pairs. |
| 8376 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 8377 | if (pc_offset == target_pc_offset) { |
| 8378 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 8379 | break; |
| 8380 | } |
| 8381 | table_cursor += 2 * kIntSize; |
| 8382 | } |
| 8383 | ASSERT(ast_id != AstNode::kNoNumber); |
| 8384 | if (FLAG_trace_osr) { |
| 8385 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 8386 | function->PrintName(); |
| 8387 | PrintF("]\n"); |
| 8388 | } |
| 8389 | |
| 8390 | // Try to compile the optimized code. A true return value from |
| 8391 | // CompileOptimized means that compilation succeeded, not necessarily |
| 8392 | // that optimization succeeded. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8393 | if (CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
| 8394 | function->IsOptimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8395 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 8396 | function->code()->deoptimization_data()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8397 | if (data->OsrPcOffset()->value() >= 0) { |
| 8398 | if (FLAG_trace_osr) { |
| 8399 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8400 | data->OsrPcOffset()->value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8401 | } |
| 8402 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 8403 | } else { |
| 8404 | // We may never generate the desired OSR entry if we emit an |
| 8405 | // early deoptimize. |
| 8406 | succeeded = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8407 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8408 | } else { |
| 8409 | succeeded = false; |
| 8410 | } |
| 8411 | } |
| 8412 | |
| 8413 | // Revert to the original stack checks in the original unoptimized code. |
| 8414 | if (FLAG_trace_osr) { |
| 8415 | PrintF("[restoring original stack checks in "); |
| 8416 | function->PrintName(); |
| 8417 | PrintF("]\n"); |
| 8418 | } |
| 8419 | StackCheckStub check_stub; |
| 8420 | Handle<Code> check_code = check_stub.GetCode(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 8421 | Handle<Code> replacement_code = isolate->builtins()->OnStackReplacement(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8422 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 8423 | *check_code, |
| 8424 | *replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8425 | |
| 8426 | // Allow OSR only at nesting level zero again. |
| 8427 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 8428 | |
| 8429 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 8430 | // smi. This tells the builtin that we need to translate the unoptimized |
| 8431 | // frame to an optimized one. |
| 8432 | if (succeeded) { |
| 8433 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 8434 | return Smi::FromInt(ast_id); |
| 8435 | } else { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8436 | if (function->IsMarkedForLazyRecompilation()) { |
| 8437 | function->ReplaceCode(function->shared()->code()); |
| 8438 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8439 | return Smi::FromInt(-1); |
| 8440 | } |
| 8441 | } |
| 8442 | |
| 8443 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 8444 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckIsBootstrapping) { |
| 8445 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 8446 | return isolate->heap()->undefined_value(); |
| 8447 | } |
| 8448 | |
| 8449 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 8450 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Apply) { |
| 8451 | HandleScope scope(isolate); |
| 8452 | ASSERT(args.length() == 5); |
| 8453 | CONVERT_CHECKED(JSReceiver, fun, args[0]); |
| 8454 | Object* receiver = args[1]; |
| 8455 | CONVERT_CHECKED(JSObject, arguments, args[2]); |
| 8456 | CONVERT_CHECKED(Smi, shift, args[3]); |
| 8457 | CONVERT_CHECKED(Smi, arity, args[4]); |
| 8458 | |
| 8459 | int offset = shift->value(); |
| 8460 | int argc = arity->value(); |
| 8461 | ASSERT(offset >= 0); |
| 8462 | ASSERT(argc >= 0); |
| 8463 | |
| 8464 | // If there are too many arguments, allocate argv via malloc. |
| 8465 | const int argv_small_size = 10; |
| 8466 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8467 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8468 | Handle<Object>* argv = argv_small_buffer; |
| 8469 | if (argc > argv_small_size) { |
| 8470 | argv = new Handle<Object>[argc]; |
| 8471 | if (argv == NULL) return isolate->StackOverflow(); |
| 8472 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8473 | } |
| 8474 | |
| 8475 | for (int i = 0; i < argc; ++i) { |
| 8476 | MaybeObject* maybe = arguments->GetElement(offset + i); |
| 8477 | Object* object; |
| 8478 | if (!maybe->To<Object>(&object)) return maybe; |
| 8479 | argv[i] = Handle<Object>(object); |
| 8480 | } |
| 8481 | |
| 8482 | bool threw = false; |
| 8483 | Handle<JSReceiver> hfun(fun); |
| 8484 | Handle<Object> hreceiver(receiver); |
| 8485 | Handle<Object> result = Execution::Call( |
| 8486 | hfun, hreceiver, argc, reinterpret_cast<Object***>(argv), &threw, true); |
| 8487 | |
| 8488 | if (threw) return Failure::Exception(); |
| 8489 | return *result; |
| 8490 | } |
| 8491 | |
| 8492 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8493 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8494 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8495 | ASSERT(args.length() == 1); |
| 8496 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8497 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 8498 | } |
| 8499 | |
| 8500 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8501 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructorDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8502 | HandleScope scope(isolate); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 8503 | ASSERT(args.length() == 1); |
| 8504 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8505 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 8506 | } |
| 8507 | |
| 8508 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8509 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewFunctionContext) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8510 | NoHandleAllocation ha; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8511 | ASSERT(args.length() == 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8512 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8513 | CONVERT_CHECKED(JSFunction, function, args[0]); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 8514 | int length = function->shared()->scope_info()->NumberOfContextSlots(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8515 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8516 | { MaybeObject* maybe_result = |
| 8517 | isolate->heap()->AllocateFunctionContext(length, function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8518 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8519 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8520 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8521 | isolate->set_context(Context::cast(result)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8522 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8523 | return result; // non-failure |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8524 | } |
| 8525 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8526 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8527 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushWithContext) { |
| 8528 | NoHandleAllocation ha; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8529 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8530 | JSObject* extension_object; |
| 8531 | if (args[0]->IsJSObject()) { |
| 8532 | extension_object = JSObject::cast(args[0]); |
| 8533 | } else { |
| 8534 | // Convert the object to a proper JavaScript object. |
| 8535 | MaybeObject* maybe_js_object = args[0]->ToObject(); |
| 8536 | if (!maybe_js_object->To(&extension_object)) { |
| 8537 | if (Failure::cast(maybe_js_object)->IsInternalError()) { |
| 8538 | HandleScope scope(isolate); |
| 8539 | Handle<Object> handle = args.at<Object>(0); |
| 8540 | Handle<Object> result = |
| 8541 | isolate->factory()->NewTypeError("with_expression", |
| 8542 | HandleVector(&handle, 1)); |
| 8543 | return isolate->Throw(*result); |
| 8544 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8545 | return maybe_js_object; |
| 8546 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8547 | } |
| 8548 | } |
| 8549 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8550 | JSFunction* function; |
| 8551 | if (args[1]->IsSmi()) { |
| 8552 | // A smi sentinel indicates a context nested inside global code rather |
| 8553 | // than some function. There is a canonical empty function that can be |
| 8554 | // gotten from the global context. |
| 8555 | function = isolate->context()->global_context()->closure(); |
| 8556 | } else { |
| 8557 | function = JSFunction::cast(args[1]); |
| 8558 | } |
| 8559 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8560 | Context* context; |
| 8561 | MaybeObject* maybe_context = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8562 | isolate->heap()->AllocateWithContext(function, |
| 8563 | isolate->context(), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8564 | extension_object); |
| 8565 | if (!maybe_context->To(&context)) return maybe_context; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8566 | isolate->set_context(context); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8567 | return context; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
| 8570 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8571 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushCatchContext) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8572 | NoHandleAllocation ha; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8573 | ASSERT(args.length() == 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8574 | String* name = String::cast(args[0]); |
| 8575 | Object* thrown_object = args[1]; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8576 | JSFunction* function; |
| 8577 | if (args[2]->IsSmi()) { |
| 8578 | // A smi sentinel indicates a context nested inside global code rather |
| 8579 | // than some function. There is a canonical empty function that can be |
| 8580 | // gotten from the global context. |
| 8581 | function = isolate->context()->global_context()->closure(); |
| 8582 | } else { |
| 8583 | function = JSFunction::cast(args[2]); |
| 8584 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8585 | Context* context; |
| 8586 | MaybeObject* maybe_context = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8587 | isolate->heap()->AllocateCatchContext(function, |
| 8588 | isolate->context(), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8589 | name, |
| 8590 | thrown_object); |
| 8591 | if (!maybe_context->To(&context)) return maybe_context; |
| 8592 | isolate->set_context(context); |
| 8593 | return context; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8594 | } |
| 8595 | |
| 8596 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 8597 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushBlockContext) { |
| 8598 | NoHandleAllocation ha; |
| 8599 | ASSERT(args.length() == 2); |
| 8600 | SerializedScopeInfo* scope_info = SerializedScopeInfo::cast(args[0]); |
| 8601 | JSFunction* function; |
| 8602 | if (args[1]->IsSmi()) { |
| 8603 | // A smi sentinel indicates a context nested inside global code rather |
| 8604 | // than some function. There is a canonical empty function that can be |
| 8605 | // gotten from the global context. |
| 8606 | function = isolate->context()->global_context()->closure(); |
| 8607 | } else { |
| 8608 | function = JSFunction::cast(args[1]); |
| 8609 | } |
| 8610 | Context* context; |
| 8611 | MaybeObject* maybe_context = |
| 8612 | isolate->heap()->AllocateBlockContext(function, |
| 8613 | isolate->context(), |
| 8614 | scope_info); |
| 8615 | if (!maybe_context->To(&context)) return maybe_context; |
| 8616 | isolate->set_context(context); |
| 8617 | return context; |
| 8618 | } |
| 8619 | |
| 8620 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8621 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8622 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8623 | ASSERT(args.length() == 2); |
| 8624 | |
| 8625 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 8626 | CONVERT_ARG_CHECKED(String, name, 1); |
| 8627 | |
| 8628 | int index; |
| 8629 | PropertyAttributes attributes; |
| 8630 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8631 | BindingFlags binding_flags; |
| 8632 | Handle<Object> holder = context->Lookup(name, |
| 8633 | flags, |
| 8634 | &index, |
| 8635 | &attributes, |
| 8636 | &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8637 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8638 | // If the slot was not found the result is true. |
| 8639 | if (holder.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8640 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8641 | } |
| 8642 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8643 | // If the slot was found in a context, it should be DONT_DELETE. |
| 8644 | if (holder->IsContext()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8645 | return isolate->heap()->false_value(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8646 | } |
| 8647 | |
| 8648 | // The slot was found in a JSObject, either a context extension object, |
| 8649 | // the global object, or an arguments object. Try to delete it |
| 8650 | // (respecting DONT_DELETE). For consistency with V8's usual behavior, |
| 8651 | // which allows deleting all parameters in functions that mention |
| 8652 | // 'arguments', we do this even for the case of slots found on an |
| 8653 | // arguments object. The slot was found on an arguments object if the |
| 8654 | // index is non-negative. |
| 8655 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 8656 | if (index >= 0) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 8657 | return object->DeleteElement(index, JSReceiver::NORMAL_DELETION); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8658 | } else { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 8659 | return object->DeleteProperty(*name, JSReceiver::NORMAL_DELETION); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8660 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8661 | } |
| 8662 | |
| 8663 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8664 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 8665 | // How this is achieved is calling convention-dependent. |
| 8666 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 8667 | // variants where a 64-bit value is returned in two 32-bit registers |
| 8668 | // (edx:eax on ia32, r1:r0 on ARM). |
| 8669 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 8670 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 8671 | // 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] | 8672 | #ifdef V8_HOST_ARCH_64_BIT |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8673 | struct ObjectPair { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8674 | MaybeObject* x; |
| 8675 | MaybeObject* y; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8676 | }; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8677 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8678 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8679 | ObjectPair result = {x, y}; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8680 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 8681 | // In Win64 they are assigned to a hidden first argument. |
| 8682 | return result; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8683 | } |
| 8684 | #else |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8685 | typedef uint64_t ObjectPair; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8686 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8687 | return reinterpret_cast<uint32_t>(x) | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8688 | (reinterpret_cast<ObjectPair>(y) << 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8689 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8690 | #endif |
| 8691 | |
| 8692 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8693 | static inline MaybeObject* Unhole(Heap* heap, |
| 8694 | MaybeObject* x, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8695 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8696 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 8697 | USE(attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8698 | return x->IsTheHole() ? heap->undefined_value() : x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8699 | } |
| 8700 | |
| 8701 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8702 | static Object* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 8703 | JSObject* holder) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8704 | ASSERT(!holder->IsGlobalObject()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8705 | Context* top = isolate->context(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8706 | // Get the context extension function. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8707 | JSFunction* context_extension_function = |
| 8708 | top->global_context()->context_extension_function(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8709 | // If the holder isn't a context extension object, we just return it |
| 8710 | // as the receiver. This allows arguments objects to be used as |
| 8711 | // receivers, but only if they are put in the context scope chain |
| 8712 | // explicitly via a with-statement. |
| 8713 | Object* constructor = holder->map()->constructor(); |
| 8714 | if (constructor != context_extension_function) return holder; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8715 | // Fall back to using the global object as the implicit receiver if |
| 8716 | // the property turns out to be a local variable allocated in a |
| 8717 | // context extension object - introduced via eval. Implicit global |
| 8718 | // receivers are indicated with the hole value. |
| 8719 | return isolate->heap()->the_hole_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8720 | } |
| 8721 | |
| 8722 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8723 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 8724 | Isolate* isolate, |
| 8725 | bool throw_error) { |
| 8726 | HandleScope scope(isolate); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8727 | ASSERT_EQ(2, args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8728 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8729 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8730 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8731 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8732 | Handle<Context> context = args.at<Context>(0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8733 | Handle<String> name = args.at<String>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8734 | |
| 8735 | int index; |
| 8736 | PropertyAttributes attributes; |
| 8737 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8738 | BindingFlags binding_flags; |
| 8739 | Handle<Object> holder = context->Lookup(name, |
| 8740 | flags, |
| 8741 | &index, |
| 8742 | &attributes, |
| 8743 | &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8744 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8745 | // If the index is non-negative, the slot has been found in a local |
| 8746 | // variable or a parameter. Read it from the context object or the |
| 8747 | // arguments object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8748 | if (index >= 0) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8749 | // If the "property" we were looking for is a local variable or an |
| 8750 | // argument in a context, the receiver is the global object; see |
| 8751 | // ECMA-262, 3rd., 10.1.6 and 10.2.3. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8752 | // |
| 8753 | // Use the hole as the receiver to signal that the receiver is |
| 8754 | // implicit and that the global receiver should be used. |
| 8755 | Handle<Object> receiver = isolate->factory()->the_hole_value(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8756 | MaybeObject* value = (holder->IsContext()) |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8757 | ? Context::cast(*holder)->get(index) |
| 8758 | : JSObject::cast(*holder)->GetElement(index); |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8759 | // Check for uninitialized bindings. |
| 8760 | if (holder->IsContext() && |
| 8761 | binding_flags == MUTABLE_CHECK_INITIALIZED && |
| 8762 | value->IsTheHole()) { |
| 8763 | Handle<Object> reference_error = |
| 8764 | isolate->factory()->NewReferenceError("not_defined", |
| 8765 | HandleVector(&name, 1)); |
| 8766 | return MakePair(isolate->Throw(*reference_error), NULL); |
| 8767 | } else { |
| 8768 | return MakePair(Unhole(isolate->heap(), value, attributes), *receiver); |
| 8769 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8770 | } |
| 8771 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8772 | // If the holder is found, we read the property from it. |
| 8773 | if (!holder.is_null() && holder->IsJSObject()) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8774 | ASSERT(Handle<JSObject>::cast(holder)->HasProperty(*name)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8775 | JSObject* object = JSObject::cast(*holder); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8776 | Object* receiver; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8777 | if (object->IsGlobalObject()) { |
| 8778 | receiver = GlobalObject::cast(object)->global_receiver(); |
| 8779 | } else if (context->is_exception_holder(*holder)) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8780 | // Use the hole as the receiver to signal that the receiver is |
| 8781 | // implicit and that the global receiver should be used. |
| 8782 | receiver = isolate->heap()->the_hole_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8783 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8784 | receiver = ComputeReceiverForNonGlobal(isolate, object); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8785 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8786 | |
| 8787 | // GetProperty below can cause GC. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8788 | Handle<Object> receiver_handle(receiver); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8789 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8790 | // No need to unhole the value here. This is taken care of by the |
| 8791 | // GetProperty function. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8792 | MaybeObject* value = object->GetProperty(*name); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8793 | return MakePair(value, *receiver_handle); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8794 | } |
| 8795 | |
| 8796 | if (throw_error) { |
| 8797 | // The property doesn't exist - throw exception. |
| 8798 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8799 | isolate->factory()->NewReferenceError("not_defined", |
| 8800 | HandleVector(&name, 1)); |
| 8801 | return MakePair(isolate->Throw(*reference_error), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8802 | } else { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8803 | // The property doesn't exist - return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8804 | return MakePair(isolate->heap()->undefined_value(), |
| 8805 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8806 | } |
| 8807 | } |
| 8808 | |
| 8809 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8810 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8811 | return LoadContextSlotHelper(args, isolate, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8812 | } |
| 8813 | |
| 8814 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8815 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlotNoReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8816 | return LoadContextSlotHelper(args, isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8817 | } |
| 8818 | |
| 8819 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8820 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8821 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8822 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8823 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8824 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8825 | CONVERT_ARG_CHECKED(Context, context, 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8826 | CONVERT_ARG_CHECKED(String, name, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8827 | CONVERT_SMI_ARG_CHECKED(strict_unchecked, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8828 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 8829 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8830 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8831 | |
| 8832 | int index; |
| 8833 | PropertyAttributes attributes; |
| 8834 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8835 | BindingFlags binding_flags; |
| 8836 | Handle<Object> holder = context->Lookup(name, |
| 8837 | flags, |
| 8838 | &index, |
| 8839 | &attributes, |
| 8840 | &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8841 | |
| 8842 | if (index >= 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8843 | if (holder->IsContext()) { |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8844 | Handle<Context> context = Handle<Context>::cast(holder); |
| 8845 | if (binding_flags == MUTABLE_CHECK_INITIALIZED && |
| 8846 | context->get(index)->IsTheHole()) { |
| 8847 | Handle<Object> error = |
| 8848 | isolate->factory()->NewReferenceError("not_defined", |
| 8849 | HandleVector(&name, 1)); |
| 8850 | return isolate->Throw(*error); |
| 8851 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8852 | // Ignore if read_only variable. |
| 8853 | if ((attributes & READ_ONLY) == 0) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8854 | // Context is a fixed array and set cannot fail. |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8855 | context->set(index, *value); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8856 | } else if (strict_mode == kStrictMode) { |
| 8857 | // Setting read only property in strict mode. |
| 8858 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8859 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 8860 | HandleVector(&name, 1)); |
| 8861 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8862 | } |
| 8863 | } else { |
| 8864 | ASSERT((attributes & READ_ONLY) == 0); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8865 | Handle<Object> result = |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8866 | SetElement(Handle<JSObject>::cast(holder), index, value, strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8867 | if (result.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8868 | ASSERT(isolate->has_pending_exception()); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8869 | return Failure::Exception(); |
| 8870 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8871 | } |
| 8872 | return *value; |
| 8873 | } |
| 8874 | |
| 8875 | // Slow case: The property is not in a FixedArray context. |
| 8876 | // It is either in an JSObject extension context or it was not found. |
| 8877 | Handle<JSObject> context_ext; |
| 8878 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8879 | if (!holder.is_null()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8880 | // The property exists in the extension context. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8881 | context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8882 | } else { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8883 | // The property was not found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8884 | ASSERT(attributes == ABSENT); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8885 | |
| 8886 | if (strict_mode == kStrictMode) { |
| 8887 | // Throw in strict mode (assignment to undefined variable). |
| 8888 | Handle<Object> error = |
| 8889 | isolate->factory()->NewReferenceError( |
| 8890 | "not_defined", HandleVector(&name, 1)); |
| 8891 | return isolate->Throw(*error); |
| 8892 | } |
| 8893 | // In non-strict mode, the property is stored in the global context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8894 | attributes = NONE; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8895 | context_ext = Handle<JSObject>(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8896 | } |
| 8897 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8898 | // Set the property, but ignore if read_only variable on the context |
| 8899 | // extension object itself. |
| 8900 | if ((attributes & READ_ONLY) == 0 || |
| 8901 | (context_ext->GetLocalPropertyAttribute(*name) == ABSENT)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8902 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8903 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8904 | SetProperty(context_ext, name, value, NONE, strict_mode)); |
| 8905 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8906 | // Setting read only property in strict mode. |
| 8907 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8908 | isolate->factory()->NewTypeError( |
| 8909 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 8910 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8911 | } |
| 8912 | return *value; |
| 8913 | } |
| 8914 | |
| 8915 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8916 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Throw) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8917 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8918 | ASSERT(args.length() == 1); |
| 8919 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8920 | return isolate->Throw(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8921 | } |
| 8922 | |
| 8923 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8924 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ReThrow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8925 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8926 | ASSERT(args.length() == 1); |
| 8927 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8928 | return isolate->ReThrow(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8929 | } |
| 8930 | |
| 8931 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8932 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PromoteScheduledException) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8933 | ASSERT_EQ(0, args.length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8934 | return isolate->PromoteScheduledException(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8935 | } |
| 8936 | |
| 8937 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8938 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8939 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8940 | ASSERT(args.length() == 1); |
| 8941 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8942 | Handle<Object> name(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8943 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8944 | isolate->factory()->NewReferenceError("not_defined", |
| 8945 | HandleVector(&name, 1)); |
| 8946 | return isolate->Throw(*reference_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8947 | } |
| 8948 | |
| 8949 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8950 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 8951 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8952 | |
| 8953 | // First check if this is a real stack overflow. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8954 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 8955 | NoHandleAllocation na; |
| 8956 | return isolate->StackOverflow(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 8957 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8958 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8959 | return Execution::HandleStackGuardInterrupt(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8960 | } |
| 8961 | |
| 8962 | |
| 8963 | // NOTE: These PrintXXX functions are defined for all builds (not just |
| 8964 | // DEBUG builds) because we may want to be able to trace function |
| 8965 | // calls in all modes. |
| 8966 | static void PrintString(String* str) { |
| 8967 | // not uncommon to have empty strings |
| 8968 | if (str->length() > 0) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 8969 | SmartArrayPointer<char> s = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8970 | str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 8971 | PrintF("%s", *s); |
| 8972 | } |
| 8973 | } |
| 8974 | |
| 8975 | |
| 8976 | static void PrintObject(Object* obj) { |
| 8977 | if (obj->IsSmi()) { |
| 8978 | PrintF("%d", Smi::cast(obj)->value()); |
| 8979 | } else if (obj->IsString() || obj->IsSymbol()) { |
| 8980 | PrintString(String::cast(obj)); |
| 8981 | } else if (obj->IsNumber()) { |
| 8982 | PrintF("%g", obj->Number()); |
| 8983 | } else if (obj->IsFailure()) { |
| 8984 | PrintF("<failure>"); |
| 8985 | } else if (obj->IsUndefined()) { |
| 8986 | PrintF("<undefined>"); |
| 8987 | } else if (obj->IsNull()) { |
| 8988 | PrintF("<null>"); |
| 8989 | } else if (obj->IsTrue()) { |
| 8990 | PrintF("<true>"); |
| 8991 | } else if (obj->IsFalse()) { |
| 8992 | PrintF("<false>"); |
| 8993 | } else { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 8994 | PrintF("%p", reinterpret_cast<void*>(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8995 | } |
| 8996 | } |
| 8997 | |
| 8998 | |
| 8999 | static int StackSize() { |
| 9000 | int n = 0; |
| 9001 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 9002 | return n; |
| 9003 | } |
| 9004 | |
| 9005 | |
| 9006 | static void PrintTransition(Object* result) { |
| 9007 | // indentation |
| 9008 | { const int nmax = 80; |
| 9009 | int n = StackSize(); |
| 9010 | if (n <= nmax) |
| 9011 | PrintF("%4d:%*s", n, n, ""); |
| 9012 | else |
| 9013 | PrintF("%4d:%*s", n, nmax, "..."); |
| 9014 | } |
| 9015 | |
| 9016 | if (result == NULL) { |
| 9017 | // constructor calls |
| 9018 | JavaScriptFrameIterator it; |
| 9019 | JavaScriptFrame* frame = it.frame(); |
| 9020 | if (frame->IsConstructor()) PrintF("new "); |
| 9021 | // function name |
| 9022 | Object* fun = frame->function(); |
| 9023 | if (fun->IsJSFunction()) { |
| 9024 | PrintObject(JSFunction::cast(fun)->shared()->name()); |
| 9025 | } else { |
| 9026 | PrintObject(fun); |
| 9027 | } |
| 9028 | // function arguments |
| 9029 | // (we are intentionally only printing the actually |
| 9030 | // supplied parameters, not all parameters required) |
| 9031 | PrintF("(this="); |
| 9032 | PrintObject(frame->receiver()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9033 | const int length = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9034 | for (int i = 0; i < length; i++) { |
| 9035 | PrintF(", "); |
| 9036 | PrintObject(frame->GetParameter(i)); |
| 9037 | } |
| 9038 | PrintF(") {\n"); |
| 9039 | |
| 9040 | } else { |
| 9041 | // function result |
| 9042 | PrintF("} -> "); |
| 9043 | PrintObject(result); |
| 9044 | PrintF("\n"); |
| 9045 | } |
| 9046 | } |
| 9047 | |
| 9048 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9049 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceEnter) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9050 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9051 | NoHandleAllocation ha; |
| 9052 | PrintTransition(NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9053 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9054 | } |
| 9055 | |
| 9056 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9057 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceExit) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9058 | NoHandleAllocation ha; |
| 9059 | PrintTransition(args[0]); |
| 9060 | return args[0]; // return TOS |
| 9061 | } |
| 9062 | |
| 9063 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9064 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9065 | NoHandleAllocation ha; |
| 9066 | ASSERT(args.length() == 1); |
| 9067 | |
| 9068 | #ifdef DEBUG |
| 9069 | if (args[0]->IsString()) { |
| 9070 | // If we have a string, assume it's a code "marker" |
| 9071 | // and print some interesting cpu debugging info. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9072 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9073 | JavaScriptFrame* frame = it.frame(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9074 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 9075 | frame->fp(), frame->sp(), frame->caller_sp()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9076 | } else { |
| 9077 | PrintF("DebugPrint: "); |
| 9078 | } |
| 9079 | args[0]->Print(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9080 | if (args[0]->IsHeapObject()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9081 | PrintF("\n"); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9082 | HeapObject::cast(args[0])->map()->Print(); |
| 9083 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9084 | #else |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9085 | // ShortPrint is available in release mode. Print is not. |
| 9086 | args[0]->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9087 | #endif |
| 9088 | PrintF("\n"); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9089 | Flush(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9090 | |
| 9091 | return args[0]; // return TOS |
| 9092 | } |
| 9093 | |
| 9094 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9095 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugTrace) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9096 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9097 | NoHandleAllocation ha; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9098 | isolate->PrintStack(); |
| 9099 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9100 | } |
| 9101 | |
| 9102 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9103 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateCurrentTime) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9104 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9105 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9106 | |
| 9107 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 9108 | // the number in a Date object representing a particular instant in |
| 9109 | // time is milliseconds. Therefore, we floor the result of getting |
| 9110 | // the OS time. |
| 9111 | double millis = floor(OS::TimeCurrentMillis()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9112 | return isolate->heap()->NumberFromDouble(millis); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9113 | } |
| 9114 | |
| 9115 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9116 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateParseString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9117 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9118 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9119 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9120 | CONVERT_ARG_CHECKED(String, str, 0); |
| 9121 | FlattenString(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9122 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9123 | CONVERT_ARG_CHECKED(JSArray, output, 1); |
| 9124 | RUNTIME_ASSERT(output->HasFastElements()); |
| 9125 | |
| 9126 | AssertNoAllocation no_allocation; |
| 9127 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9128 | FixedArray* output_array = FixedArray::cast(output->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9129 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 9130 | bool result; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9131 | String::FlatContent str_content = str->GetFlatContent(); |
| 9132 | if (str_content.IsAscii()) { |
| 9133 | result = DateParser::Parse(str_content.ToAsciiVector(), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 9134 | output_array, |
| 9135 | isolate->unicode_cache()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9136 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9137 | ASSERT(str_content.IsTwoByte()); |
| 9138 | result = DateParser::Parse(str_content.ToUC16Vector(), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 9139 | output_array, |
| 9140 | isolate->unicode_cache()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9141 | } |
| 9142 | |
| 9143 | if (result) { |
| 9144 | return *output; |
| 9145 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9146 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9147 | } |
| 9148 | } |
| 9149 | |
| 9150 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9151 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimezone) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9152 | NoHandleAllocation ha; |
| 9153 | ASSERT(args.length() == 1); |
| 9154 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9155 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 9156 | const char* zone = OS::LocalTimezone(x); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9157 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9158 | } |
| 9159 | |
| 9160 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9161 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimeOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9162 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9163 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9164 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9165 | return isolate->heap()->NumberFromDouble(OS::LocalTimeOffset()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9166 | } |
| 9167 | |
| 9168 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9169 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateDaylightSavingsOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9170 | NoHandleAllocation ha; |
| 9171 | ASSERT(args.length() == 1); |
| 9172 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9173 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9174 | return isolate->heap()->NumberFromDouble(OS::DaylightSavingsOffset(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9175 | } |
| 9176 | |
| 9177 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9178 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalReceiver) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9179 | ASSERT(args.length() == 1); |
| 9180 | Object* global = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9181 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9182 | return JSGlobalObject::cast(global)->global_receiver(); |
| 9183 | } |
| 9184 | |
| 9185 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9186 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9187 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9188 | ASSERT_EQ(1, args.length()); |
| 9189 | CONVERT_ARG_CHECKED(String, source, 0); |
| 9190 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9191 | source = Handle<String>(source->TryFlattenGetString()); |
| 9192 | // Optimized fast case where we only have ascii characters. |
| 9193 | Handle<Object> result; |
| 9194 | if (source->IsSeqAsciiString()) { |
| 9195 | result = JsonParser<true>::Parse(source); |
| 9196 | } else { |
| 9197 | result = JsonParser<false>::Parse(source); |
| 9198 | } |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9199 | if (result.is_null()) { |
| 9200 | // Syntax error or stack overflow in scanner. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9201 | ASSERT(isolate->has_pending_exception()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9202 | return Failure::Exception(); |
| 9203 | } |
| 9204 | return *result; |
| 9205 | } |
| 9206 | |
| 9207 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9208 | bool CodeGenerationFromStringsAllowed(Isolate* isolate, |
| 9209 | Handle<Context> context) { |
| 9210 | if (context->allow_code_gen_from_strings()->IsFalse()) { |
| 9211 | // Check with callback if set. |
| 9212 | AllowCodeGenerationFromStringsCallback callback = |
| 9213 | isolate->allow_code_gen_callback(); |
| 9214 | if (callback == NULL) { |
| 9215 | // No callback set and code generation disallowed. |
| 9216 | return false; |
| 9217 | } else { |
| 9218 | // Callback set. Let it decide if code generation is allowed. |
| 9219 | VMState state(isolate, EXTERNAL); |
| 9220 | return callback(v8::Utils::ToLocal(context)); |
| 9221 | } |
| 9222 | } |
| 9223 | return true; |
| 9224 | } |
| 9225 | |
| 9226 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9227 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9228 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9229 | ASSERT_EQ(1, args.length()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 9230 | CONVERT_ARG_CHECKED(String, source, 0); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9231 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9232 | // Extract global context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9233 | Handle<Context> context(isolate->context()->global_context()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9234 | |
| 9235 | // Check if global context allows code generation from |
| 9236 | // strings. Throw an exception if it doesn't. |
| 9237 | if (!CodeGenerationFromStringsAllowed(isolate, context)) { |
| 9238 | return isolate->Throw(*isolate->factory()->NewError( |
| 9239 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9240 | } |
| 9241 | |
| 9242 | // Compile source string in the global context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 9243 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval(source, |
| 9244 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9245 | true, |
| 9246 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 9247 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9248 | Handle<JSFunction> fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9249 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 9250 | context, |
| 9251 | NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9252 | return *fun; |
| 9253 | } |
| 9254 | |
| 9255 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9256 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 9257 | Handle<String> source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9258 | Handle<Object> receiver, |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 9259 | StrictModeFlag strict_mode) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9260 | Handle<Context> context = Handle<Context>(isolate->context()); |
| 9261 | Handle<Context> global_context = Handle<Context>(context->global_context()); |
| 9262 | |
| 9263 | // Check if global context allows code generation from |
| 9264 | // strings. Throw an exception if it doesn't. |
| 9265 | if (!CodeGenerationFromStringsAllowed(isolate, global_context)) { |
| 9266 | isolate->Throw(*isolate->factory()->NewError( |
| 9267 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9268 | return MakePair(Failure::Exception(), NULL); |
| 9269 | } |
| 9270 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9271 | // Deal with a normal eval call with a string argument. Compile it |
| 9272 | // and return the compiled function bound in the local context. |
| 9273 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 9274 | source, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9275 | Handle<Context>(isolate->context()), |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9276 | context->IsGlobalContext(), |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 9277 | strict_mode); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9278 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9279 | Handle<JSFunction> compiled = |
| 9280 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9281 | shared, context, NOT_TENURED); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9282 | return MakePair(*compiled, *receiver); |
| 9283 | } |
| 9284 | |
| 9285 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9286 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEval) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9287 | ASSERT(args.length() == 4); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9288 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9289 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9290 | Handle<Object> callee = args.at<Object>(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9291 | Handle<Object> receiver; // Will be overwritten. |
| 9292 | |
| 9293 | // Compute the calling context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9294 | Handle<Context> context = Handle<Context>(isolate->context(), isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9295 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9296 | // Make sure Isolate::context() agrees with the old code that traversed |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9297 | // the stack frames to compute the context. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9298 | StackFrameLocator locator; |
| 9299 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9300 | ASSERT(Context::cast(frame->context()) == *context); |
| 9301 | #endif |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9302 | |
| 9303 | // Find where the 'eval' symbol is bound. It is unaliased only if |
| 9304 | // it is bound in the global context. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9305 | int index = -1; |
| 9306 | PropertyAttributes attributes = ABSENT; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 9307 | BindingFlags binding_flags; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9308 | while (true) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9309 | receiver = context->Lookup(isolate->factory()->eval_symbol(), |
| 9310 | FOLLOW_PROTOTYPE_CHAIN, |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 9311 | &index, |
| 9312 | &attributes, |
| 9313 | &binding_flags); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9314 | // Stop search when eval is found or when the global context is |
| 9315 | // reached. |
| 9316 | if (attributes != ABSENT || context->IsGlobalContext()) break; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9317 | context = Handle<Context>(context->previous(), isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9318 | } |
| 9319 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9320 | // If eval could not be resolved, it has been deleted and we need to |
| 9321 | // throw a reference error. |
| 9322 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9323 | Handle<Object> name = isolate->factory()->eval_symbol(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9324 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9325 | isolate->factory()->NewReferenceError("not_defined", |
| 9326 | HandleVector(&name, 1)); |
| 9327 | return MakePair(isolate->Throw(*reference_error), NULL); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9328 | } |
| 9329 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9330 | if (!context->IsGlobalContext()) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9331 | // 'eval' is not bound in the global context. Just call the function |
| 9332 | // with the given arguments. This is not necessarily the global eval. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 9333 | if (receiver->IsContext() || receiver->IsJSContextExtensionObject()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9334 | receiver = isolate->factory()->the_hole_value(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9335 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9336 | return MakePair(*callee, *receiver); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9337 | } |
| 9338 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9339 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 9340 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9341 | if (*callee != isolate->global_context()->global_eval_fun() || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9342 | !args[1]->IsString()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9343 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9344 | } |
| 9345 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9346 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9347 | return CompileGlobalEval(isolate, |
| 9348 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9349 | args.at<Object>(2), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9350 | static_cast<StrictModeFlag>(args.smi_at(3))); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9351 | } |
| 9352 | |
| 9353 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9354 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEvalNoLookup) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9355 | ASSERT(args.length() == 4); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9356 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9357 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9358 | Handle<Object> callee = args.at<Object>(0); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9359 | |
| 9360 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 9361 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9362 | if (*callee != isolate->global_context()->global_eval_fun() || |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9363 | !args[1]->IsString()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9364 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9367 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9368 | return CompileGlobalEval(isolate, |
| 9369 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9370 | args.at<Object>(2), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9371 | static_cast<StrictModeFlag>(args.smi_at(3))); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9372 | } |
| 9373 | |
| 9374 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9375 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNewFunctionAttributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9376 | // This utility adjusts the property attributes for newly created Function |
| 9377 | // object ("new Function(...)") by changing the map. |
| 9378 | // All it does is changing the prototype property to enumerable |
| 9379 | // as specified in ECMA262, 15.3.5.2. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9380 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9381 | ASSERT(args.length() == 1); |
| 9382 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9383 | |
| 9384 | Handle<Map> map = func->shared()->strict_mode() |
| 9385 | ? isolate->strict_mode_function_instance_map() |
| 9386 | : isolate->function_instance_map(); |
| 9387 | |
| 9388 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 9389 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 9390 | func->set_map(*map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9391 | return *func; |
| 9392 | } |
| 9393 | |
| 9394 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9395 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateInNewSpace) { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9396 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 9397 | // Use as fallback for allocation in generated code when NewSpace |
| 9398 | // is full. |
| 9399 | ASSERT(args.length() == 1); |
| 9400 | CONVERT_ARG_CHECKED(Smi, size_smi, 0); |
| 9401 | int size = size_smi->value(); |
| 9402 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 9403 | RUNTIME_ASSERT(size > 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9404 | Heap* heap = isolate->heap(); |
| 9405 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9406 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9407 | Object* allocation; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9408 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9409 | if (maybe_allocation->ToObject(&allocation)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9410 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9411 | } |
| 9412 | return maybe_allocation; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9413 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9414 | } |
| 9415 | |
| 9416 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 9417 | // 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] | 9418 | // array. Returns true if the element was pushed on the stack and |
| 9419 | // false otherwise. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9420 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushIfAbsent) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9421 | ASSERT(args.length() == 2); |
| 9422 | CONVERT_CHECKED(JSArray, array, args[0]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 9423 | CONVERT_CHECKED(JSObject, element, args[1]); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9424 | RUNTIME_ASSERT(array->HasFastElements()); |
| 9425 | int length = Smi::cast(array->length())->value(); |
| 9426 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 9427 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9428 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9429 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9430 | Object* obj; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9431 | // Strict not needed. Used for cycle detection in Array join implementation. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 9432 | { MaybeObject* maybe_obj = |
| 9433 | array->SetFastElement(length, element, kNonStrictMode, true); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9434 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9435 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9436 | return isolate->heap()->true_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9437 | } |
| 9438 | |
| 9439 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9440 | /** |
| 9441 | * A simple visitor visits every element of Array's. |
| 9442 | * The backend storage can be a fixed array for fast elements case, |
| 9443 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 9444 | * of FixedArray, the class can be used by both fast and slow cases. |
| 9445 | * The second parameter of the constructor, fast_elements, specifies |
| 9446 | * whether the storage is a FixedArray or Dictionary. |
| 9447 | * |
| 9448 | * An index limit is used to deal with the situation that a result array |
| 9449 | * length overflows 32-bit non-negative integer. |
| 9450 | */ |
| 9451 | class ArrayConcatVisitor { |
| 9452 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9453 | ArrayConcatVisitor(Isolate* isolate, |
| 9454 | Handle<FixedArray> storage, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9455 | bool fast_elements) : |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9456 | isolate_(isolate), |
| 9457 | storage_(Handle<FixedArray>::cast( |
| 9458 | isolate->global_handles()->Create(*storage))), |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9459 | index_offset_(0u), |
| 9460 | fast_elements_(fast_elements) { } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9461 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9462 | ~ArrayConcatVisitor() { |
| 9463 | clear_storage(); |
| 9464 | } |
| 9465 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9466 | void visit(uint32_t i, Handle<Object> elm) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9467 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9468 | uint32_t index = index_offset_ + i; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9469 | |
| 9470 | if (fast_elements_) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9471 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 9472 | storage_->set(index, *elm); |
| 9473 | return; |
| 9474 | } |
| 9475 | // Our initial estimate of length was foiled, possibly by |
| 9476 | // getters on the arrays increasing the length of later arrays |
| 9477 | // during iteration. |
| 9478 | // This shouldn't happen in anything but pathological cases. |
| 9479 | SetDictionaryMode(index); |
| 9480 | // Fall-through to dictionary mode. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9481 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9482 | ASSERT(!fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9483 | Handle<NumberDictionary> dict(NumberDictionary::cast(*storage_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9484 | Handle<NumberDictionary> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9485 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9486 | if (!result.is_identical_to(dict)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9487 | // Dictionary needed to grow. |
| 9488 | clear_storage(); |
| 9489 | set_storage(*result); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9490 | } |
| 9491 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9492 | |
| 9493 | void increase_index_offset(uint32_t delta) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9494 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 9495 | index_offset_ = JSObject::kMaxElementCount; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9496 | } else { |
| 9497 | index_offset_ += delta; |
| 9498 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9501 | Handle<JSArray> ToArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9502 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9503 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9504 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9505 | Handle<Map> map; |
| 9506 | if (fast_elements_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9507 | map = isolate_->factory()->GetFastElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9508 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9509 | map = isolate_->factory()->GetSlowElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9510 | } |
| 9511 | array->set_map(*map); |
| 9512 | array->set_length(*length); |
| 9513 | array->set_elements(*storage_); |
| 9514 | return array; |
| 9515 | } |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 9516 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9517 | private: |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9518 | // Convert storage to dictionary mode. |
| 9519 | void SetDictionaryMode(uint32_t index) { |
| 9520 | ASSERT(fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9521 | Handle<FixedArray> current_storage(*storage_); |
| 9522 | Handle<NumberDictionary> slow_storage( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9523 | isolate_->factory()->NewNumberDictionary(current_storage->length())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9524 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 9525 | for (uint32_t i = 0; i < current_length; i++) { |
| 9526 | HandleScope loop_scope; |
| 9527 | Handle<Object> element(current_storage->get(i)); |
| 9528 | if (!element->IsTheHole()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9529 | Handle<NumberDictionary> new_storage = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9530 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9531 | if (!new_storage.is_identical_to(slow_storage)) { |
| 9532 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 9533 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9534 | } |
| 9535 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9536 | clear_storage(); |
| 9537 | set_storage(*slow_storage); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9538 | fast_elements_ = false; |
| 9539 | } |
| 9540 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9541 | inline void clear_storage() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9542 | isolate_->global_handles()->Destroy( |
| 9543 | Handle<Object>::cast(storage_).location()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9544 | } |
| 9545 | |
| 9546 | inline void set_storage(FixedArray* storage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9547 | storage_ = Handle<FixedArray>::cast( |
| 9548 | isolate_->global_handles()->Create(storage)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9549 | } |
| 9550 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9551 | Isolate* isolate_; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9552 | Handle<FixedArray> storage_; // Always a global handle. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9553 | // Index after last seen index. Always less than or equal to |
| 9554 | // JSObject::kMaxElementCount. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9555 | uint32_t index_offset_; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9556 | bool fast_elements_; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9557 | }; |
| 9558 | |
| 9559 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9560 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 9561 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 9562 | int element_count = 0; |
| 9563 | switch (array->GetElementsKind()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9564 | case FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9565 | // Fast elements can't have lengths that are not representable by |
| 9566 | // a 32-bit signed integer. |
| 9567 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 9568 | int fast_length = static_cast<int>(length); |
| 9569 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 9570 | for (int i = 0; i < fast_length; i++) { |
| 9571 | if (!elements->get(i)->IsTheHole()) element_count++; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9572 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 9573 | break; |
| 9574 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9575 | case DICTIONARY_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9576 | Handle<NumberDictionary> dictionary( |
| 9577 | NumberDictionary::cast(array->elements())); |
| 9578 | int capacity = dictionary->Capacity(); |
| 9579 | for (int i = 0; i < capacity; i++) { |
| 9580 | Handle<Object> key(dictionary->KeyAt(i)); |
| 9581 | if (dictionary->IsKey(*key)) { |
| 9582 | element_count++; |
| 9583 | } |
| 9584 | } |
| 9585 | break; |
| 9586 | } |
| 9587 | default: |
| 9588 | // External arrays are always dense. |
| 9589 | return length; |
| 9590 | } |
| 9591 | // As an estimate, we assume that the prototype doesn't contain any |
| 9592 | // inherited elements. |
| 9593 | return element_count; |
| 9594 | } |
| 9595 | |
| 9596 | |
| 9597 | |
| 9598 | template<class ExternalArrayClass, class ElementType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9599 | static void IterateExternalArrayElements(Isolate* isolate, |
| 9600 | Handle<JSObject> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9601 | bool elements_are_ints, |
| 9602 | bool elements_are_guaranteed_smis, |
| 9603 | ArrayConcatVisitor* visitor) { |
| 9604 | Handle<ExternalArrayClass> array( |
| 9605 | ExternalArrayClass::cast(receiver->elements())); |
| 9606 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 9607 | |
| 9608 | ASSERT(visitor != NULL); |
| 9609 | if (elements_are_ints) { |
| 9610 | if (elements_are_guaranteed_smis) { |
| 9611 | for (uint32_t j = 0; j < len; j++) { |
| 9612 | HandleScope loop_scope; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9613 | Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get_scalar(j)))); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9614 | visitor->visit(j, e); |
| 9615 | } |
| 9616 | } else { |
| 9617 | for (uint32_t j = 0; j < len; j++) { |
| 9618 | HandleScope loop_scope; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9619 | int64_t val = static_cast<int64_t>(array->get_scalar(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9620 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 9621 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 9622 | visitor->visit(j, e); |
| 9623 | } else { |
| 9624 | Handle<Object> e = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9625 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9626 | visitor->visit(j, e); |
| 9627 | } |
| 9628 | } |
| 9629 | } |
| 9630 | } else { |
| 9631 | for (uint32_t j = 0; j < len; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9632 | HandleScope loop_scope(isolate); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9633 | Handle<Object> e = isolate->factory()->NewNumber(array->get_scalar(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9634 | visitor->visit(j, e); |
| 9635 | } |
| 9636 | } |
| 9637 | } |
| 9638 | |
| 9639 | |
| 9640 | // Used for sorting indices in a List<uint32_t>. |
| 9641 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 9642 | uint32_t a = *ap; |
| 9643 | uint32_t b = *bp; |
| 9644 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 9645 | } |
| 9646 | |
| 9647 | |
| 9648 | static void CollectElementIndices(Handle<JSObject> object, |
| 9649 | uint32_t range, |
| 9650 | List<uint32_t>* indices) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9651 | ElementsKind kind = object->GetElementsKind(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9652 | switch (kind) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9653 | case FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9654 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 9655 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 9656 | if (range < length) length = range; |
| 9657 | for (uint32_t i = 0; i < length; i++) { |
| 9658 | if (!elements->get(i)->IsTheHole()) { |
| 9659 | indices->Add(i); |
| 9660 | } |
| 9661 | } |
| 9662 | break; |
| 9663 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9664 | case DICTIONARY_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9665 | Handle<NumberDictionary> dict(NumberDictionary::cast(object->elements())); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9666 | uint32_t capacity = dict->Capacity(); |
| 9667 | for (uint32_t j = 0; j < capacity; j++) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9668 | HandleScope loop_scope; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9669 | Handle<Object> k(dict->KeyAt(j)); |
| 9670 | if (dict->IsKey(*k)) { |
| 9671 | ASSERT(k->IsNumber()); |
| 9672 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 9673 | if (index < range) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9674 | indices->Add(index); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9675 | } |
| 9676 | } |
| 9677 | } |
| 9678 | break; |
| 9679 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9680 | default: { |
| 9681 | int dense_elements_length; |
| 9682 | switch (kind) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9683 | case EXTERNAL_PIXEL_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9684 | dense_elements_length = |
| 9685 | ExternalPixelArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9686 | break; |
| 9687 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9688 | case EXTERNAL_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9689 | dense_elements_length = |
| 9690 | ExternalByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9691 | break; |
| 9692 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9693 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9694 | dense_elements_length = |
| 9695 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9696 | break; |
| 9697 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9698 | case EXTERNAL_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9699 | dense_elements_length = |
| 9700 | ExternalShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9701 | break; |
| 9702 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9703 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9704 | dense_elements_length = |
| 9705 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9706 | break; |
| 9707 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9708 | case EXTERNAL_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9709 | dense_elements_length = |
| 9710 | ExternalIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9711 | break; |
| 9712 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9713 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9714 | dense_elements_length = |
| 9715 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9716 | break; |
| 9717 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9718 | case EXTERNAL_FLOAT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9719 | dense_elements_length = |
| 9720 | ExternalFloatArray::cast(object->elements())->length(); |
| 9721 | break; |
| 9722 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9723 | case EXTERNAL_DOUBLE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9724 | dense_elements_length = |
| 9725 | ExternalDoubleArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9726 | break; |
| 9727 | } |
| 9728 | default: |
| 9729 | UNREACHABLE(); |
| 9730 | dense_elements_length = 0; |
| 9731 | break; |
| 9732 | } |
| 9733 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 9734 | if (range <= length) { |
| 9735 | length = range; |
| 9736 | // We will add all indices, so we might as well clear it first |
| 9737 | // and avoid duplicates. |
| 9738 | indices->Clear(); |
| 9739 | } |
| 9740 | for (uint32_t i = 0; i < length; i++) { |
| 9741 | indices->Add(i); |
| 9742 | } |
| 9743 | if (length == range) return; // All indices accounted for already. |
| 9744 | break; |
| 9745 | } |
| 9746 | } |
| 9747 | |
| 9748 | Handle<Object> prototype(object->GetPrototype()); |
| 9749 | if (prototype->IsJSObject()) { |
| 9750 | // The prototype will usually have no inherited element indices, |
| 9751 | // but we have to check. |
| 9752 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 9753 | } |
| 9754 | } |
| 9755 | |
| 9756 | |
| 9757 | /** |
| 9758 | * A helper function that visits elements of a JSArray in numerical |
| 9759 | * order. |
| 9760 | * |
| 9761 | * The visitor argument called for each existing element in the array |
| 9762 | * with the element index and the element's value. |
| 9763 | * Afterwards it increments the base-index of the visitor by the array |
| 9764 | * length. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9765 | * Returns false if any access threw an exception, otherwise true. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9766 | */ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9767 | static bool IterateElements(Isolate* isolate, |
| 9768 | Handle<JSArray> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9769 | ArrayConcatVisitor* visitor) { |
| 9770 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 9771 | switch (receiver->GetElementsKind()) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9772 | case FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9773 | // Run through the elements FixedArray and use HasElement and GetElement |
| 9774 | // to check the prototype for missing elements. |
| 9775 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 9776 | int fast_length = static_cast<int>(length); |
| 9777 | ASSERT(fast_length <= elements->length()); |
| 9778 | for (int j = 0; j < fast_length; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9779 | HandleScope loop_scope(isolate); |
| 9780 | Handle<Object> element_value(elements->get(j), isolate); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9781 | if (!element_value->IsTheHole()) { |
| 9782 | visitor->visit(j, element_value); |
| 9783 | } else if (receiver->HasElement(j)) { |
| 9784 | // Call GetElement on receiver, not its prototype, or getters won't |
| 9785 | // have the correct receiver. |
| 9786 | element_value = GetElement(receiver, j); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9787 | if (element_value.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9788 | visitor->visit(j, element_value); |
| 9789 | } |
| 9790 | } |
| 9791 | break; |
| 9792 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9793 | case DICTIONARY_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9794 | Handle<NumberDictionary> dict(receiver->element_dictionary()); |
| 9795 | List<uint32_t> indices(dict->Capacity() / 2); |
| 9796 | // Collect all indices in the object and the prototypes less |
| 9797 | // than length. This might introduce duplicates in the indices list. |
| 9798 | CollectElementIndices(receiver, length, &indices); |
| 9799 | indices.Sort(&compareUInt32); |
| 9800 | int j = 0; |
| 9801 | int n = indices.length(); |
| 9802 | while (j < n) { |
| 9803 | HandleScope loop_scope; |
| 9804 | uint32_t index = indices[j]; |
| 9805 | Handle<Object> element = GetElement(receiver, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9806 | if (element.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9807 | visitor->visit(index, element); |
| 9808 | // Skip to next different index (i.e., omit duplicates). |
| 9809 | do { |
| 9810 | j++; |
| 9811 | } while (j < n && indices[j] == index); |
| 9812 | } |
| 9813 | break; |
| 9814 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9815 | case EXTERNAL_PIXEL_ELEMENTS: { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9816 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 9817 | receiver->elements())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9818 | for (uint32_t j = 0; j < length; j++) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9819 | Handle<Smi> e(Smi::FromInt(pixels->get_scalar(j))); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9820 | visitor->visit(j, e); |
| 9821 | } |
| 9822 | break; |
| 9823 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9824 | case EXTERNAL_BYTE_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9825 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9826 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9827 | break; |
| 9828 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9829 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9830 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9831 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9832 | break; |
| 9833 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9834 | case EXTERNAL_SHORT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9835 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9836 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9837 | break; |
| 9838 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9839 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9840 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9841 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9842 | break; |
| 9843 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9844 | case EXTERNAL_INT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9845 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9846 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9847 | break; |
| 9848 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9849 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9850 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9851 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9852 | break; |
| 9853 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9854 | case EXTERNAL_FLOAT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9855 | IterateExternalArrayElements<ExternalFloatArray, float>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9856 | isolate, receiver, false, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9857 | break; |
| 9858 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9859 | case EXTERNAL_DOUBLE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9860 | IterateExternalArrayElements<ExternalDoubleArray, double>( |
| 9861 | isolate, receiver, false, false, visitor); |
| 9862 | break; |
| 9863 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9864 | default: |
| 9865 | UNREACHABLE(); |
| 9866 | break; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9867 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9868 | visitor->increase_index_offset(length); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9869 | return true; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9870 | } |
| 9871 | |
| 9872 | |
| 9873 | /** |
| 9874 | * Array::concat implementation. |
| 9875 | * See ECMAScript 262, 15.4.4.4. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9876 | * 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] | 9877 | * following the ECMAScript 5 specification. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9878 | */ |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9879 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayConcat) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9880 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9881 | HandleScope handle_scope(isolate); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9882 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9883 | CONVERT_ARG_CHECKED(JSArray, arguments, 0); |
| 9884 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 9885 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 9886 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9887 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9888 | // Pass 1: estimate the length and number of elements of the result. |
| 9889 | // The actual length can be larger if any of the arguments have getters |
| 9890 | // that mutate other arguments (but will otherwise be precise). |
| 9891 | // 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] | 9892 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9893 | uint32_t estimate_result_length = 0; |
| 9894 | uint32_t estimate_nof_elements = 0; |
| 9895 | { |
| 9896 | for (int i = 0; i < argument_count; i++) { |
| 9897 | HandleScope loop_scope; |
| 9898 | Handle<Object> obj(elements->get(i)); |
| 9899 | uint32_t length_estimate; |
| 9900 | uint32_t element_estimate; |
| 9901 | if (obj->IsJSArray()) { |
| 9902 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
| 9903 | length_estimate = |
| 9904 | static_cast<uint32_t>(array->length()->Number()); |
| 9905 | element_estimate = |
| 9906 | EstimateElementCount(array); |
| 9907 | } else { |
| 9908 | length_estimate = 1; |
| 9909 | element_estimate = 1; |
| 9910 | } |
| 9911 | // Avoid overflows by capping at kMaxElementCount. |
| 9912 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 9913 | length_estimate) { |
| 9914 | estimate_result_length = JSObject::kMaxElementCount; |
| 9915 | } else { |
| 9916 | estimate_result_length += length_estimate; |
| 9917 | } |
| 9918 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 9919 | element_estimate) { |
| 9920 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 9921 | } else { |
| 9922 | estimate_nof_elements += element_estimate; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9923 | } |
| 9924 | } |
| 9925 | } |
| 9926 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9927 | // If estimated number of elements is more than half of length, a |
| 9928 | // fixed array (fast case) is more time and space-efficient than a |
| 9929 | // dictionary. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9930 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9931 | |
| 9932 | Handle<FixedArray> storage; |
| 9933 | if (fast_case) { |
| 9934 | // The backing storage array must have non-existing elements to |
| 9935 | // preserve holes across concat operations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9936 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 9937 | estimate_result_length); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9938 | } else { |
| 9939 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 9940 | uint32_t at_least_space_for = estimate_nof_elements + |
| 9941 | (estimate_nof_elements >> 2); |
| 9942 | storage = Handle<FixedArray>::cast( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9943 | isolate->factory()->NewNumberDictionary(at_least_space_for)); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9944 | } |
| 9945 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9946 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9947 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9948 | for (int i = 0; i < argument_count; i++) { |
| 9949 | Handle<Object> obj(elements->get(i)); |
| 9950 | if (obj->IsJSArray()) { |
| 9951 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9952 | if (!IterateElements(isolate, array, &visitor)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9953 | return Failure::Exception(); |
| 9954 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9955 | } else { |
| 9956 | visitor.visit(0, obj); |
| 9957 | visitor.increase_index_offset(1); |
| 9958 | } |
| 9959 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9960 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9961 | return *visitor.ToArray(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9962 | } |
| 9963 | |
| 9964 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9965 | // This will not allocate (flatten the string), but it may run |
| 9966 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9967 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9968 | NoHandleAllocation ha; |
| 9969 | ASSERT(args.length() == 1); |
| 9970 | |
| 9971 | CONVERT_CHECKED(String, string, args[0]); |
| 9972 | StringInputBuffer buffer(string); |
| 9973 | while (buffer.has_more()) { |
| 9974 | uint16_t character = buffer.GetNext(); |
| 9975 | PrintF("%c", character); |
| 9976 | } |
| 9977 | return string; |
| 9978 | } |
| 9979 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9980 | // Moves all own elements of an object, that are below a limit, to positions |
| 9981 | // starting at zero. All undefined values are placed after non-undefined values, |
| 9982 | // and are followed by non-existing element. Does not change the length |
| 9983 | // property. |
| 9984 | // Returns the number of non-undefined elements collected. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9985 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RemoveArrayHoles) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9986 | ASSERT(args.length() == 2); |
| 9987 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9988 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 9989 | return object->PrepareElementsForSort(limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9990 | } |
| 9991 | |
| 9992 | |
| 9993 | // Move contents of argument 0 (an array) to argument 1 (an array) |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9994 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MoveArrayContents) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9995 | ASSERT(args.length() == 2); |
| 9996 | CONVERT_CHECKED(JSArray, from, args[0]); |
| 9997 | CONVERT_CHECKED(JSArray, to, args[1]); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 9998 | FixedArrayBase* new_elements = from->elements(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9999 | MaybeObject* maybe_new_map; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10000 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 10001 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10002 | maybe_new_map = to->map()->GetFastElementsMap(); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10003 | } else if (new_elements->map() == |
| 10004 | isolate->heap()->fixed_double_array_map()) { |
| 10005 | maybe_new_map = to->map()->GetFastDoubleElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10006 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10007 | maybe_new_map = to->map()->GetSlowElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10008 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10009 | Object* new_map; |
| 10010 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10011 | to->set_map(Map::cast(new_map)); |
| 10012 | to->set_elements(new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10013 | to->set_length(from->length()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10014 | Object* obj; |
| 10015 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 10016 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 10017 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10018 | from->set_length(Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10019 | return to; |
| 10020 | } |
| 10021 | |
| 10022 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10023 | // How many elements does this object/array have? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10024 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EstimateNumberOfElements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10025 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10026 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 10027 | HeapObject* elements = object->elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10028 | if (elements->IsDictionary()) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 10029 | return Smi::FromInt(NumberDictionary::cast(elements)->NumberOfElements()); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10030 | } else if (object->IsJSArray()) { |
| 10031 | return JSArray::cast(object)->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10032 | } else { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10033 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10034 | } |
| 10035 | } |
| 10036 | |
| 10037 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10038 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SwapElements) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10039 | HandleScope handle_scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10040 | |
| 10041 | ASSERT_EQ(3, args.length()); |
| 10042 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10043 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10044 | Handle<Object> key1 = args.at<Object>(1); |
| 10045 | Handle<Object> key2 = args.at<Object>(2); |
| 10046 | |
| 10047 | uint32_t index1, index2; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10048 | if (!key1->ToArrayIndex(&index1) |
| 10049 | || !key2->ToArrayIndex(&index2)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10050 | return isolate->ThrowIllegalOperation(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10051 | } |
| 10052 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10053 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
| 10054 | Handle<Object> tmp1 = GetElement(jsobject, index1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10055 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10056 | Handle<Object> tmp2 = GetElement(jsobject, index2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10057 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10058 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10059 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 10060 | SetElement(jsobject, index1, tmp2, kStrictMode)); |
| 10061 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 10062 | SetElement(jsobject, index2, tmp1, kStrictMode)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10063 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10064 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10065 | } |
| 10066 | |
| 10067 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10068 | // 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] | 10069 | // might have elements. Can either return keys (positive integers) or |
| 10070 | // intervals (pair of a negative integer (-start-1) followed by a |
| 10071 | // positive (length)) or undefined values. |
| 10072 | // Intervals can span over some keys that are not in the object. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10073 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArrayKeys) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10074 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10075 | HandleScope scope(isolate); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 10076 | CONVERT_ARG_CHECKED(JSObject, array, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10077 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10078 | if (array->elements()->IsDictionary()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10079 | // Create an array and get all the keys into it, then remove all the |
| 10080 | // 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] | 10081 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(array, INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10082 | int keys_length = keys->length(); |
| 10083 | for (int i = 0; i < keys_length; i++) { |
| 10084 | Object* key = keys->get(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10085 | uint32_t index = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10086 | if (!key->ToArrayIndex(&index) || index >= length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10087 | // Zap invalid keys. |
| 10088 | keys->set_undefined(i); |
| 10089 | } |
| 10090 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10091 | return *isolate->factory()->NewJSArrayWithElements(keys); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10092 | } else { |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10093 | ASSERT(array->HasFastElements() || array->HasFastDoubleElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10094 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10095 | // -1 means start of array. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10096 | single_interval->set(0, Smi::FromInt(-1)); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10097 | FixedArrayBase* elements = FixedArrayBase::cast(array->elements()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10098 | uint32_t actual_length = |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10099 | static_cast<uint32_t>(elements->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 10100 | uint32_t min_length = actual_length < length ? actual_length : length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10101 | Handle<Object> length_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10102 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10103 | single_interval->set(1, *length_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10104 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10105 | } |
| 10106 | } |
| 10107 | |
| 10108 | |
| 10109 | // DefineAccessor takes an optional final argument which is the |
| 10110 | // property attributes (eg, DONT_ENUM, DONT_DELETE). IMPORTANT: due |
| 10111 | // to the way accessors are implemented, it is set for both the getter |
| 10112 | // and setter on the first call to DefineAccessor and ignored on |
| 10113 | // subsequent calls. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10114 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10115 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
| 10116 | // Compute attributes. |
| 10117 | PropertyAttributes attributes = NONE; |
| 10118 | if (args.length() == 5) { |
| 10119 | CONVERT_CHECKED(Smi, attrs, args[4]); |
| 10120 | int value = attrs->value(); |
| 10121 | // Only attribute bits should be set. |
| 10122 | ASSERT((value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 10123 | attributes = static_cast<PropertyAttributes>(value); |
| 10124 | } |
| 10125 | |
| 10126 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 10127 | CONVERT_CHECKED(String, name, args[1]); |
| 10128 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 10129 | CONVERT_CHECKED(JSFunction, fun, args[3]); |
| 10130 | return obj->DefineAccessor(name, flag->value() == 0, fun, attributes); |
| 10131 | } |
| 10132 | |
| 10133 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10134 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LookupAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10135 | ASSERT(args.length() == 3); |
| 10136 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 10137 | CONVERT_CHECKED(String, name, args[1]); |
| 10138 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 10139 | return obj->LookupAccessor(name, flag->value() == 0); |
| 10140 | } |
| 10141 | |
| 10142 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 10143 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10144 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugBreak) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 10145 | ASSERT(args.length() == 0); |
| 10146 | return Execution::DebugBreakHelper(); |
| 10147 | } |
| 10148 | |
| 10149 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10150 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 10151 | static Smi* WrapFrameId(StackFrame::Id id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 10152 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10153 | return Smi::FromInt(id >> 2); |
| 10154 | } |
| 10155 | |
| 10156 | |
| 10157 | static StackFrame::Id UnwrapFrameId(Smi* wrapped) { |
| 10158 | return static_cast<StackFrame::Id>(wrapped->value() << 2); |
| 10159 | } |
| 10160 | |
| 10161 | |
| 10162 | // Adds a JavaScript function as a debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 10163 | // args[0]: debug event listener function to set or null or undefined for |
| 10164 | // clearing the event listener function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10165 | // args[1]: object supplied during callback |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10166 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDebugEventListener) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10167 | ASSERT(args.length() == 2); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 10168 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 10169 | args[0]->IsUndefined() || |
| 10170 | args[0]->IsNull()); |
| 10171 | Handle<Object> callback = args.at<Object>(0); |
| 10172 | Handle<Object> data = args.at<Object>(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10173 | isolate->debugger()->SetEventListener(callback, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10174 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10175 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10176 | } |
| 10177 | |
| 10178 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10179 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Break) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10180 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10181 | isolate->stack_guard()->DebugBreak(); |
| 10182 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10183 | } |
| 10184 | |
| 10185 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10186 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 10187 | Object* receiver, |
| 10188 | String* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10189 | LookupResult* result, |
| 10190 | bool* caught_exception) { |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10191 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10192 | switch (result->type()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10193 | case NORMAL: |
| 10194 | value = result->holder()->GetNormalizedProperty(result); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10195 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10196 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10197 | } |
| 10198 | return value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10199 | case FIELD: |
| 10200 | value = |
| 10201 | JSObject::cast( |
| 10202 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 10203 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10204 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10205 | } |
| 10206 | return value; |
| 10207 | case CONSTANT_FUNCTION: |
| 10208 | return result->GetConstantFunction(); |
| 10209 | case CALLBACKS: { |
| 10210 | Object* structure = result->GetCallbackObject(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10211 | if (structure->IsForeign() || structure->IsAccessorInfo()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10212 | MaybeObject* maybe_value = receiver->GetPropertyWithCallback( |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 10213 | receiver, structure, name, result->holder()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10214 | if (!maybe_value->ToObject(&value)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10215 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10216 | ASSERT(maybe_value->IsException()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10217 | maybe_value = heap->isolate()->pending_exception(); |
| 10218 | heap->isolate()->clear_pending_exception(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10219 | if (caught_exception != NULL) { |
| 10220 | *caught_exception = true; |
| 10221 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10222 | return maybe_value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10223 | } |
| 10224 | return value; |
| 10225 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10226 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10227 | } |
| 10228 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10229 | case INTERCEPTOR: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10230 | case MAP_TRANSITION: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10231 | case ELEMENTS_TRANSITION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10232 | case CONSTANT_TRANSITION: |
| 10233 | case NULL_DESCRIPTOR: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10234 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10235 | default: |
| 10236 | UNREACHABLE(); |
| 10237 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10238 | UNREACHABLE(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10239 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10240 | } |
| 10241 | |
| 10242 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10243 | // Get debugger related details for an object property. |
| 10244 | // args[0]: object holding property |
| 10245 | // args[1]: name of the property |
| 10246 | // |
| 10247 | // The array returned contains the following information: |
| 10248 | // 0: Property value |
| 10249 | // 1: Property details |
| 10250 | // 2: Property value is exception |
| 10251 | // 3: Getter function if defined |
| 10252 | // 4: Setter function if defined |
| 10253 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 10254 | // defined through __defineGetter__ and/or __defineSetter__. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10255 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPropertyDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10256 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10257 | |
| 10258 | ASSERT(args.length() == 2); |
| 10259 | |
| 10260 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10261 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10262 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 10263 | // Make sure to set the current context to the context before the debugger was |
| 10264 | // entered (if the debugger is entered). The reason for switching context here |
| 10265 | // is that for some property lookups (accessors and interceptors) callbacks |
| 10266 | // into the embedding application can occour, and the embedding application |
| 10267 | // could have the assumption that its own global context is the current |
| 10268 | // context and not some internal debugger context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10269 | SaveContext save(isolate); |
| 10270 | if (isolate->debug()->InDebugger()) { |
| 10271 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 10272 | } |
| 10273 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10274 | // Skip the global proxy as it has no properties and always delegates to the |
| 10275 | // real global object. |
| 10276 | if (obj->IsJSGlobalProxy()) { |
| 10277 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 10278 | } |
| 10279 | |
| 10280 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10281 | // Check if the name is trivially convertible to an index and get the element |
| 10282 | // if so. |
| 10283 | uint32_t index; |
| 10284 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10285 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10286 | Object* element_or_char; |
| 10287 | { MaybeObject* maybe_element_or_char = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10288 | Runtime::GetElementOrCharAt(isolate, obj, index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10289 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 10290 | return maybe_element_or_char; |
| 10291 | } |
| 10292 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10293 | details->set(0, element_or_char); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10294 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10295 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10296 | } |
| 10297 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10298 | // Find the number of objects making up this. |
| 10299 | int length = LocalPrototypeChainLength(*obj); |
| 10300 | |
| 10301 | // Try local lookup on each of the objects. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10302 | Handle<JSObject> jsproto = obj; |
| 10303 | for (int i = 0; i < length; i++) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10304 | LookupResult result; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10305 | jsproto->LocalLookup(*name, &result); |
| 10306 | if (result.IsProperty()) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10307 | // LookupResult is not GC safe as it holds raw object pointers. |
| 10308 | // GC can happen later in this code so put the required fields into |
| 10309 | // local variables using handles when required for later use. |
| 10310 | PropertyType result_type = result.type(); |
| 10311 | Handle<Object> result_callback_obj; |
| 10312 | if (result_type == CALLBACKS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10313 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 10314 | isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10315 | } |
| 10316 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 10317 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 10318 | // to be copied to handles before this. |
| 10319 | bool caught_exception = false; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10320 | Object* raw_value; |
| 10321 | { MaybeObject* maybe_raw_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10322 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 10323 | &result, &caught_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10324 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 10325 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10326 | Handle<Object> value(raw_value, isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10327 | |
| 10328 | // If the callback object is a fixed array then it contains JavaScript |
| 10329 | // getter and/or setter. |
| 10330 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
| 10331 | result_callback_obj->IsFixedArray(); |
| 10332 | Handle<FixedArray> details = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10333 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10334 | details->set(0, *value); |
| 10335 | details->set(1, property_details); |
| 10336 | if (hasJavaScriptAccessors) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10337 | details->set(2, isolate->heap()->ToBoolean(caught_exception)); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10338 | details->set(3, FixedArray::cast(*result_callback_obj)->get(0)); |
| 10339 | details->set(4, FixedArray::cast(*result_callback_obj)->get(1)); |
| 10340 | } |
| 10341 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10342 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10343 | } |
| 10344 | if (i < length - 1) { |
| 10345 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 10346 | } |
| 10347 | } |
| 10348 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10349 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10350 | } |
| 10351 | |
| 10352 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10353 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetProperty) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10354 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10355 | |
| 10356 | ASSERT(args.length() == 2); |
| 10357 | |
| 10358 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10359 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10360 | |
| 10361 | LookupResult result; |
| 10362 | obj->Lookup(*name, &result); |
| 10363 | if (result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10364 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10365 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10366 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10367 | } |
| 10368 | |
| 10369 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10370 | // Return the property type calculated from the property details. |
| 10371 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10372 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyTypeFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10373 | ASSERT(args.length() == 1); |
| 10374 | CONVERT_CHECKED(Smi, details, args[0]); |
| 10375 | PropertyType type = PropertyDetails(details).type(); |
| 10376 | return Smi::FromInt(static_cast<int>(type)); |
| 10377 | } |
| 10378 | |
| 10379 | |
| 10380 | // Return the property attribute calculated from the property details. |
| 10381 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10382 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyAttributesFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10383 | ASSERT(args.length() == 1); |
| 10384 | CONVERT_CHECKED(Smi, details, args[0]); |
| 10385 | PropertyAttributes attributes = PropertyDetails(details).attributes(); |
| 10386 | return Smi::FromInt(static_cast<int>(attributes)); |
| 10387 | } |
| 10388 | |
| 10389 | |
| 10390 | // Return the property insertion index calculated from the property details. |
| 10391 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10392 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyIndexFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10393 | ASSERT(args.length() == 1); |
| 10394 | CONVERT_CHECKED(Smi, details, args[0]); |
| 10395 | int index = PropertyDetails(details).index(); |
| 10396 | return Smi::FromInt(index); |
| 10397 | } |
| 10398 | |
| 10399 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10400 | // Return property value from named interceptor. |
| 10401 | // args[0]: object |
| 10402 | // args[1]: property name |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10403 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugNamedInterceptorPropertyValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10404 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10405 | ASSERT(args.length() == 2); |
| 10406 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10407 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
| 10408 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10409 | |
| 10410 | PropertyAttributes attributes; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10411 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10412 | } |
| 10413 | |
| 10414 | |
| 10415 | // Return element value from indexed interceptor. |
| 10416 | // args[0]: object |
| 10417 | // args[1]: index |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10418 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugIndexedInterceptorElementValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10419 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10420 | ASSERT(args.length() == 2); |
| 10421 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10422 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 10423 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 10424 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10425 | return obj->GetElementWithInterceptor(*obj, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10426 | } |
| 10427 | |
| 10428 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10429 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckExecutionState) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10430 | ASSERT(args.length() >= 1); |
| 10431 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10432 | // Check that the break id is valid. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10433 | if (isolate->debug()->break_id() == 0 || |
| 10434 | break_id != isolate->debug()->break_id()) { |
| 10435 | return isolate->Throw( |
| 10436 | isolate->heap()->illegal_execution_state_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10437 | } |
| 10438 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10439 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10440 | } |
| 10441 | |
| 10442 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10443 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10444 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10445 | ASSERT(args.length() == 1); |
| 10446 | |
| 10447 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10448 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10449 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 10450 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10451 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10452 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10453 | |
| 10454 | // Count all frames which are relevant to debugging stack trace. |
| 10455 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10456 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10457 | if (id == StackFrame::NO_ID) { |
| 10458 | // If there is no JavaScript stack frame count is 0. |
| 10459 | return Smi::FromInt(0); |
| 10460 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10461 | |
| 10462 | for (JavaScriptFrameIterator it(isolate, id); !it.done(); it.Advance()) { |
| 10463 | n += it.frame()->GetInlineCount(); |
| 10464 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10465 | return Smi::FromInt(n); |
| 10466 | } |
| 10467 | |
| 10468 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10469 | class FrameInspector { |
| 10470 | public: |
| 10471 | FrameInspector(JavaScriptFrame* frame, |
| 10472 | int inlined_frame_index, |
| 10473 | Isolate* isolate) |
| 10474 | : frame_(frame), deoptimized_frame_(NULL), isolate_(isolate) { |
| 10475 | // Calculate the deoptimized frame. |
| 10476 | if (frame->is_optimized()) { |
| 10477 | deoptimized_frame_ = Deoptimizer::DebuggerInspectableFrame( |
| 10478 | frame, inlined_frame_index, isolate); |
| 10479 | } |
| 10480 | has_adapted_arguments_ = frame_->has_adapted_arguments(); |
| 10481 | is_optimized_ = frame_->is_optimized(); |
| 10482 | } |
| 10483 | |
| 10484 | ~FrameInspector() { |
| 10485 | // Get rid of the calculated deoptimized frame if any. |
| 10486 | if (deoptimized_frame_ != NULL) { |
| 10487 | Deoptimizer::DeleteDebuggerInspectableFrame(deoptimized_frame_, |
| 10488 | isolate_); |
| 10489 | } |
| 10490 | } |
| 10491 | |
| 10492 | int GetParametersCount() { |
| 10493 | return is_optimized_ |
| 10494 | ? deoptimized_frame_->parameters_count() |
| 10495 | : frame_->ComputeParametersCount(); |
| 10496 | } |
| 10497 | int expression_count() { return deoptimized_frame_->expression_count(); } |
| 10498 | Object* GetFunction() { |
| 10499 | return is_optimized_ |
| 10500 | ? deoptimized_frame_->GetFunction() |
| 10501 | : frame_->function(); |
| 10502 | } |
| 10503 | Object* GetParameter(int index) { |
| 10504 | return is_optimized_ |
| 10505 | ? deoptimized_frame_->GetParameter(index) |
| 10506 | : frame_->GetParameter(index); |
| 10507 | } |
| 10508 | Object* GetExpression(int index) { |
| 10509 | return is_optimized_ |
| 10510 | ? deoptimized_frame_->GetExpression(index) |
| 10511 | : frame_->GetExpression(index); |
| 10512 | } |
| 10513 | |
| 10514 | // To inspect all the provided arguments the frame might need to be |
| 10515 | // replaced with the arguments frame. |
| 10516 | void SetArgumentsFrame(JavaScriptFrame* frame) { |
| 10517 | ASSERT(has_adapted_arguments_); |
| 10518 | frame_ = frame; |
| 10519 | is_optimized_ = frame_->is_optimized(); |
| 10520 | ASSERT(!is_optimized_); |
| 10521 | } |
| 10522 | |
| 10523 | private: |
| 10524 | JavaScriptFrame* frame_; |
| 10525 | DeoptimizedFrameInfo* deoptimized_frame_; |
| 10526 | Isolate* isolate_; |
| 10527 | bool is_optimized_; |
| 10528 | bool has_adapted_arguments_; |
| 10529 | |
| 10530 | DISALLOW_COPY_AND_ASSIGN(FrameInspector); |
| 10531 | }; |
| 10532 | |
| 10533 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10534 | static const int kFrameDetailsFrameIdIndex = 0; |
| 10535 | static const int kFrameDetailsReceiverIndex = 1; |
| 10536 | static const int kFrameDetailsFunctionIndex = 2; |
| 10537 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 10538 | static const int kFrameDetailsLocalCountIndex = 4; |
| 10539 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 10540 | static const int kFrameDetailsConstructCallIndex = 6; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10541 | static const int kFrameDetailsAtReturnIndex = 7; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10542 | static const int kFrameDetailsFlagsIndex = 8; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10543 | static const int kFrameDetailsFirstDynamicIndex = 9; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10544 | |
| 10545 | // Return an array with frame details |
| 10546 | // args[0]: number: break id |
| 10547 | // args[1]: number: frame index |
| 10548 | // |
| 10549 | // The array returned contains the following information: |
| 10550 | // 0: Frame id |
| 10551 | // 1: Receiver |
| 10552 | // 2: Function |
| 10553 | // 3: Argument count |
| 10554 | // 4: Local count |
| 10555 | // 5: Source position |
| 10556 | // 6: Constructor call |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10557 | // 7: Is at return |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10558 | // 8: Flags |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10559 | // Arguments name, value |
| 10560 | // Locals name, value |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10561 | // Return value if any |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10562 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10563 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10564 | ASSERT(args.length() == 2); |
| 10565 | |
| 10566 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10567 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10568 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10569 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10570 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10571 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10572 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10573 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10574 | |
| 10575 | // Find the relevant frame with the requested index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10576 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10577 | if (id == StackFrame::NO_ID) { |
| 10578 | // If there are no JavaScript stack frames return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10579 | return heap->undefined_value(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10580 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10581 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10582 | int inlined_frame_index = 0; // Inlined frame index in optimized frame. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10583 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10584 | int count = 0; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10585 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10586 | for (; !it.done(); it.Advance()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10587 | if (index < count + it.frame()->GetInlineCount()) break; |
| 10588 | count += it.frame()->GetInlineCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10589 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10590 | if (it.done()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10591 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10592 | if (it.frame()->is_optimized()) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10593 | inlined_frame_index = |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10594 | it.frame()->GetInlineCount() - (index - count) - 1; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10595 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10596 | FrameInspector frame_inspector(it.frame(), inlined_frame_index, isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10597 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10598 | // Traverse the saved contexts chain to find the active context for the |
| 10599 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10600 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10601 | while (save != NULL && !save->below(it.frame())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10602 | save = save->prev(); |
| 10603 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10604 | ASSERT(save != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10605 | |
| 10606 | // Get the frame id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10607 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10608 | |
| 10609 | // Find source position. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10610 | int position = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10611 | it.frame()->LookupCode()->SourcePosition(it.frame()->pc()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10612 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10613 | // Check for constructor frame. Inlined frames cannot be construct calls. |
| 10614 | bool inlined_frame = |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10615 | it.frame()->is_optimized() && inlined_frame_index != 0; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10616 | bool constructor = !inlined_frame && it.frame()->IsConstructor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10617 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10618 | // Get scope info and read from it for local variable information. |
| 10619 | Handle<JSFunction> function(JSFunction::cast(it.frame()->function())); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 10620 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 10621 | Handle<SerializedScopeInfo> scope_info(shared->scope_info()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10622 | ASSERT(*scope_info != SerializedScopeInfo::Empty()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10623 | ScopeInfo<> info(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10624 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10625 | // Get the locals names and values into a temporary array. |
| 10626 | // |
| 10627 | // TODO(1240907): Hide compiler-introduced stack variables |
| 10628 | // (e.g. .result)? For users of the debugger, they will probably be |
| 10629 | // confusing. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10630 | Handle<FixedArray> locals = |
| 10631 | isolate->factory()->NewFixedArray(info.NumberOfLocals() * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10632 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10633 | // Fill in the values of the locals. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10634 | int i = 0; |
| 10635 | for (; i < info.number_of_stack_slots(); ++i) { |
| 10636 | // Use the value from the stack. |
| 10637 | locals->set(i * 2, *info.LocalName(i)); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10638 | locals->set(i * 2 + 1, frame_inspector.GetExpression(i)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10639 | } |
| 10640 | if (i < info.NumberOfLocals()) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 10641 | // Get the context containing declarations. |
| 10642 | Handle<Context> context( |
| 10643 | Context::cast(it.frame()->context())->declaration_context()); |
| 10644 | for (; i < info.NumberOfLocals(); ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10645 | Handle<String> name = info.LocalName(i); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 10646 | locals->set(i * 2, *name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10647 | locals->set(i * 2 + 1, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10648 | context->get(scope_info->ContextSlotIndex(*name, NULL))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10649 | } |
| 10650 | } |
| 10651 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10652 | // Check whether this frame is positioned at return. If not top |
| 10653 | // frame or if the frame is optimized it cannot be at a return. |
| 10654 | bool at_return = false; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10655 | if (!it.frame()->is_optimized() && index == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10656 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10657 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10658 | |
| 10659 | // If positioned just before return find the value to be returned and add it |
| 10660 | // to the frame information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10661 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10662 | if (at_return) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10663 | StackFrameIterator it2(isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10664 | Address internal_frame_sp = NULL; |
| 10665 | while (!it2.done()) { |
| 10666 | if (it2.frame()->is_internal()) { |
| 10667 | internal_frame_sp = it2.frame()->sp(); |
| 10668 | } else { |
| 10669 | if (it2.frame()->is_java_script()) { |
| 10670 | if (it2.frame()->id() == it.frame()->id()) { |
| 10671 | // The internal frame just before the JavaScript frame contains the |
| 10672 | // value to return on top. A debug break at return will create an |
| 10673 | // internal frame to store the return value (eax/rax/r0) before |
| 10674 | // entering the debug break exit frame. |
| 10675 | if (internal_frame_sp != NULL) { |
| 10676 | return_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10677 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 10678 | isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10679 | break; |
| 10680 | } |
| 10681 | } |
| 10682 | } |
| 10683 | |
| 10684 | // Indicate that the previous frame was not an internal frame. |
| 10685 | internal_frame_sp = NULL; |
| 10686 | } |
| 10687 | it2.Advance(); |
| 10688 | } |
| 10689 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10690 | |
| 10691 | // Now advance to the arguments adapter frame (if any). It contains all |
| 10692 | // the provided parameters whereas the function frame always have the number |
| 10693 | // of arguments matching the functions parameters. The rest of the |
| 10694 | // information (except for what is collected above) is the same. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10695 | if (it.frame()->has_adapted_arguments()) { |
| 10696 | it.AdvanceToArgumentsFrame(); |
| 10697 | frame_inspector.SetArgumentsFrame(it.frame()); |
| 10698 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10699 | |
| 10700 | // Find the number of arguments to fill. At least fill the number of |
| 10701 | // parameters for the function and fill more if more parameters are provided. |
| 10702 | int argument_count = info.number_of_parameters(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10703 | if (argument_count < frame_inspector.GetParametersCount()) { |
| 10704 | argument_count = frame_inspector.GetParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10705 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10706 | #ifdef DEBUG |
| 10707 | if (it.frame()->is_optimized()) { |
| 10708 | ASSERT_EQ(argument_count, frame_inspector.GetParametersCount()); |
| 10709 | } |
| 10710 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10711 | |
| 10712 | // Calculate the size of the result. |
| 10713 | int details_size = kFrameDetailsFirstDynamicIndex + |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10714 | 2 * (argument_count + info.NumberOfLocals()) + |
| 10715 | (at_return ? 1 : 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10716 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10717 | |
| 10718 | // Add the frame id. |
| 10719 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 10720 | |
| 10721 | // Add the function (same as in function frame). |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10722 | details->set(kFrameDetailsFunctionIndex, frame_inspector.GetFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10723 | |
| 10724 | // Add the arguments count. |
| 10725 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 10726 | |
| 10727 | // Add the locals count |
| 10728 | details->set(kFrameDetailsLocalCountIndex, |
| 10729 | Smi::FromInt(info.NumberOfLocals())); |
| 10730 | |
| 10731 | // Add the source position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10732 | if (position != RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10733 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 10734 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10735 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10736 | } |
| 10737 | |
| 10738 | // Add the constructor information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10739 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10740 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10741 | // Add the at return information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10742 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10743 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10744 | // Add flags to indicate information on whether this frame is |
| 10745 | // bit 0: invoked in the debugger context. |
| 10746 | // bit 1: optimized frame. |
| 10747 | // bit 2: inlined in optimized frame |
| 10748 | int flags = 0; |
| 10749 | if (*save->context() == *isolate->debug()->debug_context()) { |
| 10750 | flags |= 1 << 0; |
| 10751 | } |
| 10752 | if (it.frame()->is_optimized()) { |
| 10753 | flags |= 1 << 1; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10754 | flags |= inlined_frame_index << 2; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10755 | } |
| 10756 | details->set(kFrameDetailsFlagsIndex, Smi::FromInt(flags)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10757 | |
| 10758 | // Fill the dynamic part. |
| 10759 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 10760 | |
| 10761 | // Add arguments name and value. |
| 10762 | for (int i = 0; i < argument_count; i++) { |
| 10763 | // Name of the argument. |
| 10764 | if (i < info.number_of_parameters()) { |
| 10765 | details->set(details_index++, *info.parameter_name(i)); |
| 10766 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10767 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10768 | } |
| 10769 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10770 | // Parameter value. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10771 | if (i < it.frame()->ComputeParametersCount()) { |
| 10772 | // Get the value from the stack. |
| 10773 | details->set(details_index++, frame_inspector.GetParameter(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10774 | } else { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10775 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10776 | } |
| 10777 | } |
| 10778 | |
| 10779 | // Add locals name and value from the temporary copy from the function frame. |
| 10780 | for (int i = 0; i < info.NumberOfLocals() * 2; i++) { |
| 10781 | details->set(details_index++, locals->get(i)); |
| 10782 | } |
| 10783 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10784 | // Add the value being returned. |
| 10785 | if (at_return) { |
| 10786 | details->set(details_index++, *return_value); |
| 10787 | } |
| 10788 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10789 | // Add the receiver (same as in function frame). |
| 10790 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 10791 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10792 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 10793 | if (!receiver->IsJSObject() && !shared->strict_mode() && !shared->native()) { |
| 10794 | // If the receiver is not a JSObject and the function is not a |
| 10795 | // builtin or strict-mode we have hit an optimization where a |
| 10796 | // value object is not converted into a wrapped JS objects. To |
| 10797 | // hide this optimization from the debugger, we wrap the receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10798 | // by creating correct wrapper object based on the calling frame's |
| 10799 | // global context. |
| 10800 | it.Advance(); |
| 10801 | Handle<Context> calling_frames_global_context( |
| 10802 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10803 | receiver = |
| 10804 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10805 | } |
| 10806 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 10807 | |
| 10808 | ASSERT_EQ(details_size, details_index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10809 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10810 | } |
| 10811 | |
| 10812 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10813 | // 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] | 10814 | static bool CopyContextLocalsToScopeObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10815 | Isolate* isolate, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10816 | Handle<SerializedScopeInfo> serialized_scope_info, |
| 10817 | ScopeInfo<>& scope_info, |
| 10818 | Handle<Context> context, |
| 10819 | Handle<JSObject> scope_object) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10820 | // Fill all context locals to the context extension. |
| 10821 | for (int i = Context::MIN_CONTEXT_SLOTS; |
| 10822 | i < scope_info.number_of_context_slots(); |
| 10823 | i++) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10824 | int context_index = serialized_scope_info->ContextSlotIndex( |
| 10825 | *scope_info.context_slot_name(i), NULL); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10826 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10827 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10828 | isolate, |
| 10829 | SetProperty(scope_object, |
| 10830 | scope_info.context_slot_name(i), |
| 10831 | Handle<Object>(context->get(context_index), isolate), |
| 10832 | NONE, |
| 10833 | kNonStrictMode), |
| 10834 | false); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10835 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10836 | |
| 10837 | return true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10838 | } |
| 10839 | |
| 10840 | |
| 10841 | // Create a plain JSObject which materializes the local scope for the specified |
| 10842 | // frame. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10843 | static Handle<JSObject> MaterializeLocalScope( |
| 10844 | Isolate* isolate, |
| 10845 | JavaScriptFrame* frame, |
| 10846 | int inlined_frame_index) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10847 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10848 | Handle<SharedFunctionInfo> shared(function->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10849 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 10850 | ScopeInfo<> scope_info(*serialized_scope_info); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10851 | FrameInspector frame_inspector(frame, inlined_frame_index, isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10852 | |
| 10853 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 10854 | // heap locals and extension properties of the debugged function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10855 | Handle<JSObject> local_scope = |
| 10856 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10857 | |
| 10858 | // First fill all parameters. |
| 10859 | for (int i = 0; i < scope_info.number_of_parameters(); ++i) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10860 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10861 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10862 | SetProperty(local_scope, |
| 10863 | scope_info.parameter_name(i), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10864 | Handle<Object>(frame_inspector.GetParameter(i)), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10865 | NONE, |
| 10866 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10867 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10868 | } |
| 10869 | |
| 10870 | // Second fill all stack locals. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10871 | 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] | 10872 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10873 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10874 | SetProperty(local_scope, |
| 10875 | scope_info.stack_slot_name(i), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10876 | Handle<Object>(frame_inspector.GetExpression(i)), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10877 | NONE, |
| 10878 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10879 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10880 | } |
| 10881 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10882 | if (scope_info.number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
| 10883 | // Third fill all context locals. |
| 10884 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 10885 | Handle<Context> function_context(frame_context->declaration_context()); |
| 10886 | if (!CopyContextLocalsToScopeObject(isolate, |
| 10887 | serialized_scope_info, scope_info, |
| 10888 | function_context, local_scope)) { |
| 10889 | return Handle<JSObject>(); |
| 10890 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10891 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10892 | // Finally copy any properties from the function context extension. |
| 10893 | // These will be variables introduced by eval. |
| 10894 | if (function_context->closure() == *function) { |
| 10895 | if (function_context->has_extension() && |
| 10896 | !function_context->IsGlobalContext()) { |
| 10897 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
| 10898 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
| 10899 | for (int i = 0; i < keys->length(); i++) { |
| 10900 | // Names of variables introduced by eval are strings. |
| 10901 | ASSERT(keys->get(i)->IsString()); |
| 10902 | Handle<String> key(String::cast(keys->get(i))); |
| 10903 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10904 | isolate, |
| 10905 | SetProperty(local_scope, |
| 10906 | key, |
| 10907 | GetProperty(ext, key), |
| 10908 | NONE, |
| 10909 | kNonStrictMode), |
| 10910 | Handle<JSObject>()); |
| 10911 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10912 | } |
| 10913 | } |
| 10914 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10915 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10916 | return local_scope; |
| 10917 | } |
| 10918 | |
| 10919 | |
| 10920 | // Create a plain JSObject which materializes the closure content for the |
| 10921 | // context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10922 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 10923 | Handle<Context> context) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10924 | ASSERT(context->IsFunctionContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10925 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10926 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10927 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 10928 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10929 | |
| 10930 | // Allocate and initialize a JSObject with all the content of theis function |
| 10931 | // closure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10932 | Handle<JSObject> closure_scope = |
| 10933 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10934 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10935 | // Fill all context locals to the context extension. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10936 | if (!CopyContextLocalsToScopeObject(isolate, |
| 10937 | serialized_scope_info, scope_info, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10938 | context, closure_scope)) { |
| 10939 | return Handle<JSObject>(); |
| 10940 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10941 | |
| 10942 | // Finally copy any properties from the function context extension. This will |
| 10943 | // be variables introduced by eval. |
| 10944 | if (context->has_extension()) { |
| 10945 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10946 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10947 | for (int i = 0; i < keys->length(); i++) { |
| 10948 | // Names of variables introduced by eval are strings. |
| 10949 | ASSERT(keys->get(i)->IsString()); |
| 10950 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10951 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10952 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10953 | SetProperty(closure_scope, |
| 10954 | key, |
| 10955 | GetProperty(ext, key), |
| 10956 | NONE, |
| 10957 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10958 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10959 | } |
| 10960 | } |
| 10961 | |
| 10962 | return closure_scope; |
| 10963 | } |
| 10964 | |
| 10965 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10966 | // Create a plain JSObject which materializes the scope for the specified |
| 10967 | // catch context. |
| 10968 | static Handle<JSObject> MaterializeCatchScope(Isolate* isolate, |
| 10969 | Handle<Context> context) { |
| 10970 | ASSERT(context->IsCatchContext()); |
| 10971 | Handle<String> name(String::cast(context->extension())); |
| 10972 | Handle<Object> thrown_object(context->get(Context::THROWN_OBJECT_INDEX)); |
| 10973 | Handle<JSObject> catch_scope = |
| 10974 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 10975 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10976 | isolate, |
| 10977 | SetProperty(catch_scope, name, thrown_object, NONE, kNonStrictMode), |
| 10978 | Handle<JSObject>()); |
| 10979 | return catch_scope; |
| 10980 | } |
| 10981 | |
| 10982 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 10983 | // Create a plain JSObject which materializes the block scope for the specified |
| 10984 | // block context. |
| 10985 | static Handle<JSObject> MaterializeBlockScope( |
| 10986 | Isolate* isolate, |
| 10987 | Handle<Context> context) { |
| 10988 | ASSERT(context->IsBlockContext()); |
| 10989 | Handle<SerializedScopeInfo> serialized_scope_info( |
| 10990 | SerializedScopeInfo::cast(context->extension())); |
| 10991 | ScopeInfo<> scope_info(*serialized_scope_info); |
| 10992 | |
| 10993 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 10994 | // heap locals and extension properties of the debugged function. |
| 10995 | Handle<JSObject> block_scope = |
| 10996 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 10997 | |
| 10998 | // Fill all context locals. |
| 10999 | if (scope_info.number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
| 11000 | if (!CopyContextLocalsToScopeObject(isolate, |
| 11001 | serialized_scope_info, scope_info, |
| 11002 | context, block_scope)) { |
| 11003 | return Handle<JSObject>(); |
| 11004 | } |
| 11005 | } |
| 11006 | |
| 11007 | return block_scope; |
| 11008 | } |
| 11009 | |
| 11010 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11011 | // Iterate over the actual scopes visible from a stack frame. All scopes are |
| 11012 | // backed by an actual context except the local scope, which is inserted |
| 11013 | // "artifically" in the context chain. |
| 11014 | class ScopeIterator { |
| 11015 | public: |
| 11016 | enum ScopeType { |
| 11017 | ScopeTypeGlobal = 0, |
| 11018 | ScopeTypeLocal, |
| 11019 | ScopeTypeWith, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11020 | ScopeTypeClosure, |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11021 | ScopeTypeCatch, |
| 11022 | ScopeTypeBlock |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11023 | }; |
| 11024 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11025 | ScopeIterator(Isolate* isolate, |
| 11026 | JavaScriptFrame* frame, |
| 11027 | int inlined_frame_index) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11028 | : isolate_(isolate), |
| 11029 | frame_(frame), |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11030 | inlined_frame_index_(inlined_frame_index), |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11031 | function_(JSFunction::cast(frame->function())), |
| 11032 | context_(Context::cast(frame->context())), |
| 11033 | local_done_(false), |
| 11034 | at_local_(false) { |
| 11035 | |
| 11036 | // Check whether the first scope is actually a local scope. |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 11037 | // If there is a stack slot for .result then this local scope has been |
| 11038 | // created for evaluating top level code and it is not a real local scope. |
| 11039 | // Checking for the existence of .result seems fragile, but the scope info |
| 11040 | // saved with the code object does not otherwise have that information. |
| 11041 | int index = function_->shared()->scope_info()-> |
| 11042 | StackSlotIndex(isolate_->heap()->result_symbol()); |
| 11043 | if (index >= 0) { |
| 11044 | local_done_ = true; |
| 11045 | } else if (context_->IsGlobalContext() || |
| 11046 | context_->IsFunctionContext()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11047 | at_local_ = true; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 11048 | } else if (context_->closure() != *function_) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11049 | // The context_ is a block or with or catch block from the outer function. |
| 11050 | ASSERT(context_->IsWithContext() || |
| 11051 | context_->IsCatchContext() || |
| 11052 | context_->IsBlockContext()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 11053 | at_local_ = true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11054 | } |
| 11055 | } |
| 11056 | |
| 11057 | // More scopes? |
| 11058 | bool Done() { return context_.is_null(); } |
| 11059 | |
| 11060 | // Move to the next scope. |
| 11061 | void Next() { |
| 11062 | // If at a local scope mark the local scope as passed. |
| 11063 | if (at_local_) { |
| 11064 | at_local_ = false; |
| 11065 | local_done_ = true; |
| 11066 | |
| 11067 | // If the current context is not associated with the local scope the |
| 11068 | // current context is the next real scope, so don't move to the next |
| 11069 | // context in this case. |
| 11070 | if (context_->closure() != *function_) { |
| 11071 | return; |
| 11072 | } |
| 11073 | } |
| 11074 | |
| 11075 | // The global scope is always the last in the chain. |
| 11076 | if (context_->IsGlobalContext()) { |
| 11077 | context_ = Handle<Context>(); |
| 11078 | return; |
| 11079 | } |
| 11080 | |
| 11081 | // Move to the next context. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11082 | context_ = Handle<Context>(context_->previous(), isolate_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11083 | |
| 11084 | // If passing the local scope indicate that the current scope is now the |
| 11085 | // local scope. |
| 11086 | if (!local_done_ && |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11087 | (context_->IsGlobalContext() || context_->IsFunctionContext())) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11088 | at_local_ = true; |
| 11089 | } |
| 11090 | } |
| 11091 | |
| 11092 | // Return the type of the current scope. |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame^] | 11093 | ScopeType Type() { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11094 | if (at_local_) { |
| 11095 | return ScopeTypeLocal; |
| 11096 | } |
| 11097 | if (context_->IsGlobalContext()) { |
| 11098 | ASSERT(context_->global()->IsGlobalObject()); |
| 11099 | return ScopeTypeGlobal; |
| 11100 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11101 | if (context_->IsFunctionContext()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11102 | return ScopeTypeClosure; |
| 11103 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11104 | if (context_->IsCatchContext()) { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11105 | return ScopeTypeCatch; |
| 11106 | } |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11107 | if (context_->IsBlockContext()) { |
| 11108 | return ScopeTypeBlock; |
| 11109 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11110 | ASSERT(context_->IsWithContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11111 | return ScopeTypeWith; |
| 11112 | } |
| 11113 | |
| 11114 | // Return the JavaScript object with the content of the current scope. |
| 11115 | Handle<JSObject> ScopeObject() { |
| 11116 | switch (Type()) { |
| 11117 | case ScopeIterator::ScopeTypeGlobal: |
| 11118 | return Handle<JSObject>(CurrentContext()->global()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11119 | case ScopeIterator::ScopeTypeLocal: |
| 11120 | // Materialize the content of the local scope into a JSObject. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11121 | return MaterializeLocalScope(isolate_, frame_, inlined_frame_index_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11122 | case ScopeIterator::ScopeTypeWith: |
| 11123 | // Return the with object. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11124 | return Handle<JSObject>(JSObject::cast(CurrentContext()->extension())); |
| 11125 | case ScopeIterator::ScopeTypeCatch: |
| 11126 | return MaterializeCatchScope(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11127 | case ScopeIterator::ScopeTypeClosure: |
| 11128 | // Materialize the content of the closure scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11129 | return MaterializeClosure(isolate_, CurrentContext()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11130 | case ScopeIterator::ScopeTypeBlock: |
| 11131 | return MaterializeBlockScope(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11132 | } |
| 11133 | UNREACHABLE(); |
| 11134 | return Handle<JSObject>(); |
| 11135 | } |
| 11136 | |
| 11137 | // Return the context for this scope. For the local context there might not |
| 11138 | // be an actual context. |
| 11139 | Handle<Context> CurrentContext() { |
| 11140 | if (at_local_ && context_->closure() != *function_) { |
| 11141 | return Handle<Context>(); |
| 11142 | } |
| 11143 | return context_; |
| 11144 | } |
| 11145 | |
| 11146 | #ifdef DEBUG |
| 11147 | // Debug print of the content of the current scope. |
| 11148 | void DebugPrint() { |
| 11149 | switch (Type()) { |
| 11150 | case ScopeIterator::ScopeTypeGlobal: |
| 11151 | PrintF("Global:\n"); |
| 11152 | CurrentContext()->Print(); |
| 11153 | break; |
| 11154 | |
| 11155 | case ScopeIterator::ScopeTypeLocal: { |
| 11156 | PrintF("Local:\n"); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11157 | ScopeInfo<> scope_info(function_->shared()->scope_info()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11158 | scope_info.Print(); |
| 11159 | if (!CurrentContext().is_null()) { |
| 11160 | CurrentContext()->Print(); |
| 11161 | if (CurrentContext()->has_extension()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11162 | Handle<Object> extension(CurrentContext()->extension()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11163 | if (extension->IsJSContextExtensionObject()) { |
| 11164 | extension->Print(); |
| 11165 | } |
| 11166 | } |
| 11167 | } |
| 11168 | break; |
| 11169 | } |
| 11170 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11171 | case ScopeIterator::ScopeTypeWith: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11172 | PrintF("With:\n"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11173 | CurrentContext()->extension()->Print(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11174 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11175 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11176 | case ScopeIterator::ScopeTypeCatch: |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11177 | PrintF("Catch:\n"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11178 | CurrentContext()->extension()->Print(); |
| 11179 | CurrentContext()->get(Context::THROWN_OBJECT_INDEX)->Print(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11180 | break; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11181 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11182 | case ScopeIterator::ScopeTypeClosure: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11183 | PrintF("Closure:\n"); |
| 11184 | CurrentContext()->Print(); |
| 11185 | if (CurrentContext()->has_extension()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11186 | Handle<Object> extension(CurrentContext()->extension()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11187 | if (extension->IsJSContextExtensionObject()) { |
| 11188 | extension->Print(); |
| 11189 | } |
| 11190 | } |
| 11191 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11192 | |
| 11193 | default: |
| 11194 | UNREACHABLE(); |
| 11195 | } |
| 11196 | PrintF("\n"); |
| 11197 | } |
| 11198 | #endif |
| 11199 | |
| 11200 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11201 | Isolate* isolate_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11202 | JavaScriptFrame* frame_; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11203 | int inlined_frame_index_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11204 | Handle<JSFunction> function_; |
| 11205 | Handle<Context> context_; |
| 11206 | bool local_done_; |
| 11207 | bool at_local_; |
| 11208 | |
| 11209 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 11210 | }; |
| 11211 | |
| 11212 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11213 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11214 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11215 | ASSERT(args.length() == 2); |
| 11216 | |
| 11217 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11218 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11219 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11220 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11221 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11222 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11223 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 11224 | |
| 11225 | // Get the frame where the debugging is performed. |
| 11226 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11227 | JavaScriptFrameIterator it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11228 | JavaScriptFrame* frame = it.frame(); |
| 11229 | |
| 11230 | // Count the visible scopes. |
| 11231 | int n = 0; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11232 | for (ScopeIterator it(isolate, frame, 0); |
| 11233 | !it.Done(); |
| 11234 | it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11235 | n++; |
| 11236 | } |
| 11237 | |
| 11238 | return Smi::FromInt(n); |
| 11239 | } |
| 11240 | |
| 11241 | |
| 11242 | static const int kScopeDetailsTypeIndex = 0; |
| 11243 | static const int kScopeDetailsObjectIndex = 1; |
| 11244 | static const int kScopeDetailsSize = 2; |
| 11245 | |
| 11246 | // Return an array with scope details |
| 11247 | // args[0]: number: break id |
| 11248 | // args[1]: number: frame index |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11249 | // args[2]: number: inlined frame index |
| 11250 | // args[3]: number: scope index |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11251 | // |
| 11252 | // The array returned contains the following information: |
| 11253 | // 0: Scope type |
| 11254 | // 1: Scope object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11255 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11256 | HandleScope scope(isolate); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11257 | ASSERT(args.length() == 4); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11258 | |
| 11259 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11260 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11261 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11262 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11263 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11264 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11265 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11266 | CONVERT_NUMBER_CHECKED(int, inlined_frame_index, Int32, args[2]); |
| 11267 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[3]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11268 | |
| 11269 | // Get the frame where the debugging is performed. |
| 11270 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11271 | JavaScriptFrameIterator frame_it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11272 | JavaScriptFrame* frame = frame_it.frame(); |
| 11273 | |
| 11274 | // Find the requested scope. |
| 11275 | int n = 0; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11276 | ScopeIterator it(isolate, frame, inlined_frame_index); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11277 | for (; !it.Done() && n < index; it.Next()) { |
| 11278 | n++; |
| 11279 | } |
| 11280 | if (it.Done()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11281 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11282 | } |
| 11283 | |
| 11284 | // Calculate the size of the result. |
| 11285 | int details_size = kScopeDetailsSize; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11286 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11287 | |
| 11288 | // Fill in scope details. |
| 11289 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 11290 | Handle<JSObject> scope_object = it.ScopeObject(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11291 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 11292 | details->set(kScopeDetailsObjectIndex, *scope_object); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11293 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11294 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11295 | } |
| 11296 | |
| 11297 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11298 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrintScopes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11299 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11300 | ASSERT(args.length() == 0); |
| 11301 | |
| 11302 | #ifdef DEBUG |
| 11303 | // Print the scopes for the top frame. |
| 11304 | StackFrameLocator locator; |
| 11305 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11306 | for (ScopeIterator it(isolate, frame, 0); |
| 11307 | !it.Done(); |
| 11308 | it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11309 | it.DebugPrint(); |
| 11310 | } |
| 11311 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11312 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11313 | } |
| 11314 | |
| 11315 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11316 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11317 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11318 | ASSERT(args.length() == 1); |
| 11319 | |
| 11320 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11321 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11322 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 11323 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11324 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11325 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11326 | |
| 11327 | // Count all archived V8 threads. |
| 11328 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11329 | for (ThreadState* thread = |
| 11330 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11331 | thread != NULL; |
| 11332 | thread = thread->Next()) { |
| 11333 | n++; |
| 11334 | } |
| 11335 | |
| 11336 | // Total number of threads is current thread and archived threads. |
| 11337 | return Smi::FromInt(n + 1); |
| 11338 | } |
| 11339 | |
| 11340 | |
| 11341 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 11342 | static const int kThreadDetailsThreadIdIndex = 1; |
| 11343 | static const int kThreadDetailsSize = 2; |
| 11344 | |
| 11345 | // Return an array with thread details |
| 11346 | // args[0]: number: break id |
| 11347 | // args[1]: number: thread index |
| 11348 | // |
| 11349 | // The array returned contains the following information: |
| 11350 | // 0: Is current thread? |
| 11351 | // 1: Thread id |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11352 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11353 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11354 | ASSERT(args.length() == 2); |
| 11355 | |
| 11356 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11357 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11358 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11359 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11360 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11361 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11362 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 11363 | |
| 11364 | // Allocate array for result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11365 | Handle<FixedArray> details = |
| 11366 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11367 | |
| 11368 | // Thread index 0 is current thread. |
| 11369 | if (index == 0) { |
| 11370 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11371 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11372 | isolate->heap()->true_value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11373 | details->set(kThreadDetailsThreadIdIndex, |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 11374 | Smi::FromInt(ThreadId::Current().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11375 | } else { |
| 11376 | // Find the thread with the requested index. |
| 11377 | int n = 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11378 | ThreadState* thread = |
| 11379 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11380 | while (index != n && thread != NULL) { |
| 11381 | thread = thread->Next(); |
| 11382 | n++; |
| 11383 | } |
| 11384 | if (thread == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11385 | return isolate->heap()->undefined_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11386 | } |
| 11387 | |
| 11388 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11389 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11390 | isolate->heap()->false_value()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 11391 | details->set(kThreadDetailsThreadIdIndex, |
| 11392 | Smi::FromInt(thread->id().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11393 | } |
| 11394 | |
| 11395 | // Convert to JS array and return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11396 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11397 | } |
| 11398 | |
| 11399 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 11400 | // Sets the disable break state |
| 11401 | // args[0]: disable break state |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11402 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDisableBreak) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11403 | HandleScope scope(isolate); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 11404 | ASSERT(args.length() == 1); |
| 11405 | CONVERT_BOOLEAN_CHECKED(disable_break, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11406 | isolate->debug()->set_disable_break(disable_break); |
| 11407 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 11408 | } |
| 11409 | |
| 11410 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11411 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetBreakLocations) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11412 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11413 | ASSERT(args.length() == 1); |
| 11414 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 11415 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 11416 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11417 | // Find the number of break points |
| 11418 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11419 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11420 | // Return array as JS array |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11421 | return *isolate->factory()->NewJSArrayWithElements( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11422 | Handle<FixedArray>::cast(break_locations)); |
| 11423 | } |
| 11424 | |
| 11425 | |
| 11426 | // Set a break point in a function |
| 11427 | // args[0]: function |
| 11428 | // args[1]: number: break source position (within the function source) |
| 11429 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11430 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFunctionBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11431 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11432 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 11433 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 11434 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11435 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11436 | RUNTIME_ASSERT(source_position >= 0); |
| 11437 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11438 | |
| 11439 | // Set break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11440 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 11441 | &source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11442 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 11443 | return Smi::FromInt(source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11444 | } |
| 11445 | |
| 11446 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11447 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 11448 | Handle<Script> script, |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 11449 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11450 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 11451 | // script. The inner most SharedFunctionInfo containing the source position |
| 11452 | // for the requested break point is found. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11453 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11454 | // which is found is not compiled it is compiled and the heap is iterated |
| 11455 | // again as the compilation might create inner functions from the newly |
| 11456 | // compiled function and the actual requested break point might be in one of |
| 11457 | // these functions. |
| 11458 | bool done = false; |
| 11459 | // The current candidate for the source position: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 11460 | int target_start_position = RelocInfo::kNoPosition; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11461 | Handle<SharedFunctionInfo> target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11462 | while (!done) { |
| 11463 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11464 | for (HeapObject* obj = iterator.next(); |
| 11465 | obj != NULL; obj = iterator.next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11466 | if (obj->IsSharedFunctionInfo()) { |
| 11467 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 11468 | if (shared->script() == *script) { |
| 11469 | // If the SharedFunctionInfo found has the requested script data and |
| 11470 | // contains the source position it is a candidate. |
| 11471 | int start_position = shared->function_token_position(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 11472 | if (start_position == RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11473 | start_position = shared->start_position(); |
| 11474 | } |
| 11475 | if (start_position <= position && |
| 11476 | position <= shared->end_position()) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 11477 | // 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] | 11478 | // candidate this is the new candidate. |
| 11479 | if (target.is_null()) { |
| 11480 | target_start_position = start_position; |
| 11481 | target = shared; |
| 11482 | } else { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11483 | if (target_start_position == start_position && |
| 11484 | shared->end_position() == target->end_position()) { |
| 11485 | // If a top-level function contain only one function |
| 11486 | // declartion the source for the top-level and the function is |
| 11487 | // the same. In that case prefer the non top-level function. |
| 11488 | if (!shared->is_toplevel()) { |
| 11489 | target_start_position = start_position; |
| 11490 | target = shared; |
| 11491 | } |
| 11492 | } else if (target_start_position <= start_position && |
| 11493 | shared->end_position() <= target->end_position()) { |
| 11494 | // This containment check includes equality as a function inside |
| 11495 | // a top-level function can share either start or end position |
| 11496 | // with the top-level function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11497 | target_start_position = start_position; |
| 11498 | target = shared; |
| 11499 | } |
| 11500 | } |
| 11501 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11502 | } |
| 11503 | } |
| 11504 | } |
| 11505 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11506 | if (target.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11507 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11508 | } |
| 11509 | |
| 11510 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 11511 | // compilation of inner functions is introduced some additional checking |
| 11512 | // needs to be done here to compile inner functions. |
| 11513 | done = target->is_compiled(); |
| 11514 | if (!done) { |
| 11515 | // If the candidate is not compiled compile it to reveal any inner |
| 11516 | // functions which might contain the requested source position. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11517 | CompileLazyShared(target, KEEP_EXCEPTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11518 | } |
| 11519 | } |
| 11520 | |
| 11521 | return *target; |
| 11522 | } |
| 11523 | |
| 11524 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11525 | // Changes the state of a break point in a script and returns source position |
| 11526 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 11527 | // GetScriptFromScriptData. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11528 | // args[0]: script to set break point in |
| 11529 | // args[1]: number: break source position (within the script source) |
| 11530 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11531 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetScriptBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11532 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11533 | ASSERT(args.length() == 3); |
| 11534 | CONVERT_ARG_CHECKED(JSValue, wrapper, 0); |
| 11535 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11536 | RUNTIME_ASSERT(source_position >= 0); |
| 11537 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11538 | |
| 11539 | // Get the script from the script wrapper. |
| 11540 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 11541 | Handle<Script> script(Script::cast(wrapper->value())); |
| 11542 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 11543 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11544 | isolate, script, source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11545 | if (!result->IsUndefined()) { |
| 11546 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 11547 | // Find position within function. The script position might be before the |
| 11548 | // source position of the first function. |
| 11549 | int position; |
| 11550 | if (shared->start_position() > source_position) { |
| 11551 | position = 0; |
| 11552 | } else { |
| 11553 | position = source_position - shared->start_position(); |
| 11554 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11555 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11556 | position += shared->start_position(); |
| 11557 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11558 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11559 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11560 | } |
| 11561 | |
| 11562 | |
| 11563 | // Clear a break point |
| 11564 | // args[0]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11565 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11566 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11567 | ASSERT(args.length() == 1); |
| 11568 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 11569 | |
| 11570 | // Clear break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11571 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11572 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11573 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11574 | } |
| 11575 | |
| 11576 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11577 | // Change the state of break on exceptions. |
| 11578 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 11579 | // args[1]: Boolean indicating on/off. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11580 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ChangeBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11581 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11582 | ASSERT(args.length() == 2); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11583 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11584 | CONVERT_BOOLEAN_CHECKED(enable, args[1]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11585 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11586 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 11587 | // function will default to affecting caught exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11588 | ExceptionBreakType type = |
| 11589 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11590 | // Update break point state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11591 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 11592 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11593 | } |
| 11594 | |
| 11595 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11596 | // Returns the state of break on exceptions |
| 11597 | // args[0]: boolean indicating uncaught exceptions |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11598 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11599 | HandleScope scope(isolate); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11600 | ASSERT(args.length() == 1); |
| 11601 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11602 | |
| 11603 | ExceptionBreakType type = |
| 11604 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11605 | bool result = isolate->debug()->IsBreakOnException(type); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11606 | return Smi::FromInt(result); |
| 11607 | } |
| 11608 | |
| 11609 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11610 | // Prepare for stepping |
| 11611 | // args[0]: break id for checking execution state |
| 11612 | // args[1]: step action from the enumeration StepAction |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11613 | // args[2]: number of times to perform the step, for step out it is the number |
| 11614 | // of frames to step down. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11615 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrepareStep) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11616 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11617 | ASSERT(args.length() == 3); |
| 11618 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11619 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11620 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11621 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11622 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11623 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11624 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11625 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11626 | } |
| 11627 | |
| 11628 | // Get the step action and check validity. |
| 11629 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 11630 | if (step_action != StepIn && |
| 11631 | step_action != StepNext && |
| 11632 | step_action != StepOut && |
| 11633 | step_action != StepInMin && |
| 11634 | step_action != StepMin) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11635 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11636 | } |
| 11637 | |
| 11638 | // Get the number of steps. |
| 11639 | int step_count = NumberToInt32(args[2]); |
| 11640 | if (step_count < 1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11641 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11642 | } |
| 11643 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11644 | // Clear all current stepping setup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11645 | isolate->debug()->ClearStepping(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11646 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11647 | // Prepare step. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11648 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 11649 | step_count); |
| 11650 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11651 | } |
| 11652 | |
| 11653 | |
| 11654 | // Clear all stepping set by PrepareStep. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11655 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearStepping) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11656 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11657 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11658 | isolate->debug()->ClearStepping(); |
| 11659 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11660 | } |
| 11661 | |
| 11662 | |
| 11663 | // Creates a copy of the with context chain. The copy of the context chain is |
| 11664 | // is linked to the function context supplied. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11665 | static Handle<Context> CopyWithContextChain(Isolate* isolate, |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11666 | Handle<JSFunction> function, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11667 | Handle<Context> current, |
| 11668 | Handle<Context> base) { |
| 11669 | // At the end of the chain. Return the base context to link to. |
| 11670 | if (current->IsFunctionContext() || current->IsGlobalContext()) { |
| 11671 | return base; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11672 | } |
| 11673 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11674 | // Recursively copy the with and catch contexts. |
| 11675 | HandleScope scope(isolate); |
| 11676 | Handle<Context> previous(current->previous()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11677 | Handle<Context> new_previous = |
| 11678 | CopyWithContextChain(isolate, function, previous, base); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11679 | Handle<Context> new_current; |
| 11680 | if (current->IsCatchContext()) { |
| 11681 | Handle<String> name(String::cast(current->extension())); |
| 11682 | Handle<Object> thrown_object(current->get(Context::THROWN_OBJECT_INDEX)); |
| 11683 | new_current = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11684 | isolate->factory()->NewCatchContext(function, |
| 11685 | new_previous, |
| 11686 | name, |
| 11687 | thrown_object); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11688 | } else if (current->IsBlockContext()) { |
| 11689 | Handle<SerializedScopeInfo> scope_info( |
| 11690 | SerializedScopeInfo::cast(current->extension())); |
| 11691 | new_current = |
| 11692 | isolate->factory()->NewBlockContext(function, new_previous, scope_info); |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 11693 | // Copy context slots. |
| 11694 | int num_context_slots = scope_info->NumberOfContextSlots(); |
| 11695 | for (int i = Context::MIN_CONTEXT_SLOTS; i < num_context_slots; ++i) { |
| 11696 | new_current->set(i, current->get(i)); |
| 11697 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11698 | } else { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11699 | ASSERT(current->IsWithContext()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11700 | Handle<JSObject> extension(JSObject::cast(current->extension())); |
| 11701 | new_current = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11702 | isolate->factory()->NewWithContext(function, new_previous, extension); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11703 | } |
| 11704 | return scope.CloseAndEscape(new_current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11705 | } |
| 11706 | |
| 11707 | |
| 11708 | // Helper function to find or create the arguments object for |
| 11709 | // Runtime_DebugEvaluate. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11710 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 11711 | JavaScriptFrame* frame, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11712 | int inlined_frame_index, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11713 | Handle<JSFunction> function, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 11714 | Handle<SerializedScopeInfo> scope_info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11715 | const ScopeInfo<>* sinfo, |
| 11716 | Handle<Context> function_context) { |
| 11717 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 11718 | // found (that is the debugged function does not reference 'arguments' and |
| 11719 | // does not support eval) then create an 'arguments' object. |
| 11720 | int index; |
| 11721 | if (sinfo->number_of_stack_slots() > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11722 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11723 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11724 | return Handle<Object>(frame->GetExpression(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11725 | } |
| 11726 | } |
| 11727 | |
| 11728 | if (sinfo->number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11729 | index = scope_info->ContextSlotIndex(isolate->heap()->arguments_symbol(), |
| 11730 | NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11731 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11732 | return Handle<Object>(function_context->get(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11733 | } |
| 11734 | } |
| 11735 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11736 | FrameInspector frame_inspector(frame, inlined_frame_index, isolate); |
| 11737 | |
| 11738 | int length = frame_inspector.GetParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11739 | Handle<JSObject> arguments = |
| 11740 | isolate->factory()->NewArgumentsObject(function, length); |
| 11741 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11742 | |
| 11743 | AssertNoAllocation no_gc; |
| 11744 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11745 | for (int i = 0; i < length; i++) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11746 | array->set(i, frame_inspector.GetParameter(i), mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11747 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 11748 | arguments->set_elements(*array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11749 | return arguments; |
| 11750 | } |
| 11751 | |
| 11752 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11753 | static const char kSourceStr[] = |
| 11754 | "(function(arguments,__source__){return eval(__source__);})"; |
| 11755 | |
| 11756 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11757 | // 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] | 11758 | // 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] | 11759 | // extension part has all the parameters and locals of the function on the |
| 11760 | // stack frame. A function which calls eval with the code to evaluate is then |
| 11761 | // compiled in this context and called in this context. As this context |
| 11762 | // replaces the context of the function on the stack frame a new (empty) |
| 11763 | // function is created as well to be used as the closure for the context. |
| 11764 | // This function and the context acts as replacements for the function on the |
| 11765 | // stack frame presenting the same view of the values of parameters and |
| 11766 | // local variables as if the piece of JavaScript was evaluated at the point |
| 11767 | // where the function on the stack frame is currently stopped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11768 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11769 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11770 | |
| 11771 | // Check the execution state and decode arguments frame and source to be |
| 11772 | // evaluated. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11773 | ASSERT(args.length() == 6); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11774 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11775 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 11776 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11777 | if (!maybe_check_result->ToObject(&check_result)) { |
| 11778 | return maybe_check_result; |
| 11779 | } |
| 11780 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11781 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11782 | CONVERT_NUMBER_CHECKED(int, inlined_frame_index, Int32, args[2]); |
| 11783 | CONVERT_ARG_CHECKED(String, source, 3); |
| 11784 | CONVERT_BOOLEAN_CHECKED(disable_break, args[4]); |
| 11785 | Handle<Object> additional_context(args[5]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11786 | |
| 11787 | // Handle the processing of break. |
| 11788 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11789 | |
| 11790 | // Get the frame where the debugging is performed. |
| 11791 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11792 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11793 | JavaScriptFrame* frame = it.frame(); |
| 11794 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 11795 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11796 | ScopeInfo<> sinfo(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11797 | |
| 11798 | // Traverse the saved contexts chain to find the active context for the |
| 11799 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11800 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 11801 | while (save != NULL && !save->below(frame)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11802 | save = save->prev(); |
| 11803 | } |
| 11804 | ASSERT(save != NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11805 | SaveContext savex(isolate); |
| 11806 | isolate->set_context(*(save->context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11807 | |
| 11808 | // Create the (empty) function replacing the function on the stack frame for |
| 11809 | // the purpose of evaluating in the context created below. It is important |
| 11810 | // that this function does not describe any parameters and local variables |
| 11811 | // in the context. If it does then this will cause problems with the lookup |
| 11812 | // in Context::Lookup, where context slots for parameters and local variables |
| 11813 | // are looked at before the extension object. |
| 11814 | Handle<JSFunction> go_between = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11815 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 11816 | isolate->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11817 | go_between->set_context(function->context()); |
| 11818 | #ifdef DEBUG |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11819 | ScopeInfo<> go_between_sinfo(go_between->shared()->scope_info()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11820 | ASSERT(go_between_sinfo.number_of_parameters() == 0); |
| 11821 | ASSERT(go_between_sinfo.number_of_context_slots() == 0); |
| 11822 | #endif |
| 11823 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11824 | // Materialize the content of the local scope into a JSObject. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11825 | Handle<JSObject> local_scope = MaterializeLocalScope( |
| 11826 | isolate, frame, inlined_frame_index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11827 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11828 | |
| 11829 | // Allocate a new context for the debug evaluation and set the extension |
| 11830 | // object build. |
| 11831 | Handle<Context> context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11832 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 11833 | go_between); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11834 | context->set_extension(*local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11835 | // 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] | 11836 | Handle<Context> frame_context(Context::cast(frame->context())); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 11837 | Handle<Context> function_context; |
| 11838 | // Get the function's context if it has one. |
| 11839 | if (scope_info->HasHeapAllocatedLocals()) { |
| 11840 | function_context = Handle<Context>(frame_context->declaration_context()); |
| 11841 | } |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11842 | context = CopyWithContextChain(isolate, go_between, frame_context, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11843 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11844 | if (additional_context->IsJSObject()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11845 | Handle<JSObject> extension = Handle<JSObject>::cast(additional_context); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11846 | context = |
| 11847 | isolate->factory()->NewWithContext(go_between, context, extension); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11848 | } |
| 11849 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11850 | // Wrap the evaluation statement in a new function compiled in the newly |
| 11851 | // created context. The function has one parameter which has to be called |
| 11852 | // '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] | 11853 | // the function being debugged. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11854 | // function(arguments,__source__) {return eval(__source__);} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11855 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11856 | Handle<String> function_source = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11857 | isolate->factory()->NewStringFromAscii( |
| 11858 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11859 | |
| 11860 | // Currently, the eval code will be executed in non-strict mode, |
| 11861 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11862 | Handle<SharedFunctionInfo> shared = |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 11863 | Compiler::CompileEval(function_source, |
| 11864 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11865 | context->IsGlobalContext(), |
| 11866 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11867 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11868 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11869 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11870 | |
| 11871 | // Invoke the result of the compilation to get the evaluation function. |
| 11872 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11873 | Handle<Object> receiver(frame->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11874 | Handle<Object> evaluation_function = |
| 11875 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 11876 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 11877 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11878 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11879 | Handle<Object> arguments = GetArgumentsObject(isolate, |
| 11880 | frame, inlined_frame_index, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11881 | function, scope_info, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11882 | &sinfo, function_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11883 | |
| 11884 | // Invoke the evaluation function and return the result. |
| 11885 | const int argc = 2; |
| 11886 | Object** argv[argc] = { arguments.location(), |
| 11887 | Handle<Object>::cast(source).location() }; |
| 11888 | Handle<Object> result = |
| 11889 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), receiver, |
| 11890 | argc, argv, &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 11891 | if (has_pending_exception) return Failure::Exception(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11892 | |
| 11893 | // Skip the global proxy as it has no properties and always delegates to the |
| 11894 | // real global object. |
| 11895 | if (result->IsJSGlobalProxy()) { |
| 11896 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 11897 | } |
| 11898 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11899 | return *result; |
| 11900 | } |
| 11901 | |
| 11902 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11903 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluateGlobal) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11904 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11905 | |
| 11906 | // Check the execution state and decode arguments frame and source to be |
| 11907 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11908 | ASSERT(args.length() == 4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11909 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11910 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 11911 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11912 | if (!maybe_check_result->ToObject(&check_result)) { |
| 11913 | return maybe_check_result; |
| 11914 | } |
| 11915 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11916 | CONVERT_ARG_CHECKED(String, source, 1); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11917 | CONVERT_BOOLEAN_CHECKED(disable_break, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11918 | Handle<Object> additional_context(args[3]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11919 | |
| 11920 | // Handle the processing of break. |
| 11921 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11922 | |
| 11923 | // Enter the top context from before the debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11924 | SaveContext save(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11925 | SaveContext* top = &save; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11926 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11927 | top = top->prev(); |
| 11928 | } |
| 11929 | if (top != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11930 | isolate->set_context(*top->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11931 | } |
| 11932 | |
| 11933 | // Get the global context now set to the top context from before the |
| 11934 | // debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11935 | Handle<Context> context = isolate->global_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11936 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11937 | bool is_global = true; |
| 11938 | |
| 11939 | if (additional_context->IsJSObject()) { |
| 11940 | // 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] | 11941 | Handle<JSFunction> go_between = isolate->factory()->NewFunction( |
| 11942 | isolate->factory()->empty_string(), |
| 11943 | isolate->factory()->undefined_value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11944 | go_between->set_context(*context); |
| 11945 | context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11946 | isolate->factory()->NewFunctionContext( |
| 11947 | Context::MIN_CONTEXT_SLOTS, go_between); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11948 | context->set_extension(JSObject::cast(*additional_context)); |
| 11949 | is_global = false; |
| 11950 | } |
| 11951 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11952 | // Compile the source to be evaluated. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11953 | // Currently, the eval code will be executed in non-strict mode, |
| 11954 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11955 | Handle<SharedFunctionInfo> shared = |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11956 | Compiler::CompileEval(source, context, is_global, kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11957 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11958 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11959 | Handle<JSFunction>( |
| 11960 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 11961 | context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11962 | |
| 11963 | // Invoke the result of the compilation to get the evaluation function. |
| 11964 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11965 | Handle<Object> receiver = isolate->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11966 | Handle<Object> result = |
| 11967 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 11968 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 11969 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11970 | return *result; |
| 11971 | } |
| 11972 | |
| 11973 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11974 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetLoadedScripts) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11975 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11976 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11977 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11978 | // Fill the script objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11979 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11980 | |
| 11981 | // Convert the script objects to proper JS objects. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 11982 | for (int i = 0; i < instances->length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 11983 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 11984 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 11985 | // because using |
| 11986 | // instances->set(i, *GetScriptWrapper(script)) |
| 11987 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
| 11988 | // already have deferenced the instances handle. |
| 11989 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 11990 | instances->set(i, *wrapper); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11991 | } |
| 11992 | |
| 11993 | // Return result as a JS array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11994 | Handle<JSObject> result = |
| 11995 | isolate->factory()->NewJSObject(isolate->array_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11996 | Handle<JSArray>::cast(result)->SetContent(*instances); |
| 11997 | return *result; |
| 11998 | } |
| 11999 | |
| 12000 | |
| 12001 | // Helper function used by Runtime_DebugReferencedBy below. |
| 12002 | static int DebugReferencedBy(JSObject* target, |
| 12003 | Object* instance_filter, int max_references, |
| 12004 | FixedArray* instances, int instances_size, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12005 | JSFunction* arguments_function) { |
| 12006 | NoHandleAllocation ha; |
| 12007 | AssertNoAllocation no_alloc; |
| 12008 | |
| 12009 | // Iterate the heap. |
| 12010 | int count = 0; |
| 12011 | JSObject* last = NULL; |
| 12012 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12013 | HeapObject* heap_obj = NULL; |
| 12014 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12015 | (max_references == 0 || count < max_references)) { |
| 12016 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12017 | if (heap_obj->IsJSObject()) { |
| 12018 | // Skip context extension objects and argument arrays as these are |
| 12019 | // checked in the context of functions using them. |
| 12020 | JSObject* obj = JSObject::cast(heap_obj); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 12021 | if (obj->IsJSContextExtensionObject() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12022 | obj->map()->constructor() == arguments_function) { |
| 12023 | continue; |
| 12024 | } |
| 12025 | |
| 12026 | // Check if the JS object has a reference to the object looked for. |
| 12027 | if (obj->ReferencesObject(target)) { |
| 12028 | // Check instance filter if supplied. This is normally used to avoid |
| 12029 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 12030 | if (!instance_filter->IsUndefined()) { |
| 12031 | Object* V = obj; |
| 12032 | while (true) { |
| 12033 | Object* prototype = V->GetPrototype(); |
| 12034 | if (prototype->IsNull()) { |
| 12035 | break; |
| 12036 | } |
| 12037 | if (instance_filter == prototype) { |
| 12038 | obj = NULL; // Don't add this object. |
| 12039 | break; |
| 12040 | } |
| 12041 | V = prototype; |
| 12042 | } |
| 12043 | } |
| 12044 | |
| 12045 | if (obj != NULL) { |
| 12046 | // Valid reference found add to instance array if supplied an update |
| 12047 | // count. |
| 12048 | if (instances != NULL && count < instances_size) { |
| 12049 | instances->set(count, obj); |
| 12050 | } |
| 12051 | last = obj; |
| 12052 | count++; |
| 12053 | } |
| 12054 | } |
| 12055 | } |
| 12056 | } |
| 12057 | |
| 12058 | // Check for circular reference only. This can happen when the object is only |
| 12059 | // referenced from mirrors and has a circular reference in which case the |
| 12060 | // object is not really alive and would have been garbage collected if not |
| 12061 | // referenced from the mirror. |
| 12062 | if (count == 1 && last == target) { |
| 12063 | count = 0; |
| 12064 | } |
| 12065 | |
| 12066 | // Return the number of referencing objects found. |
| 12067 | return count; |
| 12068 | } |
| 12069 | |
| 12070 | |
| 12071 | // Scan the heap for objects with direct references to an object |
| 12072 | // args[0]: the object to find references to |
| 12073 | // args[1]: constructor function for instances to exclude (Mirror) |
| 12074 | // args[2]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12075 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugReferencedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12076 | ASSERT(args.length() == 3); |
| 12077 | |
| 12078 | // 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] | 12079 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12080 | |
| 12081 | // Check parameters. |
| 12082 | CONVERT_CHECKED(JSObject, target, args[0]); |
| 12083 | Object* instance_filter = args[1]; |
| 12084 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 12085 | instance_filter->IsJSObject()); |
| 12086 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 12087 | RUNTIME_ASSERT(max_references >= 0); |
| 12088 | |
| 12089 | // Get the constructor function for context extension and arguments array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12090 | JSObject* arguments_boilerplate = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12091 | isolate->context()->global_context()->arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12092 | JSFunction* arguments_function = |
| 12093 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 12094 | |
| 12095 | // Get the number of referencing objects. |
| 12096 | int count; |
| 12097 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 12098 | NULL, 0, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12099 | |
| 12100 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12101 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12102 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12103 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12104 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12105 | FixedArray* instances = FixedArray::cast(object); |
| 12106 | |
| 12107 | // Fill the referencing objects. |
| 12108 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 12109 | instances, count, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12110 | |
| 12111 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12112 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12113 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 12114 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12115 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12116 | } |
| 12117 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12118 | return result; |
| 12119 | } |
| 12120 | |
| 12121 | |
| 12122 | // Helper function used by Runtime_DebugConstructedBy below. |
| 12123 | static int DebugConstructedBy(JSFunction* constructor, int max_references, |
| 12124 | FixedArray* instances, int instances_size) { |
| 12125 | AssertNoAllocation no_alloc; |
| 12126 | |
| 12127 | // Iterate the heap. |
| 12128 | int count = 0; |
| 12129 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12130 | HeapObject* heap_obj = NULL; |
| 12131 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12132 | (max_references == 0 || count < max_references)) { |
| 12133 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12134 | if (heap_obj->IsJSObject()) { |
| 12135 | JSObject* obj = JSObject::cast(heap_obj); |
| 12136 | if (obj->map()->constructor() == constructor) { |
| 12137 | // Valid reference found add to instance array if supplied an update |
| 12138 | // count. |
| 12139 | if (instances != NULL && count < instances_size) { |
| 12140 | instances->set(count, obj); |
| 12141 | } |
| 12142 | count++; |
| 12143 | } |
| 12144 | } |
| 12145 | } |
| 12146 | |
| 12147 | // Return the number of referencing objects found. |
| 12148 | return count; |
| 12149 | } |
| 12150 | |
| 12151 | |
| 12152 | // Scan the heap for objects constructed by a specific function. |
| 12153 | // args[0]: the constructor to find instances of |
| 12154 | // args[1]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12155 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugConstructedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12156 | ASSERT(args.length() == 2); |
| 12157 | |
| 12158 | // First perform a full GC in order to avoid dead objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12159 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12160 | |
| 12161 | // Check parameters. |
| 12162 | CONVERT_CHECKED(JSFunction, constructor, args[0]); |
| 12163 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 12164 | RUNTIME_ASSERT(max_references >= 0); |
| 12165 | |
| 12166 | // Get the number of referencing objects. |
| 12167 | int count; |
| 12168 | count = DebugConstructedBy(constructor, max_references, NULL, 0); |
| 12169 | |
| 12170 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12171 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12172 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12173 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12174 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12175 | FixedArray* instances = FixedArray::cast(object); |
| 12176 | |
| 12177 | // Fill the referencing objects. |
| 12178 | count = DebugConstructedBy(constructor, max_references, instances, count); |
| 12179 | |
| 12180 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12181 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12182 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 12183 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12184 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12185 | } |
| 12186 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12187 | return result; |
| 12188 | } |
| 12189 | |
| 12190 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 12191 | // Find the effective prototype object as returned by __proto__. |
| 12192 | // args[0]: the object to find the prototype for. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12193 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPrototype) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12194 | ASSERT(args.length() == 1); |
| 12195 | |
| 12196 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 12197 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 12198 | // Use the __proto__ accessor. |
| 12199 | return Accessors::ObjectPrototype.getter(obj, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12200 | } |
| 12201 | |
| 12202 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12203 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SystemBreak) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12204 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12205 | CPU::DebugBreak(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12206 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12207 | } |
| 12208 | |
| 12209 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12210 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleFunction) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12211 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12212 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12213 | ASSERT(args.length() == 1); |
| 12214 | // Get the function and make sure it is compiled. |
| 12215 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12216 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 12217 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12218 | return Failure::Exception(); |
| 12219 | } |
| 12220 | func->code()->PrintLn(); |
| 12221 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12222 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12223 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 12224 | |
| 12225 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12226 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleConstructor) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12227 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12228 | HandleScope scope(isolate); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12229 | ASSERT(args.length() == 1); |
| 12230 | // Get the function and make sure it is compiled. |
| 12231 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12232 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 12233 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12234 | return Failure::Exception(); |
| 12235 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12236 | shared->construct_stub()->PrintLn(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12237 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12238 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12239 | } |
| 12240 | |
| 12241 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12242 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetInferredName) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 12243 | NoHandleAllocation ha; |
| 12244 | ASSERT(args.length() == 1); |
| 12245 | |
| 12246 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 12247 | return f->shared()->inferred_name(); |
| 12248 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12249 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12250 | |
| 12251 | static int FindSharedFunctionInfosForScript(Script* script, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12252 | FixedArray* buffer) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12253 | AssertNoAllocation no_allocations; |
| 12254 | |
| 12255 | int counter = 0; |
| 12256 | int buffer_size = buffer->length(); |
| 12257 | HeapIterator iterator; |
| 12258 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 12259 | ASSERT(obj != NULL); |
| 12260 | if (!obj->IsSharedFunctionInfo()) { |
| 12261 | continue; |
| 12262 | } |
| 12263 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 12264 | if (shared->script() != script) { |
| 12265 | continue; |
| 12266 | } |
| 12267 | if (counter < buffer_size) { |
| 12268 | buffer->set(counter, shared); |
| 12269 | } |
| 12270 | counter++; |
| 12271 | } |
| 12272 | return counter; |
| 12273 | } |
| 12274 | |
| 12275 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 12276 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 12277 | // in OpaqueReferences. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12278 | RUNTIME_FUNCTION(MaybeObject*, |
| 12279 | Runtime_LiveEditFindSharedFunctionInfosForScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12280 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12281 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12282 | CONVERT_CHECKED(JSValue, script_value, args[0]); |
| 12283 | |
| 12284 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 12285 | |
| 12286 | const int kBufferSize = 32; |
| 12287 | |
| 12288 | Handle<FixedArray> array; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12289 | array = isolate->factory()->NewFixedArray(kBufferSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12290 | int number = FindSharedFunctionInfosForScript(*script, *array); |
| 12291 | if (number > kBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12292 | array = isolate->factory()->NewFixedArray(number); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12293 | FindSharedFunctionInfosForScript(*script, *array); |
| 12294 | } |
| 12295 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12296 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12297 | result->set_length(Smi::FromInt(number)); |
| 12298 | |
| 12299 | LiveEdit::WrapSharedFunctionInfos(result); |
| 12300 | |
| 12301 | return *result; |
| 12302 | } |
| 12303 | |
| 12304 | // For a script calculates compilation information about all its functions. |
| 12305 | // The script source is explicitly specified by the second argument. |
| 12306 | // The source of the actual script is not used, however it is important that |
| 12307 | // all generated code keeps references to this particular instance of script. |
| 12308 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 12309 | // each function with all its descendant is always stored in a continues range |
| 12310 | // with the function itself going first. The root function is a script function. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12311 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditGatherCompileInfo) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12312 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12313 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12314 | CONVERT_CHECKED(JSValue, script, args[0]); |
| 12315 | CONVERT_ARG_CHECKED(String, source, 1); |
| 12316 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 12317 | |
| 12318 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 12319 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12320 | if (isolate->has_pending_exception()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12321 | return Failure::Exception(); |
| 12322 | } |
| 12323 | |
| 12324 | return result; |
| 12325 | } |
| 12326 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12327 | // Changes the source of the script to a new_source. |
| 12328 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 12329 | // the script with its original source and sends notification to debugger. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12330 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12331 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12332 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12333 | CONVERT_CHECKED(JSValue, original_script_value, args[0]); |
| 12334 | CONVERT_ARG_CHECKED(String, new_source, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12335 | Handle<Object> old_script_name(args[2], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12336 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12337 | CONVERT_CHECKED(Script, original_script_pointer, |
| 12338 | original_script_value->value()); |
| 12339 | Handle<Script> original_script(original_script_pointer); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12340 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12341 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 12342 | new_source, |
| 12343 | old_script_name); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12344 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12345 | if (old_script->IsScript()) { |
| 12346 | Handle<Script> script_handle(Script::cast(old_script)); |
| 12347 | return *(GetScriptWrapper(script_handle)); |
| 12348 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12349 | return isolate->heap()->null_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12350 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12351 | } |
| 12352 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12353 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12354 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSourceUpdated) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12355 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12356 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12357 | CONVERT_ARG_CHECKED(JSArray, shared_info, 0); |
| 12358 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 12359 | } |
| 12360 | |
| 12361 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12362 | // Replaces code of SharedFunctionInfo with a new one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12363 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceFunctionCode) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12364 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12365 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12366 | CONVERT_ARG_CHECKED(JSArray, new_compile_info, 0); |
| 12367 | CONVERT_ARG_CHECKED(JSArray, shared_info, 1); |
| 12368 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 12369 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12370 | } |
| 12371 | |
| 12372 | // Connects SharedFunctionInfo to another script. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12373 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSetScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12374 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12375 | HandleScope scope(isolate); |
| 12376 | Handle<Object> function_object(args[0], isolate); |
| 12377 | Handle<Object> script_object(args[1], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12378 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12379 | if (function_object->IsJSValue()) { |
| 12380 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 12381 | if (script_object->IsJSValue()) { |
| 12382 | CONVERT_CHECKED(Script, script, JSValue::cast(*script_object)->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12383 | script_object = Handle<Object>(script, isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12384 | } |
| 12385 | |
| 12386 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 12387 | } else { |
| 12388 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 12389 | // and we check it in this function. |
| 12390 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12391 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12392 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12393 | } |
| 12394 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12395 | |
| 12396 | // In a code of a parent function replaces original function as embedded object |
| 12397 | // with a substitution one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12398 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceRefToNestedFunction) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12399 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12400 | HandleScope scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12401 | |
| 12402 | CONVERT_ARG_CHECKED(JSValue, parent_wrapper, 0); |
| 12403 | CONVERT_ARG_CHECKED(JSValue, orig_wrapper, 1); |
| 12404 | CONVERT_ARG_CHECKED(JSValue, subst_wrapper, 2); |
| 12405 | |
| 12406 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 12407 | subst_wrapper); |
| 12408 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12409 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12410 | } |
| 12411 | |
| 12412 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12413 | // Updates positions of a shared function info (first parameter) according |
| 12414 | // to script source change. Text change is described in second parameter as |
| 12415 | // array of groups of 3 numbers: |
| 12416 | // (change_begin, change_end, change_end_new_position). |
| 12417 | // Each group describes a change in text; groups are sorted by change_begin. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12418 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditPatchFunctionPositions) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12419 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12420 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12421 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
| 12422 | CONVERT_ARG_CHECKED(JSArray, position_change_array, 1); |
| 12423 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 12424 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12425 | } |
| 12426 | |
| 12427 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12428 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 12429 | // checks that none of them have activations on stacks (of any thread). |
| 12430 | // Returns array of the same length with corresponding results of |
| 12431 | // LiveEdit::FunctionPatchabilityStatus type. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12432 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCheckAndDropActivations) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12433 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12434 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12435 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12436 | CONVERT_BOOLEAN_CHECKED(do_drop, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12437 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12438 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12439 | } |
| 12440 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 12441 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 12442 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 12443 | // of diff chunks. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12444 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCompareStrings) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12445 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12446 | HandleScope scope(isolate); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12447 | CONVERT_ARG_CHECKED(String, s1, 0); |
| 12448 | CONVERT_ARG_CHECKED(String, s2, 1); |
| 12449 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 12450 | return *LiveEdit::CompareStrings(s1, s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12451 | } |
| 12452 | |
| 12453 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12454 | // A testing entry. Returns statement position which is the closest to |
| 12455 | // source_position. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12456 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionCodePositionFromSource) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12457 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12458 | HandleScope scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12459 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 12460 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 12461 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12462 | Handle<Code> code(function->code(), isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12463 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12464 | if (code->kind() != Code::FUNCTION && |
| 12465 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12466 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12467 | } |
| 12468 | |
| 12469 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12470 | int closest_pc = 0; |
| 12471 | int distance = kMaxInt; |
| 12472 | while (!it.done()) { |
| 12473 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 12474 | // Check if this break point is closer that what was previously found. |
| 12475 | if (source_position <= statement_position && |
| 12476 | statement_position - source_position < distance) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 12477 | closest_pc = |
| 12478 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12479 | distance = statement_position - source_position; |
| 12480 | // Check whether we can't get any closer. |
| 12481 | if (distance == 0) break; |
| 12482 | } |
| 12483 | it.next(); |
| 12484 | } |
| 12485 | |
| 12486 | return Smi::FromInt(closest_pc); |
| 12487 | } |
| 12488 | |
| 12489 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12490 | // Calls specified function with or without entering the debugger. |
| 12491 | // This is used in unit tests to run code as if debugger is entered or simply |
| 12492 | // to have a stack with C++ frame in the middle. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12493 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ExecuteInDebugContext) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12494 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12495 | HandleScope scope(isolate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12496 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 12497 | CONVERT_BOOLEAN_CHECKED(without_debugger, args[1]); |
| 12498 | |
| 12499 | Handle<Object> result; |
| 12500 | bool pending_exception; |
| 12501 | { |
| 12502 | if (without_debugger) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12503 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12504 | &pending_exception); |
| 12505 | } else { |
| 12506 | EnterDebugger enter_debugger; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12507 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12508 | &pending_exception); |
| 12509 | } |
| 12510 | } |
| 12511 | if (!pending_exception) { |
| 12512 | return *result; |
| 12513 | } else { |
| 12514 | return Failure::Exception(); |
| 12515 | } |
| 12516 | } |
| 12517 | |
| 12518 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12519 | // Sets a v8 flag. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12520 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFlags) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12521 | CONVERT_CHECKED(String, arg, args[0]); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 12522 | SmartArrayPointer<char> flags = |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12523 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 12524 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12525 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12526 | } |
| 12527 | |
| 12528 | |
| 12529 | // Performs a GC. |
| 12530 | // Presently, it only does a full GC. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12531 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectGarbage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12532 | isolate->heap()->CollectAllGarbage(true); |
| 12533 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12534 | } |
| 12535 | |
| 12536 | |
| 12537 | // Gets the current heap usage. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12538 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHeapUsage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12539 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12540 | if (!Smi::IsValid(usage)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12541 | return *isolate->factory()->NewNumberFromInt(usage); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12542 | } |
| 12543 | return Smi::FromInt(usage); |
| 12544 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12545 | |
| 12546 | |
| 12547 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12548 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLOLEnabled) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12549 | #ifdef LIVE_OBJECT_LIST |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12550 | return isolate->heap()->true_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12551 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12552 | return isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12553 | #endif |
| 12554 | } |
| 12555 | |
| 12556 | |
| 12557 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12558 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CaptureLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12559 | #ifdef LIVE_OBJECT_LIST |
| 12560 | return LiveObjectList::Capture(); |
| 12561 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12562 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12563 | #endif |
| 12564 | } |
| 12565 | |
| 12566 | |
| 12567 | // Deletes the specified live object list. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12568 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12569 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12570 | CONVERT_SMI_ARG_CHECKED(id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12571 | bool success = LiveObjectList::Delete(id); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12572 | return isolate->heap()->ToBoolean(success); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12573 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12574 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12575 | #endif |
| 12576 | } |
| 12577 | |
| 12578 | |
| 12579 | // Generates the response to a debugger request for a dump of the objects |
| 12580 | // contained in the difference between the captured live object lists |
| 12581 | // specified by id1 and id2. |
| 12582 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 12583 | // dumped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12584 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DumpLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12585 | #ifdef LIVE_OBJECT_LIST |
| 12586 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12587 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 12588 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
| 12589 | CONVERT_SMI_ARG_CHECKED(start, 2); |
| 12590 | CONVERT_SMI_ARG_CHECKED(count, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12591 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 4); |
| 12592 | EnterDebugger enter_debugger; |
| 12593 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 12594 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12595 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12596 | #endif |
| 12597 | } |
| 12598 | |
| 12599 | |
| 12600 | // Gets the specified object as requested by the debugger. |
| 12601 | // This is only used for obj ids shown in live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12602 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12603 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12604 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12605 | Object* result = LiveObjectList::GetObj(obj_id); |
| 12606 | return result; |
| 12607 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12608 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12609 | #endif |
| 12610 | } |
| 12611 | |
| 12612 | |
| 12613 | // Gets the obj id for the specified address if valid. |
| 12614 | // This is only used for obj ids shown in live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12615 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjId) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12616 | #ifdef LIVE_OBJECT_LIST |
| 12617 | HandleScope scope; |
| 12618 | CONVERT_ARG_CHECKED(String, address, 0); |
| 12619 | Object* result = LiveObjectList::GetObjId(address); |
| 12620 | return result; |
| 12621 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12622 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12623 | #endif |
| 12624 | } |
| 12625 | |
| 12626 | |
| 12627 | // Gets the retainers that references the specified object alive. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12628 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjRetainers) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12629 | #ifdef LIVE_OBJECT_LIST |
| 12630 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12631 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12632 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 12633 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 12634 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 12635 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
| 12636 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 5); |
| 12637 | |
| 12638 | Handle<JSObject> instance_filter; |
| 12639 | if (args[1]->IsJSObject()) { |
| 12640 | instance_filter = args.at<JSObject>(1); |
| 12641 | } |
| 12642 | bool verbose = false; |
| 12643 | if (args[2]->IsBoolean()) { |
| 12644 | verbose = args[2]->IsTrue(); |
| 12645 | } |
| 12646 | int start = 0; |
| 12647 | if (args[3]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12648 | start = args.smi_at(3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12649 | } |
| 12650 | int limit = Smi::kMaxValue; |
| 12651 | if (args[4]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12652 | limit = args.smi_at(4); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12653 | } |
| 12654 | |
| 12655 | return LiveObjectList::GetObjRetainers(obj_id, |
| 12656 | instance_filter, |
| 12657 | verbose, |
| 12658 | start, |
| 12659 | limit, |
| 12660 | filter_obj); |
| 12661 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12662 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12663 | #endif |
| 12664 | } |
| 12665 | |
| 12666 | |
| 12667 | // Gets the reference path between 2 objects. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12668 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLPath) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12669 | #ifdef LIVE_OBJECT_LIST |
| 12670 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12671 | CONVERT_SMI_ARG_CHECKED(obj_id1, 0); |
| 12672 | CONVERT_SMI_ARG_CHECKED(obj_id2, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12673 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 12674 | |
| 12675 | Handle<JSObject> instance_filter; |
| 12676 | if (args[2]->IsJSObject()) { |
| 12677 | instance_filter = args.at<JSObject>(2); |
| 12678 | } |
| 12679 | |
| 12680 | Object* result = |
| 12681 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 12682 | return result; |
| 12683 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12684 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12685 | #endif |
| 12686 | } |
| 12687 | |
| 12688 | |
| 12689 | // Generates the response to a debugger request for a list of all |
| 12690 | // previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12691 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InfoLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12692 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12693 | CONVERT_SMI_ARG_CHECKED(start, 0); |
| 12694 | CONVERT_SMI_ARG_CHECKED(count, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12695 | return LiveObjectList::Info(start, count); |
| 12696 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12697 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12698 | #endif |
| 12699 | } |
| 12700 | |
| 12701 | |
| 12702 | // Gets a dump of the specified object as requested by the debugger. |
| 12703 | // This is only used for obj ids shown in live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12704 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrintLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12705 | #ifdef LIVE_OBJECT_LIST |
| 12706 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12707 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12708 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 12709 | return result; |
| 12710 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12711 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12712 | #endif |
| 12713 | } |
| 12714 | |
| 12715 | |
| 12716 | // Resets and releases all previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12717 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ResetLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12718 | #ifdef LIVE_OBJECT_LIST |
| 12719 | LiveObjectList::Reset(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12720 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12721 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12722 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12723 | #endif |
| 12724 | } |
| 12725 | |
| 12726 | |
| 12727 | // Generates the response to a debugger request for a summary of the types |
| 12728 | // of objects in the difference between the captured live object lists |
| 12729 | // specified by id1 and id2. |
| 12730 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 12731 | // summarized. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12732 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SummarizeLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12733 | #ifdef LIVE_OBJECT_LIST |
| 12734 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12735 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 12736 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12737 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 2); |
| 12738 | |
| 12739 | EnterDebugger enter_debugger; |
| 12740 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 12741 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12742 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12743 | #endif |
| 12744 | } |
| 12745 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12746 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 12747 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12748 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12749 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerResume) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12750 | NoHandleAllocation ha; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 12751 | v8::V8::ResumeProfiler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12752 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12753 | } |
| 12754 | |
| 12755 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12756 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerPause) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12757 | NoHandleAllocation ha; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 12758 | v8::V8::PauseProfiler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12759 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12760 | } |
| 12761 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12762 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12763 | // Finds the script object from the script data. NOTE: This operation uses |
| 12764 | // heap traversal to find the function generated for the source position |
| 12765 | // for the requested break point. For lazily compiled functions several heap |
| 12766 | // traversals might be required rendering this operation as a rather slow |
| 12767 | // operation. However for setting break points which is normally done through |
| 12768 | // some kind of user interaction the performance is not crucial. |
| 12769 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 12770 | Handle<String> script_name) { |
| 12771 | // Scan the heap for Script objects to find the script with the requested |
| 12772 | // script data. |
| 12773 | Handle<Script> script; |
| 12774 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12775 | HeapObject* obj = NULL; |
| 12776 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12777 | // If a script is found check if it has the script data requested. |
| 12778 | if (obj->IsScript()) { |
| 12779 | if (Script::cast(obj)->name()->IsString()) { |
| 12780 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 12781 | script = Handle<Script>(Script::cast(obj)); |
| 12782 | } |
| 12783 | } |
| 12784 | } |
| 12785 | } |
| 12786 | |
| 12787 | // 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] | 12788 | if (script.is_null()) return FACTORY->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12789 | |
| 12790 | // Return the script found. |
| 12791 | return GetScriptWrapper(script); |
| 12792 | } |
| 12793 | |
| 12794 | |
| 12795 | // Get the script object from script data. NOTE: Regarding performance |
| 12796 | // see the NOTE for GetScriptFromScriptData. |
| 12797 | // args[0]: script data for the script to find the source for |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12798 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12799 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12800 | |
| 12801 | ASSERT(args.length() == 1); |
| 12802 | |
| 12803 | CONVERT_CHECKED(String, script_name, args[0]); |
| 12804 | |
| 12805 | // Find the requested script. |
| 12806 | Handle<Object> result = |
| 12807 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 12808 | return *result; |
| 12809 | } |
| 12810 | |
| 12811 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12812 | // Determines whether the given stack frame should be displayed in |
| 12813 | // a stack trace. The caller is the error constructor that asked |
| 12814 | // for the stack trace to be collected. The first time a construct |
| 12815 | // call to this function is encountered it is skipped. The seen_caller |
| 12816 | // in/out parameter is used to remember if the caller has been seen |
| 12817 | // yet. |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 12818 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, |
| 12819 | Object* caller, |
| 12820 | bool* seen_caller) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12821 | // Only display JS frames. |
| 12822 | if (!raw_frame->is_java_script()) |
| 12823 | return false; |
| 12824 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 12825 | Object* raw_fun = frame->function(); |
| 12826 | // Not sure when this can happen but skip it just in case. |
| 12827 | if (!raw_fun->IsJSFunction()) |
| 12828 | return false; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12829 | if ((raw_fun == caller) && !(*seen_caller)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12830 | *seen_caller = true; |
| 12831 | return false; |
| 12832 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 12833 | // Skip all frames until we've seen the caller. |
| 12834 | if (!(*seen_caller)) return false; |
| 12835 | // Also, skip the most obvious builtin calls. We recognize builtins |
| 12836 | // as (1) functions called with the builtins object as the receiver and |
| 12837 | // as (2) functions from native scripts called with undefined as the |
| 12838 | // receiver (direct calls to helper functions in the builtins |
| 12839 | // code). Some builtin calls (such as Number.ADD which is invoked |
| 12840 | // using 'call') are very difficult to recognize so we're leaving |
| 12841 | // them in for now. |
| 12842 | if (frame->receiver()->IsJSBuiltinsObject()) { |
| 12843 | return false; |
| 12844 | } |
| 12845 | JSFunction* fun = JSFunction::cast(raw_fun); |
| 12846 | Object* raw_script = fun->shared()->script(); |
| 12847 | if (frame->receiver()->IsUndefined() && raw_script->IsScript()) { |
| 12848 | int script_type = Script::cast(raw_script)->type()->value(); |
| 12849 | return script_type != Script::TYPE_NATIVE; |
| 12850 | } |
| 12851 | return true; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12852 | } |
| 12853 | |
| 12854 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12855 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 12856 | // element segments each containing a receiver, function, code and |
| 12857 | // native code offset. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12858 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectStackTrace) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12859 | ASSERT_EQ(args.length(), 2); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12860 | Handle<Object> caller = args.at<Object>(0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12861 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[1]); |
| 12862 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12863 | HandleScope scope(isolate); |
| 12864 | Factory* factory = isolate->factory(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12865 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 12866 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 12867 | int initial_size = Min(limit, 10); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12868 | Handle<FixedArray> elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12869 | factory->NewFixedArrayWithHoles(initial_size * 4); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12870 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 12871 | StackFrameIterator iter(isolate); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12872 | // If the caller parameter is a function we skip frames until we're |
| 12873 | // under it before starting to collect. |
| 12874 | bool seen_caller = !caller->IsJSFunction(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12875 | int cursor = 0; |
| 12876 | int frames_seen = 0; |
| 12877 | while (!iter.done() && frames_seen < limit) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12878 | StackFrame* raw_frame = iter.frame(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12879 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12880 | frames_seen++; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12881 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 12882 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 12883 | // function. |
| 12884 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12885 | frame->Summarize(&frames); |
| 12886 | for (int i = frames.length() - 1; i >= 0; i--) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12887 | if (cursor + 4 > elements->length()) { |
| 12888 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 12889 | Handle<FixedArray> new_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12890 | factory->NewFixedArrayWithHoles(new_capacity); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12891 | for (int i = 0; i < cursor; i++) { |
| 12892 | new_elements->set(i, elements->get(i)); |
| 12893 | } |
| 12894 | elements = new_elements; |
| 12895 | } |
| 12896 | ASSERT(cursor + 4 <= elements->length()); |
| 12897 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12898 | Handle<Object> recv = frames[i].receiver(); |
| 12899 | Handle<JSFunction> fun = frames[i].function(); |
| 12900 | Handle<Code> code = frames[i].code(); |
| 12901 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12902 | elements->set(cursor++, *recv); |
| 12903 | elements->set(cursor++, *fun); |
| 12904 | elements->set(cursor++, *code); |
| 12905 | elements->set(cursor++, *offset); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12906 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12907 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12908 | iter.Advance(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12909 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12910 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12911 | result->set_length(Smi::FromInt(cursor)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12912 | return *result; |
| 12913 | } |
| 12914 | |
| 12915 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12916 | // Returns V8 version as a string. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12917 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetV8Version) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12918 | ASSERT_EQ(args.length(), 0); |
| 12919 | |
| 12920 | NoHandleAllocation ha; |
| 12921 | |
| 12922 | const char* version_string = v8::V8::GetVersion(); |
| 12923 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12924 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 12925 | NOT_TENURED); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12926 | } |
| 12927 | |
| 12928 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Abort) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12930 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12931 | OS::PrintError("abort: %s\n", |
| 12932 | reinterpret_cast<char*>(args[0]) + args.smi_at(1)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12933 | isolate->PrintStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12934 | OS::Abort(); |
| 12935 | UNREACHABLE(); |
| 12936 | return NULL; |
| 12937 | } |
| 12938 | |
| 12939 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12940 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFromCache) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12941 | // 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] | 12942 | CONVERT_CHECKED(JSFunctionResultCache, cache, args[0]); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12943 | Object* key = args[1]; |
| 12944 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12945 | int finger_index = cache->finger_index(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12946 | Object* o = cache->get(finger_index); |
| 12947 | if (o == key) { |
| 12948 | // The fastest case: hit the same place again. |
| 12949 | return cache->get(finger_index + 1); |
| 12950 | } |
| 12951 | |
| 12952 | for (int i = finger_index - 2; |
| 12953 | i >= JSFunctionResultCache::kEntriesIndex; |
| 12954 | i -= 2) { |
| 12955 | o = cache->get(i); |
| 12956 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12957 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12958 | return cache->get(i + 1); |
| 12959 | } |
| 12960 | } |
| 12961 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12962 | int size = cache->size(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12963 | ASSERT(size <= cache->length()); |
| 12964 | |
| 12965 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 12966 | o = cache->get(i); |
| 12967 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12968 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12969 | return cache->get(i + 1); |
| 12970 | } |
| 12971 | } |
| 12972 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12973 | // 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] | 12974 | HandleScope scope(isolate); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12975 | |
| 12976 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 12977 | Handle<Object> key_handle(key); |
| 12978 | Handle<Object> value; |
| 12979 | { |
| 12980 | Handle<JSFunction> factory(JSFunction::cast( |
| 12981 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 12982 | // TODO(antonm): consider passing a receiver when constructing a cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12983 | Handle<Object> receiver(isolate->global_context()->global()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12984 | // This handle is nor shared, nor used later, so it's safe. |
| 12985 | Object** argv[] = { key_handle.location() }; |
| 12986 | bool pending_exception = false; |
| 12987 | value = Execution::Call(factory, |
| 12988 | receiver, |
| 12989 | 1, |
| 12990 | argv, |
| 12991 | &pending_exception); |
| 12992 | if (pending_exception) return Failure::Exception(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12993 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12994 | |
| 12995 | #ifdef DEBUG |
| 12996 | cache_handle->JSFunctionResultCacheVerify(); |
| 12997 | #endif |
| 12998 | |
| 12999 | // Function invocation may have cleared the cache. Reread all the data. |
| 13000 | finger_index = cache_handle->finger_index(); |
| 13001 | size = cache_handle->size(); |
| 13002 | |
| 13003 | // If we have spare room, put new data into it, otherwise evict post finger |
| 13004 | // entry which is likely to be the least recently used. |
| 13005 | int index = -1; |
| 13006 | if (size < cache_handle->length()) { |
| 13007 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 13008 | index = size; |
| 13009 | } else { |
| 13010 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 13011 | if (index == cache_handle->length()) { |
| 13012 | index = JSFunctionResultCache::kEntriesIndex; |
| 13013 | } |
| 13014 | } |
| 13015 | |
| 13016 | ASSERT(index % 2 == 0); |
| 13017 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 13018 | ASSERT(index < cache_handle->length()); |
| 13019 | |
| 13020 | cache_handle->set(index, *key_handle); |
| 13021 | cache_handle->set(index + 1, *value); |
| 13022 | cache_handle->set_finger_index(index); |
| 13023 | |
| 13024 | #ifdef DEBUG |
| 13025 | cache_handle->JSFunctionResultCacheVerify(); |
| 13026 | #endif |
| 13027 | |
| 13028 | return *value; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13029 | } |
| 13030 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13031 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13032 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewMessageObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13033 | HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13034 | CONVERT_ARG_CHECKED(String, type, 0); |
| 13035 | CONVERT_ARG_CHECKED(JSArray, arguments, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13036 | return *isolate->factory()->NewJSMessageObject( |
| 13037 | type, |
| 13038 | arguments, |
| 13039 | 0, |
| 13040 | 0, |
| 13041 | isolate->factory()->undefined_value(), |
| 13042 | isolate->factory()->undefined_value(), |
| 13043 | isolate->factory()->undefined_value()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13044 | } |
| 13045 | |
| 13046 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13047 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetType) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13048 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13049 | return message->type(); |
| 13050 | } |
| 13051 | |
| 13052 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13053 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetArguments) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13054 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13055 | return message->arguments(); |
| 13056 | } |
| 13057 | |
| 13058 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13059 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetStartPosition) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13060 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13061 | return Smi::FromInt(message->start_position()); |
| 13062 | } |
| 13063 | |
| 13064 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13065 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetScript) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13066 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 13067 | return message->script(); |
| 13068 | } |
| 13069 | |
| 13070 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 13071 | #ifdef DEBUG |
| 13072 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 13073 | // Exclude the code in release mode. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13074 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ListNatives) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 13075 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13076 | HandleScope scope; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13077 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 13078 | int entry_count = 0 |
| 13079 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 13080 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 13081 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 13082 | #undef COUNT_ENTRY |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13083 | Factory* factory = isolate->factory(); |
| 13084 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13085 | int index = 0; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13086 | bool inline_runtime_functions = false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 13087 | #define ADD_ENTRY(Name, argc, ressize) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13088 | { \ |
| 13089 | HandleScope inner; \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13090 | Handle<String> name; \ |
| 13091 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 13092 | if (inline_runtime_functions) { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13093 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13094 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 13095 | } else { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13096 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13097 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 13098 | } \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13099 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13100 | pair_elements->set(0, *name); \ |
| 13101 | pair_elements->set(1, Smi::FromInt(argc)); \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13102 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13103 | elements->set(index++, *pair); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13104 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13105 | inline_runtime_functions = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13106 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13107 | inline_runtime_functions = true; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13108 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13109 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13110 | #undef ADD_ENTRY |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13111 | ASSERT_EQ(index, entry_count); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13112 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13113 | return *result; |
| 13114 | } |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 13115 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13116 | |
| 13117 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13118 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Log) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 13119 | ASSERT(args.length() == 2); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 13120 | CONVERT_CHECKED(String, format, args[0]); |
| 13121 | CONVERT_CHECKED(JSArray, elms, args[1]); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 13122 | String::FlatContent format_content = format->GetFlatContent(); |
| 13123 | RUNTIME_ASSERT(format_content.IsAscii()); |
| 13124 | Vector<const char> chars = format_content.ToAsciiVector(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13125 | LOGGER->LogRuntime(chars, elms); |
| 13126 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 13127 | } |
| 13128 | |
| 13129 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13130 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IS_VAR) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13131 | UNREACHABLE(); // implemented as macro in the parser |
| 13132 | return NULL; |
| 13133 | } |
| 13134 | |
| 13135 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 13136 | #define ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(Name) \ |
| 13137 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Has##Name) { \ |
| 13138 | CONVERT_CHECKED(JSObject, obj, args[0]); \ |
| 13139 | return isolate->heap()->ToBoolean(obj->Has##Name()); \ |
| 13140 | } |
| 13141 | |
| 13142 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastElements) |
| 13143 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastDoubleElements) |
| 13144 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(DictionaryElements) |
| 13145 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalPixelElements) |
| 13146 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalArrayElements) |
| 13147 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalByteElements) |
| 13148 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedByteElements) |
| 13149 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalShortElements) |
| 13150 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedShortElements) |
| 13151 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalIntElements) |
| 13152 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedIntElements) |
| 13153 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalFloatElements) |
| 13154 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalDoubleElements) |
| 13155 | |
| 13156 | #undef ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION |
| 13157 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13158 | // ---------------------------------------------------------------------------- |
| 13159 | // Implementation of Runtime |
| 13160 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13161 | #define F(name, number_of_args, result_size) \ |
| 13162 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 13163 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13164 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13165 | |
| 13166 | #define I(name, number_of_args, result_size) \ |
| 13167 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 13168 | "_" #name, NULL, number_of_args, result_size }, |
| 13169 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13170 | static const Runtime::Function kIntrinsicFunctions[] = { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13171 | RUNTIME_FUNCTION_LIST(F) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13172 | INLINE_FUNCTION_LIST(I) |
| 13173 | INLINE_RUNTIME_FUNCTION_LIST(I) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13174 | }; |
| 13175 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13176 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13177 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 13178 | Object* dictionary) { |
| 13179 | ASSERT(Isolate::Current()->heap() == heap); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13180 | ASSERT(dictionary != NULL); |
| 13181 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 13182 | for (int i = 0; i < kNumFunctions; ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13183 | Object* name_symbol; |
| 13184 | { MaybeObject* maybe_name_symbol = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13185 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13186 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 13187 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13188 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13189 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 13190 | String::cast(name_symbol), |
| 13191 | Smi::FromInt(i), |
| 13192 | PropertyDetails(NONE, NORMAL)); |
| 13193 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 13194 | // Non-recoverable failure. Calling code must restart heap |
| 13195 | // initialization. |
| 13196 | return maybe_dictionary; |
| 13197 | } |
| 13198 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13199 | } |
| 13200 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13201 | } |
| 13202 | |
| 13203 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13204 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 13205 | Heap* heap = name->GetHeap(); |
| 13206 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13207 | if (entry != kNotFound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13208 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13209 | int function_index = Smi::cast(smi_index)->value(); |
| 13210 | return &(kIntrinsicFunctions[function_index]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13211 | } |
| 13212 | return NULL; |
| 13213 | } |
| 13214 | |
| 13215 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13216 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13217 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 13218 | } |
| 13219 | |
| 13220 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13221 | void Runtime::PerformGC(Object* result) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 13222 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13223 | Failure* failure = Failure::cast(result); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13224 | if (failure->IsRetryAfterGC()) { |
| 13225 | // Try to do a garbage collection; ignore it if it fails. The C |
| 13226 | // entry stub will throw an out-of-memory exception in that case. |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 13227 | isolate->heap()->CollectGarbage(failure->allocation_space()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13228 | } else { |
| 13229 | // Handle last resort GC and make sure to allow future allocations |
| 13230 | // to grow the heap without causing GCs (if possible). |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 13231 | isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 13232 | isolate->heap()->CollectAllGarbage(false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13233 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13234 | } |
| 13235 | |
| 13236 | |
| 13237 | } } // namespace v8::internal |