rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1 | // Copyright 2012 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" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 45 | #include "isolate-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | #include "jsregexp.h" |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 47 | #include "json-parser.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 48 | #include "liveedit.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 49 | #include "liveobjectlist-inl.h" |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 50 | #include "misc-intrinsics.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 51 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 53 | #include "runtime-profiler.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 54 | #include "runtime.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 55 | #include "scopeinfo.h" |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 56 | #include "smart-array-pointer.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 57 | #include "string-search.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 58 | #include "stub-cache.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 59 | #include "v8threads.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 60 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 62 | namespace v8 { |
| 63 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 64 | |
| 65 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 66 | #define RUNTIME_ASSERT(value) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | if (!(value)) return isolate->ThrowIllegalOperation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | |
| 69 | // Cast the given object to a value of the specified type and store |
| 70 | // it in a variable with the given name. If the object is not of the |
| 71 | // expected type call IllegalOperation and return. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 73 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 74 | Type* name = Type::cast(args[index]); |
| 75 | |
| 76 | #define CONVERT_ARG_HANDLE_CHECKED(Type, name, index) \ |
| 77 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | Handle<Type> name = args.at<Type>(index); |
| 79 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 80 | // Cast the given object to a boolean and store it in a variable with |
| 81 | // the given name. If the object is not a boolean call IllegalOperation |
| 82 | // and return. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 83 | #define CONVERT_BOOLEAN_ARG_CHECKED(name, index) \ |
| 84 | RUNTIME_ASSERT(args[index]->IsBoolean()); \ |
| 85 | bool name = args[index]->IsTrue(); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 86 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 87 | // Cast the given argument to a Smi and store its value in an int variable |
| 88 | // with the given name. If the argument is not a Smi call IllegalOperation |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 89 | // and return. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 90 | #define CONVERT_SMI_ARG_CHECKED(name, index) \ |
| 91 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 92 | int name = args.smi_at(index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 93 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 94 | // Cast the given argument to a double and store it in a variable with |
| 95 | // the given name. If the argument is not a number (as opposed to |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | // the number not-a-number) call IllegalOperation and return. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 97 | #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \ |
| 98 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 99 | double name = args.number_at(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | |
| 101 | // Call the specified converter on the object *comand store the result in |
| 102 | // a variable of the specified type with the given name. If the |
| 103 | // object is not a Number call IllegalOperation and return. |
| 104 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 105 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 106 | type name = NumberTo##Type(obj); |
| 107 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 109 | // Cast the given argument to PropertyDetails and store its value in a |
| 110 | // variable with the given name. If the argument is not a Smi call |
| 111 | // IllegalOperation and return. |
| 112 | #define CONVERT_PROPERTY_DETAILS_CHECKED(name, index) \ |
| 113 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 114 | PropertyDetails name = PropertyDetails(Smi::cast(args[index])); |
| 115 | |
| 116 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 117 | // Assert that the given argument has a valid value for a StrictModeFlag |
| 118 | // and store it in a StrictModeFlag variable with the given name. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 119 | #define CONVERT_STRICT_MODE_ARG_CHECKED(name, index) \ |
| 120 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 121 | RUNTIME_ASSERT(args.smi_at(index) == kStrictMode || \ |
| 122 | args.smi_at(index) == kNonStrictMode); \ |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 123 | StrictModeFlag name = \ |
| 124 | static_cast<StrictModeFlag>(args.smi_at(index)); |
| 125 | |
| 126 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 127 | // Assert that the given argument has a valid value for a LanguageMode |
| 128 | // and store it in a LanguageMode variable with the given name. |
| 129 | #define CONVERT_LANGUAGE_MODE_ARG(name, index) \ |
| 130 | ASSERT(args[index]->IsSmi()); \ |
| 131 | ASSERT(args.smi_at(index) == CLASSIC_MODE || \ |
| 132 | args.smi_at(index) == STRICT_MODE || \ |
| 133 | args.smi_at(index) == EXTENDED_MODE); \ |
| 134 | LanguageMode name = \ |
| 135 | static_cast<LanguageMode>(args.smi_at(index)); |
| 136 | |
| 137 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 138 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 139 | JSObject* boilerplate) { |
| 140 | StackLimitCheck check(isolate); |
| 141 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 143 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 144 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 145 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 146 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 147 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 148 | JSObject* copy = JSObject::cast(result); |
| 149 | |
| 150 | // Deep copy local properties. |
| 151 | if (copy->HasFastProperties()) { |
| 152 | FixedArray* properties = copy->properties(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 153 | for (int i = 0; i < properties->length(); i++) { |
| 154 | Object* value = properties->get(i); |
| 155 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 156 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 157 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 158 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 159 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 160 | properties->set(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 163 | int nof = copy->map()->inobject_properties(); |
| 164 | for (int i = 0; i < nof; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 165 | Object* value = copy->InObjectPropertyAt(i); |
| 166 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 167 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 168 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 169 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 170 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 171 | copy->InObjectPropertyAtPut(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 175 | { MaybeObject* maybe_result = |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 176 | heap->AllocateFixedArray(copy->NumberOfLocalProperties()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 177 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 178 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 179 | FixedArray* names = FixedArray::cast(result); |
| 180 | copy->GetLocalPropertyNames(names, 0); |
| 181 | for (int i = 0; i < names->length(); i++) { |
| 182 | ASSERT(names->get(i)->IsString()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 183 | String* key_string = String::cast(names->get(i)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 184 | PropertyAttributes attributes = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 185 | copy->GetLocalPropertyAttribute(key_string); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 186 | // Only deep copy fields from the object literal expression. |
| 187 | // In particular, don't try to copy the length attribute of |
| 188 | // an array. |
| 189 | if (attributes != NONE) continue; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 190 | Object* value = |
| 191 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 192 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 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, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 195 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 196 | } |
| 197 | { MaybeObject* maybe_result = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 198 | // Creating object copy for literals. No strict mode needed. |
| 199 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 200 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 201 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Deep copy local elements. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 207 | // Pixel elements cannot be created using an object literal. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 208 | ASSERT(!copy->HasExternalArrayElements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 209 | switch (copy->GetElementsKind()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 210 | case FAST_SMI_ONLY_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 211 | case FAST_ELEMENTS: { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 212 | FixedArray* elements = FixedArray::cast(copy->elements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 213 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 214 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 215 | #ifdef DEBUG |
| 216 | for (int i = 0; i < elements->length(); i++) { |
| 217 | ASSERT(!elements->get(i)->IsJSObject()); |
| 218 | } |
| 219 | #endif |
| 220 | } else { |
| 221 | for (int i = 0; i < elements->length(); i++) { |
| 222 | Object* value = elements->get(i); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 223 | ASSERT(value->IsSmi() || |
| 224 | value->IsTheHole() || |
| 225 | (copy->GetElementsKind() == FAST_ELEMENTS)); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 226 | if (value->IsJSObject()) { |
| 227 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 228 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 229 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 230 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 231 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 232 | elements->set(i, result); |
| 233 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 236 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 237 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 238 | case DICTIONARY_ELEMENTS: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 239 | SeededNumberDictionary* element_dictionary = copy->element_dictionary(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 240 | int capacity = element_dictionary->Capacity(); |
| 241 | for (int i = 0; i < capacity; i++) { |
| 242 | Object* k = element_dictionary->KeyAt(i); |
| 243 | if (element_dictionary->IsKey(k)) { |
| 244 | Object* value = element_dictionary->ValueAt(i); |
| 245 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 246 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 247 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 248 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 249 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 250 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 251 | element_dictionary->ValueAtPut(i, result); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | break; |
| 256 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 257 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 258 | UNIMPLEMENTED(); |
| 259 | break; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 260 | case EXTERNAL_PIXEL_ELEMENTS: |
| 261 | case EXTERNAL_BYTE_ELEMENTS: |
| 262 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 263 | case EXTERNAL_SHORT_ELEMENTS: |
| 264 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 265 | case EXTERNAL_INT_ELEMENTS: |
| 266 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 267 | case EXTERNAL_FLOAT_ELEMENTS: |
| 268 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 269 | case FAST_DOUBLE_ELEMENTS: |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 270 | // No contained objects, nothing to do. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 271 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 272 | } |
| 273 | return copy; |
| 274 | } |
| 275 | |
| 276 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 277 | static Handle<Map> ComputeObjectLiteralMap( |
| 278 | Handle<Context> context, |
| 279 | Handle<FixedArray> constant_properties, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 280 | bool* is_result_from_cache) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 281 | Isolate* isolate = context->GetIsolate(); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 282 | int properties_length = constant_properties->length(); |
| 283 | int number_of_properties = properties_length / 2; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 284 | // Check that there are only symbols and array indices among keys. |
| 285 | int number_of_symbol_keys = 0; |
| 286 | for (int p = 0; p != properties_length; p += 2) { |
| 287 | Object* key = constant_properties->get(p); |
| 288 | uint32_t element_index = 0; |
| 289 | if (key->IsSymbol()) { |
| 290 | number_of_symbol_keys++; |
| 291 | } else if (key->ToArrayIndex(&element_index)) { |
| 292 | // An index key does not require space in the property backing store. |
| 293 | number_of_properties--; |
| 294 | } else { |
| 295 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 296 | // ASSERT to make sure that the if condition after the loop is false. |
| 297 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 298 | break; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 299 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 300 | } |
| 301 | // If we only have symbols and array indices among keys then we can |
| 302 | // use the map cache in the global context. |
| 303 | const int kMaxKeys = 10; |
| 304 | if ((number_of_symbol_keys == number_of_properties) && |
| 305 | (number_of_symbol_keys < kMaxKeys)) { |
| 306 | // Create the fixed array with the key. |
| 307 | Handle<FixedArray> keys = |
| 308 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
| 309 | if (number_of_symbol_keys > 0) { |
| 310 | int index = 0; |
| 311 | for (int p = 0; p < properties_length; p += 2) { |
| 312 | Object* key = constant_properties->get(p); |
| 313 | if (key->IsSymbol()) { |
| 314 | keys->set(index++, key); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 315 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 316 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 317 | ASSERT(index == number_of_symbol_keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 318 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 319 | *is_result_from_cache = true; |
| 320 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 321 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 322 | *is_result_from_cache = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 323 | return isolate->factory()->CopyMap( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 324 | Handle<Map>(context->object_function()->initial_map()), |
| 325 | number_of_properties); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 329 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 330 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 331 | Handle<FixedArray> literals, |
| 332 | Handle<FixedArray> constant_properties); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 333 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 334 | |
| 335 | static Handle<Object> CreateObjectLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 336 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 337 | Handle<FixedArray> literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 338 | Handle<FixedArray> constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 339 | bool should_have_fast_elements, |
| 340 | bool has_function_literal) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 341 | // Get the global context from the literals array. This is the |
| 342 | // context in which the function was created and we use the object |
| 343 | // function from this context to create the object literal. We do |
| 344 | // not use the object function from the current global context |
| 345 | // because this might be the object function from another context |
| 346 | // which we should not have access to. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 347 | Handle<Context> context = |
| 348 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 349 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 350 | // In case we have function literals, we want the object to be in |
| 351 | // slow properties mode for now. We don't go in the map cache because |
| 352 | // maps with constant functions can't be shared if the functions are |
| 353 | // not the same (which is the common case). |
| 354 | bool is_result_from_cache = false; |
| 355 | Handle<Map> map = has_function_literal |
| 356 | ? Handle<Map>(context->object_function()->initial_map()) |
| 357 | : ComputeObjectLiteralMap(context, |
| 358 | constant_properties, |
| 359 | &is_result_from_cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 360 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 361 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 362 | |
| 363 | // Normalize the elements of the boilerplate to save space if needed. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 364 | if (!should_have_fast_elements) JSObject::NormalizeElements(boilerplate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 365 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 366 | // Add the constant properties to the boilerplate. |
| 367 | int length = constant_properties->length(); |
| 368 | bool should_transform = |
| 369 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 370 | if (should_transform || has_function_literal) { |
| 371 | // Normalize the properties of object to avoid n^2 behavior |
| 372 | // when extending the object multiple properties. Indicate the number of |
| 373 | // properties to be added. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 374 | JSObject::NormalizeProperties( |
| 375 | boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | for (int index = 0; index < length; index +=2) { |
| 379 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 380 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 381 | if (value->IsFixedArray()) { |
| 382 | // The value contains the constant_properties of a |
| 383 | // simple object or array literal. |
| 384 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 385 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 386 | if (value.is_null()) return value; |
| 387 | } |
| 388 | Handle<Object> result; |
| 389 | uint32_t element_index = 0; |
| 390 | if (key->IsSymbol()) { |
| 391 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 392 | // Array index as string (uint32). |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 393 | result = JSObject::SetOwnElement( |
| 394 | boilerplate, element_index, value, kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 395 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 396 | Handle<String> name(String::cast(*key)); |
| 397 | ASSERT(!name->AsArrayIndex(&element_index)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 398 | result = JSObject::SetLocalPropertyIgnoreAttributes( |
| 399 | boilerplate, name, value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 400 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 401 | } else if (key->ToArrayIndex(&element_index)) { |
| 402 | // Array index (uint32). |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 403 | result = JSObject::SetOwnElement( |
| 404 | boilerplate, element_index, value, kNonStrictMode); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 405 | } else { |
| 406 | // Non-uint32 number. |
| 407 | ASSERT(key->IsNumber()); |
| 408 | double num = key->Number(); |
| 409 | char arr[100]; |
| 410 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 411 | const char* str = DoubleToCString(num, buffer); |
| 412 | Handle<String> name = |
| 413 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 414 | result = JSObject::SetLocalPropertyIgnoreAttributes( |
| 415 | boilerplate, name, value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 417 | // If setting the property on the boilerplate throws an |
| 418 | // exception, the exception is converted to an empty handle in |
| 419 | // the handle based operations. In that case, we need to |
| 420 | // convert back to an exception. |
| 421 | if (result.is_null()) return result; |
| 422 | } |
| 423 | |
| 424 | // Transform to fast properties if necessary. For object literals with |
| 425 | // containing function literals we defer this operation until after all |
| 426 | // computed properties have been assigned so that we can generate |
| 427 | // constant function properties. |
| 428 | if (should_transform && !has_function_literal) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 429 | JSObject::TransformToFastProperties( |
| 430 | boilerplate, boilerplate->map()->unused_property_fields()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 431 | } |
| 432 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 433 | return boilerplate; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 437 | MaybeObject* TransitionElements(Handle<Object> object, |
| 438 | ElementsKind to_kind, |
| 439 | Isolate* isolate) { |
| 440 | HandleScope scope(isolate); |
| 441 | if (!object->IsJSObject()) return isolate->ThrowIllegalOperation(); |
| 442 | ElementsKind from_kind = |
| 443 | Handle<JSObject>::cast(object)->map()->elements_kind(); |
| 444 | if (Map::IsValidElementsTransition(from_kind, to_kind)) { |
| 445 | Handle<Object> result = JSObject::TransitionElementsKind( |
| 446 | Handle<JSObject>::cast(object), to_kind); |
| 447 | if (result.is_null()) return isolate->ThrowIllegalOperation(); |
| 448 | return *result; |
| 449 | } |
| 450 | return isolate->ThrowIllegalOperation(); |
| 451 | } |
| 452 | |
| 453 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 454 | static const int kSmiOnlyLiteralMinimumLength = 1024; |
| 455 | |
| 456 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 457 | Handle<Object> Runtime::CreateArrayLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 458 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 459 | Handle<FixedArray> literals, |
| 460 | Handle<FixedArray> elements) { |
| 461 | // Create the JSArray. |
| 462 | Handle<JSFunction> constructor( |
| 463 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 464 | Handle<JSArray> object = |
| 465 | Handle<JSArray>::cast(isolate->factory()->NewJSObject(constructor)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 466 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 467 | ElementsKind constant_elements_kind = |
| 468 | static_cast<ElementsKind>(Smi::cast(elements->get(0))->value()); |
| 469 | Handle<FixedArrayBase> constant_elements_values( |
| 470 | FixedArrayBase::cast(elements->get(1))); |
| 471 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 472 | Context* global_context = isolate->context()->global_context(); |
| 473 | if (constant_elements_kind == FAST_SMI_ONLY_ELEMENTS) { |
| 474 | object->set_map(Map::cast(global_context->smi_js_array_map())); |
| 475 | } else if (constant_elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 476 | object->set_map(Map::cast(global_context->double_js_array_map())); |
| 477 | } else { |
| 478 | object->set_map(Map::cast(global_context->object_js_array_map())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 479 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 480 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 481 | Handle<FixedArrayBase> copied_elements_values; |
| 482 | if (constant_elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 483 | ASSERT(FLAG_smi_only_arrays); |
| 484 | copied_elements_values = isolate->factory()->CopyFixedDoubleArray( |
| 485 | Handle<FixedDoubleArray>::cast(constant_elements_values)); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 486 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 487 | ASSERT(constant_elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 488 | constant_elements_kind == FAST_ELEMENTS); |
| 489 | const bool is_cow = |
| 490 | (constant_elements_values->map() == |
| 491 | isolate->heap()->fixed_cow_array_map()); |
| 492 | if (is_cow) { |
| 493 | copied_elements_values = constant_elements_values; |
| 494 | #if DEBUG |
| 495 | Handle<FixedArray> fixed_array_values = |
| 496 | Handle<FixedArray>::cast(copied_elements_values); |
| 497 | for (int i = 0; i < fixed_array_values->length(); i++) { |
| 498 | ASSERT(!fixed_array_values->get(i)->IsFixedArray()); |
| 499 | } |
| 500 | #endif |
| 501 | } else { |
| 502 | Handle<FixedArray> fixed_array_values = |
| 503 | Handle<FixedArray>::cast(constant_elements_values); |
| 504 | Handle<FixedArray> fixed_array_values_copy = |
| 505 | isolate->factory()->CopyFixedArray(fixed_array_values); |
| 506 | copied_elements_values = fixed_array_values_copy; |
| 507 | for (int i = 0; i < fixed_array_values->length(); i++) { |
| 508 | Object* current = fixed_array_values->get(i); |
| 509 | if (current->IsFixedArray()) { |
| 510 | // The value contains the constant_properties of a |
| 511 | // simple object or array literal. |
| 512 | Handle<FixedArray> fa(FixedArray::cast(fixed_array_values->get(i))); |
| 513 | Handle<Object> result = |
| 514 | CreateLiteralBoilerplate(isolate, literals, fa); |
| 515 | if (result.is_null()) return result; |
| 516 | fixed_array_values_copy->set(i, *result); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 517 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 518 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 521 | object->set_elements(*copied_elements_values); |
| 522 | object->set_length(Smi::FromInt(copied_elements_values->length())); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 523 | |
| 524 | // Ensure that the boilerplate object has FAST_ELEMENTS, unless the flag is |
| 525 | // on or the object is larger than the threshold. |
| 526 | if (!FLAG_smi_only_arrays && |
| 527 | constant_elements_values->length() < kSmiOnlyLiteralMinimumLength) { |
| 528 | if (object->GetElementsKind() != FAST_ELEMENTS) { |
| 529 | CHECK(!TransitionElements(object, FAST_ELEMENTS, isolate)->IsFailure()); |
| 530 | } |
| 531 | } |
| 532 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 533 | return object; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 538 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 539 | Handle<FixedArray> literals, |
| 540 | Handle<FixedArray> array) { |
| 541 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 542 | const bool kHasNoFunctionLiteral = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 543 | switch (CompileTimeValue::GetType(array)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 544 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 545 | return CreateObjectLiteralBoilerplate(isolate, |
| 546 | literals, |
| 547 | elements, |
| 548 | true, |
| 549 | kHasNoFunctionLiteral); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 550 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 551 | return CreateObjectLiteralBoilerplate(isolate, |
| 552 | literals, |
| 553 | elements, |
| 554 | false, |
| 555 | kHasNoFunctionLiteral); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 556 | case CompileTimeValue::ARRAY_LITERAL: |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 557 | return Runtime::CreateArrayLiteralBoilerplate( |
| 558 | isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 559 | default: |
| 560 | UNREACHABLE(); |
| 561 | return Handle<Object>::null(); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 566 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 567 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 568 | ASSERT(args.length() == 4); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 569 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 570 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 571 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, constant_properties, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 572 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 573 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 574 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 575 | |
| 576 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 577 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 578 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 579 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 580 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 581 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 582 | should_have_fast_elements, |
| 583 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 584 | if (boilerplate.is_null()) return Failure::Exception(); |
| 585 | // Update the functions literal and return the boilerplate. |
| 586 | literals->set(literals_index, *boilerplate); |
| 587 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 588 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 592 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 593 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 594 | ASSERT(args.length() == 4); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 595 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 596 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 597 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, constant_properties, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 598 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 599 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 600 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 601 | |
| 602 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 603 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 604 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 605 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 606 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 607 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 608 | should_have_fast_elements, |
| 609 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 610 | if (boilerplate.is_null()) return Failure::Exception(); |
| 611 | // Update the functions literal and return the boilerplate. |
| 612 | literals->set(literals_index, *boilerplate); |
| 613 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 614 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 618 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 619 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 620 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 621 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 622 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 623 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, elements, 2); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 624 | |
| 625 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 626 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 627 | if (*boilerplate == isolate->heap()->undefined_value()) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 628 | boilerplate = |
| 629 | Runtime::CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 630 | if (boilerplate.is_null()) return Failure::Exception(); |
| 631 | // Update the functions literal and return the boilerplate. |
| 632 | literals->set(literals_index, *boilerplate); |
| 633 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 634 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 638 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 639 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 640 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 641 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 642 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 643 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, elements, 2); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 644 | |
| 645 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 646 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 647 | if (*boilerplate == isolate->heap()->undefined_value()) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 648 | ASSERT(*elements != isolate->heap()->empty_fixed_array()); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 649 | boilerplate = |
| 650 | Runtime::CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 651 | if (boilerplate.is_null()) return Failure::Exception(); |
| 652 | // Update the functions literal and return the boilerplate. |
| 653 | literals->set(literals_index, *boilerplate); |
| 654 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 655 | if (JSObject::cast(*boilerplate)->elements()->map() == |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 656 | isolate->heap()->fixed_cow_array_map()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 657 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 658 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 659 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 663 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSProxy) { |
| 664 | ASSERT(args.length() == 2); |
| 665 | Object* handler = args[0]; |
| 666 | Object* prototype = args[1]; |
| 667 | Object* used_prototype = |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 668 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 669 | return isolate->heap()->AllocateJSProxy(handler, used_prototype); |
| 670 | } |
| 671 | |
| 672 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 673 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSFunctionProxy) { |
| 674 | ASSERT(args.length() == 4); |
| 675 | Object* handler = args[0]; |
| 676 | Object* call_trap = args[1]; |
| 677 | Object* construct_trap = args[2]; |
| 678 | Object* prototype = args[3]; |
| 679 | Object* used_prototype = |
| 680 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
| 681 | return isolate->heap()->AllocateJSFunctionProxy( |
| 682 | handler, call_trap, construct_trap, used_prototype); |
| 683 | } |
| 684 | |
| 685 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 686 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSProxy) { |
| 687 | ASSERT(args.length() == 1); |
| 688 | Object* obj = args[0]; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 689 | return isolate->heap()->ToBoolean(obj->IsJSProxy()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 693 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSFunctionProxy) { |
| 694 | ASSERT(args.length() == 1); |
| 695 | Object* obj = args[0]; |
| 696 | return isolate->heap()->ToBoolean(obj->IsJSFunctionProxy()); |
| 697 | } |
| 698 | |
| 699 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 700 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHandler) { |
| 701 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 702 | CONVERT_ARG_CHECKED(JSProxy, proxy, 0); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 703 | return proxy->handler(); |
| 704 | } |
| 705 | |
| 706 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 707 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetCallTrap) { |
| 708 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 709 | CONVERT_ARG_CHECKED(JSFunctionProxy, proxy, 0); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 710 | return proxy->call_trap(); |
| 711 | } |
| 712 | |
| 713 | |
| 714 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructTrap) { |
| 715 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 716 | CONVERT_ARG_CHECKED(JSFunctionProxy, proxy, 0); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 717 | return proxy->construct_trap(); |
| 718 | } |
| 719 | |
| 720 | |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 721 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { |
| 722 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 723 | CONVERT_ARG_CHECKED(JSProxy, proxy, 0); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 724 | proxy->Fix(); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 725 | return isolate->heap()->undefined_value(); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 729 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { |
| 730 | HandleScope scope(isolate); |
| 731 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 732 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 733 | Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); |
| 734 | holder->set_table(*table); |
| 735 | return *holder; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetAdd) { |
| 740 | HandleScope scope(isolate); |
| 741 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 742 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 743 | Handle<Object> key(args[1]); |
| 744 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 745 | table = ObjectHashSetAdd(table, key); |
| 746 | holder->set_table(*table); |
| 747 | return isolate->heap()->undefined_symbol(); |
| 748 | } |
| 749 | |
| 750 | |
| 751 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetHas) { |
| 752 | HandleScope scope(isolate); |
| 753 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 754 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 755 | Handle<Object> key(args[1]); |
| 756 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 757 | return isolate->heap()->ToBoolean(table->Contains(*key)); |
| 758 | } |
| 759 | |
| 760 | |
| 761 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDelete) { |
| 762 | HandleScope scope(isolate); |
| 763 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 764 | CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 765 | Handle<Object> key(args[1]); |
| 766 | Handle<ObjectHashSet> table(ObjectHashSet::cast(holder->table())); |
| 767 | table = ObjectHashSetRemove(table, key); |
| 768 | holder->set_table(*table); |
| 769 | return isolate->heap()->undefined_symbol(); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapInitialize) { |
| 774 | HandleScope scope(isolate); |
| 775 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 776 | CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 777 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 778 | holder->set_table(*table); |
| 779 | return *holder; |
| 780 | } |
| 781 | |
| 782 | |
| 783 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapGet) { |
| 784 | HandleScope scope(isolate); |
| 785 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 786 | CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 787 | Handle<Object> key(args[1]); |
| 788 | return ObjectHashTable::cast(holder->table())->Lookup(*key); |
| 789 | } |
| 790 | |
| 791 | |
| 792 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MapSet) { |
| 793 | HandleScope scope(isolate); |
| 794 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 795 | CONVERT_ARG_HANDLE_CHECKED(JSMap, holder, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 796 | Handle<Object> key(args[1]); |
| 797 | Handle<Object> value(args[2]); |
| 798 | Handle<ObjectHashTable> table(ObjectHashTable::cast(holder->table())); |
| 799 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 800 | holder->set_table(*new_table); |
| 801 | return *value; |
| 802 | } |
| 803 | |
| 804 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 805 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapInitialize) { |
| 806 | HandleScope scope(isolate); |
| 807 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 808 | CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 809 | ASSERT(weakmap->map()->inobject_properties() == 0); |
| 810 | Handle<ObjectHashTable> table = isolate->factory()->NewObjectHashTable(0); |
| 811 | weakmap->set_table(*table); |
| 812 | weakmap->set_next(Smi::FromInt(0)); |
| 813 | return *weakmap; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapGet) { |
| 818 | NoHandleAllocation ha; |
| 819 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 820 | CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); |
| 821 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 822 | return ObjectHashTable::cast(weakmap->table())->Lookup(*key); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | |
| 826 | RUNTIME_FUNCTION(MaybeObject*, Runtime_WeakMapSet) { |
| 827 | HandleScope scope(isolate); |
| 828 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 829 | CONVERT_ARG_HANDLE_CHECKED(JSWeakMap, weakmap, 0); |
| 830 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, key, 1); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 831 | Handle<Object> value(args[2]); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 832 | Handle<ObjectHashTable> table(ObjectHashTable::cast(weakmap->table())); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 833 | Handle<ObjectHashTable> new_table = PutIntoObjectHashTable(table, key, value); |
| 834 | weakmap->set_table(*new_table); |
| 835 | return *value; |
| 836 | } |
| 837 | |
| 838 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 839 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClassOf) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | NoHandleAllocation ha; |
| 841 | ASSERT(args.length() == 1); |
| 842 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 843 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 844 | return JSObject::cast(obj)->class_name(); |
| 845 | } |
| 846 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 847 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 848 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPrototype) { |
| 849 | NoHandleAllocation ha; |
| 850 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 851 | CONVERT_ARG_CHECKED(JSReceiver, input_obj, 0); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 852 | Object* obj = input_obj; |
| 853 | // We don't expect access checks to be needed on JSProxy objects. |
| 854 | ASSERT(!obj->IsAccessCheckNeeded() || obj->IsJSObject()); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 855 | do { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 856 | if (obj->IsAccessCheckNeeded() && |
| 857 | !isolate->MayNamedAccess(JSObject::cast(obj), |
| 858 | isolate->heap()->Proto_symbol(), |
| 859 | v8::ACCESS_GET)) { |
| 860 | isolate->ReportFailedAccessCheck(JSObject::cast(obj), v8::ACCESS_GET); |
| 861 | return isolate->heap()->undefined_value(); |
| 862 | } |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 863 | obj = obj->GetPrototype(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 864 | } while (obj->IsJSObject() && |
| 865 | JSObject::cast(obj)->map()->is_hidden_prototype()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 866 | return obj; |
| 867 | } |
| 868 | |
| 869 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 870 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsInPrototypeChain) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 871 | NoHandleAllocation ha; |
| 872 | ASSERT(args.length() == 2); |
| 873 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 874 | Object* O = args[0]; |
| 875 | Object* V = args[1]; |
| 876 | while (true) { |
| 877 | Object* prototype = V->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 878 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 879 | if (O == prototype) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 880 | V = prototype; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 885 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsConstructCall) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 886 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 887 | ASSERT(args.length() == 0); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 888 | JavaScriptFrameIterator it(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 889 | return isolate->heap()->ToBoolean(it.frame()->IsConstructor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 893 | // Recursively traverses hidden prototypes if property is not found |
| 894 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 895 | String* name, |
| 896 | LookupResult* result) { |
| 897 | obj->LocalLookupRealNamedProperty(name, result); |
| 898 | |
| 899 | if (!result->IsProperty()) { |
| 900 | Object* proto = obj->GetPrototype(); |
| 901 | if (proto->IsJSObject() && |
| 902 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 903 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 904 | name, result); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 909 | static bool CheckAccessException(LookupResult* result, |
| 910 | v8::AccessType access_type) { |
| 911 | if (result->type() == CALLBACKS) { |
| 912 | Object* callback = result->GetCallbackObject(); |
| 913 | if (callback->IsAccessorInfo()) { |
| 914 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 915 | bool can_access = |
| 916 | (access_type == v8::ACCESS_HAS && |
| 917 | (info->all_can_read() || info->all_can_write())) || |
| 918 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 919 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 920 | return can_access; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | |
| 928 | static bool CheckAccess(JSObject* obj, |
| 929 | String* name, |
| 930 | LookupResult* result, |
| 931 | v8::AccessType access_type) { |
| 932 | ASSERT(result->IsProperty()); |
| 933 | |
| 934 | JSObject* holder = result->holder(); |
| 935 | JSObject* current = obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 936 | Isolate* isolate = obj->GetIsolate(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 937 | while (true) { |
| 938 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 939 | !isolate->MayNamedAccess(current, name, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 940 | // Access check callback denied the access, but some properties |
| 941 | // can have a special permissions which override callbacks descision |
| 942 | // (currently see v8::AccessControl). |
| 943 | break; |
| 944 | } |
| 945 | |
| 946 | if (current == holder) { |
| 947 | return true; |
| 948 | } |
| 949 | |
| 950 | current = JSObject::cast(current->GetPrototype()); |
| 951 | } |
| 952 | |
| 953 | // API callbacks can have per callback access exceptions. |
| 954 | switch (result->type()) { |
| 955 | case CALLBACKS: { |
| 956 | if (CheckAccessException(result, access_type)) { |
| 957 | return true; |
| 958 | } |
| 959 | break; |
| 960 | } |
| 961 | case INTERCEPTOR: { |
| 962 | // If the object has an interceptor, try real named properties. |
| 963 | // Overwrite the result to fetch the correct property later. |
| 964 | holder->LookupRealNamedProperty(name, result); |
| 965 | if (result->IsProperty()) { |
| 966 | if (CheckAccessException(result, access_type)) { |
| 967 | return true; |
| 968 | } |
| 969 | } |
| 970 | break; |
| 971 | } |
| 972 | default: |
| 973 | break; |
| 974 | } |
| 975 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 976 | isolate->ReportFailedAccessCheck(current, access_type); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 977 | return false; |
| 978 | } |
| 979 | |
| 980 | |
| 981 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 982 | static bool CheckElementAccess(JSObject* obj, |
| 983 | uint32_t index, |
| 984 | v8::AccessType access_type) { |
| 985 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 986 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 987 | return false; |
| 988 | } |
| 989 | |
| 990 | return true; |
| 991 | } |
| 992 | |
| 993 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 994 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 995 | enum PropertyDescriptorIndices { |
| 996 | IS_ACCESSOR_INDEX, |
| 997 | VALUE_INDEX, |
| 998 | GETTER_INDEX, |
| 999 | SETTER_INDEX, |
| 1000 | WRITABLE_INDEX, |
| 1001 | ENUMERABLE_INDEX, |
| 1002 | CONFIGURABLE_INDEX, |
| 1003 | DESCRIPTOR_SIZE |
| 1004 | }; |
| 1005 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1006 | // Returns an array with the property description: |
| 1007 | // if args[1] is not a property on args[0] |
| 1008 | // returns undefined |
| 1009 | // if args[1] is a data property on args[0] |
| 1010 | // [false, value, Writeable, Enumerable, Configurable] |
| 1011 | // if args[1] is an accessor on args[0] |
| 1012 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1013 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOwnProperty) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1014 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1015 | Heap* heap = isolate->heap(); |
| 1016 | HandleScope scope(isolate); |
| 1017 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 1018 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1019 | LookupResult result(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1020 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 1021 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1022 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1023 | // This could be an element. |
| 1024 | uint32_t index; |
| 1025 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1026 | switch (obj->HasLocalElement(index)) { |
| 1027 | case JSObject::UNDEFINED_ELEMENT: |
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 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1030 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 1031 | // Special handling of string objects according to ECMAScript 5 |
| 1032 | // 15.5.5.2. Note that this might be a string object with elements |
| 1033 | // other than the actual string value. This is covered by the |
| 1034 | // subsequent cases. |
| 1035 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 1036 | Handle<String> str(String::cast(js_value->value())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1037 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1038 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1039 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1040 | elms->set(VALUE_INDEX, *substr); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1041 | elms->set(WRITABLE_INDEX, heap->false_value()); |
| 1042 | elms->set(ENUMERABLE_INDEX, heap->false_value()); |
| 1043 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1044 | return *desc; |
| 1045 | } |
| 1046 | |
| 1047 | case JSObject::INTERCEPTED_ELEMENT: |
| 1048 | case JSObject::FAST_ELEMENT: { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1049 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1050 | Handle<Object> value = Object::GetElement(obj, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1051 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1052 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1053 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 1054 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 1055 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1056 | return *desc; |
| 1057 | } |
| 1058 | |
| 1059 | case JSObject::DICTIONARY_ELEMENT: { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1060 | Handle<JSObject> holder = obj; |
| 1061 | if (obj->IsJSGlobalProxy()) { |
| 1062 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1063 | if (proto->IsNull()) return heap->undefined_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1064 | ASSERT(proto->IsJSGlobalObject()); |
| 1065 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 1066 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1067 | FixedArray* elements = FixedArray::cast(holder->elements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1068 | SeededNumberDictionary* dictionary = NULL; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1069 | if (elements->map() == heap->non_strict_arguments_elements_map()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1070 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1071 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1072 | dictionary = SeededNumberDictionary::cast(elements); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1073 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1074 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1075 | ASSERT(entry != SeededNumberDictionary::kNotFound); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1076 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 1077 | switch (details.type()) { |
| 1078 | case CALLBACKS: { |
| 1079 | // This is an accessor property with getter and/or setter. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1080 | AccessorPair* accessors = |
| 1081 | AccessorPair::cast(dictionary->ValueAt(entry)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1082 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1083 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1084 | elms->set(GETTER_INDEX, accessors->getter()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1085 | } |
| 1086 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1087 | elms->set(SETTER_INDEX, accessors->setter()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1088 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1089 | break; |
| 1090 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1091 | case NORMAL: { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1092 | // This is a data property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1093 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1094 | Handle<Object> value = Object::GetElement(obj, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1095 | ASSERT(!value.is_null()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1096 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1097 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1098 | break; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1099 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1100 | default: |
| 1101 | UNREACHABLE(); |
| 1102 | break; |
| 1103 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1104 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 1105 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1106 | return *desc; |
| 1107 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1111 | // Use recursive implementation to also traverse hidden prototypes |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1112 | GetOwnPropertyImplementation(*obj, *name, &result); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1113 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1114 | if (!result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1115 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1116 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1117 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1118 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1119 | return heap->false_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1122 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 1123 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1124 | |
| 1125 | bool is_js_accessor = (result.type() == CALLBACKS) && |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1126 | (result.GetCallbackObject()->IsAccessorPair()); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1127 | |
| 1128 | if (is_js_accessor) { |
| 1129 | // __defineGetter__/__defineSetter__ callback. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1130 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1131 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1132 | AccessorPair* accessors = AccessorPair::cast(result.GetCallbackObject()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1133 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1134 | elms->set(GETTER_INDEX, accessors->getter()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1135 | } |
| 1136 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1137 | elms->set(SETTER_INDEX, accessors->setter()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1138 | } |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1139 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1140 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 1141 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1142 | |
| 1143 | PropertyAttributes attrs; |
| 1144 | Object* value; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 1145 | // GetProperty will check access and report any violations. |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 1146 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 1147 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 1148 | } |
| 1149 | elms->set(VALUE_INDEX, value); |
| 1150 | } |
| 1151 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1152 | return *desc; |
| 1153 | } |
| 1154 | |
| 1155 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1156 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PreventExtensions) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1157 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1158 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1159 | return obj->PreventExtensions(); |
| 1160 | } |
| 1161 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1162 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1163 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsExtensible) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1164 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1165 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1166 | if (obj->IsJSGlobalProxy()) { |
| 1167 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1168 | if (proto->IsNull()) return isolate->heap()->false_value(); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1169 | ASSERT(proto->IsJSGlobalObject()); |
| 1170 | obj = JSObject::cast(proto); |
| 1171 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1172 | return isolate->heap()->ToBoolean(obj->map()->is_extensible()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1176 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1177 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1178 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1179 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, re, 0); |
| 1180 | CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1); |
| 1181 | CONVERT_ARG_HANDLE_CHECKED(String, flags, 2); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 1182 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 1183 | if (result.is_null()) return Failure::Exception(); |
| 1184 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1188 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateApiFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1189 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1190 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1191 | CONVERT_ARG_HANDLE_CHECKED(FunctionTemplateInfo, data, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1192 | return *isolate->factory()->CreateApiFunction(data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1196 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsTemplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1197 | ASSERT(args.length() == 1); |
| 1198 | Object* arg = args[0]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1199 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1200 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1204 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetTemplateField) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1205 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1206 | CONVERT_ARG_CHECKED(HeapObject, templ, 0); |
| 1207 | CONVERT_SMI_ARG_CHECKED(index, 1) |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1208 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1209 | InstanceType type = templ->map()->instance_type(); |
| 1210 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1211 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1212 | RUNTIME_ASSERT(offset > 0); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1213 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1214 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1215 | } else { |
| 1216 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1217 | } |
| 1218 | return *HeapObject::RawField(templ, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1222 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DisableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1223 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1224 | CONVERT_ARG_CHECKED(HeapObject, object, 0); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1225 | Map* old_map = object->map(); |
| 1226 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1227 | if (needs_access_checks) { |
| 1228 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1229 | Object* new_map; |
| 1230 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1231 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1232 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1233 | |
| 1234 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1235 | object->set_map(Map::cast(new_map)); |
| 1236 | } |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1237 | return isolate->heap()->ToBoolean(needs_access_checks); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1241 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EnableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1242 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1243 | CONVERT_ARG_CHECKED(HeapObject, object, 0); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1244 | Map* old_map = object->map(); |
| 1245 | if (!old_map->is_access_check_needed()) { |
| 1246 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1247 | Object* new_map; |
| 1248 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1249 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1250 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1251 | |
| 1252 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1253 | object->set_map(Map::cast(new_map)); |
| 1254 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1255 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1259 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1260 | const char* type, |
| 1261 | Handle<String> name) { |
| 1262 | HandleScope scope(isolate); |
| 1263 | Handle<Object> type_handle = |
| 1264 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1265 | Handle<Object> args[2] = { type_handle, name }; |
| 1266 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1267 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1268 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1272 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareGlobals) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1273 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1274 | HandleScope scope(isolate); |
| 1275 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1276 | isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1277 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1278 | Handle<Context> context = args.at<Context>(0); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1279 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, pairs, 1); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1280 | CONVERT_SMI_ARG_CHECKED(flags, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1281 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1282 | // Traverse the name/value pairs and set the properties. |
| 1283 | int length = pairs->length(); |
| 1284 | for (int i = 0; i < length; i += 2) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1285 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | Handle<String> name(String::cast(pairs->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1287 | Handle<Object> value(pairs->get(i + 1), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1288 | |
| 1289 | // We have to declare a global const property. To capture we only |
| 1290 | // assign to it when evaluating the assignment for "const x = |
| 1291 | // <expr>" the initial value is the hole. |
| 1292 | bool is_const_property = value->IsTheHole(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1293 | bool is_function_declaration = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1294 | if (value->IsUndefined() || is_const_property) { |
| 1295 | // Lookup the property in the global object, and don't set the |
| 1296 | // value of the variable if the property is already there. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1297 | LookupResult lookup(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1298 | global->Lookup(*name, &lookup); |
| 1299 | if (lookup.IsProperty()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1300 | // We found an existing property. Unless it was an interceptor |
| 1301 | // that claims the property is absent, skip this declaration. |
| 1302 | if (lookup.type() != INTERCEPTOR) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1303 | continue; |
| 1304 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1305 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1306 | if (attributes != ABSENT) { |
| 1307 | continue; |
| 1308 | } |
| 1309 | // Fall-through and introduce the absent property by using |
| 1310 | // SetProperty. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1311 | } |
| 1312 | } else { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1313 | is_function_declaration = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1314 | // Copy the function and update its context. Use it as value. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1315 | Handle<SharedFunctionInfo> shared = |
| 1316 | Handle<SharedFunctionInfo>::cast(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1317 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1318 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1319 | context, |
| 1320 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1321 | value = function; |
| 1322 | } |
| 1323 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1324 | LookupResult lookup(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1325 | global->LocalLookup(*name, &lookup); |
| 1326 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1327 | // Compute the property attributes. According to ECMA-262, section |
| 1328 | // 13, page 71, the property must be read-only and |
| 1329 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1330 | // property as read-only, so we don't either. |
| 1331 | int attr = NONE; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1332 | if (!DeclareGlobalsEvalFlag::decode(flags)) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1333 | attr |= DONT_DELETE; |
| 1334 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1335 | bool is_native = DeclareGlobalsNativeFlag::decode(flags); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1336 | if (is_const_property || (is_native && is_function_declaration)) { |
| 1337 | attr |= READ_ONLY; |
| 1338 | } |
| 1339 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1340 | // Safari does not allow the invocation of callback setters for |
| 1341 | // function declarations. To mimic this behavior, we do not allow |
| 1342 | // the invocation of setters for function values. This makes a |
| 1343 | // difference for global functions with the same names as event |
| 1344 | // handlers such as "function onload() {}". Firefox does call the |
| 1345 | // onload setter in those case and Safari does not. We follow |
| 1346 | // Safari for compatibility. |
| 1347 | if (value->IsJSFunction()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1348 | // Do not change DONT_DELETE to false from true. |
| 1349 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1350 | attr |= lookup.GetAttributes() & DONT_DELETE; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1351 | } |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 1352 | PropertyAttributes attributes = static_cast<PropertyAttributes>(attr); |
| 1353 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1354 | RETURN_IF_EMPTY_HANDLE( |
| 1355 | isolate, |
| 1356 | JSObject::SetLocalPropertyIgnoreAttributes(global, name, value, |
| 1357 | attributes)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1358 | } else { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1359 | LanguageMode language_mode = DeclareGlobalsLanguageMode::decode(flags); |
| 1360 | StrictModeFlag strict_mode_flag = (language_mode == CLASSIC_MODE) |
| 1361 | ? kNonStrictMode : kStrictMode; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1362 | RETURN_IF_EMPTY_HANDLE( |
| 1363 | isolate, |
| 1364 | JSReceiver::SetProperty(global, name, value, |
| 1365 | static_cast<PropertyAttributes>(attr), |
| 1366 | strict_mode_flag)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1369 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1370 | ASSERT(!isolate->has_pending_exception()); |
| 1371 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1375 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1376 | HandleScope scope(isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1377 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1378 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1379 | // Declarations are always made in a function or global context. In the |
| 1380 | // case of eval code, the context passed is the context of the caller, |
| 1381 | // which may be some nested context and not the declaration context. |
| 1382 | RUNTIME_ASSERT(args[0]->IsContext()); |
| 1383 | Handle<Context> context(Context::cast(args[0])->declaration_context()); |
| 1384 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1385 | Handle<String> name(String::cast(args[1])); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1386 | PropertyAttributes mode = static_cast<PropertyAttributes>(args.smi_at(2)); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1387 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1388 | Handle<Object> initial_value(args[3], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1389 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1390 | int index; |
| 1391 | PropertyAttributes attributes; |
| 1392 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1393 | BindingFlags binding_flags; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1394 | Handle<Object> holder = |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1395 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (attributes != ABSENT) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1398 | // The name was declared before; check for conflicting re-declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1399 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1400 | // Functions are not read-only. |
| 1401 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1402 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1403 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | // Initialize it if necessary. |
| 1407 | if (*initial_value != NULL) { |
| 1408 | if (index >= 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1409 | ASSERT(holder.is_identical_to(context)); |
| 1410 | if (((attributes & READ_ONLY) == 0) || |
| 1411 | context->get(index)->IsTheHole()) { |
| 1412 | context->set(index, *initial_value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1413 | } |
| 1414 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1415 | // Slow case: The property is in the context extension object of a |
| 1416 | // function context or the global object of a global context. |
| 1417 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1418 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1419 | isolate, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1420 | JSReceiver::SetProperty(object, name, initial_value, mode, |
| 1421 | kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1424 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1426 | // The property is not in the function context. It needs to be |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1427 | // "declared" in the function context's extension context or as a |
| 1428 | // property of the the global object. |
| 1429 | Handle<JSObject> object; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1430 | if (context->has_extension()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1431 | object = Handle<JSObject>(JSObject::cast(context->extension())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1432 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1433 | // Context extension objects are allocated lazily. |
| 1434 | ASSERT(context->IsFunctionContext()); |
| 1435 | object = isolate->factory()->NewJSObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1436 | isolate->context_extension_function()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1437 | context->set_extension(*object); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1438 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1439 | ASSERT(*object != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1440 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1441 | // Declare the property by setting it to the initial value if provided, |
| 1442 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1443 | // constant declarations). |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1444 | ASSERT(!object->HasLocalProperty(*name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1445 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1446 | if (*initial_value != NULL) value = initial_value; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1447 | // Declaring a const context slot is a conflicting declaration if |
| 1448 | // there is a callback with that name in a prototype. It is |
| 1449 | // allowed to introduce const variables in |
| 1450 | // JSContextExtensionObjects. They are treated specially in |
| 1451 | // SetProperty and no setters are invoked for those since they are |
| 1452 | // not real JSObjects. |
| 1453 | if (initial_value->IsTheHole() && |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1454 | !object->IsJSContextExtensionObject()) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1455 | LookupResult lookup(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1456 | object->Lookup(*name, &lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1457 | if (lookup.IsFound() && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1458 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1461 | RETURN_IF_EMPTY_HANDLE( |
| 1462 | isolate, |
| 1463 | JSReceiver::SetProperty(object, name, value, mode, kNonStrictMode)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1466 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1470 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeVarGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | NoHandleAllocation nha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1472 | // args[0] == name |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1473 | // args[1] == language_mode |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1474 | // args[2] == value (optional) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Determine if we need to assign to the variable if it already |
| 1477 | // exists (based on the number of arguments). |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1478 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1479 | bool assign = args.length() == 3; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1480 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1481 | CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1482 | GlobalObject* global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1483 | RUNTIME_ASSERT(args[1]->IsSmi()); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1484 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 1); |
| 1485 | StrictModeFlag strict_mode_flag = (language_mode == CLASSIC_MODE) |
| 1486 | ? kNonStrictMode : kStrictMode; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1487 | |
| 1488 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1489 | // not be deletable. |
| 1490 | PropertyAttributes attributes = DONT_DELETE; |
| 1491 | |
| 1492 | // 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] | 1493 | // there, there is a property with this name in the prototype chain. |
| 1494 | // We follow Safari and Firefox behavior and only set the property |
| 1495 | // locally if there is an explicit initialization value that we have |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1496 | // to assign to the property. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1497 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1498 | // the whole chain of hidden prototypes to do a 'local' lookup. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1499 | Object* object = global; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1500 | LookupResult lookup(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1501 | while (object->IsJSObject() && |
| 1502 | JSObject::cast(object)->map()->is_hidden_prototype()) { |
| 1503 | JSObject* raw_holder = JSObject::cast(object); |
| 1504 | raw_holder->LocalLookup(*name, &lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1505 | if (lookup.IsFound() && lookup.type() == INTERCEPTOR) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1506 | HandleScope handle_scope(isolate); |
| 1507 | Handle<JSObject> holder(raw_holder); |
| 1508 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1509 | // Update the raw pointer in case it's changed due to GC. |
| 1510 | raw_holder = *holder; |
| 1511 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
| 1512 | // Found an interceptor that's not read only. |
| 1513 | if (assign) { |
| 1514 | return raw_holder->SetProperty( |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1515 | &lookup, *name, args[2], attributes, strict_mode_flag); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1516 | } else { |
| 1517 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1518 | } |
| 1519 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1520 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1521 | object = raw_holder->GetPrototype(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1524 | // Reload global in case the loop above performed a GC. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1525 | global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1526 | if (assign) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1527 | return global->SetProperty(*name, args[2], attributes, strict_mode_flag); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1528 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1529 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1533 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1534 | // All constants are declared with an initial value. The name |
| 1535 | // of the constant is the first argument and the initial value |
| 1536 | // is the second. |
| 1537 | RUNTIME_ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1538 | CONVERT_ARG_HANDLE_CHECKED(String, name, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1539 | Handle<Object> value = args.at<Object>(1); |
| 1540 | |
| 1541 | // Get the current global object from top. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1542 | GlobalObject* global = isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1543 | |
| 1544 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1545 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1546 | PropertyAttributes attributes = |
| 1547 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1548 | |
| 1549 | // Lookup the property locally in the global object. If it isn't |
| 1550 | // there, we add the property and take special precautions to always |
| 1551 | // add it as a local property even in case of callbacks in the |
| 1552 | // prototype chain (this rules out using SetProperty). |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1553 | // We use SetLocalPropertyIgnoreAttributes instead |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1554 | LookupResult lookup(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1555 | global->LocalLookup(*name, &lookup); |
| 1556 | if (!lookup.IsProperty()) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1557 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1558 | *value, |
| 1559 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1562 | if (!lookup.IsReadOnly()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | // Restore global object from context (in case of GC) and continue |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1564 | // with setting the value. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1565 | HandleScope handle_scope(isolate); |
| 1566 | Handle<GlobalObject> global(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1567 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1568 | // 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] | 1569 | // property through an interceptor and only do it if it's |
| 1570 | // uninitialized, e.g. the hole. Nirk... |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1571 | // Passing non-strict mode because the property is writable. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1572 | RETURN_IF_EMPTY_HANDLE( |
| 1573 | isolate, |
| 1574 | JSReceiver::SetProperty(global, name, value, attributes, |
| 1575 | kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1576 | return *value; |
| 1577 | } |
| 1578 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1579 | // Set the value, but only if we're assigning the initial value to a |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1580 | // constant. For now, we determine this by checking if the |
| 1581 | // current value is the hole. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1582 | // Strict mode handling not needed (const is disallowed in strict mode). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1583 | PropertyType type = lookup.type(); |
| 1584 | if (type == FIELD) { |
| 1585 | FixedArray* properties = global->properties(); |
| 1586 | int index = lookup.GetFieldIndex(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1587 | if (properties->get(index)->IsTheHole() || !lookup.IsReadOnly()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1588 | properties->set(index, *value); |
| 1589 | } |
| 1590 | } else if (type == NORMAL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1591 | if (global->GetNormalizedProperty(&lookup)->IsTheHole() || |
| 1592 | !lookup.IsReadOnly()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1593 | global->SetNormalizedProperty(&lookup, *value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1594 | } |
| 1595 | } else { |
| 1596 | // Ignore re-initialization of constants that have already been |
| 1597 | // assigned a function value. |
| 1598 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1599 | } |
| 1600 | |
| 1601 | // Use the set value as the result of the operation. |
| 1602 | return *value; |
| 1603 | } |
| 1604 | |
| 1605 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1606 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1607 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1608 | ASSERT(args.length() == 3); |
| 1609 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1610 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1611 | ASSERT(!value->IsTheHole()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1612 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1613 | // Initializations are always done in a function or global context. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1614 | RUNTIME_ASSERT(args[1]->IsContext()); |
| 1615 | Handle<Context> context(Context::cast(args[1])->declaration_context()); |
| 1616 | |
| 1617 | Handle<String> name(String::cast(args[2])); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1618 | |
| 1619 | int index; |
| 1620 | PropertyAttributes attributes; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1621 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1622 | BindingFlags binding_flags; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1623 | Handle<Object> holder = |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1624 | context->Lookup(name, flags, &index, &attributes, &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1625 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1626 | if (index >= 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1627 | ASSERT(holder->IsContext()); |
| 1628 | // Property was found in a context. Perform the assignment if we |
| 1629 | // found some non-constant or an uninitialized constant. |
| 1630 | Handle<Context> context = Handle<Context>::cast(holder); |
| 1631 | if ((attributes & READ_ONLY) == 0 || context->get(index)->IsTheHole()) { |
| 1632 | context->set(index, *value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1633 | } |
| 1634 | return *value; |
| 1635 | } |
| 1636 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1637 | // The property could not be found, we introduce it as a property of the |
| 1638 | // global object. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1639 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1640 | Handle<JSObject> global = Handle<JSObject>( |
| 1641 | isolate->context()->global()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1642 | // Strict mode not needed (const disallowed in strict mode). |
| 1643 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1644 | isolate, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1645 | JSReceiver::SetProperty(global, name, value, NONE, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1646 | return *value; |
| 1647 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1648 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1649 | // The property was present in some function's context extension object, |
| 1650 | // as a property on the subject of a with, or as a property of the global |
| 1651 | // object. |
| 1652 | // |
| 1653 | // In most situations, eval-introduced consts should still be present in |
| 1654 | // the context extension object. However, because declaration and |
| 1655 | // initialization are separate, the property might have been deleted |
| 1656 | // before we reach the initialization point. |
| 1657 | // |
| 1658 | // Example: |
| 1659 | // |
| 1660 | // function f() { eval("delete x; const x;"); } |
| 1661 | // |
| 1662 | // In that case, the initialization behaves like a normal assignment. |
| 1663 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1664 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1665 | if (*object == context->extension()) { |
| 1666 | // This is the property that was introduced by the const declaration. |
| 1667 | // Set it if it hasn't been set before. NOTE: We cannot use |
| 1668 | // GetProperty() to get the current value as it 'unholes' the value. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1669 | LookupResult lookup(isolate); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1670 | object->LocalLookupRealNamedProperty(*name, &lookup); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1671 | ASSERT(lookup.IsFound()); // the property was declared |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1672 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1673 | |
| 1674 | PropertyType type = lookup.type(); |
| 1675 | if (type == FIELD) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1676 | FixedArray* properties = object->properties(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1677 | int index = lookup.GetFieldIndex(); |
| 1678 | if (properties->get(index)->IsTheHole()) { |
| 1679 | properties->set(index, *value); |
| 1680 | } |
| 1681 | } else if (type == NORMAL) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1682 | if (object->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1683 | object->SetNormalizedProperty(&lookup, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } else { |
| 1686 | // We should not reach here. Any real, named property should be |
| 1687 | // either a field or a dictionary slot. |
| 1688 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1689 | } |
| 1690 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1691 | // The property was found on some other object. Set it if it is not a |
| 1692 | // read-only property. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1693 | if ((attributes & READ_ONLY) == 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1694 | // Strict mode not needed (const disallowed in strict mode). |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1695 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1696 | isolate, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1697 | JSReceiver::SetProperty(object, name, value, attributes, |
| 1698 | kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1699 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1700 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1701 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1702 | return *value; |
| 1703 | } |
| 1704 | |
| 1705 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1706 | RUNTIME_FUNCTION(MaybeObject*, |
| 1707 | Runtime_OptimizeObjectForAddingMultipleProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1708 | HandleScope scope(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1709 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1710 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1711 | CONVERT_SMI_ARG_CHECKED(properties, 1); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1712 | if (object->HasFastProperties()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1713 | JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1714 | } |
| 1715 | return *object; |
| 1716 | } |
| 1717 | |
| 1718 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1719 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExec) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1720 | HandleScope scope(isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1721 | ASSERT(args.length() == 4); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1722 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); |
| 1723 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1724 | // 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] | 1725 | // 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] | 1726 | CONVERT_SMI_ARG_CHECKED(index, 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1727 | CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1728 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1729 | RUNTIME_ASSERT(index >= 0); |
| 1730 | RUNTIME_ASSERT(index <= subject->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1731 | isolate->counters()->regexp_entry_runtime()->Increment(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1732 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1733 | subject, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1734 | index, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1735 | last_match_info); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1736 | if (result.is_null()) return Failure::Exception(); |
| 1737 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1741 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpConstructResult) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1742 | ASSERT(args.length() == 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1743 | CONVERT_SMI_ARG_CHECKED(elements_count, 0); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 1744 | if (elements_count < 0 || |
| 1745 | elements_count > FixedArray::kMaxLength || |
| 1746 | !Smi::IsValid(elements_count)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1747 | return isolate->ThrowIllegalOperation(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1748 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1749 | Object* new_object; |
| 1750 | { MaybeObject* maybe_new_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1751 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1752 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1753 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1754 | FixedArray* elements = FixedArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1755 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1756 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1757 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1758 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1759 | { |
| 1760 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1761 | HandleScope scope(isolate); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1762 | reinterpret_cast<HeapObject*>(new_object)-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1763 | set_map(isolate->global_context()->regexp_result_map()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1764 | } |
| 1765 | JSArray* array = JSArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1766 | array->set_properties(isolate->heap()->empty_fixed_array()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1767 | array->set_elements(elements); |
| 1768 | array->set_length(Smi::FromInt(elements_count)); |
| 1769 | // Write in-object properties after the length of the array. |
| 1770 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1771 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1772 | return array; |
| 1773 | } |
| 1774 | |
| 1775 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1776 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpInitializeObject) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1777 | AssertNoAllocation no_alloc; |
| 1778 | ASSERT(args.length() == 5); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1779 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1780 | CONVERT_ARG_CHECKED(String, source, 1); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1781 | |
| 1782 | Object* global = args[2]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1783 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1784 | |
| 1785 | Object* ignoreCase = args[3]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1786 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1787 | |
| 1788 | Object* multiline = args[4]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1789 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1790 | |
| 1791 | Map* map = regexp->map(); |
| 1792 | Object* constructor = map->constructor(); |
| 1793 | if (constructor->IsJSFunction() && |
| 1794 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1795 | // If we still have the original map, set in-object properties directly. |
| 1796 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 1797 | // Both true and false are immovable immortal objects so no need for write |
| 1798 | // barrier. |
| 1799 | regexp->InObjectPropertyAtPut( |
| 1800 | JSRegExp::kGlobalFieldIndex, global, SKIP_WRITE_BARRIER); |
| 1801 | regexp->InObjectPropertyAtPut( |
| 1802 | JSRegExp::kIgnoreCaseFieldIndex, ignoreCase, SKIP_WRITE_BARRIER); |
| 1803 | regexp->InObjectPropertyAtPut( |
| 1804 | JSRegExp::kMultilineFieldIndex, multiline, SKIP_WRITE_BARRIER); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1805 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1806 | Smi::FromInt(0), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1807 | SKIP_WRITE_BARRIER); // It's a Smi. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1808 | return regexp; |
| 1809 | } |
| 1810 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1811 | // Map has changed, so use generic, but slower, method. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1812 | PropertyAttributes final = |
| 1813 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1814 | PropertyAttributes writable = |
| 1815 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1816 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1817 | MaybeObject* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1818 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1819 | source, |
| 1820 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1821 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1822 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1823 | global, |
| 1824 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1825 | ASSERT(!result->IsFailure()); |
| 1826 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1827 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1828 | ignoreCase, |
| 1829 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1830 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1831 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1832 | multiline, |
| 1833 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1834 | ASSERT(!result->IsFailure()); |
| 1835 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1836 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1837 | Smi::FromInt(0), |
| 1838 | writable); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1839 | ASSERT(!result->IsFailure()); |
| 1840 | USE(result); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1841 | return regexp; |
| 1842 | } |
| 1843 | |
| 1844 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1845 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinishArrayPrototypeSetup) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1846 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1847 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1848 | CONVERT_ARG_HANDLE_CHECKED(JSArray, prototype, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1849 | // This is necessary to enable fast checks for absence of elements |
| 1850 | // on Array.prototype and below. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1851 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1852 | return Smi::FromInt(0); |
| 1853 | } |
| 1854 | |
| 1855 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1856 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1857 | Handle<JSObject> holder, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1858 | const char* name, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1859 | Builtins::Name builtin_name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1860 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1861 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1862 | Handle<JSFunction> optimized = |
| 1863 | isolate->factory()->NewFunction(key, |
| 1864 | JS_OBJECT_TYPE, |
| 1865 | JSObject::kHeaderSize, |
| 1866 | code, |
| 1867 | false); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1868 | optimized->shared()->DontAdaptArguments(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1869 | JSReceiver::SetProperty(holder, key, optimized, NONE, kStrictMode); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1870 | return optimized; |
| 1871 | } |
| 1872 | |
| 1873 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1874 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1875 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1876 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1877 | CONVERT_ARG_HANDLE_CHECKED(JSObject, holder, 0); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1878 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1879 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
| 1880 | InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
| 1881 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 1882 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 1883 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 1884 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
| 1885 | InstallBuiltin(isolate, holder, "concat", Builtins::kArrayConcat); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1886 | |
| 1887 | return *holder; |
| 1888 | } |
| 1889 | |
| 1890 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1891 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetDefaultReceiver) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1892 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1893 | CONVERT_ARG_CHECKED(JSReceiver, callable, 0); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1894 | |
| 1895 | if (!callable->IsJSFunction()) { |
| 1896 | HandleScope scope(isolate); |
| 1897 | bool threw = false; |
| 1898 | Handle<Object> delegate = |
| 1899 | Execution::TryGetFunctionDelegate(Handle<JSReceiver>(callable), &threw); |
| 1900 | if (threw) return Failure::Exception(); |
| 1901 | callable = JSFunction::cast(*delegate); |
| 1902 | } |
| 1903 | JSFunction* function = JSFunction::cast(callable); |
| 1904 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1905 | SharedFunctionInfo* shared = function->shared(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1906 | if (shared->native() || !shared->is_classic_mode()) { |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1907 | return isolate->heap()->undefined_value(); |
| 1908 | } |
| 1909 | // Returns undefined for strict or native functions, or |
| 1910 | // the associated global receiver for "normal" functions. |
| 1911 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1912 | Context* global_context = |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 1913 | function->context()->global()->global_context(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1914 | return global_context->global()->global_receiver(); |
| 1915 | } |
| 1916 | |
| 1917 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1918 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MaterializeRegExpLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1919 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1920 | ASSERT(args.length() == 4); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1921 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1922 | int index = args.smi_at(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | Handle<String> pattern = args.at<String>(2); |
| 1924 | Handle<String> flags = args.at<String>(3); |
| 1925 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1926 | // Get the RegExp function from the context in the literals array. |
| 1927 | // This is the RegExp function from the context in which the |
| 1928 | // function was created. We do not use the RegExp function from the |
| 1929 | // current global context because this might be the RegExp function |
| 1930 | // 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] | 1931 | Handle<JSFunction> constructor = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1932 | Handle<JSFunction>( |
| 1933 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1934 | // Compute the regular expression literal. |
| 1935 | bool has_pending_exception; |
| 1936 | Handle<Object> regexp = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1937 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1938 | &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1939 | if (has_pending_exception) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1940 | ASSERT(isolate->has_pending_exception()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1941 | return Failure::Exception(); |
| 1942 | } |
| 1943 | literals->set(index, *regexp); |
| 1944 | return *regexp; |
| 1945 | } |
| 1946 | |
| 1947 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1948 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1949 | NoHandleAllocation ha; |
| 1950 | ASSERT(args.length() == 1); |
| 1951 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1952 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1953 | return f->shared()->name(); |
| 1954 | } |
| 1955 | |
| 1956 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1957 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetName) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1958 | NoHandleAllocation ha; |
| 1959 | ASSERT(args.length() == 2); |
| 1960 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1961 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 1962 | CONVERT_ARG_CHECKED(String, name, 1); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1963 | f->shared()->set_name(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1964 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1968 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionNameShouldPrintAsAnonymous) { |
| 1969 | NoHandleAllocation ha; |
| 1970 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1971 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1972 | return isolate->heap()->ToBoolean( |
| 1973 | f->shared()->name_should_print_as_anonymous()); |
| 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionMarkNameShouldPrintAsAnonymous) { |
| 1978 | NoHandleAllocation ha; |
| 1979 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1980 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1981 | f->shared()->set_name_should_print_as_anonymous(true); |
| 1982 | return isolate->heap()->undefined_value(); |
| 1983 | } |
| 1984 | |
| 1985 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1986 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1987 | NoHandleAllocation ha; |
| 1988 | ASSERT(args.length() == 1); |
| 1989 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 1990 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1991 | Object* obj = f->RemovePrototype(); |
| 1992 | if (obj->IsFailure()) return obj; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1993 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1994 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1998 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1999 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2000 | ASSERT(args.length() == 1); |
| 2001 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2002 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2003 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 2004 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2005 | |
| 2006 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 2007 | } |
| 2008 | |
| 2009 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2010 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetSourceCode) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 2011 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2012 | ASSERT(args.length() == 1); |
| 2013 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2014 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 2015 | Handle<SharedFunctionInfo> shared(f->shared()); |
| 2016 | return *shared->GetSourceCode(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2020 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScriptSourcePosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2021 | NoHandleAllocation ha; |
| 2022 | ASSERT(args.length() == 1); |
| 2023 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2024 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2025 | int pos = fun->shared()->start_position(); |
| 2026 | return Smi::FromInt(pos); |
| 2027 | } |
| 2028 | |
| 2029 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2030 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetPositionForOffset) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2031 | ASSERT(args.length() == 2); |
| 2032 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2033 | CONVERT_ARG_CHECKED(Code, code, 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2034 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 2035 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2036 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 2037 | |
| 2038 | Address pc = code->address() + offset; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2039 | return Smi::FromInt(code->SourcePosition(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2043 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetInstanceClassName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2044 | NoHandleAllocation ha; |
| 2045 | ASSERT(args.length() == 2); |
| 2046 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2047 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 2048 | CONVERT_ARG_CHECKED(String, name, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | fun->SetInstanceClassName(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2050 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2054 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2055 | NoHandleAllocation ha; |
| 2056 | ASSERT(args.length() == 2); |
| 2057 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2058 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 2059 | CONVERT_SMI_ARG_CHECKED(length, 1); |
| 2060 | fun->shared()->set_length(length); |
| 2061 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2065 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetPrototype) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2066 | NoHandleAllocation ha; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2067 | ASSERT(args.length() == 2); |
| 2068 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2069 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2070 | ASSERT(fun->should_have_prototype()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2071 | Object* obj; |
| 2072 | { MaybeObject* maybe_obj = |
| 2073 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 2074 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2075 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2076 | return args[0]; // return TOS |
| 2077 | } |
| 2078 | |
| 2079 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 2080 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetReadOnlyPrototype) { |
| 2081 | NoHandleAllocation ha; |
| 2082 | RUNTIME_ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2083 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 2084 | |
| 2085 | MaybeObject* maybe_name = |
| 2086 | isolate->heap()->AllocateStringFromAscii(CStrVector("prototype")); |
| 2087 | String* name; |
| 2088 | if (!maybe_name->To(&name)) return maybe_name; |
| 2089 | |
| 2090 | if (function->HasFastProperties()) { |
| 2091 | // Construct a new field descriptor with updated attributes. |
| 2092 | DescriptorArray* instance_desc = function->map()->instance_descriptors(); |
| 2093 | int index = instance_desc->Search(name); |
| 2094 | ASSERT(index != DescriptorArray::kNotFound); |
| 2095 | PropertyDetails details(instance_desc->GetDetails(index)); |
| 2096 | CallbacksDescriptor new_desc(name, |
| 2097 | instance_desc->GetValue(index), |
| 2098 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2099 | details.index()); |
| 2100 | // Construct a new field descriptors array containing the new descriptor. |
| 2101 | Object* descriptors_unchecked; |
| 2102 | { MaybeObject* maybe_descriptors_unchecked = |
| 2103 | instance_desc->CopyInsert(&new_desc, REMOVE_TRANSITIONS); |
| 2104 | if (!maybe_descriptors_unchecked->ToObject(&descriptors_unchecked)) { |
| 2105 | return maybe_descriptors_unchecked; |
| 2106 | } |
| 2107 | } |
| 2108 | DescriptorArray* new_descriptors = |
| 2109 | DescriptorArray::cast(descriptors_unchecked); |
| 2110 | // Create a new map featuring the new field descriptors array. |
| 2111 | Object* map_unchecked; |
| 2112 | { MaybeObject* maybe_map_unchecked = function->map()->CopyDropDescriptors(); |
| 2113 | if (!maybe_map_unchecked->ToObject(&map_unchecked)) { |
| 2114 | return maybe_map_unchecked; |
| 2115 | } |
| 2116 | } |
| 2117 | Map* new_map = Map::cast(map_unchecked); |
| 2118 | new_map->set_instance_descriptors(new_descriptors); |
| 2119 | function->set_map(new_map); |
| 2120 | } else { // Dictionary properties. |
| 2121 | // Directly manipulate the property details. |
| 2122 | int entry = function->property_dictionary()->FindEntry(name); |
| 2123 | ASSERT(entry != StringDictionary::kNotFound); |
| 2124 | PropertyDetails details = function->property_dictionary()->DetailsAt(entry); |
| 2125 | PropertyDetails new_details( |
| 2126 | static_cast<PropertyAttributes>(details.attributes() | READ_ONLY), |
| 2127 | details.type(), |
| 2128 | details.index()); |
| 2129 | function->property_dictionary()->DetailsAtPut(entry, new_details); |
| 2130 | } |
| 2131 | return function; |
| 2132 | } |
| 2133 | |
| 2134 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2135 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsAPIFunction) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2136 | NoHandleAllocation ha; |
| 2137 | ASSERT(args.length() == 1); |
| 2138 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2139 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2140 | return isolate->heap()->ToBoolean(f->shared()->IsApiFunction()); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2143 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2144 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsBuiltin) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 2145 | NoHandleAllocation ha; |
| 2146 | ASSERT(args.length() == 1); |
| 2147 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2148 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 2149 | return isolate->heap()->ToBoolean(f->IsBuiltin()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 2152 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2153 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetCode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2154 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2155 | ASSERT(args.length() == 2); |
| 2156 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2157 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2158 | Handle<Object> code = args.at<Object>(1); |
| 2159 | |
| 2160 | Handle<Context> context(target->context()); |
| 2161 | |
| 2162 | if (!code->IsNull()) { |
| 2163 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 2164 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2165 | Handle<SharedFunctionInfo> shared(fun->shared()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2166 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2167 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2168 | return Failure::Exception(); |
| 2169 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2170 | // Since we don't store the source for this we should never |
| 2171 | // optimize this. |
| 2172 | shared->code()->set_optimizable(false); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2173 | // Set the code, scope info, formal parameter count, |
| 2174 | // and the length of the target function. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2175 | target->shared()->set_code(shared->code()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2176 | target->ReplaceCode(shared->code()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2177 | target->shared()->set_scope_info(shared->scope_info()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2178 | target->shared()->set_length(shared->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2179 | target->shared()->set_formal_parameter_count( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2180 | shared->formal_parameter_count()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2181 | // Set the source code of the target function to undefined. |
| 2182 | // SetCode is only used for built-in constructors like String, |
| 2183 | // Array, and Object, and some web code |
| 2184 | // doesn't like seeing source code for constructors. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2185 | target->shared()->set_script(isolate->heap()->undefined_value()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2186 | target->shared()->code()->set_optimizable(false); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2187 | // Clear the optimization hints related to the compiled code as these are no |
| 2188 | // longer valid when the code is overwritten. |
| 2189 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2190 | context = Handle<Context>(fun->context()); |
| 2191 | |
| 2192 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2193 | // cross context contamination. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2194 | int number_of_literals = fun->NumberOfLiterals(); |
| 2195 | Handle<FixedArray> literals = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2196 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2197 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2198 | // Insert the object, regexp and array functions in the literals |
| 2199 | // array prefix. These are the functions that will be used when |
| 2200 | // creating object, regexp and array literals. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2201 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2202 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2203 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2204 | target->set_literals(*literals); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2205 | target->set_next_function_link(isolate->heap()->undefined_value()); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 2206 | |
| 2207 | if (isolate->logger()->is_logging() || CpuProfiler::is_profiling(isolate)) { |
| 2208 | isolate->logger()->LogExistingFunction( |
| 2209 | shared, Handle<Code>(shared->code())); |
| 2210 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | target->set_context(*context); |
| 2214 | return *target; |
| 2215 | } |
| 2216 | |
| 2217 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2218 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2219 | HandleScope scope(isolate); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2220 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2221 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2222 | CONVERT_SMI_ARG_CHECKED(num, 1); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2223 | RUNTIME_ASSERT(num >= 0); |
| 2224 | SetExpectedNofProperties(function, num); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2225 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2229 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2230 | Object* char_code) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2231 | uint32_t code; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2232 | if (char_code->ToArrayIndex(&code)) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2233 | if (code <= 0xffff) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2234 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2235 | } |
| 2236 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2237 | return isolate->heap()->empty_string(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2241 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCharCodeAt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2242 | NoHandleAllocation ha; |
| 2243 | ASSERT(args.length() == 2); |
| 2244 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 2245 | CONVERT_ARG_CHECKED(String, subject, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2246 | Object* index = args[1]; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2247 | RUNTIME_ASSERT(index->IsNumber()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2248 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2249 | uint32_t i = 0; |
| 2250 | if (index->IsSmi()) { |
| 2251 | int value = Smi::cast(index)->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2252 | if (value < 0) return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2253 | i = value; |
| 2254 | } else { |
| 2255 | ASSERT(index->IsHeapNumber()); |
| 2256 | double value = HeapNumber::cast(index)->value(); |
| 2257 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
kasperl@chromium.org | 74e4e5e | 2010-01-25 10:15:52 +0000 | [diff] [blame] | 2258 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2259 | |
| 2260 | // Flatten the string. If someone wants to get a char at an index |
| 2261 | // in a cons string, it is likely that more indices will be |
| 2262 | // accessed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2263 | Object* flat; |
| 2264 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2265 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2266 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2267 | subject = String::cast(flat); |
| 2268 | |
| 2269 | if (i >= static_cast<uint32_t>(subject->length())) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2270 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | return Smi::FromInt(subject->Get(i)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2277 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CharFromCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2278 | NoHandleAllocation ha; |
| 2279 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2280 | return CharFromCode(isolate, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2283 | |
| 2284 | class FixedArrayBuilder { |
| 2285 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2286 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2287 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2288 | length_(0), |
| 2289 | has_non_smi_elements_(false) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2290 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2291 | // extend the array will work. |
| 2292 | ASSERT(initial_capacity > 0); |
| 2293 | } |
| 2294 | |
| 2295 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2296 | : array_(backing_store), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2297 | length_(0), |
| 2298 | has_non_smi_elements_(false) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2299 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2300 | // extend the array will work. |
| 2301 | ASSERT(backing_store->length() > 0); |
| 2302 | } |
| 2303 | |
| 2304 | bool HasCapacity(int elements) { |
| 2305 | int length = array_->length(); |
| 2306 | int required_length = length_ + elements; |
| 2307 | return (length >= required_length); |
| 2308 | } |
| 2309 | |
| 2310 | void EnsureCapacity(int elements) { |
| 2311 | int length = array_->length(); |
| 2312 | int required_length = length_ + elements; |
| 2313 | if (length < required_length) { |
| 2314 | int new_length = length; |
| 2315 | do { |
| 2316 | new_length *= 2; |
| 2317 | } while (new_length < required_length); |
| 2318 | Handle<FixedArray> extended_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2319 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2320 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2321 | array_ = extended_array; |
| 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | void Add(Object* value) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2326 | ASSERT(!value->IsSmi()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2327 | ASSERT(length_ < capacity()); |
| 2328 | array_->set(length_, value); |
| 2329 | length_++; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2330 | has_non_smi_elements_ = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | void Add(Smi* value) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2334 | ASSERT(value->IsSmi()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2335 | ASSERT(length_ < capacity()); |
| 2336 | array_->set(length_, value); |
| 2337 | length_++; |
| 2338 | } |
| 2339 | |
| 2340 | Handle<FixedArray> array() { |
| 2341 | return array_; |
| 2342 | } |
| 2343 | |
| 2344 | int length() { |
| 2345 | return length_; |
| 2346 | } |
| 2347 | |
| 2348 | int capacity() { |
| 2349 | return array_->length(); |
| 2350 | } |
| 2351 | |
| 2352 | Handle<JSArray> ToJSArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2353 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2354 | result_array->set_length(Smi::FromInt(length_)); |
| 2355 | return result_array; |
| 2356 | } |
| 2357 | |
| 2358 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2359 | FACTORY->SetContent(target_array, array_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2360 | target_array->set_length(Smi::FromInt(length_)); |
| 2361 | return target_array; |
| 2362 | } |
| 2363 | |
| 2364 | private: |
| 2365 | Handle<FixedArray> array_; |
| 2366 | int length_; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2367 | bool has_non_smi_elements_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2368 | }; |
| 2369 | |
| 2370 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2371 | // Forward declarations. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2372 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2373 | const int kStringBuilderConcatHelperPositionBits = 19; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2374 | |
| 2375 | template <typename schar> |
| 2376 | static inline void StringBuilderConcatHelper(String*, |
| 2377 | schar*, |
| 2378 | FixedArray*, |
| 2379 | int); |
| 2380 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2381 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2382 | StringBuilderSubstringLength; |
| 2383 | typedef BitField<int, |
| 2384 | kStringBuilderConcatHelperLengthBits, |
| 2385 | kStringBuilderConcatHelperPositionBits> |
| 2386 | StringBuilderSubstringPosition; |
| 2387 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2388 | |
| 2389 | class ReplacementStringBuilder { |
| 2390 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2391 | ReplacementStringBuilder(Heap* heap, |
| 2392 | Handle<String> subject, |
| 2393 | int estimated_part_count) |
| 2394 | : heap_(heap), |
| 2395 | array_builder_(heap->isolate(), estimated_part_count), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2396 | subject_(subject), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2397 | character_count_(0), |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2398 | is_ascii_(subject->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2399 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2400 | // extend the array will work. |
| 2401 | ASSERT(estimated_part_count > 0); |
| 2402 | } |
| 2403 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2404 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2405 | int from, |
| 2406 | int to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2407 | ASSERT(from >= 0); |
| 2408 | int length = to - from; |
| 2409 | ASSERT(length > 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2410 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2411 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2412 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2413 | StringBuilderSubstringPosition::encode(from); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2414 | builder->Add(Smi::FromInt(encoded_slice)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2415 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2416 | // Otherwise encode as two smis. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2417 | builder->Add(Smi::FromInt(-length)); |
| 2418 | builder->Add(Smi::FromInt(from)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2419 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | |
| 2423 | void EnsureCapacity(int elements) { |
| 2424 | array_builder_.EnsureCapacity(elements); |
| 2425 | } |
| 2426 | |
| 2427 | |
| 2428 | void AddSubjectSlice(int from, int to) { |
| 2429 | AddSubjectSlice(&array_builder_, from, to); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2430 | IncrementCharacterCount(to - from); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | |
| 2434 | void AddString(Handle<String> string) { |
| 2435 | int length = string->length(); |
| 2436 | ASSERT(length > 0); |
| 2437 | AddElement(*string); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2438 | if (!string->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2439 | is_ascii_ = false; |
| 2440 | } |
| 2441 | IncrementCharacterCount(length); |
| 2442 | } |
| 2443 | |
| 2444 | |
| 2445 | Handle<String> ToString() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2446 | if (array_builder_.length() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2447 | return heap_->isolate()->factory()->empty_string(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | Handle<String> joined_string; |
| 2451 | if (is_ascii_) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2452 | Handle<SeqAsciiString> seq = NewRawAsciiString(character_count_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2453 | AssertNoAllocation no_alloc; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2454 | char* char_buffer = seq->GetChars(); |
| 2455 | StringBuilderConcatHelper(*subject_, |
| 2456 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2457 | *array_builder_.array(), |
| 2458 | array_builder_.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2459 | joined_string = Handle<String>::cast(seq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2460 | } else { |
| 2461 | // Non-ASCII. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2462 | Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2463 | AssertNoAllocation no_alloc; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2464 | uc16* char_buffer = seq->GetChars(); |
| 2465 | StringBuilderConcatHelper(*subject_, |
| 2466 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2467 | *array_builder_.array(), |
| 2468 | array_builder_.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2469 | joined_string = Handle<String>::cast(seq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2470 | } |
| 2471 | return joined_string; |
| 2472 | } |
| 2473 | |
| 2474 | |
| 2475 | void IncrementCharacterCount(int by) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2476 | if (character_count_ > String::kMaxLength - by) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2477 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2478 | } |
| 2479 | character_count_ += by; |
| 2480 | } |
| 2481 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2482 | Handle<JSArray> GetParts() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2483 | return array_builder_.ToJSArray(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2484 | } |
kmillikin@chromium.org | d982519 | 2010-03-30 08:36:16 +0000 | [diff] [blame] | 2485 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2486 | private: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2487 | Handle<SeqAsciiString> NewRawAsciiString(int length) { |
| 2488 | return heap_->isolate()->factory()->NewRawAsciiString(length); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2492 | Handle<SeqTwoByteString> NewRawTwoByteString(int length) { |
| 2493 | return heap_->isolate()->factory()->NewRawTwoByteString(length); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | |
| 2497 | void AddElement(Object* element) { |
| 2498 | ASSERT(element->IsSmi() || element->IsString()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2499 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2500 | array_builder_.Add(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2503 | Heap* heap_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2504 | FixedArrayBuilder array_builder_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2505 | Handle<String> subject_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2506 | int character_count_; |
| 2507 | bool is_ascii_; |
| 2508 | }; |
| 2509 | |
| 2510 | |
| 2511 | class CompiledReplacement { |
| 2512 | public: |
| 2513 | CompiledReplacement() |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2514 | : parts_(1), replacement_substrings_(0), simple_hint_(false) {} |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2515 | |
| 2516 | void Compile(Handle<String> replacement, |
| 2517 | int capture_count, |
| 2518 | int subject_length); |
| 2519 | |
| 2520 | void Apply(ReplacementStringBuilder* builder, |
| 2521 | int match_from, |
| 2522 | int match_to, |
| 2523 | Handle<JSArray> last_match_info); |
| 2524 | |
| 2525 | // Number of distinct parts of the replacement pattern. |
| 2526 | int parts() { |
| 2527 | return parts_.length(); |
| 2528 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2529 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2530 | bool simple_hint() { |
| 2531 | return simple_hint_; |
| 2532 | } |
| 2533 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2534 | private: |
| 2535 | enum PartType { |
| 2536 | SUBJECT_PREFIX = 1, |
| 2537 | SUBJECT_SUFFIX, |
| 2538 | SUBJECT_CAPTURE, |
| 2539 | REPLACEMENT_SUBSTRING, |
| 2540 | REPLACEMENT_STRING, |
| 2541 | |
| 2542 | NUMBER_OF_PART_TYPES |
| 2543 | }; |
| 2544 | |
| 2545 | struct ReplacementPart { |
| 2546 | static inline ReplacementPart SubjectMatch() { |
| 2547 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2548 | } |
| 2549 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2550 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2551 | } |
| 2552 | static inline ReplacementPart SubjectPrefix() { |
| 2553 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2554 | } |
| 2555 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2556 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2557 | } |
| 2558 | static inline ReplacementPart ReplacementString() { |
| 2559 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2560 | } |
| 2561 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2562 | ASSERT(from >= 0); |
| 2563 | ASSERT(to > from); |
| 2564 | return ReplacementPart(-from, to); |
| 2565 | } |
| 2566 | |
| 2567 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2568 | // the replacement pattern, otherwise it's a value from PartType. |
| 2569 | ReplacementPart(int tag, int data) |
| 2570 | : tag(tag), data(data) { |
| 2571 | // Must be non-positive or a PartType value. |
| 2572 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2573 | } |
| 2574 | // Either a value of PartType or a non-positive number that is |
| 2575 | // the negation of an index into the replacement string. |
| 2576 | int tag; |
| 2577 | // The data value's interpretation depends on the value of tag: |
| 2578 | // tag == SUBJECT_PREFIX || |
| 2579 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2580 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2581 | // tag == REPLACEMENT_SUBSTRING || |
| 2582 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2583 | // of the replacement string. |
| 2584 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2585 | // string ranging over -tag .. data. |
| 2586 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2587 | // substring objects. |
| 2588 | int data; |
| 2589 | }; |
| 2590 | |
| 2591 | template<typename Char> |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2592 | static bool ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2593 | Vector<Char> characters, |
| 2594 | int capture_count, |
| 2595 | int subject_length) { |
| 2596 | int length = characters.length(); |
| 2597 | int last = 0; |
| 2598 | for (int i = 0; i < length; i++) { |
| 2599 | Char c = characters[i]; |
| 2600 | if (c == '$') { |
| 2601 | int next_index = i + 1; |
| 2602 | if (next_index == length) { // No next character! |
| 2603 | break; |
| 2604 | } |
| 2605 | Char c2 = characters[next_index]; |
| 2606 | switch (c2) { |
| 2607 | case '$': |
| 2608 | if (i > last) { |
| 2609 | // There is a substring before. Include the first "$". |
| 2610 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2611 | last = next_index + 1; // Continue after the second "$". |
| 2612 | } else { |
| 2613 | // Let the next substring start with the second "$". |
| 2614 | last = next_index; |
| 2615 | } |
| 2616 | i = next_index; |
| 2617 | break; |
| 2618 | case '`': |
| 2619 | if (i > last) { |
| 2620 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2621 | } |
| 2622 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2623 | i = next_index; |
| 2624 | last = i + 1; |
| 2625 | break; |
| 2626 | case '\'': |
| 2627 | if (i > last) { |
| 2628 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2629 | } |
| 2630 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2631 | i = next_index; |
| 2632 | last = i + 1; |
| 2633 | break; |
| 2634 | case '&': |
| 2635 | if (i > last) { |
| 2636 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2637 | } |
| 2638 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2639 | i = next_index; |
| 2640 | last = i + 1; |
| 2641 | break; |
| 2642 | case '0': |
| 2643 | case '1': |
| 2644 | case '2': |
| 2645 | case '3': |
| 2646 | case '4': |
| 2647 | case '5': |
| 2648 | case '6': |
| 2649 | case '7': |
| 2650 | case '8': |
| 2651 | case '9': { |
| 2652 | int capture_ref = c2 - '0'; |
| 2653 | if (capture_ref > capture_count) { |
| 2654 | i = next_index; |
| 2655 | continue; |
| 2656 | } |
| 2657 | int second_digit_index = next_index + 1; |
| 2658 | if (second_digit_index < length) { |
| 2659 | // Peek ahead to see if we have two digits. |
| 2660 | Char c3 = characters[second_digit_index]; |
| 2661 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2662 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2663 | if (double_digit_ref <= capture_count) { |
| 2664 | next_index = second_digit_index; |
| 2665 | capture_ref = double_digit_ref; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | if (capture_ref > 0) { |
| 2670 | if (i > last) { |
| 2671 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2672 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2673 | ASSERT(capture_ref <= capture_count); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2674 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2675 | last = next_index + 1; |
| 2676 | } |
| 2677 | i = next_index; |
| 2678 | break; |
| 2679 | } |
| 2680 | default: |
| 2681 | i = next_index; |
| 2682 | break; |
| 2683 | } |
| 2684 | } |
| 2685 | } |
| 2686 | if (length > last) { |
| 2687 | if (last == 0) { |
| 2688 | parts->Add(ReplacementPart::ReplacementString()); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2689 | return true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2690 | } else { |
| 2691 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2692 | } |
| 2693 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2694 | return false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | ZoneList<ReplacementPart> parts_; |
| 2698 | ZoneList<Handle<String> > replacement_substrings_; |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2699 | bool simple_hint_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2700 | }; |
| 2701 | |
| 2702 | |
| 2703 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2704 | int capture_count, |
| 2705 | int subject_length) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2706 | { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2707 | AssertNoAllocation no_alloc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2708 | String::FlatContent content = replacement->GetFlatContent(); |
| 2709 | ASSERT(content.IsFlat()); |
| 2710 | if (content.IsAscii()) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2711 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2712 | content.ToAsciiVector(), |
| 2713 | capture_count, |
| 2714 | subject_length); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2715 | } else { |
| 2716 | ASSERT(content.IsTwoByte()); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2717 | simple_hint_ = ParseReplacementPattern(&parts_, |
| 2718 | content.ToUC16Vector(), |
| 2719 | capture_count, |
| 2720 | subject_length); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2721 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2722 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2723 | Isolate* isolate = replacement->GetIsolate(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2724 | // Find substrings of replacement string and create them as String objects. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2725 | int substring_index = 0; |
| 2726 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2727 | int tag = parts_[i].tag; |
| 2728 | if (tag <= 0) { // A replacement string slice. |
| 2729 | int from = -tag; |
| 2730 | int to = parts_[i].data; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2731 | replacement_substrings_.Add( |
| 2732 | isolate->factory()->NewSubString(replacement, from, to)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2733 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2734 | parts_[i].data = substring_index; |
| 2735 | substring_index++; |
| 2736 | } else if (tag == REPLACEMENT_STRING) { |
| 2737 | replacement_substrings_.Add(replacement); |
| 2738 | parts_[i].data = substring_index; |
| 2739 | substring_index++; |
| 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | |
| 2745 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2746 | int match_from, |
| 2747 | int match_to, |
| 2748 | Handle<JSArray> last_match_info) { |
| 2749 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2750 | ReplacementPart part = parts_[i]; |
| 2751 | switch (part.tag) { |
| 2752 | case SUBJECT_PREFIX: |
| 2753 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2754 | break; |
| 2755 | case SUBJECT_SUFFIX: { |
| 2756 | int subject_length = part.data; |
| 2757 | if (match_to < subject_length) { |
| 2758 | builder->AddSubjectSlice(match_to, subject_length); |
| 2759 | } |
| 2760 | break; |
| 2761 | } |
| 2762 | case SUBJECT_CAPTURE: { |
| 2763 | int capture = part.data; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2764 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2765 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2766 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2767 | if (from >= 0 && to > from) { |
| 2768 | builder->AddSubjectSlice(from, to); |
| 2769 | } |
| 2770 | break; |
| 2771 | } |
| 2772 | case REPLACEMENT_SUBSTRING: |
| 2773 | case REPLACEMENT_STRING: |
| 2774 | builder->AddString(replacement_substrings_[part.data]); |
| 2775 | break; |
| 2776 | default: |
| 2777 | UNREACHABLE(); |
| 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2783 | void FindAsciiStringIndices(Vector<const char> subject, |
| 2784 | char pattern, |
| 2785 | ZoneList<int>* indices, |
| 2786 | unsigned int limit) { |
| 2787 | ASSERT(limit > 0); |
| 2788 | // Collect indices of pattern in subject using memchr. |
| 2789 | // Stop after finding at most limit values. |
| 2790 | const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 2791 | const char* subject_end = subject_start + subject.length(); |
| 2792 | const char* pos = subject_start; |
| 2793 | while (limit > 0) { |
| 2794 | pos = reinterpret_cast<const char*>( |
| 2795 | memchr(pos, pattern, subject_end - pos)); |
| 2796 | if (pos == NULL) return; |
| 2797 | indices->Add(static_cast<int>(pos - subject_start)); |
| 2798 | pos++; |
| 2799 | limit--; |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | |
| 2804 | template <typename SubjectChar, typename PatternChar> |
| 2805 | void FindStringIndices(Isolate* isolate, |
| 2806 | Vector<const SubjectChar> subject, |
| 2807 | Vector<const PatternChar> pattern, |
| 2808 | ZoneList<int>* indices, |
| 2809 | unsigned int limit) { |
| 2810 | ASSERT(limit > 0); |
| 2811 | // Collect indices of pattern in subject. |
| 2812 | // Stop after finding at most limit values. |
| 2813 | int pattern_length = pattern.length(); |
| 2814 | int index = 0; |
| 2815 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
| 2816 | while (limit > 0) { |
| 2817 | index = search.Search(subject, index); |
| 2818 | if (index < 0) return; |
| 2819 | indices->Add(index); |
| 2820 | index += pattern_length; |
| 2821 | limit--; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | |
| 2826 | void FindStringIndicesDispatch(Isolate* isolate, |
| 2827 | String* subject, |
| 2828 | String* pattern, |
| 2829 | ZoneList<int>* indices, |
| 2830 | unsigned int limit) { |
| 2831 | { |
| 2832 | AssertNoAllocation no_gc; |
| 2833 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 2834 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 2835 | ASSERT(subject_content.IsFlat()); |
| 2836 | ASSERT(pattern_content.IsFlat()); |
| 2837 | if (subject_content.IsAscii()) { |
| 2838 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 2839 | if (pattern_content.IsAscii()) { |
| 2840 | Vector<const char> pattern_vector = pattern_content.ToAsciiVector(); |
| 2841 | if (pattern_vector.length() == 1) { |
| 2842 | FindAsciiStringIndices(subject_vector, |
| 2843 | pattern_vector[0], |
| 2844 | indices, |
| 2845 | limit); |
| 2846 | } else { |
| 2847 | FindStringIndices(isolate, |
| 2848 | subject_vector, |
| 2849 | pattern_vector, |
| 2850 | indices, |
| 2851 | limit); |
| 2852 | } |
| 2853 | } else { |
| 2854 | FindStringIndices(isolate, |
| 2855 | subject_vector, |
| 2856 | pattern_content.ToUC16Vector(), |
| 2857 | indices, |
| 2858 | limit); |
| 2859 | } |
| 2860 | } else { |
| 2861 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2862 | if (pattern_content.IsAscii()) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2863 | FindStringIndices(isolate, |
| 2864 | subject_vector, |
| 2865 | pattern_content.ToAsciiVector(), |
| 2866 | indices, |
| 2867 | limit); |
| 2868 | } else { |
| 2869 | FindStringIndices(isolate, |
| 2870 | subject_vector, |
| 2871 | pattern_content.ToUC16Vector(), |
| 2872 | indices, |
| 2873 | limit); |
| 2874 | } |
| 2875 | } |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | |
| 2880 | template<typename ResultSeqString> |
| 2881 | MUST_USE_RESULT static MaybeObject* StringReplaceStringWithString( |
| 2882 | Isolate* isolate, |
| 2883 | Handle<String> subject, |
| 2884 | Handle<JSRegExp> pattern_regexp, |
| 2885 | Handle<String> replacement) { |
| 2886 | ASSERT(subject->IsFlat()); |
| 2887 | ASSERT(replacement->IsFlat()); |
| 2888 | |
| 2889 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
| 2890 | ZoneList<int> indices(8); |
| 2891 | ASSERT_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); |
| 2892 | String* pattern = |
| 2893 | String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
| 2894 | int subject_len = subject->length(); |
| 2895 | int pattern_len = pattern->length(); |
| 2896 | int replacement_len = replacement->length(); |
| 2897 | |
| 2898 | FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff); |
| 2899 | |
| 2900 | int matches = indices.length(); |
| 2901 | if (matches == 0) return *subject; |
| 2902 | |
| 2903 | int result_len = (replacement_len - pattern_len) * matches + subject_len; |
| 2904 | int subject_pos = 0; |
| 2905 | int result_pos = 0; |
| 2906 | |
| 2907 | Handle<ResultSeqString> result; |
| 2908 | if (ResultSeqString::kHasAsciiEncoding) { |
| 2909 | result = Handle<ResultSeqString>::cast( |
| 2910 | isolate->factory()->NewRawAsciiString(result_len)); |
| 2911 | } else { |
| 2912 | result = Handle<ResultSeqString>::cast( |
| 2913 | isolate->factory()->NewRawTwoByteString(result_len)); |
| 2914 | } |
| 2915 | |
| 2916 | for (int i = 0; i < matches; i++) { |
| 2917 | // Copy non-matched subject content. |
| 2918 | if (subject_pos < indices.at(i)) { |
| 2919 | String::WriteToFlat(*subject, |
| 2920 | result->GetChars() + result_pos, |
| 2921 | subject_pos, |
| 2922 | indices.at(i)); |
| 2923 | result_pos += indices.at(i) - subject_pos; |
| 2924 | } |
| 2925 | |
| 2926 | // Replace match. |
| 2927 | if (replacement_len > 0) { |
| 2928 | String::WriteToFlat(*replacement, |
| 2929 | result->GetChars() + result_pos, |
| 2930 | 0, |
| 2931 | replacement_len); |
| 2932 | result_pos += replacement_len; |
| 2933 | } |
| 2934 | |
| 2935 | subject_pos = indices.at(i) + pattern_len; |
| 2936 | } |
| 2937 | // Add remaining subject content at the end. |
| 2938 | if (subject_pos < subject_len) { |
| 2939 | String::WriteToFlat(*subject, |
| 2940 | result->GetChars() + result_pos, |
| 2941 | subject_pos, |
| 2942 | subject_len); |
| 2943 | } |
| 2944 | return *result; |
| 2945 | } |
| 2946 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2947 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2948 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2949 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2950 | String* subject, |
| 2951 | JSRegExp* regexp, |
| 2952 | String* replacement, |
| 2953 | JSArray* last_match_info) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2954 | ASSERT(subject->IsFlat()); |
| 2955 | ASSERT(replacement->IsFlat()); |
| 2956 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2957 | HandleScope handles(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2958 | |
| 2959 | int length = subject->length(); |
| 2960 | Handle<String> subject_handle(subject); |
| 2961 | Handle<JSRegExp> regexp_handle(regexp); |
| 2962 | Handle<String> replacement_handle(replacement); |
| 2963 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2964 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2965 | subject_handle, |
| 2966 | 0, |
| 2967 | last_match_info_handle); |
| 2968 | if (match.is_null()) { |
| 2969 | return Failure::Exception(); |
| 2970 | } |
| 2971 | if (match->IsNull()) { |
| 2972 | return *subject_handle; |
| 2973 | } |
| 2974 | |
| 2975 | int capture_count = regexp_handle->CaptureCount(); |
| 2976 | |
| 2977 | // CompiledReplacement uses zone allocation. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2978 | ZoneScope zone(isolate, DELETE_ON_EXIT); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2979 | CompiledReplacement compiled_replacement; |
| 2980 | compiled_replacement.Compile(replacement_handle, |
| 2981 | capture_count, |
| 2982 | length); |
| 2983 | |
| 2984 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 2985 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2986 | // Shortcut for simple non-regexp global replacements |
| 2987 | if (is_global && |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2988 | regexp_handle->TypeTag() == JSRegExp::ATOM && |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 2989 | compiled_replacement.simple_hint()) { |
| 2990 | if (subject_handle->HasOnlyAsciiChars() && |
| 2991 | replacement_handle->HasOnlyAsciiChars()) { |
| 2992 | return StringReplaceStringWithString<SeqAsciiString>( |
| 2993 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 2994 | } else { |
| 2995 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 2996 | isolate, subject_handle, regexp_handle, replacement_handle); |
| 2997 | } |
| 2998 | } |
| 2999 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3000 | // Guessing the number of parts that the final result string is built |
| 3001 | // from. Global regexps can match any number of times, so we guess |
| 3002 | // conservatively. |
| 3003 | int expected_parts = |
| 3004 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3005 | ReplacementStringBuilder builder(isolate->heap(), |
| 3006 | subject_handle, |
| 3007 | expected_parts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3008 | |
| 3009 | // Index of end of last match. |
| 3010 | int prev = 0; |
| 3011 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3012 | // Number of parts added by compiled replacement plus preceeding |
| 3013 | // string and possibly suffix after last match. It is possible for |
| 3014 | // all components to use two elements when encoded as two smis. |
| 3015 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3016 | bool matched = true; |
| 3017 | do { |
| 3018 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3019 | // Increase the capacity of the builder before entering local handle-scope, |
| 3020 | // so its internal buffer can safely allocate a new handle if it grows. |
| 3021 | builder.EnsureCapacity(parts_added_per_loop); |
| 3022 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3023 | HandleScope loop_scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3024 | int start, end; |
| 3025 | { |
| 3026 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3027 | FixedArray* match_info_array = |
| 3028 | FixedArray::cast(last_match_info_handle->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3029 | |
| 3030 | ASSERT_EQ(capture_count * 2 + 2, |
| 3031 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 3032 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3033 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3034 | } |
| 3035 | |
| 3036 | if (prev < start) { |
| 3037 | builder.AddSubjectSlice(prev, start); |
| 3038 | } |
| 3039 | compiled_replacement.Apply(&builder, |
| 3040 | start, |
| 3041 | end, |
| 3042 | last_match_info_handle); |
| 3043 | prev = end; |
| 3044 | |
| 3045 | // Only continue checking for global regexps. |
| 3046 | if (!is_global) break; |
| 3047 | |
| 3048 | // Continue from where the match ended, unless it was an empty match. |
| 3049 | int next = end; |
| 3050 | if (start == end) { |
| 3051 | next = end + 1; |
| 3052 | if (next > length) break; |
| 3053 | } |
| 3054 | |
| 3055 | match = RegExpImpl::Exec(regexp_handle, |
| 3056 | subject_handle, |
| 3057 | next, |
| 3058 | last_match_info_handle); |
| 3059 | if (match.is_null()) { |
| 3060 | return Failure::Exception(); |
| 3061 | } |
| 3062 | matched = !match->IsNull(); |
| 3063 | } while (matched); |
| 3064 | |
| 3065 | if (prev < length) { |
| 3066 | builder.AddSubjectSlice(prev, length); |
| 3067 | } |
| 3068 | |
| 3069 | return *(builder.ToString()); |
| 3070 | } |
| 3071 | |
| 3072 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3073 | template <typename ResultSeqString> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3074 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3075 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3076 | String* subject, |
| 3077 | JSRegExp* regexp, |
| 3078 | JSArray* last_match_info) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3079 | ASSERT(subject->IsFlat()); |
| 3080 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3081 | HandleScope handles(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3082 | |
| 3083 | Handle<String> subject_handle(subject); |
| 3084 | Handle<JSRegExp> regexp_handle(regexp); |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 3085 | |
| 3086 | // Shortcut for simple non-regexp global replacements |
| 3087 | if (regexp_handle->GetFlags().is_global() && |
| 3088 | regexp_handle->TypeTag() == JSRegExp::ATOM) { |
| 3089 | Handle<String> empty_string_handle(HEAP->empty_string()); |
| 3090 | if (subject_handle->HasOnlyAsciiChars()) { |
| 3091 | return StringReplaceStringWithString<SeqAsciiString>( |
| 3092 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3093 | } else { |
| 3094 | return StringReplaceStringWithString<SeqTwoByteString>( |
| 3095 | isolate, subject_handle, regexp_handle, empty_string_handle); |
| 3096 | } |
| 3097 | } |
| 3098 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3099 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 3100 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 3101 | subject_handle, |
| 3102 | 0, |
| 3103 | last_match_info_handle); |
| 3104 | if (match.is_null()) return Failure::Exception(); |
| 3105 | if (match->IsNull()) return *subject_handle; |
| 3106 | |
| 3107 | ASSERT(last_match_info_handle->HasFastElements()); |
| 3108 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3109 | int start, end; |
| 3110 | { |
| 3111 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3112 | FixedArray* match_info_array = |
| 3113 | FixedArray::cast(last_match_info_handle->elements()); |
| 3114 | |
| 3115 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3116 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3117 | } |
| 3118 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 3119 | int length = subject_handle->length(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3120 | int new_length = length - (end - start); |
| 3121 | if (new_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3122 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3123 | } |
| 3124 | Handle<ResultSeqString> answer; |
| 3125 | if (ResultSeqString::kHasAsciiEncoding) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3126 | answer = Handle<ResultSeqString>::cast( |
| 3127 | isolate->factory()->NewRawAsciiString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3128 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3129 | answer = Handle<ResultSeqString>::cast( |
| 3130 | isolate->factory()->NewRawTwoByteString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | // If the regexp isn't global, only match once. |
| 3134 | if (!regexp_handle->GetFlags().is_global()) { |
| 3135 | if (start > 0) { |
| 3136 | String::WriteToFlat(*subject_handle, |
| 3137 | answer->GetChars(), |
| 3138 | 0, |
| 3139 | start); |
| 3140 | } |
| 3141 | if (end < length) { |
| 3142 | String::WriteToFlat(*subject_handle, |
| 3143 | answer->GetChars() + start, |
| 3144 | end, |
| 3145 | length); |
| 3146 | } |
| 3147 | return *answer; |
| 3148 | } |
| 3149 | |
| 3150 | int prev = 0; // Index of end of last match. |
| 3151 | int next = 0; // Start of next search (prev unless last match was empty). |
| 3152 | int position = 0; |
| 3153 | |
| 3154 | do { |
| 3155 | if (prev < start) { |
| 3156 | // Add substring subject[prev;start] to answer string. |
| 3157 | String::WriteToFlat(*subject_handle, |
| 3158 | answer->GetChars() + position, |
| 3159 | prev, |
| 3160 | start); |
| 3161 | position += start - prev; |
| 3162 | } |
| 3163 | prev = end; |
| 3164 | next = end; |
| 3165 | // Continue from where the match ended, unless it was an empty match. |
| 3166 | if (start == end) { |
| 3167 | next++; |
| 3168 | if (next > length) break; |
| 3169 | } |
| 3170 | match = RegExpImpl::Exec(regexp_handle, |
| 3171 | subject_handle, |
| 3172 | next, |
| 3173 | last_match_info_handle); |
| 3174 | if (match.is_null()) return Failure::Exception(); |
| 3175 | if (match->IsNull()) break; |
| 3176 | |
| 3177 | ASSERT(last_match_info_handle->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3178 | HandleScope loop_scope(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3179 | { |
| 3180 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 3181 | FixedArray* match_info_array = |
| 3182 | FixedArray::cast(last_match_info_handle->elements()); |
| 3183 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 3184 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 3185 | } |
| 3186 | } while (true); |
| 3187 | |
| 3188 | if (prev < length) { |
| 3189 | // Add substring subject[prev;length] to answer string. |
| 3190 | String::WriteToFlat(*subject_handle, |
| 3191 | answer->GetChars() + position, |
| 3192 | prev, |
| 3193 | length); |
| 3194 | position += length - prev; |
| 3195 | } |
| 3196 | |
| 3197 | if (position == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3198 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | // Shorten string and fill |
| 3202 | int string_size = ResultSeqString::SizeFor(position); |
| 3203 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 3204 | int delta = allocated_string_size - string_size; |
| 3205 | |
| 3206 | answer->set_length(position); |
| 3207 | if (delta == 0) return *answer; |
| 3208 | |
| 3209 | Address end_of_string = answer->address() + string_size; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3210 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3211 | if (Marking::IsBlack(Marking::MarkBitFrom(*answer))) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 3212 | MemoryChunk::IncrementLiveBytesFromMutator(answer->address(), -delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3213 | } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3214 | |
| 3215 | return *answer; |
| 3216 | } |
| 3217 | |
| 3218 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3219 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceRegExpWithString) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3220 | ASSERT(args.length() == 4); |
| 3221 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3222 | CONVERT_ARG_CHECKED(String, subject, 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3223 | if (!subject->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3224 | Object* flat_subject; |
| 3225 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 3226 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 3227 | return maybe_flat_subject; |
| 3228 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3229 | } |
| 3230 | subject = String::cast(flat_subject); |
| 3231 | } |
| 3232 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3233 | CONVERT_ARG_CHECKED(String, replacement, 2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3234 | if (!replacement->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3235 | Object* flat_replacement; |
| 3236 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 3237 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 3238 | return maybe_flat_replacement; |
| 3239 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3240 | } |
| 3241 | replacement = String::cast(flat_replacement); |
| 3242 | } |
| 3243 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3244 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3245 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3246 | |
| 3247 | ASSERT(last_match_info->HasFastElements()); |
| 3248 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3249 | if (replacement->length() == 0) { |
| 3250 | if (subject->HasOnlyAsciiChars()) { |
| 3251 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3252 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3253 | } else { |
| 3254 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3255 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3259 | return StringReplaceRegExpWithString(isolate, |
| 3260 | subject, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3261 | regexp, |
| 3262 | replacement, |
| 3263 | last_match_info); |
| 3264 | } |
| 3265 | |
| 3266 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 3267 | Handle<String> Runtime::StringReplaceOneCharWithString(Isolate* isolate, |
| 3268 | Handle<String> subject, |
| 3269 | Handle<String> search, |
| 3270 | Handle<String> replace, |
| 3271 | bool* found, |
| 3272 | int recursion_limit) { |
| 3273 | if (recursion_limit == 0) return Handle<String>::null(); |
| 3274 | if (subject->IsConsString()) { |
| 3275 | ConsString* cons = ConsString::cast(*subject); |
| 3276 | Handle<String> first = Handle<String>(cons->first()); |
| 3277 | Handle<String> second = Handle<String>(cons->second()); |
| 3278 | Handle<String> new_first = |
| 3279 | StringReplaceOneCharWithString(isolate, |
| 3280 | first, |
| 3281 | search, |
| 3282 | replace, |
| 3283 | found, |
| 3284 | recursion_limit - 1); |
| 3285 | if (*found) return isolate->factory()->NewConsString(new_first, second); |
| 3286 | if (new_first.is_null()) return new_first; |
| 3287 | |
| 3288 | Handle<String> new_second = |
| 3289 | StringReplaceOneCharWithString(isolate, |
| 3290 | second, |
| 3291 | search, |
| 3292 | replace, |
| 3293 | found, |
| 3294 | recursion_limit - 1); |
| 3295 | if (*found) return isolate->factory()->NewConsString(first, new_second); |
| 3296 | if (new_second.is_null()) return new_second; |
| 3297 | |
| 3298 | return subject; |
| 3299 | } else { |
| 3300 | int index = StringMatch(isolate, subject, search, 0); |
| 3301 | if (index == -1) return subject; |
| 3302 | *found = true; |
| 3303 | Handle<String> first = isolate->factory()->NewSubString(subject, 0, index); |
| 3304 | Handle<String> cons1 = isolate->factory()->NewConsString(first, replace); |
| 3305 | Handle<String> second = |
| 3306 | isolate->factory()->NewSubString(subject, index + 1, subject->length()); |
| 3307 | return isolate->factory()->NewConsString(cons1, second); |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | |
| 3312 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceOneCharWithString) { |
| 3313 | ASSERT(args.length() == 3); |
| 3314 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3315 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 3316 | CONVERT_ARG_HANDLE_CHECKED(String, search, 1); |
| 3317 | CONVERT_ARG_HANDLE_CHECKED(String, replace, 2); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 3318 | |
| 3319 | // If the cons string tree is too deep, we simply abort the recursion and |
| 3320 | // retry with a flattened subject string. |
| 3321 | const int kRecursionLimit = 0x1000; |
| 3322 | bool found = false; |
| 3323 | Handle<String> result = |
| 3324 | Runtime::StringReplaceOneCharWithString(isolate, |
| 3325 | subject, |
| 3326 | search, |
| 3327 | replace, |
| 3328 | &found, |
| 3329 | kRecursionLimit); |
| 3330 | if (!result.is_null()) return *result; |
| 3331 | return *Runtime::StringReplaceOneCharWithString(isolate, |
| 3332 | FlattenGetString(subject), |
| 3333 | search, |
| 3334 | replace, |
| 3335 | &found, |
| 3336 | kRecursionLimit); |
| 3337 | } |
| 3338 | |
| 3339 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3340 | // Perform string match of pattern on subject, starting at start index. |
| 3341 | // Caller must ensure that 0 <= start_index <= sub->length(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3342 | // and should check that pat->length() + start_index <= sub->length(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3343 | int Runtime::StringMatch(Isolate* isolate, |
| 3344 | Handle<String> sub, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3345 | Handle<String> pat, |
| 3346 | int start_index) { |
| 3347 | ASSERT(0 <= start_index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3348 | ASSERT(start_index <= sub->length()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3349 | |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3350 | int pattern_length = pat->length(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3351 | if (pattern_length == 0) return start_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3352 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3353 | int subject_length = sub->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3354 | if (start_index + pattern_length > subject_length) return -1; |
| 3355 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3356 | if (!sub->IsFlat()) FlattenString(sub); |
| 3357 | if (!pat->IsFlat()) FlattenString(pat); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 3358 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3359 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 3360 | // Extract flattened substrings of cons strings before determining asciiness. |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3361 | String::FlatContent seq_sub = sub->GetFlatContent(); |
| 3362 | String::FlatContent seq_pat = pat->GetFlatContent(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 3363 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3364 | // dispatch on type of strings |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3365 | if (seq_pat.IsAscii()) { |
| 3366 | Vector<const char> pat_vector = seq_pat.ToAsciiVector(); |
| 3367 | if (seq_sub.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3368 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3369 | seq_sub.ToAsciiVector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3370 | pat_vector, |
| 3371 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 3372 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3373 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3374 | seq_sub.ToUC16Vector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3375 | pat_vector, |
| 3376 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 3377 | } |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3378 | Vector<const uc16> pat_vector = seq_pat.ToUC16Vector(); |
| 3379 | if (seq_sub.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3380 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3381 | seq_sub.ToAsciiVector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3382 | pat_vector, |
| 3383 | start_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3384 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3385 | return SearchString(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3386 | seq_sub.ToUC16Vector(), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3387 | pat_vector, |
| 3388 | start_index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3392 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3393 | HandleScope scope(isolate); // create a new handle scope |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3394 | ASSERT(args.length() == 3); |
| 3395 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3396 | CONVERT_ARG_HANDLE_CHECKED(String, sub, 0); |
| 3397 | CONVERT_ARG_HANDLE_CHECKED(String, pat, 1); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3398 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3399 | Object* index = args[2]; |
| 3400 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3401 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 3402 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3403 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3404 | int position = |
| 3405 | Runtime::StringMatch(isolate, sub, pat, start_index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3406 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3410 | template <typename schar, typename pchar> |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3411 | static int StringMatchBackwards(Vector<const schar> subject, |
| 3412 | Vector<const pchar> pattern, |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3413 | int idx) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3414 | int pattern_length = pattern.length(); |
| 3415 | ASSERT(pattern_length >= 1); |
| 3416 | ASSERT(idx + pattern_length <= subject.length()); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3417 | |
| 3418 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3419 | for (int i = 0; i < pattern_length; i++) { |
| 3420 | uc16 c = pattern[i]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3421 | if (c > String::kMaxAsciiCharCode) { |
| 3422 | return -1; |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3427 | pchar pattern_first_char = pattern[0]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3428 | for (int i = idx; i >= 0; i--) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3429 | if (subject[i] != pattern_first_char) continue; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3430 | int j = 1; |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3431 | while (j < pattern_length) { |
| 3432 | if (pattern[j] != subject[i+j]) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3433 | break; |
| 3434 | } |
| 3435 | j++; |
| 3436 | } |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 3437 | if (j == pattern_length) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3438 | return i; |
| 3439 | } |
| 3440 | } |
| 3441 | return -1; |
| 3442 | } |
| 3443 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3444 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLastIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3445 | HandleScope scope(isolate); // create a new handle scope |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3446 | ASSERT(args.length() == 3); |
| 3447 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3448 | CONVERT_ARG_HANDLE_CHECKED(String, sub, 0); |
| 3449 | CONVERT_ARG_HANDLE_CHECKED(String, pat, 1); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3450 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3451 | Object* index = args[2]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3452 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3453 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3454 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3455 | uint32_t pat_length = pat->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3456 | uint32_t sub_length = sub->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3457 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3458 | if (start_index + pat_length > sub_length) { |
| 3459 | start_index = sub_length - pat_length; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 3460 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3461 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3462 | if (pat_length == 0) { |
| 3463 | return Smi::FromInt(start_index); |
| 3464 | } |
| 3465 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3466 | if (!sub->IsFlat()) FlattenString(sub); |
| 3467 | if (!pat->IsFlat()) FlattenString(pat); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3468 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3469 | int position = -1; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3470 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3471 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3472 | String::FlatContent sub_content = sub->GetFlatContent(); |
| 3473 | String::FlatContent pat_content = pat->GetFlatContent(); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3474 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3475 | if (pat_content.IsAscii()) { |
| 3476 | Vector<const char> pat_vector = pat_content.ToAsciiVector(); |
| 3477 | if (sub_content.IsAscii()) { |
| 3478 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3479 | pat_vector, |
| 3480 | start_index); |
| 3481 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3482 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3483 | pat_vector, |
| 3484 | start_index); |
| 3485 | } |
| 3486 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3487 | Vector<const uc16> pat_vector = pat_content.ToUC16Vector(); |
| 3488 | if (sub_content.IsAscii()) { |
| 3489 | position = StringMatchBackwards(sub_content.ToAsciiVector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3490 | pat_vector, |
| 3491 | start_index); |
| 3492 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3493 | position = StringMatchBackwards(sub_content.ToUC16Vector(), |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3494 | pat_vector, |
| 3495 | start_index); |
| 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3503 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLocaleCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3504 | NoHandleAllocation ha; |
| 3505 | ASSERT(args.length() == 2); |
| 3506 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3507 | CONVERT_ARG_CHECKED(String, str1, 0); |
| 3508 | CONVERT_ARG_CHECKED(String, str2, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3509 | |
| 3510 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3511 | int str1_length = str1->length(); |
| 3512 | int str2_length = str2->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3513 | |
| 3514 | // Decide trivial cases without flattening. |
| 3515 | if (str1_length == 0) { |
| 3516 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3517 | return Smi::FromInt(-str2_length); |
| 3518 | } else { |
| 3519 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3520 | } |
| 3521 | |
| 3522 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3523 | |
| 3524 | // No need to flatten if we are going to find the answer on the first |
| 3525 | // character. At this point we know there is at least one character |
| 3526 | // in each string, due to the trivial case handling above. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3527 | int d = str1->Get(0) - str2->Get(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3528 | if (d != 0) return Smi::FromInt(d); |
| 3529 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3530 | str1->TryFlatten(); |
| 3531 | str2->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3532 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3533 | StringInputBuffer& buf1 = |
| 3534 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3535 | StringInputBuffer& buf2 = |
| 3536 | *isolate->runtime_state()->string_locale_compare_buf2(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3537 | |
| 3538 | buf1.Reset(str1); |
| 3539 | buf2.Reset(str2); |
| 3540 | |
| 3541 | for (int i = 0; i < end; i++) { |
| 3542 | uint16_t char1 = buf1.GetNext(); |
| 3543 | uint16_t char2 = buf2.GetNext(); |
| 3544 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3545 | } |
| 3546 | |
| 3547 | return Smi::FromInt(str1_length - str2_length); |
| 3548 | } |
| 3549 | |
| 3550 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3551 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SubString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3552 | NoHandleAllocation ha; |
| 3553 | ASSERT(args.length() == 3); |
| 3554 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3555 | CONVERT_ARG_CHECKED(String, value, 0); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3556 | int start, end; |
| 3557 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3558 | // the common case where from and to are Smis. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3559 | if (args[1]->IsSmi() && args[2]->IsSmi()) { |
| 3560 | CONVERT_SMI_ARG_CHECKED(from_number, 1); |
| 3561 | CONVERT_SMI_ARG_CHECKED(to_number, 2); |
| 3562 | start = from_number; |
| 3563 | end = to_number; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3564 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3565 | CONVERT_DOUBLE_ARG_CHECKED(from_number, 1); |
| 3566 | CONVERT_DOUBLE_ARG_CHECKED(to_number, 2); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3567 | start = FastD2I(from_number); |
| 3568 | end = FastD2I(to_number); |
| 3569 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3570 | RUNTIME_ASSERT(end >= start); |
| 3571 | RUNTIME_ASSERT(start >= 0); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3572 | RUNTIME_ASSERT(end <= value->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3573 | isolate->counters()->sub_string_runtime()->Increment(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3574 | return value->SubString(start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3578 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringMatch) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3579 | ASSERT_EQ(3, args.length()); |
| 3580 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3581 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 3582 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1); |
| 3583 | CONVERT_ARG_HANDLE_CHECKED(JSArray, regexp_info, 2); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3584 | HandleScope handles; |
| 3585 | |
| 3586 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3587 | |
| 3588 | if (match.is_null()) { |
| 3589 | return Failure::Exception(); |
| 3590 | } |
| 3591 | if (match->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3592 | return isolate->heap()->null_value(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3593 | } |
| 3594 | int length = subject->length(); |
| 3595 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 3596 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3597 | ZoneList<int> offsets(8); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3598 | int start; |
| 3599 | int end; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3600 | do { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3601 | { |
| 3602 | AssertNoAllocation no_alloc; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3603 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3604 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3605 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3606 | } |
| 3607 | offsets.Add(start); |
| 3608 | offsets.Add(end); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3609 | if (start == end) if (++end > length) break; |
| 3610 | match = RegExpImpl::Exec(regexp, subject, end, regexp_info); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3611 | if (match.is_null()) { |
| 3612 | return Failure::Exception(); |
| 3613 | } |
| 3614 | } while (!match->IsNull()); |
| 3615 | int matches = offsets.length() / 2; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3616 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3617 | Handle<String> substring = isolate->factory()-> |
| 3618 | NewSubString(subject, offsets.at(0), offsets.at(1)); |
| 3619 | elements->set(0, *substring); |
| 3620 | for (int i = 1; i < matches ; i++) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3621 | int from = offsets.at(i * 2); |
| 3622 | int to = offsets.at(i * 2 + 1); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3623 | Handle<String> substring = isolate->factory()-> |
| 3624 | NewProperSubString(subject, from, to); |
| 3625 | elements->set(i, *substring); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3626 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3627 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3628 | result->set_length(Smi::FromInt(matches)); |
| 3629 | return *result; |
| 3630 | } |
| 3631 | |
| 3632 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3633 | // Two smis before and after the match, for very long strings. |
| 3634 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3635 | |
| 3636 | |
| 3637 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3638 | Handle<JSArray> last_match_info, |
| 3639 | int match_start, |
| 3640 | int match_end) { |
| 3641 | // Fill last_match_info with a single capture. |
| 3642 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3643 | AssertNoAllocation no_gc; |
| 3644 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3645 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3646 | RegExpImpl::SetLastInput(elements, *subject); |
| 3647 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3648 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3649 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3650 | } |
| 3651 | |
| 3652 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3653 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3654 | static bool SearchStringMultiple(Isolate* isolate, |
| 3655 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3656 | Vector<const PatternChar> pattern, |
| 3657 | String* pattern_string, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3658 | FixedArrayBuilder* builder, |
| 3659 | int* match_pos) { |
| 3660 | int pos = *match_pos; |
| 3661 | int subject_length = subject.length(); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3662 | int pattern_length = pattern.length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3663 | int max_search_start = subject_length - pattern_length; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3664 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3665 | while (pos <= max_search_start) { |
| 3666 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3667 | *match_pos = pos; |
| 3668 | return false; |
| 3669 | } |
| 3670 | // Position of end of previous match. |
| 3671 | int match_end = pos + pattern_length; |
| 3672 | int new_pos = search.Search(subject, match_end); |
| 3673 | if (new_pos >= 0) { |
| 3674 | // A match. |
| 3675 | if (new_pos > match_end) { |
| 3676 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3677 | match_end, |
| 3678 | new_pos); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3679 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3680 | pos = new_pos; |
| 3681 | builder->Add(pattern_string); |
| 3682 | } else { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3683 | break; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3684 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3685 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3686 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3687 | if (pos < max_search_start) { |
| 3688 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3689 | pos + pattern_length, |
| 3690 | subject_length); |
| 3691 | } |
| 3692 | *match_pos = pos; |
| 3693 | return true; |
| 3694 | } |
| 3695 | |
| 3696 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3697 | static bool SearchStringMultiple(Isolate* isolate, |
| 3698 | Handle<String> subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3699 | Handle<String> pattern, |
| 3700 | Handle<JSArray> last_match_info, |
| 3701 | FixedArrayBuilder* builder) { |
| 3702 | ASSERT(subject->IsFlat()); |
| 3703 | ASSERT(pattern->IsFlat()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3704 | |
| 3705 | // Treating as if a previous match was before first character. |
| 3706 | int match_pos = -pattern->length(); |
| 3707 | |
| 3708 | for (;;) { // Break when search complete. |
| 3709 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3710 | AssertNoAllocation no_gc; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3711 | String::FlatContent subject_content = subject->GetFlatContent(); |
| 3712 | String::FlatContent pattern_content = pattern->GetFlatContent(); |
| 3713 | if (subject_content.IsAscii()) { |
| 3714 | Vector<const char> subject_vector = subject_content.ToAsciiVector(); |
| 3715 | if (pattern_content.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3716 | if (SearchStringMultiple(isolate, |
| 3717 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3718 | pattern_content.ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3719 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3720 | builder, |
| 3721 | &match_pos)) break; |
| 3722 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3723 | if (SearchStringMultiple(isolate, |
| 3724 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3725 | pattern_content.ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3726 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3727 | builder, |
| 3728 | &match_pos)) break; |
| 3729 | } |
| 3730 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3731 | Vector<const uc16> subject_vector = subject_content.ToUC16Vector(); |
| 3732 | if (pattern_content.IsAscii()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3733 | if (SearchStringMultiple(isolate, |
| 3734 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3735 | pattern_content.ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3736 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3737 | builder, |
| 3738 | &match_pos)) break; |
| 3739 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3740 | if (SearchStringMultiple(isolate, |
| 3741 | subject_vector, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 3742 | pattern_content.ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3743 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3744 | builder, |
| 3745 | &match_pos)) break; |
| 3746 | } |
| 3747 | } |
| 3748 | } |
| 3749 | |
| 3750 | if (match_pos >= 0) { |
| 3751 | SetLastMatchInfoNoCaptures(subject, |
| 3752 | last_match_info, |
| 3753 | match_pos, |
| 3754 | match_pos + pattern->length()); |
| 3755 | return true; |
| 3756 | } |
| 3757 | return false; // No matches at all. |
| 3758 | } |
| 3759 | |
| 3760 | |
| 3761 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3762 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3763 | Handle<String> subject, |
| 3764 | Handle<JSRegExp> regexp, |
| 3765 | Handle<JSArray> last_match_array, |
| 3766 | FixedArrayBuilder* builder) { |
| 3767 | ASSERT(subject->IsFlat()); |
| 3768 | int match_start = -1; |
| 3769 | int match_end = 0; |
| 3770 | int pos = 0; |
| 3771 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3772 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3773 | |
| 3774 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3775 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3776 | int subject_length = subject->length(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3777 | bool first = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3778 | |
| 3779 | for (;;) { // Break on failure, return on exception. |
| 3780 | RegExpImpl::IrregexpResult result = |
| 3781 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3782 | subject, |
| 3783 | pos, |
| 3784 | register_vector); |
| 3785 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3786 | match_start = register_vector[0]; |
| 3787 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3788 | if (match_end < match_start) { |
| 3789 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3790 | match_end, |
| 3791 | match_start); |
| 3792 | } |
| 3793 | match_end = register_vector[1]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3794 | HandleScope loop_scope(isolate); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3795 | if (!first) { |
| 3796 | builder->Add(*isolate->factory()->NewProperSubString(subject, |
| 3797 | match_start, |
| 3798 | match_end)); |
| 3799 | } else { |
| 3800 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3801 | match_start, |
| 3802 | match_end)); |
| 3803 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3804 | if (match_start != match_end) { |
| 3805 | pos = match_end; |
| 3806 | } else { |
| 3807 | pos = match_end + 1; |
| 3808 | if (pos > subject_length) break; |
| 3809 | } |
| 3810 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3811 | break; |
| 3812 | } else { |
| 3813 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3814 | return result; |
| 3815 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3816 | first = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | if (match_start >= 0) { |
| 3820 | if (match_end < subject_length) { |
| 3821 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3822 | match_end, |
| 3823 | subject_length); |
| 3824 | } |
| 3825 | SetLastMatchInfoNoCaptures(subject, |
| 3826 | last_match_array, |
| 3827 | match_start, |
| 3828 | match_end); |
| 3829 | return RegExpImpl::RE_SUCCESS; |
| 3830 | } else { |
| 3831 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3832 | } |
| 3833 | } |
| 3834 | |
| 3835 | |
| 3836 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3837 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3838 | Handle<String> subject, |
| 3839 | Handle<JSRegExp> regexp, |
| 3840 | Handle<JSArray> last_match_array, |
| 3841 | FixedArrayBuilder* builder) { |
| 3842 | |
| 3843 | ASSERT(subject->IsFlat()); |
| 3844 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3845 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3846 | |
| 3847 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3848 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3849 | |
| 3850 | RegExpImpl::IrregexpResult result = |
| 3851 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3852 | subject, |
| 3853 | 0, |
| 3854 | register_vector); |
| 3855 | |
| 3856 | int capture_count = regexp->CaptureCount(); |
| 3857 | int subject_length = subject->length(); |
| 3858 | |
| 3859 | // Position to search from. |
| 3860 | int pos = 0; |
| 3861 | // End of previous match. Differs from pos if match was empty. |
| 3862 | int match_end = 0; |
| 3863 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3864 | // Need to keep a copy of the previous match for creating last_match_info |
| 3865 | // at the end, so we have two vectors that we swap between. |
| 3866 | OffsetsVector registers2(required_registers); |
| 3867 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3868 | bool first = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3869 | do { |
| 3870 | int match_start = register_vector[0]; |
| 3871 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3872 | if (match_end < match_start) { |
| 3873 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3874 | match_end, |
| 3875 | match_start); |
| 3876 | } |
| 3877 | match_end = register_vector[1]; |
| 3878 | |
| 3879 | { |
| 3880 | // Avoid accumulating new handles inside loop. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3881 | HandleScope temp_scope(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3882 | // Arguments array to replace function is match, captures, index and |
| 3883 | // subject, i.e., 3 + capture count in total. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3884 | Handle<FixedArray> elements = |
| 3885 | isolate->factory()->NewFixedArray(3 + capture_count); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3886 | Handle<String> match; |
| 3887 | if (!first) { |
| 3888 | match = isolate->factory()->NewProperSubString(subject, |
| 3889 | match_start, |
| 3890 | match_end); |
| 3891 | } else { |
| 3892 | match = isolate->factory()->NewSubString(subject, |
| 3893 | match_start, |
| 3894 | match_end); |
| 3895 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3896 | elements->set(0, *match); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3897 | for (int i = 1; i <= capture_count; i++) { |
| 3898 | int start = register_vector[i * 2]; |
| 3899 | if (start >= 0) { |
| 3900 | int end = register_vector[i * 2 + 1]; |
| 3901 | ASSERT(start <= end); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3902 | Handle<String> substring; |
| 3903 | if (!first) { |
| 3904 | substring = isolate->factory()->NewProperSubString(subject, |
| 3905 | start, |
| 3906 | end); |
| 3907 | } else { |
| 3908 | substring = isolate->factory()->NewSubString(subject, start, end); |
| 3909 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3910 | elements->set(i, *substring); |
| 3911 | } else { |
| 3912 | ASSERT(register_vector[i * 2 + 1] < 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3913 | elements->set(i, isolate->heap()->undefined_value()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3917 | elements->set(capture_count + 2, *subject); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3918 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3919 | } |
| 3920 | // Swap register vectors, so the last successful match is in |
| 3921 | // prev_register_vector. |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3922 | Vector<int32_t> tmp = prev_register_vector; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3923 | prev_register_vector = register_vector; |
| 3924 | register_vector = tmp; |
| 3925 | |
| 3926 | if (match_end > match_start) { |
| 3927 | pos = match_end; |
| 3928 | } else { |
| 3929 | pos = match_end + 1; |
| 3930 | if (pos > subject_length) { |
| 3931 | break; |
| 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3936 | subject, |
| 3937 | pos, |
| 3938 | register_vector); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3939 | first = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3940 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3941 | |
| 3942 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3943 | // Finished matching, with at least one match. |
| 3944 | if (match_end < subject_length) { |
| 3945 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3946 | match_end, |
| 3947 | subject_length); |
| 3948 | } |
| 3949 | |
| 3950 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3951 | int last_match_array_size = |
| 3952 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3953 | last_match_array->EnsureSize(last_match_array_size); |
| 3954 | AssertNoAllocation no_gc; |
| 3955 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3956 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3957 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3958 | RegExpImpl::SetLastInput(elements, *subject); |
| 3959 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3960 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3961 | } |
| 3962 | return RegExpImpl::RE_SUCCESS; |
| 3963 | } |
| 3964 | } |
| 3965 | // No matches at all, return failure or exception result directly. |
| 3966 | return result; |
| 3967 | } |
| 3968 | |
| 3969 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3970 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3971 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3972 | HandleScope handles(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3973 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3974 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 1); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 3975 | if (!subject->IsFlat()) FlattenString(subject); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 3976 | CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); |
| 3977 | CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 2); |
| 3978 | CONVERT_ARG_HANDLE_CHECKED(JSArray, result_array, 3); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3979 | |
| 3980 | ASSERT(last_match_info->HasFastElements()); |
| 3981 | ASSERT(regexp->GetFlags().is_global()); |
| 3982 | Handle<FixedArray> result_elements; |
| 3983 | if (result_array->HasFastElements()) { |
| 3984 | result_elements = |
| 3985 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3986 | } |
| 3987 | if (result_elements.is_null() || result_elements->length() < 16) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3988 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3989 | } |
| 3990 | FixedArrayBuilder builder(result_elements); |
| 3991 | |
| 3992 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 3993 | Handle<String> pattern( |
| 3994 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3995 | ASSERT(pattern->IsFlat()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3996 | if (SearchStringMultiple(isolate, subject, pattern, |
| 3997 | last_match_info, &builder)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3998 | return *builder.ToJSArray(result_array); |
| 3999 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4000 | return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 4004 | |
| 4005 | RegExpImpl::IrregexpResult result; |
| 4006 | if (regexp->CaptureCount() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4007 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 4008 | subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4009 | regexp, |
| 4010 | last_match_info, |
| 4011 | &builder); |
| 4012 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4013 | result = SearchRegExpMultiple(isolate, |
| 4014 | subject, |
| 4015 | regexp, |
| 4016 | last_match_info, |
| 4017 | &builder); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4018 | } |
| 4019 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4020 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4021 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 4022 | return Failure::Exception(); |
| 4023 | } |
| 4024 | |
| 4025 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4026 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToRadixString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4027 | NoHandleAllocation ha; |
| 4028 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4029 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 4030 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4031 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4032 | // Fast case where the result is a one character string. |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 4033 | if (args[0]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4034 | int value = args.smi_at(0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4035 | if (value >= 0 && value < radix) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4036 | // Character array used for conversion. |
| 4037 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4038 | return isolate->heap()-> |
| 4039 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | // Slow case. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4044 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4045 | if (isnan(value)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4046 | return *isolate->factory()->nan_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4047 | } |
| 4048 | if (isinf(value)) { |
| 4049 | if (value < 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4050 | return *isolate->factory()->minus_infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4051 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4052 | return *isolate->factory()->infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4053 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4054 | char* str = DoubleToRadixCString(value, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4055 | MaybeObject* result = |
| 4056 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4057 | DeleteArray(str); |
| 4058 | return result; |
| 4059 | } |
| 4060 | |
| 4061 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4062 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToFixed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4063 | NoHandleAllocation ha; |
| 4064 | ASSERT(args.length() == 2); |
| 4065 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4066 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4067 | if (isnan(value)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4068 | return *isolate->factory()->nan_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4069 | } |
| 4070 | if (isinf(value)) { |
| 4071 | if (value < 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4072 | return *isolate->factory()->minus_infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4073 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4074 | return *isolate->factory()->infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4075 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4076 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4077 | int f = FastD2I(f_number); |
| 4078 | RUNTIME_ASSERT(f >= 0); |
| 4079 | char* str = DoubleToFixedCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4080 | MaybeObject* res = |
| 4081 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4082 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4083 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4087 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToExponential) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4088 | NoHandleAllocation ha; |
| 4089 | ASSERT(args.length() == 2); |
| 4090 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4091 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4092 | if (isnan(value)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4093 | return *isolate->factory()->nan_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4094 | } |
| 4095 | if (isinf(value)) { |
| 4096 | if (value < 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4097 | return *isolate->factory()->minus_infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4098 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4099 | return *isolate->factory()->infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4100 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4101 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4102 | int f = FastD2I(f_number); |
| 4103 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 4104 | char* str = DoubleToExponentialCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4105 | MaybeObject* res = |
| 4106 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4107 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4108 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4112 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPrecision) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4113 | NoHandleAllocation ha; |
| 4114 | ASSERT(args.length() == 2); |
| 4115 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4116 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4117 | if (isnan(value)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4118 | return *isolate->factory()->nan_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4119 | } |
| 4120 | if (isinf(value)) { |
| 4121 | if (value < 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4122 | return *isolate->factory()->minus_infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4123 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4124 | return *isolate->factory()->infinity_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4125 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4126 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4127 | int f = FastD2I(f_number); |
| 4128 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 4129 | char* str = DoubleToPrecisionCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4130 | MaybeObject* res = |
| 4131 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4132 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4133 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | |
| 4137 | // Returns a single character string where first character equals |
| 4138 | // string->Get(index). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4139 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4140 | if (index < static_cast<uint32_t>(string->length())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4141 | string->TryFlatten(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4142 | return LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4143 | string->Get(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4144 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4145 | return Execution::CharAt(string, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4149 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 4150 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4151 | uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4152 | // Handle [] indexing on Strings |
| 4153 | if (object->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4154 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 4155 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | // Handle [] indexing on String objects |
| 4159 | if (object->IsStringObjectWithCharacterAt(index)) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4160 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 4161 | Handle<Object> result = |
| 4162 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 4163 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4167 | return object->GetPrototype()->GetElement(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | return object->GetElement(index); |
| 4171 | } |
| 4172 | |
| 4173 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4174 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 4175 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4176 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4177 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4178 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4179 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4180 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4181 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4182 | isolate->factory()->NewTypeError("non_object_property_load", |
| 4183 | HandleVector(args, 2)); |
| 4184 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | // Check if the given key is an array index. |
| 4188 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4189 | if (key->ToArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4190 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | // 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] | 4194 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4195 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4196 | name = Handle<String>::cast(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4198 | bool has_pending_exception = false; |
| 4199 | Handle<Object> converted = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4200 | Execution::ToString(key, &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4201 | if (has_pending_exception) return Failure::Exception(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4202 | name = Handle<String>::cast(converted); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4205 | // 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] | 4206 | // the element if so. |
| 4207 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4208 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4209 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4210 | return object->GetProperty(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4211 | } |
| 4212 | } |
| 4213 | |
| 4214 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4215 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4216 | NoHandleAllocation ha; |
| 4217 | ASSERT(args.length() == 2); |
| 4218 | |
| 4219 | Handle<Object> object = args.at<Object>(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4220 | Handle<Object> key = args.at<Object>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4221 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4222 | return Runtime::GetObjectProperty(isolate, object, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4226 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4227 | RUNTIME_FUNCTION(MaybeObject*, Runtime_KeyedGetProperty) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4228 | NoHandleAllocation ha; |
| 4229 | ASSERT(args.length() == 2); |
| 4230 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4231 | // Fast cases for getting named properties of the receiver JSObject |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4232 | // itself. |
| 4233 | // |
| 4234 | // The global proxy objects has to be excluded since LocalLookup on |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 4235 | // 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] | 4236 | // global proxy object never has properties. This is the case |
| 4237 | // because the global proxy object forwards everything to its hidden |
| 4238 | // prototype including local lookups. |
| 4239 | // |
| 4240 | // Additionally, we need to make sure that we do not cache results |
| 4241 | // for objects that require access checks. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4242 | if (args[0]->IsJSObject()) { |
| 4243 | if (!args[0]->IsJSGlobalProxy() && |
| 4244 | !args[0]->IsAccessCheckNeeded() && |
| 4245 | args[1]->IsString()) { |
| 4246 | JSObject* receiver = JSObject::cast(args[0]); |
| 4247 | String* key = String::cast(args[1]); |
| 4248 | if (receiver->HasFastProperties()) { |
| 4249 | // Attempt to use lookup cache. |
| 4250 | Map* receiver_map = receiver->map(); |
| 4251 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 4252 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
| 4253 | if (offset != -1) { |
| 4254 | Object* value = receiver->FastPropertyAt(offset); |
| 4255 | return value->IsTheHole() |
| 4256 | ? isolate->heap()->undefined_value() |
| 4257 | : value; |
| 4258 | } |
| 4259 | // Lookup cache miss. Perform lookup and update the cache if |
| 4260 | // appropriate. |
| 4261 | LookupResult result(isolate); |
| 4262 | receiver->LocalLookup(key, &result); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 4263 | if (result.IsFound() && result.type() == FIELD) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4264 | int offset = result.GetFieldIndex(); |
| 4265 | keyed_lookup_cache->Update(receiver_map, key, offset); |
| 4266 | return receiver->FastPropertyAt(offset); |
| 4267 | } |
| 4268 | } else { |
| 4269 | // Attempt dictionary lookup. |
| 4270 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 4271 | int entry = dictionary->FindEntry(key); |
| 4272 | if ((entry != StringDictionary::kNotFound) && |
| 4273 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
| 4274 | Object* value = dictionary->ValueAt(entry); |
| 4275 | if (!receiver->IsGlobalObject()) return value; |
| 4276 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 4277 | if (!value->IsTheHole()) return value; |
| 4278 | // If value is the hole do the general lookup. |
| 4279 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4280 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4281 | } else if (FLAG_smi_only_arrays && args.at<Object>(1)->IsSmi()) { |
| 4282 | // JSObject without a string key. If the key is a Smi, check for a |
| 4283 | // definite out-of-bounds access to elements, which is a strong indicator |
| 4284 | // that subsequent accesses will also call the runtime. Proactively |
| 4285 | // transition elements to FAST_ELEMENTS to avoid excessive boxing of |
| 4286 | // doubles for those future calls in the case that the elements would |
| 4287 | // become FAST_DOUBLE_ELEMENTS. |
| 4288 | Handle<JSObject> js_object(args.at<JSObject>(0)); |
| 4289 | ElementsKind elements_kind = js_object->GetElementsKind(); |
| 4290 | if (elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4291 | elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 4292 | FixedArrayBase* elements = js_object->elements(); |
| 4293 | if (args.at<Smi>(1)->value() >= elements->length()) { |
| 4294 | MaybeObject* maybe_object = TransitionElements(js_object, |
| 4295 | FAST_ELEMENTS, |
| 4296 | isolate); |
| 4297 | if (maybe_object->IsFailure()) return maybe_object; |
| 4298 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4299 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4300 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4301 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 4302 | // Fast case for string indexing using [] with a smi index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4303 | HandleScope scope(isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4304 | Handle<String> str = args.at<String>(0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4305 | int index = args.smi_at(1); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4306 | if (index >= 0 && index < str->length()) { |
| 4307 | Handle<Object> result = GetCharAt(str, index); |
| 4308 | return *result; |
| 4309 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4310 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4311 | |
| 4312 | // Fall back to GetObjectProperty. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4313 | return Runtime::GetObjectProperty(isolate, |
| 4314 | args.at<Object>(0), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 4315 | args.at<Object>(1)); |
| 4316 | } |
| 4317 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4318 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4319 | // There are 3 cases that lead here: |
| 4320 | // Step 4b - define a new accessor property. |
| 4321 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 4322 | // Step 12 - update an existing accessor property with an accessor or generic |
| 4323 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4324 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineAccessorProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4325 | ASSERT(args.length() == 5); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4326 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4327 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 4328 | CONVERT_ARG_CHECKED(String, name, 1); |
| 4329 | CONVERT_SMI_ARG_CHECKED(flag_setter, 2); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4330 | Object* fun = args[3]; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4331 | CONVERT_SMI_ARG_CHECKED(unchecked, 4); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4332 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4333 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4334 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4335 | |
| 4336 | RUNTIME_ASSERT(!obj->IsNull()); |
| 4337 | RUNTIME_ASSERT(fun->IsSpecFunction() || fun->IsUndefined()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4338 | return obj->DefineAccessor(name, flag_setter == 0, fun, attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4341 | // Implements part of 8.12.9 DefineOwnProperty. |
| 4342 | // There are 3 cases that lead here: |
| 4343 | // Step 4a - define a new data property. |
| 4344 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 4345 | // Step 12 - update an existing data property with a data or generic |
| 4346 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4347 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineDataProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4348 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4349 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4350 | CONVERT_ARG_HANDLE_CHECKED(JSObject, js_object, 0); |
| 4351 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4352 | Handle<Object> obj_value = args.at<Object>(2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4353 | CONVERT_SMI_ARG_CHECKED(unchecked, 3); |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 4354 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4355 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4356 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 4357 | |
| 4358 | // Check if this is an element. |
| 4359 | uint32_t index; |
| 4360 | bool is_element = name->AsArrayIndex(&index); |
| 4361 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4362 | // Special case for elements if any of the flags might be involved. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4363 | // If elements are in fast case we always implicitly assume that: |
| 4364 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4365 | if (is_element && (attr != NONE || |
| 4366 | js_object->HasLocalElement(index) == JSObject::DICTIONARY_ELEMENT)) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4367 | // Normalize the elements to enable attributes on the property. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4368 | if (js_object->IsJSGlobalProxy()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 4369 | // We do not need to do access checks here since these has already |
| 4370 | // been performed by the call to GetOwnProperty. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4371 | Handle<Object> proto(js_object->GetPrototype()); |
| 4372 | // If proxy is detached, ignore the assignment. Alternatively, |
| 4373 | // we could throw an exception. |
| 4374 | if (proto->IsNull()) return *obj_value; |
| 4375 | js_object = Handle<JSObject>::cast(proto); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4376 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4377 | |
| 4378 | // Don't allow element properties to be redefined on objects with external |
| 4379 | // array elements. |
| 4380 | if (js_object->HasExternalArrayElements()) { |
| 4381 | Handle<Object> args[2] = { js_object, name }; |
| 4382 | Handle<Object> error = |
| 4383 | isolate->factory()->NewTypeError("redef_external_array_element", |
| 4384 | HandleVector(args, 2)); |
| 4385 | return isolate->Throw(*error); |
| 4386 | } |
| 4387 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4388 | Handle<SeededNumberDictionary> dictionary = |
| 4389 | JSObject::NormalizeElements(js_object); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4390 | // Make sure that we never go back to fast case. |
| 4391 | dictionary->set_requires_slow_elements(); |
| 4392 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4393 | Handle<SeededNumberDictionary> extended_dictionary = |
| 4394 | SeededNumberDictionary::Set(dictionary, index, obj_value, details); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4395 | if (*extended_dictionary != *dictionary) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 4396 | if (js_object->GetElementsKind() == NON_STRICT_ARGUMENTS_ELEMENTS) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 4397 | FixedArray::cast(js_object->elements())->set(1, *extended_dictionary); |
| 4398 | } else { |
| 4399 | js_object->set_elements(*extended_dictionary); |
| 4400 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 4401 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4402 | return *obj_value; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4405 | LookupResult result(isolate); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4406 | js_object->LocalLookupRealNamedProperty(*name, &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4407 | |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 4408 | // Special case for callback properties. |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 4409 | if (result.IsFound() && result.type() == CALLBACKS) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 4410 | Object* callback = result.GetCallbackObject(); |
| 4411 | // To be compatible with Safari we do not change the value on API objects |
| 4412 | // in Object.defineProperty(). Firefox disagrees here, and actually changes |
| 4413 | // the value. |
| 4414 | if (callback->IsAccessorInfo()) { |
| 4415 | return isolate->heap()->undefined_value(); |
| 4416 | } |
| 4417 | // Avoid redefining foreign callback as data property, just use the stored |
| 4418 | // setter to update the value instead. |
| 4419 | // TODO(mstarzinger): So far this only works if property attributes don't |
| 4420 | // change, this should be fixed once we cleanup the underlying code. |
| 4421 | if (callback->IsForeign() && result.GetAttributes() == attr) { |
| 4422 | return js_object->SetPropertyWithCallback(callback, |
| 4423 | *name, |
| 4424 | *obj_value, |
| 4425 | result.holder(), |
| 4426 | kStrictMode); |
| 4427 | } |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4430 | // Take special care when attributes are different and there is already |
| 4431 | // a property. For simplicity we normalize the property which enables us |
| 4432 | // to not worry about changing the instance_descriptor and creating a new |
| 4433 | // map. The current version of SetObjectProperty does not handle attributes |
| 4434 | // correctly in the case where a property is a field and is reset with |
| 4435 | // new attributes. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4436 | if (result.IsProperty() && |
| 4437 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4438 | // New attributes - normalize to avoid writing to instance descriptor |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 4439 | if (js_object->IsJSGlobalProxy()) { |
| 4440 | // Since the result is a property, the prototype will exist so |
| 4441 | // we don't have to check for null. |
| 4442 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4443 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4444 | JSObject::NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4445 | // Use IgnoreAttributes version since a readonly property may be |
| 4446 | // overridden and SetProperty does not allow this. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4447 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 4448 | *obj_value, |
| 4449 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4450 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 4451 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4452 | return Runtime::ForceSetObjectProperty(isolate, |
| 4453 | js_object, |
| 4454 | name, |
| 4455 | obj_value, |
| 4456 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4460 | // Special case for elements if any of the flags are true. |
| 4461 | // If elements are in fast case we always implicitly assume that: |
| 4462 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 4463 | static MaybeObject* NormalizeObjectSetElement(Isolate* isolate, |
| 4464 | Handle<JSObject> js_object, |
| 4465 | uint32_t index, |
| 4466 | Handle<Object> value, |
| 4467 | PropertyAttributes attr) { |
| 4468 | // Normalize the elements to enable attributes on the property. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4469 | Handle<SeededNumberDictionary> dictionary = |
| 4470 | JSObject::NormalizeElements(js_object); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4471 | // Make sure that we never go back to fast case. |
| 4472 | dictionary->set_requires_slow_elements(); |
| 4473 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4474 | Handle<SeededNumberDictionary> extended_dictionary = |
| 4475 | SeededNumberDictionary::Set(dictionary, index, value, details); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4476 | if (*extended_dictionary != *dictionary) { |
| 4477 | js_object->set_elements(*extended_dictionary); |
| 4478 | } |
| 4479 | return *value; |
| 4480 | } |
| 4481 | |
| 4482 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4483 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 4484 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4485 | Handle<Object> key, |
| 4486 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4487 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4488 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4489 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4490 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4491 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4492 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4493 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4494 | isolate->factory()->NewTypeError("non_object_property_store", |
| 4495 | HandleVector(args, 2)); |
| 4496 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4499 | if (object->IsJSProxy()) { |
| 4500 | bool has_pending_exception = false; |
| 4501 | Handle<Object> name = Execution::ToString(key, &has_pending_exception); |
| 4502 | if (has_pending_exception) return Failure::Exception(); |
| 4503 | return JSProxy::cast(*object)->SetProperty( |
| 4504 | String::cast(*name), *value, attr, strict_mode); |
| 4505 | } |
| 4506 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4507 | // If the object isn't a JavaScript object, we ignore the store. |
| 4508 | if (!object->IsJSObject()) return *value; |
| 4509 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4510 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 4511 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4512 | // Check if the given key is an array index. |
| 4513 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4514 | if (key->ToArrayIndex(&index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4515 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4516 | // of a string using [] notation. We need to support this too in |
| 4517 | // JavaScript. |
| 4518 | // In the case of a String object we just need to redirect the assignment to |
| 4519 | // the underlying string if the index is in range. Since the underlying |
| 4520 | // string does nothing with the assignment then we can ignore such |
| 4521 | // assignments. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4522 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4523 | return *value; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4524 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4525 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4526 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4527 | return NormalizeObjectSetElement(isolate, js_object, index, value, attr); |
| 4528 | } |
| 4529 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4530 | Handle<Object> result = |
| 4531 | JSObject::SetElement(js_object, index, value, strict_mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4532 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4533 | return *value; |
| 4534 | } |
| 4535 | |
| 4536 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4537 | Handle<Object> result; |
| 4538 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4539 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4540 | return NormalizeObjectSetElement(isolate, |
| 4541 | js_object, |
| 4542 | index, |
| 4543 | value, |
| 4544 | attr); |
| 4545 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4546 | result = |
| 4547 | JSObject::SetElement(js_object, index, value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4548 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4549 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4550 | key_string->TryFlatten(); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4551 | result = JSReceiver::SetProperty( |
| 4552 | js_object, key_string, value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4553 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4554 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4555 | return *value; |
| 4556 | } |
| 4557 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4558 | // Call-back into JavaScript to convert the key to a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4559 | bool has_pending_exception = false; |
| 4560 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4561 | if (has_pending_exception) return Failure::Exception(); |
| 4562 | Handle<String> name = Handle<String>::cast(converted); |
| 4563 | |
| 4564 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4565 | return js_object->SetElement(index, *value, strict_mode, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4566 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4567 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4572 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4573 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4574 | Handle<Object> key, |
| 4575 | Handle<Object> value, |
| 4576 | PropertyAttributes attr) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4577 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Check if the given key is an array index. |
| 4580 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4581 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4582 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4583 | // of a string using [] notation. We need to support this too in |
| 4584 | // JavaScript. |
| 4585 | // In the case of a String object we just need to redirect the assignment to |
| 4586 | // the underlying string if the index is in range. Since the underlying |
| 4587 | // string does nothing with the assignment then we can ignore such |
| 4588 | // assignments. |
| 4589 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4590 | return *value; |
| 4591 | } |
| 4592 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4593 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | if (key->IsString()) { |
| 4597 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4598 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4599 | } else { |
| 4600 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4601 | key_string->TryFlatten(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4602 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4603 | *value, |
| 4604 | attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | // Call-back into JavaScript to convert the key to a string. |
| 4609 | bool has_pending_exception = false; |
| 4610 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4611 | if (has_pending_exception) return Failure::Exception(); |
| 4612 | Handle<String> name = Handle<String>::cast(converted); |
| 4613 | |
| 4614 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4615 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4616 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4617 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4618 | } |
| 4619 | } |
| 4620 | |
| 4621 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4622 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4623 | Handle<JSReceiver> receiver, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4624 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4625 | HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4626 | |
| 4627 | // Check if the given key is an array index. |
| 4628 | uint32_t index; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4629 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4630 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4631 | // characters of a string using [] notation. In the case of a |
| 4632 | // String object we just need to redirect the deletion to the |
| 4633 | // underlying string if the index is in range. Since the |
| 4634 | // underlying string does nothing with the deletion, we can ignore |
| 4635 | // such deletions. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4636 | if (receiver->IsStringObjectWithCharacterAt(index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4637 | return isolate->heap()->true_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4640 | return receiver->DeleteElement(index, JSReceiver::FORCE_DELETION); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | Handle<String> key_string; |
| 4644 | if (key->IsString()) { |
| 4645 | key_string = Handle<String>::cast(key); |
| 4646 | } else { |
| 4647 | // Call-back into JavaScript to convert the key to a string. |
| 4648 | bool has_pending_exception = false; |
| 4649 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4650 | if (has_pending_exception) return Failure::Exception(); |
| 4651 | key_string = Handle<String>::cast(converted); |
| 4652 | } |
| 4653 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4654 | key_string->TryFlatten(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4655 | return receiver->DeleteProperty(*key_string, JSReceiver::FORCE_DELETION); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
| 4658 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4659 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4660 | NoHandleAllocation ha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4661 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4662 | |
| 4663 | Handle<Object> object = args.at<Object>(0); |
| 4664 | Handle<Object> key = args.at<Object>(1); |
| 4665 | Handle<Object> value = args.at<Object>(2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4666 | CONVERT_SMI_ARG_CHECKED(unchecked_attributes, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4667 | RUNTIME_ASSERT( |
| 4668 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4669 | // Compute attributes. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4670 | PropertyAttributes attributes = |
| 4671 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4672 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4673 | StrictModeFlag strict_mode = kNonStrictMode; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4674 | if (args.length() == 5) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4675 | CONVERT_STRICT_MODE_ARG_CHECKED(strict_mode_flag, 4); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4676 | strict_mode = strict_mode_flag; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4677 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4678 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4679 | return Runtime::SetObjectProperty(isolate, |
| 4680 | object, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4681 | key, |
| 4682 | value, |
| 4683 | attributes, |
| 4684 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4688 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TransitionElementsSmiToDouble) { |
| 4689 | NoHandleAllocation ha; |
| 4690 | RUNTIME_ASSERT(args.length() == 1); |
| 4691 | Handle<Object> object = args.at<Object>(0); |
| 4692 | return TransitionElements(object, FAST_DOUBLE_ELEMENTS, isolate); |
| 4693 | } |
| 4694 | |
| 4695 | |
| 4696 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TransitionElementsDoubleToObject) { |
| 4697 | NoHandleAllocation ha; |
| 4698 | RUNTIME_ASSERT(args.length() == 1); |
| 4699 | Handle<Object> object = args.at<Object>(0); |
| 4700 | return TransitionElements(object, FAST_ELEMENTS, isolate); |
| 4701 | } |
| 4702 | |
| 4703 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4704 | // Set the native flag on the function. |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4705 | // This is used to decide if we should transform null and undefined |
| 4706 | // into the global object when doing call and apply. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4707 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNativeFlag) { |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4708 | NoHandleAllocation ha; |
| 4709 | RUNTIME_ASSERT(args.length() == 1); |
| 4710 | |
| 4711 | Handle<Object> object = args.at<Object>(0); |
| 4712 | |
| 4713 | if (object->IsJSFunction()) { |
| 4714 | JSFunction* func = JSFunction::cast(*object); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4715 | func->shared()->set_native(true); |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4716 | } |
| 4717 | return isolate->heap()->undefined_value(); |
| 4718 | } |
| 4719 | |
| 4720 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4721 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreArrayLiteralElement) { |
| 4722 | RUNTIME_ASSERT(args.length() == 5); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4723 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4724 | CONVERT_SMI_ARG_CHECKED(store_index, 1); |
| 4725 | Handle<Object> value = args.at<Object>(2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4726 | CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 3); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4727 | CONVERT_SMI_ARG_CHECKED(literal_index, 4); |
| 4728 | HandleScope scope; |
| 4729 | |
| 4730 | Object* raw_boilerplate_object = literals->get(literal_index); |
| 4731 | Handle<JSArray> boilerplate_object(JSArray::cast(raw_boilerplate_object)); |
| 4732 | #if DEBUG |
| 4733 | ElementsKind elements_kind = object->GetElementsKind(); |
| 4734 | #endif |
| 4735 | ASSERT(elements_kind <= FAST_DOUBLE_ELEMENTS); |
| 4736 | // Smis should never trigger transitions. |
| 4737 | ASSERT(!value->IsSmi()); |
| 4738 | |
| 4739 | if (value->IsNumber()) { |
| 4740 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4741 | JSObject::TransitionElementsKind(object, FAST_DOUBLE_ELEMENTS); |
| 4742 | JSObject::TransitionElementsKind(boilerplate_object, FAST_DOUBLE_ELEMENTS); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4743 | ASSERT(object->GetElementsKind() == FAST_DOUBLE_ELEMENTS); |
| 4744 | FixedDoubleArray* double_array = |
| 4745 | FixedDoubleArray::cast(object->elements()); |
| 4746 | HeapNumber* number = HeapNumber::cast(*value); |
| 4747 | double_array->set(store_index, number->Number()); |
| 4748 | } else { |
| 4749 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS || |
| 4750 | elements_kind == FAST_DOUBLE_ELEMENTS); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4751 | JSObject::TransitionElementsKind(object, FAST_ELEMENTS); |
| 4752 | JSObject::TransitionElementsKind(boilerplate_object, FAST_ELEMENTS); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 4753 | FixedArray* object_array = |
| 4754 | FixedArray::cast(object->elements()); |
| 4755 | object_array->set(store_index, *value); |
| 4756 | } |
| 4757 | return *object; |
| 4758 | } |
| 4759 | |
| 4760 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4761 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4762 | // exist, it will be added with attributes NONE. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4763 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4764 | NoHandleAllocation ha; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4765 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4766 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 4767 | CONVERT_ARG_CHECKED(String, name, 1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4768 | // Compute attributes. |
| 4769 | PropertyAttributes attributes = NONE; |
| 4770 | if (args.length() == 4) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4771 | CONVERT_SMI_ARG_CHECKED(unchecked_value, 3); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4772 | // Only attribute bits should be set. |
| 4773 | RUNTIME_ASSERT( |
| 4774 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4775 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4776 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4777 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4778 | return object-> |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4779 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4783 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4784 | NoHandleAllocation ha; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4785 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4786 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4787 | CONVERT_ARG_CHECKED(JSReceiver, object, 0); |
| 4788 | CONVERT_ARG_CHECKED(String, key, 1); |
| 4789 | CONVERT_STRICT_MODE_ARG_CHECKED(strict_mode, 2); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 4790 | return object->DeleteProperty(key, (strict_mode == kStrictMode) |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 4791 | ? JSReceiver::STRICT_DELETION |
| 4792 | : JSReceiver::NORMAL_DELETION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4796 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4797 | Handle<JSObject> object, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4798 | Handle<String> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4799 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4800 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4801 | // then we have to check it for properties, because they are supposed to |
| 4802 | // look like they are on this object. |
| 4803 | Handle<Object> proto(object->GetPrototype()); |
| 4804 | if (proto->IsJSObject() && |
| 4805 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4806 | return HasLocalPropertyImplementation(isolate, |
| 4807 | Handle<JSObject>::cast(proto), |
| 4808 | key); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4809 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4810 | return isolate->heap()->false_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4814 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLocalProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4815 | NoHandleAllocation ha; |
| 4816 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4817 | CONVERT_ARG_CHECKED(String, key, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4818 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4819 | uint32_t index; |
| 4820 | const bool key_is_array_index = key->AsArrayIndex(&index); |
| 4821 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4822 | Object* obj = args[0]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4823 | // Only JS objects can have properties. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4824 | if (obj->IsJSObject()) { |
| 4825 | JSObject* object = JSObject::cast(obj); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4826 | // Fast case: either the key is a real named property or it is not |
| 4827 | // an array index and there are no interceptors or hidden |
| 4828 | // prototypes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4829 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4830 | Map* map = object->map(); |
| 4831 | if (!key_is_array_index && |
| 4832 | !map->has_named_interceptor() && |
| 4833 | !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) { |
| 4834 | return isolate->heap()->false_value(); |
| 4835 | } |
| 4836 | // Slow case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4837 | HandleScope scope(isolate); |
| 4838 | return HasLocalPropertyImplementation(isolate, |
| 4839 | Handle<JSObject>(object), |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4840 | Handle<String>(key)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4841 | } else if (obj->IsString() && key_is_array_index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4842 | // Well, there is one exception: Handle [] on strings. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4843 | String* string = String::cast(obj); |
| 4844 | if (index < static_cast<uint32_t>(string->length())) { |
| 4845 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4846 | } |
| 4847 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4848 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4852 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4853 | NoHandleAllocation na; |
| 4854 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4855 | CONVERT_ARG_CHECKED(JSReceiver, receiver, 0); |
| 4856 | CONVERT_ARG_CHECKED(String, key, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4857 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4858 | bool result = receiver->HasProperty(key); |
| 4859 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 4860 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4864 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasElement) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4865 | NoHandleAllocation na; |
| 4866 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4867 | CONVERT_ARG_CHECKED(JSReceiver, receiver, 0); |
| 4868 | CONVERT_SMI_ARG_CHECKED(index, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4869 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4870 | bool result = receiver->HasElement(index); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4871 | if (isolate->has_pending_exception()) return Failure::Exception(); |
| 4872 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4876 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsPropertyEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4877 | NoHandleAllocation ha; |
| 4878 | ASSERT(args.length() == 2); |
| 4879 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4880 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 4881 | CONVERT_ARG_CHECKED(String, key, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4882 | |
| 4883 | uint32_t index; |
| 4884 | if (key->AsArrayIndex(&index)) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4885 | JSObject::LocalElementType type = object->HasLocalElement(index); |
| 4886 | switch (type) { |
| 4887 | case JSObject::UNDEFINED_ELEMENT: |
| 4888 | case JSObject::STRING_CHARACTER_ELEMENT: |
| 4889 | return isolate->heap()->false_value(); |
| 4890 | case JSObject::INTERCEPTED_ELEMENT: |
| 4891 | case JSObject::FAST_ELEMENT: |
| 4892 | return isolate->heap()->true_value(); |
| 4893 | case JSObject::DICTIONARY_ELEMENT: { |
| 4894 | if (object->IsJSGlobalProxy()) { |
| 4895 | Object* proto = object->GetPrototype(); |
| 4896 | if (proto->IsNull()) { |
| 4897 | return isolate->heap()->false_value(); |
| 4898 | } |
| 4899 | ASSERT(proto->IsJSGlobalObject()); |
| 4900 | object = JSObject::cast(proto); |
| 4901 | } |
| 4902 | FixedArray* elements = FixedArray::cast(object->elements()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4903 | SeededNumberDictionary* dictionary = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4904 | if (elements->map() == |
| 4905 | isolate->heap()->non_strict_arguments_elements_map()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4906 | dictionary = SeededNumberDictionary::cast(elements->get(1)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4907 | } else { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4908 | dictionary = SeededNumberDictionary::cast(elements); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4909 | } |
| 4910 | int entry = dictionary->FindEntry(index); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4911 | ASSERT(entry != SeededNumberDictionary::kNotFound); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 4912 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 4913 | return isolate->heap()->ToBoolean(!details.IsDontEnum()); |
| 4914 | } |
| 4915 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4918 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4919 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4920 | } |
| 4921 | |
| 4922 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4923 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4924 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4925 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4926 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, object, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4927 | bool threw = false; |
| 4928 | Handle<JSArray> result = GetKeysFor(object, &threw); |
| 4929 | if (threw) return Failure::Exception(); |
| 4930 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | |
| 4934 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4935 | // or, if the given object has an enum cache that contains |
| 4936 | // all enumerable properties of the object and its prototypes |
| 4937 | // have none, the map of the object. This is used to speed up |
| 4938 | // the check for deletions during a for-in. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4939 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNamesFast) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4940 | ASSERT(args.length() == 1); |
| 4941 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4942 | CONVERT_ARG_CHECKED(JSReceiver, raw_object, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4943 | |
| 4944 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4945 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4946 | HandleScope scope(isolate); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 4947 | Handle<JSReceiver> object(raw_object); |
| 4948 | bool threw = false; |
| 4949 | Handle<FixedArray> content = |
| 4950 | GetKeysInFixedArrayFor(object, INCLUDE_PROTOS, &threw); |
| 4951 | if (threw) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4952 | |
| 4953 | // Test again, since cache may have been built by preceding call. |
| 4954 | if (object->IsSimpleEnum()) return object->map(); |
| 4955 | |
| 4956 | return *content; |
| 4957 | } |
| 4958 | |
| 4959 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4960 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4961 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4962 | // is prototype for. |
| 4963 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4964 | int count = 1; |
| 4965 | Object* proto = obj->GetPrototype(); |
| 4966 | while (proto->IsJSObject() && |
| 4967 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4968 | count++; |
| 4969 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4970 | } |
| 4971 | return count; |
| 4972 | } |
| 4973 | |
| 4974 | |
| 4975 | // Return the names of the local named properties. |
| 4976 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4977 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4978 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4979 | ASSERT(args.length() == 1); |
| 4980 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4981 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4982 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 4983 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4984 | |
| 4985 | // Skip the global proxy as it has no properties and always delegates to the |
| 4986 | // real global object. |
| 4987 | if (obj->IsJSGlobalProxy()) { |
| 4988 | // Only collect names if access is permitted. |
| 4989 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4990 | !isolate->MayNamedAccess(*obj, |
| 4991 | isolate->heap()->undefined_value(), |
| 4992 | v8::ACCESS_KEYS)) { |
| 4993 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4994 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4995 | } |
| 4996 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4997 | } |
| 4998 | |
| 4999 | // Find the number of objects making up this. |
| 5000 | int length = LocalPrototypeChainLength(*obj); |
| 5001 | |
| 5002 | // Find the number of local properties for each of the objects. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 5003 | ScopedVector<int> local_property_count(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5004 | int total_property_count = 0; |
| 5005 | Handle<JSObject> jsproto = obj; |
| 5006 | for (int i = 0; i < length; i++) { |
| 5007 | // Only collect names if access is permitted. |
| 5008 | if (jsproto->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5009 | !isolate->MayNamedAccess(*jsproto, |
| 5010 | isolate->heap()->undefined_value(), |
| 5011 | v8::ACCESS_KEYS)) { |
| 5012 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 5013 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5014 | } |
| 5015 | int n; |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 5016 | n = jsproto->NumberOfLocalProperties(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5017 | local_property_count[i] = n; |
| 5018 | total_property_count += n; |
| 5019 | if (i < length - 1) { |
| 5020 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | // Allocate an array with storage for all the property names. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5025 | Handle<FixedArray> names = |
| 5026 | isolate->factory()->NewFixedArray(total_property_count); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5027 | |
| 5028 | // Get the property names. |
| 5029 | jsproto = obj; |
| 5030 | int proto_with_hidden_properties = 0; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 5031 | int next_copy_index = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5032 | for (int i = 0; i < length; i++) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 5033 | jsproto->GetLocalPropertyNames(*names, next_copy_index); |
| 5034 | next_copy_index += local_property_count[i]; |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 5035 | if (jsproto->HasHiddenProperties()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5036 | proto_with_hidden_properties++; |
| 5037 | } |
| 5038 | if (i < length - 1) { |
| 5039 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 5040 | } |
| 5041 | } |
| 5042 | |
| 5043 | // Filter out name of hidden propeties object. |
| 5044 | if (proto_with_hidden_properties > 0) { |
| 5045 | Handle<FixedArray> old_names = names; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5046 | names = isolate->factory()->NewFixedArray( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5047 | names->length() - proto_with_hidden_properties); |
| 5048 | int dest_pos = 0; |
| 5049 | for (int i = 0; i < total_property_count; i++) { |
| 5050 | Object* name = old_names->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5051 | if (name == isolate->heap()->hidden_symbol()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5052 | continue; |
| 5053 | } |
| 5054 | names->set(dest_pos++, name); |
| 5055 | } |
| 5056 | } |
| 5057 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5058 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | |
| 5062 | // Return the names of the local indexed properties. |
| 5063 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5064 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5065 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5066 | ASSERT(args.length() == 1); |
| 5067 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5068 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5069 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5070 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5071 | |
| 5072 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5073 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5074 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5075 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | |
| 5079 | // Return information on whether an object has a named or indexed interceptor. |
| 5080 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5081 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetInterceptorInfo) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5082 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5083 | ASSERT(args.length() == 1); |
| 5084 | if (!args[0]->IsJSObject()) { |
| 5085 | return Smi::FromInt(0); |
| 5086 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5087 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5088 | |
| 5089 | int result = 0; |
| 5090 | if (obj->HasNamedInterceptor()) result |= 2; |
| 5091 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 5092 | |
| 5093 | return Smi::FromInt(result); |
| 5094 | } |
| 5095 | |
| 5096 | |
| 5097 | // Return property names from named interceptor. |
| 5098 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5099 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetNamedInterceptorPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5100 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5101 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5102 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5103 | |
| 5104 | if (obj->HasNamedInterceptor()) { |
| 5105 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 5106 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 5107 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5108 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
| 5111 | |
| 5112 | // Return element names from indexed interceptor. |
| 5113 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5114 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetIndexedInterceptorElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5115 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5116 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5117 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5118 | |
| 5119 | if (obj->HasIndexedInterceptor()) { |
| 5120 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 5121 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 5122 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5123 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5127 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LocalKeys) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 5128 | ASSERT_EQ(args.length(), 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5129 | CONVERT_ARG_CHECKED(JSObject, raw_object, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5130 | HandleScope scope(isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 5131 | Handle<JSObject> object(raw_object); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5132 | |
| 5133 | if (object->IsJSGlobalProxy()) { |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 5134 | // Do access checks before going to the global object. |
| 5135 | if (object->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5136 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 5137 | v8::ACCESS_KEYS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5138 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 5139 | return *isolate->factory()->NewJSArray(0); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5142 | Handle<Object> proto(object->GetPrototype()); |
| 5143 | // If proxy is detached we simply return an empty array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5144 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 5145 | object = Handle<JSObject>::cast(proto); |
| 5146 | } |
| 5147 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 5148 | bool threw = false; |
| 5149 | Handle<FixedArray> contents = |
| 5150 | GetKeysInFixedArrayFor(object, LOCAL_ONLY, &threw); |
| 5151 | if (threw) return Failure::Exception(); |
| 5152 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 5153 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 5154 | // property array and since the result is mutable we have to create |
| 5155 | // a fresh clone on each invocation. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5156 | int length = contents->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5157 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5158 | for (int i = 0; i < length; i++) { |
| 5159 | Object* entry = contents->get(i); |
| 5160 | if (entry->IsString()) { |
| 5161 | copy->set(i, entry); |
| 5162 | } else { |
| 5163 | ASSERT(entry->IsNumber()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5164 | HandleScope scope(isolate); |
| 5165 | Handle<Object> entry_handle(entry, isolate); |
| 5166 | Handle<Object> entry_str = |
| 5167 | isolate->factory()->NumberToString(entry_handle); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 5168 | copy->set(i, *entry_str); |
| 5169 | } |
| 5170 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5171 | return *isolate->factory()->NewJSArrayWithElements(copy); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5175 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArgumentsProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5176 | NoHandleAllocation ha; |
| 5177 | ASSERT(args.length() == 1); |
| 5178 | |
| 5179 | // Compute the frame holding the arguments. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 5180 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5181 | it.AdvanceToArgumentsFrame(); |
| 5182 | JavaScriptFrame* frame = it.frame(); |
| 5183 | |
| 5184 | // Get the actual number of provided arguments. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 5185 | const uint32_t n = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5186 | |
| 5187 | // Try to convert the key to an index. If successful and within |
| 5188 | // index return the the argument from the frame. |
| 5189 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5190 | if (args[0]->ToArrayIndex(&index) && index < n) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5191 | return frame->GetParameter(index); |
| 5192 | } |
| 5193 | |
| 5194 | // Convert the key to a string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5195 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5196 | bool exception = false; |
| 5197 | Handle<Object> converted = |
| 5198 | Execution::ToString(args.at<Object>(0), &exception); |
| 5199 | if (exception) return Failure::Exception(); |
| 5200 | Handle<String> key = Handle<String>::cast(converted); |
| 5201 | |
| 5202 | // Try to convert the string key into an array index. |
| 5203 | if (key->AsArrayIndex(&index)) { |
| 5204 | if (index < n) { |
| 5205 | return frame->GetParameter(index); |
| 5206 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5207 | return isolate->initial_object_prototype()->GetElement(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | // Handle special arguments properties. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5212 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 5213 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 5214 | Object* function = frame->function(); |
| 5215 | if (function->IsJSFunction() && |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 5216 | !JSFunction::cast(function)->shared()->is_classic_mode()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5217 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 5218 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 5219 | } |
| 5220 | return function; |
| 5221 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5222 | |
| 5223 | // Lookup in the initial Object.prototype object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5224 | return isolate->initial_object_prototype()->GetProperty(*key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5228 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToFastProperties) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5229 | ASSERT(args.length() == 1); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5230 | Object* object = args[0]; |
| 5231 | return (object->IsJSObject() && !object->IsGlobalObject()) |
| 5232 | ? JSObject::cast(object)->TransformToFastProperties(0) |
| 5233 | : object; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5237 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToSlowProperties) { |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5238 | ASSERT(args.length() == 1); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 5239 | Object* obj = args[0]; |
| 5240 | return (obj->IsJSObject() && !obj->IsJSGlobalProxy()) |
| 5241 | ? JSObject::cast(obj)->NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0) |
| 5242 | : obj; |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5246 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToBool) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5247 | NoHandleAllocation ha; |
| 5248 | ASSERT(args.length() == 1); |
| 5249 | |
| 5250 | return args[0]->ToBoolean(); |
| 5251 | } |
| 5252 | |
| 5253 | |
| 5254 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 5255 | // Possible optimizations: put the type string into the oddballs. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5256 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Typeof) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5257 | NoHandleAllocation ha; |
| 5258 | |
| 5259 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5260 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5261 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 5262 | |
| 5263 | // typeof an undetectable object is 'undefined' |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5264 | if (heap_obj->map()->is_undetectable()) { |
| 5265 | return isolate->heap()->undefined_symbol(); |
| 5266 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5267 | |
| 5268 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 5269 | if (instance_type < FIRST_NONSTRING_TYPE) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5270 | return isolate->heap()->string_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5271 | } |
| 5272 | |
| 5273 | switch (instance_type) { |
| 5274 | case ODDBALL_TYPE: |
| 5275 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5276 | return isolate->heap()->boolean_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5277 | } |
| 5278 | if (heap_obj->IsNull()) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 5279 | return FLAG_harmony_typeof |
| 5280 | ? isolate->heap()->null_symbol() |
| 5281 | : isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5282 | } |
| 5283 | ASSERT(heap_obj->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5284 | return isolate->heap()->undefined_symbol(); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5285 | case JS_FUNCTION_TYPE: |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 5286 | case JS_FUNCTION_PROXY_TYPE: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5287 | return isolate->heap()->function_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5288 | default: |
| 5289 | // For any kind of object not handled above, the spec rule for |
| 5290 | // host objects gives that it is okay to return "object" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5291 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5296 | static bool AreDigits(const char*s, int from, int to) { |
| 5297 | for (int i = from; i < to; i++) { |
| 5298 | if (s[i] < '0' || s[i] > '9') return false; |
| 5299 | } |
| 5300 | |
| 5301 | return true; |
| 5302 | } |
| 5303 | |
| 5304 | |
| 5305 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 5306 | ASSERT(to - from < 10); // Overflow is not possible. |
| 5307 | ASSERT(from < to); |
| 5308 | int d = s[from] - '0'; |
| 5309 | |
| 5310 | for (int i = from + 1; i < to; i++) { |
| 5311 | d = 10 * d + (s[i] - '0'); |
| 5312 | } |
| 5313 | |
| 5314 | return d; |
| 5315 | } |
| 5316 | |
| 5317 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5318 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5319 | NoHandleAllocation ha; |
| 5320 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5321 | CONVERT_ARG_CHECKED(String, subject, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5322 | subject->TryFlatten(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5323 | |
| 5324 | // Fast case: short integer or some sorts of junk values. |
| 5325 | int len = subject->length(); |
| 5326 | if (subject->IsSeqAsciiString()) { |
| 5327 | if (len == 0) return Smi::FromInt(0); |
| 5328 | |
| 5329 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 5330 | bool minus = (data[0] == '-'); |
| 5331 | int start_pos = (minus ? 1 : 0); |
| 5332 | |
| 5333 | if (start_pos == len) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5334 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5335 | } else if (data[start_pos] > '9') { |
| 5336 | // Fast check for a junk value. A valid string may start from a |
| 5337 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 5338 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 5339 | // '9' except 'I'. |
| 5340 | if (data[start_pos] != 'I') { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5341 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5342 | } |
| 5343 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 5344 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 5345 | // know it will fit into the smi-data type. |
| 5346 | int d = ParseDecimalInteger(data, start_pos, len); |
| 5347 | if (minus) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5348 | if (d == 0) return isolate->heap()->minus_zero_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5349 | d = -d; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5350 | } else if (!subject->HasHashCode() && |
| 5351 | len <= String::kMaxArrayIndexSize && |
| 5352 | (len == 1 || data[0] != '0')) { |
| 5353 | // String hash is not calculated yet but all the data are present. |
| 5354 | // Update the hash field to speed up sequential convertions. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 5355 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5356 | #ifdef DEBUG |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5357 | subject->Hash(); // Force hash calculation. |
| 5358 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 5359 | static_cast<int>(hash)); |
| 5360 | #endif |
| 5361 | subject->set_hash_field(hash); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5362 | } |
| 5363 | return Smi::FromInt(d); |
| 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | // Slower case. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5368 | return isolate->heap()->NumberFromDouble( |
| 5369 | StringToDouble(isolate->unicode_cache(), subject, ALLOW_HEX)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5373 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringFromCharCodeArray) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5374 | NoHandleAllocation ha; |
| 5375 | ASSERT(args.length() == 1); |
| 5376 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5377 | CONVERT_ARG_CHECKED(JSArray, codes, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5378 | int length = Smi::cast(codes->length())->value(); |
| 5379 | |
| 5380 | // Check if the string can be ASCII. |
| 5381 | int i; |
| 5382 | for (i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5383 | Object* element; |
| 5384 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5385 | // We probably can't get an exception here, but just in order to enforce |
| 5386 | // the checking of inputs in the runtime calls we check here. |
| 5387 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5388 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5389 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 5390 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 5391 | break; |
| 5392 | } |
| 5393 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5394 | MaybeObject* maybe_object = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5395 | if (i == length) { // The string is ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5396 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5397 | } else { // The string is not ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5398 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5399 | } |
| 5400 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5401 | Object* object = NULL; |
| 5402 | if (!maybe_object->ToObject(&object)) return maybe_object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5403 | String* result = String::cast(object); |
| 5404 | for (int i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5405 | Object* element; |
| 5406 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 5407 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 5408 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5409 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5410 | result->Set(i, chr & 0xffff); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5411 | } |
| 5412 | return result; |
| 5413 | } |
| 5414 | |
| 5415 | |
| 5416 | // kNotEscaped is generated by the following: |
| 5417 | // |
| 5418 | // #!/bin/perl |
| 5419 | // for (my $i = 0; $i < 256; $i++) { |
| 5420 | // print "\n" if $i % 16 == 0; |
| 5421 | // my $c = chr($i); |
| 5422 | // my $escaped = 1; |
| 5423 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 5424 | // print $escaped ? "0, " : "1, "; |
| 5425 | // } |
| 5426 | |
| 5427 | |
| 5428 | static bool IsNotEscaped(uint16_t character) { |
| 5429 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 5430 | ASSERT(character < 256); |
| 5431 | static const char kNotEscaped[256] = { |
| 5432 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5433 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5434 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 5435 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 5436 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5437 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 5438 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 5439 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 5440 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5441 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5442 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5443 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5444 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5445 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5446 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5447 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 5448 | }; |
| 5449 | return kNotEscaped[character] != 0; |
| 5450 | } |
| 5451 | |
| 5452 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5453 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5454 | const char hex_chars[] = "0123456789ABCDEF"; |
| 5455 | NoHandleAllocation ha; |
| 5456 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5457 | CONVERT_ARG_CHECKED(String, source, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5458 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5459 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5460 | |
| 5461 | int escaped_length = 0; |
| 5462 | int length = source->length(); |
| 5463 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5464 | Access<StringInputBuffer> buffer( |
| 5465 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5466 | buffer->Reset(source); |
| 5467 | while (buffer->has_more()) { |
| 5468 | uint16_t character = buffer->GetNext(); |
| 5469 | if (character >= 256) { |
| 5470 | escaped_length += 6; |
| 5471 | } else if (IsNotEscaped(character)) { |
| 5472 | escaped_length++; |
| 5473 | } else { |
| 5474 | escaped_length += 3; |
| 5475 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5476 | // 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] | 5477 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5478 | if (escaped_length > String::kMaxLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5479 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5480 | return Failure::OutOfMemoryException(); |
| 5481 | } |
| 5482 | } |
| 5483 | } |
| 5484 | // No length change implies no change. Return original string if no change. |
| 5485 | if (escaped_length == length) { |
| 5486 | return source; |
| 5487 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5488 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5489 | { MaybeObject* maybe_o = |
| 5490 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5491 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5492 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5493 | String* destination = String::cast(o); |
| 5494 | int dest_position = 0; |
| 5495 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5496 | Access<StringInputBuffer> buffer( |
| 5497 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5498 | buffer->Rewind(); |
| 5499 | while (buffer->has_more()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5500 | uint16_t chr = buffer->GetNext(); |
| 5501 | if (chr >= 256) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5502 | destination->Set(dest_position, '%'); |
| 5503 | destination->Set(dest_position+1, 'u'); |
| 5504 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 5505 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 5506 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 5507 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5508 | dest_position += 6; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5509 | } else if (IsNotEscaped(chr)) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5510 | destination->Set(dest_position, chr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5511 | dest_position++; |
| 5512 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5513 | destination->Set(dest_position, '%'); |
| 5514 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 5515 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5516 | dest_position += 3; |
| 5517 | } |
| 5518 | } |
| 5519 | return destination; |
| 5520 | } |
| 5521 | |
| 5522 | |
| 5523 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 5524 | static const signed char kHexValue['g'] = { |
| 5525 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5526 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5527 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5528 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 5529 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5530 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 5531 | -1, 10, 11, 12, 13, 14, 15 }; |
| 5532 | |
| 5533 | if (character1 > 'f') return -1; |
| 5534 | int hi = kHexValue[character1]; |
| 5535 | if (hi == -1) return -1; |
| 5536 | if (character2 > 'f') return -1; |
| 5537 | int lo = kHexValue[character2]; |
| 5538 | if (lo == -1) return -1; |
| 5539 | return (hi << 4) + lo; |
| 5540 | } |
| 5541 | |
| 5542 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5543 | static inline int Unescape(String* source, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5544 | int i, |
| 5545 | int length, |
| 5546 | int* step) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5547 | uint16_t character = source->Get(i); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5548 | int32_t hi = 0; |
| 5549 | int32_t lo = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5550 | if (character == '%' && |
| 5551 | i <= length - 6 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5552 | source->Get(i + 1) == 'u' && |
| 5553 | (hi = TwoDigitHex(source->Get(i + 2), |
| 5554 | source->Get(i + 3))) != -1 && |
| 5555 | (lo = TwoDigitHex(source->Get(i + 4), |
| 5556 | source->Get(i + 5))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5557 | *step = 6; |
| 5558 | return (hi << 8) + lo; |
| 5559 | } else if (character == '%' && |
| 5560 | i <= length - 3 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5561 | (lo = TwoDigitHex(source->Get(i + 1), |
| 5562 | source->Get(i + 2))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5563 | *step = 3; |
| 5564 | return lo; |
| 5565 | } else { |
| 5566 | *step = 1; |
| 5567 | return character; |
| 5568 | } |
| 5569 | } |
| 5570 | |
| 5571 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5572 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5573 | NoHandleAllocation ha; |
| 5574 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5575 | CONVERT_ARG_CHECKED(String, source, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5576 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5577 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5578 | |
| 5579 | bool ascii = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5580 | int length = source->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5581 | |
| 5582 | int unescaped_length = 0; |
| 5583 | for (int i = 0; i < length; unescaped_length++) { |
| 5584 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5585 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5586 | ascii = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5587 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5588 | i += step; |
| 5589 | } |
| 5590 | |
| 5591 | // No length change implies no change. Return original string if no change. |
| 5592 | if (unescaped_length == length) |
| 5593 | return source; |
| 5594 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5595 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5596 | { MaybeObject* maybe_o = |
| 5597 | ascii ? |
| 5598 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 5599 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5600 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5601 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5602 | String* destination = String::cast(o); |
| 5603 | |
| 5604 | int dest_position = 0; |
| 5605 | for (int i = 0; i < length; dest_position++) { |
| 5606 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5607 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5608 | i += step; |
| 5609 | } |
| 5610 | return destination; |
| 5611 | } |
| 5612 | |
| 5613 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5614 | static const unsigned int kQuoteTableLength = 128u; |
| 5615 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5616 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 5617 | static const char* const JsonQuotes = |
| 5618 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 5619 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 5620 | "\\b \\t \\n \\u000b " |
| 5621 | "\\f \\r \\u000e \\u000f " |
| 5622 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 5623 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 5624 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 5625 | "\\u001c \\u001d \\u001e \\u001f " |
| 5626 | " ! \\\" # " |
| 5627 | "$ % & ' " |
| 5628 | "( ) * + " |
| 5629 | ", - . / " |
| 5630 | "0 1 2 3 " |
| 5631 | "4 5 6 7 " |
| 5632 | "8 9 : ; " |
| 5633 | "< = > ? " |
| 5634 | "@ A B C " |
| 5635 | "D E F G " |
| 5636 | "H I J K " |
| 5637 | "L M N O " |
| 5638 | "P Q R S " |
| 5639 | "T U V W " |
| 5640 | "X Y Z [ " |
| 5641 | "\\\\ ] ^ _ " |
| 5642 | "` a b c " |
| 5643 | "d e f g " |
| 5644 | "h i j k " |
| 5645 | "l m n o " |
| 5646 | "p q r s " |
| 5647 | "t u v w " |
| 5648 | "x y z { " |
| 5649 | "| } ~ \177 "; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5650 | |
| 5651 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5652 | // For a string that is less than 32k characters it should always be |
| 5653 | // possible to allocate it in new space. |
| 5654 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5655 | |
| 5656 | |
| 5657 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5658 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5659 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5660 | static const int kSpaceForQuotesAndComma = 3; |
| 5661 | static const int kSpaceForBrackets = 2; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5662 | |
| 5663 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5664 | // quoting). |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5665 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5666 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5667 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5668 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5669 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5670 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5671 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5672 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5673 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5674 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5675 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5676 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5677 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5678 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5679 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5680 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5681 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5682 | }; |
| 5683 | |
| 5684 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5685 | template <typename StringType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5686 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5687 | |
| 5688 | |
| 5689 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5690 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5691 | return isolate->heap()->AllocateRawTwoByteString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | |
| 5695 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5696 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5697 | return isolate->heap()->AllocateRawAsciiString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5701 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5702 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5703 | Vector<const Char> characters) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5704 | int length = characters.length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5705 | const Char* read_cursor = characters.start(); |
| 5706 | const Char* end = read_cursor + length; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5707 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5708 | int quoted_length = kSpaceForQuotes; |
| 5709 | while (read_cursor < end) { |
| 5710 | Char c = *(read_cursor++); |
| 5711 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5712 | quoted_length++; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5713 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5714 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5715 | } |
| 5716 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5717 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5718 | quoted_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5719 | Object* new_object; |
| 5720 | if (!new_alloc->ToObject(&new_object)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5721 | return new_alloc; |
| 5722 | } |
| 5723 | StringType* new_string = StringType::cast(new_object); |
| 5724 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5725 | Char* write_cursor = reinterpret_cast<Char*>( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5726 | new_string->address() + SeqString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5727 | if (comma) *(write_cursor++) = ','; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5728 | *(write_cursor++) = '"'; |
| 5729 | |
| 5730 | read_cursor = characters.start(); |
| 5731 | while (read_cursor < end) { |
| 5732 | Char c = *(read_cursor++); |
| 5733 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5734 | *(write_cursor++) = c; |
| 5735 | } else { |
| 5736 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5737 | const char* replacement = JsonQuotes + |
| 5738 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5739 | for (int i = 0; i < len; i++) { |
| 5740 | *write_cursor++ = *replacement++; |
| 5741 | } |
| 5742 | } |
| 5743 | } |
| 5744 | *(write_cursor++) = '"'; |
| 5745 | return new_string; |
| 5746 | } |
| 5747 | |
| 5748 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5749 | template <typename SinkChar, typename SourceChar> |
| 5750 | static inline SinkChar* WriteQuoteJsonString( |
| 5751 | Isolate* isolate, |
| 5752 | SinkChar* write_cursor, |
| 5753 | Vector<const SourceChar> characters) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5754 | // SinkChar is only char if SourceChar is guaranteed to be char. |
| 5755 | ASSERT(sizeof(SinkChar) >= sizeof(SourceChar)); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5756 | const SourceChar* read_cursor = characters.start(); |
| 5757 | const SourceChar* end = read_cursor + characters.length(); |
| 5758 | *(write_cursor++) = '"'; |
| 5759 | while (read_cursor < end) { |
| 5760 | SourceChar c = *(read_cursor++); |
| 5761 | if (sizeof(SourceChar) > 1u && |
| 5762 | static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5763 | *(write_cursor++) = static_cast<SinkChar>(c); |
| 5764 | } else { |
| 5765 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5766 | const char* replacement = JsonQuotes + |
| 5767 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5768 | write_cursor[0] = replacement[0]; |
| 5769 | if (len > 1) { |
| 5770 | write_cursor[1] = replacement[1]; |
| 5771 | if (len > 2) { |
| 5772 | ASSERT(len == 6); |
| 5773 | write_cursor[2] = replacement[2]; |
| 5774 | write_cursor[3] = replacement[3]; |
| 5775 | write_cursor[4] = replacement[4]; |
| 5776 | write_cursor[5] = replacement[5]; |
| 5777 | } |
| 5778 | } |
| 5779 | write_cursor += len; |
| 5780 | } |
| 5781 | } |
| 5782 | *(write_cursor++) = '"'; |
| 5783 | return write_cursor; |
| 5784 | } |
| 5785 | |
| 5786 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5787 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5788 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5789 | Vector<const Char> characters) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5790 | int length = characters.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5791 | isolate->counters()->quote_json_char_count()->Increment(length); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5792 | int worst_case_length = |
| 5793 | length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotesAndComma; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5794 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5795 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5798 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5799 | worst_case_length); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5800 | Object* new_object; |
| 5801 | if (!new_alloc->ToObject(&new_object)) { |
| 5802 | return new_alloc; |
| 5803 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5804 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5805 | // Even if our string is small enough to fit in new space we still have to |
| 5806 | // handle it being allocated in old space as may happen in the third |
| 5807 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5808 | // CEntryStub::GenerateCore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5809 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5810 | } |
| 5811 | StringType* new_string = StringType::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5812 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5813 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5814 | Char* write_cursor = reinterpret_cast<Char*>( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5815 | new_string->address() + SeqString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5816 | if (comma) *(write_cursor++) = ','; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5817 | write_cursor = WriteQuoteJsonString<Char, Char>(isolate, |
| 5818 | write_cursor, |
| 5819 | characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5820 | int final_length = static_cast<int>( |
| 5821 | write_cursor - reinterpret_cast<Char*>( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5822 | new_string->address() + SeqString::kHeaderSize)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5823 | isolate->heap()->new_space()-> |
| 5824 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5825 | new_string, final_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5826 | return new_string; |
| 5827 | } |
| 5828 | |
| 5829 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5830 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5831 | NoHandleAllocation ha; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5832 | CONVERT_ARG_CHECKED(String, str, 0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5833 | if (!str->IsFlat()) { |
| 5834 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5835 | Object* flat; |
| 5836 | if (!try_flatten->ToObject(&flat)) { |
| 5837 | return try_flatten; |
| 5838 | } |
| 5839 | str = String::cast(flat); |
| 5840 | ASSERT(str->IsFlat()); |
| 5841 | } |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5842 | String::FlatContent flat = str->GetFlatContent(); |
| 5843 | ASSERT(flat.IsFlat()); |
| 5844 | if (flat.IsTwoByte()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5845 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5846 | flat.ToUC16Vector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5847 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5848 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5849 | flat.ToAsciiVector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5850 | } |
| 5851 | } |
| 5852 | |
| 5853 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5854 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringComma) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5855 | NoHandleAllocation ha; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5856 | CONVERT_ARG_CHECKED(String, str, 0); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5857 | if (!str->IsFlat()) { |
| 5858 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5859 | Object* flat; |
| 5860 | if (!try_flatten->ToObject(&flat)) { |
| 5861 | return try_flatten; |
| 5862 | } |
| 5863 | str = String::cast(flat); |
| 5864 | ASSERT(str->IsFlat()); |
| 5865 | } |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5866 | String::FlatContent flat = str->GetFlatContent(); |
| 5867 | if (flat.IsTwoByte()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5868 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5869 | flat.ToUC16Vector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5870 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5871 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5872 | flat.ToAsciiVector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5873 | } |
| 5874 | } |
| 5875 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5876 | |
| 5877 | template <typename Char, typename StringType> |
| 5878 | static MaybeObject* QuoteJsonStringArray(Isolate* isolate, |
| 5879 | FixedArray* array, |
| 5880 | int worst_case_length) { |
| 5881 | int length = array->length(); |
| 5882 | |
| 5883 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5884 | worst_case_length); |
| 5885 | Object* new_object; |
| 5886 | if (!new_alloc->ToObject(&new_object)) { |
| 5887 | return new_alloc; |
| 5888 | } |
| 5889 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
| 5890 | // Even if our string is small enough to fit in new space we still have to |
| 5891 | // handle it being allocated in old space as may happen in the third |
| 5892 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5893 | // CEntryStub::GenerateCore. |
| 5894 | return isolate->heap()->undefined_value(); |
| 5895 | } |
| 5896 | AssertNoAllocation no_gc; |
| 5897 | StringType* new_string = StringType::cast(new_object); |
| 5898 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
| 5899 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5900 | Char* write_cursor = reinterpret_cast<Char*>( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5901 | new_string->address() + SeqString::kHeaderSize); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5902 | *(write_cursor++) = '['; |
| 5903 | for (int i = 0; i < length; i++) { |
| 5904 | if (i != 0) *(write_cursor++) = ','; |
| 5905 | String* str = String::cast(array->get(i)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5906 | String::FlatContent content = str->GetFlatContent(); |
| 5907 | ASSERT(content.IsFlat()); |
| 5908 | if (content.IsTwoByte()) { |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5909 | write_cursor = WriteQuoteJsonString<Char, uc16>(isolate, |
| 5910 | write_cursor, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5911 | content.ToUC16Vector()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5912 | } else { |
| 5913 | write_cursor = WriteQuoteJsonString<Char, char>(isolate, |
| 5914 | write_cursor, |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 5915 | content.ToAsciiVector()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5916 | } |
| 5917 | } |
| 5918 | *(write_cursor++) = ']'; |
| 5919 | |
| 5920 | int final_length = static_cast<int>( |
| 5921 | write_cursor - reinterpret_cast<Char*>( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 5922 | new_string->address() + SeqString::kHeaderSize)); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5923 | isolate->heap()->new_space()-> |
| 5924 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5925 | new_string, final_length); |
| 5926 | return new_string; |
| 5927 | } |
| 5928 | |
| 5929 | |
| 5930 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringArray) { |
| 5931 | NoHandleAllocation ha; |
| 5932 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5933 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5934 | |
| 5935 | if (!array->HasFastElements()) return isolate->heap()->undefined_value(); |
| 5936 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 5937 | int n = elements->length(); |
| 5938 | bool ascii = true; |
| 5939 | int total_length = 0; |
| 5940 | |
| 5941 | for (int i = 0; i < n; i++) { |
| 5942 | Object* elt = elements->get(i); |
| 5943 | if (!elt->IsString()) return isolate->heap()->undefined_value(); |
| 5944 | String* element = String::cast(elt); |
| 5945 | if (!element->IsFlat()) return isolate->heap()->undefined_value(); |
| 5946 | total_length += element->length(); |
| 5947 | if (ascii && element->IsTwoByteRepresentation()) { |
| 5948 | ascii = false; |
| 5949 | } |
| 5950 | } |
| 5951 | |
| 5952 | int worst_case_length = |
| 5953 | kSpaceForBrackets + n * kSpaceForQuotesAndComma |
| 5954 | + total_length * kJsonQuoteWorstCaseBlowup; |
| 5955 | |
| 5956 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
| 5957 | return isolate->heap()->undefined_value(); |
| 5958 | } |
| 5959 | |
| 5960 | if (ascii) { |
| 5961 | return QuoteJsonStringArray<char, SeqAsciiString>(isolate, |
| 5962 | elements, |
| 5963 | worst_case_length); |
| 5964 | } else { |
| 5965 | return QuoteJsonStringArray<uc16, SeqTwoByteString>(isolate, |
| 5966 | elements, |
| 5967 | worst_case_length); |
| 5968 | } |
| 5969 | } |
| 5970 | |
| 5971 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5972 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseInt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5973 | NoHandleAllocation ha; |
| 5974 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5975 | CONVERT_ARG_CHECKED(String, s, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5976 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5977 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5978 | s->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5979 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5980 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5981 | double value = StringToInt(isolate->unicode_cache(), s, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5982 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5986 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseFloat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5987 | NoHandleAllocation ha; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 5988 | CONVERT_ARG_CHECKED(String, str, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5989 | |
| 5990 | // ECMA-262 section 15.1.2.3, empty string is NaN |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5991 | double value = StringToDouble(isolate->unicode_cache(), |
| 5992 | str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5993 | |
| 5994 | // Create a number object from the value. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5995 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
| 5998 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5999 | template <class Converter> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6000 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6001 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6002 | String* s, |
| 6003 | int length, |
| 6004 | int input_string_length, |
| 6005 | unibrow::Mapping<Converter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6006 | // We try this twice, once with the assumption that the result is no longer |
| 6007 | // than the input and, if that assumption breaks, again with the exact |
| 6008 | // length. This may not be pretty, but it is nicer than what was here before |
| 6009 | // and I hereby claim my vaffel-is. |
| 6010 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6011 | // Allocate the resulting string. |
| 6012 | // |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6013 | // NOTE: This assumes that the upper/lower case of an ASCII |
| 6014 | // character is also ASCII. This is currently the case, but it |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6015 | // might break in the future if we implement more context and locale |
| 6016 | // dependent upper/lower conversions. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6017 | Object* o; |
| 6018 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6019 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 6020 | : isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6021 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 6022 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6023 | String* result = String::cast(o); |
| 6024 | bool has_changed_character = false; |
| 6025 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6026 | // Convert all characters to upper case, assuming that they will fit |
| 6027 | // in the buffer |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6028 | Access<StringInputBuffer> buffer( |
| 6029 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6030 | buffer->Reset(s); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 6031 | unibrow::uchar chars[Converter::kMaxWidth]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6032 | // We can assume that the string is not empty |
| 6033 | uc32 current = buffer->GetNext(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6034 | for (int i = 0; i < length;) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6035 | bool has_next = buffer->has_more(); |
| 6036 | uc32 next = has_next ? buffer->GetNext() : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6037 | int char_length = mapping->get(current, next, chars); |
| 6038 | if (char_length == 0) { |
| 6039 | // The case conversion of this character is the character itself. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6040 | result->Set(i, current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6041 | i++; |
| 6042 | } else if (char_length == 1) { |
| 6043 | // Common case: converting the letter resulted in one character. |
| 6044 | ASSERT(static_cast<uc32>(chars[0]) != current); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6045 | result->Set(i, chars[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6046 | has_changed_character = true; |
| 6047 | i++; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6048 | } else if (length == input_string_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6049 | // We've assumed that the result would be as long as the |
| 6050 | // input but here is a character that converts to several |
| 6051 | // characters. No matter, we calculate the exact length |
| 6052 | // of the result and try the whole thing again. |
| 6053 | // |
| 6054 | // Note that this leaves room for optimization. We could just |
| 6055 | // memcpy what we already have to the result string. Also, |
| 6056 | // the result string is the last object allocated we could |
| 6057 | // "realloc" it and probably, in the vast majority of cases, |
| 6058 | // extend the existing string to be able to hold the full |
| 6059 | // result. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6060 | int next_length = 0; |
| 6061 | if (has_next) { |
| 6062 | next_length = mapping->get(next, 0, chars); |
| 6063 | if (next_length == 0) next_length = 1; |
| 6064 | } |
| 6065 | int current_length = i + char_length + next_length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6066 | while (buffer->has_more()) { |
| 6067 | current = buffer->GetNext(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6068 | // NOTE: we use 0 as the next character here because, while |
| 6069 | // the next character may affect what a character converts to, |
| 6070 | // it does not in any case affect the length of what it convert |
| 6071 | // to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6072 | int char_length = mapping->get(current, 0, chars); |
| 6073 | if (char_length == 0) char_length = 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6074 | current_length += char_length; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6075 | if (current_length > Smi::kMaxValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6076 | isolate->context()->mark_out_of_memory(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6077 | return Failure::OutOfMemoryException(); |
| 6078 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6079 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6080 | // Try again with the real length. |
| 6081 | return Smi::FromInt(current_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6082 | } else { |
| 6083 | for (int j = 0; j < char_length; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6084 | result->Set(i, chars[j]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6085 | i++; |
| 6086 | } |
| 6087 | has_changed_character = true; |
| 6088 | } |
| 6089 | current = next; |
| 6090 | } |
| 6091 | if (has_changed_character) { |
| 6092 | return result; |
| 6093 | } else { |
| 6094 | // If we didn't actually change anything in doing the conversion |
| 6095 | // we simple return the result and let the converted string |
| 6096 | // become garbage; there is no reason to keep two identical strings |
| 6097 | // alive. |
| 6098 | return s; |
| 6099 | } |
| 6100 | } |
| 6101 | |
| 6102 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6103 | namespace { |
| 6104 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6105 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 6106 | |
| 6107 | |
| 6108 | // Given a word and two range boundaries returns a word with high bit |
| 6109 | // set in every byte iff the corresponding input byte was strictly in |
| 6110 | // the range (m, n). All the other bits in the result are cleared. |
| 6111 | // This function is only useful when it can be inlined and the |
| 6112 | // boundaries are statically known. |
| 6113 | // Requires: all bytes in the input word and the boundaries must be |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6114 | // ASCII (less than 0x7F). |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6115 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6116 | // Every byte in an ASCII string is less than or equal to 0x7F. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6117 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 6118 | // Use strict inequalities since in edge cases the function could be |
| 6119 | // further simplified. |
| 6120 | ASSERT(0 < m && m < n && n < 0x7F); |
| 6121 | // Has high bit set in every w byte less than n. |
| 6122 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 6123 | // Has high bit set in every w byte greater than m. |
| 6124 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 6125 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 6126 | } |
| 6127 | |
| 6128 | |
| 6129 | enum AsciiCaseConversion { |
| 6130 | ASCII_TO_LOWER, |
| 6131 | ASCII_TO_UPPER |
| 6132 | }; |
| 6133 | |
| 6134 | |
| 6135 | template <AsciiCaseConversion dir> |
| 6136 | struct FastAsciiConverter { |
| 6137 | static bool Convert(char* dst, char* src, int length) { |
| 6138 | #ifdef DEBUG |
| 6139 | char* saved_dst = dst; |
| 6140 | char* saved_src = src; |
| 6141 | #endif |
| 6142 | // We rely on the distance between upper and lower case letters |
| 6143 | // being a known power of 2. |
| 6144 | ASSERT('a' - 'A' == (1 << 5)); |
| 6145 | // Boundaries for the range of input characters than require conversion. |
| 6146 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 6147 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 6148 | bool changed = false; |
| 6149 | char* const limit = src + length; |
| 6150 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 6151 | // Process the prefix of the input that requires no conversion one |
| 6152 | // (machine) word at a time. |
| 6153 | while (src <= limit - sizeof(uintptr_t)) { |
| 6154 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6155 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 6156 | changed = true; |
| 6157 | break; |
| 6158 | } |
| 6159 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 6160 | src += sizeof(uintptr_t); |
| 6161 | dst += sizeof(uintptr_t); |
| 6162 | } |
| 6163 | // Process the remainder of the input performing conversion when |
| 6164 | // required one word at a time. |
| 6165 | while (src <= limit - sizeof(uintptr_t)) { |
| 6166 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 6167 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 6168 | // The mask has high (7th) bit set in every byte that needs |
| 6169 | // conversion and we know that the distance between cases is |
| 6170 | // 1 << 5. |
| 6171 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 6172 | src += sizeof(uintptr_t); |
| 6173 | dst += sizeof(uintptr_t); |
| 6174 | } |
| 6175 | #endif |
| 6176 | // Process the last few bytes of the input (or the whole input if |
| 6177 | // unaligned access is not supported). |
| 6178 | while (src < limit) { |
| 6179 | char c = *src; |
| 6180 | if (lo < c && c < hi) { |
| 6181 | c ^= (1 << 5); |
| 6182 | changed = true; |
| 6183 | } |
| 6184 | *dst = c; |
| 6185 | ++src; |
| 6186 | ++dst; |
| 6187 | } |
| 6188 | #ifdef DEBUG |
| 6189 | CheckConvert(saved_dst, saved_src, length, changed); |
| 6190 | #endif |
| 6191 | return changed; |
| 6192 | } |
| 6193 | |
| 6194 | #ifdef DEBUG |
| 6195 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 6196 | bool expected_changed = false; |
| 6197 | for (int i = 0; i < length; i++) { |
| 6198 | if (dst[i] == src[i]) continue; |
| 6199 | expected_changed = true; |
| 6200 | if (dir == ASCII_TO_LOWER) { |
| 6201 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 6202 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 6203 | } else { |
| 6204 | ASSERT(dir == ASCII_TO_UPPER); |
| 6205 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 6206 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 6207 | } |
| 6208 | } |
| 6209 | ASSERT(expected_changed == changed); |
| 6210 | } |
| 6211 | #endif |
| 6212 | }; |
| 6213 | |
| 6214 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6215 | struct ToLowerTraits { |
| 6216 | typedef unibrow::ToLowercase UnibrowConverter; |
| 6217 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6218 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6219 | }; |
| 6220 | |
| 6221 | |
| 6222 | struct ToUpperTraits { |
| 6223 | typedef unibrow::ToUppercase UnibrowConverter; |
| 6224 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6225 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6226 | }; |
| 6227 | |
| 6228 | } // namespace |
| 6229 | |
| 6230 | |
| 6231 | template <typename ConvertTraits> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6232 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6233 | Arguments args, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6234 | Isolate* isolate, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6235 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6236 | NoHandleAllocation ha; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6237 | CONVERT_ARG_CHECKED(String, s, 0); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6238 | s = s->TryFlattenGetString(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6239 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6240 | const int length = s->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6241 | // 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] | 6242 | if (length == 0) return s; |
| 6243 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6244 | // Simpler handling of ASCII strings. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6245 | // |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6246 | // NOTE: This assumes that the upper/lower case of an ASCII |
| 6247 | // character is also ASCII. This is currently the case, but it |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6248 | // might break in the future if we implement more context and locale |
| 6249 | // dependent upper/lower conversions. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6250 | if (s->IsSeqAsciiString()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6251 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6252 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6253 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 6254 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6255 | SeqAsciiString* result = SeqAsciiString::cast(o); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6256 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6257 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6258 | return has_changed_character ? result : s; |
| 6259 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6260 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6261 | Object* answer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6262 | { MaybeObject* maybe_answer = |
| 6263 | ConvertCaseHelper(isolate, s, length, length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6264 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6265 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6266 | if (answer->IsSmi()) { |
| 6267 | // Retry with correct length. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6268 | { MaybeObject* maybe_answer = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6269 | ConvertCaseHelper(isolate, |
| 6270 | s, Smi::cast(answer)->value(), length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6271 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 6272 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6273 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6274 | return answer; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6278 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6279 | return ConvertCase<ToLowerTraits>( |
| 6280 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6284 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToUpperCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6285 | return ConvertCase<ToUpperTraits>( |
| 6286 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6287 | } |
| 6288 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6289 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6290 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 6291 | return unibrow::WhiteSpace::Is(c) || c == 0x200b || c == 0xfeff; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6292 | } |
| 6293 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6294 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6295 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringTrim) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6296 | NoHandleAllocation ha; |
| 6297 | ASSERT(args.length() == 3); |
| 6298 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6299 | CONVERT_ARG_CHECKED(String, s, 0); |
| 6300 | CONVERT_BOOLEAN_ARG_CHECKED(trimLeft, 1); |
| 6301 | CONVERT_BOOLEAN_ARG_CHECKED(trimRight, 2); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6302 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6303 | s->TryFlatten(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6304 | int length = s->length(); |
| 6305 | |
| 6306 | int left = 0; |
| 6307 | if (trimLeft) { |
| 6308 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 6309 | left++; |
| 6310 | } |
| 6311 | } |
| 6312 | |
| 6313 | int right = length; |
| 6314 | if (trimRight) { |
| 6315 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 6316 | right--; |
| 6317 | } |
| 6318 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6319 | return s->SubString(left, right); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6320 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6321 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6322 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6323 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6324 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6325 | HandleScope handle_scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6326 | CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 6327 | CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6328 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 6329 | |
| 6330 | int subject_length = subject->length(); |
| 6331 | int pattern_length = pattern->length(); |
| 6332 | RUNTIME_ASSERT(pattern_length > 0); |
| 6333 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6334 | if (limit == 0xffffffffu) { |
| 6335 | Handle<Object> cached_answer(StringSplitCache::Lookup( |
| 6336 | isolate->heap()->string_split_cache(), |
| 6337 | *subject, |
| 6338 | *pattern)); |
| 6339 | if (*cached_answer != Smi::FromInt(0)) { |
| 6340 | Handle<JSArray> result = |
| 6341 | isolate->factory()->NewJSArrayWithElements( |
| 6342 | Handle<FixedArray>::cast(cached_answer)); |
| 6343 | return *result; |
| 6344 | } |
| 6345 | } |
| 6346 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6347 | // The limit can be very large (0xffffffffu), but since the pattern |
| 6348 | // isn't empty, we can never create more parts than ~half the length |
| 6349 | // of the subject. |
| 6350 | |
| 6351 | if (!subject->IsFlat()) FlattenString(subject); |
| 6352 | |
| 6353 | static const int kMaxInitialListCapacity = 16; |
| 6354 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 6355 | ZoneScope scope(isolate, DELETE_ON_EXIT); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6356 | |
| 6357 | // Find (up to limit) indices of separator and end-of-string in subject |
| 6358 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 6359 | ZoneList<int> indices(initial_capacity); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6360 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 6361 | |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 6362 | FindStringIndicesDispatch(isolate, *subject, *pattern, &indices, limit); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6363 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6364 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 6365 | indices.Add(subject_length); |
| 6366 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6367 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6368 | // 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] | 6369 | |
| 6370 | // Create JSArray of substrings separated by separator. |
| 6371 | int part_count = indices.length(); |
| 6372 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6373 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6374 | MaybeObject* maybe_result = result->EnsureCanContainHeapObjectElements(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6375 | if (maybe_result->IsFailure()) return maybe_result; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6376 | result->set_length(Smi::FromInt(part_count)); |
| 6377 | |
| 6378 | ASSERT(result->HasFastElements()); |
| 6379 | |
| 6380 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 6381 | FixedArray::cast(result->elements())->set(0, *subject); |
| 6382 | return *result; |
| 6383 | } |
| 6384 | |
| 6385 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 6386 | int part_start = 0; |
| 6387 | for (int i = 0; i < part_count; i++) { |
| 6388 | HandleScope local_loop_handle; |
| 6389 | int part_end = indices.at(i); |
| 6390 | Handle<String> substring = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6391 | isolate->factory()->NewProperSubString(subject, part_start, part_end); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6392 | elements->set(i, *substring); |
| 6393 | part_start = part_end + pattern_length; |
| 6394 | } |
| 6395 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6396 | if (limit == 0xffffffffu) { |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 6397 | if (result->HasFastElements()) { |
| 6398 | StringSplitCache::Enter(isolate->heap(), |
| 6399 | isolate->heap()->string_split_cache(), |
| 6400 | *subject, |
| 6401 | *pattern, |
| 6402 | *elements); |
| 6403 | } |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 6404 | } |
| 6405 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6406 | return *result; |
| 6407 | } |
| 6408 | |
| 6409 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 6410 | // Copies ASCII characters to the given fixed array looking up |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6411 | // one-char strings in the cache. Gives up on the first char that is |
| 6412 | // not in the cache and fills the remainder with smi zeros. Returns |
| 6413 | // the length of the successfully copied prefix. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6414 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 6415 | const char* chars, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6416 | FixedArray* elements, |
| 6417 | int length) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6418 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6419 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 6420 | Object* undefined = heap->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6421 | int i; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6422 | WriteBarrierMode mode = elements->GetWriteBarrierMode(no_gc); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6423 | for (i = 0; i < length; ++i) { |
| 6424 | Object* value = ascii_cache->get(chars[i]); |
| 6425 | if (value == undefined) break; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6426 | elements->set(i, value, mode); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6427 | } |
| 6428 | if (i < length) { |
| 6429 | ASSERT(Smi::FromInt(0) == 0); |
| 6430 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 6431 | } |
| 6432 | #ifdef DEBUG |
| 6433 | for (int j = 0; j < length; ++j) { |
| 6434 | Object* element = elements->get(j); |
| 6435 | ASSERT(element == Smi::FromInt(0) || |
| 6436 | (element->IsString() && String::cast(element)->LooksValid())); |
| 6437 | } |
| 6438 | #endif |
| 6439 | return i; |
| 6440 | } |
| 6441 | |
| 6442 | |
| 6443 | // Converts a String to JSArray. |
| 6444 | // For example, "foo" => ["f", "o", "o"]. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6445 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToArray) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6446 | HandleScope scope(isolate); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 6447 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6448 | CONVERT_ARG_HANDLE_CHECKED(String, s, 0); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 6449 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6450 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6451 | s = FlattenGetString(s); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 6452 | 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] | 6453 | |
| 6454 | Handle<FixedArray> elements; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6455 | int position = 0; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6456 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6457 | // Try using cached chars where possible. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6458 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6459 | { MaybeObject* maybe_obj = |
| 6460 | isolate->heap()->AllocateUninitializedFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6461 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6462 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6463 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6464 | String::FlatContent content = s->GetFlatContent(); |
| 6465 | if (content.IsAscii()) { |
| 6466 | Vector<const char> chars = content.ToAsciiVector(); |
| 6467 | // Note, this will initialize all elements (not only the prefix) |
| 6468 | // to prevent GC from seeing partially initialized array. |
| 6469 | position = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 6470 | chars.start(), |
| 6471 | *elements, |
| 6472 | length); |
| 6473 | } else { |
| 6474 | MemsetPointer(elements->data_start(), |
| 6475 | isolate->heap()->undefined_value(), |
| 6476 | length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6477 | } |
| 6478 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6479 | elements = isolate->factory()->NewFixedArray(length); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 6480 | } |
| 6481 | for (int i = position; i < length; ++i) { |
| 6482 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 6483 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
| 6486 | #ifdef DEBUG |
| 6487 | for (int i = 0; i < length; ++i) { |
| 6488 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 6489 | } |
| 6490 | #endif |
| 6491 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6492 | return *isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
| 6495 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6496 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStringWrapper) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6497 | NoHandleAllocation ha; |
| 6498 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6499 | CONVERT_ARG_CHECKED(String, value, 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 6500 | return value->ToObject(); |
| 6501 | } |
| 6502 | |
| 6503 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6504 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 6505 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6506 | 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] | 6507 | return char_length == 0; |
| 6508 | } |
| 6509 | |
| 6510 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6511 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6512 | NoHandleAllocation ha; |
| 6513 | ASSERT(args.length() == 1); |
| 6514 | |
| 6515 | Object* number = args[0]; |
| 6516 | RUNTIME_ASSERT(number->IsNumber()); |
| 6517 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6518 | return isolate->heap()->NumberToString(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6519 | } |
| 6520 | |
| 6521 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6522 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToStringSkipCache) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6523 | NoHandleAllocation ha; |
| 6524 | ASSERT(args.length() == 1); |
| 6525 | |
| 6526 | Object* number = args[0]; |
| 6527 | RUNTIME_ASSERT(number->IsNumber()); |
| 6528 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6529 | return isolate->heap()->NumberToString(number, false); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6533 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToInteger) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6534 | NoHandleAllocation ha; |
| 6535 | ASSERT(args.length() == 1); |
| 6536 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6537 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6538 | |
| 6539 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6540 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6541 | return Smi::FromInt(static_cast<int>(number)); |
| 6542 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6543 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6544 | } |
| 6545 | |
| 6546 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6547 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6548 | NoHandleAllocation ha; |
| 6549 | ASSERT(args.length() == 1); |
| 6550 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6551 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6552 | |
| 6553 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6554 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6555 | return Smi::FromInt(static_cast<int>(number)); |
| 6556 | } |
| 6557 | |
| 6558 | double double_value = DoubleToInteger(number); |
| 6559 | // Map both -0 and +0 to +0. |
| 6560 | if (double_value == 0) double_value = 0; |
| 6561 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6562 | return isolate->heap()->NumberFromDouble(double_value); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
| 6565 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6566 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSUint32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6567 | NoHandleAllocation ha; |
| 6568 | ASSERT(args.length() == 1); |
| 6569 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6570 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6571 | return isolate->heap()->NumberFromUint32(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
| 6574 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6575 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSInt32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6576 | NoHandleAllocation ha; |
| 6577 | ASSERT(args.length() == 1); |
| 6578 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6579 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6580 | |
| 6581 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6582 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6583 | return Smi::FromInt(static_cast<int>(number)); |
| 6584 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6585 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
| 6588 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6589 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 6590 | // a small integer. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6591 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToSmi) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6592 | NoHandleAllocation ha; |
| 6593 | ASSERT(args.length() == 1); |
| 6594 | |
| 6595 | Object* obj = args[0]; |
| 6596 | if (obj->IsSmi()) { |
| 6597 | return obj; |
| 6598 | } |
| 6599 | if (obj->IsHeapNumber()) { |
| 6600 | double value = HeapNumber::cast(obj)->value(); |
| 6601 | int int_value = FastD2I(value); |
| 6602 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 6603 | return Smi::FromInt(int_value); |
| 6604 | } |
| 6605 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6606 | return isolate->heap()->nan_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6609 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6610 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateHeapNumber) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6611 | NoHandleAllocation ha; |
| 6612 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6613 | return isolate->heap()->AllocateHeapNumber(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6614 | } |
| 6615 | |
| 6616 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6617 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6618 | NoHandleAllocation ha; |
| 6619 | ASSERT(args.length() == 2); |
| 6620 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6621 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6622 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6623 | return isolate->heap()->NumberFromDouble(x + y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6627 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSub) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6628 | NoHandleAllocation ha; |
| 6629 | ASSERT(args.length() == 2); |
| 6630 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6631 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6632 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6633 | return isolate->heap()->NumberFromDouble(x - y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6634 | } |
| 6635 | |
| 6636 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6637 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMul) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6638 | NoHandleAllocation ha; |
| 6639 | ASSERT(args.length() == 2); |
| 6640 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6641 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6642 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6643 | return isolate->heap()->NumberFromDouble(x * y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6647 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberUnaryMinus) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6648 | NoHandleAllocation ha; |
| 6649 | ASSERT(args.length() == 1); |
| 6650 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6651 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6652 | return isolate->heap()->NumberFromDouble(-x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6653 | } |
| 6654 | |
| 6655 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6656 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAlloc) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6657 | NoHandleAllocation ha; |
| 6658 | ASSERT(args.length() == 0); |
| 6659 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6660 | return isolate->heap()->NumberFromDouble(9876543210.0); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6664 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberDiv) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6665 | NoHandleAllocation ha; |
| 6666 | ASSERT(args.length() == 2); |
| 6667 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6668 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6669 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6670 | return isolate->heap()->NumberFromDouble(x / y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
| 6673 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6674 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMod) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6675 | NoHandleAllocation ha; |
| 6676 | ASSERT(args.length() == 2); |
| 6677 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6678 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6679 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6680 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6681 | x = modulo(x, y); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6682 | // NumberFromDouble may return a Smi instead of a Number object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6683 | return isolate->heap()->NumberFromDouble(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6687 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6688 | NoHandleAllocation ha; |
| 6689 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6690 | CONVERT_ARG_CHECKED(String, str1, 0); |
| 6691 | CONVERT_ARG_CHECKED(String, str2, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6692 | isolate->counters()->string_add_runtime()->Increment(); |
| 6693 | return isolate->heap()->AllocateConsString(str1, str2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
| 6696 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6697 | template <typename sinkchar> |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6698 | static inline void StringBuilderConcatHelper(String* special, |
| 6699 | sinkchar* sink, |
| 6700 | FixedArray* fixed_array, |
| 6701 | int array_length) { |
| 6702 | int position = 0; |
| 6703 | for (int i = 0; i < array_length; i++) { |
| 6704 | Object* element = fixed_array->get(i); |
| 6705 | if (element->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6706 | // Smi encoding of position and length. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6707 | int encoded_slice = Smi::cast(element)->value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6708 | int pos; |
| 6709 | int len; |
| 6710 | if (encoded_slice > 0) { |
| 6711 | // Position and length encoded in one smi. |
| 6712 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 6713 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 6714 | } else { |
| 6715 | // Position and length encoded in two smis. |
| 6716 | Object* obj = fixed_array->get(++i); |
| 6717 | ASSERT(obj->IsSmi()); |
| 6718 | pos = Smi::cast(obj)->value(); |
| 6719 | len = -encoded_slice; |
| 6720 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6721 | String::WriteToFlat(special, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6722 | sink + position, |
| 6723 | pos, |
| 6724 | pos + len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6725 | position += len; |
| 6726 | } else { |
| 6727 | String* string = String::cast(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6728 | int element_length = string->length(); |
| 6729 | String::WriteToFlat(string, sink + position, 0, element_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6730 | position += element_length; |
| 6731 | } |
| 6732 | } |
| 6733 | } |
| 6734 | |
| 6735 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6736 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6737 | NoHandleAllocation ha; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6738 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6739 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6740 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6741 | isolate->context()->mark_out_of_memory(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6742 | return Failure::OutOfMemoryException(); |
| 6743 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6744 | int array_length = args.smi_at(1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6745 | CONVERT_ARG_CHECKED(String, special, 2); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6746 | |
| 6747 | // This assumption is used by the slice encoding in one or two smis. |
| 6748 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6749 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 6750 | MaybeObject* maybe_result = array->EnsureCanContainHeapObjectElements(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6751 | if (maybe_result->IsFailure()) return maybe_result; |
| 6752 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6753 | int special_length = special->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6754 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6755 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6756 | } |
| 6757 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6758 | if (fixed_array->length() < array_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6759 | array_length = fixed_array->length(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6760 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6761 | |
| 6762 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6763 | return isolate->heap()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6764 | } else if (array_length == 1) { |
| 6765 | Object* first = fixed_array->get(0); |
| 6766 | if (first->IsString()) return first; |
| 6767 | } |
| 6768 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6769 | bool ascii = special->HasOnlyAsciiChars(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6770 | int position = 0; |
| 6771 | for (int i = 0; i < array_length; i++) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6772 | int increment = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6773 | Object* elt = fixed_array->get(i); |
| 6774 | if (elt->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6775 | // Smi encoding of position and length. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6776 | int smi_value = Smi::cast(elt)->value(); |
| 6777 | int pos; |
| 6778 | int len; |
| 6779 | if (smi_value > 0) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6780 | // Position and length encoded in one smi. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6781 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6782 | len = StringBuilderSubstringLength::decode(smi_value); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6783 | } else { |
| 6784 | // Position and length encoded in two smis. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6785 | len = -smi_value; |
| 6786 | // Get the position and check that it is a positive smi. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6787 | i++; |
| 6788 | if (i >= array_length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6789 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6790 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6791 | Object* next_smi = fixed_array->get(i); |
| 6792 | if (!next_smi->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6793 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6794 | } |
| 6795 | pos = Smi::cast(next_smi)->value(); |
| 6796 | if (pos < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6797 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6798 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6799 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6800 | ASSERT(pos >= 0); |
| 6801 | ASSERT(len >= 0); |
| 6802 | if (pos > special_length || len > special_length - pos) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6803 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6804 | } |
| 6805 | increment = len; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6806 | } else if (elt->IsString()) { |
| 6807 | String* element = String::cast(elt); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6808 | int element_length = element->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6809 | increment = element_length; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6810 | if (ascii && !element->HasOnlyAsciiChars()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6811 | ascii = false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6812 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6813 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6814 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6815 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6816 | if (increment > String::kMaxLength - position) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6817 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6818 | return Failure::OutOfMemoryException(); |
| 6819 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6820 | position += increment; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
| 6823 | int length = position; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6824 | Object* object; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6825 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6826 | if (ascii) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6827 | { MaybeObject* maybe_object = |
| 6828 | isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6829 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6830 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6831 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6832 | StringBuilderConcatHelper(special, |
| 6833 | answer->GetChars(), |
| 6834 | fixed_array, |
| 6835 | array_length); |
| 6836 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6837 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6838 | { MaybeObject* maybe_object = |
| 6839 | isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6840 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6841 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6842 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6843 | StringBuilderConcatHelper(special, |
| 6844 | answer->GetChars(), |
| 6845 | fixed_array, |
| 6846 | array_length); |
| 6847 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6848 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
| 6851 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6852 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6853 | NoHandleAllocation ha; |
| 6854 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6855 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6856 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6857 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6858 | return Failure::OutOfMemoryException(); |
| 6859 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6860 | int array_length = args.smi_at(1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6861 | CONVERT_ARG_CHECKED(String, separator, 2); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6862 | |
| 6863 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6864 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6865 | } |
| 6866 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6867 | if (fixed_array->length() < array_length) { |
| 6868 | array_length = fixed_array->length(); |
| 6869 | } |
| 6870 | |
| 6871 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6872 | return isolate->heap()->empty_string(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6873 | } else if (array_length == 1) { |
| 6874 | Object* first = fixed_array->get(0); |
| 6875 | if (first->IsString()) return first; |
| 6876 | } |
| 6877 | |
| 6878 | int separator_length = separator->length(); |
| 6879 | int max_nof_separators = |
| 6880 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6881 | if (max_nof_separators < (array_length - 1)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6882 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6883 | return Failure::OutOfMemoryException(); |
| 6884 | } |
| 6885 | int length = (array_length - 1) * separator_length; |
| 6886 | for (int i = 0; i < array_length; i++) { |
| 6887 | Object* element_obj = fixed_array->get(i); |
| 6888 | if (!element_obj->IsString()) { |
| 6889 | // TODO(1161): handle this case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6890 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6891 | } |
| 6892 | String* element = String::cast(element_obj); |
| 6893 | int increment = element->length(); |
| 6894 | if (increment > String::kMaxLength - length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6895 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6896 | return Failure::OutOfMemoryException(); |
| 6897 | } |
| 6898 | length += increment; |
| 6899 | } |
| 6900 | |
| 6901 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6902 | { MaybeObject* maybe_object = |
| 6903 | isolate->heap()->AllocateRawTwoByteString(length); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6904 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6905 | } |
| 6906 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6907 | |
| 6908 | uc16* sink = answer->GetChars(); |
| 6909 | #ifdef DEBUG |
| 6910 | uc16* end = sink + length; |
| 6911 | #endif |
| 6912 | |
| 6913 | String* first = String::cast(fixed_array->get(0)); |
| 6914 | int first_length = first->length(); |
| 6915 | String::WriteToFlat(first, sink, 0, first_length); |
| 6916 | sink += first_length; |
| 6917 | |
| 6918 | for (int i = 1; i < array_length; i++) { |
| 6919 | ASSERT(sink + separator_length <= end); |
| 6920 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6921 | sink += separator_length; |
| 6922 | |
| 6923 | String* element = String::cast(fixed_array->get(i)); |
| 6924 | int element_length = element->length(); |
| 6925 | ASSERT(sink + element_length <= end); |
| 6926 | String::WriteToFlat(element, sink, 0, element_length); |
| 6927 | sink += element_length; |
| 6928 | } |
| 6929 | ASSERT(sink == end); |
| 6930 | |
| 6931 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6932 | return answer; |
| 6933 | } |
| 6934 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6935 | template <typename Char> |
| 6936 | static void JoinSparseArrayWithSeparator(FixedArray* elements, |
| 6937 | int elements_length, |
| 6938 | uint32_t array_length, |
| 6939 | String* separator, |
| 6940 | Vector<Char> buffer) { |
| 6941 | int previous_separator_position = 0; |
| 6942 | int separator_length = separator->length(); |
| 6943 | int cursor = 0; |
| 6944 | for (int i = 0; i < elements_length; i += 2) { |
| 6945 | int position = NumberToInt32(elements->get(i)); |
| 6946 | String* string = String::cast(elements->get(i + 1)); |
| 6947 | int string_length = string->length(); |
| 6948 | if (string->length() > 0) { |
| 6949 | while (previous_separator_position < position) { |
| 6950 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6951 | 0, separator_length); |
| 6952 | cursor += separator_length; |
| 6953 | previous_separator_position++; |
| 6954 | } |
| 6955 | String::WriteToFlat<Char>(string, &buffer[cursor], |
| 6956 | 0, string_length); |
| 6957 | cursor += string->length(); |
| 6958 | } |
| 6959 | } |
| 6960 | if (separator_length > 0) { |
| 6961 | // Array length must be representable as a signed 32-bit number, |
| 6962 | // otherwise the total string length would have been too large. |
| 6963 | ASSERT(array_length <= 0x7fffffff); // Is int32_t. |
| 6964 | int last_array_index = static_cast<int>(array_length - 1); |
| 6965 | while (previous_separator_position < last_array_index) { |
| 6966 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6967 | 0, separator_length); |
| 6968 | cursor += separator_length; |
| 6969 | previous_separator_position++; |
| 6970 | } |
| 6971 | } |
| 6972 | ASSERT(cursor <= buffer.length()); |
| 6973 | } |
| 6974 | |
| 6975 | |
| 6976 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) { |
| 6977 | NoHandleAllocation ha; |
| 6978 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6979 | CONVERT_ARG_CHECKED(JSArray, elements_array, 0); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6980 | RUNTIME_ASSERT(elements_array->HasFastElements() || |
| 6981 | elements_array->HasFastSmiOnlyElements()); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6982 | CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 6983 | CONVERT_ARG_CHECKED(String, separator, 2); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6984 | // elements_array is fast-mode JSarray of alternating positions |
| 6985 | // (increasing order) and strings. |
| 6986 | // array_length is length of original array (used to add separators); |
| 6987 | // separator is string to put between elements. Assumed to be non-empty. |
| 6988 | |
| 6989 | // Find total length of join result. |
| 6990 | int string_length = 0; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 6991 | bool is_ascii = separator->IsAsciiRepresentation(); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 6992 | int max_string_length; |
| 6993 | if (is_ascii) { |
| 6994 | max_string_length = SeqAsciiString::kMaxLength; |
| 6995 | } else { |
| 6996 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6997 | } |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6998 | bool overflow = false; |
| 6999 | CONVERT_NUMBER_CHECKED(int, elements_length, |
| 7000 | Int32, elements_array->length()); |
| 7001 | RUNTIME_ASSERT((elements_length & 1) == 0); // Even length. |
| 7002 | FixedArray* elements = FixedArray::cast(elements_array->elements()); |
| 7003 | for (int i = 0; i < elements_length; i += 2) { |
| 7004 | RUNTIME_ASSERT(elements->get(i)->IsNumber()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7005 | RUNTIME_ASSERT(elements->get(i + 1)->IsString()); |
| 7006 | String* string = String::cast(elements->get(i + 1)); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 7007 | int length = string->length(); |
| 7008 | if (is_ascii && !string->IsAsciiRepresentation()) { |
| 7009 | is_ascii = false; |
| 7010 | max_string_length = SeqTwoByteString::kMaxLength; |
| 7011 | } |
| 7012 | if (length > max_string_length || |
| 7013 | max_string_length - length < string_length) { |
| 7014 | overflow = true; |
| 7015 | break; |
| 7016 | } |
| 7017 | string_length += length; |
| 7018 | } |
| 7019 | int separator_length = separator->length(); |
| 7020 | if (!overflow && separator_length > 0) { |
| 7021 | if (array_length <= 0x7fffffffu) { |
| 7022 | int separator_count = static_cast<int>(array_length) - 1; |
| 7023 | int remaining_length = max_string_length - string_length; |
| 7024 | if ((remaining_length / separator_length) >= separator_count) { |
| 7025 | string_length += separator_length * (array_length - 1); |
| 7026 | } else { |
| 7027 | // Not room for the separators within the maximal string length. |
| 7028 | overflow = true; |
| 7029 | } |
| 7030 | } else { |
| 7031 | // Nonempty separator and at least 2^31-1 separators necessary |
| 7032 | // means that the string is too large to create. |
| 7033 | STATIC_ASSERT(String::kMaxLength < 0x7fffffff); |
| 7034 | overflow = true; |
| 7035 | } |
| 7036 | } |
| 7037 | if (overflow) { |
| 7038 | // Throw OutOfMemory exception for creating too large a string. |
| 7039 | V8::FatalProcessOutOfMemory("Array join result too large."); |
| 7040 | } |
| 7041 | |
| 7042 | if (is_ascii) { |
| 7043 | MaybeObject* result_allocation = |
| 7044 | isolate->heap()->AllocateRawAsciiString(string_length); |
| 7045 | if (result_allocation->IsFailure()) return result_allocation; |
| 7046 | SeqAsciiString* result_string = |
| 7047 | SeqAsciiString::cast(result_allocation->ToObjectUnchecked()); |
| 7048 | JoinSparseArrayWithSeparator<char>(elements, |
| 7049 | elements_length, |
| 7050 | array_length, |
| 7051 | separator, |
| 7052 | Vector<char>(result_string->GetChars(), |
| 7053 | string_length)); |
| 7054 | return result_string; |
| 7055 | } else { |
| 7056 | MaybeObject* result_allocation = |
| 7057 | isolate->heap()->AllocateRawTwoByteString(string_length); |
| 7058 | if (result_allocation->IsFailure()) return result_allocation; |
| 7059 | SeqTwoByteString* result_string = |
| 7060 | SeqTwoByteString::cast(result_allocation->ToObjectUnchecked()); |
| 7061 | JoinSparseArrayWithSeparator<uc16>(elements, |
| 7062 | elements_length, |
| 7063 | array_length, |
| 7064 | separator, |
| 7065 | Vector<uc16>(result_string->GetChars(), |
| 7066 | string_length)); |
| 7067 | return result_string; |
| 7068 | } |
| 7069 | } |
| 7070 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 7071 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7072 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberOr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7073 | NoHandleAllocation ha; |
| 7074 | ASSERT(args.length() == 2); |
| 7075 | |
| 7076 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7077 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7078 | return isolate->heap()->NumberFromInt32(x | y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7079 | } |
| 7080 | |
| 7081 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7082 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAnd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7083 | NoHandleAllocation ha; |
| 7084 | ASSERT(args.length() == 2); |
| 7085 | |
| 7086 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7087 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7088 | return isolate->heap()->NumberFromInt32(x & y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7089 | } |
| 7090 | |
| 7091 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7092 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberXor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7093 | NoHandleAllocation ha; |
| 7094 | ASSERT(args.length() == 2); |
| 7095 | |
| 7096 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7097 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7098 | return isolate->heap()->NumberFromInt32(x ^ y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7102 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberNot) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7103 | NoHandleAllocation ha; |
| 7104 | ASSERT(args.length() == 1); |
| 7105 | |
| 7106 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7107 | return isolate->heap()->NumberFromInt32(~x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7108 | } |
| 7109 | |
| 7110 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7111 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShl) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7112 | NoHandleAllocation ha; |
| 7113 | ASSERT(args.length() == 2); |
| 7114 | |
| 7115 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7116 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7117 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7118 | } |
| 7119 | |
| 7120 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7121 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7122 | NoHandleAllocation ha; |
| 7123 | ASSERT(args.length() == 2); |
| 7124 | |
| 7125 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 7126 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7127 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7128 | } |
| 7129 | |
| 7130 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7131 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSar) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7132 | NoHandleAllocation ha; |
| 7133 | ASSERT(args.length() == 2); |
| 7134 | |
| 7135 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 7136 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7137 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7138 | } |
| 7139 | |
| 7140 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7141 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7142 | NoHandleAllocation ha; |
| 7143 | ASSERT(args.length() == 2); |
| 7144 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7145 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7146 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7147 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 7148 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 7149 | if (x == y) return Smi::FromInt(EQUAL); |
| 7150 | Object* result; |
| 7151 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 7152 | result = Smi::FromInt(EQUAL); |
| 7153 | } else { |
| 7154 | result = Smi::FromInt(NOT_EQUAL); |
| 7155 | } |
| 7156 | return result; |
| 7157 | } |
| 7158 | |
| 7159 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7160 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7161 | NoHandleAllocation ha; |
| 7162 | ASSERT(args.length() == 2); |
| 7163 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7164 | CONVERT_ARG_CHECKED(String, x, 0); |
| 7165 | CONVERT_ARG_CHECKED(String, y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7166 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7167 | bool not_equal = !x->Equals(y); |
| 7168 | // This is slightly convoluted because the value that signifies |
| 7169 | // equality is 0 and inequality is 1 so we have to negate the result |
| 7170 | // from String::Equals. |
| 7171 | ASSERT(not_equal == 0 || not_equal == 1); |
| 7172 | STATIC_CHECK(EQUAL == 0); |
| 7173 | STATIC_CHECK(NOT_EQUAL == 1); |
| 7174 | return Smi::FromInt(not_equal); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7178 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7179 | NoHandleAllocation ha; |
| 7180 | ASSERT(args.length() == 3); |
| 7181 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7182 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7183 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7184 | if (isnan(x) || isnan(y)) return args[2]; |
| 7185 | if (x == y) return Smi::FromInt(EQUAL); |
| 7186 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 7187 | return Smi::FromInt(GREATER); |
| 7188 | } |
| 7189 | |
| 7190 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7191 | // Compare two Smis as if they were converted to strings and then |
| 7192 | // compared lexicographically. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7193 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7194 | NoHandleAllocation ha; |
| 7195 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7196 | CONVERT_SMI_ARG_CHECKED(x_value, 0); |
| 7197 | CONVERT_SMI_ARG_CHECKED(y_value, 1); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7198 | |
| 7199 | // If the integers are equal so are the string representations. |
| 7200 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 7201 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7202 | // 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] | 7203 | // same as the lexicographic order of the string representations. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7204 | if (x_value == 0 || y_value == 0) |
| 7205 | return Smi::FromInt(x_value < y_value ? LESS : GREATER); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7206 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 7207 | // 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] | 7208 | // smallest because the char code of '-' is less than the char code |
| 7209 | // of any digit. Otherwise, we make both values positive. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7210 | |
| 7211 | // Use unsigned values otherwise the logic is incorrect for -MIN_INT on |
| 7212 | // architectures using 32-bit Smis. |
| 7213 | uint32_t x_scaled = x_value; |
| 7214 | uint32_t y_scaled = y_value; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7215 | if (x_value < 0 || y_value < 0) { |
| 7216 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 7217 | if (x_value >= 0) return Smi::FromInt(GREATER); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7218 | x_scaled = -x_value; |
| 7219 | y_scaled = -y_value; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7222 | static const uint32_t kPowersOf10[] = { |
| 7223 | 1, 10, 100, 1000, 10*1000, 100*1000, |
| 7224 | 1000*1000, 10*1000*1000, 100*1000*1000, |
| 7225 | 1000*1000*1000 |
| 7226 | }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7227 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7228 | // If the integers have the same number of decimal digits they can be |
| 7229 | // compared directly as the numeric order is the same as the |
| 7230 | // lexicographic order. If one integer has fewer digits, it is scaled |
| 7231 | // by some power of 10 to have the same number of digits as the longer |
| 7232 | // integer. If the scaled integers are equal it means the shorter |
| 7233 | // integer comes first in the lexicographic order. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7234 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7235 | // From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
| 7236 | int x_log2 = IntegerLog2(x_scaled); |
| 7237 | int x_log10 = ((x_log2 + 1) * 1233) >> 12; |
| 7238 | x_log10 -= x_scaled < kPowersOf10[x_log10]; |
| 7239 | |
| 7240 | int y_log2 = IntegerLog2(y_scaled); |
| 7241 | int y_log10 = ((y_log2 + 1) * 1233) >> 12; |
| 7242 | y_log10 -= y_scaled < kPowersOf10[y_log10]; |
| 7243 | |
| 7244 | int tie = EQUAL; |
| 7245 | |
| 7246 | if (x_log10 < y_log10) { |
| 7247 | // X has fewer digits. We would like to simply scale up X but that |
| 7248 | // might overflow, e.g when comparing 9 with 1_000_000_000, 9 would |
| 7249 | // be scaled up to 9_000_000_000. So we scale up by the next |
| 7250 | // smallest power and scale down Y to drop one digit. It is OK to |
| 7251 | // drop one digit from the longer integer since the final digit is |
| 7252 | // past the length of the shorter integer. |
| 7253 | x_scaled *= kPowersOf10[y_log10 - x_log10 - 1]; |
| 7254 | y_scaled /= 10; |
| 7255 | tie = LESS; |
| 7256 | } else if (y_log10 < x_log10) { |
| 7257 | y_scaled *= kPowersOf10[x_log10 - y_log10 - 1]; |
| 7258 | x_scaled /= 10; |
| 7259 | tie = GREATER; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7260 | } |
| 7261 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 7262 | if (x_scaled < y_scaled) return Smi::FromInt(LESS); |
| 7263 | if (x_scaled > y_scaled) return Smi::FromInt(GREATER); |
| 7264 | return Smi::FromInt(tie); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
| 7267 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7268 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 7269 | String* x, |
| 7270 | String* y) { |
| 7271 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 7272 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7273 | bufx.Reset(x); |
| 7274 | bufy.Reset(y); |
| 7275 | while (bufx.has_more() && bufy.has_more()) { |
| 7276 | int d = bufx.GetNext() - bufy.GetNext(); |
| 7277 | if (d < 0) return Smi::FromInt(LESS); |
| 7278 | else if (d > 0) return Smi::FromInt(GREATER); |
| 7279 | } |
| 7280 | |
| 7281 | // x is (non-trivial) prefix of y: |
| 7282 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 7283 | // y is prefix of x: |
| 7284 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 7285 | } |
| 7286 | |
| 7287 | |
| 7288 | static Object* FlatStringCompare(String* x, String* y) { |
| 7289 | ASSERT(x->IsFlat()); |
| 7290 | ASSERT(y->IsFlat()); |
| 7291 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 7292 | int prefix_length = x->length(); |
| 7293 | if (y->length() < prefix_length) { |
| 7294 | prefix_length = y->length(); |
| 7295 | equal_prefix_result = Smi::FromInt(GREATER); |
| 7296 | } else if (y->length() > prefix_length) { |
| 7297 | equal_prefix_result = Smi::FromInt(LESS); |
| 7298 | } |
| 7299 | int r; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7300 | String::FlatContent x_content = x->GetFlatContent(); |
| 7301 | String::FlatContent y_content = y->GetFlatContent(); |
| 7302 | if (x_content.IsAscii()) { |
| 7303 | Vector<const char> x_chars = x_content.ToAsciiVector(); |
| 7304 | if (y_content.IsAscii()) { |
| 7305 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 7306 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7307 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7308 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7309 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7310 | } |
| 7311 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7312 | Vector<const uc16> x_chars = x_content.ToUC16Vector(); |
| 7313 | if (y_content.IsAscii()) { |
| 7314 | Vector<const char> y_chars = y_content.ToAsciiVector(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7315 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7316 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7317 | Vector<const uc16> y_chars = y_content.ToUC16Vector(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7318 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 7319 | } |
| 7320 | } |
| 7321 | Object* result; |
| 7322 | if (r == 0) { |
| 7323 | result = equal_prefix_result; |
| 7324 | } else { |
| 7325 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 7326 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7327 | ASSERT(result == |
| 7328 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7329 | return result; |
| 7330 | } |
| 7331 | |
| 7332 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7333 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7334 | NoHandleAllocation ha; |
| 7335 | ASSERT(args.length() == 2); |
| 7336 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7337 | CONVERT_ARG_CHECKED(String, x, 0); |
| 7338 | CONVERT_ARG_CHECKED(String, y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7339 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7340 | isolate->counters()->string_compare_runtime()->Increment(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 7341 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7342 | // A few fast case tests before we flatten. |
| 7343 | if (x == y) return Smi::FromInt(EQUAL); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7344 | if (y->length() == 0) { |
| 7345 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7346 | return Smi::FromInt(GREATER); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7347 | } else if (x->length() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7348 | return Smi::FromInt(LESS); |
| 7349 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7350 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 7351 | int d = x->Get(0) - y->Get(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7352 | if (d < 0) return Smi::FromInt(LESS); |
| 7353 | else if (d > 0) return Smi::FromInt(GREATER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7354 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7355 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7356 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7357 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7358 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7359 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7360 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7361 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7362 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7363 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7364 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7365 | } |
| 7366 | |
| 7367 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7368 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_acos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7369 | NoHandleAllocation ha; |
| 7370 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7371 | isolate->counters()->math_acos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7372 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7373 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7374 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7375 | } |
| 7376 | |
| 7377 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7378 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_asin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7379 | NoHandleAllocation ha; |
| 7380 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7381 | isolate->counters()->math_asin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7382 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7383 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7384 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7385 | } |
| 7386 | |
| 7387 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7388 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7389 | NoHandleAllocation ha; |
| 7390 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7391 | isolate->counters()->math_atan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7392 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7393 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7394 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7395 | } |
| 7396 | |
| 7397 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7398 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 7399 | |
| 7400 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7401 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7402 | NoHandleAllocation ha; |
| 7403 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7404 | isolate->counters()->math_atan2()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7405 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7406 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7407 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7408 | double result; |
| 7409 | if (isinf(x) && isinf(y)) { |
| 7410 | // Make sure that the result in case of two infinite arguments |
| 7411 | // is a multiple of Pi / 4. The sign of the result is determined |
| 7412 | // by the first argument (x) and the sign of the second argument |
| 7413 | // determines the multiplier: one or three. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7414 | int multiplier = (x < 0) ? -1 : 1; |
| 7415 | if (y < 0) multiplier *= 3; |
| 7416 | result = multiplier * kPiDividedBy4; |
| 7417 | } else { |
| 7418 | result = atan2(x, y); |
| 7419 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7420 | return isolate->heap()->AllocateHeapNumber(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7424 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_ceil) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7425 | NoHandleAllocation ha; |
| 7426 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7427 | isolate->counters()->math_ceil()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7428 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7429 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7430 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
| 7433 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7434 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7435 | NoHandleAllocation ha; |
| 7436 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7437 | isolate->counters()->math_cos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7438 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7439 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7440 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7441 | } |
| 7442 | |
| 7443 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7444 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_exp) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7445 | NoHandleAllocation ha; |
| 7446 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7447 | isolate->counters()->math_exp()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7448 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7449 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7450 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
| 7453 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7454 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_floor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7455 | NoHandleAllocation ha; |
| 7456 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7457 | isolate->counters()->math_floor()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7458 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7459 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7460 | return isolate->heap()->NumberFromDouble(floor(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7461 | } |
| 7462 | |
| 7463 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7464 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7465 | NoHandleAllocation ha; |
| 7466 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7467 | isolate->counters()->math_log()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7468 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7469 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7470 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7471 | } |
| 7472 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7473 | // Slow version of Math.pow. We check for fast paths for special cases. |
| 7474 | // Used if SSE2/VFP3 is not available. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7475 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7476 | NoHandleAllocation ha; |
| 7477 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7478 | isolate->counters()->math_pow()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7479 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7480 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7481 | |
| 7482 | // If the second argument is a smi, it is much faster to call the |
| 7483 | // custom powi() function than the generic pow(). |
| 7484 | if (args[1]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7485 | int y = args.smi_at(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7486 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7487 | } |
| 7488 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7489 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7490 | int y_int = static_cast<int>(y); |
| 7491 | double result; |
| 7492 | if (y == y_int) { |
| 7493 | result = power_double_int(x, y_int); // Returns 1 if exponent is 0. |
| 7494 | } else if (y == 0.5) { |
| 7495 | result = (isinf(x)) ? V8_INFINITY : sqrt(x + 0.0); // Convert -0 to +0. |
| 7496 | } else if (y == -0.5) { |
| 7497 | result = (isinf(x)) ? 0 : 1.0 / sqrt(x + 0.0); // Convert -0 to +0. |
| 7498 | } else { |
| 7499 | result = power_double_double(x, y); |
| 7500 | } |
| 7501 | if (isnan(result)) return isolate->heap()->nan_value(); |
| 7502 | return isolate->heap()->AllocateHeapNumber(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7505 | // Fast version of Math.pow if we know that y is not an integer and y is not |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 7506 | // -0.5 or 0.5. Used as slow case from full codegen. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7507 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7508 | NoHandleAllocation ha; |
| 7509 | ASSERT(args.length() == 2); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7510 | isolate->counters()->math_pow()->Increment(); |
| 7511 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7512 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 7513 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7514 | if (y == 0) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7515 | return Smi::FromInt(1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7516 | } else { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 7517 | double result = power_double_double(x, y); |
| 7518 | if (isnan(result)) return isolate->heap()->nan_value(); |
| 7519 | return isolate->heap()->AllocateHeapNumber(result); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7520 | } |
| 7521 | } |
| 7522 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7523 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7524 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7525 | NoHandleAllocation ha; |
| 7526 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7527 | isolate->counters()->math_round()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7528 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7529 | if (!args[0]->IsHeapNumber()) { |
| 7530 | // Must be smi. Return the argument unchanged for all the other types |
| 7531 | // to make fuzz-natives test happy. |
| 7532 | return args[0]; |
| 7533 | } |
| 7534 | |
| 7535 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 7536 | |
| 7537 | double value = number->value(); |
| 7538 | int exponent = number->get_exponent(); |
| 7539 | int sign = number->get_sign(); |
| 7540 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7541 | if (exponent < -1) { |
| 7542 | // Number in range ]-0.5..0.5[. These always round to +/-zero. |
| 7543 | if (sign) return isolate->heap()->minus_zero_value(); |
| 7544 | return Smi::FromInt(0); |
| 7545 | } |
| 7546 | |
| 7547 | // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and |
| 7548 | // should be rounded to 2^30, which is not smi (for 31-bit smis, similar |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 7549 | // argument holds for 32-bit smis). |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 7550 | if (!sign && exponent < kSmiValueSize - 2) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7551 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 7552 | } |
| 7553 | |
| 7554 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 7555 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 7556 | if (exponent >= 52) { |
| 7557 | return number; |
| 7558 | } |
| 7559 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7560 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7561 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7562 | // Do not call NumberFromDouble() to avoid extra checks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7563 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7567 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7568 | NoHandleAllocation ha; |
| 7569 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7570 | isolate->counters()->math_sin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7571 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7572 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7573 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7574 | } |
| 7575 | |
| 7576 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7577 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sqrt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7578 | NoHandleAllocation ha; |
| 7579 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7580 | isolate->counters()->math_sqrt()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7581 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7582 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7583 | return isolate->heap()->AllocateHeapNumber(sqrt(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7584 | } |
| 7585 | |
| 7586 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7587 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_tan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7588 | NoHandleAllocation ha; |
| 7589 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7590 | isolate->counters()->math_tan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7591 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7592 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7593 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7594 | } |
| 7595 | |
| 7596 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7597 | static int MakeDay(int year, int month) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7598 | static const int day_from_month[] = {0, 31, 59, 90, 120, 151, |
| 7599 | 181, 212, 243, 273, 304, 334}; |
| 7600 | static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152, |
| 7601 | 182, 213, 244, 274, 305, 335}; |
| 7602 | |
| 7603 | year += month / 12; |
| 7604 | month %= 12; |
| 7605 | if (month < 0) { |
| 7606 | year--; |
| 7607 | month += 12; |
| 7608 | } |
| 7609 | |
| 7610 | ASSERT(month >= 0); |
| 7611 | ASSERT(month < 12); |
| 7612 | |
| 7613 | // year_delta is an arbitrary number such that: |
| 7614 | // a) year_delta = -1 (mod 400) |
| 7615 | // b) year + year_delta > 0 for years in the range defined by |
| 7616 | // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
| 7617 | // Jan 1 1970. This is required so that we don't run into integer |
| 7618 | // division of negative numbers. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7619 | // 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] | 7620 | // operations. |
| 7621 | static const int year_delta = 399999; |
| 7622 | static const int base_day = 365 * (1970 + year_delta) + |
| 7623 | (1970 + year_delta) / 4 - |
| 7624 | (1970 + year_delta) / 100 + |
| 7625 | (1970 + year_delta) / 400; |
| 7626 | |
| 7627 | int year1 = year + year_delta; |
| 7628 | int day_from_year = 365 * year1 + |
| 7629 | year1 / 4 - |
| 7630 | year1 / 100 + |
| 7631 | year1 / 400 - |
| 7632 | base_day; |
| 7633 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7634 | if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) { |
| 7635 | return day_from_year + day_from_month[month]; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7638 | return day_from_year + day_from_month_leap[month]; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7642 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7643 | NoHandleAllocation ha; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7644 | ASSERT(args.length() == 2); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7645 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7646 | CONVERT_SMI_ARG_CHECKED(year, 0); |
| 7647 | CONVERT_SMI_ARG_CHECKED(month, 1); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7648 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7649 | return Smi::FromInt(MakeDay(year, month)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
| 7652 | |
| 7653 | static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1}; |
| 7654 | static const int kDaysIn4Years = 4 * 365 + 1; |
| 7655 | static const int kDaysIn100Years = 25 * kDaysIn4Years - 1; |
| 7656 | static const int kDaysIn400Years = 4 * kDaysIn100Years + 1; |
| 7657 | static const int kDays1970to2000 = 30 * 365 + 7; |
| 7658 | static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - |
| 7659 | kDays1970to2000; |
| 7660 | static const int kYearsOffset = 400000; |
| 7661 | |
| 7662 | static const char kDayInYear[] = { |
| 7663 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7664 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7665 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7666 | 22, 23, 24, 25, 26, 27, 28, |
| 7667 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7668 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7669 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7670 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7671 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7672 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7673 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7674 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7675 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7676 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7677 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7678 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7679 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7680 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7681 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7682 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7683 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7684 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7685 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7686 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7687 | |
| 7688 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7689 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7690 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7691 | 22, 23, 24, 25, 26, 27, 28, |
| 7692 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7693 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7694 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7695 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7696 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7697 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7698 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7699 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7700 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7701 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7702 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7703 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7704 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7705 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7706 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7707 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7708 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7709 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7710 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7711 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7712 | |
| 7713 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7714 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7715 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7716 | 22, 23, 24, 25, 26, 27, 28, 29, |
| 7717 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7718 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7719 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7720 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7721 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7722 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7723 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7724 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7725 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7726 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7727 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7728 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7729 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7730 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7731 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7732 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7733 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7734 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7735 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7736 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7737 | |
| 7738 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7739 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7740 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7741 | 22, 23, 24, 25, 26, 27, 28, |
| 7742 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7743 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7744 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7745 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7746 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7747 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7748 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7749 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7750 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7751 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7752 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7753 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7754 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7755 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7756 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7757 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7758 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7759 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7760 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7761 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 7762 | |
| 7763 | static const char kMonthInYear[] = { |
| 7764 | 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, |
| 7765 | 0, 0, 0, 0, 0, 0, |
| 7766 | 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, |
| 7767 | 1, 1, 1, |
| 7768 | 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, |
| 7769 | 2, 2, 2, 2, 2, 2, |
| 7770 | 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, |
| 7771 | 3, 3, 3, 3, 3, |
| 7772 | 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, |
| 7773 | 4, 4, 4, 4, 4, 4, |
| 7774 | 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, |
| 7775 | 5, 5, 5, 5, 5, |
| 7776 | 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, |
| 7777 | 6, 6, 6, 6, 6, 6, |
| 7778 | 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, |
| 7779 | 7, 7, 7, 7, 7, 7, |
| 7780 | 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, |
| 7781 | 8, 8, 8, 8, 8, |
| 7782 | 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, |
| 7783 | 9, 9, 9, 9, 9, 9, |
| 7784 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7785 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7786 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7787 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7788 | |
| 7789 | 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, |
| 7790 | 0, 0, 0, 0, 0, 0, |
| 7791 | 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, |
| 7792 | 1, 1, 1, |
| 7793 | 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, |
| 7794 | 2, 2, 2, 2, 2, 2, |
| 7795 | 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, |
| 7796 | 3, 3, 3, 3, 3, |
| 7797 | 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, |
| 7798 | 4, 4, 4, 4, 4, 4, |
| 7799 | 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, |
| 7800 | 5, 5, 5, 5, 5, |
| 7801 | 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, |
| 7802 | 6, 6, 6, 6, 6, 6, |
| 7803 | 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, |
| 7804 | 7, 7, 7, 7, 7, 7, |
| 7805 | 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, |
| 7806 | 8, 8, 8, 8, 8, |
| 7807 | 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, |
| 7808 | 9, 9, 9, 9, 9, 9, |
| 7809 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7810 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7811 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7812 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7813 | |
| 7814 | 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, |
| 7815 | 0, 0, 0, 0, 0, 0, |
| 7816 | 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, |
| 7817 | 1, 1, 1, 1, |
| 7818 | 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, |
| 7819 | 2, 2, 2, 2, 2, 2, |
| 7820 | 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, |
| 7821 | 3, 3, 3, 3, 3, |
| 7822 | 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, |
| 7823 | 4, 4, 4, 4, 4, 4, |
| 7824 | 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, |
| 7825 | 5, 5, 5, 5, 5, |
| 7826 | 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, |
| 7827 | 6, 6, 6, 6, 6, 6, |
| 7828 | 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, |
| 7829 | 7, 7, 7, 7, 7, 7, |
| 7830 | 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, |
| 7831 | 8, 8, 8, 8, 8, |
| 7832 | 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, |
| 7833 | 9, 9, 9, 9, 9, 9, |
| 7834 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7835 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7836 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7837 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7838 | |
| 7839 | 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, |
| 7840 | 0, 0, 0, 0, 0, 0, |
| 7841 | 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, |
| 7842 | 1, 1, 1, |
| 7843 | 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, |
| 7844 | 2, 2, 2, 2, 2, 2, |
| 7845 | 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, |
| 7846 | 3, 3, 3, 3, 3, |
| 7847 | 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, |
| 7848 | 4, 4, 4, 4, 4, 4, |
| 7849 | 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, |
| 7850 | 5, 5, 5, 5, 5, |
| 7851 | 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, |
| 7852 | 6, 6, 6, 6, 6, 6, |
| 7853 | 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, |
| 7854 | 7, 7, 7, 7, 7, 7, |
| 7855 | 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, |
| 7856 | 8, 8, 8, 8, 8, |
| 7857 | 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, |
| 7858 | 9, 9, 9, 9, 9, 9, |
| 7859 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7860 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7861 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7862 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; |
| 7863 | |
| 7864 | |
| 7865 | // This function works for dates from 1970 to 2099. |
| 7866 | static inline void DateYMDFromTimeAfter1970(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7867 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7868 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7869 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7870 | #endif |
| 7871 | |
| 7872 | year = 1970 + (4 * date + 2) / kDaysIn4Years; |
| 7873 | date %= kDaysIn4Years; |
| 7874 | |
| 7875 | month = kMonthInYear[date]; |
| 7876 | day = kDayInYear[date]; |
| 7877 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7878 | ASSERT(MakeDay(year, month) + day - 1 == save_date); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7879 | } |
| 7880 | |
| 7881 | |
| 7882 | static inline void DateYMDFromTimeSlow(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7883 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7884 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7885 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7886 | #endif |
| 7887 | |
| 7888 | date += kDaysOffset; |
| 7889 | year = 400 * (date / kDaysIn400Years) - kYearsOffset; |
| 7890 | date %= kDaysIn400Years; |
| 7891 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7892 | ASSERT(MakeDay(year, 0) + date == save_date); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7893 | |
| 7894 | date--; |
| 7895 | int yd1 = date / kDaysIn100Years; |
| 7896 | date %= kDaysIn100Years; |
| 7897 | year += 100 * yd1; |
| 7898 | |
| 7899 | date++; |
| 7900 | int yd2 = date / kDaysIn4Years; |
| 7901 | date %= kDaysIn4Years; |
| 7902 | year += 4 * yd2; |
| 7903 | |
| 7904 | date--; |
| 7905 | int yd3 = date / 365; |
| 7906 | date %= 365; |
| 7907 | year += yd3; |
| 7908 | |
| 7909 | bool is_leap = (!yd1 || yd2) && !yd3; |
| 7910 | |
| 7911 | ASSERT(date >= -1); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7912 | ASSERT(is_leap || (date >= 0)); |
| 7913 | ASSERT((date < 365) || (is_leap && (date < 366))); |
| 7914 | ASSERT(is_leap == ((year % 4 == 0) && (year % 100 || (year % 400 == 0)))); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7915 | ASSERT(is_leap || ((MakeDay(year, 0) + date) == save_date)); |
| 7916 | ASSERT(!is_leap || ((MakeDay(year, 0) + date + 1) == save_date)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7917 | |
| 7918 | if (is_leap) { |
| 7919 | day = kDayInYear[2*365 + 1 + date]; |
| 7920 | month = kMonthInYear[2*365 + 1 + date]; |
| 7921 | } else { |
| 7922 | day = kDayInYear[date]; |
| 7923 | month = kMonthInYear[date]; |
| 7924 | } |
| 7925 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7926 | ASSERT(MakeDay(year, month) + day - 1 == save_date); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
| 7929 | |
| 7930 | static inline void DateYMDFromTime(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7931 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7932 | if (date >= 0 && date < 32 * kDaysIn4Years) { |
| 7933 | DateYMDFromTimeAfter1970(date, year, month, day); |
| 7934 | } else { |
| 7935 | DateYMDFromTimeSlow(date, year, month, day); |
| 7936 | } |
| 7937 | } |
| 7938 | |
| 7939 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7940 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateYMDFromTime) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7941 | NoHandleAllocation ha; |
| 7942 | ASSERT(args.length() == 2); |
| 7943 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7944 | CONVERT_DOUBLE_ARG_CHECKED(t, 0); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 7945 | CONVERT_ARG_CHECKED(JSArray, res_array, 1); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7946 | |
| 7947 | int year, month, day; |
| 7948 | DateYMDFromTime(static_cast<int>(floor(t / 86400000)), year, month, day); |
| 7949 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7950 | FixedArrayBase* elms_base = FixedArrayBase::cast(res_array->elements()); |
| 7951 | RUNTIME_ASSERT(elms_base->length() == 3); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7952 | RUNTIME_ASSERT(res_array->HasFastTypeElements()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 7953 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 7954 | MaybeObject* maybe = res_array->EnsureWritableFastElements(); |
| 7955 | if (maybe->IsFailure()) return maybe; |
| 7956 | FixedArray* elms = FixedArray::cast(res_array->elements()); |
| 7957 | elms->set(0, Smi::FromInt(year)); |
| 7958 | elms->set(1, Smi::FromInt(month)); |
| 7959 | elms->set(2, Smi::FromInt(day)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7960 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7961 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7962 | } |
| 7963 | |
| 7964 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7965 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewArgumentsFast) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7966 | HandleScope scope(isolate); |
| 7967 | ASSERT(args.length() == 3); |
| 7968 | |
| 7969 | Handle<JSFunction> callee = args.at<JSFunction>(0); |
| 7970 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7971 | const int argument_count = Smi::cast(args[2])->value(); |
| 7972 | |
| 7973 | Handle<JSObject> result = |
| 7974 | isolate->factory()->NewArgumentsObject(callee, argument_count); |
| 7975 | // Allocate the elements if needed. |
| 7976 | int parameter_count = callee->shared()->formal_parameter_count(); |
| 7977 | if (argument_count > 0) { |
| 7978 | if (parameter_count > 0) { |
| 7979 | int mapped_count = Min(argument_count, parameter_count); |
| 7980 | Handle<FixedArray> parameter_map = |
| 7981 | isolate->factory()->NewFixedArray(mapped_count + 2, NOT_TENURED); |
| 7982 | parameter_map->set_map( |
| 7983 | isolate->heap()->non_strict_arguments_elements_map()); |
| 7984 | |
| 7985 | Handle<Map> old_map(result->map()); |
| 7986 | Handle<Map> new_map = |
| 7987 | isolate->factory()->CopyMapDropTransitions(old_map); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 7988 | new_map->set_elements_kind(NON_STRICT_ARGUMENTS_ELEMENTS); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7989 | |
| 7990 | result->set_map(*new_map); |
| 7991 | result->set_elements(*parameter_map); |
| 7992 | |
| 7993 | // Store the context and the arguments array at the beginning of the |
| 7994 | // parameter map. |
| 7995 | Handle<Context> context(isolate->context()); |
| 7996 | Handle<FixedArray> arguments = |
| 7997 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7998 | parameter_map->set(0, *context); |
| 7999 | parameter_map->set(1, *arguments); |
| 8000 | |
| 8001 | // Loop over the actual parameters backwards. |
| 8002 | int index = argument_count - 1; |
| 8003 | while (index >= mapped_count) { |
| 8004 | // These go directly in the arguments array and have no |
| 8005 | // corresponding slot in the parameter map. |
| 8006 | arguments->set(index, *(parameters - index - 1)); |
| 8007 | --index; |
| 8008 | } |
| 8009 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8010 | Handle<ScopeInfo> scope_info(callee->shared()->scope_info()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8011 | while (index >= 0) { |
| 8012 | // Detect duplicate names to the right in the parameter list. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8013 | Handle<String> name(scope_info->ParameterName(index)); |
| 8014 | int context_local_count = scope_info->ContextLocalCount(); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8015 | bool duplicate = false; |
| 8016 | for (int j = index + 1; j < parameter_count; ++j) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8017 | if (scope_info->ParameterName(j) == *name) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8018 | duplicate = true; |
| 8019 | break; |
| 8020 | } |
| 8021 | } |
| 8022 | |
| 8023 | if (duplicate) { |
| 8024 | // This goes directly in the arguments array with a hole in the |
| 8025 | // parameter map. |
| 8026 | arguments->set(index, *(parameters - index - 1)); |
| 8027 | parameter_map->set_the_hole(index + 2); |
| 8028 | } else { |
| 8029 | // The context index goes in the parameter map with a hole in the |
| 8030 | // arguments array. |
| 8031 | int context_index = -1; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8032 | for (int j = 0; j < context_local_count; ++j) { |
| 8033 | if (scope_info->ContextLocalName(j) == *name) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8034 | context_index = j; |
| 8035 | break; |
| 8036 | } |
| 8037 | } |
| 8038 | ASSERT(context_index >= 0); |
| 8039 | arguments->set_the_hole(index); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8040 | parameter_map->set(index + 2, Smi::FromInt( |
| 8041 | Context::MIN_CONTEXT_SLOTS + context_index)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8042 | } |
| 8043 | |
| 8044 | --index; |
| 8045 | } |
| 8046 | } else { |
| 8047 | // If there is no aliasing, the arguments object elements are not |
| 8048 | // special in any way. |
| 8049 | Handle<FixedArray> elements = |
| 8050 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 8051 | result->set_elements(*elements); |
| 8052 | for (int i = 0; i < argument_count; ++i) { |
| 8053 | elements->set(i, *(parameters - i - 1)); |
| 8054 | } |
| 8055 | } |
| 8056 | } |
| 8057 | return *result; |
| 8058 | } |
| 8059 | |
| 8060 | |
| 8061 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStrictArgumentsFast) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8062 | NoHandleAllocation ha; |
| 8063 | ASSERT(args.length() == 3); |
| 8064 | |
| 8065 | JSFunction* callee = JSFunction::cast(args[0]); |
| 8066 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8067 | const int length = args.smi_at(2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8068 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8069 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8070 | { MaybeObject* maybe_result = |
| 8071 | isolate->heap()->AllocateArgumentsObject(callee, length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8072 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8073 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8074 | // Allocate the elements if needed. |
| 8075 | if (length > 0) { |
| 8076 | // Allocate the fixed array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8077 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8078 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8079 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8080 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8081 | |
| 8082 | AssertNoAllocation no_gc; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 8083 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8084 | array->set_map_no_write_barrier(isolate->heap()->fixed_array_map()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8085 | array->set_length(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8086 | |
| 8087 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8088 | for (int i = 0; i < length; i++) { |
| 8089 | array->set(i, *--parameters, mode); |
| 8090 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8091 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 8092 | } |
| 8093 | return result; |
| 8094 | } |
| 8095 | |
| 8096 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8097 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewClosure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8098 | HandleScope scope(isolate); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 8099 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8100 | CONVERT_ARG_HANDLE_CHECKED(Context, context, 0); |
| 8101 | CONVERT_ARG_HANDLE_CHECKED(SharedFunctionInfo, shared, 1); |
| 8102 | CONVERT_BOOLEAN_ARG_CHECKED(pretenure, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8103 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8104 | // The caller ensures that we pretenure closures that are assigned |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 8105 | // directly to properties. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 8106 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8107 | Handle<JSFunction> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8108 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 8109 | context, |
| 8110 | pretenure_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8111 | return *result; |
| 8112 | } |
| 8113 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8114 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8115 | // Find the arguments of the JavaScript function invocation that called |
| 8116 | // into C++ code. Collect these in a newly allocated array of handles (possibly |
| 8117 | // prefixed by a number of empty handles). |
| 8118 | static SmartArrayPointer<Handle<Object> > GetCallerArguments( |
| 8119 | int prefix_argc, |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 8120 | int* total_argc) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8121 | // Find frame containing arguments passed to the caller. |
| 8122 | JavaScriptFrameIterator it; |
| 8123 | JavaScriptFrame* frame = it.frame(); |
| 8124 | List<JSFunction*> functions(2); |
| 8125 | frame->GetFunctions(&functions); |
| 8126 | if (functions.length() > 1) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 8127 | int inlined_jsframe_index = functions.length() - 1; |
| 8128 | JSFunction* inlined_function = functions[inlined_jsframe_index]; |
| 8129 | Vector<SlotRef> args_slots = |
| 8130 | SlotRef::ComputeSlotMappingForArguments( |
| 8131 | frame, |
| 8132 | inlined_jsframe_index, |
| 8133 | inlined_function->shared()->formal_parameter_count()); |
| 8134 | |
| 8135 | int args_count = args_slots.length(); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8136 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8137 | *total_argc = prefix_argc + args_count; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 8138 | SmartArrayPointer<Handle<Object> > param_data( |
| 8139 | NewArray<Handle<Object> >(*total_argc)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8140 | for (int i = 0; i < args_count; i++) { |
| 8141 | Handle<Object> val = args_slots[i].GetValue(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8142 | param_data[prefix_argc + i] = val; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8143 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 8144 | |
| 8145 | args_slots.Dispose(); |
| 8146 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8147 | return param_data; |
| 8148 | } else { |
| 8149 | it.AdvanceToArgumentsFrame(); |
| 8150 | frame = it.frame(); |
| 8151 | int args_count = frame->ComputeParametersCount(); |
| 8152 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8153 | *total_argc = prefix_argc + args_count; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 8154 | SmartArrayPointer<Handle<Object> > param_data( |
| 8155 | NewArray<Handle<Object> >(*total_argc)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8156 | for (int i = 0; i < args_count; i++) { |
| 8157 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8158 | param_data[prefix_argc + i] = val; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8159 | } |
| 8160 | return param_data; |
| 8161 | } |
| 8162 | } |
| 8163 | |
| 8164 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8165 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionBindArguments) { |
| 8166 | HandleScope scope(isolate); |
| 8167 | ASSERT(args.length() == 4); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8168 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, bound_function, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8169 | RUNTIME_ASSERT(args[3]->IsNumber()); |
| 8170 | Handle<Object> bindee = args.at<Object>(1); |
| 8171 | |
| 8172 | // TODO(lrn): Create bound function in C++ code from premade shared info. |
| 8173 | bound_function->shared()->set_bound(true); |
| 8174 | // Get all arguments of calling function (Function.prototype.bind). |
| 8175 | int argc = 0; |
| 8176 | SmartArrayPointer<Handle<Object> > arguments = GetCallerArguments(0, &argc); |
| 8177 | // Don't count the this-arg. |
| 8178 | if (argc > 0) { |
| 8179 | ASSERT(*arguments[0] == args[2]); |
| 8180 | argc--; |
| 8181 | } else { |
| 8182 | ASSERT(args[2]->IsUndefined()); |
| 8183 | } |
| 8184 | // Initialize array of bindings (function, this, and any existing arguments |
| 8185 | // if the function was already bound). |
| 8186 | Handle<FixedArray> new_bindings; |
| 8187 | int i; |
| 8188 | if (bindee->IsJSFunction() && JSFunction::cast(*bindee)->shared()->bound()) { |
| 8189 | Handle<FixedArray> old_bindings( |
| 8190 | JSFunction::cast(*bindee)->function_bindings()); |
| 8191 | new_bindings = |
| 8192 | isolate->factory()->NewFixedArray(old_bindings->length() + argc); |
| 8193 | bindee = Handle<Object>(old_bindings->get(JSFunction::kBoundFunctionIndex)); |
| 8194 | i = 0; |
| 8195 | for (int n = old_bindings->length(); i < n; i++) { |
| 8196 | new_bindings->set(i, old_bindings->get(i)); |
| 8197 | } |
| 8198 | } else { |
| 8199 | int array_size = JSFunction::kBoundArgumentsStartIndex + argc; |
| 8200 | new_bindings = isolate->factory()->NewFixedArray(array_size); |
| 8201 | new_bindings->set(JSFunction::kBoundFunctionIndex, *bindee); |
| 8202 | new_bindings->set(JSFunction::kBoundThisIndex, args[2]); |
| 8203 | i = 2; |
| 8204 | } |
| 8205 | // Copy arguments, skipping the first which is "this_arg". |
| 8206 | for (int j = 0; j < argc; j++, i++) { |
| 8207 | new_bindings->set(i, *arguments[j + 1]); |
| 8208 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 8209 | new_bindings->set_map_no_write_barrier( |
| 8210 | isolate->heap()->fixed_cow_array_map()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8211 | bound_function->set_function_bindings(*new_bindings); |
| 8212 | |
| 8213 | // Update length. |
| 8214 | Handle<String> length_symbol = isolate->factory()->length_symbol(); |
| 8215 | Handle<Object> new_length(args.at<Object>(3)); |
| 8216 | PropertyAttributes attr = |
| 8217 | static_cast<PropertyAttributes>(DONT_DELETE | DONT_ENUM | READ_ONLY); |
| 8218 | ForceSetProperty(bound_function, length_symbol, new_length, attr); |
| 8219 | return *bound_function; |
| 8220 | } |
| 8221 | |
| 8222 | |
| 8223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_BoundFunctionGetBindings) { |
| 8224 | HandleScope handles(isolate); |
| 8225 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8226 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, callable, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8227 | if (callable->IsJSFunction()) { |
| 8228 | Handle<JSFunction> function = Handle<JSFunction>::cast(callable); |
| 8229 | if (function->shared()->bound()) { |
| 8230 | Handle<FixedArray> bindings(function->function_bindings()); |
| 8231 | ASSERT(bindings->map() == isolate->heap()->fixed_cow_array_map()); |
| 8232 | return *isolate->factory()->NewJSArrayWithElements(bindings); |
| 8233 | } |
| 8234 | } |
| 8235 | return isolate->heap()->undefined_value(); |
| 8236 | } |
| 8237 | |
| 8238 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8239 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObjectFromBound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8240 | HandleScope scope(isolate); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8241 | ASSERT(args.length() == 1); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8242 | // First argument is a function to use as a constructor. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8243 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8244 | RUNTIME_ASSERT(function->shared()->bound()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8245 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8246 | // The argument is a bound function. Extract its bound arguments |
| 8247 | // and callable. |
| 8248 | Handle<FixedArray> bound_args = |
| 8249 | Handle<FixedArray>(FixedArray::cast(function->function_bindings())); |
| 8250 | int bound_argc = bound_args->length() - JSFunction::kBoundArgumentsStartIndex; |
| 8251 | Handle<Object> bound_function( |
| 8252 | JSReceiver::cast(bound_args->get(JSFunction::kBoundFunctionIndex))); |
| 8253 | ASSERT(!bound_function->IsJSFunction() || |
| 8254 | !Handle<JSFunction>::cast(bound_function)->shared()->bound()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8255 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8256 | int total_argc = 0; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 8257 | SmartArrayPointer<Handle<Object> > param_data = |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8258 | GetCallerArguments(bound_argc, &total_argc); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8259 | for (int i = 0; i < bound_argc; i++) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8260 | param_data[i] = Handle<Object>(bound_args->get( |
| 8261 | JSFunction::kBoundArgumentsStartIndex + i)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8262 | } |
| 8263 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8264 | if (!bound_function->IsJSFunction()) { |
| 8265 | bool exception_thrown; |
| 8266 | bound_function = Execution::TryGetConstructorDelegate(bound_function, |
| 8267 | &exception_thrown); |
| 8268 | if (exception_thrown) return Failure::Exception(); |
| 8269 | } |
| 8270 | ASSERT(bound_function->IsJSFunction()); |
| 8271 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 8272 | bool exception = false; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8273 | Handle<Object> result = |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8274 | Execution::New(Handle<JSFunction>::cast(bound_function), |
| 8275 | total_argc, *param_data, &exception); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 8276 | if (exception) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8277 | return Failure::Exception(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 8278 | } |
| 8279 | ASSERT(!result.is_null()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 8280 | return *result; |
| 8281 | } |
| 8282 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8283 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8284 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 8285 | Handle<JSFunction> function) { |
| 8286 | Handle<Object> prototype = isolate->factory()->null_value(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8287 | if (function->has_instance_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8288 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 8289 | } |
| 8290 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8291 | ConstructStubCompiler compiler(isolate); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8292 | Handle<Code> code = compiler.CompileConstructStub(function); |
| 8293 | function->shared()->set_construct_stub(*code); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8294 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
| 8297 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8298 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8299 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8300 | ASSERT(args.length() == 1); |
| 8301 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8302 | Handle<Object> constructor = args.at<Object>(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8303 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8304 | // If the constructor isn't a proper function we throw a type error. |
| 8305 | if (!constructor->IsJSFunction()) { |
| 8306 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 8307 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8308 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 8309 | return isolate->Throw(*type_error); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8310 | } |
| 8311 | |
| 8312 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 8313 | |
| 8314 | // If function should not have prototype, construction is not allowed. In this |
| 8315 | // case generated code bailouts here, since function has no initial_map. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8316 | if (!function->should_have_prototype() && !function->shared()->bound()) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 8317 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 8318 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8319 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 8320 | return isolate->Throw(*type_error); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 8321 | } |
| 8322 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8323 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8324 | Debug* debug = isolate->debug(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8325 | // Handle stepping into constructors if step into is active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8326 | if (debug->StepInActive()) { |
| 8327 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8328 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 8329 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8330 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8331 | if (function->has_initial_map()) { |
| 8332 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8333 | // The 'Function' function ignores the receiver object when |
| 8334 | // called using 'new' and creates a new JSFunction object that |
| 8335 | // is returned. The receiver object is only used for error |
| 8336 | // reporting if an error occurs when constructing the new |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8337 | // JSFunction. FACTORY->NewJSObject() should not be used to |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8338 | // allocate JSFunctions since it does not properly initialize |
| 8339 | // the shared part of the function. Since the receiver is |
| 8340 | // ignored anyway, we use the global object as the receiver |
| 8341 | // instead of a new JSFunction object. This way, errors are |
| 8342 | // reported the same way whether or not 'Function' is called |
| 8343 | // using 'new'. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8344 | return isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8345 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8348 | // The function should be compiled for the optimization hints to be |
| 8349 | // available. We cannot use EnsureCompiled because that forces a |
| 8350 | // compilation through the shared function info which makes it |
| 8351 | // impossible for us to optimize. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8352 | if (!function->is_compiled()) { |
| 8353 | JSFunction::CompileLazy(function, CLEAR_EXCEPTION); |
| 8354 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8355 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8356 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8357 | if (!function->has_initial_map() && |
| 8358 | shared->IsInobjectSlackTrackingInProgress()) { |
| 8359 | // The tracking is already in progress for another function. We can only |
| 8360 | // track one initial_map at a time, so we force the completion before the |
| 8361 | // function is called as a constructor for the first time. |
| 8362 | shared->CompleteInobjectSlackTracking(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8363 | } |
| 8364 | |
| 8365 | bool first_allocation = !shared->live_objects_may_exist(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8366 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 8367 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8368 | // Delay setting the stub if inobject slack tracking is in progress. |
| 8369 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8370 | TrySettingInlineConstructStub(isolate, function); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8371 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8372 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8373 | isolate->counters()->constructed_objects()->Increment(); |
| 8374 | isolate->counters()->constructed_objects_runtime()->Increment(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 8375 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 8376 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8377 | } |
| 8378 | |
| 8379 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8380 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinalizeInstanceSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8381 | HandleScope scope(isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8382 | ASSERT(args.length() == 1); |
| 8383 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8384 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8385 | function->shared()->CompleteInobjectSlackTracking(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8386 | TrySettingInlineConstructStub(isolate, function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8387 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8388 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 8389 | } |
| 8390 | |
| 8391 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8392 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8393 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8394 | ASSERT(args.length() == 1); |
| 8395 | |
| 8396 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 8397 | #ifdef DEBUG |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 8398 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8399 | PrintF("[lazy: "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8400 | function->PrintName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8401 | PrintF("]\n"); |
| 8402 | } |
| 8403 | #endif |
| 8404 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8405 | // Compile the target function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8406 | ASSERT(!function->is_compiled()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8407 | if (!JSFunction::CompileLazy(function, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8408 | return Failure::Exception(); |
| 8409 | } |
| 8410 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8411 | // All done. Return the compiled code. |
| 8412 | ASSERT(function->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8413 | return function->code(); |
| 8414 | } |
| 8415 | |
| 8416 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8417 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8418 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8419 | ASSERT(args.length() == 1); |
| 8420 | Handle<JSFunction> function = args.at<JSFunction>(0); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8421 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 8422 | function->shared()->set_profiler_ticks(0); |
| 8423 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8424 | // If the function is not compiled ignore the lazy |
| 8425 | // recompilation. This can happen if the debugger is activated and |
| 8426 | // the function is returned to the not compiled state. |
| 8427 | if (!function->shared()->is_compiled()) { |
| 8428 | function->ReplaceCode(function->shared()->code()); |
| 8429 | return function->code(); |
| 8430 | } |
| 8431 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8432 | // If the function is not optimizable or debugger is active continue using the |
| 8433 | // code from the full compiler. |
| 8434 | if (!function->shared()->code()->optimizable() || |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8435 | isolate->DebuggerHasBreakPoints()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8436 | if (FLAG_trace_opt) { |
| 8437 | PrintF("[failed to optimize "); |
| 8438 | function->PrintName(); |
| 8439 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 8440 | function->shared()->code()->optimizable() ? "T" : "F", |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8441 | isolate->DebuggerHasBreakPoints() ? "T" : "F"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8442 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8443 | function->ReplaceCode(function->shared()->code()); |
| 8444 | return function->code(); |
| 8445 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8446 | if (JSFunction::CompileOptimized(function, |
| 8447 | AstNode::kNoNumber, |
| 8448 | CLEAR_EXCEPTION)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8449 | return function->code(); |
| 8450 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 8451 | if (FLAG_trace_opt) { |
| 8452 | PrintF("[failed to optimize "); |
| 8453 | function->PrintName(); |
| 8454 | PrintF(": optimized compilation failed]\n"); |
| 8455 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8456 | function->ReplaceCode(function->shared()->code()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8457 | return function->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8458 | } |
| 8459 | |
| 8460 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 8461 | class ActivationsFinder : public ThreadVisitor { |
| 8462 | public: |
| 8463 | explicit ActivationsFinder(JSFunction* function) |
| 8464 | : function_(function), has_activations_(false) {} |
| 8465 | |
| 8466 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 8467 | if (has_activations_) return; |
| 8468 | |
| 8469 | for (JavaScriptFrameIterator it(isolate, top); !it.done(); it.Advance()) { |
| 8470 | JavaScriptFrame* frame = it.frame(); |
| 8471 | if (frame->is_optimized() && frame->function() == function_) { |
| 8472 | has_activations_ = true; |
| 8473 | return; |
| 8474 | } |
| 8475 | } |
| 8476 | } |
| 8477 | |
| 8478 | bool has_activations() { return has_activations_; } |
| 8479 | |
| 8480 | private: |
| 8481 | JSFunction* function_; |
| 8482 | bool has_activations_; |
| 8483 | }; |
| 8484 | |
| 8485 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8486 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyDeoptimized) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8487 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8488 | ASSERT(args.length() == 1); |
| 8489 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 8490 | Deoptimizer::BailoutType type = |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8491 | static_cast<Deoptimizer::BailoutType>(args.smi_at(0)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8492 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 8493 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 8494 | int jsframes = deoptimizer->jsframe_count(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8495 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8496 | deoptimizer->MaterializeHeapNumbers(); |
| 8497 | delete deoptimizer; |
| 8498 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8499 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8500 | JavaScriptFrame* frame = NULL; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 8501 | for (int i = 0; i < jsframes - 1; i++) it.Advance(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8502 | frame = it.frame(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8503 | |
| 8504 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8505 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8506 | Handle<Object> arguments; |
| 8507 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8508 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8509 | if (arguments.is_null()) { |
| 8510 | // FunctionGetArguments can't throw an exception, so cast away the |
| 8511 | // doubt with an assert. |
| 8512 | arguments = Handle<Object>( |
| 8513 | Accessors::FunctionGetArguments(*function, |
| 8514 | NULL)->ToObjectUnchecked()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8515 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 8516 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8517 | } |
| 8518 | frame->SetExpression(i, *arguments); |
| 8519 | } |
| 8520 | } |
| 8521 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8522 | if (type == Deoptimizer::EAGER) { |
| 8523 | RUNTIME_ASSERT(function->IsOptimized()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8524 | } |
| 8525 | |
| 8526 | // Avoid doing too much work when running with --always-opt and keep |
| 8527 | // the optimized code around. |
| 8528 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8529 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8530 | } |
| 8531 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 8532 | // Find other optimized activations of the function. |
| 8533 | bool has_other_activations = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8534 | while (!it.done()) { |
| 8535 | JavaScriptFrame* frame = it.frame(); |
| 8536 | if (frame->is_optimized() && frame->function() == *function) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 8537 | has_other_activations = true; |
| 8538 | break; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8539 | } |
| 8540 | it.Advance(); |
| 8541 | } |
| 8542 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 8543 | if (!has_other_activations) { |
| 8544 | ActivationsFinder activations_finder(*function); |
| 8545 | isolate->thread_manager()->IterateArchivedThreads(&activations_finder); |
| 8546 | has_other_activations = activations_finder.has_activations(); |
| 8547 | } |
| 8548 | |
| 8549 | if (!has_other_activations) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8550 | if (FLAG_trace_deopt) { |
| 8551 | PrintF("[removing optimized code for: "); |
| 8552 | function->PrintName(); |
| 8553 | PrintF("]\n"); |
| 8554 | } |
| 8555 | function->ReplaceCode(function->shared()->code()); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8556 | } else { |
| 8557 | Deoptimizer::DeoptimizeFunction(*function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8558 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8559 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8560 | } |
| 8561 | |
| 8562 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8563 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyOSR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8564 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8565 | delete deoptimizer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8566 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8567 | } |
| 8568 | |
| 8569 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8570 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeoptimizeFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8571 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8572 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8573 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8574 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8575 | |
| 8576 | Deoptimizer::DeoptimizeFunction(*function); |
| 8577 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8578 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8579 | } |
| 8580 | |
| 8581 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8582 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RunningInSimulator) { |
| 8583 | #if defined(USE_SIMULATOR) |
| 8584 | return isolate->heap()->true_value(); |
| 8585 | #else |
| 8586 | return isolate->heap()->false_value(); |
| 8587 | #endif |
| 8588 | } |
| 8589 | |
| 8590 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8591 | RUNTIME_FUNCTION(MaybeObject*, Runtime_OptimizeFunctionOnNextCall) { |
| 8592 | HandleScope scope(isolate); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame^] | 8593 | RUNTIME_ASSERT(args.length() == 1 || args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8594 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame^] | 8595 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8596 | if (!function->IsOptimizable()) return isolate->heap()->undefined_value(); |
| 8597 | function->MarkForLazyRecompilation(); |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame^] | 8598 | |
| 8599 | Code* unoptimized = function->shared()->code(); |
| 8600 | if (args.length() == 2 && |
| 8601 | unoptimized->kind() == Code::FUNCTION) { |
| 8602 | CONVERT_ARG_HANDLE_CHECKED(String, type, 1); |
| 8603 | CHECK(type->IsEqualTo(CStrVector("osr"))); |
| 8604 | isolate->runtime_profiler()->AttemptOnStackReplacement(*function); |
| 8605 | unoptimized->set_allow_osr_at_loop_nesting_level( |
| 8606 | Code::kMaxLoopNestingMarker); |
| 8607 | } |
| 8608 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8609 | return isolate->heap()->undefined_value(); |
| 8610 | } |
| 8611 | |
| 8612 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8613 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationStatus) { |
| 8614 | HandleScope scope(isolate); |
| 8615 | ASSERT(args.length() == 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8616 | // The least significant bit (after untagging) indicates whether the |
| 8617 | // function is currently optimized, regardless of reason. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8618 | if (!V8::UseCrankshaft()) { |
| 8619 | return Smi::FromInt(4); // 4 == "never". |
| 8620 | } |
| 8621 | if (FLAG_always_opt) { |
| 8622 | return Smi::FromInt(3); // 3 == "always". |
| 8623 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8624 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8625 | return function->IsOptimized() ? Smi::FromInt(1) // 1 == "yes". |
| 8626 | : Smi::FromInt(2); // 2 == "no". |
| 8627 | } |
| 8628 | |
| 8629 | |
| 8630 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationCount) { |
| 8631 | HandleScope scope(isolate); |
| 8632 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8633 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 8634 | return Smi::FromInt(function->shared()->opt_count()); |
| 8635 | } |
| 8636 | |
| 8637 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8638 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileForOnStackReplacement) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8639 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8640 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8641 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8642 | |
| 8643 | // We're not prepared to handle a function with arguments object. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8644 | ASSERT(!function->shared()->uses_arguments()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8645 | |
| 8646 | // We have hit a back edge in an unoptimized frame for a function that was |
| 8647 | // selected for on-stack replacement. Find the unoptimized code object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8648 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8649 | // Keep track of whether we've succeeded in optimizing. |
| 8650 | bool succeeded = unoptimized->optimizable(); |
| 8651 | if (succeeded) { |
| 8652 | // If we are trying to do OSR when there are already optimized |
| 8653 | // activations of the function, it means (a) the function is directly or |
| 8654 | // indirectly recursive and (b) an optimized invocation has been |
| 8655 | // deoptimized so that we are currently in an unoptimized activation. |
| 8656 | // Check for optimized activations of this function. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8657 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8658 | while (succeeded && !it.done()) { |
| 8659 | JavaScriptFrame* frame = it.frame(); |
| 8660 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 8661 | it.Advance(); |
| 8662 | } |
| 8663 | } |
| 8664 | |
| 8665 | int ast_id = AstNode::kNoNumber; |
| 8666 | if (succeeded) { |
| 8667 | // The top JS function is this one, the PC is somewhere in the |
| 8668 | // unoptimized code. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8669 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8670 | JavaScriptFrame* frame = it.frame(); |
| 8671 | ASSERT(frame->function() == *function); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8672 | ASSERT(frame->LookupCode() == *unoptimized); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8673 | ASSERT(unoptimized->contains(frame->pc())); |
| 8674 | |
| 8675 | // Use linear search of the unoptimized code's stack check table to find |
| 8676 | // the AST id matching the PC. |
| 8677 | Address start = unoptimized->instruction_start(); |
| 8678 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8679 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8680 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 8681 | table_cursor += kIntSize; |
| 8682 | for (unsigned i = 0; i < table_length; ++i) { |
| 8683 | // Table entries are (AST id, pc offset) pairs. |
| 8684 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 8685 | if (pc_offset == target_pc_offset) { |
| 8686 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 8687 | break; |
| 8688 | } |
| 8689 | table_cursor += 2 * kIntSize; |
| 8690 | } |
| 8691 | ASSERT(ast_id != AstNode::kNoNumber); |
| 8692 | if (FLAG_trace_osr) { |
| 8693 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 8694 | function->PrintName(); |
| 8695 | PrintF("]\n"); |
| 8696 | } |
| 8697 | |
| 8698 | // Try to compile the optimized code. A true return value from |
| 8699 | // CompileOptimized means that compilation succeeded, not necessarily |
| 8700 | // that optimization succeeded. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 8701 | if (JSFunction::CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8702 | function->IsOptimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8703 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 8704 | function->code()->deoptimization_data()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8705 | if (data->OsrPcOffset()->value() >= 0) { |
| 8706 | if (FLAG_trace_osr) { |
| 8707 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8708 | data->OsrPcOffset()->value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8709 | } |
| 8710 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 8711 | } else { |
| 8712 | // We may never generate the desired OSR entry if we emit an |
| 8713 | // early deoptimize. |
| 8714 | succeeded = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8715 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8716 | } else { |
| 8717 | succeeded = false; |
| 8718 | } |
| 8719 | } |
| 8720 | |
| 8721 | // Revert to the original stack checks in the original unoptimized code. |
| 8722 | if (FLAG_trace_osr) { |
| 8723 | PrintF("[restoring original stack checks in "); |
| 8724 | function->PrintName(); |
| 8725 | PrintF("]\n"); |
| 8726 | } |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 8727 | Handle<Code> check_code; |
| 8728 | #ifdef V8_TARGET_ARCH_IA32 |
| 8729 | if (FLAG_count_based_interrupts) { |
| 8730 | InterruptStub interrupt_stub; |
| 8731 | check_code = interrupt_stub.GetCode(); |
| 8732 | } else // NOLINT |
| 8733 | #endif |
| 8734 | { // NOLINT |
| 8735 | StackCheckStub check_stub; |
| 8736 | check_code = check_stub.GetCode(); |
| 8737 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 8738 | Handle<Code> replacement_code = isolate->builtins()->OnStackReplacement(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8739 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 8740 | *check_code, |
| 8741 | *replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8742 | |
| 8743 | // Allow OSR only at nesting level zero again. |
| 8744 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 8745 | |
| 8746 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 8747 | // smi. This tells the builtin that we need to translate the unoptimized |
| 8748 | // frame to an optimized one. |
| 8749 | if (succeeded) { |
| 8750 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 8751 | return Smi::FromInt(ast_id); |
| 8752 | } else { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8753 | if (function->IsMarkedForLazyRecompilation()) { |
| 8754 | function->ReplaceCode(function->shared()->code()); |
| 8755 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8756 | return Smi::FromInt(-1); |
| 8757 | } |
| 8758 | } |
| 8759 | |
| 8760 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 8761 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckIsBootstrapping) { |
| 8762 | RUNTIME_ASSERT(isolate->bootstrapper()->IsActive()); |
| 8763 | return isolate->heap()->undefined_value(); |
| 8764 | } |
| 8765 | |
| 8766 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8767 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Call) { |
| 8768 | HandleScope scope(isolate); |
| 8769 | ASSERT(args.length() >= 2); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8770 | int argc = args.length() - 2; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8771 | CONVERT_ARG_CHECKED(JSReceiver, fun, argc + 1); |
| 8772 | Object* receiver = args[0]; |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 8773 | |
| 8774 | // If there are too many arguments, allocate argv via malloc. |
| 8775 | const int argv_small_size = 10; |
| 8776 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8777 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8778 | Handle<Object>* argv = argv_small_buffer; |
| 8779 | if (argc > argv_small_size) { |
| 8780 | argv = new Handle<Object>[argc]; |
| 8781 | if (argv == NULL) return isolate->StackOverflow(); |
| 8782 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8783 | } |
| 8784 | |
| 8785 | for (int i = 0; i < argc; ++i) { |
| 8786 | MaybeObject* maybe = args[1 + i]; |
| 8787 | Object* object; |
| 8788 | if (!maybe->To<Object>(&object)) return maybe; |
| 8789 | argv[i] = Handle<Object>(object); |
| 8790 | } |
| 8791 | |
| 8792 | bool threw; |
| 8793 | Handle<JSReceiver> hfun(fun); |
| 8794 | Handle<Object> hreceiver(receiver); |
| 8795 | Handle<Object> result = |
| 8796 | Execution::Call(hfun, hreceiver, argc, argv, &threw, true); |
| 8797 | |
| 8798 | if (threw) return Failure::Exception(); |
| 8799 | return *result; |
| 8800 | } |
| 8801 | |
| 8802 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8803 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Apply) { |
| 8804 | HandleScope scope(isolate); |
| 8805 | ASSERT(args.length() == 5); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8806 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, fun, 0); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8807 | Handle<Object> receiver = args.at<Object>(1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8808 | CONVERT_ARG_HANDLE_CHECKED(JSObject, arguments, 2); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8809 | CONVERT_SMI_ARG_CHECKED(offset, 3); |
| 8810 | CONVERT_SMI_ARG_CHECKED(argc, 4); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8811 | ASSERT(offset >= 0); |
| 8812 | ASSERT(argc >= 0); |
| 8813 | |
| 8814 | // If there are too many arguments, allocate argv via malloc. |
| 8815 | const int argv_small_size = 10; |
| 8816 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 8817 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 8818 | Handle<Object>* argv = argv_small_buffer; |
| 8819 | if (argc > argv_small_size) { |
| 8820 | argv = new Handle<Object>[argc]; |
| 8821 | if (argv == NULL) return isolate->StackOverflow(); |
| 8822 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 8823 | } |
| 8824 | |
| 8825 | for (int i = 0; i < argc; ++i) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8826 | argv[i] = Object::GetElement(arguments, offset + i); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8827 | } |
| 8828 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8829 | bool threw; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 8830 | Handle<Object> result = |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 8831 | Execution::Call(fun, receiver, argc, argv, &threw, true); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 8832 | |
| 8833 | if (threw) return Failure::Exception(); |
| 8834 | return *result; |
| 8835 | } |
| 8836 | |
| 8837 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8838 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8839 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8840 | ASSERT(args.length() == 1); |
| 8841 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8842 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 8843 | } |
| 8844 | |
| 8845 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8846 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructorDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8847 | HandleScope scope(isolate); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 8848 | ASSERT(args.length() == 1); |
| 8849 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8850 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 8851 | } |
| 8852 | |
| 8853 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8854 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewFunctionContext) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8855 | NoHandleAllocation ha; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8856 | ASSERT(args.length() == 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8857 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8858 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8859 | int length = function->shared()->scope_info()->ContextLength(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8860 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8861 | { MaybeObject* maybe_result = |
| 8862 | isolate->heap()->AllocateFunctionContext(length, function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8863 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8864 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8865 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8866 | isolate->set_context(Context::cast(result)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8867 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8868 | return result; // non-failure |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8869 | } |
| 8870 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8871 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8872 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushWithContext) { |
| 8873 | NoHandleAllocation ha; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8874 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8875 | JSObject* extension_object; |
| 8876 | if (args[0]->IsJSObject()) { |
| 8877 | extension_object = JSObject::cast(args[0]); |
| 8878 | } else { |
| 8879 | // Convert the object to a proper JavaScript object. |
| 8880 | MaybeObject* maybe_js_object = args[0]->ToObject(); |
| 8881 | if (!maybe_js_object->To(&extension_object)) { |
| 8882 | if (Failure::cast(maybe_js_object)->IsInternalError()) { |
| 8883 | HandleScope scope(isolate); |
| 8884 | Handle<Object> handle = args.at<Object>(0); |
| 8885 | Handle<Object> result = |
| 8886 | isolate->factory()->NewTypeError("with_expression", |
| 8887 | HandleVector(&handle, 1)); |
| 8888 | return isolate->Throw(*result); |
| 8889 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8890 | return maybe_js_object; |
| 8891 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8892 | } |
| 8893 | } |
| 8894 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8895 | JSFunction* function; |
| 8896 | if (args[1]->IsSmi()) { |
| 8897 | // A smi sentinel indicates a context nested inside global code rather |
| 8898 | // than some function. There is a canonical empty function that can be |
| 8899 | // gotten from the global context. |
| 8900 | function = isolate->context()->global_context()->closure(); |
| 8901 | } else { |
| 8902 | function = JSFunction::cast(args[1]); |
| 8903 | } |
| 8904 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8905 | Context* context; |
| 8906 | MaybeObject* maybe_context = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8907 | isolate->heap()->AllocateWithContext(function, |
| 8908 | isolate->context(), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8909 | extension_object); |
| 8910 | if (!maybe_context->To(&context)) return maybe_context; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8911 | isolate->set_context(context); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8912 | return context; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8913 | } |
| 8914 | |
| 8915 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8916 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushCatchContext) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8917 | NoHandleAllocation ha; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8918 | ASSERT(args.length() == 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8919 | String* name = String::cast(args[0]); |
| 8920 | Object* thrown_object = args[1]; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8921 | JSFunction* function; |
| 8922 | if (args[2]->IsSmi()) { |
| 8923 | // A smi sentinel indicates a context nested inside global code rather |
| 8924 | // than some function. There is a canonical empty function that can be |
| 8925 | // gotten from the global context. |
| 8926 | function = isolate->context()->global_context()->closure(); |
| 8927 | } else { |
| 8928 | function = JSFunction::cast(args[2]); |
| 8929 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8930 | Context* context; |
| 8931 | MaybeObject* maybe_context = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8932 | isolate->heap()->AllocateCatchContext(function, |
| 8933 | isolate->context(), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8934 | name, |
| 8935 | thrown_object); |
| 8936 | if (!maybe_context->To(&context)) return maybe_context; |
| 8937 | isolate->set_context(context); |
| 8938 | return context; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8939 | } |
| 8940 | |
| 8941 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 8942 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushBlockContext) { |
| 8943 | NoHandleAllocation ha; |
| 8944 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 8945 | ScopeInfo* scope_info = ScopeInfo::cast(args[0]); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 8946 | JSFunction* function; |
| 8947 | if (args[1]->IsSmi()) { |
| 8948 | // A smi sentinel indicates a context nested inside global code rather |
| 8949 | // than some function. There is a canonical empty function that can be |
| 8950 | // gotten from the global context. |
| 8951 | function = isolate->context()->global_context()->closure(); |
| 8952 | } else { |
| 8953 | function = JSFunction::cast(args[1]); |
| 8954 | } |
| 8955 | Context* context; |
| 8956 | MaybeObject* maybe_context = |
| 8957 | isolate->heap()->AllocateBlockContext(function, |
| 8958 | isolate->context(), |
| 8959 | scope_info); |
| 8960 | if (!maybe_context->To(&context)) return maybe_context; |
| 8961 | isolate->set_context(context); |
| 8962 | return context; |
| 8963 | } |
| 8964 | |
| 8965 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8966 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8967 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8968 | ASSERT(args.length() == 2); |
| 8969 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 8970 | CONVERT_ARG_HANDLE_CHECKED(Context, context, 0); |
| 8971 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8972 | |
| 8973 | int index; |
| 8974 | PropertyAttributes attributes; |
| 8975 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 8976 | BindingFlags binding_flags; |
| 8977 | Handle<Object> holder = context->Lookup(name, |
| 8978 | flags, |
| 8979 | &index, |
| 8980 | &attributes, |
| 8981 | &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8982 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8983 | // If the slot was not found the result is true. |
| 8984 | if (holder.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8985 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8986 | } |
| 8987 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8988 | // If the slot was found in a context, it should be DONT_DELETE. |
| 8989 | if (holder->IsContext()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8990 | return isolate->heap()->false_value(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8991 | } |
| 8992 | |
| 8993 | // The slot was found in a JSObject, either a context extension object, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8994 | // the global object, or the subject of a with. Try to delete it |
| 8995 | // (respecting DONT_DELETE). |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8996 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 8997 | return object->DeleteProperty(*name, JSReceiver::NORMAL_DELETION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8998 | } |
| 8999 | |
| 9000 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 9001 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 9002 | // How this is achieved is calling convention-dependent. |
| 9003 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 9004 | // variants where a 64-bit value is returned in two 32-bit registers |
| 9005 | // (edx:eax on ia32, r1:r0 on ARM). |
| 9006 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 9007 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 9008 | // 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] | 9009 | #ifdef V8_HOST_ARCH_64_BIT |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9010 | struct ObjectPair { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9011 | MaybeObject* x; |
| 9012 | MaybeObject* y; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9013 | }; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 9014 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9015 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9016 | ObjectPair result = {x, y}; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 9017 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 9018 | // In Win64 they are assigned to a hidden first argument. |
| 9019 | return result; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9020 | } |
| 9021 | #else |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9022 | typedef uint64_t ObjectPair; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9023 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9024 | return reinterpret_cast<uint32_t>(x) | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9025 | (reinterpret_cast<ObjectPair>(y) << 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9026 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9027 | #endif |
| 9028 | |
| 9029 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9030 | static inline MaybeObject* Unhole(Heap* heap, |
| 9031 | MaybeObject* x, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9032 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9033 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 9034 | USE(attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9035 | return x->IsTheHole() ? heap->undefined_value() : x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9036 | } |
| 9037 | |
| 9038 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9039 | static Object* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 9040 | JSObject* holder) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9041 | ASSERT(!holder->IsGlobalObject()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9042 | Context* top = isolate->context(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9043 | // Get the context extension function. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9044 | JSFunction* context_extension_function = |
| 9045 | top->global_context()->context_extension_function(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9046 | // If the holder isn't a context extension object, we just return it |
| 9047 | // as the receiver. This allows arguments objects to be used as |
| 9048 | // receivers, but only if they are put in the context scope chain |
| 9049 | // explicitly via a with-statement. |
| 9050 | Object* constructor = holder->map()->constructor(); |
| 9051 | if (constructor != context_extension_function) return holder; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9052 | // Fall back to using the global object as the implicit receiver if |
| 9053 | // the property turns out to be a local variable allocated in a |
| 9054 | // context extension object - introduced via eval. Implicit global |
| 9055 | // receivers are indicated with the hole value. |
| 9056 | return isolate->heap()->the_hole_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9057 | } |
| 9058 | |
| 9059 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9060 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 9061 | Isolate* isolate, |
| 9062 | bool throw_error) { |
| 9063 | HandleScope scope(isolate); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 9064 | ASSERT_EQ(2, args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9065 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9066 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9067 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9068 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9069 | Handle<Context> context = args.at<Context>(0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9070 | Handle<String> name = args.at<String>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9071 | |
| 9072 | int index; |
| 9073 | PropertyAttributes attributes; |
| 9074 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 9075 | BindingFlags binding_flags; |
| 9076 | Handle<Object> holder = context->Lookup(name, |
| 9077 | flags, |
| 9078 | &index, |
| 9079 | &attributes, |
| 9080 | &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9081 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9082 | // If the index is non-negative, the slot has been found in a context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9083 | if (index >= 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9084 | ASSERT(holder->IsContext()); |
| 9085 | // If the "property" we were looking for is a local variable, the |
| 9086 | // receiver is the global object; see ECMA-262, 3rd., 10.1.6 and 10.2.3. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9087 | // |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9088 | // Use the hole as the receiver to signal that the receiver is implicit |
| 9089 | // and that the global receiver should be used (as distinguished from an |
| 9090 | // explicit receiver that happens to be a global object). |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9091 | Handle<Object> receiver = isolate->factory()->the_hole_value(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9092 | Object* value = Context::cast(*holder)->get(index); |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 9093 | // Check for uninitialized bindings. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9094 | switch (binding_flags) { |
| 9095 | case MUTABLE_CHECK_INITIALIZED: |
| 9096 | case IMMUTABLE_CHECK_INITIALIZED_HARMONY: |
| 9097 | if (value->IsTheHole()) { |
| 9098 | Handle<Object> reference_error = |
| 9099 | isolate->factory()->NewReferenceError("not_defined", |
| 9100 | HandleVector(&name, 1)); |
| 9101 | return MakePair(isolate->Throw(*reference_error), NULL); |
| 9102 | } |
| 9103 | // FALLTHROUGH |
| 9104 | case MUTABLE_IS_INITIALIZED: |
| 9105 | case IMMUTABLE_IS_INITIALIZED: |
| 9106 | case IMMUTABLE_IS_INITIALIZED_HARMONY: |
| 9107 | ASSERT(!value->IsTheHole()); |
| 9108 | return MakePair(value, *receiver); |
| 9109 | case IMMUTABLE_CHECK_INITIALIZED: |
| 9110 | return MakePair(Unhole(isolate->heap(), value, attributes), *receiver); |
| 9111 | case MISSING_BINDING: |
| 9112 | UNREACHABLE(); |
| 9113 | return MakePair(NULL, NULL); |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 9114 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9115 | } |
| 9116 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9117 | // Otherwise, if the slot was found the holder is a context extension |
| 9118 | // object, subject of a with, or a global object. We read the named |
| 9119 | // property from it. |
| 9120 | if (!holder.is_null()) { |
| 9121 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 9122 | ASSERT(object->HasProperty(*name)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 9123 | // GetProperty below can cause GC. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9124 | Handle<Object> receiver_handle(object->IsGlobalObject() |
| 9125 | ? GlobalObject::cast(*object)->global_receiver() |
| 9126 | : ComputeReceiverForNonGlobal(isolate, *object)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 9127 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9128 | // No need to unhole the value here. This is taken care of by the |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9129 | // GetProperty function. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9130 | MaybeObject* value = object->GetProperty(*name); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 9131 | return MakePair(value, *receiver_handle); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9132 | } |
| 9133 | |
| 9134 | if (throw_error) { |
| 9135 | // The property doesn't exist - throw exception. |
| 9136 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9137 | isolate->factory()->NewReferenceError("not_defined", |
| 9138 | HandleVector(&name, 1)); |
| 9139 | return MakePair(isolate->Throw(*reference_error), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9140 | } else { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9141 | // The property doesn't exist - return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9142 | return MakePair(isolate->heap()->undefined_value(), |
| 9143 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9144 | } |
| 9145 | } |
| 9146 | |
| 9147 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9148 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9149 | return LoadContextSlotHelper(args, isolate, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9150 | } |
| 9151 | |
| 9152 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9153 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlotNoReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9154 | return LoadContextSlotHelper(args, isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9155 | } |
| 9156 | |
| 9157 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9158 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9159 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9160 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9161 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9162 | Handle<Object> value(args[0], isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9163 | CONVERT_ARG_HANDLE_CHECKED(Context, context, 1); |
| 9164 | CONVERT_ARG_HANDLE_CHECKED(String, name, 2); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9165 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 3); |
| 9166 | StrictModeFlag strict_mode = (language_mode == CLASSIC_MODE) |
| 9167 | ? kNonStrictMode : kStrictMode; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9168 | |
| 9169 | int index; |
| 9170 | PropertyAttributes attributes; |
| 9171 | ContextLookupFlags flags = FOLLOW_CHAINS; |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 9172 | BindingFlags binding_flags; |
| 9173 | Handle<Object> holder = context->Lookup(name, |
| 9174 | flags, |
| 9175 | &index, |
| 9176 | &attributes, |
| 9177 | &binding_flags); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9178 | |
| 9179 | if (index >= 0) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9180 | // The property was found in a context slot. |
| 9181 | Handle<Context> context = Handle<Context>::cast(holder); |
| 9182 | if (binding_flags == MUTABLE_CHECK_INITIALIZED && |
| 9183 | context->get(index)->IsTheHole()) { |
| 9184 | Handle<Object> error = |
| 9185 | isolate->factory()->NewReferenceError("not_defined", |
| 9186 | HandleVector(&name, 1)); |
| 9187 | return isolate->Throw(*error); |
| 9188 | } |
| 9189 | // Ignore if read_only variable. |
| 9190 | if ((attributes & READ_ONLY) == 0) { |
| 9191 | // Context is a fixed array and set cannot fail. |
| 9192 | context->set(index, *value); |
| 9193 | } else if (strict_mode == kStrictMode) { |
| 9194 | // Setting read only property in strict mode. |
| 9195 | Handle<Object> error = |
| 9196 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 9197 | HandleVector(&name, 1)); |
| 9198 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9199 | } |
| 9200 | return *value; |
| 9201 | } |
| 9202 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9203 | // Slow case: The property is not in a context slot. It is either in a |
| 9204 | // context extension object, a property of the subject of a with, or a |
| 9205 | // property of the global object. |
| 9206 | Handle<JSObject> object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9207 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9208 | if (!holder.is_null()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9209 | // The property exists on the holder. |
| 9210 | object = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9211 | } else { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 9212 | // The property was not found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9213 | ASSERT(attributes == ABSENT); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 9214 | |
| 9215 | if (strict_mode == kStrictMode) { |
| 9216 | // Throw in strict mode (assignment to undefined variable). |
| 9217 | Handle<Object> error = |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9218 | isolate->factory()->NewReferenceError( |
| 9219 | "not_defined", HandleVector(&name, 1)); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 9220 | return isolate->Throw(*error); |
| 9221 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9222 | // In non-strict mode, the property is added to the global object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9223 | attributes = NONE; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9224 | object = Handle<JSObject>(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9225 | } |
| 9226 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9227 | // Set the property if it's not read only or doesn't yet exist. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9228 | if ((attributes & READ_ONLY) == 0 || |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9229 | (object->GetLocalPropertyAttribute(*name) == ABSENT)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9230 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9231 | isolate, |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9232 | JSReceiver::SetProperty(object, name, value, NONE, strict_mode)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9233 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9234 | // Setting read only property in strict mode. |
| 9235 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9236 | isolate->factory()->NewTypeError( |
| 9237 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 9238 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9239 | } |
| 9240 | return *value; |
| 9241 | } |
| 9242 | |
| 9243 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9244 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Throw) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9245 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9246 | ASSERT(args.length() == 1); |
| 9247 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9248 | return isolate->Throw(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9249 | } |
| 9250 | |
| 9251 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9252 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ReThrow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9253 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9254 | ASSERT(args.length() == 1); |
| 9255 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9256 | return isolate->ReThrow(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9257 | } |
| 9258 | |
| 9259 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9260 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PromoteScheduledException) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9261 | ASSERT_EQ(0, args.length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9262 | return isolate->PromoteScheduledException(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 9263 | } |
| 9264 | |
| 9265 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9266 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9267 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9268 | ASSERT(args.length() == 1); |
| 9269 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9270 | Handle<Object> name(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9271 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9272 | isolate->factory()->NewReferenceError("not_defined", |
| 9273 | HandleVector(&name, 1)); |
| 9274 | return isolate->Throw(*reference_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9275 | } |
| 9276 | |
| 9277 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9278 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 9279 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9280 | |
| 9281 | // First check if this is a real stack overflow. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9282 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 9283 | NoHandleAllocation na; |
| 9284 | return isolate->StackOverflow(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9285 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9286 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 9287 | return Execution::HandleStackGuardInterrupt(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9288 | } |
| 9289 | |
| 9290 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 9291 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Interrupt) { |
| 9292 | ASSERT(args.length() == 0); |
| 9293 | return Execution::HandleStackGuardInterrupt(); |
| 9294 | } |
| 9295 | |
| 9296 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9297 | static int StackSize() { |
| 9298 | int n = 0; |
| 9299 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 9300 | return n; |
| 9301 | } |
| 9302 | |
| 9303 | |
| 9304 | static void PrintTransition(Object* result) { |
| 9305 | // indentation |
| 9306 | { const int nmax = 80; |
| 9307 | int n = StackSize(); |
| 9308 | if (n <= nmax) |
| 9309 | PrintF("%4d:%*s", n, n, ""); |
| 9310 | else |
| 9311 | PrintF("%4d:%*s", n, nmax, "..."); |
| 9312 | } |
| 9313 | |
| 9314 | if (result == NULL) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9315 | JavaScriptFrame::PrintTop(stdout, true, false); |
| 9316 | PrintF(" {\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9317 | } else { |
| 9318 | // function result |
| 9319 | PrintF("} -> "); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 9320 | result->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9321 | PrintF("\n"); |
| 9322 | } |
| 9323 | } |
| 9324 | |
| 9325 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9326 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceEnter) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9327 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9328 | NoHandleAllocation ha; |
| 9329 | PrintTransition(NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9330 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9331 | } |
| 9332 | |
| 9333 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9334 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceExit) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9335 | NoHandleAllocation ha; |
| 9336 | PrintTransition(args[0]); |
| 9337 | return args[0]; // return TOS |
| 9338 | } |
| 9339 | |
| 9340 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9341 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9342 | NoHandleAllocation ha; |
| 9343 | ASSERT(args.length() == 1); |
| 9344 | |
| 9345 | #ifdef DEBUG |
| 9346 | if (args[0]->IsString()) { |
| 9347 | // If we have a string, assume it's a code "marker" |
| 9348 | // and print some interesting cpu debugging info. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9349 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9350 | JavaScriptFrame* frame = it.frame(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9351 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 9352 | frame->fp(), frame->sp(), frame->caller_sp()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9353 | } else { |
| 9354 | PrintF("DebugPrint: "); |
| 9355 | } |
| 9356 | args[0]->Print(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9357 | if (args[0]->IsHeapObject()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9358 | PrintF("\n"); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 9359 | HeapObject::cast(args[0])->map()->Print(); |
| 9360 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9361 | #else |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9362 | // ShortPrint is available in release mode. Print is not. |
| 9363 | args[0]->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9364 | #endif |
| 9365 | PrintF("\n"); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9366 | Flush(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9367 | |
| 9368 | return args[0]; // return TOS |
| 9369 | } |
| 9370 | |
| 9371 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9372 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugTrace) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9373 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9374 | NoHandleAllocation ha; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9375 | isolate->PrintStack(); |
| 9376 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9377 | } |
| 9378 | |
| 9379 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9380 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateCurrentTime) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9381 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9382 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9383 | |
| 9384 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 9385 | // the number in a Date object representing a particular instant in |
| 9386 | // time is milliseconds. Therefore, we floor the result of getting |
| 9387 | // the OS time. |
| 9388 | double millis = floor(OS::TimeCurrentMillis()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9389 | return isolate->heap()->NumberFromDouble(millis); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9390 | } |
| 9391 | |
| 9392 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9393 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateParseString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9394 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9395 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9396 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9397 | CONVERT_ARG_HANDLE_CHECKED(String, str, 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9398 | FlattenString(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9399 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9400 | CONVERT_ARG_HANDLE_CHECKED(JSArray, output, 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9401 | |
| 9402 | MaybeObject* maybe_result_array = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 9403 | output->EnsureCanContainHeapObjectElements(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9404 | if (maybe_result_array->IsFailure()) return maybe_result_array; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9405 | RUNTIME_ASSERT(output->HasFastElements()); |
| 9406 | |
| 9407 | AssertNoAllocation no_allocation; |
| 9408 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9409 | FixedArray* output_array = FixedArray::cast(output->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9410 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 9411 | bool result; |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9412 | String::FlatContent str_content = str->GetFlatContent(); |
| 9413 | if (str_content.IsAscii()) { |
| 9414 | result = DateParser::Parse(str_content.ToAsciiVector(), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 9415 | output_array, |
| 9416 | isolate->unicode_cache()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9417 | } else { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 9418 | ASSERT(str_content.IsTwoByte()); |
| 9419 | result = DateParser::Parse(str_content.ToUC16Vector(), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 9420 | output_array, |
| 9421 | isolate->unicode_cache()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 9422 | } |
| 9423 | |
| 9424 | if (result) { |
| 9425 | return *output; |
| 9426 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9427 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9428 | } |
| 9429 | } |
| 9430 | |
| 9431 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9432 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimezone) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9433 | NoHandleAllocation ha; |
| 9434 | ASSERT(args.length() == 1); |
| 9435 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9436 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 9437 | const char* zone = OS::LocalTimezone(x); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9438 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9439 | } |
| 9440 | |
| 9441 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9442 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimeOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9443 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9444 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9445 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9446 | return isolate->heap()->NumberFromDouble(OS::LocalTimeOffset()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9447 | } |
| 9448 | |
| 9449 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9450 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateDaylightSavingsOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9451 | NoHandleAllocation ha; |
| 9452 | ASSERT(args.length() == 1); |
| 9453 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 9454 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9455 | return isolate->heap()->NumberFromDouble(OS::DaylightSavingsOffset(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9456 | } |
| 9457 | |
| 9458 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9459 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalReceiver) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9460 | ASSERT(args.length() == 1); |
| 9461 | Object* global = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9462 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 9463 | return JSGlobalObject::cast(global)->global_receiver(); |
| 9464 | } |
| 9465 | |
| 9466 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9467 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9468 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9469 | ASSERT_EQ(1, args.length()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9470 | CONVERT_ARG_HANDLE_CHECKED(String, source, 0); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9471 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9472 | source = Handle<String>(source->TryFlattenGetString()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 9473 | // Optimized fast case where we only have ASCII characters. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 9474 | Handle<Object> result; |
| 9475 | if (source->IsSeqAsciiString()) { |
| 9476 | result = JsonParser<true>::Parse(source); |
| 9477 | } else { |
| 9478 | result = JsonParser<false>::Parse(source); |
| 9479 | } |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9480 | if (result.is_null()) { |
| 9481 | // Syntax error or stack overflow in scanner. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9482 | ASSERT(isolate->has_pending_exception()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9483 | return Failure::Exception(); |
| 9484 | } |
| 9485 | return *result; |
| 9486 | } |
| 9487 | |
| 9488 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9489 | bool CodeGenerationFromStringsAllowed(Isolate* isolate, |
| 9490 | Handle<Context> context) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9491 | ASSERT(context->allow_code_gen_from_strings()->IsFalse()); |
| 9492 | // Check with callback if set. |
| 9493 | AllowCodeGenerationFromStringsCallback callback = |
| 9494 | isolate->allow_code_gen_callback(); |
| 9495 | if (callback == NULL) { |
| 9496 | // No callback set and code generation disallowed. |
| 9497 | return false; |
| 9498 | } else { |
| 9499 | // Callback set. Let it decide if code generation is allowed. |
| 9500 | VMState state(isolate, EXTERNAL); |
| 9501 | return callback(v8::Utils::ToLocal(context)); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9502 | } |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9503 | } |
| 9504 | |
| 9505 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9506 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9507 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 9508 | ASSERT_EQ(1, args.length()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9509 | CONVERT_ARG_HANDLE_CHECKED(String, source, 0); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9510 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9511 | // Extract global context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9512 | Handle<Context> context(isolate->context()->global_context()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9513 | |
| 9514 | // Check if global context allows code generation from |
| 9515 | // strings. Throw an exception if it doesn't. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9516 | if (context->allow_code_gen_from_strings()->IsFalse() && |
| 9517 | !CodeGenerationFromStringsAllowed(isolate, context)) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9518 | return isolate->Throw(*isolate->factory()->NewError( |
| 9519 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9520 | } |
| 9521 | |
| 9522 | // Compile source string in the global context. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9523 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9524 | source, context, true, CLASSIC_MODE, RelocInfo::kNoPosition); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 9525 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9526 | Handle<JSFunction> fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9527 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 9528 | context, |
| 9529 | NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9530 | return *fun; |
| 9531 | } |
| 9532 | |
| 9533 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9534 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 9535 | Handle<String> source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9536 | Handle<Object> receiver, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9537 | LanguageMode language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9538 | int scope_position) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9539 | Handle<Context> context = Handle<Context>(isolate->context()); |
| 9540 | Handle<Context> global_context = Handle<Context>(context->global_context()); |
| 9541 | |
| 9542 | // Check if global context allows code generation from |
| 9543 | // strings. Throw an exception if it doesn't. |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9544 | if (global_context->allow_code_gen_from_strings()->IsFalse() && |
| 9545 | !CodeGenerationFromStringsAllowed(isolate, global_context)) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9546 | isolate->Throw(*isolate->factory()->NewError( |
| 9547 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 9548 | return MakePair(Failure::Exception(), NULL); |
| 9549 | } |
| 9550 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9551 | // Deal with a normal eval call with a string argument. Compile it |
| 9552 | // and return the compiled function bound in the local context. |
| 9553 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 9554 | source, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9555 | Handle<Context>(isolate->context()), |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9556 | context->IsGlobalContext(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9557 | language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9558 | scope_position); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9559 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9560 | Handle<JSFunction> compiled = |
| 9561 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 9562 | shared, context, NOT_TENURED); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 9563 | return MakePair(*compiled, *receiver); |
| 9564 | } |
| 9565 | |
| 9566 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9567 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEval) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9568 | ASSERT(args.length() == 5); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9569 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9570 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9571 | Handle<Object> callee = args.at<Object>(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9572 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 9573 | // If "eval" didn't refer to the original GlobalEval, it's not a |
| 9574 | // direct call to eval. |
| 9575 | // (And even if it is, but the first argument isn't a string, just let |
| 9576 | // execution default to an indirect call to eval, which will also return |
| 9577 | // the first argument without doing anything). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9578 | if (*callee != isolate->global_context()->global_eval_fun() || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9579 | !args[1]->IsString()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 9580 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9583 | CONVERT_LANGUAGE_MODE_ARG(language_mode, 3); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9584 | ASSERT(args[4]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9585 | return CompileGlobalEval(isolate, |
| 9586 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 9587 | args.at<Object>(2), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9588 | language_mode, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 9589 | args.smi_at(4)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9590 | } |
| 9591 | |
| 9592 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9593 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNewFunctionAttributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9594 | // This utility adjusts the property attributes for newly created Function |
| 9595 | // object ("new Function(...)") by changing the map. |
| 9596 | // All it does is changing the prototype property to enumerable |
| 9597 | // as specified in ECMA262, 15.3.5.2. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9598 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9599 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9600 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9601 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 9602 | Handle<Map> map = func->shared()->is_classic_mode() |
| 9603 | ? isolate->function_instance_map() |
| 9604 | : isolate->strict_mode_function_instance_map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9605 | |
| 9606 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 9607 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 9608 | func->set_map(*map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9609 | return *func; |
| 9610 | } |
| 9611 | |
| 9612 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9613 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateInNewSpace) { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9614 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 9615 | // Use as fallback for allocation in generated code when NewSpace |
| 9616 | // is full. |
| 9617 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9618 | CONVERT_ARG_HANDLE_CHECKED(Smi, size_smi, 0); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9619 | int size = size_smi->value(); |
| 9620 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 9621 | RUNTIME_ASSERT(size > 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9622 | Heap* heap = isolate->heap(); |
| 9623 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9624 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9625 | Object* allocation; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9626 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9627 | if (maybe_allocation->ToObject(&allocation)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9628 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9629 | } |
| 9630 | return maybe_allocation; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9631 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 9632 | } |
| 9633 | |
| 9634 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 9635 | // 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] | 9636 | // array. Returns true if the element was pushed on the stack and |
| 9637 | // false otherwise. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9638 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushIfAbsent) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9639 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 9640 | CONVERT_ARG_CHECKED(JSArray, array, 0); |
| 9641 | CONVERT_ARG_CHECKED(JSObject, element, 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9642 | RUNTIME_ASSERT(array->HasFastElements() || array->HasFastSmiOnlyElements()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9643 | int length = Smi::cast(array->length())->value(); |
| 9644 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 9645 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9646 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9647 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9648 | Object* obj; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9649 | // Strict not needed. Used for cycle detection in Array join implementation. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 9650 | { MaybeObject* maybe_obj = |
| 9651 | array->SetFastElement(length, element, kNonStrictMode, true); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9652 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9653 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9654 | return isolate->heap()->true_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 9655 | } |
| 9656 | |
| 9657 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9658 | /** |
| 9659 | * A simple visitor visits every element of Array's. |
| 9660 | * The backend storage can be a fixed array for fast elements case, |
| 9661 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 9662 | * of FixedArray, the class can be used by both fast and slow cases. |
| 9663 | * The second parameter of the constructor, fast_elements, specifies |
| 9664 | * whether the storage is a FixedArray or Dictionary. |
| 9665 | * |
| 9666 | * An index limit is used to deal with the situation that a result array |
| 9667 | * length overflows 32-bit non-negative integer. |
| 9668 | */ |
| 9669 | class ArrayConcatVisitor { |
| 9670 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9671 | ArrayConcatVisitor(Isolate* isolate, |
| 9672 | Handle<FixedArray> storage, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9673 | bool fast_elements) : |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9674 | isolate_(isolate), |
| 9675 | storage_(Handle<FixedArray>::cast( |
| 9676 | isolate->global_handles()->Create(*storage))), |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9677 | index_offset_(0u), |
| 9678 | fast_elements_(fast_elements) { } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9679 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9680 | ~ArrayConcatVisitor() { |
| 9681 | clear_storage(); |
| 9682 | } |
| 9683 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9684 | void visit(uint32_t i, Handle<Object> elm) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9685 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9686 | uint32_t index = index_offset_ + i; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9687 | |
| 9688 | if (fast_elements_) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9689 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 9690 | storage_->set(index, *elm); |
| 9691 | return; |
| 9692 | } |
| 9693 | // Our initial estimate of length was foiled, possibly by |
| 9694 | // getters on the arrays increasing the length of later arrays |
| 9695 | // during iteration. |
| 9696 | // This shouldn't happen in anything but pathological cases. |
| 9697 | SetDictionaryMode(index); |
| 9698 | // Fall-through to dictionary mode. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9699 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9700 | ASSERT(!fast_elements_); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9701 | Handle<SeededNumberDictionary> dict( |
| 9702 | SeededNumberDictionary::cast(*storage_)); |
| 9703 | Handle<SeededNumberDictionary> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9704 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9705 | if (!result.is_identical_to(dict)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9706 | // Dictionary needed to grow. |
| 9707 | clear_storage(); |
| 9708 | set_storage(*result); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9709 | } |
| 9710 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9711 | |
| 9712 | void increase_index_offset(uint32_t delta) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9713 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 9714 | index_offset_ = JSObject::kMaxElementCount; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9715 | } else { |
| 9716 | index_offset_ += delta; |
| 9717 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9718 | } |
| 9719 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9720 | Handle<JSArray> ToArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9721 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9722 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9723 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9724 | Handle<Map> map; |
| 9725 | if (fast_elements_) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9726 | map = isolate_->factory()->GetElementsTransitionMap(array, |
| 9727 | FAST_ELEMENTS); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9728 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9729 | map = isolate_->factory()->GetElementsTransitionMap(array, |
| 9730 | DICTIONARY_ELEMENTS); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9731 | } |
| 9732 | array->set_map(*map); |
| 9733 | array->set_length(*length); |
| 9734 | array->set_elements(*storage_); |
| 9735 | return array; |
| 9736 | } |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 9737 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9738 | private: |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9739 | // Convert storage to dictionary mode. |
| 9740 | void SetDictionaryMode(uint32_t index) { |
| 9741 | ASSERT(fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9742 | Handle<FixedArray> current_storage(*storage_); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9743 | Handle<SeededNumberDictionary> slow_storage( |
| 9744 | isolate_->factory()->NewSeededNumberDictionary( |
| 9745 | current_storage->length())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9746 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 9747 | for (uint32_t i = 0; i < current_length; i++) { |
| 9748 | HandleScope loop_scope; |
| 9749 | Handle<Object> element(current_storage->get(i)); |
| 9750 | if (!element->IsTheHole()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9751 | Handle<SeededNumberDictionary> new_storage = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9752 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9753 | if (!new_storage.is_identical_to(slow_storage)) { |
| 9754 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 9755 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9756 | } |
| 9757 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9758 | clear_storage(); |
| 9759 | set_storage(*slow_storage); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9760 | fast_elements_ = false; |
| 9761 | } |
| 9762 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9763 | inline void clear_storage() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9764 | isolate_->global_handles()->Destroy( |
| 9765 | Handle<Object>::cast(storage_).location()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9766 | } |
| 9767 | |
| 9768 | inline void set_storage(FixedArray* storage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9769 | storage_ = Handle<FixedArray>::cast( |
| 9770 | isolate_->global_handles()->Create(storage)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9771 | } |
| 9772 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9773 | Isolate* isolate_; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9774 | Handle<FixedArray> storage_; // Always a global handle. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9775 | // Index after last seen index. Always less than or equal to |
| 9776 | // JSObject::kMaxElementCount. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9777 | uint32_t index_offset_; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9778 | bool fast_elements_; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9779 | }; |
| 9780 | |
| 9781 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9782 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 9783 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 9784 | int element_count = 0; |
| 9785 | switch (array->GetElementsKind()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 9786 | case FAST_SMI_ONLY_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9787 | case FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9788 | // Fast elements can't have lengths that are not representable by |
| 9789 | // a 32-bit signed integer. |
| 9790 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 9791 | int fast_length = static_cast<int>(length); |
| 9792 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 9793 | for (int i = 0; i < fast_length; i++) { |
| 9794 | if (!elements->get(i)->IsTheHole()) element_count++; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9795 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 9796 | break; |
| 9797 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 9798 | case FAST_DOUBLE_ELEMENTS: |
| 9799 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9800 | UNREACHABLE(); |
| 9801 | break; |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9802 | case DICTIONARY_ELEMENTS: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9803 | Handle<SeededNumberDictionary> dictionary( |
| 9804 | SeededNumberDictionary::cast(array->elements())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9805 | int capacity = dictionary->Capacity(); |
| 9806 | for (int i = 0; i < capacity; i++) { |
| 9807 | Handle<Object> key(dictionary->KeyAt(i)); |
| 9808 | if (dictionary->IsKey(*key)) { |
| 9809 | element_count++; |
| 9810 | } |
| 9811 | } |
| 9812 | break; |
| 9813 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 9814 | case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 9815 | case EXTERNAL_BYTE_ELEMENTS: |
| 9816 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 9817 | case EXTERNAL_SHORT_ELEMENTS: |
| 9818 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 9819 | case EXTERNAL_INT_ELEMENTS: |
| 9820 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 9821 | case EXTERNAL_FLOAT_ELEMENTS: |
| 9822 | case EXTERNAL_DOUBLE_ELEMENTS: |
| 9823 | case EXTERNAL_PIXEL_ELEMENTS: |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9824 | // External arrays are always dense. |
| 9825 | return length; |
| 9826 | } |
| 9827 | // As an estimate, we assume that the prototype doesn't contain any |
| 9828 | // inherited elements. |
| 9829 | return element_count; |
| 9830 | } |
| 9831 | |
| 9832 | |
| 9833 | |
| 9834 | template<class ExternalArrayClass, class ElementType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9835 | static void IterateExternalArrayElements(Isolate* isolate, |
| 9836 | Handle<JSObject> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9837 | bool elements_are_ints, |
| 9838 | bool elements_are_guaranteed_smis, |
| 9839 | ArrayConcatVisitor* visitor) { |
| 9840 | Handle<ExternalArrayClass> array( |
| 9841 | ExternalArrayClass::cast(receiver->elements())); |
| 9842 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 9843 | |
| 9844 | ASSERT(visitor != NULL); |
| 9845 | if (elements_are_ints) { |
| 9846 | if (elements_are_guaranteed_smis) { |
| 9847 | for (uint32_t j = 0; j < len; j++) { |
| 9848 | HandleScope loop_scope; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9849 | 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] | 9850 | visitor->visit(j, e); |
| 9851 | } |
| 9852 | } else { |
| 9853 | for (uint32_t j = 0; j < len; j++) { |
| 9854 | HandleScope loop_scope; |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9855 | int64_t val = static_cast<int64_t>(array->get_scalar(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9856 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 9857 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 9858 | visitor->visit(j, e); |
| 9859 | } else { |
| 9860 | Handle<Object> e = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9861 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9862 | visitor->visit(j, e); |
| 9863 | } |
| 9864 | } |
| 9865 | } |
| 9866 | } else { |
| 9867 | for (uint32_t j = 0; j < len; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9868 | HandleScope loop_scope(isolate); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 9869 | Handle<Object> e = isolate->factory()->NewNumber(array->get_scalar(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9870 | visitor->visit(j, e); |
| 9871 | } |
| 9872 | } |
| 9873 | } |
| 9874 | |
| 9875 | |
| 9876 | // Used for sorting indices in a List<uint32_t>. |
| 9877 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 9878 | uint32_t a = *ap; |
| 9879 | uint32_t b = *bp; |
| 9880 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 9881 | } |
| 9882 | |
| 9883 | |
| 9884 | static void CollectElementIndices(Handle<JSObject> object, |
| 9885 | uint32_t range, |
| 9886 | List<uint32_t>* indices) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9887 | ElementsKind kind = object->GetElementsKind(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9888 | switch (kind) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 9889 | case FAST_SMI_ONLY_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9890 | case FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9891 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 9892 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 9893 | if (range < length) length = range; |
| 9894 | for (uint32_t i = 0; i < length; i++) { |
| 9895 | if (!elements->get(i)->IsTheHole()) { |
| 9896 | indices->Add(i); |
| 9897 | } |
| 9898 | } |
| 9899 | break; |
| 9900 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 9901 | case FAST_DOUBLE_ELEMENTS: { |
| 9902 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 9903 | UNREACHABLE(); |
| 9904 | break; |
| 9905 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9906 | case DICTIONARY_ELEMENTS: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 9907 | Handle<SeededNumberDictionary> dict( |
| 9908 | SeededNumberDictionary::cast(object->elements())); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9909 | uint32_t capacity = dict->Capacity(); |
| 9910 | for (uint32_t j = 0; j < capacity; j++) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9911 | HandleScope loop_scope; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9912 | Handle<Object> k(dict->KeyAt(j)); |
| 9913 | if (dict->IsKey(*k)) { |
| 9914 | ASSERT(k->IsNumber()); |
| 9915 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 9916 | if (index < range) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9917 | indices->Add(index); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9918 | } |
| 9919 | } |
| 9920 | } |
| 9921 | break; |
| 9922 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9923 | default: { |
| 9924 | int dense_elements_length; |
| 9925 | switch (kind) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9926 | case EXTERNAL_PIXEL_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9927 | dense_elements_length = |
| 9928 | ExternalPixelArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9929 | break; |
| 9930 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9931 | case EXTERNAL_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9932 | dense_elements_length = |
| 9933 | ExternalByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9934 | break; |
| 9935 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9936 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9937 | dense_elements_length = |
| 9938 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9939 | break; |
| 9940 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9941 | case EXTERNAL_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9942 | dense_elements_length = |
| 9943 | ExternalShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9944 | break; |
| 9945 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9946 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9947 | dense_elements_length = |
| 9948 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9949 | break; |
| 9950 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9951 | case EXTERNAL_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9952 | dense_elements_length = |
| 9953 | ExternalIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9954 | break; |
| 9955 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9956 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9957 | dense_elements_length = |
| 9958 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9959 | break; |
| 9960 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9961 | case EXTERNAL_FLOAT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9962 | dense_elements_length = |
| 9963 | ExternalFloatArray::cast(object->elements())->length(); |
| 9964 | break; |
| 9965 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 9966 | case EXTERNAL_DOUBLE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9967 | dense_elements_length = |
| 9968 | ExternalDoubleArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9969 | break; |
| 9970 | } |
| 9971 | default: |
| 9972 | UNREACHABLE(); |
| 9973 | dense_elements_length = 0; |
| 9974 | break; |
| 9975 | } |
| 9976 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 9977 | if (range <= length) { |
| 9978 | length = range; |
| 9979 | // We will add all indices, so we might as well clear it first |
| 9980 | // and avoid duplicates. |
| 9981 | indices->Clear(); |
| 9982 | } |
| 9983 | for (uint32_t i = 0; i < length; i++) { |
| 9984 | indices->Add(i); |
| 9985 | } |
| 9986 | if (length == range) return; // All indices accounted for already. |
| 9987 | break; |
| 9988 | } |
| 9989 | } |
| 9990 | |
| 9991 | Handle<Object> prototype(object->GetPrototype()); |
| 9992 | if (prototype->IsJSObject()) { |
| 9993 | // The prototype will usually have no inherited element indices, |
| 9994 | // but we have to check. |
| 9995 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 9996 | } |
| 9997 | } |
| 9998 | |
| 9999 | |
| 10000 | /** |
| 10001 | * A helper function that visits elements of a JSArray in numerical |
| 10002 | * order. |
| 10003 | * |
| 10004 | * The visitor argument called for each existing element in the array |
| 10005 | * with the element index and the element's value. |
| 10006 | * Afterwards it increments the base-index of the visitor by the array |
| 10007 | * length. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 10008 | * Returns false if any access threw an exception, otherwise true. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10009 | */ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10010 | static bool IterateElements(Isolate* isolate, |
| 10011 | Handle<JSArray> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10012 | ArrayConcatVisitor* visitor) { |
| 10013 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 10014 | switch (receiver->GetElementsKind()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10015 | case FAST_SMI_ONLY_ELEMENTS: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10016 | case FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10017 | // Run through the elements FixedArray and use HasElement and GetElement |
| 10018 | // to check the prototype for missing elements. |
| 10019 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 10020 | int fast_length = static_cast<int>(length); |
| 10021 | ASSERT(fast_length <= elements->length()); |
| 10022 | for (int j = 0; j < fast_length; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10023 | HandleScope loop_scope(isolate); |
| 10024 | Handle<Object> element_value(elements->get(j), isolate); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10025 | if (!element_value->IsTheHole()) { |
| 10026 | visitor->visit(j, element_value); |
| 10027 | } else if (receiver->HasElement(j)) { |
| 10028 | // Call GetElement on receiver, not its prototype, or getters won't |
| 10029 | // have the correct receiver. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10030 | element_value = Object::GetElement(receiver, j); |
| 10031 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, element_value, false); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10032 | visitor->visit(j, element_value); |
| 10033 | } |
| 10034 | } |
| 10035 | break; |
| 10036 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10037 | case FAST_DOUBLE_ELEMENTS: { |
| 10038 | // TODO(1810): Decide if it's worthwhile to implement this. |
| 10039 | UNREACHABLE(); |
| 10040 | break; |
| 10041 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10042 | case DICTIONARY_ELEMENTS: { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10043 | Handle<SeededNumberDictionary> dict(receiver->element_dictionary()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10044 | List<uint32_t> indices(dict->Capacity() / 2); |
| 10045 | // Collect all indices in the object and the prototypes less |
| 10046 | // than length. This might introduce duplicates in the indices list. |
| 10047 | CollectElementIndices(receiver, length, &indices); |
| 10048 | indices.Sort(&compareUInt32); |
| 10049 | int j = 0; |
| 10050 | int n = indices.length(); |
| 10051 | while (j < n) { |
| 10052 | HandleScope loop_scope; |
| 10053 | uint32_t index = indices[j]; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10054 | Handle<Object> element = Object::GetElement(receiver, index); |
| 10055 | RETURN_IF_EMPTY_HANDLE_VALUE(isolate, element, false); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10056 | visitor->visit(index, element); |
| 10057 | // Skip to next different index (i.e., omit duplicates). |
| 10058 | do { |
| 10059 | j++; |
| 10060 | } while (j < n && indices[j] == index); |
| 10061 | } |
| 10062 | break; |
| 10063 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10064 | case EXTERNAL_PIXEL_ELEMENTS: { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 10065 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 10066 | receiver->elements())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10067 | for (uint32_t j = 0; j < length; j++) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10068 | Handle<Smi> e(Smi::FromInt(pixels->get_scalar(j))); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10069 | visitor->visit(j, e); |
| 10070 | } |
| 10071 | break; |
| 10072 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10073 | case EXTERNAL_BYTE_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10074 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10075 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10076 | break; |
| 10077 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10078 | case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10079 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10080 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10081 | break; |
| 10082 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10083 | case EXTERNAL_SHORT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10084 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10085 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10086 | break; |
| 10087 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10088 | case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10089 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10090 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10091 | break; |
| 10092 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10093 | case EXTERNAL_INT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10094 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10095 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10096 | break; |
| 10097 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10098 | case EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10099 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10100 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10101 | break; |
| 10102 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10103 | case EXTERNAL_FLOAT_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10104 | IterateExternalArrayElements<ExternalFloatArray, float>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10105 | isolate, receiver, false, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10106 | break; |
| 10107 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10108 | case EXTERNAL_DOUBLE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 10109 | IterateExternalArrayElements<ExternalDoubleArray, double>( |
| 10110 | isolate, receiver, false, false, visitor); |
| 10111 | break; |
| 10112 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 10113 | default: |
| 10114 | UNREACHABLE(); |
| 10115 | break; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10116 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10117 | visitor->increase_index_offset(length); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 10118 | return true; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10119 | } |
| 10120 | |
| 10121 | |
| 10122 | /** |
| 10123 | * Array::concat implementation. |
| 10124 | * See ECMAScript 262, 15.4.4.4. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10125 | * 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] | 10126 | * following the ECMAScript 5 specification. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10127 | */ |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10128 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayConcat) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10129 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10130 | HandleScope handle_scope(isolate); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10131 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10132 | CONVERT_ARG_HANDLE_CHECKED(JSArray, arguments, 0); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10133 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 10134 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 10135 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10136 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10137 | // Pass 1: estimate the length and number of elements of the result. |
| 10138 | // The actual length can be larger if any of the arguments have getters |
| 10139 | // that mutate other arguments (but will otherwise be precise). |
| 10140 | // 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] | 10141 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10142 | uint32_t estimate_result_length = 0; |
| 10143 | uint32_t estimate_nof_elements = 0; |
| 10144 | { |
| 10145 | for (int i = 0; i < argument_count; i++) { |
| 10146 | HandleScope loop_scope; |
| 10147 | Handle<Object> obj(elements->get(i)); |
| 10148 | uint32_t length_estimate; |
| 10149 | uint32_t element_estimate; |
| 10150 | if (obj->IsJSArray()) { |
| 10151 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10152 | // TODO(1810): Find out if it's worthwhile to properly support |
| 10153 | // arbitrary ElementsKinds. For now, pessimistically transition to |
| 10154 | // FAST_ELEMENTS. |
| 10155 | if (array->HasFastDoubleElements()) { |
| 10156 | array = Handle<JSArray>::cast( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10157 | JSObject::TransitionElementsKind(array, FAST_ELEMENTS)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10158 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10159 | length_estimate = |
| 10160 | static_cast<uint32_t>(array->length()->Number()); |
| 10161 | element_estimate = |
| 10162 | EstimateElementCount(array); |
| 10163 | } else { |
| 10164 | length_estimate = 1; |
| 10165 | element_estimate = 1; |
| 10166 | } |
| 10167 | // Avoid overflows by capping at kMaxElementCount. |
| 10168 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 10169 | length_estimate) { |
| 10170 | estimate_result_length = JSObject::kMaxElementCount; |
| 10171 | } else { |
| 10172 | estimate_result_length += length_estimate; |
| 10173 | } |
| 10174 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 10175 | element_estimate) { |
| 10176 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 10177 | } else { |
| 10178 | estimate_nof_elements += element_estimate; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10179 | } |
| 10180 | } |
| 10181 | } |
| 10182 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10183 | // If estimated number of elements is more than half of length, a |
| 10184 | // fixed array (fast case) is more time and space-efficient than a |
| 10185 | // dictionary. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10186 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10187 | |
| 10188 | Handle<FixedArray> storage; |
| 10189 | if (fast_case) { |
| 10190 | // The backing storage array must have non-existing elements to |
| 10191 | // preserve holes across concat operations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10192 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 10193 | estimate_result_length); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10194 | } else { |
| 10195 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 10196 | uint32_t at_least_space_for = estimate_nof_elements + |
| 10197 | (estimate_nof_elements >> 2); |
| 10198 | storage = Handle<FixedArray>::cast( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10199 | isolate->factory()->NewSeededNumberDictionary(at_least_space_for)); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10200 | } |
| 10201 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10202 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10203 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10204 | for (int i = 0; i < argument_count; i++) { |
| 10205 | Handle<Object> obj(elements->get(i)); |
| 10206 | if (obj->IsJSArray()) { |
| 10207 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10208 | if (!IterateElements(isolate, array, &visitor)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 10209 | return Failure::Exception(); |
| 10210 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10211 | } else { |
| 10212 | visitor.visit(0, obj); |
| 10213 | visitor.increase_index_offset(1); |
| 10214 | } |
| 10215 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10216 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 10217 | return *visitor.ToArray(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 10218 | } |
| 10219 | |
| 10220 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10221 | // This will not allocate (flatten the string), but it may run |
| 10222 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10224 | NoHandleAllocation ha; |
| 10225 | ASSERT(args.length() == 1); |
| 10226 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10227 | CONVERT_ARG_CHECKED(String, string, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10228 | StringInputBuffer buffer(string); |
| 10229 | while (buffer.has_more()) { |
| 10230 | uint16_t character = buffer.GetNext(); |
| 10231 | PrintF("%c", character); |
| 10232 | } |
| 10233 | return string; |
| 10234 | } |
| 10235 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 10236 | // Moves all own elements of an object, that are below a limit, to positions |
| 10237 | // starting at zero. All undefined values are placed after non-undefined values, |
| 10238 | // and are followed by non-existing element. Does not change the length |
| 10239 | // property. |
| 10240 | // Returns the number of non-undefined elements collected. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10241 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RemoveArrayHoles) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 10242 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10243 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 10244 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 10245 | return object->PrepareElementsForSort(limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10246 | } |
| 10247 | |
| 10248 | |
| 10249 | // 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] | 10250 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MoveArrayContents) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10251 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10252 | CONVERT_ARG_CHECKED(JSArray, from, 0); |
| 10253 | CONVERT_ARG_CHECKED(JSArray, to, 1); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 10254 | FixedArrayBase* new_elements = from->elements(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10255 | MaybeObject* maybe_new_map; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10256 | ElementsKind elements_kind; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10257 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 10258 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10259 | elements_kind = FAST_ELEMENTS; |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10260 | } else if (new_elements->map() == |
| 10261 | isolate->heap()->fixed_double_array_map()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10262 | elements_kind = FAST_DOUBLE_ELEMENTS; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10263 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10264 | elements_kind = DICTIONARY_ELEMENTS; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10265 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 10266 | maybe_new_map = to->GetElementsTransitionMap(isolate, elements_kind); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10267 | Object* new_map; |
| 10268 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 10269 | to->set_map(Map::cast(new_map)); |
| 10270 | to->set_elements(new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10271 | to->set_length(from->length()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10272 | Object* obj; |
| 10273 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 10274 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 10275 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10276 | from->set_length(Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10277 | return to; |
| 10278 | } |
| 10279 | |
| 10280 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10281 | // How many elements does this object/array have? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10282 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EstimateNumberOfElements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10283 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10284 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10285 | HeapObject* elements = object->elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10286 | if (elements->IsDictionary()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10287 | int result = SeededNumberDictionary::cast(elements)->NumberOfElements(); |
| 10288 | return Smi::FromInt(result); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10289 | } else if (object->IsJSArray()) { |
| 10290 | return JSArray::cast(object)->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10291 | } else { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 10292 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10293 | } |
| 10294 | } |
| 10295 | |
| 10296 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10297 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SwapElements) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10298 | HandleScope handle_scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10299 | |
| 10300 | ASSERT_EQ(3, args.length()); |
| 10301 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10302 | CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10303 | Handle<Object> key1 = args.at<Object>(1); |
| 10304 | Handle<Object> key2 = args.at<Object>(2); |
| 10305 | |
| 10306 | uint32_t index1, index2; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10307 | if (!key1->ToArrayIndex(&index1) |
| 10308 | || !key2->ToArrayIndex(&index2)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10309 | return isolate->ThrowIllegalOperation(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10310 | } |
| 10311 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10312 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10313 | Handle<Object> tmp1 = Object::GetElement(jsobject, index1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10314 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10315 | Handle<Object> tmp2 = Object::GetElement(jsobject, index2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10316 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10317 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10318 | RETURN_IF_EMPTY_HANDLE( |
| 10319 | isolate, JSObject::SetElement(jsobject, index1, tmp2, kStrictMode)); |
| 10320 | RETURN_IF_EMPTY_HANDLE( |
| 10321 | isolate, JSObject::SetElement(jsobject, index2, tmp1, kStrictMode)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 10322 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10323 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 10324 | } |
| 10325 | |
| 10326 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10327 | // 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] | 10328 | // might have elements. Can either return keys (positive integers) or |
| 10329 | // intervals (pair of a negative integer (-start-1) followed by a |
| 10330 | // positive (length)) or undefined values. |
| 10331 | // 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] | 10332 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArrayKeys) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10333 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10334 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10335 | CONVERT_ARG_HANDLE_CHECKED(JSObject, array, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10336 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10337 | if (array->elements()->IsDictionary()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10338 | // Create an array and get all the keys into it, then remove all the |
| 10339 | // keys that are not integers in the range 0 to length-1. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10340 | bool threw = false; |
| 10341 | Handle<FixedArray> keys = |
| 10342 | GetKeysInFixedArrayFor(array, INCLUDE_PROTOS, &threw); |
| 10343 | if (threw) return Failure::Exception(); |
| 10344 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10345 | int keys_length = keys->length(); |
| 10346 | for (int i = 0; i < keys_length; i++) { |
| 10347 | Object* key = keys->get(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10348 | uint32_t index = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10349 | if (!key->ToArrayIndex(&index) || index >= length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10350 | // Zap invalid keys. |
| 10351 | keys->set_undefined(i); |
| 10352 | } |
| 10353 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10354 | return *isolate->factory()->NewJSArrayWithElements(keys); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10355 | } else { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10356 | ASSERT(array->HasFastElements() || |
| 10357 | array->HasFastSmiOnlyElements() || |
| 10358 | array->HasFastDoubleElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10359 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10360 | // -1 means start of array. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10361 | single_interval->set(0, Smi::FromInt(-1)); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10362 | FixedArrayBase* elements = FixedArrayBase::cast(array->elements()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10363 | uint32_t actual_length = |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 10364 | static_cast<uint32_t>(elements->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 10365 | uint32_t min_length = actual_length < length ? actual_length : length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10366 | Handle<Object> length_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10367 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10368 | single_interval->set(1, *length_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10369 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10370 | } |
| 10371 | } |
| 10372 | |
| 10373 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10374 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LookupAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10375 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10376 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 10377 | CONVERT_ARG_CHECKED(String, name, 1); |
| 10378 | CONVERT_SMI_ARG_CHECKED(flag, 2); |
| 10379 | return obj->LookupAccessor(name, flag == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10380 | } |
| 10381 | |
| 10382 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 10383 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10384 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugBreak) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 10385 | ASSERT(args.length() == 0); |
| 10386 | return Execution::DebugBreakHelper(); |
| 10387 | } |
| 10388 | |
| 10389 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10390 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 10391 | static Smi* WrapFrameId(StackFrame::Id id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 10392 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10393 | return Smi::FromInt(id >> 2); |
| 10394 | } |
| 10395 | |
| 10396 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10397 | static StackFrame::Id UnwrapFrameId(int wrapped) { |
| 10398 | return static_cast<StackFrame::Id>(wrapped << 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10399 | } |
| 10400 | |
| 10401 | |
| 10402 | // Adds a JavaScript function as a debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 10403 | // args[0]: debug event listener function to set or null or undefined for |
| 10404 | // clearing the event listener function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10405 | // args[1]: object supplied during callback |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10406 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDebugEventListener) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10407 | ASSERT(args.length() == 2); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 10408 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 10409 | args[0]->IsUndefined() || |
| 10410 | args[0]->IsNull()); |
| 10411 | Handle<Object> callback = args.at<Object>(0); |
| 10412 | Handle<Object> data = args.at<Object>(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10413 | isolate->debugger()->SetEventListener(callback, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10414 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10415 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10416 | } |
| 10417 | |
| 10418 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10419 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Break) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10420 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10421 | isolate->stack_guard()->DebugBreak(); |
| 10422 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10423 | } |
| 10424 | |
| 10425 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10426 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 10427 | Object* receiver, |
| 10428 | String* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10429 | LookupResult* result, |
| 10430 | bool* caught_exception) { |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10431 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10432 | switch (result->type()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 10433 | case NORMAL: |
| 10434 | value = result->holder()->GetNormalizedProperty(result); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10435 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10436 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10437 | } |
| 10438 | return value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10439 | case FIELD: |
| 10440 | value = |
| 10441 | JSObject::cast( |
| 10442 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 10443 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10444 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10445 | } |
| 10446 | return value; |
| 10447 | case CONSTANT_FUNCTION: |
| 10448 | return result->GetConstantFunction(); |
| 10449 | case CALLBACKS: { |
| 10450 | Object* structure = result->GetCallbackObject(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 10451 | if (structure->IsForeign() || structure->IsAccessorInfo()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 10452 | MaybeObject* maybe_value = result->holder()->GetPropertyWithCallback( |
| 10453 | receiver, structure, name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10454 | if (!maybe_value->ToObject(&value)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10455 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10456 | ASSERT(maybe_value->IsException()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10457 | maybe_value = heap->isolate()->pending_exception(); |
| 10458 | heap->isolate()->clear_pending_exception(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10459 | if (caught_exception != NULL) { |
| 10460 | *caught_exception = true; |
| 10461 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10462 | return maybe_value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10463 | } |
| 10464 | return value; |
| 10465 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10466 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 10467 | } |
| 10468 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10469 | case INTERCEPTOR: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10470 | case MAP_TRANSITION: |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 10471 | case ELEMENTS_TRANSITION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10472 | case CONSTANT_TRANSITION: |
| 10473 | case NULL_DESCRIPTOR: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10474 | return heap->undefined_value(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 10475 | case HANDLER: |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10476 | UNREACHABLE(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 10477 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10478 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 10479 | UNREACHABLE(); // keep the compiler happy |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10480 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10481 | } |
| 10482 | |
| 10483 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10484 | // Get debugger related details for an object property. |
| 10485 | // args[0]: object holding property |
| 10486 | // args[1]: name of the property |
| 10487 | // |
| 10488 | // The array returned contains the following information: |
| 10489 | // 0: Property value |
| 10490 | // 1: Property details |
| 10491 | // 2: Property value is exception |
| 10492 | // 3: Getter function if defined |
| 10493 | // 4: Setter function if defined |
| 10494 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 10495 | // defined through __defineGetter__ and/or __defineSetter__. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10496 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPropertyDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10497 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10498 | |
| 10499 | ASSERT(args.length() == 2); |
| 10500 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10501 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 10502 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10503 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 10504 | // Make sure to set the current context to the context before the debugger was |
| 10505 | // entered (if the debugger is entered). The reason for switching context here |
| 10506 | // is that for some property lookups (accessors and interceptors) callbacks |
| 10507 | // into the embedding application can occour, and the embedding application |
| 10508 | // could have the assumption that its own global context is the current |
| 10509 | // context and not some internal debugger context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10510 | SaveContext save(isolate); |
| 10511 | if (isolate->debug()->InDebugger()) { |
| 10512 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 10513 | } |
| 10514 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10515 | // Skip the global proxy as it has no properties and always delegates to the |
| 10516 | // real global object. |
| 10517 | if (obj->IsJSGlobalProxy()) { |
| 10518 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 10519 | } |
| 10520 | |
| 10521 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10522 | // Check if the name is trivially convertible to an index and get the element |
| 10523 | // if so. |
| 10524 | uint32_t index; |
| 10525 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10526 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10527 | Object* element_or_char; |
| 10528 | { MaybeObject* maybe_element_or_char = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10529 | Runtime::GetElementOrCharAt(isolate, obj, index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10530 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 10531 | return maybe_element_or_char; |
| 10532 | } |
| 10533 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10534 | details->set(0, element_or_char); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10535 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10536 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10537 | } |
| 10538 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10539 | // Find the number of objects making up this. |
| 10540 | int length = LocalPrototypeChainLength(*obj); |
| 10541 | |
| 10542 | // Try local lookup on each of the objects. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10543 | Handle<JSObject> jsproto = obj; |
| 10544 | for (int i = 0; i < length; i++) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10545 | LookupResult result(isolate); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10546 | jsproto->LocalLookup(*name, &result); |
| 10547 | if (result.IsProperty()) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10548 | // LookupResult is not GC safe as it holds raw object pointers. |
| 10549 | // GC can happen later in this code so put the required fields into |
| 10550 | // local variables using handles when required for later use. |
| 10551 | PropertyType result_type = result.type(); |
| 10552 | Handle<Object> result_callback_obj; |
| 10553 | if (result_type == CALLBACKS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10554 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 10555 | isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10556 | } |
| 10557 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 10558 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 10559 | // to be copied to handles before this. |
| 10560 | bool caught_exception = false; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10561 | Object* raw_value; |
| 10562 | { MaybeObject* maybe_raw_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10563 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 10564 | &result, &caught_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10565 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 10566 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10567 | Handle<Object> value(raw_value, isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10568 | |
| 10569 | // If the callback object is a fixed array then it contains JavaScript |
| 10570 | // getter and/or setter. |
| 10571 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10572 | result_callback_obj->IsAccessorPair(); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10573 | Handle<FixedArray> details = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10574 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10575 | details->set(0, *value); |
| 10576 | details->set(1, property_details); |
| 10577 | if (hasJavaScriptAccessors) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 10578 | details->set(2, isolate->heap()->ToBoolean(caught_exception)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10579 | details->set(3, AccessorPair::cast(*result_callback_obj)->getter()); |
| 10580 | details->set(4, AccessorPair::cast(*result_callback_obj)->setter()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10581 | } |
| 10582 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10583 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 10584 | } |
| 10585 | if (i < length - 1) { |
| 10586 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 10587 | } |
| 10588 | } |
| 10589 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10590 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10591 | } |
| 10592 | |
| 10593 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10594 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetProperty) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10595 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10596 | |
| 10597 | ASSERT(args.length() == 2); |
| 10598 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10599 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
| 10600 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10601 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 10602 | LookupResult result(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10603 | obj->Lookup(*name, &result); |
| 10604 | if (result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10605 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10606 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10607 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10608 | } |
| 10609 | |
| 10610 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10611 | // Return the property type calculated from the property details. |
| 10612 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10613 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyTypeFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10614 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10615 | CONVERT_PROPERTY_DETAILS_CHECKED(details, 0); |
| 10616 | return Smi::FromInt(static_cast<int>(details.type())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10617 | } |
| 10618 | |
| 10619 | |
| 10620 | // Return the property attribute calculated from the property details. |
| 10621 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10622 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyAttributesFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10623 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10624 | CONVERT_PROPERTY_DETAILS_CHECKED(details, 0); |
| 10625 | return Smi::FromInt(static_cast<int>(details.attributes())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10626 | } |
| 10627 | |
| 10628 | |
| 10629 | // Return the property insertion index calculated from the property details. |
| 10630 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10631 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyIndexFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10632 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10633 | CONVERT_PROPERTY_DETAILS_CHECKED(details, 0); |
| 10634 | return Smi::FromInt(details.index()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10635 | } |
| 10636 | |
| 10637 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10638 | // Return property value from named interceptor. |
| 10639 | // args[0]: object |
| 10640 | // args[1]: property name |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10641 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugNamedInterceptorPropertyValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10642 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10643 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10644 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10645 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10646 | CONVERT_ARG_HANDLE_CHECKED(String, name, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10647 | |
| 10648 | PropertyAttributes attributes; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10649 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10650 | } |
| 10651 | |
| 10652 | |
| 10653 | // Return element value from indexed interceptor. |
| 10654 | // args[0]: object |
| 10655 | // args[1]: index |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10656 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugIndexedInterceptorElementValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10657 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10658 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 10659 | CONVERT_ARG_HANDLE_CHECKED(JSObject, obj, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10660 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 10661 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 10662 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10663 | return obj->GetElementWithInterceptor(*obj, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10664 | } |
| 10665 | |
| 10666 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10667 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckExecutionState) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10668 | ASSERT(args.length() >= 1); |
| 10669 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10670 | // Check that the break id is valid. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10671 | if (isolate->debug()->break_id() == 0 || |
| 10672 | break_id != isolate->debug()->break_id()) { |
| 10673 | return isolate->Throw( |
| 10674 | isolate->heap()->illegal_execution_state_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10675 | } |
| 10676 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10677 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10678 | } |
| 10679 | |
| 10680 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10681 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10682 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10683 | ASSERT(args.length() == 1); |
| 10684 | |
| 10685 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10686 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10687 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 10688 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10689 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10690 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10691 | |
| 10692 | // Count all frames which are relevant to debugging stack trace. |
| 10693 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10694 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10695 | if (id == StackFrame::NO_ID) { |
| 10696 | // If there is no JavaScript stack frame count is 0. |
| 10697 | return Smi::FromInt(0); |
| 10698 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10699 | |
| 10700 | for (JavaScriptFrameIterator it(isolate, id); !it.done(); it.Advance()) { |
| 10701 | n += it.frame()->GetInlineCount(); |
| 10702 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10703 | return Smi::FromInt(n); |
| 10704 | } |
| 10705 | |
| 10706 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10707 | class FrameInspector { |
| 10708 | public: |
| 10709 | FrameInspector(JavaScriptFrame* frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10710 | int inlined_jsframe_index, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10711 | Isolate* isolate) |
| 10712 | : frame_(frame), deoptimized_frame_(NULL), isolate_(isolate) { |
| 10713 | // Calculate the deoptimized frame. |
| 10714 | if (frame->is_optimized()) { |
| 10715 | deoptimized_frame_ = Deoptimizer::DebuggerInspectableFrame( |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10716 | frame, inlined_jsframe_index, isolate); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10717 | } |
| 10718 | has_adapted_arguments_ = frame_->has_adapted_arguments(); |
| 10719 | is_optimized_ = frame_->is_optimized(); |
| 10720 | } |
| 10721 | |
| 10722 | ~FrameInspector() { |
| 10723 | // Get rid of the calculated deoptimized frame if any. |
| 10724 | if (deoptimized_frame_ != NULL) { |
| 10725 | Deoptimizer::DeleteDebuggerInspectableFrame(deoptimized_frame_, |
| 10726 | isolate_); |
| 10727 | } |
| 10728 | } |
| 10729 | |
| 10730 | int GetParametersCount() { |
| 10731 | return is_optimized_ |
| 10732 | ? deoptimized_frame_->parameters_count() |
| 10733 | : frame_->ComputeParametersCount(); |
| 10734 | } |
| 10735 | int expression_count() { return deoptimized_frame_->expression_count(); } |
| 10736 | Object* GetFunction() { |
| 10737 | return is_optimized_ |
| 10738 | ? deoptimized_frame_->GetFunction() |
| 10739 | : frame_->function(); |
| 10740 | } |
| 10741 | Object* GetParameter(int index) { |
| 10742 | return is_optimized_ |
| 10743 | ? deoptimized_frame_->GetParameter(index) |
| 10744 | : frame_->GetParameter(index); |
| 10745 | } |
| 10746 | Object* GetExpression(int index) { |
| 10747 | return is_optimized_ |
| 10748 | ? deoptimized_frame_->GetExpression(index) |
| 10749 | : frame_->GetExpression(index); |
| 10750 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 10751 | int GetSourcePosition() { |
| 10752 | return is_optimized_ |
| 10753 | ? deoptimized_frame_->GetSourcePosition() |
| 10754 | : frame_->LookupCode()->SourcePosition(frame_->pc()); |
| 10755 | } |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10756 | |
| 10757 | // To inspect all the provided arguments the frame might need to be |
| 10758 | // replaced with the arguments frame. |
| 10759 | void SetArgumentsFrame(JavaScriptFrame* frame) { |
| 10760 | ASSERT(has_adapted_arguments_); |
| 10761 | frame_ = frame; |
| 10762 | is_optimized_ = frame_->is_optimized(); |
| 10763 | ASSERT(!is_optimized_); |
| 10764 | } |
| 10765 | |
| 10766 | private: |
| 10767 | JavaScriptFrame* frame_; |
| 10768 | DeoptimizedFrameInfo* deoptimized_frame_; |
| 10769 | Isolate* isolate_; |
| 10770 | bool is_optimized_; |
| 10771 | bool has_adapted_arguments_; |
| 10772 | |
| 10773 | DISALLOW_COPY_AND_ASSIGN(FrameInspector); |
| 10774 | }; |
| 10775 | |
| 10776 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10777 | static const int kFrameDetailsFrameIdIndex = 0; |
| 10778 | static const int kFrameDetailsReceiverIndex = 1; |
| 10779 | static const int kFrameDetailsFunctionIndex = 2; |
| 10780 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 10781 | static const int kFrameDetailsLocalCountIndex = 4; |
| 10782 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 10783 | static const int kFrameDetailsConstructCallIndex = 6; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10784 | static const int kFrameDetailsAtReturnIndex = 7; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10785 | static const int kFrameDetailsFlagsIndex = 8; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10786 | static const int kFrameDetailsFirstDynamicIndex = 9; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10787 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10788 | |
| 10789 | static SaveContext* FindSavedContextForFrame(Isolate* isolate, |
| 10790 | JavaScriptFrame* frame) { |
| 10791 | SaveContext* save = isolate->save_context(); |
| 10792 | while (save != NULL && !save->IsBelowFrame(frame)) { |
| 10793 | save = save->prev(); |
| 10794 | } |
| 10795 | ASSERT(save != NULL); |
| 10796 | return save; |
| 10797 | } |
| 10798 | |
| 10799 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10800 | // Return an array with frame details |
| 10801 | // args[0]: number: break id |
| 10802 | // args[1]: number: frame index |
| 10803 | // |
| 10804 | // The array returned contains the following information: |
| 10805 | // 0: Frame id |
| 10806 | // 1: Receiver |
| 10807 | // 2: Function |
| 10808 | // 3: Argument count |
| 10809 | // 4: Local count |
| 10810 | // 5: Source position |
| 10811 | // 6: Constructor call |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10812 | // 7: Is at return |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10813 | // 8: Flags |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10814 | // Arguments name, value |
| 10815 | // Locals name, value |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10816 | // Return value if any |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10817 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10818 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10819 | ASSERT(args.length() == 2); |
| 10820 | |
| 10821 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10822 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10823 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10824 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10825 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10826 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10827 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10828 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10829 | |
| 10830 | // Find the relevant frame with the requested index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10831 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10832 | if (id == StackFrame::NO_ID) { |
| 10833 | // If there are no JavaScript stack frames return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10834 | return heap->undefined_value(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10835 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10836 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10837 | int count = 0; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10838 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10839 | for (; !it.done(); it.Advance()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10840 | if (index < count + it.frame()->GetInlineCount()) break; |
| 10841 | count += it.frame()->GetInlineCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10842 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10843 | if (it.done()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10844 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10845 | bool is_optimized = it.frame()->is_optimized(); |
| 10846 | |
| 10847 | int inlined_jsframe_index = 0; // Inlined frame index in optimized frame. |
| 10848 | if (is_optimized) { |
| 10849 | inlined_jsframe_index = |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10850 | it.frame()->GetInlineCount() - (index - count) - 1; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10851 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10852 | FrameInspector frame_inspector(it.frame(), inlined_jsframe_index, isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10853 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10854 | // Traverse the saved contexts chain to find the active context for the |
| 10855 | // selected frame. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10856 | SaveContext* save = FindSavedContextForFrame(isolate, it.frame()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10857 | |
| 10858 | // Get the frame id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10859 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10860 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 10861 | // Find source position in unoptimized code. |
| 10862 | int position = frame_inspector.GetSourcePosition(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10863 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10864 | // Check for constructor frame. Inlined frames cannot be construct calls. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10865 | bool inlined_frame = is_optimized && inlined_jsframe_index != 0; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10866 | bool constructor = !inlined_frame && it.frame()->IsConstructor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10867 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10868 | // Get scope info and read from it for local variable information. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10869 | Handle<JSFunction> function(JSFunction::cast(frame_inspector.GetFunction())); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 10870 | Handle<SharedFunctionInfo> shared(function->shared()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10871 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
| 10872 | ASSERT(*scope_info != ScopeInfo::Empty()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10873 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10874 | // Get the locals names and values into a temporary array. |
| 10875 | // |
| 10876 | // TODO(1240907): Hide compiler-introduced stack variables |
| 10877 | // (e.g. .result)? For users of the debugger, they will probably be |
| 10878 | // confusing. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10879 | Handle<FixedArray> locals = |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10880 | isolate->factory()->NewFixedArray(scope_info->LocalCount() * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10881 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10882 | // Fill in the values of the locals. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10883 | int i = 0; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10884 | for (; i < scope_info->StackLocalCount(); ++i) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10885 | // Use the value from the stack. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10886 | locals->set(i * 2, scope_info->LocalName(i)); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10887 | locals->set(i * 2 + 1, frame_inspector.GetExpression(i)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10888 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10889 | if (i < scope_info->LocalCount()) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 10890 | // Get the context containing declarations. |
| 10891 | Handle<Context> context( |
| 10892 | Context::cast(it.frame()->context())->declaration_context()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10893 | for (; i < scope_info->LocalCount(); ++i) { |
| 10894 | Handle<String> name(scope_info->LocalName(i)); |
| 10895 | VariableMode mode; |
| 10896 | InitializationFlag init_flag; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 10897 | locals->set(i * 2, *name); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10898 | locals->set(i * 2 + 1, context->get( |
| 10899 | scope_info->ContextSlotIndex(*name, &mode, &init_flag))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10900 | } |
| 10901 | } |
| 10902 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10903 | // Check whether this frame is positioned at return. If not top |
| 10904 | // frame or if the frame is optimized it cannot be at a return. |
| 10905 | bool at_return = false; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10906 | if (!is_optimized && index == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10907 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10908 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10909 | |
| 10910 | // If positioned just before return find the value to be returned and add it |
| 10911 | // to the frame information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10912 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10913 | if (at_return) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10914 | StackFrameIterator it2(isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10915 | Address internal_frame_sp = NULL; |
| 10916 | while (!it2.done()) { |
| 10917 | if (it2.frame()->is_internal()) { |
| 10918 | internal_frame_sp = it2.frame()->sp(); |
| 10919 | } else { |
| 10920 | if (it2.frame()->is_java_script()) { |
| 10921 | if (it2.frame()->id() == it.frame()->id()) { |
| 10922 | // The internal frame just before the JavaScript frame contains the |
| 10923 | // value to return on top. A debug break at return will create an |
| 10924 | // internal frame to store the return value (eax/rax/r0) before |
| 10925 | // entering the debug break exit frame. |
| 10926 | if (internal_frame_sp != NULL) { |
| 10927 | return_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10928 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 10929 | isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10930 | break; |
| 10931 | } |
| 10932 | } |
| 10933 | } |
| 10934 | |
| 10935 | // Indicate that the previous frame was not an internal frame. |
| 10936 | internal_frame_sp = NULL; |
| 10937 | } |
| 10938 | it2.Advance(); |
| 10939 | } |
| 10940 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10941 | |
| 10942 | // Now advance to the arguments adapter frame (if any). It contains all |
| 10943 | // the provided parameters whereas the function frame always have the number |
| 10944 | // of arguments matching the functions parameters. The rest of the |
| 10945 | // information (except for what is collected above) is the same. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10946 | if ((inlined_jsframe_index == 0) && it.frame()->has_adapted_arguments()) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10947 | it.AdvanceToArgumentsFrame(); |
| 10948 | frame_inspector.SetArgumentsFrame(it.frame()); |
| 10949 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10950 | |
| 10951 | // Find the number of arguments to fill. At least fill the number of |
| 10952 | // parameters for the function and fill more if more parameters are provided. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10953 | int argument_count = scope_info->ParameterCount(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10954 | if (argument_count < frame_inspector.GetParametersCount()) { |
| 10955 | argument_count = frame_inspector.GetParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10956 | } |
| 10957 | |
| 10958 | // Calculate the size of the result. |
| 10959 | int details_size = kFrameDetailsFirstDynamicIndex + |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10960 | 2 * (argument_count + scope_info->LocalCount()) + |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10961 | (at_return ? 1 : 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10962 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10963 | |
| 10964 | // Add the frame id. |
| 10965 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 10966 | |
| 10967 | // Add the function (same as in function frame). |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 10968 | details->set(kFrameDetailsFunctionIndex, frame_inspector.GetFunction()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10969 | |
| 10970 | // Add the arguments count. |
| 10971 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 10972 | |
| 10973 | // Add the locals count |
| 10974 | details->set(kFrameDetailsLocalCountIndex, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10975 | Smi::FromInt(scope_info->LocalCount())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10976 | |
| 10977 | // Add the source position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10978 | if (position != RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10979 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 10980 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10981 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10982 | } |
| 10983 | |
| 10984 | // Add the constructor information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10985 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10986 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10987 | // Add the at return information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10988 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10989 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10990 | // Add flags to indicate information on whether this frame is |
| 10991 | // bit 0: invoked in the debugger context. |
| 10992 | // bit 1: optimized frame. |
| 10993 | // bit 2: inlined in optimized frame |
| 10994 | int flags = 0; |
| 10995 | if (*save->context() == *isolate->debug()->debug_context()) { |
| 10996 | flags |= 1 << 0; |
| 10997 | } |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10998 | if (is_optimized) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10999 | flags |= 1 << 1; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11000 | flags |= inlined_jsframe_index << 2; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11001 | } |
| 11002 | details->set(kFrameDetailsFlagsIndex, Smi::FromInt(flags)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11003 | |
| 11004 | // Fill the dynamic part. |
| 11005 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 11006 | |
| 11007 | // Add arguments name and value. |
| 11008 | for (int i = 0; i < argument_count; i++) { |
| 11009 | // Name of the argument. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11010 | if (i < scope_info->ParameterCount()) { |
| 11011 | details->set(details_index++, scope_info->ParameterName(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11012 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11013 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11014 | } |
| 11015 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 11016 | // Parameter value. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11017 | if (i < frame_inspector.GetParametersCount()) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11018 | // Get the value from the stack. |
| 11019 | details->set(details_index++, frame_inspector.GetParameter(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11020 | } else { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11021 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11022 | } |
| 11023 | } |
| 11024 | |
| 11025 | // Add locals name and value from the temporary copy from the function frame. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11026 | for (int i = 0; i < scope_info->LocalCount() * 2; i++) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11027 | details->set(details_index++, locals->get(i)); |
| 11028 | } |
| 11029 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 11030 | // Add the value being returned. |
| 11031 | if (at_return) { |
| 11032 | details->set(details_index++, *return_value); |
| 11033 | } |
| 11034 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11035 | // Add the receiver (same as in function frame). |
| 11036 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 11037 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11038 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11039 | if (!receiver->IsJSObject() && |
| 11040 | shared->is_classic_mode() && |
| 11041 | !shared->native()) { |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 11042 | // If the receiver is not a JSObject and the function is not a |
| 11043 | // builtin or strict-mode we have hit an optimization where a |
| 11044 | // value object is not converted into a wrapped JS objects. To |
| 11045 | // hide this optimization from the debugger, we wrap the receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11046 | // by creating correct wrapper object based on the calling frame's |
| 11047 | // global context. |
| 11048 | it.Advance(); |
| 11049 | Handle<Context> calling_frames_global_context( |
| 11050 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11051 | receiver = |
| 11052 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11053 | } |
| 11054 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 11055 | |
| 11056 | ASSERT_EQ(details_size, details_index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11057 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11058 | } |
| 11059 | |
| 11060 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11061 | // 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] | 11062 | static bool CopyContextLocalsToScopeObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11063 | Isolate* isolate, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11064 | Handle<ScopeInfo> scope_info, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 11065 | Handle<Context> context, |
| 11066 | Handle<JSObject> scope_object) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11067 | // Fill all context locals to the context extension. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11068 | for (int i = 0; i < scope_info->ContextLocalCount(); i++) { |
| 11069 | VariableMode mode; |
| 11070 | InitializationFlag init_flag; |
| 11071 | int context_index = scope_info->ContextSlotIndex( |
| 11072 | scope_info->ContextLocalName(i), &mode, &init_flag); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11073 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11074 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11075 | isolate, |
| 11076 | SetProperty(scope_object, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11077 | Handle<String>(scope_info->ContextLocalName(i)), |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 11078 | Handle<Object>(context->get(context_index), isolate), |
| 11079 | NONE, |
| 11080 | kNonStrictMode), |
| 11081 | false); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11082 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11083 | |
| 11084 | return true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11085 | } |
| 11086 | |
| 11087 | |
| 11088 | // Create a plain JSObject which materializes the local scope for the specified |
| 11089 | // frame. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11090 | static Handle<JSObject> MaterializeLocalScopeWithFrameInspector( |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11091 | Isolate* isolate, |
| 11092 | JavaScriptFrame* frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11093 | FrameInspector* frame_inspector) { |
| 11094 | Handle<JSFunction> function(JSFunction::cast(frame_inspector->GetFunction())); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11095 | Handle<SharedFunctionInfo> shared(function->shared()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11096 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11097 | |
| 11098 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 11099 | // heap locals and extension properties of the debugged function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11100 | Handle<JSObject> local_scope = |
| 11101 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11102 | |
| 11103 | // First fill all parameters. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11104 | for (int i = 0; i < scope_info->ParameterCount(); ++i) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11105 | Handle<Object> value( |
| 11106 | i < frame_inspector->GetParametersCount() ? |
| 11107 | frame_inspector->GetParameter(i) : isolate->heap()->undefined_value()); |
| 11108 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11109 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11110 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11111 | SetProperty(local_scope, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11112 | Handle<String>(scope_info->ParameterName(i)), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11113 | value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11114 | NONE, |
| 11115 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11116 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11117 | } |
| 11118 | |
| 11119 | // Second fill all stack locals. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11120 | for (int i = 0; i < scope_info->StackLocalCount(); ++i) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11121 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11122 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11123 | SetProperty(local_scope, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11124 | Handle<String>(scope_info->StackLocalName(i)), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11125 | Handle<Object>(frame_inspector->GetExpression(i)), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11126 | NONE, |
| 11127 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11128 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11129 | } |
| 11130 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11131 | if (scope_info->HasContext()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11132 | // Third fill all context locals. |
| 11133 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 11134 | Handle<Context> function_context(frame_context->declaration_context()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11135 | if (!CopyContextLocalsToScopeObject( |
| 11136 | isolate, scope_info, function_context, local_scope)) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11137 | return Handle<JSObject>(); |
| 11138 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11139 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11140 | // Finally copy any properties from the function context extension. |
| 11141 | // These will be variables introduced by eval. |
| 11142 | if (function_context->closure() == *function) { |
| 11143 | if (function_context->has_extension() && |
| 11144 | !function_context->IsGlobalContext()) { |
| 11145 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11146 | bool threw = false; |
| 11147 | Handle<FixedArray> keys = |
| 11148 | GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS, &threw); |
| 11149 | if (threw) return Handle<JSObject>(); |
| 11150 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11151 | for (int i = 0; i < keys->length(); i++) { |
| 11152 | // Names of variables introduced by eval are strings. |
| 11153 | ASSERT(keys->get(i)->IsString()); |
| 11154 | Handle<String> key(String::cast(keys->get(i))); |
| 11155 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11156 | isolate, |
| 11157 | SetProperty(local_scope, |
| 11158 | key, |
| 11159 | GetProperty(ext, key), |
| 11160 | NONE, |
| 11161 | kNonStrictMode), |
| 11162 | Handle<JSObject>()); |
| 11163 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11164 | } |
| 11165 | } |
| 11166 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 11167 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11168 | return local_scope; |
| 11169 | } |
| 11170 | |
| 11171 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11172 | static Handle<JSObject> MaterializeLocalScope( |
| 11173 | Isolate* isolate, |
| 11174 | JavaScriptFrame* frame, |
| 11175 | int inlined_jsframe_index) { |
| 11176 | FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate); |
| 11177 | return MaterializeLocalScopeWithFrameInspector(isolate, |
| 11178 | frame, |
| 11179 | &frame_inspector); |
| 11180 | } |
| 11181 | |
| 11182 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11183 | // Create a plain JSObject which materializes the closure content for the |
| 11184 | // context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11185 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 11186 | Handle<Context> context) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11187 | ASSERT(context->IsFunctionContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11188 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11189 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11190 | Handle<ScopeInfo> scope_info(shared->scope_info()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11191 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11192 | // Allocate and initialize a JSObject with all the content of this function |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11193 | // closure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11194 | Handle<JSObject> closure_scope = |
| 11195 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11196 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11197 | // Fill all context locals to the context extension. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11198 | if (!CopyContextLocalsToScopeObject( |
| 11199 | isolate, scope_info, context, closure_scope)) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11200 | return Handle<JSObject>(); |
| 11201 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11202 | |
| 11203 | // Finally copy any properties from the function context extension. This will |
| 11204 | // be variables introduced by eval. |
| 11205 | if (context->has_extension()) { |
| 11206 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11207 | bool threw = false; |
| 11208 | Handle<FixedArray> keys = |
| 11209 | GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS, &threw); |
| 11210 | if (threw) return Handle<JSObject>(); |
| 11211 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11212 | for (int i = 0; i < keys->length(); i++) { |
| 11213 | // Names of variables introduced by eval are strings. |
| 11214 | ASSERT(keys->get(i)->IsString()); |
| 11215 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11216 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11217 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11218 | SetProperty(closure_scope, |
| 11219 | key, |
| 11220 | GetProperty(ext, key), |
| 11221 | NONE, |
| 11222 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 11223 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11224 | } |
| 11225 | } |
| 11226 | |
| 11227 | return closure_scope; |
| 11228 | } |
| 11229 | |
| 11230 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11231 | // Create a plain JSObject which materializes the scope for the specified |
| 11232 | // catch context. |
| 11233 | static Handle<JSObject> MaterializeCatchScope(Isolate* isolate, |
| 11234 | Handle<Context> context) { |
| 11235 | ASSERT(context->IsCatchContext()); |
| 11236 | Handle<String> name(String::cast(context->extension())); |
| 11237 | Handle<Object> thrown_object(context->get(Context::THROWN_OBJECT_INDEX)); |
| 11238 | Handle<JSObject> catch_scope = |
| 11239 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 11240 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 11241 | isolate, |
| 11242 | SetProperty(catch_scope, name, thrown_object, NONE, kNonStrictMode), |
| 11243 | Handle<JSObject>()); |
| 11244 | return catch_scope; |
| 11245 | } |
| 11246 | |
| 11247 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11248 | // Create a plain JSObject which materializes the block scope for the specified |
| 11249 | // block context. |
| 11250 | static Handle<JSObject> MaterializeBlockScope( |
| 11251 | Isolate* isolate, |
| 11252 | Handle<Context> context) { |
| 11253 | ASSERT(context->IsBlockContext()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11254 | Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension())); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11255 | |
| 11256 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 11257 | // heap locals and extension properties of the debugged function. |
| 11258 | Handle<JSObject> block_scope = |
| 11259 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 11260 | |
| 11261 | // Fill all context locals. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11262 | if (!CopyContextLocalsToScopeObject( |
| 11263 | isolate, scope_info, context, block_scope)) { |
| 11264 | return Handle<JSObject>(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11265 | } |
| 11266 | |
| 11267 | return block_scope; |
| 11268 | } |
| 11269 | |
| 11270 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11271 | // Create a plain JSObject which materializes the module scope for the specified |
| 11272 | // module context. |
| 11273 | static Handle<JSObject> MaterializeModuleScope( |
| 11274 | Isolate* isolate, |
| 11275 | Handle<Context> context) { |
| 11276 | ASSERT(context->IsModuleContext()); |
| 11277 | Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension())); |
| 11278 | |
| 11279 | // Allocate and initialize a JSObject with all the members of the debugged |
| 11280 | // module. |
| 11281 | Handle<JSObject> module_scope = |
| 11282 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 11283 | |
| 11284 | // Fill all context locals. |
| 11285 | if (!CopyContextLocalsToScopeObject( |
| 11286 | isolate, scope_info, context, module_scope)) { |
| 11287 | return Handle<JSObject>(); |
| 11288 | } |
| 11289 | |
| 11290 | return module_scope; |
| 11291 | } |
| 11292 | |
| 11293 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11294 | // Iterate over the actual scopes visible from a stack frame. The iteration |
| 11295 | // proceeds from the innermost visible nested scope outwards. All scopes are |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11296 | // backed by an actual context except the local scope, which is inserted |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11297 | // "artificially" in the context chain. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11298 | class ScopeIterator { |
| 11299 | public: |
| 11300 | enum ScopeType { |
| 11301 | ScopeTypeGlobal = 0, |
| 11302 | ScopeTypeLocal, |
| 11303 | ScopeTypeWith, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11304 | ScopeTypeClosure, |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11305 | ScopeTypeCatch, |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11306 | ScopeTypeBlock, |
| 11307 | ScopeTypeModule |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11308 | }; |
| 11309 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11310 | ScopeIterator(Isolate* isolate, |
| 11311 | JavaScriptFrame* frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11312 | int inlined_jsframe_index) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11313 | : isolate_(isolate), |
| 11314 | frame_(frame), |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11315 | inlined_jsframe_index_(inlined_jsframe_index), |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11316 | function_(JSFunction::cast(frame->function())), |
| 11317 | context_(Context::cast(frame->context())), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11318 | nested_scope_chain_(4) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11319 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11320 | // Catch the case when the debugger stops in an internal function. |
| 11321 | Handle<SharedFunctionInfo> shared_info(function_->shared()); |
| 11322 | Handle<ScopeInfo> scope_info(shared_info->scope_info()); |
| 11323 | if (shared_info->script() == isolate->heap()->undefined_value()) { |
| 11324 | while (context_->closure() == *function_) { |
| 11325 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11326 | } |
| 11327 | return; |
| 11328 | } |
| 11329 | |
| 11330 | // Get the debug info (create it if it does not exist). |
| 11331 | if (!isolate->debug()->EnsureDebugInfo(shared_info)) { |
| 11332 | // Return if ensuring debug info failed. |
| 11333 | return; |
| 11334 | } |
| 11335 | Handle<DebugInfo> debug_info = Debug::GetDebugInfo(shared_info); |
| 11336 | |
| 11337 | // Find the break point where execution has stopped. |
| 11338 | BreakLocationIterator break_location_iterator(debug_info, |
| 11339 | ALL_BREAK_LOCATIONS); |
| 11340 | break_location_iterator.FindBreakLocationFromAddress(frame->pc()); |
| 11341 | if (break_location_iterator.IsExit()) { |
| 11342 | // We are within the return sequence. At the momemt it is not possible to |
| 11343 | // get a source position which is consistent with the current scope chain. |
| 11344 | // Thus all nested with, catch and block contexts are skipped and we only |
| 11345 | // provide the function scope. |
| 11346 | if (scope_info->HasContext()) { |
| 11347 | context_ = Handle<Context>(context_->declaration_context(), isolate_); |
| 11348 | } else { |
| 11349 | while (context_->closure() == *function_) { |
| 11350 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11351 | } |
| 11352 | } |
| 11353 | if (scope_info->Type() != EVAL_SCOPE) nested_scope_chain_.Add(scope_info); |
| 11354 | } else { |
| 11355 | // Reparse the code and analyze the scopes. |
| 11356 | ZoneScope zone_scope(isolate, DELETE_ON_EXIT); |
| 11357 | Handle<Script> script(Script::cast(shared_info->script())); |
| 11358 | Scope* scope = NULL; |
| 11359 | |
| 11360 | // Check whether we are in global, eval or function code. |
| 11361 | Handle<ScopeInfo> scope_info(shared_info->scope_info()); |
| 11362 | if (scope_info->Type() != FUNCTION_SCOPE) { |
| 11363 | // Global or eval code. |
| 11364 | CompilationInfo info(script); |
| 11365 | if (scope_info->Type() == GLOBAL_SCOPE) { |
| 11366 | info.MarkAsGlobal(); |
| 11367 | } else { |
| 11368 | ASSERT(scope_info->Type() == EVAL_SCOPE); |
| 11369 | info.MarkAsEval(); |
| 11370 | info.SetCallingContext(Handle<Context>(function_->context())); |
| 11371 | } |
| 11372 | if (ParserApi::Parse(&info, kNoParsingFlags) && Scope::Analyze(&info)) { |
| 11373 | scope = info.function()->scope(); |
| 11374 | } |
| 11375 | } else { |
| 11376 | // Function code |
| 11377 | CompilationInfo info(shared_info); |
| 11378 | if (ParserApi::Parse(&info, kNoParsingFlags) && Scope::Analyze(&info)) { |
| 11379 | scope = info.function()->scope(); |
| 11380 | } |
| 11381 | } |
| 11382 | |
| 11383 | // Retrieve the scope chain for the current position. |
| 11384 | if (scope != NULL) { |
| 11385 | int source_position = shared_info->code()->SourcePosition(frame_->pc()); |
| 11386 | scope->GetNestedScopeChain(&nested_scope_chain_, source_position); |
| 11387 | } else { |
| 11388 | // A failed reparse indicates that the preparser has diverged from the |
| 11389 | // parser or that the preparse data given to the initial parse has been |
| 11390 | // faulty. We fail in debug mode but in release mode we only provide the |
| 11391 | // information we get from the context chain but nothing about |
| 11392 | // completely stack allocated scopes or stack allocated locals. |
| 11393 | UNREACHABLE(); |
| 11394 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11395 | } |
| 11396 | } |
| 11397 | |
| 11398 | // More scopes? |
| 11399 | bool Done() { return context_.is_null(); } |
| 11400 | |
| 11401 | // Move to the next scope. |
| 11402 | void Next() { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11403 | ScopeType scope_type = Type(); |
| 11404 | if (scope_type == ScopeTypeGlobal) { |
| 11405 | // The global scope is always the last in the chain. |
| 11406 | ASSERT(context_->IsGlobalContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11407 | context_ = Handle<Context>(); |
| 11408 | return; |
| 11409 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11410 | if (nested_scope_chain_.is_empty()) { |
| 11411 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11412 | } else { |
| 11413 | if (nested_scope_chain_.last()->HasContext()) { |
| 11414 | ASSERT(context_->previous() != NULL); |
| 11415 | context_ = Handle<Context>(context_->previous(), isolate_); |
| 11416 | } |
| 11417 | nested_scope_chain_.RemoveLast(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11418 | } |
| 11419 | } |
| 11420 | |
| 11421 | // Return the type of the current scope. |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 11422 | ScopeType Type() { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11423 | if (!nested_scope_chain_.is_empty()) { |
| 11424 | Handle<ScopeInfo> scope_info = nested_scope_chain_.last(); |
| 11425 | switch (scope_info->Type()) { |
| 11426 | case FUNCTION_SCOPE: |
| 11427 | ASSERT(context_->IsFunctionContext() || |
| 11428 | !scope_info->HasContext()); |
| 11429 | return ScopeTypeLocal; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11430 | case MODULE_SCOPE: |
| 11431 | ASSERT(context_->IsModuleContext()); |
| 11432 | return ScopeTypeModule; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11433 | case GLOBAL_SCOPE: |
| 11434 | ASSERT(context_->IsGlobalContext()); |
| 11435 | return ScopeTypeGlobal; |
| 11436 | case WITH_SCOPE: |
| 11437 | ASSERT(context_->IsWithContext()); |
| 11438 | return ScopeTypeWith; |
| 11439 | case CATCH_SCOPE: |
| 11440 | ASSERT(context_->IsCatchContext()); |
| 11441 | return ScopeTypeCatch; |
| 11442 | case BLOCK_SCOPE: |
| 11443 | ASSERT(!scope_info->HasContext() || |
| 11444 | context_->IsBlockContext()); |
| 11445 | return ScopeTypeBlock; |
| 11446 | case EVAL_SCOPE: |
| 11447 | UNREACHABLE(); |
| 11448 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11449 | } |
| 11450 | if (context_->IsGlobalContext()) { |
| 11451 | ASSERT(context_->global()->IsGlobalObject()); |
| 11452 | return ScopeTypeGlobal; |
| 11453 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11454 | if (context_->IsFunctionContext()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11455 | return ScopeTypeClosure; |
| 11456 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11457 | if (context_->IsCatchContext()) { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11458 | return ScopeTypeCatch; |
| 11459 | } |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11460 | if (context_->IsBlockContext()) { |
| 11461 | return ScopeTypeBlock; |
| 11462 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11463 | if (context_->IsModuleContext()) { |
| 11464 | return ScopeTypeModule; |
| 11465 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11466 | ASSERT(context_->IsWithContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11467 | return ScopeTypeWith; |
| 11468 | } |
| 11469 | |
| 11470 | // Return the JavaScript object with the content of the current scope. |
| 11471 | Handle<JSObject> ScopeObject() { |
| 11472 | switch (Type()) { |
| 11473 | case ScopeIterator::ScopeTypeGlobal: |
| 11474 | return Handle<JSObject>(CurrentContext()->global()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11475 | case ScopeIterator::ScopeTypeLocal: |
| 11476 | // Materialize the content of the local scope into a JSObject. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11477 | ASSERT(nested_scope_chain_.length() == 1); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11478 | return MaterializeLocalScope(isolate_, frame_, inlined_jsframe_index_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11479 | case ScopeIterator::ScopeTypeWith: |
| 11480 | // Return the with object. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11481 | return Handle<JSObject>(JSObject::cast(CurrentContext()->extension())); |
| 11482 | case ScopeIterator::ScopeTypeCatch: |
| 11483 | return MaterializeCatchScope(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11484 | case ScopeIterator::ScopeTypeClosure: |
| 11485 | // Materialize the content of the closure scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11486 | return MaterializeClosure(isolate_, CurrentContext()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 11487 | case ScopeIterator::ScopeTypeBlock: |
| 11488 | return MaterializeBlockScope(isolate_, CurrentContext()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11489 | case ScopeIterator::ScopeTypeModule: |
| 11490 | return MaterializeModuleScope(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11491 | } |
| 11492 | UNREACHABLE(); |
| 11493 | return Handle<JSObject>(); |
| 11494 | } |
| 11495 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11496 | Handle<ScopeInfo> CurrentScopeInfo() { |
| 11497 | if (!nested_scope_chain_.is_empty()) { |
| 11498 | return nested_scope_chain_.last(); |
| 11499 | } else if (context_->IsBlockContext()) { |
| 11500 | return Handle<ScopeInfo>(ScopeInfo::cast(context_->extension())); |
| 11501 | } else if (context_->IsFunctionContext()) { |
| 11502 | return Handle<ScopeInfo>(context_->closure()->shared()->scope_info()); |
| 11503 | } |
| 11504 | return Handle<ScopeInfo>::null(); |
| 11505 | } |
| 11506 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11507 | // Return the context for this scope. For the local context there might not |
| 11508 | // be an actual context. |
| 11509 | Handle<Context> CurrentContext() { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11510 | if (Type() == ScopeTypeGlobal || |
| 11511 | nested_scope_chain_.is_empty()) { |
| 11512 | return context_; |
| 11513 | } else if (nested_scope_chain_.last()->HasContext()) { |
| 11514 | return context_; |
| 11515 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11516 | return Handle<Context>(); |
| 11517 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11518 | } |
| 11519 | |
| 11520 | #ifdef DEBUG |
| 11521 | // Debug print of the content of the current scope. |
| 11522 | void DebugPrint() { |
| 11523 | switch (Type()) { |
| 11524 | case ScopeIterator::ScopeTypeGlobal: |
| 11525 | PrintF("Global:\n"); |
| 11526 | CurrentContext()->Print(); |
| 11527 | break; |
| 11528 | |
| 11529 | case ScopeIterator::ScopeTypeLocal: { |
| 11530 | PrintF("Local:\n"); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11531 | function_->shared()->scope_info()->Print(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11532 | if (!CurrentContext().is_null()) { |
| 11533 | CurrentContext()->Print(); |
| 11534 | if (CurrentContext()->has_extension()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11535 | Handle<Object> extension(CurrentContext()->extension()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11536 | if (extension->IsJSContextExtensionObject()) { |
| 11537 | extension->Print(); |
| 11538 | } |
| 11539 | } |
| 11540 | } |
| 11541 | break; |
| 11542 | } |
| 11543 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11544 | case ScopeIterator::ScopeTypeWith: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11545 | PrintF("With:\n"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11546 | CurrentContext()->extension()->Print(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11547 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11548 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11549 | case ScopeIterator::ScopeTypeCatch: |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11550 | PrintF("Catch:\n"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11551 | CurrentContext()->extension()->Print(); |
| 11552 | CurrentContext()->get(Context::THROWN_OBJECT_INDEX)->Print(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11553 | break; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11554 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11555 | case ScopeIterator::ScopeTypeClosure: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11556 | PrintF("Closure:\n"); |
| 11557 | CurrentContext()->Print(); |
| 11558 | if (CurrentContext()->has_extension()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11559 | Handle<Object> extension(CurrentContext()->extension()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11560 | if (extension->IsJSContextExtensionObject()) { |
| 11561 | extension->Print(); |
| 11562 | } |
| 11563 | } |
| 11564 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11565 | |
| 11566 | default: |
| 11567 | UNREACHABLE(); |
| 11568 | } |
| 11569 | PrintF("\n"); |
| 11570 | } |
| 11571 | #endif |
| 11572 | |
| 11573 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11574 | Isolate* isolate_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11575 | JavaScriptFrame* frame_; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11576 | int inlined_jsframe_index_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11577 | Handle<JSFunction> function_; |
| 11578 | Handle<Context> context_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 11579 | List<Handle<ScopeInfo> > nested_scope_chain_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11580 | |
| 11581 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 11582 | }; |
| 11583 | |
| 11584 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11585 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11586 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11587 | ASSERT(args.length() == 2); |
| 11588 | |
| 11589 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11590 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11591 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11592 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11593 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11594 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11595 | CONVERT_SMI_ARG_CHECKED(wrapped_id, 1); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11596 | |
| 11597 | // Get the frame where the debugging is performed. |
| 11598 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11599 | JavaScriptFrameIterator it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11600 | JavaScriptFrame* frame = it.frame(); |
| 11601 | |
| 11602 | // Count the visible scopes. |
| 11603 | int n = 0; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11604 | for (ScopeIterator it(isolate, frame, 0); |
| 11605 | !it.Done(); |
| 11606 | it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11607 | n++; |
| 11608 | } |
| 11609 | |
| 11610 | return Smi::FromInt(n); |
| 11611 | } |
| 11612 | |
| 11613 | |
| 11614 | static const int kScopeDetailsTypeIndex = 0; |
| 11615 | static const int kScopeDetailsObjectIndex = 1; |
| 11616 | static const int kScopeDetailsSize = 2; |
| 11617 | |
| 11618 | // Return an array with scope details |
| 11619 | // args[0]: number: break id |
| 11620 | // args[1]: number: frame index |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11621 | // args[2]: number: inlined frame index |
| 11622 | // args[3]: number: scope index |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11623 | // |
| 11624 | // The array returned contains the following information: |
| 11625 | // 0: Scope type |
| 11626 | // 1: Scope object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11627 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11628 | HandleScope scope(isolate); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11629 | ASSERT(args.length() == 4); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11630 | |
| 11631 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11632 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11633 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11634 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11635 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11636 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11637 | CONVERT_SMI_ARG_CHECKED(wrapped_id, 1); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11638 | CONVERT_NUMBER_CHECKED(int, inlined_jsframe_index, Int32, args[2]); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11639 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[3]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11640 | |
| 11641 | // Get the frame where the debugging is performed. |
| 11642 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11643 | JavaScriptFrameIterator frame_it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11644 | JavaScriptFrame* frame = frame_it.frame(); |
| 11645 | |
| 11646 | // Find the requested scope. |
| 11647 | int n = 0; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11648 | ScopeIterator it(isolate, frame, inlined_jsframe_index); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11649 | for (; !it.Done() && n < index; it.Next()) { |
| 11650 | n++; |
| 11651 | } |
| 11652 | if (it.Done()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11653 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11654 | } |
| 11655 | |
| 11656 | // Calculate the size of the result. |
| 11657 | int details_size = kScopeDetailsSize; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11658 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11659 | |
| 11660 | // Fill in scope details. |
| 11661 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 11662 | Handle<JSObject> scope_object = it.ScopeObject(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11663 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 11664 | details->set(kScopeDetailsObjectIndex, *scope_object); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11665 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11666 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11667 | } |
| 11668 | |
| 11669 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11670 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrintScopes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11671 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11672 | ASSERT(args.length() == 0); |
| 11673 | |
| 11674 | #ifdef DEBUG |
| 11675 | // Print the scopes for the top frame. |
| 11676 | StackFrameLocator locator; |
| 11677 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 11678 | for (ScopeIterator it(isolate, frame, 0); |
| 11679 | !it.Done(); |
| 11680 | it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11681 | it.DebugPrint(); |
| 11682 | } |
| 11683 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11684 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11685 | } |
| 11686 | |
| 11687 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11688 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11689 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11690 | ASSERT(args.length() == 1); |
| 11691 | |
| 11692 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11693 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11694 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 11695 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11696 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11697 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11698 | |
| 11699 | // Count all archived V8 threads. |
| 11700 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11701 | for (ThreadState* thread = |
| 11702 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11703 | thread != NULL; |
| 11704 | thread = thread->Next()) { |
| 11705 | n++; |
| 11706 | } |
| 11707 | |
| 11708 | // Total number of threads is current thread and archived threads. |
| 11709 | return Smi::FromInt(n + 1); |
| 11710 | } |
| 11711 | |
| 11712 | |
| 11713 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 11714 | static const int kThreadDetailsThreadIdIndex = 1; |
| 11715 | static const int kThreadDetailsSize = 2; |
| 11716 | |
| 11717 | // Return an array with thread details |
| 11718 | // args[0]: number: break id |
| 11719 | // args[1]: number: thread index |
| 11720 | // |
| 11721 | // The array returned contains the following information: |
| 11722 | // 0: Is current thread? |
| 11723 | // 1: Thread id |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11724 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11725 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11726 | ASSERT(args.length() == 2); |
| 11727 | |
| 11728 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11729 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11730 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11731 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11732 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11733 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11734 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 11735 | |
| 11736 | // Allocate array for result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11737 | Handle<FixedArray> details = |
| 11738 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11739 | |
| 11740 | // Thread index 0 is current thread. |
| 11741 | if (index == 0) { |
| 11742 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11743 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11744 | isolate->heap()->true_value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11745 | details->set(kThreadDetailsThreadIdIndex, |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 11746 | Smi::FromInt(ThreadId::Current().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11747 | } else { |
| 11748 | // Find the thread with the requested index. |
| 11749 | int n = 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11750 | ThreadState* thread = |
| 11751 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11752 | while (index != n && thread != NULL) { |
| 11753 | thread = thread->Next(); |
| 11754 | n++; |
| 11755 | } |
| 11756 | if (thread == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11757 | return isolate->heap()->undefined_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11758 | } |
| 11759 | |
| 11760 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11761 | details->set(kThreadDetailsCurrentThreadIndex, |
| 11762 | isolate->heap()->false_value()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 11763 | details->set(kThreadDetailsThreadIdIndex, |
| 11764 | Smi::FromInt(thread->id().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11765 | } |
| 11766 | |
| 11767 | // Convert to JS array and return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11768 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 11769 | } |
| 11770 | |
| 11771 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 11772 | // Sets the disable break state |
| 11773 | // args[0]: disable break state |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11774 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDisableBreak) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11775 | HandleScope scope(isolate); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 11776 | ASSERT(args.length() == 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11777 | CONVERT_BOOLEAN_ARG_CHECKED(disable_break, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11778 | isolate->debug()->set_disable_break(disable_break); |
| 11779 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 11780 | } |
| 11781 | |
| 11782 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11783 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetBreakLocations) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11784 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11785 | ASSERT(args.length() == 1); |
| 11786 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11787 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 11788 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11789 | // Find the number of break points |
| 11790 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11791 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11792 | // Return array as JS array |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11793 | return *isolate->factory()->NewJSArrayWithElements( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11794 | Handle<FixedArray>::cast(break_locations)); |
| 11795 | } |
| 11796 | |
| 11797 | |
| 11798 | // Set a break point in a function |
| 11799 | // args[0]: function |
| 11800 | // args[1]: number: break source position (within the function source) |
| 11801 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11802 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFunctionBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11803 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11804 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11805 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 11806 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11807 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11808 | RUNTIME_ASSERT(source_position >= 0); |
| 11809 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11810 | |
| 11811 | // Set break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11812 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 11813 | &source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11814 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 11815 | return Smi::FromInt(source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11816 | } |
| 11817 | |
| 11818 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11819 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 11820 | Handle<Script> script, |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 11821 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11822 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 11823 | // script. The inner most SharedFunctionInfo containing the source position |
| 11824 | // for the requested break point is found. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11825 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11826 | // which is found is not compiled it is compiled and the heap is iterated |
| 11827 | // again as the compilation might create inner functions from the newly |
| 11828 | // compiled function and the actual requested break point might be in one of |
| 11829 | // these functions. |
| 11830 | bool done = false; |
| 11831 | // The current candidate for the source position: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 11832 | int target_start_position = RelocInfo::kNoPosition; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11833 | Handle<SharedFunctionInfo> target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11834 | while (!done) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11835 | { // Extra scope for iterator and no-allocation. |
| 11836 | isolate->heap()->EnsureHeapIsIterable(); |
| 11837 | AssertNoAllocation no_alloc_during_heap_iteration; |
| 11838 | HeapIterator iterator; |
| 11839 | for (HeapObject* obj = iterator.next(); |
| 11840 | obj != NULL; obj = iterator.next()) { |
| 11841 | if (obj->IsSharedFunctionInfo()) { |
| 11842 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 11843 | if (shared->script() == *script) { |
| 11844 | // If the SharedFunctionInfo found has the requested script data and |
| 11845 | // contains the source position it is a candidate. |
| 11846 | int start_position = shared->function_token_position(); |
| 11847 | if (start_position == RelocInfo::kNoPosition) { |
| 11848 | start_position = shared->start_position(); |
| 11849 | } |
| 11850 | if (start_position <= position && |
| 11851 | position <= shared->end_position()) { |
| 11852 | // If there is no candidate or this function is within the current |
| 11853 | // candidate this is the new candidate. |
| 11854 | if (target.is_null()) { |
| 11855 | target_start_position = start_position; |
| 11856 | target = shared; |
| 11857 | } else { |
| 11858 | if (target_start_position == start_position && |
| 11859 | shared->end_position() == target->end_position()) { |
| 11860 | // If a top-level function contain only one function |
| 11861 | // declartion the source for the top-level and the |
| 11862 | // function is the same. In that case prefer the non |
| 11863 | // top-level function. |
| 11864 | if (!shared->is_toplevel()) { |
| 11865 | target_start_position = start_position; |
| 11866 | target = shared; |
| 11867 | } |
| 11868 | } else if (target_start_position <= start_position && |
| 11869 | shared->end_position() <= target->end_position()) { |
| 11870 | // This containment check includes equality as a function |
| 11871 | // inside a top-level function can share either start or end |
| 11872 | // position with the top-level function. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11873 | target_start_position = start_position; |
| 11874 | target = shared; |
| 11875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11876 | } |
| 11877 | } |
| 11878 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11879 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11880 | } // End for loop. |
| 11881 | } // End No allocation scope. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11882 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11883 | if (target.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11884 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11885 | } |
| 11886 | |
| 11887 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 11888 | // compilation of inner functions is introduced some additional checking |
| 11889 | // needs to be done here to compile inner functions. |
| 11890 | done = target->is_compiled(); |
| 11891 | if (!done) { |
| 11892 | // If the candidate is not compiled compile it to reveal any inner |
| 11893 | // functions which might contain the requested source position. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 11894 | SharedFunctionInfo::CompileLazy(target, KEEP_EXCEPTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11895 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 11896 | } // End while loop. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11897 | |
| 11898 | return *target; |
| 11899 | } |
| 11900 | |
| 11901 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11902 | // Changes the state of a break point in a script and returns source position |
| 11903 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 11904 | // GetScriptFromScriptData. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11905 | // args[0]: script to set break point in |
| 11906 | // args[1]: number: break source position (within the script source) |
| 11907 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11908 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetScriptBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11909 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11910 | ASSERT(args.length() == 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11911 | CONVERT_ARG_HANDLE_CHECKED(JSValue, wrapper, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11912 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11913 | RUNTIME_ASSERT(source_position >= 0); |
| 11914 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 11915 | |
| 11916 | // Get the script from the script wrapper. |
| 11917 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 11918 | Handle<Script> script(Script::cast(wrapper->value())); |
| 11919 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 11920 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11921 | isolate, script, source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11922 | if (!result->IsUndefined()) { |
| 11923 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 11924 | // Find position within function. The script position might be before the |
| 11925 | // source position of the first function. |
| 11926 | int position; |
| 11927 | if (shared->start_position() > source_position) { |
| 11928 | position = 0; |
| 11929 | } else { |
| 11930 | position = source_position - shared->start_position(); |
| 11931 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11932 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 11933 | position += shared->start_position(); |
| 11934 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11935 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11936 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11937 | } |
| 11938 | |
| 11939 | |
| 11940 | // Clear a break point |
| 11941 | // args[0]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11942 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11943 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11944 | ASSERT(args.length() == 1); |
| 11945 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 11946 | |
| 11947 | // Clear break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11948 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11949 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11950 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11951 | } |
| 11952 | |
| 11953 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11954 | // Change the state of break on exceptions. |
| 11955 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 11956 | // args[1]: Boolean indicating on/off. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11957 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ChangeBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11958 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11959 | ASSERT(args.length() == 2); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11960 | RUNTIME_ASSERT(args[0]->IsNumber()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 11961 | CONVERT_BOOLEAN_ARG_CHECKED(enable, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11962 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11963 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 11964 | // function will default to affecting caught exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11965 | ExceptionBreakType type = |
| 11966 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11967 | // Update break point state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11968 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 11969 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11970 | } |
| 11971 | |
| 11972 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11973 | // Returns the state of break on exceptions |
| 11974 | // args[0]: boolean indicating uncaught exceptions |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11975 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11976 | HandleScope scope(isolate); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11977 | ASSERT(args.length() == 1); |
| 11978 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11979 | |
| 11980 | ExceptionBreakType type = |
| 11981 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11982 | bool result = isolate->debug()->IsBreakOnException(type); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11983 | return Smi::FromInt(result); |
| 11984 | } |
| 11985 | |
| 11986 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11987 | // Prepare for stepping |
| 11988 | // args[0]: break id for checking execution state |
| 11989 | // args[1]: step action from the enumeration StepAction |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11990 | // args[2]: number of times to perform the step, for step out it is the number |
| 11991 | // of frames to step down. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11992 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrepareStep) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11993 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11994 | ASSERT(args.length() == 3); |
| 11995 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11996 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11997 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11998 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11999 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 12000 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12001 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12002 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12003 | } |
| 12004 | |
| 12005 | // Get the step action and check validity. |
| 12006 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 12007 | if (step_action != StepIn && |
| 12008 | step_action != StepNext && |
| 12009 | step_action != StepOut && |
| 12010 | step_action != StepInMin && |
| 12011 | step_action != StepMin) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12012 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12013 | } |
| 12014 | |
| 12015 | // Get the number of steps. |
| 12016 | int step_count = NumberToInt32(args[2]); |
| 12017 | if (step_count < 1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12018 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12019 | } |
| 12020 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 12021 | // Clear all current stepping setup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12022 | isolate->debug()->ClearStepping(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 12023 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12024 | // Prepare step. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12025 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 12026 | step_count); |
| 12027 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12028 | } |
| 12029 | |
| 12030 | |
| 12031 | // Clear all stepping set by PrepareStep. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12032 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearStepping) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12033 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12034 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12035 | isolate->debug()->ClearStepping(); |
| 12036 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12037 | } |
| 12038 | |
| 12039 | |
| 12040 | // Creates a copy of the with context chain. The copy of the context chain is |
| 12041 | // is linked to the function context supplied. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12042 | static Handle<Context> CopyNestedScopeContextChain(Isolate* isolate, |
| 12043 | Handle<JSFunction> function, |
| 12044 | Handle<Context> base, |
| 12045 | JavaScriptFrame* frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12046 | int inlined_jsframe_index) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12047 | HandleScope scope(isolate); |
| 12048 | List<Handle<ScopeInfo> > scope_chain; |
| 12049 | List<Handle<Context> > context_chain; |
| 12050 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12051 | ScopeIterator it(isolate, frame, inlined_jsframe_index); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12052 | for (; it.Type() != ScopeIterator::ScopeTypeGlobal && |
| 12053 | it.Type() != ScopeIterator::ScopeTypeLocal ; it.Next()) { |
| 12054 | ASSERT(!it.Done()); |
| 12055 | scope_chain.Add(it.CurrentScopeInfo()); |
| 12056 | context_chain.Add(it.CurrentContext()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12057 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12058 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12059 | // At the end of the chain. Return the base context to link to. |
| 12060 | Handle<Context> context = base; |
| 12061 | |
| 12062 | // Iteratively copy and or materialize the nested contexts. |
| 12063 | while (!scope_chain.is_empty()) { |
| 12064 | Handle<ScopeInfo> scope_info = scope_chain.RemoveLast(); |
| 12065 | Handle<Context> current = context_chain.RemoveLast(); |
| 12066 | ASSERT(!(scope_info->HasContext() & current.is_null())); |
| 12067 | |
| 12068 | if (scope_info->Type() == CATCH_SCOPE) { |
| 12069 | Handle<String> name(String::cast(current->extension())); |
| 12070 | Handle<Object> thrown_object(current->get(Context::THROWN_OBJECT_INDEX)); |
| 12071 | context = |
| 12072 | isolate->factory()->NewCatchContext(function, |
| 12073 | context, |
| 12074 | name, |
| 12075 | thrown_object); |
| 12076 | } else if (scope_info->Type() == BLOCK_SCOPE) { |
| 12077 | // Materialize the contents of the block scope into a JSObject. |
| 12078 | Handle<JSObject> block_scope_object = |
| 12079 | MaterializeBlockScope(isolate, current); |
| 12080 | if (block_scope_object.is_null()) { |
| 12081 | return Handle<Context>::null(); |
| 12082 | } |
| 12083 | // Allocate a new function context for the debug evaluation and set the |
| 12084 | // extension object. |
| 12085 | Handle<Context> new_context = |
| 12086 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 12087 | function); |
| 12088 | new_context->set_extension(*block_scope_object); |
| 12089 | new_context->set_previous(*context); |
| 12090 | context = new_context; |
| 12091 | } else { |
| 12092 | ASSERT(scope_info->Type() == WITH_SCOPE); |
| 12093 | ASSERT(current->IsWithContext()); |
| 12094 | Handle<JSObject> extension(JSObject::cast(current->extension())); |
| 12095 | context = |
| 12096 | isolate->factory()->NewWithContext(function, context, extension); |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 12097 | } |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 12098 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12099 | |
| 12100 | return scope.CloseAndEscape(context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12101 | } |
| 12102 | |
| 12103 | |
| 12104 | // Helper function to find or create the arguments object for |
| 12105 | // Runtime_DebugEvaluate. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12106 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 12107 | JavaScriptFrame* frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12108 | FrameInspector* frame_inspector, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12109 | Handle<ScopeInfo> scope_info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12110 | Handle<Context> function_context) { |
| 12111 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 12112 | // found (that is the debugged function does not reference 'arguments' and |
| 12113 | // does not support eval) then create an 'arguments' object. |
| 12114 | int index; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12115 | if (scope_info->StackLocalCount() > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12116 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12117 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12118 | return Handle<Object>(frame->GetExpression(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12119 | } |
| 12120 | } |
| 12121 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12122 | if (scope_info->HasHeapAllocatedLocals()) { |
| 12123 | VariableMode mode; |
| 12124 | InitializationFlag init_flag; |
| 12125 | index = scope_info->ContextSlotIndex( |
| 12126 | isolate->heap()->arguments_symbol(), &mode, &init_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12127 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12128 | return Handle<Object>(function_context->get(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12129 | } |
| 12130 | } |
| 12131 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12132 | Handle<JSFunction> function(JSFunction::cast(frame_inspector->GetFunction())); |
| 12133 | int length = frame_inspector->GetParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12134 | Handle<JSObject> arguments = |
| 12135 | isolate->factory()->NewArgumentsObject(function, length); |
| 12136 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12137 | |
| 12138 | AssertNoAllocation no_gc; |
| 12139 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12140 | for (int i = 0; i < length; i++) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12141 | array->set(i, frame_inspector->GetParameter(i), mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12142 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 12143 | arguments->set_elements(*array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12144 | return arguments; |
| 12145 | } |
| 12146 | |
| 12147 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12148 | static const char kSourceStr[] = |
| 12149 | "(function(arguments,__source__){return eval(__source__);})"; |
| 12150 | |
| 12151 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12152 | // 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] | 12153 | // 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] | 12154 | // extension part has all the parameters and locals of the function on the |
| 12155 | // stack frame. A function which calls eval with the code to evaluate is then |
| 12156 | // compiled in this context and called in this context. As this context |
| 12157 | // replaces the context of the function on the stack frame a new (empty) |
| 12158 | // function is created as well to be used as the closure for the context. |
| 12159 | // This function and the context acts as replacements for the function on the |
| 12160 | // stack frame presenting the same view of the values of parameters and |
| 12161 | // local variables as if the piece of JavaScript was evaluated at the point |
| 12162 | // where the function on the stack frame is currently stopped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12163 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12164 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12165 | |
| 12166 | // Check the execution state and decode arguments frame and source to be |
| 12167 | // evaluated. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12168 | ASSERT(args.length() == 6); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12169 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12170 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 12171 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12172 | if (!maybe_check_result->ToObject(&check_result)) { |
| 12173 | return maybe_check_result; |
| 12174 | } |
| 12175 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12176 | CONVERT_SMI_ARG_CHECKED(wrapped_id, 1); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12177 | CONVERT_NUMBER_CHECKED(int, inlined_jsframe_index, Int32, args[2]); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12178 | CONVERT_ARG_HANDLE_CHECKED(String, source, 3); |
| 12179 | CONVERT_BOOLEAN_ARG_CHECKED(disable_break, 4); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12180 | Handle<Object> additional_context(args[5]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 12181 | |
| 12182 | // Handle the processing of break. |
| 12183 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12184 | |
| 12185 | // Get the frame where the debugging is performed. |
| 12186 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 12187 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12188 | JavaScriptFrame* frame = it.frame(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12189 | FrameInspector frame_inspector(frame, inlined_jsframe_index, isolate); |
| 12190 | Handle<JSFunction> function(JSFunction::cast(frame_inspector.GetFunction())); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12191 | Handle<ScopeInfo> scope_info(function->shared()->scope_info()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12192 | |
| 12193 | // Traverse the saved contexts chain to find the active context for the |
| 12194 | // selected frame. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12195 | SaveContext* save = FindSavedContextForFrame(isolate, frame); |
| 12196 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12197 | SaveContext savex(isolate); |
| 12198 | isolate->set_context(*(save->context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12199 | |
| 12200 | // Create the (empty) function replacing the function on the stack frame for |
| 12201 | // the purpose of evaluating in the context created below. It is important |
| 12202 | // that this function does not describe any parameters and local variables |
| 12203 | // in the context. If it does then this will cause problems with the lookup |
| 12204 | // in Context::Lookup, where context slots for parameters and local variables |
| 12205 | // are looked at before the extension object. |
| 12206 | Handle<JSFunction> go_between = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12207 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 12208 | isolate->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12209 | go_between->set_context(function->context()); |
| 12210 | #ifdef DEBUG |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12211 | Handle<ScopeInfo> go_between_scope_info(go_between->shared()->scope_info()); |
| 12212 | ASSERT(go_between_scope_info->ParameterCount() == 0); |
| 12213 | ASSERT(go_between_scope_info->ContextLocalCount() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12214 | #endif |
| 12215 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 12216 | // Materialize the content of the local scope into a JSObject. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12217 | Handle<JSObject> local_scope = MaterializeLocalScopeWithFrameInspector( |
| 12218 | isolate, frame, &frame_inspector); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12219 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12220 | |
| 12221 | // Allocate a new context for the debug evaluation and set the extension |
| 12222 | // object build. |
| 12223 | Handle<Context> context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12224 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 12225 | go_between); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 12226 | context->set_extension(*local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12227 | // 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] | 12228 | Handle<Context> frame_context(Context::cast(frame->context())); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12229 | Handle<Context> function_context; |
| 12230 | // Get the function's context if it has one. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12231 | if (scope_info->HasContext()) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 12232 | function_context = Handle<Context>(frame_context->declaration_context()); |
| 12233 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12234 | context = CopyNestedScopeContextChain(isolate, |
| 12235 | go_between, |
| 12236 | context, |
| 12237 | frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12238 | inlined_jsframe_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12239 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 12240 | if (additional_context->IsJSObject()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12241 | Handle<JSObject> extension = Handle<JSObject>::cast(additional_context); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 12242 | context = |
| 12243 | isolate->factory()->NewWithContext(go_between, context, extension); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 12244 | } |
| 12245 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12246 | // Wrap the evaluation statement in a new function compiled in the newly |
| 12247 | // created context. The function has one parameter which has to be called |
| 12248 | // '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] | 12249 | // the function being debugged. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12250 | // function(arguments,__source__) {return eval(__source__);} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12251 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12252 | Handle<String> function_source = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12253 | isolate->factory()->NewStringFromAscii( |
| 12254 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 12255 | |
| 12256 | // Currently, the eval code will be executed in non-strict mode, |
| 12257 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 12258 | Handle<SharedFunctionInfo> shared = |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12259 | Compiler::CompileEval(function_source, |
| 12260 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 12261 | context->IsGlobalContext(), |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12262 | CLASSIC_MODE, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12263 | RelocInfo::kNoPosition); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 12264 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12265 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12266 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12267 | |
| 12268 | // Invoke the result of the compilation to get the evaluation function. |
| 12269 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12270 | Handle<Object> receiver(frame->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12271 | Handle<Object> evaluation_function = |
| 12272 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 12273 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 12274 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12275 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 12276 | Handle<Object> arguments = GetArgumentsObject(isolate, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12277 | frame, |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12278 | &frame_inspector, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 12279 | scope_info, |
| 12280 | function_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12281 | |
| 12282 | // Invoke the evaluation function and return the result. |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12283 | Handle<Object> argv[] = { arguments, source }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12284 | Handle<Object> result = |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 12285 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), |
| 12286 | receiver, |
| 12287 | ARRAY_SIZE(argv), |
| 12288 | argv, |
| 12289 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 12290 | if (has_pending_exception) return Failure::Exception(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 12291 | |
| 12292 | // Skip the global proxy as it has no properties and always delegates to the |
| 12293 | // real global object. |
| 12294 | if (result->IsJSGlobalProxy()) { |
| 12295 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 12296 | } |
| 12297 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12298 | return *result; |
| 12299 | } |
| 12300 | |
| 12301 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12302 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluateGlobal) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12303 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12304 | |
| 12305 | // Check the execution state and decode arguments frame and source to be |
| 12306 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 12307 | ASSERT(args.length() == 4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12308 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12309 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 12310 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12311 | if (!maybe_check_result->ToObject(&check_result)) { |
| 12312 | return maybe_check_result; |
| 12313 | } |
| 12314 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12315 | CONVERT_ARG_HANDLE_CHECKED(String, source, 1); |
| 12316 | CONVERT_BOOLEAN_ARG_CHECKED(disable_break, 2); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 12317 | Handle<Object> additional_context(args[3]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 12318 | |
| 12319 | // Handle the processing of break. |
| 12320 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12321 | |
| 12322 | // Enter the top context from before the debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12323 | SaveContext save(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12324 | SaveContext* top = &save; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12325 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12326 | top = top->prev(); |
| 12327 | } |
| 12328 | if (top != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12329 | isolate->set_context(*top->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12330 | } |
| 12331 | |
| 12332 | // Get the global context now set to the top context from before the |
| 12333 | // debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12334 | Handle<Context> context = isolate->global_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12335 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 12336 | bool is_global = true; |
| 12337 | |
| 12338 | if (additional_context->IsJSObject()) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 12339 | // Create a new with context with the additional context information between |
| 12340 | // the context of the debugged function and the eval code to be executed. |
| 12341 | context = isolate->factory()->NewWithContext( |
| 12342 | Handle<JSFunction>(context->closure()), |
| 12343 | context, |
| 12344 | Handle<JSObject>::cast(additional_context)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 12345 | is_global = false; |
| 12346 | } |
| 12347 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12348 | // Compile the source to be evaluated. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 12349 | // Currently, the eval code will be executed in non-strict mode, |
| 12350 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 12351 | Handle<SharedFunctionInfo> shared = |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12352 | Compiler::CompileEval(source, |
| 12353 | context, |
| 12354 | is_global, |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 12355 | CLASSIC_MODE, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 12356 | RelocInfo::kNoPosition); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 12357 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12358 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12359 | Handle<JSFunction>( |
| 12360 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 12361 | context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12362 | |
| 12363 | // Invoke the result of the compilation to get the evaluation function. |
| 12364 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12365 | Handle<Object> receiver = isolate->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12366 | Handle<Object> result = |
| 12367 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 12368 | &has_pending_exception); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12369 | // Clear the oneshot breakpoints so that the debugger does not step further. |
| 12370 | isolate->debug()->ClearStepping(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 12371 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12372 | return *result; |
| 12373 | } |
| 12374 | |
| 12375 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12376 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetLoadedScripts) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12377 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12378 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12379 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12380 | // Fill the script objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12381 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12382 | |
| 12383 | // Convert the script objects to proper JS objects. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 12384 | for (int i = 0; i < instances->length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 12385 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 12386 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 12387 | // because using |
| 12388 | // instances->set(i, *GetScriptWrapper(script)) |
| 12389 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 12390 | // already have dereferenced the instances handle. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 12391 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 12392 | instances->set(i, *wrapper); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12393 | } |
| 12394 | |
| 12395 | // Return result as a JS array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12396 | Handle<JSObject> result = |
| 12397 | isolate->factory()->NewJSObject(isolate->array_function()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12398 | isolate->factory()->SetContent(Handle<JSArray>::cast(result), instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12399 | return *result; |
| 12400 | } |
| 12401 | |
| 12402 | |
| 12403 | // Helper function used by Runtime_DebugReferencedBy below. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12404 | static int DebugReferencedBy(HeapIterator* iterator, |
| 12405 | JSObject* target, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12406 | Object* instance_filter, int max_references, |
| 12407 | FixedArray* instances, int instances_size, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12408 | JSFunction* arguments_function) { |
| 12409 | NoHandleAllocation ha; |
| 12410 | AssertNoAllocation no_alloc; |
| 12411 | |
| 12412 | // Iterate the heap. |
| 12413 | int count = 0; |
| 12414 | JSObject* last = NULL; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12415 | HeapObject* heap_obj = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12416 | while (((heap_obj = iterator->next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12417 | (max_references == 0 || count < max_references)) { |
| 12418 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12419 | if (heap_obj->IsJSObject()) { |
| 12420 | // Skip context extension objects and argument arrays as these are |
| 12421 | // checked in the context of functions using them. |
| 12422 | JSObject* obj = JSObject::cast(heap_obj); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 12423 | if (obj->IsJSContextExtensionObject() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12424 | obj->map()->constructor() == arguments_function) { |
| 12425 | continue; |
| 12426 | } |
| 12427 | |
| 12428 | // Check if the JS object has a reference to the object looked for. |
| 12429 | if (obj->ReferencesObject(target)) { |
| 12430 | // Check instance filter if supplied. This is normally used to avoid |
| 12431 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 12432 | if (!instance_filter->IsUndefined()) { |
| 12433 | Object* V = obj; |
| 12434 | while (true) { |
| 12435 | Object* prototype = V->GetPrototype(); |
| 12436 | if (prototype->IsNull()) { |
| 12437 | break; |
| 12438 | } |
| 12439 | if (instance_filter == prototype) { |
| 12440 | obj = NULL; // Don't add this object. |
| 12441 | break; |
| 12442 | } |
| 12443 | V = prototype; |
| 12444 | } |
| 12445 | } |
| 12446 | |
| 12447 | if (obj != NULL) { |
| 12448 | // Valid reference found add to instance array if supplied an update |
| 12449 | // count. |
| 12450 | if (instances != NULL && count < instances_size) { |
| 12451 | instances->set(count, obj); |
| 12452 | } |
| 12453 | last = obj; |
| 12454 | count++; |
| 12455 | } |
| 12456 | } |
| 12457 | } |
| 12458 | } |
| 12459 | |
| 12460 | // Check for circular reference only. This can happen when the object is only |
| 12461 | // referenced from mirrors and has a circular reference in which case the |
| 12462 | // object is not really alive and would have been garbage collected if not |
| 12463 | // referenced from the mirror. |
| 12464 | if (count == 1 && last == target) { |
| 12465 | count = 0; |
| 12466 | } |
| 12467 | |
| 12468 | // Return the number of referencing objects found. |
| 12469 | return count; |
| 12470 | } |
| 12471 | |
| 12472 | |
| 12473 | // Scan the heap for objects with direct references to an object |
| 12474 | // args[0]: the object to find references to |
| 12475 | // args[1]: constructor function for instances to exclude (Mirror) |
| 12476 | // args[2]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12477 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugReferencedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12478 | ASSERT(args.length() == 3); |
| 12479 | |
| 12480 | // First perform a full GC in order to avoid references from dead objects. |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 12481 | isolate->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 12482 | "%DebugReferencedBy"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12483 | // The heap iterator reserves the right to do a GC to make the heap iterable. |
| 12484 | // Due to the GC above we know it won't need to do that, but it seems cleaner |
| 12485 | // to get the heap iterator constructed before we start having unprotected |
| 12486 | // Object* locals that are not protected by handles. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12487 | |
| 12488 | // Check parameters. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12489 | CONVERT_ARG_CHECKED(JSObject, target, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12490 | Object* instance_filter = args[1]; |
| 12491 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 12492 | instance_filter->IsJSObject()); |
| 12493 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 12494 | RUNTIME_ASSERT(max_references >= 0); |
| 12495 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12496 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12497 | // Get the constructor function for context extension and arguments array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12498 | JSObject* arguments_boilerplate = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12499 | isolate->context()->global_context()->arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12500 | JSFunction* arguments_function = |
| 12501 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 12502 | |
| 12503 | // Get the number of referencing objects. |
| 12504 | int count; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12505 | HeapIterator heap_iterator; |
| 12506 | count = DebugReferencedBy(&heap_iterator, |
| 12507 | target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 12508 | NULL, 0, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12509 | |
| 12510 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12511 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12512 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12513 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12514 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12515 | FixedArray* instances = FixedArray::cast(object); |
| 12516 | |
| 12517 | // Fill the referencing objects. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12518 | // AllocateFixedArray above does not make the heap non-iterable. |
| 12519 | ASSERT(HEAP->IsHeapIterable()); |
| 12520 | HeapIterator heap_iterator2; |
| 12521 | count = DebugReferencedBy(&heap_iterator2, |
| 12522 | target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 12523 | instances, count, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12524 | |
| 12525 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12526 | Object* result; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12527 | MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12528 | isolate->context()->global_context()->array_function()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12529 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12530 | return JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12531 | } |
| 12532 | |
| 12533 | |
| 12534 | // Helper function used by Runtime_DebugConstructedBy below. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12535 | static int DebugConstructedBy(HeapIterator* iterator, |
| 12536 | JSFunction* constructor, |
| 12537 | int max_references, |
| 12538 | FixedArray* instances, |
| 12539 | int instances_size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12540 | AssertNoAllocation no_alloc; |
| 12541 | |
| 12542 | // Iterate the heap. |
| 12543 | int count = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12544 | HeapObject* heap_obj = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12545 | while (((heap_obj = iterator->next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12546 | (max_references == 0 || count < max_references)) { |
| 12547 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12548 | if (heap_obj->IsJSObject()) { |
| 12549 | JSObject* obj = JSObject::cast(heap_obj); |
| 12550 | if (obj->map()->constructor() == constructor) { |
| 12551 | // Valid reference found add to instance array if supplied an update |
| 12552 | // count. |
| 12553 | if (instances != NULL && count < instances_size) { |
| 12554 | instances->set(count, obj); |
| 12555 | } |
| 12556 | count++; |
| 12557 | } |
| 12558 | } |
| 12559 | } |
| 12560 | |
| 12561 | // Return the number of referencing objects found. |
| 12562 | return count; |
| 12563 | } |
| 12564 | |
| 12565 | |
| 12566 | // Scan the heap for objects constructed by a specific function. |
| 12567 | // args[0]: the constructor to find instances of |
| 12568 | // args[1]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12569 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugConstructedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12570 | ASSERT(args.length() == 2); |
| 12571 | |
| 12572 | // First perform a full GC in order to avoid dead objects. |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 12573 | isolate->heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 12574 | "%DebugConstructedBy"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12575 | |
| 12576 | // Check parameters. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12577 | CONVERT_ARG_CHECKED(JSFunction, constructor, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12578 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 12579 | RUNTIME_ASSERT(max_references >= 0); |
| 12580 | |
| 12581 | // Get the number of referencing objects. |
| 12582 | int count; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12583 | HeapIterator heap_iterator; |
| 12584 | count = DebugConstructedBy(&heap_iterator, |
| 12585 | constructor, |
| 12586 | max_references, |
| 12587 | NULL, |
| 12588 | 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12589 | |
| 12590 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12591 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12592 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12593 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 12594 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12595 | FixedArray* instances = FixedArray::cast(object); |
| 12596 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12597 | ASSERT(HEAP->IsHeapIterable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12598 | // Fill the referencing objects. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12599 | HeapIterator heap_iterator2; |
| 12600 | count = DebugConstructedBy(&heap_iterator2, |
| 12601 | constructor, |
| 12602 | max_references, |
| 12603 | instances, |
| 12604 | count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12605 | |
| 12606 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12607 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12608 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 12609 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12610 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 12611 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12612 | return JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12613 | } |
| 12614 | |
| 12615 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 12616 | // Find the effective prototype object as returned by __proto__. |
| 12617 | // args[0]: the object to find the prototype for. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12618 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPrototype) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12619 | ASSERT(args.length() == 1); |
| 12620 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12621 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12622 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 12623 | // Use the __proto__ accessor. |
| 12624 | return Accessors::ObjectPrototype.getter(obj, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12625 | } |
| 12626 | |
| 12627 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12628 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SystemBreak) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12629 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12630 | CPU::DebugBreak(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12631 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12632 | } |
| 12633 | |
| 12634 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12635 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleFunction) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12636 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12637 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12638 | ASSERT(args.length() == 1); |
| 12639 | // Get the function and make sure it is compiled. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12640 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12641 | Handle<SharedFunctionInfo> shared(func->shared()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12642 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12643 | return Failure::Exception(); |
| 12644 | } |
| 12645 | func->code()->PrintLn(); |
| 12646 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12647 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12648 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 12649 | |
| 12650 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12651 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleConstructor) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12652 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12653 | HandleScope scope(isolate); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12654 | ASSERT(args.length() == 1); |
| 12655 | // Get the function and make sure it is compiled. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12656 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12657 | Handle<SharedFunctionInfo> shared(func->shared()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 12658 | if (!SharedFunctionInfo::EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12659 | return Failure::Exception(); |
| 12660 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12661 | shared->construct_stub()->PrintLn(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12662 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12663 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 12664 | } |
| 12665 | |
| 12666 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12667 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetInferredName) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 12668 | NoHandleAllocation ha; |
| 12669 | ASSERT(args.length() == 1); |
| 12670 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12671 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 12672 | return f->shared()->inferred_name(); |
| 12673 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12674 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12675 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12676 | static int FindSharedFunctionInfosForScript(HeapIterator* iterator, |
| 12677 | Script* script, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12678 | FixedArray* buffer) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12679 | AssertNoAllocation no_allocations; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12680 | int counter = 0; |
| 12681 | int buffer_size = buffer->length(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12682 | for (HeapObject* obj = iterator->next(); |
| 12683 | obj != NULL; |
| 12684 | obj = iterator->next()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12685 | ASSERT(obj != NULL); |
| 12686 | if (!obj->IsSharedFunctionInfo()) { |
| 12687 | continue; |
| 12688 | } |
| 12689 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 12690 | if (shared->script() != script) { |
| 12691 | continue; |
| 12692 | } |
| 12693 | if (counter < buffer_size) { |
| 12694 | buffer->set(counter, shared); |
| 12695 | } |
| 12696 | counter++; |
| 12697 | } |
| 12698 | return counter; |
| 12699 | } |
| 12700 | |
| 12701 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 12702 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 12703 | // in OpaqueReferences. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12704 | RUNTIME_FUNCTION(MaybeObject*, |
| 12705 | Runtime_LiveEditFindSharedFunctionInfosForScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12706 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12707 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12708 | CONVERT_ARG_CHECKED(JSValue, script_value, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12709 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12710 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12711 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 12712 | |
| 12713 | const int kBufferSize = 32; |
| 12714 | |
| 12715 | Handle<FixedArray> array; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12716 | array = isolate->factory()->NewFixedArray(kBufferSize); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12717 | int number; |
| 12718 | { |
| 12719 | isolate->heap()->EnsureHeapIsIterable(); |
| 12720 | AssertNoAllocation no_allocations; |
| 12721 | HeapIterator heap_iterator; |
| 12722 | Script* scr = *script; |
| 12723 | FixedArray* arr = *array; |
| 12724 | number = FindSharedFunctionInfosForScript(&heap_iterator, scr, arr); |
| 12725 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12726 | if (number > kBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12727 | array = isolate->factory()->NewFixedArray(number); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 12728 | isolate->heap()->EnsureHeapIsIterable(); |
| 12729 | AssertNoAllocation no_allocations; |
| 12730 | HeapIterator heap_iterator; |
| 12731 | Script* scr = *script; |
| 12732 | FixedArray* arr = *array; |
| 12733 | FindSharedFunctionInfosForScript(&heap_iterator, scr, arr); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12734 | } |
| 12735 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12736 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12737 | result->set_length(Smi::FromInt(number)); |
| 12738 | |
| 12739 | LiveEdit::WrapSharedFunctionInfos(result); |
| 12740 | |
| 12741 | return *result; |
| 12742 | } |
| 12743 | |
| 12744 | // For a script calculates compilation information about all its functions. |
| 12745 | // The script source is explicitly specified by the second argument. |
| 12746 | // The source of the actual script is not used, however it is important that |
| 12747 | // all generated code keeps references to this particular instance of script. |
| 12748 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 12749 | // each function with all its descendant is always stored in a continues range |
| 12750 | // 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] | 12751 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditGatherCompileInfo) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12752 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12753 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12754 | CONVERT_ARG_CHECKED(JSValue, script, 0); |
| 12755 | CONVERT_ARG_HANDLE_CHECKED(String, source, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12756 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 12757 | |
| 12758 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 12759 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12760 | if (isolate->has_pending_exception()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12761 | return Failure::Exception(); |
| 12762 | } |
| 12763 | |
| 12764 | return result; |
| 12765 | } |
| 12766 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12767 | // Changes the source of the script to a new_source. |
| 12768 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 12769 | // the script with its original source and sends notification to debugger. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12770 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12771 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12772 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12773 | CONVERT_ARG_CHECKED(JSValue, original_script_value, 0); |
| 12774 | CONVERT_ARG_HANDLE_CHECKED(String, new_source, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12775 | Handle<Object> old_script_name(args[2], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12776 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12777 | RUNTIME_ASSERT(original_script_value->value()->IsScript()); |
| 12778 | Handle<Script> original_script(Script::cast(original_script_value->value())); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12779 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12780 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 12781 | new_source, |
| 12782 | old_script_name); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12783 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12784 | if (old_script->IsScript()) { |
| 12785 | Handle<Script> script_handle(Script::cast(old_script)); |
| 12786 | return *(GetScriptWrapper(script_handle)); |
| 12787 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12788 | return isolate->heap()->null_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12789 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12790 | } |
| 12791 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12792 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12793 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSourceUpdated) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12794 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12795 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12796 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_info, 0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12797 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 12798 | } |
| 12799 | |
| 12800 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12801 | // Replaces code of SharedFunctionInfo with a new one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12802 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceFunctionCode) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12803 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12804 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12805 | CONVERT_ARG_HANDLE_CHECKED(JSArray, new_compile_info, 0); |
| 12806 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_info, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12807 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 12808 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12809 | } |
| 12810 | |
| 12811 | // Connects SharedFunctionInfo to another script. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12812 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSetScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12813 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12814 | HandleScope scope(isolate); |
| 12815 | Handle<Object> function_object(args[0], isolate); |
| 12816 | Handle<Object> script_object(args[1], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12817 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12818 | if (function_object->IsJSValue()) { |
| 12819 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 12820 | if (script_object->IsJSValue()) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12821 | RUNTIME_ASSERT(JSValue::cast(*script_object)->value()->IsScript()); |
| 12822 | Script* script = Script::cast(JSValue::cast(*script_object)->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12823 | script_object = Handle<Object>(script, isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12824 | } |
| 12825 | |
| 12826 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 12827 | } else { |
| 12828 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 12829 | // and we check it in this function. |
| 12830 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12831 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12832 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12833 | } |
| 12834 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12835 | |
| 12836 | // In a code of a parent function replaces original function as embedded object |
| 12837 | // with a substitution one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12838 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceRefToNestedFunction) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12839 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12840 | HandleScope scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12841 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12842 | CONVERT_ARG_HANDLE_CHECKED(JSValue, parent_wrapper, 0); |
| 12843 | CONVERT_ARG_HANDLE_CHECKED(JSValue, orig_wrapper, 1); |
| 12844 | CONVERT_ARG_HANDLE_CHECKED(JSValue, subst_wrapper, 2); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12845 | |
| 12846 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 12847 | subst_wrapper); |
| 12848 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12849 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 12850 | } |
| 12851 | |
| 12852 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12853 | // Updates positions of a shared function info (first parameter) according |
| 12854 | // to script source change. Text change is described in second parameter as |
| 12855 | // array of groups of 3 numbers: |
| 12856 | // (change_begin, change_end, change_end_new_position). |
| 12857 | // 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] | 12858 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditPatchFunctionPositions) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12859 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12860 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12861 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_array, 0); |
| 12862 | CONVERT_ARG_HANDLE_CHECKED(JSArray, position_change_array, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12863 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 12864 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12865 | } |
| 12866 | |
| 12867 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12868 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 12869 | // checks that none of them have activations on stacks (of any thread). |
| 12870 | // Returns array of the same length with corresponding results of |
| 12871 | // LiveEdit::FunctionPatchabilityStatus type. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12872 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCheckAndDropActivations) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12873 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12874 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12875 | CONVERT_ARG_HANDLE_CHECKED(JSArray, shared_array, 0); |
| 12876 | CONVERT_BOOLEAN_ARG_CHECKED(do_drop, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12877 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12878 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12879 | } |
| 12880 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 12881 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 12882 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 12883 | // of diff chunks. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12884 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCompareStrings) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12885 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12886 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12887 | CONVERT_ARG_HANDLE_CHECKED(String, s1, 0); |
| 12888 | CONVERT_ARG_HANDLE_CHECKED(String, s2, 1); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12889 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 12890 | return *LiveEdit::CompareStrings(s1, s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12891 | } |
| 12892 | |
| 12893 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12894 | // A testing entry. Returns statement position which is the closest to |
| 12895 | // source_position. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12896 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionCodePositionFromSource) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12897 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12898 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12899 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12900 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 12901 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12902 | Handle<Code> code(function->code(), isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12903 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12904 | if (code->kind() != Code::FUNCTION && |
| 12905 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12906 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12907 | } |
| 12908 | |
| 12909 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12910 | int closest_pc = 0; |
| 12911 | int distance = kMaxInt; |
| 12912 | while (!it.done()) { |
| 12913 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 12914 | // Check if this break point is closer that what was previously found. |
| 12915 | if (source_position <= statement_position && |
| 12916 | statement_position - source_position < distance) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 12917 | closest_pc = |
| 12918 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 12919 | distance = statement_position - source_position; |
| 12920 | // Check whether we can't get any closer. |
| 12921 | if (distance == 0) break; |
| 12922 | } |
| 12923 | it.next(); |
| 12924 | } |
| 12925 | |
| 12926 | return Smi::FromInt(closest_pc); |
| 12927 | } |
| 12928 | |
| 12929 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12930 | // Calls specified function with or without entering the debugger. |
| 12931 | // This is used in unit tests to run code as if debugger is entered or simply |
| 12932 | // to have a stack with C++ frame in the middle. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12933 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ExecuteInDebugContext) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12934 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12935 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12936 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
| 12937 | CONVERT_BOOLEAN_ARG_CHECKED(without_debugger, 1); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12938 | |
| 12939 | Handle<Object> result; |
| 12940 | bool pending_exception; |
| 12941 | { |
| 12942 | if (without_debugger) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12943 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12944 | &pending_exception); |
| 12945 | } else { |
| 12946 | EnterDebugger enter_debugger; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12947 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 12948 | &pending_exception); |
| 12949 | } |
| 12950 | } |
| 12951 | if (!pending_exception) { |
| 12952 | return *result; |
| 12953 | } else { |
| 12954 | return Failure::Exception(); |
| 12955 | } |
| 12956 | } |
| 12957 | |
| 12958 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12959 | // Sets a v8 flag. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12960 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFlags) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 12961 | CONVERT_ARG_CHECKED(String, arg, 0); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 12962 | SmartArrayPointer<char> flags = |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12963 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 12964 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12965 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12966 | } |
| 12967 | |
| 12968 | |
| 12969 | // Performs a GC. |
| 12970 | // Presently, it only does a full GC. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12971 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectGarbage) { |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 12972 | isolate->heap()->CollectAllGarbage(true, "%CollectGarbage"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12973 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12974 | } |
| 12975 | |
| 12976 | |
| 12977 | // Gets the current heap usage. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12978 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHeapUsage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12979 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12980 | if (!Smi::IsValid(usage)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12981 | return *isolate->factory()->NewNumberFromInt(usage); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 12982 | } |
| 12983 | return Smi::FromInt(usage); |
| 12984 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12985 | |
| 12986 | |
| 12987 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12988 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLOLEnabled) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12989 | #ifdef LIVE_OBJECT_LIST |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12990 | return isolate->heap()->true_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12991 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12992 | return isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12993 | #endif |
| 12994 | } |
| 12995 | |
| 12996 | |
| 12997 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12998 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CaptureLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12999 | #ifdef LIVE_OBJECT_LIST |
| 13000 | return LiveObjectList::Capture(); |
| 13001 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13002 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13003 | #endif |
| 13004 | } |
| 13005 | |
| 13006 | |
| 13007 | // Deletes the specified live object list. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13008 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13009 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13010 | CONVERT_SMI_ARG_CHECKED(id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13011 | bool success = LiveObjectList::Delete(id); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 13012 | return isolate->heap()->ToBoolean(success); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13013 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13014 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13015 | #endif |
| 13016 | } |
| 13017 | |
| 13018 | |
| 13019 | // Generates the response to a debugger request for a dump of the objects |
| 13020 | // contained in the difference between the captured live object lists |
| 13021 | // specified by id1 and id2. |
| 13022 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 13023 | // dumped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13024 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DumpLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13025 | #ifdef LIVE_OBJECT_LIST |
| 13026 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13027 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 13028 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
| 13029 | CONVERT_SMI_ARG_CHECKED(start, 2); |
| 13030 | CONVERT_SMI_ARG_CHECKED(count, 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13031 | CONVERT_ARG_HANDLE_CHECKED(JSObject, filter_obj, 4); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13032 | EnterDebugger enter_debugger; |
| 13033 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 13034 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13035 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13036 | #endif |
| 13037 | } |
| 13038 | |
| 13039 | |
| 13040 | // Gets the specified object as requested by the debugger. |
| 13041 | // 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] | 13042 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13043 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13044 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13045 | Object* result = LiveObjectList::GetObj(obj_id); |
| 13046 | return result; |
| 13047 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13048 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13049 | #endif |
| 13050 | } |
| 13051 | |
| 13052 | |
| 13053 | // Gets the obj id for the specified address if valid. |
| 13054 | // 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] | 13055 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjId) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13056 | #ifdef LIVE_OBJECT_LIST |
| 13057 | HandleScope scope; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13058 | CONVERT_ARG_HANDLE_CHECKED(String, address, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13059 | Object* result = LiveObjectList::GetObjId(address); |
| 13060 | return result; |
| 13061 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13062 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13063 | #endif |
| 13064 | } |
| 13065 | |
| 13066 | |
| 13067 | // Gets the retainers that references the specified object alive. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13068 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjRetainers) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13069 | #ifdef LIVE_OBJECT_LIST |
| 13070 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13071 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13072 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 13073 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 13074 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 13075 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13076 | CONVERT_ARG_HANDLE_CHECKED(JSObject, filter_obj, 5); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13077 | |
| 13078 | Handle<JSObject> instance_filter; |
| 13079 | if (args[1]->IsJSObject()) { |
| 13080 | instance_filter = args.at<JSObject>(1); |
| 13081 | } |
| 13082 | bool verbose = false; |
| 13083 | if (args[2]->IsBoolean()) { |
| 13084 | verbose = args[2]->IsTrue(); |
| 13085 | } |
| 13086 | int start = 0; |
| 13087 | if (args[3]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13088 | start = args.smi_at(3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13089 | } |
| 13090 | int limit = Smi::kMaxValue; |
| 13091 | if (args[4]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13092 | limit = args.smi_at(4); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13093 | } |
| 13094 | |
| 13095 | return LiveObjectList::GetObjRetainers(obj_id, |
| 13096 | instance_filter, |
| 13097 | verbose, |
| 13098 | start, |
| 13099 | limit, |
| 13100 | filter_obj); |
| 13101 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13102 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13103 | #endif |
| 13104 | } |
| 13105 | |
| 13106 | |
| 13107 | // Gets the reference path between 2 objects. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13108 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLPath) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13109 | #ifdef LIVE_OBJECT_LIST |
| 13110 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13111 | CONVERT_SMI_ARG_CHECKED(obj_id1, 0); |
| 13112 | CONVERT_SMI_ARG_CHECKED(obj_id2, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13113 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 13114 | |
| 13115 | Handle<JSObject> instance_filter; |
| 13116 | if (args[2]->IsJSObject()) { |
| 13117 | instance_filter = args.at<JSObject>(2); |
| 13118 | } |
| 13119 | |
| 13120 | Object* result = |
| 13121 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 13122 | return result; |
| 13123 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13124 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13125 | #endif |
| 13126 | } |
| 13127 | |
| 13128 | |
| 13129 | // Generates the response to a debugger request for a list of all |
| 13130 | // previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13131 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InfoLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13132 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13133 | CONVERT_SMI_ARG_CHECKED(start, 0); |
| 13134 | CONVERT_SMI_ARG_CHECKED(count, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13135 | return LiveObjectList::Info(start, count); |
| 13136 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13137 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13138 | #endif |
| 13139 | } |
| 13140 | |
| 13141 | |
| 13142 | // Gets a dump of the specified object as requested by the debugger. |
| 13143 | // 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] | 13144 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrintLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13145 | #ifdef LIVE_OBJECT_LIST |
| 13146 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13147 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13148 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 13149 | return result; |
| 13150 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13151 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13152 | #endif |
| 13153 | } |
| 13154 | |
| 13155 | |
| 13156 | // Resets and releases all previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13157 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ResetLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13158 | #ifdef LIVE_OBJECT_LIST |
| 13159 | LiveObjectList::Reset(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13160 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13161 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13162 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13163 | #endif |
| 13164 | } |
| 13165 | |
| 13166 | |
| 13167 | // Generates the response to a debugger request for a summary of the types |
| 13168 | // of objects in the difference between the captured live object lists |
| 13169 | // specified by id1 and id2. |
| 13170 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 13171 | // summarized. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13172 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SummarizeLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13173 | #ifdef LIVE_OBJECT_LIST |
| 13174 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13175 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 13176 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13177 | CONVERT_ARG_HANDLE_CHECKED(JSObject, filter_obj, 2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13178 | |
| 13179 | EnterDebugger enter_debugger; |
| 13180 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 13181 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13182 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 13183 | #endif |
| 13184 | } |
| 13185 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13186 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 13187 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13188 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13189 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerResume) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13190 | NoHandleAllocation ha; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 13191 | v8::V8::ResumeProfiler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13192 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13193 | } |
| 13194 | |
| 13195 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13196 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerPause) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13197 | NoHandleAllocation ha; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 13198 | v8::V8::PauseProfiler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13199 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 13200 | } |
| 13201 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 13202 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13203 | // Finds the script object from the script data. NOTE: This operation uses |
| 13204 | // heap traversal to find the function generated for the source position |
| 13205 | // for the requested break point. For lazily compiled functions several heap |
| 13206 | // traversals might be required rendering this operation as a rather slow |
| 13207 | // operation. However for setting break points which is normally done through |
| 13208 | // some kind of user interaction the performance is not crucial. |
| 13209 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 13210 | Handle<String> script_name) { |
| 13211 | // Scan the heap for Script objects to find the script with the requested |
| 13212 | // script data. |
| 13213 | Handle<Script> script; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 13214 | script_name->GetHeap()->EnsureHeapIsIterable(); |
| 13215 | AssertNoAllocation no_allocation_during_heap_iteration; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13216 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13217 | HeapObject* obj = NULL; |
| 13218 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13219 | // If a script is found check if it has the script data requested. |
| 13220 | if (obj->IsScript()) { |
| 13221 | if (Script::cast(obj)->name()->IsString()) { |
| 13222 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 13223 | script = Handle<Script>(Script::cast(obj)); |
| 13224 | } |
| 13225 | } |
| 13226 | } |
| 13227 | } |
| 13228 | |
| 13229 | // 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] | 13230 | if (script.is_null()) return FACTORY->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13231 | |
| 13232 | // Return the script found. |
| 13233 | return GetScriptWrapper(script); |
| 13234 | } |
| 13235 | |
| 13236 | |
| 13237 | // Get the script object from script data. NOTE: Regarding performance |
| 13238 | // see the NOTE for GetScriptFromScriptData. |
| 13239 | // 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] | 13240 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13241 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13242 | |
| 13243 | ASSERT(args.length() == 1); |
| 13244 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13245 | CONVERT_ARG_CHECKED(String, script_name, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13246 | |
| 13247 | // Find the requested script. |
| 13248 | Handle<Object> result = |
| 13249 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 13250 | return *result; |
| 13251 | } |
| 13252 | |
| 13253 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13254 | // Determines whether the given stack frame should be displayed in |
| 13255 | // a stack trace. The caller is the error constructor that asked |
| 13256 | // for the stack trace to be collected. The first time a construct |
| 13257 | // call to this function is encountered it is skipped. The seen_caller |
| 13258 | // in/out parameter is used to remember if the caller has been seen |
| 13259 | // yet. |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 13260 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, |
| 13261 | Object* caller, |
| 13262 | bool* seen_caller) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13263 | // Only display JS frames. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13264 | if (!raw_frame->is_java_script()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13265 | return false; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13266 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13267 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 13268 | Object* raw_fun = frame->function(); |
| 13269 | // Not sure when this can happen but skip it just in case. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13270 | if (!raw_fun->IsJSFunction()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13271 | return false; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13272 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13273 | if ((raw_fun == caller) && !(*seen_caller)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13274 | *seen_caller = true; |
| 13275 | return false; |
| 13276 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 13277 | // Skip all frames until we've seen the caller. |
| 13278 | if (!(*seen_caller)) return false; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13279 | // Also, skip non-visible built-in functions and any call with the builtins |
| 13280 | // object as receiver, so as to not reveal either the builtins object or |
| 13281 | // an internal function. |
| 13282 | // The --builtins-in-stack-traces command line flag allows including |
| 13283 | // internal call sites in the stack trace for debugging purposes. |
| 13284 | if (!FLAG_builtins_in_stack_traces) { |
| 13285 | JSFunction* fun = JSFunction::cast(raw_fun); |
| 13286 | if (frame->receiver()->IsJSBuiltinsObject() || |
| 13287 | (fun->IsBuiltin() && !fun->shared()->native())) { |
| 13288 | return false; |
| 13289 | } |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 13290 | } |
| 13291 | return true; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13292 | } |
| 13293 | |
| 13294 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 13295 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 13296 | // element segments each containing a receiver, function, code and |
| 13297 | // native code offset. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13298 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectStackTrace) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 13299 | ASSERT_EQ(args.length(), 3); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13300 | CONVERT_ARG_HANDLE_CHECKED(JSObject, error_object, 0); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 13301 | Handle<Object> caller = args.at<Object>(1); |
| 13302 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[2]); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13303 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13304 | HandleScope scope(isolate); |
| 13305 | Factory* factory = isolate->factory(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13306 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 13307 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 13308 | int initial_size = Min(limit, 10); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13309 | Handle<FixedArray> elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13310 | factory->NewFixedArrayWithHoles(initial_size * 4); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13311 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 13312 | StackFrameIterator iter(isolate); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13313 | // If the caller parameter is a function we skip frames until we're |
| 13314 | // under it before starting to collect. |
| 13315 | bool seen_caller = !caller->IsJSFunction(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13316 | int cursor = 0; |
| 13317 | int frames_seen = 0; |
| 13318 | while (!iter.done() && frames_seen < limit) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13319 | StackFrame* raw_frame = iter.frame(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 13320 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13321 | frames_seen++; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13322 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 13323 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 13324 | // function. |
| 13325 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 13326 | frame->Summarize(&frames); |
| 13327 | for (int i = frames.length() - 1; i >= 0; i--) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13328 | if (cursor + 4 > elements->length()) { |
| 13329 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 13330 | Handle<FixedArray> new_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13331 | factory->NewFixedArrayWithHoles(new_capacity); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13332 | for (int i = 0; i < cursor; i++) { |
| 13333 | new_elements->set(i, elements->get(i)); |
| 13334 | } |
| 13335 | elements = new_elements; |
| 13336 | } |
| 13337 | ASSERT(cursor + 4 <= elements->length()); |
| 13338 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 13339 | Handle<Object> recv = frames[i].receiver(); |
| 13340 | Handle<JSFunction> fun = frames[i].function(); |
| 13341 | Handle<Code> code = frames[i].code(); |
| 13342 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13343 | elements->set(cursor++, *recv); |
| 13344 | elements->set(cursor++, *fun); |
| 13345 | elements->set(cursor++, *code); |
| 13346 | elements->set(cursor++, *offset); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13347 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13348 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 13349 | iter.Advance(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13350 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13351 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 13352 | // Capture and attach a more detailed stack trace if necessary. |
| 13353 | isolate->CaptureAndSetCurrentStackTraceFor(error_object); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 13354 | result->set_length(Smi::FromInt(cursor)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13355 | return *result; |
| 13356 | } |
| 13357 | |
| 13358 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13359 | // Returns V8 version as a string. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13360 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetV8Version) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13361 | ASSERT_EQ(args.length(), 0); |
| 13362 | |
| 13363 | NoHandleAllocation ha; |
| 13364 | |
| 13365 | const char* version_string = v8::V8::GetVersion(); |
| 13366 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13367 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 13368 | NOT_TENURED); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 13369 | } |
| 13370 | |
| 13371 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13372 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Abort) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13373 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13374 | OS::PrintError("abort: %s\n", |
| 13375 | reinterpret_cast<char*>(args[0]) + args.smi_at(1)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13376 | isolate->PrintStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13377 | OS::Abort(); |
| 13378 | UNREACHABLE(); |
| 13379 | return NULL; |
| 13380 | } |
| 13381 | |
| 13382 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13383 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFromCache) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13384 | // This is only called from codegen, so checks might be more lax. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13385 | CONVERT_ARG_CHECKED(JSFunctionResultCache, cache, 0); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13386 | Object* key = args[1]; |
| 13387 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13388 | int finger_index = cache->finger_index(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13389 | Object* o = cache->get(finger_index); |
| 13390 | if (o == key) { |
| 13391 | // The fastest case: hit the same place again. |
| 13392 | return cache->get(finger_index + 1); |
| 13393 | } |
| 13394 | |
| 13395 | for (int i = finger_index - 2; |
| 13396 | i >= JSFunctionResultCache::kEntriesIndex; |
| 13397 | i -= 2) { |
| 13398 | o = cache->get(i); |
| 13399 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13400 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13401 | return cache->get(i + 1); |
| 13402 | } |
| 13403 | } |
| 13404 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13405 | int size = cache->size(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13406 | ASSERT(size <= cache->length()); |
| 13407 | |
| 13408 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 13409 | o = cache->get(i); |
| 13410 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13411 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13412 | return cache->get(i + 1); |
| 13413 | } |
| 13414 | } |
| 13415 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13416 | // 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] | 13417 | HandleScope scope(isolate); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13418 | |
| 13419 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 13420 | Handle<Object> key_handle(key); |
| 13421 | Handle<Object> value; |
| 13422 | { |
| 13423 | Handle<JSFunction> factory(JSFunction::cast( |
| 13424 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 13425 | // TODO(antonm): consider passing a receiver when constructing a cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13426 | Handle<Object> receiver(isolate->global_context()->global()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13427 | // This handle is nor shared, nor used later, so it's safe. |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 13428 | Handle<Object> argv[] = { key_handle }; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 13429 | bool pending_exception; |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13430 | value = Execution::Call(factory, |
| 13431 | receiver, |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 13432 | ARRAY_SIZE(argv), |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13433 | argv, |
| 13434 | &pending_exception); |
| 13435 | if (pending_exception) return Failure::Exception(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13436 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13437 | |
| 13438 | #ifdef DEBUG |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13439 | if (FLAG_verify_heap) { |
| 13440 | cache_handle->JSFunctionResultCacheVerify(); |
| 13441 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13442 | #endif |
| 13443 | |
| 13444 | // Function invocation may have cleared the cache. Reread all the data. |
| 13445 | finger_index = cache_handle->finger_index(); |
| 13446 | size = cache_handle->size(); |
| 13447 | |
| 13448 | // If we have spare room, put new data into it, otherwise evict post finger |
| 13449 | // entry which is likely to be the least recently used. |
| 13450 | int index = -1; |
| 13451 | if (size < cache_handle->length()) { |
| 13452 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 13453 | index = size; |
| 13454 | } else { |
| 13455 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 13456 | if (index == cache_handle->length()) { |
| 13457 | index = JSFunctionResultCache::kEntriesIndex; |
| 13458 | } |
| 13459 | } |
| 13460 | |
| 13461 | ASSERT(index % 2 == 0); |
| 13462 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 13463 | ASSERT(index < cache_handle->length()); |
| 13464 | |
| 13465 | cache_handle->set(index, *key_handle); |
| 13466 | cache_handle->set(index + 1, *value); |
| 13467 | cache_handle->set_finger_index(index); |
| 13468 | |
| 13469 | #ifdef DEBUG |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 13470 | if (FLAG_verify_heap) { |
| 13471 | cache_handle->JSFunctionResultCacheVerify(); |
| 13472 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 13473 | #endif |
| 13474 | |
| 13475 | return *value; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 13476 | } |
| 13477 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13478 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13479 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewMessageObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13480 | HandleScope scope(isolate); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13481 | CONVERT_ARG_HANDLE_CHECKED(String, type, 0); |
| 13482 | CONVERT_ARG_HANDLE_CHECKED(JSArray, arguments, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13483 | return *isolate->factory()->NewJSMessageObject( |
| 13484 | type, |
| 13485 | arguments, |
| 13486 | 0, |
| 13487 | 0, |
| 13488 | isolate->factory()->undefined_value(), |
| 13489 | isolate->factory()->undefined_value(), |
| 13490 | isolate->factory()->undefined_value()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13491 | } |
| 13492 | |
| 13493 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13494 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetType) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13495 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13496 | return message->type(); |
| 13497 | } |
| 13498 | |
| 13499 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13500 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetArguments) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13501 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13502 | return message->arguments(); |
| 13503 | } |
| 13504 | |
| 13505 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13506 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetStartPosition) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13507 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13508 | return Smi::FromInt(message->start_position()); |
| 13509 | } |
| 13510 | |
| 13511 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetScript) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13513 | CONVERT_ARG_CHECKED(JSMessageObject, message, 0); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 13514 | return message->script(); |
| 13515 | } |
| 13516 | |
| 13517 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 13518 | #ifdef DEBUG |
| 13519 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 13520 | // Exclude the code in release mode. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13521 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ListNatives) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 13522 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13523 | HandleScope scope; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13524 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 13525 | int entry_count = 0 |
| 13526 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 13527 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 13528 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 13529 | #undef COUNT_ENTRY |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13530 | Factory* factory = isolate->factory(); |
| 13531 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13532 | int index = 0; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13533 | bool inline_runtime_functions = false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 13534 | #define ADD_ENTRY(Name, argc, ressize) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13535 | { \ |
| 13536 | HandleScope inner; \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13537 | Handle<String> name; \ |
| 13538 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 13539 | if (inline_runtime_functions) { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13540 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13541 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 13542 | } else { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13543 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13544 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 13545 | } \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13546 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13547 | pair_elements->set(0, *name); \ |
| 13548 | pair_elements->set(1, Smi::FromInt(argc)); \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13549 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13550 | elements->set(index++, *pair); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13551 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13552 | inline_runtime_functions = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13553 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13554 | inline_runtime_functions = true; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13555 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 13556 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13557 | #undef ADD_ENTRY |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 13558 | ASSERT_EQ(index, entry_count); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13559 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13560 | return *result; |
| 13561 | } |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 13562 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13563 | |
| 13564 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13565 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Log) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 13566 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13567 | CONVERT_ARG_CHECKED(String, format, 0); |
| 13568 | CONVERT_ARG_CHECKED(JSArray, elms, 1); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 13569 | String::FlatContent format_content = format->GetFlatContent(); |
| 13570 | RUNTIME_ASSERT(format_content.IsAscii()); |
| 13571 | Vector<const char> chars = format_content.ToAsciiVector(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13572 | LOGGER->LogRuntime(chars, elms); |
| 13573 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 13574 | } |
| 13575 | |
| 13576 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 13577 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IS_VAR) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13578 | UNREACHABLE(); // implemented as macro in the parser |
| 13579 | return NULL; |
| 13580 | } |
| 13581 | |
| 13582 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 13583 | #define ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(Name) \ |
| 13584 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Has##Name) { \ |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13585 | CONVERT_ARG_CHECKED(JSObject, obj, 0); \ |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 13586 | return isolate->heap()->ToBoolean(obj->Has##Name()); \ |
| 13587 | } |
| 13588 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 13589 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastSmiOnlyElements) |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 13590 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastElements) |
| 13591 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastDoubleElements) |
| 13592 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(DictionaryElements) |
| 13593 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalPixelElements) |
| 13594 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalArrayElements) |
| 13595 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalByteElements) |
| 13596 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedByteElements) |
| 13597 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalShortElements) |
| 13598 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedShortElements) |
| 13599 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalIntElements) |
| 13600 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedIntElements) |
| 13601 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalFloatElements) |
| 13602 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalDoubleElements) |
| 13603 | |
| 13604 | #undef ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION |
| 13605 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 13606 | |
| 13607 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HaveSameMap) { |
| 13608 | ASSERT(args.length() == 2); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 13609 | CONVERT_ARG_CHECKED(JSObject, obj1, 0); |
| 13610 | CONVERT_ARG_CHECKED(JSObject, obj2, 1); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 13611 | return isolate->heap()->ToBoolean(obj1->map() == obj2->map()); |
| 13612 | } |
| 13613 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13614 | // ---------------------------------------------------------------------------- |
| 13615 | // Implementation of Runtime |
| 13616 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13617 | #define F(name, number_of_args, result_size) \ |
| 13618 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 13619 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13620 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13621 | |
| 13622 | #define I(name, number_of_args, result_size) \ |
| 13623 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 13624 | "_" #name, NULL, number_of_args, result_size }, |
| 13625 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13626 | static const Runtime::Function kIntrinsicFunctions[] = { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13627 | RUNTIME_FUNCTION_LIST(F) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13628 | INLINE_FUNCTION_LIST(I) |
| 13629 | INLINE_RUNTIME_FUNCTION_LIST(I) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13630 | }; |
| 13631 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13632 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13633 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 13634 | Object* dictionary) { |
| 13635 | ASSERT(Isolate::Current()->heap() == heap); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13636 | ASSERT(dictionary != NULL); |
| 13637 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 13638 | for (int i = 0; i < kNumFunctions; ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13639 | Object* name_symbol; |
| 13640 | { MaybeObject* maybe_name_symbol = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13641 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13642 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 13643 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13644 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 13645 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 13646 | String::cast(name_symbol), |
| 13647 | Smi::FromInt(i), |
| 13648 | PropertyDetails(NONE, NORMAL)); |
| 13649 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 13650 | // Non-recoverable failure. Calling code must restart heap |
| 13651 | // initialization. |
| 13652 | return maybe_dictionary; |
| 13653 | } |
| 13654 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13655 | } |
| 13656 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13657 | } |
| 13658 | |
| 13659 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13660 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 13661 | Heap* heap = name->GetHeap(); |
| 13662 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13663 | if (entry != kNotFound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13664 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13665 | int function_index = Smi::cast(smi_index)->value(); |
| 13666 | return &(kIntrinsicFunctions[function_index]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13667 | } |
| 13668 | return NULL; |
| 13669 | } |
| 13670 | |
| 13671 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13672 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 13673 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 13674 | } |
| 13675 | |
| 13676 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13677 | void Runtime::PerformGC(Object* result) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 13678 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13679 | Failure* failure = Failure::cast(result); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13680 | if (failure->IsRetryAfterGC()) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 13681 | if (isolate->heap()->new_space()->AddFreshPage()) { |
| 13682 | return; |
| 13683 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13684 | // Try to do a garbage collection; ignore it if it fails. The C |
| 13685 | // entry stub will throw an out-of-memory exception in that case. |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 13686 | isolate->heap()->CollectGarbage(failure->allocation_space(), |
| 13687 | "Runtime::PerformGC"); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13688 | } else { |
| 13689 | // Handle last resort GC and make sure to allow future allocations |
| 13690 | // to grow the heap without causing GCs (if possible). |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 13691 | isolate->counters()->gc_last_resort_from_js()->Increment(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 13692 | isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 13693 | "Runtime::PerformGC"); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 13694 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 13695 | } |
| 13696 | |
| 13697 | |
| 13698 | } } // namespace v8::internal |