ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "accessors.h" |
| 33 | #include "api.h" |
| 34 | #include "arguments.h" |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 35 | #include "codegen.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 36 | #include "compilation-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "compiler.h" |
| 38 | #include "cpu.h" |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 39 | #include "dateparser-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 40 | #include "debug.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 41 | #include "deoptimizer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | #include "execution.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 43 | #include "global-handles.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | #include "jsregexp.h" |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 45 | #include "json-parser.h" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 46 | #include "liveedit.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 47 | #include "liveobjectlist-inl.h" |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 48 | #include "misc-intrinsics.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 49 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 51 | #include "runtime-profiler.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 52 | #include "runtime.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | #include "scopeinfo.h" |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 54 | #include "smart-pointer.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 55 | #include "string-search.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 56 | #include "stub-cache.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 57 | #include "v8threads.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 58 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 60 | namespace v8 { |
| 61 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | |
| 63 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 64 | #define RUNTIME_ASSERT(value) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 65 | if (!(value)) return isolate->ThrowIllegalOperation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | |
| 67 | // Cast the given object to a value of the specified type and store |
| 68 | // it in a variable with the given name. If the object is not of the |
| 69 | // expected type call IllegalOperation and return. |
| 70 | #define CONVERT_CHECKED(Type, name, obj) \ |
| 71 | RUNTIME_ASSERT(obj->Is##Type()); \ |
| 72 | Type* name = Type::cast(obj); |
| 73 | |
| 74 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 75 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 76 | Handle<Type> name = args.at<Type>(index); |
| 77 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 78 | // Cast the given object to a boolean and store it in a variable with |
| 79 | // the given name. If the object is not a boolean call IllegalOperation |
| 80 | // and return. |
| 81 | #define CONVERT_BOOLEAN_CHECKED(name, obj) \ |
| 82 | RUNTIME_ASSERT(obj->IsBoolean()); \ |
| 83 | bool name = (obj)->IsTrue(); |
| 84 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 85 | // Cast the given argument to a Smi and store its value in an int variable |
| 86 | // 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] | 87 | // and return. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 88 | #define CONVERT_SMI_ARG_CHECKED(name, index) \ |
| 89 | RUNTIME_ASSERT(args[index]->IsSmi()); \ |
| 90 | int name = args.smi_at(index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 91 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 92 | // Cast the given argument to a double and store it in a variable with |
| 93 | // 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] | 94 | // the number not-a-number) call IllegalOperation and return. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 95 | #define CONVERT_DOUBLE_ARG_CHECKED(name, index) \ |
| 96 | RUNTIME_ASSERT(args[index]->IsNumber()); \ |
| 97 | double name = args.number_at(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | |
| 99 | // Call the specified converter on the object *comand store the result in |
| 100 | // a variable of the specified type with the given name. If the |
| 101 | // object is not a Number call IllegalOperation and return. |
| 102 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 103 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 104 | type name = NumberTo##Type(obj); |
| 105 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 107 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 108 | JSObject* boilerplate) { |
| 109 | StackLimitCheck check(isolate); |
| 110 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 113 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 114 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 115 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 116 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 117 | JSObject* copy = JSObject::cast(result); |
| 118 | |
| 119 | // Deep copy local properties. |
| 120 | if (copy->HasFastProperties()) { |
| 121 | FixedArray* properties = copy->properties(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 122 | for (int i = 0; i < properties->length(); i++) { |
| 123 | Object* value = properties->get(i); |
| 124 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 125 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 126 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 127 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 128 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 129 | properties->set(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 132 | int nof = copy->map()->inobject_properties(); |
| 133 | for (int i = 0; i < nof; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 134 | Object* value = copy->InObjectPropertyAt(i); |
| 135 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 136 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 137 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 138 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 139 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 140 | copy->InObjectPropertyAtPut(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 144 | { MaybeObject* maybe_result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 145 | heap->AllocateFixedArray(copy->NumberOfLocalProperties(NONE)); |
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 | FixedArray* names = FixedArray::cast(result); |
| 149 | copy->GetLocalPropertyNames(names, 0); |
| 150 | for (int i = 0; i < names->length(); i++) { |
| 151 | ASSERT(names->get(i)->IsString()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 152 | String* key_string = String::cast(names->get(i)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 153 | PropertyAttributes attributes = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 154 | copy->GetLocalPropertyAttribute(key_string); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 155 | // Only deep copy fields from the object literal expression. |
| 156 | // In particular, don't try to copy the length attribute of |
| 157 | // an array. |
| 158 | if (attributes != NONE) continue; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 159 | Object* value = |
| 160 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 161 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 162 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 163 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 164 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 165 | } |
| 166 | { MaybeObject* maybe_result = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 167 | // Creating object copy for literals. No strict mode needed. |
| 168 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 169 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 170 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Deep copy local elements. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 176 | // Pixel elements cannot be created using an object literal. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 177 | ASSERT(!copy->HasExternalArrayElements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 178 | switch (copy->GetElementsKind()) { |
| 179 | case JSObject::FAST_ELEMENTS: { |
| 180 | FixedArray* elements = FixedArray::cast(copy->elements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 181 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 182 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 183 | #ifdef DEBUG |
| 184 | for (int i = 0; i < elements->length(); i++) { |
| 185 | ASSERT(!elements->get(i)->IsJSObject()); |
| 186 | } |
| 187 | #endif |
| 188 | } else { |
| 189 | for (int i = 0; i < elements->length(); i++) { |
| 190 | Object* value = elements->get(i); |
| 191 | if (value->IsJSObject()) { |
| 192 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 193 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 194 | 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 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 197 | elements->set(i, result); |
| 198 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 201 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 202 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 203 | case JSObject::DICTIONARY_ELEMENTS: { |
| 204 | NumberDictionary* element_dictionary = copy->element_dictionary(); |
| 205 | int capacity = element_dictionary->Capacity(); |
| 206 | for (int i = 0; i < capacity; i++) { |
| 207 | Object* k = element_dictionary->KeyAt(i); |
| 208 | if (element_dictionary->IsKey(k)) { |
| 209 | Object* value = element_dictionary->ValueAt(i); |
| 210 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 211 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 212 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 213 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 214 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 215 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 216 | element_dictionary->ValueAtPut(i, result); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | break; |
| 221 | } |
| 222 | default: |
| 223 | UNREACHABLE(); |
| 224 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 225 | } |
| 226 | return copy; |
| 227 | } |
| 228 | |
| 229 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 230 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CloneLiteralBoilerplate) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 231 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 232 | return DeepCopyBoilerplate(isolate, boilerplate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 236 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CloneShallowLiteralBoilerplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 238 | return isolate->heap()->CopyJSObject(boilerplate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 242 | static Handle<Map> ComputeObjectLiteralMap( |
| 243 | Handle<Context> context, |
| 244 | Handle<FixedArray> constant_properties, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 245 | bool* is_result_from_cache) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 246 | Isolate* isolate = context->GetIsolate(); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 247 | int properties_length = constant_properties->length(); |
| 248 | int number_of_properties = properties_length / 2; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 249 | if (FLAG_canonicalize_object_literal_maps) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 250 | // Check that there are only symbols and array indices among keys. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 251 | int number_of_symbol_keys = 0; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 252 | for (int p = 0; p != properties_length; p += 2) { |
| 253 | Object* key = constant_properties->get(p); |
| 254 | uint32_t element_index = 0; |
| 255 | if (key->IsSymbol()) { |
| 256 | number_of_symbol_keys++; |
| 257 | } else if (key->ToArrayIndex(&element_index)) { |
| 258 | // An index key does not require space in the property backing store. |
| 259 | number_of_properties--; |
| 260 | } else { |
| 261 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 262 | // ASSERT to make sure that the if condition after the loop is false. |
| 263 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 264 | break; |
| 265 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 266 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 267 | // If we only have symbols and array indices among keys then we can |
| 268 | // use the map cache in the global context. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 269 | const int kMaxKeys = 10; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 270 | if ((number_of_symbol_keys == number_of_properties) && |
| 271 | (number_of_symbol_keys < kMaxKeys)) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 272 | // Create the fixed array with the key. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 273 | Handle<FixedArray> keys = |
| 274 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 275 | if (number_of_symbol_keys > 0) { |
| 276 | int index = 0; |
| 277 | for (int p = 0; p < properties_length; p += 2) { |
| 278 | Object* key = constant_properties->get(p); |
| 279 | if (key->IsSymbol()) { |
| 280 | keys->set(index++, key); |
| 281 | } |
| 282 | } |
| 283 | ASSERT(index == number_of_symbol_keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 284 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 285 | *is_result_from_cache = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 286 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 289 | *is_result_from_cache = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 290 | return isolate->factory()->CopyMap( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 291 | Handle<Map>(context->object_function()->initial_map()), |
| 292 | number_of_properties); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 296 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 297 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 298 | Handle<FixedArray> literals, |
| 299 | Handle<FixedArray> constant_properties); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 300 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 301 | |
| 302 | static Handle<Object> CreateObjectLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 303 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 304 | Handle<FixedArray> literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 305 | Handle<FixedArray> constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 306 | bool should_have_fast_elements, |
| 307 | bool has_function_literal) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 308 | // Get the global context from the literals array. This is the |
| 309 | // context in which the function was created and we use the object |
| 310 | // function from this context to create the object literal. We do |
| 311 | // not use the object function from the current global context |
| 312 | // because this might be the object function from another context |
| 313 | // which we should not have access to. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 314 | Handle<Context> context = |
| 315 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 316 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 317 | // In case we have function literals, we want the object to be in |
| 318 | // slow properties mode for now. We don't go in the map cache because |
| 319 | // maps with constant functions can't be shared if the functions are |
| 320 | // not the same (which is the common case). |
| 321 | bool is_result_from_cache = false; |
| 322 | Handle<Map> map = has_function_literal |
| 323 | ? Handle<Map>(context->object_function()->initial_map()) |
| 324 | : ComputeObjectLiteralMap(context, |
| 325 | constant_properties, |
| 326 | &is_result_from_cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 328 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 329 | |
| 330 | // Normalize the elements of the boilerplate to save space if needed. |
| 331 | if (!should_have_fast_elements) NormalizeElements(boilerplate); |
| 332 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 333 | // Add the constant properties to the boilerplate. |
| 334 | int length = constant_properties->length(); |
| 335 | bool should_transform = |
| 336 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 337 | if (should_transform || has_function_literal) { |
| 338 | // Normalize the properties of object to avoid n^2 behavior |
| 339 | // when extending the object multiple properties. Indicate the number of |
| 340 | // properties to be added. |
| 341 | NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
| 342 | } |
| 343 | |
| 344 | for (int index = 0; index < length; index +=2) { |
| 345 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 346 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 347 | if (value->IsFixedArray()) { |
| 348 | // The value contains the constant_properties of a |
| 349 | // simple object or array literal. |
| 350 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 351 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 352 | if (value.is_null()) return value; |
| 353 | } |
| 354 | Handle<Object> result; |
| 355 | uint32_t element_index = 0; |
| 356 | if (key->IsSymbol()) { |
| 357 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 358 | // Array index as string (uint32). |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 359 | result = SetOwnElement(boilerplate, |
| 360 | element_index, |
| 361 | value, |
| 362 | kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 363 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 364 | Handle<String> name(String::cast(*key)); |
| 365 | ASSERT(!name->AsArrayIndex(&element_index)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 366 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 367 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 369 | } else if (key->ToArrayIndex(&element_index)) { |
| 370 | // Array index (uint32). |
| 371 | result = SetOwnElement(boilerplate, |
| 372 | element_index, |
| 373 | value, |
| 374 | kNonStrictMode); |
| 375 | } else { |
| 376 | // Non-uint32 number. |
| 377 | ASSERT(key->IsNumber()); |
| 378 | double num = key->Number(); |
| 379 | char arr[100]; |
| 380 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 381 | const char* str = DoubleToCString(num, buffer); |
| 382 | Handle<String> name = |
| 383 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
| 384 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 385 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 386 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 387 | // If setting the property on the boilerplate throws an |
| 388 | // exception, the exception is converted to an empty handle in |
| 389 | // the handle based operations. In that case, we need to |
| 390 | // convert back to an exception. |
| 391 | if (result.is_null()) return result; |
| 392 | } |
| 393 | |
| 394 | // Transform to fast properties if necessary. For object literals with |
| 395 | // containing function literals we defer this operation until after all |
| 396 | // computed properties have been assigned so that we can generate |
| 397 | // constant function properties. |
| 398 | if (should_transform && !has_function_literal) { |
| 399 | TransformToFastProperties(boilerplate, |
| 400 | boilerplate->map()->unused_property_fields()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 401 | } |
| 402 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 403 | return boilerplate; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 407 | static Handle<Object> CreateArrayLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 408 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 409 | Handle<FixedArray> literals, |
| 410 | Handle<FixedArray> elements) { |
| 411 | // Create the JSArray. |
| 412 | Handle<JSFunction> constructor( |
| 413 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 414 | Handle<Object> object = isolate->factory()->NewJSObject(constructor); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 415 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 416 | const bool is_cow = |
| 417 | (elements->map() == isolate->heap()->fixed_cow_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 418 | Handle<FixedArray> copied_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 419 | is_cow ? elements : isolate->factory()->CopyFixedArray(elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 420 | |
| 421 | Handle<FixedArray> content = Handle<FixedArray>::cast(copied_elements); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 422 | if (is_cow) { |
| 423 | #ifdef DEBUG |
| 424 | // Copy-on-write arrays must be shallow (and simple). |
| 425 | for (int i = 0; i < content->length(); i++) { |
| 426 | ASSERT(!content->get(i)->IsFixedArray()); |
| 427 | } |
| 428 | #endif |
| 429 | } else { |
| 430 | for (int i = 0; i < content->length(); i++) { |
| 431 | if (content->get(i)->IsFixedArray()) { |
| 432 | // The value contains the constant_properties of a |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 433 | // simple object or array literal. |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 434 | Handle<FixedArray> fa(FixedArray::cast(content->get(i))); |
| 435 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 436 | CreateLiteralBoilerplate(isolate, literals, fa); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 437 | if (result.is_null()) return result; |
| 438 | content->set(i, *result); |
| 439 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | // Set the elements. |
| 444 | Handle<JSArray>::cast(object)->SetContent(*content); |
| 445 | return object; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 450 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 451 | Handle<FixedArray> literals, |
| 452 | Handle<FixedArray> array) { |
| 453 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 454 | const bool kHasNoFunctionLiteral = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 455 | switch (CompileTimeValue::GetType(array)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 456 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 457 | return CreateObjectLiteralBoilerplate(isolate, |
| 458 | literals, |
| 459 | elements, |
| 460 | true, |
| 461 | kHasNoFunctionLiteral); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 462 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 463 | return CreateObjectLiteralBoilerplate(isolate, |
| 464 | literals, |
| 465 | elements, |
| 466 | false, |
| 467 | kHasNoFunctionLiteral); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 468 | case CompileTimeValue::ARRAY_LITERAL: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 469 | return CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 470 | default: |
| 471 | UNREACHABLE(); |
| 472 | return Handle<Object>::null(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 477 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralBoilerplate) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 478 | // Takes a FixedArray of elements containing the literal elements of |
| 479 | // the array literal and produces JSArray with those elements. |
| 480 | // Additionally takes the literals array of the surrounding function |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 481 | // which contains the context from which to get the Array function |
| 482 | // to use for creating the array literal. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 483 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 484 | ASSERT(args.length() == 3); |
| 485 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 486 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 487 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 488 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 489 | Handle<Object> object = |
| 490 | CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 491 | if (object.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 492 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 493 | // Update the functions literal and return the boilerplate. |
| 494 | literals->set(literals_index, *object); |
| 495 | return *object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 499 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 500 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 501 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 502 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 503 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 504 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 505 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 506 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 507 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 508 | |
| 509 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 510 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 511 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 512 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 513 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 514 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 515 | should_have_fast_elements, |
| 516 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 517 | if (boilerplate.is_null()) return Failure::Exception(); |
| 518 | // Update the functions literal and return the boilerplate. |
| 519 | literals->set(literals_index, *boilerplate); |
| 520 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 521 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 525 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 526 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 527 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 528 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 529 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 530 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 531 | CONVERT_SMI_ARG_CHECKED(flags, 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 532 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 533 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 534 | |
| 535 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 536 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 537 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 538 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 539 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 540 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 541 | should_have_fast_elements, |
| 542 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 543 | if (boilerplate.is_null()) return Failure::Exception(); |
| 544 | // Update the functions literal and return the boilerplate. |
| 545 | literals->set(literals_index, *boilerplate); |
| 546 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 547 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 551 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 552 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 553 | ASSERT(args.length() == 3); |
| 554 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 555 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 556 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 557 | |
| 558 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 559 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 560 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 561 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 562 | if (boilerplate.is_null()) return Failure::Exception(); |
| 563 | // Update the functions literal and return the boilerplate. |
| 564 | literals->set(literals_index, *boilerplate); |
| 565 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 566 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 570 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 571 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 572 | ASSERT(args.length() == 3); |
| 573 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 574 | CONVERT_SMI_ARG_CHECKED(literals_index, 1); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 575 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 576 | |
| 577 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 578 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 579 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 580 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 581 | if (boilerplate.is_null()) return Failure::Exception(); |
| 582 | // Update the functions literal and return the boilerplate. |
| 583 | literals->set(literals_index, *boilerplate); |
| 584 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 585 | if (JSObject::cast(*boilerplate)->elements()->map() == |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 586 | isolate->heap()->fixed_cow_array_map()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 587 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 588 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 589 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 593 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSProxy) { |
| 594 | ASSERT(args.length() == 2); |
| 595 | Object* handler = args[0]; |
| 596 | Object* prototype = args[1]; |
| 597 | Object* used_prototype = |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 598 | prototype->IsJSReceiver() ? prototype : isolate->heap()->null_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 599 | return isolate->heap()->AllocateJSProxy(handler, used_prototype); |
| 600 | } |
| 601 | |
| 602 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 603 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsJSProxy) { |
| 604 | ASSERT(args.length() == 1); |
| 605 | Object* obj = args[0]; |
| 606 | return obj->IsJSProxy() |
| 607 | ? isolate->heap()->true_value() : isolate->heap()->false_value(); |
| 608 | } |
| 609 | |
| 610 | |
| 611 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHandler) { |
| 612 | ASSERT(args.length() == 1); |
| 613 | CONVERT_CHECKED(JSProxy, proxy, args[0]); |
| 614 | return proxy->handler(); |
| 615 | } |
| 616 | |
| 617 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 618 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClassOf) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 619 | NoHandleAllocation ha; |
| 620 | ASSERT(args.length() == 1); |
| 621 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 622 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 623 | return JSObject::cast(obj)->class_name(); |
| 624 | } |
| 625 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 626 | |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 627 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPrototype) { |
| 628 | NoHandleAllocation ha; |
| 629 | ASSERT(args.length() == 1); |
| 630 | Object* obj = args[0]; |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 631 | do { |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 632 | obj = obj->GetPrototype(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 633 | } while (obj->IsJSObject() && |
| 634 | JSObject::cast(obj)->map()->is_hidden_prototype()); |
erik.corry@gmail.com | d6076d9 | 2011-06-06 09:39:18 +0000 | [diff] [blame] | 635 | return obj; |
| 636 | } |
| 637 | |
| 638 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 639 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsInPrototypeChain) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | NoHandleAllocation ha; |
| 641 | ASSERT(args.length() == 2); |
| 642 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 643 | Object* O = args[0]; |
| 644 | Object* V = args[1]; |
| 645 | while (true) { |
| 646 | Object* prototype = V->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 647 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 648 | if (O == prototype) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | V = prototype; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 654 | // Inserts an object as the hidden prototype of another object. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 655 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetHiddenPrototype) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 656 | NoHandleAllocation ha; |
| 657 | ASSERT(args.length() == 2); |
| 658 | CONVERT_CHECKED(JSObject, jsobject, args[0]); |
| 659 | CONVERT_CHECKED(JSObject, proto, args[1]); |
| 660 | |
| 661 | // Sanity checks. The old prototype (that we are replacing) could |
| 662 | // theoretically be null, but if it is not null then check that we |
| 663 | // didn't already install a hidden prototype here. |
| 664 | RUNTIME_ASSERT(!jsobject->GetPrototype()->IsHeapObject() || |
| 665 | !HeapObject::cast(jsobject->GetPrototype())->map()->is_hidden_prototype()); |
| 666 | RUNTIME_ASSERT(!proto->map()->is_hidden_prototype()); |
| 667 | |
| 668 | // Allocate up front before we start altering state in case we get a GC. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 669 | Object* map_or_failure; |
| 670 | { MaybeObject* maybe_map_or_failure = proto->map()->CopyDropTransitions(); |
| 671 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 672 | return maybe_map_or_failure; |
| 673 | } |
| 674 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 675 | Map* new_proto_map = Map::cast(map_or_failure); |
| 676 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 677 | { MaybeObject* maybe_map_or_failure = jsobject->map()->CopyDropTransitions(); |
| 678 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 679 | return maybe_map_or_failure; |
| 680 | } |
| 681 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 682 | Map* new_map = Map::cast(map_or_failure); |
| 683 | |
| 684 | // Set proto's prototype to be the old prototype of the object. |
| 685 | new_proto_map->set_prototype(jsobject->GetPrototype()); |
| 686 | proto->set_map(new_proto_map); |
| 687 | new_proto_map->set_is_hidden_prototype(); |
| 688 | |
| 689 | // Set the object's prototype to proto. |
| 690 | new_map->set_prototype(proto); |
| 691 | jsobject->set_map(new_map); |
| 692 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 693 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 697 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsConstructCall) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 698 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 699 | ASSERT(args.length() == 0); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 700 | JavaScriptFrameIterator it(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 701 | return isolate->heap()->ToBoolean(it.frame()->IsConstructor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 705 | // Recursively traverses hidden prototypes if property is not found |
| 706 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 707 | String* name, |
| 708 | LookupResult* result) { |
| 709 | obj->LocalLookupRealNamedProperty(name, result); |
| 710 | |
| 711 | if (!result->IsProperty()) { |
| 712 | Object* proto = obj->GetPrototype(); |
| 713 | if (proto->IsJSObject() && |
| 714 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 715 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 716 | name, result); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 721 | static bool CheckAccessException(LookupResult* result, |
| 722 | v8::AccessType access_type) { |
| 723 | if (result->type() == CALLBACKS) { |
| 724 | Object* callback = result->GetCallbackObject(); |
| 725 | if (callback->IsAccessorInfo()) { |
| 726 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 727 | bool can_access = |
| 728 | (access_type == v8::ACCESS_HAS && |
| 729 | (info->all_can_read() || info->all_can_write())) || |
| 730 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 731 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 732 | return can_access; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | static bool CheckAccess(JSObject* obj, |
| 741 | String* name, |
| 742 | LookupResult* result, |
| 743 | v8::AccessType access_type) { |
| 744 | ASSERT(result->IsProperty()); |
| 745 | |
| 746 | JSObject* holder = result->holder(); |
| 747 | JSObject* current = obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 748 | Isolate* isolate = obj->GetIsolate(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 749 | while (true) { |
| 750 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 751 | !isolate->MayNamedAccess(current, name, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 752 | // Access check callback denied the access, but some properties |
| 753 | // can have a special permissions which override callbacks descision |
| 754 | // (currently see v8::AccessControl). |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | if (current == holder) { |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | current = JSObject::cast(current->GetPrototype()); |
| 763 | } |
| 764 | |
| 765 | // API callbacks can have per callback access exceptions. |
| 766 | switch (result->type()) { |
| 767 | case CALLBACKS: { |
| 768 | if (CheckAccessException(result, access_type)) { |
| 769 | return true; |
| 770 | } |
| 771 | break; |
| 772 | } |
| 773 | case INTERCEPTOR: { |
| 774 | // If the object has an interceptor, try real named properties. |
| 775 | // Overwrite the result to fetch the correct property later. |
| 776 | holder->LookupRealNamedProperty(name, result); |
| 777 | if (result->IsProperty()) { |
| 778 | if (CheckAccessException(result, access_type)) { |
| 779 | return true; |
| 780 | } |
| 781 | } |
| 782 | break; |
| 783 | } |
| 784 | default: |
| 785 | break; |
| 786 | } |
| 787 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 788 | isolate->ReportFailedAccessCheck(current, access_type); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | |
| 792 | |
| 793 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 794 | static bool CheckElementAccess(JSObject* obj, |
| 795 | uint32_t index, |
| 796 | v8::AccessType access_type) { |
| 797 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 798 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 806 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 807 | enum PropertyDescriptorIndices { |
| 808 | IS_ACCESSOR_INDEX, |
| 809 | VALUE_INDEX, |
| 810 | GETTER_INDEX, |
| 811 | SETTER_INDEX, |
| 812 | WRITABLE_INDEX, |
| 813 | ENUMERABLE_INDEX, |
| 814 | CONFIGURABLE_INDEX, |
| 815 | DESCRIPTOR_SIZE |
| 816 | }; |
| 817 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 818 | // Returns an array with the property description: |
| 819 | // if args[1] is not a property on args[0] |
| 820 | // returns undefined |
| 821 | // if args[1] is a data property on args[0] |
| 822 | // [false, value, Writeable, Enumerable, Configurable] |
| 823 | // if args[1] is an accessor on args[0] |
| 824 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 825 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOwnProperty) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 826 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 827 | Heap* heap = isolate->heap(); |
| 828 | HandleScope scope(isolate); |
| 829 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 830 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 831 | LookupResult result; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 832 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 833 | CONVERT_ARG_CHECKED(String, name, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 834 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 835 | // This could be an element. |
| 836 | uint32_t index; |
| 837 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 838 | switch (obj->HasLocalElement(index)) { |
| 839 | case JSObject::UNDEFINED_ELEMENT: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 840 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 841 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 842 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 843 | // Special handling of string objects according to ECMAScript 5 |
| 844 | // 15.5.5.2. Note that this might be a string object with elements |
| 845 | // other than the actual string value. This is covered by the |
| 846 | // subsequent cases. |
| 847 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 848 | Handle<String> str(String::cast(js_value->value())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 849 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 850 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 851 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 852 | elms->set(VALUE_INDEX, *substr); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 853 | elms->set(WRITABLE_INDEX, heap->false_value()); |
| 854 | elms->set(ENUMERABLE_INDEX, heap->false_value()); |
| 855 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 856 | return *desc; |
| 857 | } |
| 858 | |
| 859 | case JSObject::INTERCEPTED_ELEMENT: |
| 860 | case JSObject::FAST_ELEMENT: { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 861 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 862 | Handle<Object> value = GetElement(obj, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 863 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 864 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 865 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 866 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 867 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 868 | return *desc; |
| 869 | } |
| 870 | |
| 871 | case JSObject::DICTIONARY_ELEMENT: { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 872 | Handle<JSObject> holder = obj; |
| 873 | if (obj->IsJSGlobalProxy()) { |
| 874 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 875 | if (proto->IsNull()) return heap->undefined_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 876 | ASSERT(proto->IsJSGlobalObject()); |
| 877 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 878 | } |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 879 | FixedArray* elements = FixedArray::cast(holder->elements()); |
| 880 | NumberDictionary* dictionary = NULL; |
| 881 | if (elements->map() == heap->non_strict_arguments_elements_map()) { |
| 882 | dictionary = NumberDictionary::cast(elements->get(1)); |
| 883 | } else { |
| 884 | dictionary = NumberDictionary::cast(elements); |
| 885 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 886 | int entry = dictionary->FindEntry(index); |
| 887 | ASSERT(entry != NumberDictionary::kNotFound); |
| 888 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 889 | switch (details.type()) { |
| 890 | case CALLBACKS: { |
| 891 | // This is an accessor property with getter and/or setter. |
| 892 | FixedArray* callbacks = |
| 893 | FixedArray::cast(dictionary->ValueAt(entry)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 894 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 895 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
| 896 | elms->set(GETTER_INDEX, callbacks->get(0)); |
| 897 | } |
| 898 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
| 899 | elms->set(SETTER_INDEX, callbacks->get(1)); |
| 900 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 901 | break; |
| 902 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 903 | case NORMAL: { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 904 | // This is a data property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 905 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 906 | Handle<Object> value = GetElement(obj, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 907 | ASSERT(!value.is_null()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 908 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 909 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 910 | break; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 911 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 912 | default: |
| 913 | UNREACHABLE(); |
| 914 | break; |
| 915 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 916 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 917 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 918 | return *desc; |
| 919 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 923 | // Use recursive implementation to also traverse hidden prototypes |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 924 | GetOwnPropertyImplementation(*obj, *name, &result); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 925 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 926 | if (!result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 927 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 928 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 929 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 930 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 931 | return heap->false_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 932 | } |
| 933 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 934 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 935 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 936 | |
| 937 | bool is_js_accessor = (result.type() == CALLBACKS) && |
| 938 | (result.GetCallbackObject()->IsFixedArray()); |
| 939 | |
| 940 | if (is_js_accessor) { |
| 941 | // __defineGetter__/__defineSetter__ callback. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 942 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 943 | |
| 944 | FixedArray* structure = FixedArray::cast(result.GetCallbackObject()); |
| 945 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
| 946 | elms->set(GETTER_INDEX, structure->get(0)); |
| 947 | } |
| 948 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
| 949 | elms->set(SETTER_INDEX, structure->get(1)); |
| 950 | } |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 951 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 952 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 953 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 954 | |
| 955 | PropertyAttributes attrs; |
| 956 | Object* value; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 957 | // GetProperty will check access and report any violations. |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 958 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 959 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 960 | } |
| 961 | elms->set(VALUE_INDEX, value); |
| 962 | } |
| 963 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 964 | return *desc; |
| 965 | } |
| 966 | |
| 967 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 968 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PreventExtensions) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 969 | ASSERT(args.length() == 1); |
| 970 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 971 | return obj->PreventExtensions(); |
| 972 | } |
| 973 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 974 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 975 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsExtensible) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 976 | ASSERT(args.length() == 1); |
| 977 | CONVERT_CHECKED(JSObject, obj, args[0]); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 978 | if (obj->IsJSGlobalProxy()) { |
| 979 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 980 | if (proto->IsNull()) return isolate->heap()->false_value(); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 981 | ASSERT(proto->IsJSGlobalObject()); |
| 982 | obj = JSObject::cast(proto); |
| 983 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 984 | return obj->map()->is_extensible() ? isolate->heap()->true_value() |
| 985 | : isolate->heap()->false_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 989 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 990 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 991 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 992 | CONVERT_ARG_CHECKED(JSRegExp, re, 0); |
| 993 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 994 | CONVERT_ARG_CHECKED(String, flags, 2); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 995 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 996 | if (result.is_null()) return Failure::Exception(); |
| 997 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1001 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateApiFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1002 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1003 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1004 | CONVERT_ARG_CHECKED(FunctionTemplateInfo, data, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1005 | return *isolate->factory()->CreateApiFunction(data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1009 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsTemplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1010 | ASSERT(args.length() == 1); |
| 1011 | Object* arg = args[0]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1012 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1013 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1017 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetTemplateField) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1018 | ASSERT(args.length() == 2); |
| 1019 | CONVERT_CHECKED(HeapObject, templ, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1020 | CONVERT_CHECKED(Smi, field, args[1]); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1021 | int index = field->value(); |
| 1022 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1023 | InstanceType type = templ->map()->instance_type(); |
| 1024 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1025 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1026 | RUNTIME_ASSERT(offset > 0); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1027 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1028 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1029 | } else { |
| 1030 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1031 | } |
| 1032 | return *HeapObject::RawField(templ, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1036 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DisableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1037 | ASSERT(args.length() == 1); |
| 1038 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1039 | Map* old_map = object->map(); |
| 1040 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1041 | if (needs_access_checks) { |
| 1042 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1043 | Object* new_map; |
| 1044 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1045 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1046 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1047 | |
| 1048 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1049 | object->set_map(Map::cast(new_map)); |
| 1050 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1051 | return needs_access_checks ? isolate->heap()->true_value() |
| 1052 | : isolate->heap()->false_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1056 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EnableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1057 | ASSERT(args.length() == 1); |
| 1058 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1059 | Map* old_map = object->map(); |
| 1060 | if (!old_map->is_access_check_needed()) { |
| 1061 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1062 | Object* new_map; |
| 1063 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1064 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1065 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1066 | |
| 1067 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1068 | object->set_map(Map::cast(new_map)); |
| 1069 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1070 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1074 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1075 | const char* type, |
| 1076 | Handle<String> name) { |
| 1077 | HandleScope scope(isolate); |
| 1078 | Handle<Object> type_handle = |
| 1079 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1080 | Handle<Object> args[2] = { type_handle, name }; |
| 1081 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1082 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1083 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1087 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareGlobals) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1088 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1089 | HandleScope scope(isolate); |
| 1090 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1091 | isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1092 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1093 | Handle<Context> context = args.at<Context>(0); |
| 1094 | CONVERT_ARG_CHECKED(FixedArray, pairs, 1); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1095 | bool is_eval = args.smi_at(2) == 1; |
| 1096 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(args.smi_at(3)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1097 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1098 | |
| 1099 | // Compute the property attributes. According to ECMA-262, section |
| 1100 | // 13, page 71, the property must be read-only and |
| 1101 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1102 | // property as read-only, so we don't either. |
| 1103 | PropertyAttributes base = is_eval ? NONE : DONT_DELETE; |
| 1104 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1105 | // Traverse the name/value pairs and set the properties. |
| 1106 | int length = pairs->length(); |
| 1107 | for (int i = 0; i < length; i += 2) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1108 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1109 | Handle<String> name(String::cast(pairs->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1110 | Handle<Object> value(pairs->get(i + 1), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1111 | |
| 1112 | // We have to declare a global const property. To capture we only |
| 1113 | // assign to it when evaluating the assignment for "const x = |
| 1114 | // <expr>" the initial value is the hole. |
| 1115 | bool is_const_property = value->IsTheHole(); |
| 1116 | |
| 1117 | if (value->IsUndefined() || is_const_property) { |
| 1118 | // Lookup the property in the global object, and don't set the |
| 1119 | // value of the variable if the property is already there. |
| 1120 | LookupResult lookup; |
| 1121 | global->Lookup(*name, &lookup); |
| 1122 | if (lookup.IsProperty()) { |
| 1123 | // Determine if the property is local by comparing the holder |
| 1124 | // against the global object. The information will be used to |
| 1125 | // avoid throwing re-declaration errors when declaring |
| 1126 | // variables or constants that exist in the prototype chain. |
| 1127 | bool is_local = (*global == lookup.holder()); |
| 1128 | // Get the property attributes and determine if the property is |
| 1129 | // read-only. |
| 1130 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1131 | bool is_read_only = (attributes & READ_ONLY) != 0; |
| 1132 | if (lookup.type() == INTERCEPTOR) { |
| 1133 | // If the interceptor says the property is there, we |
| 1134 | // just return undefined without overwriting the property. |
| 1135 | // Otherwise, we continue to setting the property. |
| 1136 | if (attributes != ABSENT) { |
| 1137 | // Check if the existing property conflicts with regards to const. |
| 1138 | if (is_local && (is_read_only || is_const_property)) { |
| 1139 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1140 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1141 | }; |
| 1142 | // The property already exists without conflicting: Go to |
| 1143 | // the next declaration. |
| 1144 | continue; |
| 1145 | } |
| 1146 | // Fall-through and introduce the absent property by using |
| 1147 | // SetProperty. |
| 1148 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1149 | // For const properties, we treat a callback with this name |
| 1150 | // even in the prototype as a conflicting declaration. |
| 1151 | if (is_const_property && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1152 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1153 | } |
| 1154 | // Otherwise, we check for locally conflicting declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1155 | if (is_local && (is_read_only || is_const_property)) { |
| 1156 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1157 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1158 | } |
| 1159 | // The property already exists without conflicting: Go to |
| 1160 | // the next declaration. |
| 1161 | continue; |
| 1162 | } |
| 1163 | } |
| 1164 | } else { |
| 1165 | // Copy the function and update its context. Use it as value. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1166 | Handle<SharedFunctionInfo> shared = |
| 1167 | Handle<SharedFunctionInfo>::cast(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1168 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1169 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1170 | context, |
| 1171 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1172 | value = function; |
| 1173 | } |
| 1174 | |
| 1175 | LookupResult lookup; |
| 1176 | global->LocalLookup(*name, &lookup); |
| 1177 | |
| 1178 | PropertyAttributes attributes = is_const_property |
| 1179 | ? static_cast<PropertyAttributes>(base | READ_ONLY) |
| 1180 | : base; |
| 1181 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1182 | // There's a local property that we need to overwrite because |
| 1183 | // we're either declaring a function or there's an interceptor |
| 1184 | // that claims the property is absent. |
| 1185 | // |
| 1186 | // Check for conflicting re-declarations. We cannot have |
| 1187 | // conflicting types in case of intercepted properties because |
| 1188 | // they are absent. |
| 1189 | if (lookup.IsProperty() && |
| 1190 | (lookup.type() != INTERCEPTOR) && |
| 1191 | (lookup.IsReadOnly() || is_const_property)) { |
| 1192 | const char* type = (lookup.IsReadOnly()) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1193 | return ThrowRedeclarationError(isolate, type, name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1194 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1195 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1196 | // Safari does not allow the invocation of callback setters for |
| 1197 | // function declarations. To mimic this behavior, we do not allow |
| 1198 | // the invocation of setters for function values. This makes a |
| 1199 | // difference for global functions with the same names as event |
| 1200 | // handlers such as "function onload() {}". Firefox does call the |
| 1201 | // onload setter in those case and Safari does not. We follow |
| 1202 | // Safari for compatibility. |
| 1203 | if (value->IsJSFunction()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1204 | // Do not change DONT_DELETE to false from true. |
| 1205 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
| 1206 | attributes = static_cast<PropertyAttributes>( |
| 1207 | attributes | (lookup.GetAttributes() & DONT_DELETE)); |
| 1208 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1209 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1210 | SetLocalPropertyIgnoreAttributes(global, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1211 | name, |
| 1212 | value, |
| 1213 | attributes)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1214 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1215 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1216 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1217 | name, |
| 1218 | value, |
| 1219 | attributes, |
| 1220 | strict_mode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1223 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1224 | ASSERT(!isolate->has_pending_exception()); |
| 1225 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1229 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1230 | HandleScope scope(isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1231 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1232 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1233 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 1234 | Handle<String> name(String::cast(args[1])); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1235 | PropertyAttributes mode = static_cast<PropertyAttributes>(args.smi_at(2)); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1236 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1237 | Handle<Object> initial_value(args[3], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1238 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1239 | // Declarations are always done in a function or global context. |
| 1240 | context = Handle<Context>(context->declaration_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1241 | |
| 1242 | int index; |
| 1243 | PropertyAttributes attributes; |
| 1244 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1245 | Handle<Object> holder = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1246 | context->Lookup(name, flags, &index, &attributes); |
| 1247 | |
| 1248 | if (attributes != ABSENT) { |
| 1249 | // The name was declared before; check for conflicting |
| 1250 | // re-declarations: This is similar to the code in parser.cc in |
| 1251 | // the AstBuildingParser::Declare function. |
| 1252 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1253 | // Functions are not read-only. |
| 1254 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1255 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1256 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | // Initialize it if necessary. |
| 1260 | if (*initial_value != NULL) { |
| 1261 | if (index >= 0) { |
| 1262 | // The variable or constant context slot should always be in |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1263 | // the function context or the arguments object. |
| 1264 | if (holder->IsContext()) { |
| 1265 | ASSERT(holder.is_identical_to(context)); |
| 1266 | if (((attributes & READ_ONLY) == 0) || |
| 1267 | context->get(index)->IsTheHole()) { |
| 1268 | context->set(index, *initial_value); |
| 1269 | } |
| 1270 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1271 | // The holder is an arguments object. |
| 1272 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1273 | Handle<Object> result = SetElement(arguments, index, initial_value, |
| 1274 | kNonStrictMode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1275 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } else { |
| 1278 | // Slow case: The property is not in the FixedArray part of the context. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1279 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1280 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1281 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1282 | SetProperty(context_ext, name, initial_value, |
| 1283 | mode, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1286 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1287 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1288 | // The property is not in the function context. It needs to be |
| 1289 | // "declared" in the function context's extension context, or in the |
| 1290 | // global context. |
| 1291 | Handle<JSObject> context_ext; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1292 | if (context->has_extension()) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1293 | // The function context's extension context exists - use it. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1294 | context_ext = Handle<JSObject>(JSObject::cast(context->extension())); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1295 | } else { |
| 1296 | // The function context's extension context does not exists - allocate |
| 1297 | // it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1298 | context_ext = isolate->factory()->NewJSObject( |
| 1299 | isolate->context_extension_function()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1300 | // And store it in the extension slot. |
| 1301 | context->set_extension(*context_ext); |
| 1302 | } |
| 1303 | ASSERT(*context_ext != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1304 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1305 | // Declare the property by setting it to the initial value if provided, |
| 1306 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1307 | // constant declarations). |
| 1308 | ASSERT(!context_ext->HasLocalProperty(*name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1309 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1310 | if (*initial_value != NULL) value = initial_value; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1311 | // Declaring a const context slot is a conflicting declaration if |
| 1312 | // there is a callback with that name in a prototype. It is |
| 1313 | // allowed to introduce const variables in |
| 1314 | // JSContextExtensionObjects. They are treated specially in |
| 1315 | // SetProperty and no setters are invoked for those since they are |
| 1316 | // not real JSObjects. |
| 1317 | if (initial_value->IsTheHole() && |
| 1318 | !context_ext->IsJSContextExtensionObject()) { |
| 1319 | LookupResult lookup; |
| 1320 | context_ext->Lookup(*name, &lookup); |
| 1321 | if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1322 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1325 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1326 | SetProperty(context_ext, name, value, mode, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1327 | kNonStrictMode)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1330 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1334 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeVarGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1335 | NoHandleAllocation nha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1336 | // args[0] == name |
| 1337 | // args[1] == strict_mode |
| 1338 | // args[2] == value (optional) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | |
| 1340 | // Determine if we need to assign to the variable if it already |
| 1341 | // exists (based on the number of arguments). |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1342 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1343 | bool assign = args.length() == 3; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | |
| 1345 | CONVERT_ARG_CHECKED(String, name, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1346 | GlobalObject* global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1347 | RUNTIME_ASSERT(args[1]->IsSmi()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1348 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(args.smi_at(1)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1349 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | |
| 1351 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1352 | // not be deletable. |
| 1353 | PropertyAttributes attributes = DONT_DELETE; |
| 1354 | |
| 1355 | // 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] | 1356 | // there, there is a property with this name in the prototype chain. |
| 1357 | // We follow Safari and Firefox behavior and only set the property |
| 1358 | // locally if there is an explicit initialization value that we have |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1359 | // to assign to the property. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1360 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1361 | // the whole chain of hidden prototypes to do a 'local' lookup. |
| 1362 | JSObject* real_holder = global; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | LookupResult lookup; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1364 | while (true) { |
| 1365 | real_holder->LocalLookup(*name, &lookup); |
| 1366 | if (lookup.IsProperty()) { |
| 1367 | // Determine if this is a redeclaration of something read-only. |
| 1368 | if (lookup.IsReadOnly()) { |
| 1369 | // If we found readonly property on one of hidden prototypes, |
| 1370 | // just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1371 | if (real_holder != isolate->context()->global()) break; |
| 1372 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | // Determine if this is a redeclaration of an intercepted read-only |
| 1376 | // property and figure out if the property exists at all. |
| 1377 | bool found = true; |
| 1378 | PropertyType type = lookup.type(); |
| 1379 | if (type == INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1380 | HandleScope handle_scope(isolate); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1381 | Handle<JSObject> holder(real_holder); |
| 1382 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1383 | real_holder = *holder; |
| 1384 | if (intercepted == ABSENT) { |
| 1385 | // The interceptor claims the property isn't there. We need to |
| 1386 | // make sure to introduce it. |
| 1387 | found = false; |
| 1388 | } else if ((intercepted & READ_ONLY) != 0) { |
| 1389 | // The property is present, but read-only. Since we're trying to |
| 1390 | // overwrite it with a variable declaration we must throw a |
| 1391 | // re-declaration error. However if we found readonly property |
| 1392 | // on one of hidden prototypes, just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1393 | if (real_holder != isolate->context()->global()) break; |
| 1394 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | if (found && !assign) { |
| 1399 | // The global property is there and we're not assigning any value |
| 1400 | // to it. Just return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1401 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | // Assign the value (or undefined) to the property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1405 | Object* value = (assign) ? args[2] : isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1406 | return real_holder->SetProperty( |
| 1407 | &lookup, *name, value, attributes, strict_mode); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1408 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1409 | |
| 1410 | Object* proto = real_holder->GetPrototype(); |
| 1411 | if (!proto->IsJSObject()) |
| 1412 | break; |
| 1413 | |
| 1414 | if (!JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 1415 | break; |
| 1416 | |
| 1417 | real_holder = JSObject::cast(proto); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1420 | global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1421 | if (assign) { |
| 1422 | return global->SetProperty(*name, args[2], attributes, strict_mode); |
| 1423 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1424 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1428 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1429 | // All constants are declared with an initial value. The name |
| 1430 | // of the constant is the first argument and the initial value |
| 1431 | // is the second. |
| 1432 | RUNTIME_ASSERT(args.length() == 2); |
| 1433 | CONVERT_ARG_CHECKED(String, name, 0); |
| 1434 | Handle<Object> value = args.at<Object>(1); |
| 1435 | |
| 1436 | // Get the current global object from top. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1437 | GlobalObject* global = isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1438 | |
| 1439 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1440 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1441 | PropertyAttributes attributes = |
| 1442 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1443 | |
| 1444 | // Lookup the property locally in the global object. If it isn't |
| 1445 | // there, we add the property and take special precautions to always |
| 1446 | // add it as a local property even in case of callbacks in the |
| 1447 | // prototype chain (this rules out using SetProperty). |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1448 | // We use SetLocalPropertyIgnoreAttributes instead |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | LookupResult lookup; |
| 1450 | global->LocalLookup(*name, &lookup); |
| 1451 | if (!lookup.IsProperty()) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1452 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1453 | *value, |
| 1454 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | // Determine if this is a redeclaration of something not |
| 1458 | // read-only. In case the result is hidden behind an interceptor we |
| 1459 | // need to ask it for the property attributes. |
| 1460 | if (!lookup.IsReadOnly()) { |
| 1461 | if (lookup.type() != INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1462 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | PropertyAttributes intercepted = global->GetPropertyAttribute(*name); |
| 1466 | |
| 1467 | // Throw re-declaration error if the intercepted property is present |
| 1468 | // but not read-only. |
| 1469 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1470 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | // Restore global object from context (in case of GC) and continue |
| 1474 | // with setting the value because the property is either absent or |
| 1475 | // read-only. We also have to do redo the lookup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1476 | HandleScope handle_scope(isolate); |
| 1477 | Handle<GlobalObject> global(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1478 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1479 | // 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] | 1480 | // property through an interceptor and only do it if it's |
| 1481 | // uninitialized, e.g. the hole. Nirk... |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1482 | // Passing non-strict mode because the property is writable. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1483 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1484 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1485 | name, |
| 1486 | value, |
| 1487 | attributes, |
| 1488 | kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1489 | return *value; |
| 1490 | } |
| 1491 | |
| 1492 | // Set the value, but only we're assigning the initial value to a |
| 1493 | // constant. For now, we determine this by checking if the |
| 1494 | // current value is the hole. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1495 | // Strict mode handling not needed (const disallowed in strict mode). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1496 | PropertyType type = lookup.type(); |
| 1497 | if (type == FIELD) { |
| 1498 | FixedArray* properties = global->properties(); |
| 1499 | int index = lookup.GetFieldIndex(); |
| 1500 | if (properties->get(index)->IsTheHole()) { |
| 1501 | properties->set(index, *value); |
| 1502 | } |
| 1503 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1504 | if (global->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1505 | global->SetNormalizedProperty(&lookup, *value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1506 | } |
| 1507 | } else { |
| 1508 | // Ignore re-initialization of constants that have already been |
| 1509 | // assigned a function value. |
| 1510 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1511 | } |
| 1512 | |
| 1513 | // Use the set value as the result of the operation. |
| 1514 | return *value; |
| 1515 | } |
| 1516 | |
| 1517 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1518 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1519 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1520 | ASSERT(args.length() == 3); |
| 1521 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1522 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1523 | ASSERT(!value->IsTheHole()); |
| 1524 | CONVERT_ARG_CHECKED(Context, context, 1); |
| 1525 | Handle<String> name(String::cast(args[2])); |
| 1526 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1527 | // Initializations are always done in a function or global context. |
| 1528 | context = Handle<Context>(context->declaration_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1529 | |
| 1530 | int index; |
| 1531 | PropertyAttributes attributes; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1532 | ContextLookupFlags flags = FOLLOW_CHAINS; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1533 | Handle<Object> holder = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1534 | context->Lookup(name, flags, &index, &attributes); |
| 1535 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1536 | // In most situations, the property introduced by the const |
| 1537 | // declaration should be present in the context extension object. |
| 1538 | // However, because declaration and initialization are separate, the |
| 1539 | // property might have been deleted (if it was introduced by eval) |
| 1540 | // before we reach the initialization point. |
| 1541 | // |
| 1542 | // Example: |
| 1543 | // |
| 1544 | // function f() { eval("delete x; const x;"); } |
| 1545 | // |
| 1546 | // In that case, the initialization behaves like a normal assignment |
| 1547 | // to property 'x'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1548 | if (index >= 0) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1549 | if (holder->IsContext()) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1550 | // Property was found in a context. Perform the assignment if we |
| 1551 | // found some non-constant or an uninitialized constant. |
| 1552 | Handle<Context> context = Handle<Context>::cast(holder); |
| 1553 | if ((attributes & READ_ONLY) == 0 || context->get(index)->IsTheHole()) { |
| 1554 | context->set(index, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } else { |
| 1557 | // The holder is an arguments object. |
| 1558 | ASSERT((attributes & READ_ONLY) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1559 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1560 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1561 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1562 | SetElement(arguments, index, value, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1563 | } |
| 1564 | return *value; |
| 1565 | } |
| 1566 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1567 | // The property could not be found, we introduce it in the global |
| 1568 | // context. |
| 1569 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1570 | Handle<JSObject> global = Handle<JSObject>( |
| 1571 | isolate->context()->global()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1572 | // Strict mode not needed (const disallowed in strict mode). |
| 1573 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1574 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1575 | SetProperty(global, name, value, NONE, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1576 | return *value; |
| 1577 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1578 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1579 | // The property was present in a context extension object. |
| 1580 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1581 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1582 | if (*context_ext == context->extension()) { |
| 1583 | // This is the property that was introduced by the const |
| 1584 | // declaration. Set it if it hasn't been set before. NOTE: We |
| 1585 | // cannot use GetProperty() to get the current value as it |
| 1586 | // 'unholes' the value. |
| 1587 | LookupResult lookup; |
| 1588 | context_ext->LocalLookupRealNamedProperty(*name, &lookup); |
| 1589 | ASSERT(lookup.IsProperty()); // the property was declared |
| 1590 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1591 | |
| 1592 | PropertyType type = lookup.type(); |
| 1593 | if (type == FIELD) { |
| 1594 | FixedArray* properties = context_ext->properties(); |
| 1595 | int index = lookup.GetFieldIndex(); |
| 1596 | if (properties->get(index)->IsTheHole()) { |
| 1597 | properties->set(index, *value); |
| 1598 | } |
| 1599 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1600 | if (context_ext->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1601 | context_ext->SetNormalizedProperty(&lookup, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } else { |
| 1604 | // We should not reach here. Any real, named property should be |
| 1605 | // either a field or a dictionary slot. |
| 1606 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } else { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1609 | // The property was found in a different context extension object. |
| 1610 | // Set it if it is not a read-only property. |
| 1611 | if ((attributes & READ_ONLY) == 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1612 | // Strict mode not needed (const disallowed in strict mode). |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1613 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1614 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1615 | SetProperty(context_ext, name, value, attributes, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1616 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1617 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1618 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1619 | return *value; |
| 1620 | } |
| 1621 | |
| 1622 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1623 | RUNTIME_FUNCTION(MaybeObject*, |
| 1624 | Runtime_OptimizeObjectForAddingMultipleProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1625 | HandleScope scope(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1626 | ASSERT(args.length() == 2); |
| 1627 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1628 | CONVERT_SMI_ARG_CHECKED(properties, 1); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1629 | if (object->HasFastProperties()) { |
| 1630 | NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
| 1631 | } |
| 1632 | return *object; |
| 1633 | } |
| 1634 | |
| 1635 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1636 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExec) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1637 | HandleScope scope(isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1638 | ASSERT(args.length() == 4); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1639 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1640 | CONVERT_ARG_CHECKED(String, subject, 1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1641 | // 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] | 1642 | // 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] | 1643 | CONVERT_SMI_ARG_CHECKED(index, 2); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1644 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1645 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1646 | RUNTIME_ASSERT(index >= 0); |
| 1647 | RUNTIME_ASSERT(index <= subject->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1648 | isolate->counters()->regexp_entry_runtime()->Increment(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1649 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1650 | subject, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1651 | index, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1652 | last_match_info); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1653 | if (result.is_null()) return Failure::Exception(); |
| 1654 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1658 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpConstructResult) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1659 | ASSERT(args.length() == 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1660 | CONVERT_SMI_ARG_CHECKED(elements_count, 0); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1661 | if (elements_count > JSArray::kMaxFastElementsLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1662 | return isolate->ThrowIllegalOperation(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1663 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1664 | Object* new_object; |
| 1665 | { MaybeObject* maybe_new_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1666 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1667 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1668 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1669 | FixedArray* elements = FixedArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1670 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1671 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1672 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1673 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1674 | { |
| 1675 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1676 | HandleScope scope(isolate); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1677 | reinterpret_cast<HeapObject*>(new_object)-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1678 | set_map(isolate->global_context()->regexp_result_map()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1679 | } |
| 1680 | JSArray* array = JSArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1681 | array->set_properties(isolate->heap()->empty_fixed_array()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1682 | array->set_elements(elements); |
| 1683 | array->set_length(Smi::FromInt(elements_count)); |
| 1684 | // Write in-object properties after the length of the array. |
| 1685 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1686 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1687 | return array; |
| 1688 | } |
| 1689 | |
| 1690 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1691 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpInitializeObject) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1692 | AssertNoAllocation no_alloc; |
| 1693 | ASSERT(args.length() == 5); |
| 1694 | CONVERT_CHECKED(JSRegExp, regexp, args[0]); |
| 1695 | CONVERT_CHECKED(String, source, args[1]); |
| 1696 | |
| 1697 | Object* global = args[2]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1698 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1699 | |
| 1700 | Object* ignoreCase = args[3]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1701 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1702 | |
| 1703 | Object* multiline = args[4]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1704 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1705 | |
| 1706 | Map* map = regexp->map(); |
| 1707 | Object* constructor = map->constructor(); |
| 1708 | if (constructor->IsJSFunction() && |
| 1709 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1710 | // If we still have the original map, set in-object properties directly. |
| 1711 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
| 1712 | // TODO(lrn): Consider skipping write barrier on booleans as well. |
| 1713 | // Both true and false should be in oldspace at all times. |
| 1714 | regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, global); |
| 1715 | regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, ignoreCase); |
| 1716 | regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, multiline); |
| 1717 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1718 | Smi::FromInt(0), |
| 1719 | SKIP_WRITE_BARRIER); |
| 1720 | return regexp; |
| 1721 | } |
| 1722 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1723 | // Map has changed, so use generic, but slower, method. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1724 | PropertyAttributes final = |
| 1725 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1726 | PropertyAttributes writable = |
| 1727 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1728 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1729 | MaybeObject* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1730 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1731 | source, |
| 1732 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1733 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1734 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1735 | global, |
| 1736 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1737 | ASSERT(!result->IsFailure()); |
| 1738 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1739 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1740 | ignoreCase, |
| 1741 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1742 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1743 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1744 | multiline, |
| 1745 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1746 | ASSERT(!result->IsFailure()); |
| 1747 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1748 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1749 | Smi::FromInt(0), |
| 1750 | writable); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1751 | ASSERT(!result->IsFailure()); |
| 1752 | USE(result); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1753 | return regexp; |
| 1754 | } |
| 1755 | |
| 1756 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1757 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinishArrayPrototypeSetup) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1758 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1759 | ASSERT(args.length() == 1); |
| 1760 | CONVERT_ARG_CHECKED(JSArray, prototype, 0); |
| 1761 | // This is necessary to enable fast checks for absence of elements |
| 1762 | // on Array.prototype and below. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1763 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1764 | return Smi::FromInt(0); |
| 1765 | } |
| 1766 | |
| 1767 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1768 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1769 | Handle<JSObject> holder, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1770 | const char* name, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1771 | Builtins::Name builtin_name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1772 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1773 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1774 | Handle<JSFunction> optimized = |
| 1775 | isolate->factory()->NewFunction(key, |
| 1776 | JS_OBJECT_TYPE, |
| 1777 | JSObject::kHeaderSize, |
| 1778 | code, |
| 1779 | false); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1780 | optimized->shared()->DontAdaptArguments(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1781 | SetProperty(holder, key, optimized, NONE, kStrictMode); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1782 | return optimized; |
| 1783 | } |
| 1784 | |
| 1785 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1786 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1787 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1788 | ASSERT(args.length() == 1); |
| 1789 | CONVERT_ARG_CHECKED(JSObject, holder, 0); |
| 1790 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1791 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
| 1792 | InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
| 1793 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 1794 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 1795 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 1796 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
| 1797 | InstallBuiltin(isolate, holder, "concat", Builtins::kArrayConcat); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1798 | |
| 1799 | return *holder; |
| 1800 | } |
| 1801 | |
| 1802 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1803 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetGlobalReceiver) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1804 | // Returns a real global receiver, not one of builtins object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1805 | Context* global_context = |
| 1806 | isolate->context()->global()->global_context(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1807 | return global_context->global()->global_receiver(); |
| 1808 | } |
| 1809 | |
| 1810 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1811 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MaterializeRegExpLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1812 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1813 | ASSERT(args.length() == 4); |
| 1814 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1815 | int index = args.smi_at(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1816 | Handle<String> pattern = args.at<String>(2); |
| 1817 | Handle<String> flags = args.at<String>(3); |
| 1818 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1819 | // Get the RegExp function from the context in the literals array. |
| 1820 | // This is the RegExp function from the context in which the |
| 1821 | // function was created. We do not use the RegExp function from the |
| 1822 | // current global context because this might be the RegExp function |
| 1823 | // 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] | 1824 | Handle<JSFunction> constructor = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1825 | Handle<JSFunction>( |
| 1826 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1827 | // Compute the regular expression literal. |
| 1828 | bool has_pending_exception; |
| 1829 | Handle<Object> regexp = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1830 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1831 | &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1832 | if (has_pending_exception) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1833 | ASSERT(isolate->has_pending_exception()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1834 | return Failure::Exception(); |
| 1835 | } |
| 1836 | literals->set(index, *regexp); |
| 1837 | return *regexp; |
| 1838 | } |
| 1839 | |
| 1840 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1841 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1842 | NoHandleAllocation ha; |
| 1843 | ASSERT(args.length() == 1); |
| 1844 | |
| 1845 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1846 | return f->shared()->name(); |
| 1847 | } |
| 1848 | |
| 1849 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1850 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetName) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1851 | NoHandleAllocation ha; |
| 1852 | ASSERT(args.length() == 2); |
| 1853 | |
| 1854 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1855 | CONVERT_CHECKED(String, name, args[1]); |
| 1856 | f->shared()->set_name(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1857 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1861 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetBound) { |
| 1862 | HandleScope scope(isolate); |
| 1863 | ASSERT(args.length() == 1); |
| 1864 | |
| 1865 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1866 | fun->shared()->set_bound(true); |
| 1867 | return isolate->heap()->undefined_value(); |
| 1868 | } |
| 1869 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1870 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1871 | NoHandleAllocation ha; |
| 1872 | ASSERT(args.length() == 1); |
| 1873 | |
| 1874 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1875 | Object* obj = f->RemovePrototype(); |
| 1876 | if (obj->IsFailure()) return obj; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1877 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1878 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1882 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1883 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | ASSERT(args.length() == 1); |
| 1885 | |
| 1886 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1887 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 1888 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1889 | |
| 1890 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 1891 | } |
| 1892 | |
| 1893 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1894 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetSourceCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | NoHandleAllocation ha; |
| 1896 | ASSERT(args.length() == 1); |
| 1897 | |
| 1898 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1899 | return f->shared()->GetSourceCode(); |
| 1900 | } |
| 1901 | |
| 1902 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1903 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScriptSourcePosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1904 | NoHandleAllocation ha; |
| 1905 | ASSERT(args.length() == 1); |
| 1906 | |
| 1907 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1908 | int pos = fun->shared()->start_position(); |
| 1909 | return Smi::FromInt(pos); |
| 1910 | } |
| 1911 | |
| 1912 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1913 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetPositionForOffset) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1914 | ASSERT(args.length() == 2); |
| 1915 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1916 | CONVERT_CHECKED(Code, code, args[0]); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1917 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 1918 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1919 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 1920 | |
| 1921 | Address pc = code->address() + offset; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1922 | return Smi::FromInt(code->SourcePosition(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1926 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetInstanceClassName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1927 | NoHandleAllocation ha; |
| 1928 | ASSERT(args.length() == 2); |
| 1929 | |
| 1930 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1931 | CONVERT_CHECKED(String, name, args[1]); |
| 1932 | fun->SetInstanceClassName(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1933 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1937 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1938 | NoHandleAllocation ha; |
| 1939 | ASSERT(args.length() == 2); |
| 1940 | |
| 1941 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1942 | CONVERT_CHECKED(Smi, length, args[1]); |
| 1943 | fun->shared()->set_length(length->value()); |
| 1944 | return length; |
| 1945 | } |
| 1946 | |
| 1947 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1948 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetPrototype) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1949 | NoHandleAllocation ha; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | ASSERT(args.length() == 2); |
| 1951 | |
| 1952 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1953 | ASSERT(fun->should_have_prototype()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1954 | Object* obj; |
| 1955 | { MaybeObject* maybe_obj = |
| 1956 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 1957 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1958 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1959 | return args[0]; // return TOS |
| 1960 | } |
| 1961 | |
| 1962 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1963 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsAPIFunction) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1964 | NoHandleAllocation ha; |
| 1965 | ASSERT(args.length() == 1); |
| 1966 | |
| 1967 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1968 | return f->shared()->IsApiFunction() ? isolate->heap()->true_value() |
| 1969 | : isolate->heap()->false_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1972 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1973 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsBuiltin) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1974 | NoHandleAllocation ha; |
| 1975 | ASSERT(args.length() == 1); |
| 1976 | |
| 1977 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1978 | return f->IsBuiltin() ? isolate->heap()->true_value() : |
| 1979 | isolate->heap()->false_value(); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1982 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1983 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetCode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1984 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | ASSERT(args.length() == 2); |
| 1986 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1987 | CONVERT_ARG_CHECKED(JSFunction, target, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1988 | Handle<Object> code = args.at<Object>(1); |
| 1989 | |
| 1990 | Handle<Context> context(target->context()); |
| 1991 | |
| 1992 | if (!code->IsNull()) { |
| 1993 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 1994 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1995 | Handle<SharedFunctionInfo> shared(fun->shared()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1996 | |
| 1997 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1998 | return Failure::Exception(); |
| 1999 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2000 | // Since we don't store the source for this we should never |
| 2001 | // optimize this. |
| 2002 | shared->code()->set_optimizable(false); |
| 2003 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2004 | // Set the code, scope info, formal parameter count, |
| 2005 | // and the length of the target function. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2006 | target->shared()->set_code(shared->code()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2007 | target->ReplaceCode(shared->code()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2008 | target->shared()->set_scope_info(shared->scope_info()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2009 | target->shared()->set_length(shared->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | target->shared()->set_formal_parameter_count( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2011 | shared->formal_parameter_count()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2012 | // Set the source code of the target function to undefined. |
| 2013 | // SetCode is only used for built-in constructors like String, |
| 2014 | // Array, and Object, and some web code |
| 2015 | // doesn't like seeing source code for constructors. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2016 | target->shared()->set_script(isolate->heap()->undefined_value()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2017 | target->shared()->code()->set_optimizable(false); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2018 | // Clear the optimization hints related to the compiled code as these are no |
| 2019 | // longer valid when the code is overwritten. |
| 2020 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2021 | context = Handle<Context>(fun->context()); |
| 2022 | |
| 2023 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2024 | // cross context contamination. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2025 | int number_of_literals = fun->NumberOfLiterals(); |
| 2026 | Handle<FixedArray> literals = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2027 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2028 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2029 | // Insert the object, regexp and array functions in the literals |
| 2030 | // array prefix. These are the functions that will be used when |
| 2031 | // creating object, regexp and array literals. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2032 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2033 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2034 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2035 | // It's okay to skip the write barrier here because the literals |
| 2036 | // are guaranteed to be in old space. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2037 | target->set_literals(*literals, SKIP_WRITE_BARRIER); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2038 | target->set_next_function_link(isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | target->set_context(*context); |
| 2042 | return *target; |
| 2043 | } |
| 2044 | |
| 2045 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2046 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2047 | HandleScope scope(isolate); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2048 | ASSERT(args.length() == 2); |
| 2049 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 2050 | CONVERT_SMI_ARG_CHECKED(num, 1); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2051 | RUNTIME_ASSERT(num >= 0); |
| 2052 | SetExpectedNofProperties(function, num); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2053 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2057 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2058 | Object* char_code) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2059 | uint32_t code; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2060 | if (char_code->ToArrayIndex(&code)) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2061 | if (code <= 0xffff) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2062 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2063 | } |
| 2064 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2065 | return isolate->heap()->empty_string(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2069 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCharCodeAt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2070 | NoHandleAllocation ha; |
| 2071 | ASSERT(args.length() == 2); |
| 2072 | |
| 2073 | CONVERT_CHECKED(String, subject, args[0]); |
| 2074 | Object* index = args[1]; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2075 | RUNTIME_ASSERT(index->IsNumber()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2076 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2077 | uint32_t i = 0; |
| 2078 | if (index->IsSmi()) { |
| 2079 | int value = Smi::cast(index)->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2080 | if (value < 0) return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2081 | i = value; |
| 2082 | } else { |
| 2083 | ASSERT(index->IsHeapNumber()); |
| 2084 | double value = HeapNumber::cast(index)->value(); |
| 2085 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
kasperl@chromium.org | 74e4e5e | 2010-01-25 10:15:52 +0000 | [diff] [blame] | 2086 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2087 | |
| 2088 | // Flatten the string. If someone wants to get a char at an index |
| 2089 | // in a cons string, it is likely that more indices will be |
| 2090 | // accessed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2091 | Object* flat; |
| 2092 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2093 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2094 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2095 | subject = String::cast(flat); |
| 2096 | |
| 2097 | if (i >= static_cast<uint32_t>(subject->length())) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2098 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | return Smi::FromInt(subject->Get(i)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2105 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CharFromCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2106 | NoHandleAllocation ha; |
| 2107 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2108 | return CharFromCode(isolate, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2111 | |
| 2112 | class FixedArrayBuilder { |
| 2113 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2114 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2115 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2116 | length_(0) { |
| 2117 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2118 | // extend the array will work. |
| 2119 | ASSERT(initial_capacity > 0); |
| 2120 | } |
| 2121 | |
| 2122 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2123 | : array_(backing_store), |
| 2124 | length_(0) { |
| 2125 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2126 | // extend the array will work. |
| 2127 | ASSERT(backing_store->length() > 0); |
| 2128 | } |
| 2129 | |
| 2130 | bool HasCapacity(int elements) { |
| 2131 | int length = array_->length(); |
| 2132 | int required_length = length_ + elements; |
| 2133 | return (length >= required_length); |
| 2134 | } |
| 2135 | |
| 2136 | void EnsureCapacity(int elements) { |
| 2137 | int length = array_->length(); |
| 2138 | int required_length = length_ + elements; |
| 2139 | if (length < required_length) { |
| 2140 | int new_length = length; |
| 2141 | do { |
| 2142 | new_length *= 2; |
| 2143 | } while (new_length < required_length); |
| 2144 | Handle<FixedArray> extended_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2145 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2146 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2147 | array_ = extended_array; |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | void Add(Object* value) { |
| 2152 | ASSERT(length_ < capacity()); |
| 2153 | array_->set(length_, value); |
| 2154 | length_++; |
| 2155 | } |
| 2156 | |
| 2157 | void Add(Smi* value) { |
| 2158 | ASSERT(length_ < capacity()); |
| 2159 | array_->set(length_, value); |
| 2160 | length_++; |
| 2161 | } |
| 2162 | |
| 2163 | Handle<FixedArray> array() { |
| 2164 | return array_; |
| 2165 | } |
| 2166 | |
| 2167 | int length() { |
| 2168 | return length_; |
| 2169 | } |
| 2170 | |
| 2171 | int capacity() { |
| 2172 | return array_->length(); |
| 2173 | } |
| 2174 | |
| 2175 | Handle<JSArray> ToJSArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2176 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2177 | result_array->set_length(Smi::FromInt(length_)); |
| 2178 | return result_array; |
| 2179 | } |
| 2180 | |
| 2181 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
| 2182 | target_array->set_elements(*array_); |
| 2183 | target_array->set_length(Smi::FromInt(length_)); |
| 2184 | return target_array; |
| 2185 | } |
| 2186 | |
| 2187 | private: |
| 2188 | Handle<FixedArray> array_; |
| 2189 | int length_; |
| 2190 | }; |
| 2191 | |
| 2192 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2193 | // Forward declarations. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2194 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2195 | const int kStringBuilderConcatHelperPositionBits = 19; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2196 | |
| 2197 | template <typename schar> |
| 2198 | static inline void StringBuilderConcatHelper(String*, |
| 2199 | schar*, |
| 2200 | FixedArray*, |
| 2201 | int); |
| 2202 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2203 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2204 | StringBuilderSubstringLength; |
| 2205 | typedef BitField<int, |
| 2206 | kStringBuilderConcatHelperLengthBits, |
| 2207 | kStringBuilderConcatHelperPositionBits> |
| 2208 | StringBuilderSubstringPosition; |
| 2209 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2210 | |
| 2211 | class ReplacementStringBuilder { |
| 2212 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2213 | ReplacementStringBuilder(Heap* heap, |
| 2214 | Handle<String> subject, |
| 2215 | int estimated_part_count) |
| 2216 | : heap_(heap), |
| 2217 | array_builder_(heap->isolate(), estimated_part_count), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2218 | subject_(subject), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2219 | character_count_(0), |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2220 | is_ascii_(subject->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2221 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2222 | // extend the array will work. |
| 2223 | ASSERT(estimated_part_count > 0); |
| 2224 | } |
| 2225 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2226 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2227 | int from, |
| 2228 | int to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2229 | ASSERT(from >= 0); |
| 2230 | int length = to - from; |
| 2231 | ASSERT(length > 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2232 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2233 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2234 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2235 | StringBuilderSubstringPosition::encode(from); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2236 | builder->Add(Smi::FromInt(encoded_slice)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2237 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2238 | // Otherwise encode as two smis. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2239 | builder->Add(Smi::FromInt(-length)); |
| 2240 | builder->Add(Smi::FromInt(from)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2241 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | |
| 2245 | void EnsureCapacity(int elements) { |
| 2246 | array_builder_.EnsureCapacity(elements); |
| 2247 | } |
| 2248 | |
| 2249 | |
| 2250 | void AddSubjectSlice(int from, int to) { |
| 2251 | AddSubjectSlice(&array_builder_, from, to); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2252 | IncrementCharacterCount(to - from); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | |
| 2256 | void AddString(Handle<String> string) { |
| 2257 | int length = string->length(); |
| 2258 | ASSERT(length > 0); |
| 2259 | AddElement(*string); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2260 | if (!string->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2261 | is_ascii_ = false; |
| 2262 | } |
| 2263 | IncrementCharacterCount(length); |
| 2264 | } |
| 2265 | |
| 2266 | |
| 2267 | Handle<String> ToString() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2268 | if (array_builder_.length() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2269 | return heap_->isolate()->factory()->empty_string(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | Handle<String> joined_string; |
| 2273 | if (is_ascii_) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2274 | Handle<SeqAsciiString> seq = NewRawAsciiString(character_count_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2275 | AssertNoAllocation no_alloc; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2276 | char* char_buffer = seq->GetChars(); |
| 2277 | StringBuilderConcatHelper(*subject_, |
| 2278 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2279 | *array_builder_.array(), |
| 2280 | array_builder_.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2281 | joined_string = Handle<String>::cast(seq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2282 | } else { |
| 2283 | // Non-ASCII. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2284 | Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2285 | AssertNoAllocation no_alloc; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2286 | uc16* char_buffer = seq->GetChars(); |
| 2287 | StringBuilderConcatHelper(*subject_, |
| 2288 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2289 | *array_builder_.array(), |
| 2290 | array_builder_.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2291 | joined_string = Handle<String>::cast(seq); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2292 | } |
| 2293 | return joined_string; |
| 2294 | } |
| 2295 | |
| 2296 | |
| 2297 | void IncrementCharacterCount(int by) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2298 | if (character_count_ > String::kMaxLength - by) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2299 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2300 | } |
| 2301 | character_count_ += by; |
| 2302 | } |
| 2303 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2304 | Handle<JSArray> GetParts() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2305 | return array_builder_.ToJSArray(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2306 | } |
kmillikin@chromium.org | d982519 | 2010-03-30 08:36:16 +0000 | [diff] [blame] | 2307 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2308 | private: |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2309 | Handle<SeqAsciiString> NewRawAsciiString(int length) { |
| 2310 | return heap_->isolate()->factory()->NewRawAsciiString(length); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 2314 | Handle<SeqTwoByteString> NewRawTwoByteString(int length) { |
| 2315 | return heap_->isolate()->factory()->NewRawTwoByteString(length); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | |
| 2319 | void AddElement(Object* element) { |
| 2320 | ASSERT(element->IsSmi() || element->IsString()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2321 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2322 | array_builder_.Add(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2325 | Heap* heap_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2326 | FixedArrayBuilder array_builder_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2327 | Handle<String> subject_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2328 | int character_count_; |
| 2329 | bool is_ascii_; |
| 2330 | }; |
| 2331 | |
| 2332 | |
| 2333 | class CompiledReplacement { |
| 2334 | public: |
| 2335 | CompiledReplacement() |
| 2336 | : parts_(1), replacement_substrings_(0) {} |
| 2337 | |
| 2338 | void Compile(Handle<String> replacement, |
| 2339 | int capture_count, |
| 2340 | int subject_length); |
| 2341 | |
| 2342 | void Apply(ReplacementStringBuilder* builder, |
| 2343 | int match_from, |
| 2344 | int match_to, |
| 2345 | Handle<JSArray> last_match_info); |
| 2346 | |
| 2347 | // Number of distinct parts of the replacement pattern. |
| 2348 | int parts() { |
| 2349 | return parts_.length(); |
| 2350 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2351 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2352 | private: |
| 2353 | enum PartType { |
| 2354 | SUBJECT_PREFIX = 1, |
| 2355 | SUBJECT_SUFFIX, |
| 2356 | SUBJECT_CAPTURE, |
| 2357 | REPLACEMENT_SUBSTRING, |
| 2358 | REPLACEMENT_STRING, |
| 2359 | |
| 2360 | NUMBER_OF_PART_TYPES |
| 2361 | }; |
| 2362 | |
| 2363 | struct ReplacementPart { |
| 2364 | static inline ReplacementPart SubjectMatch() { |
| 2365 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2366 | } |
| 2367 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2368 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2369 | } |
| 2370 | static inline ReplacementPart SubjectPrefix() { |
| 2371 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2372 | } |
| 2373 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2374 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2375 | } |
| 2376 | static inline ReplacementPart ReplacementString() { |
| 2377 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2378 | } |
| 2379 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2380 | ASSERT(from >= 0); |
| 2381 | ASSERT(to > from); |
| 2382 | return ReplacementPart(-from, to); |
| 2383 | } |
| 2384 | |
| 2385 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2386 | // the replacement pattern, otherwise it's a value from PartType. |
| 2387 | ReplacementPart(int tag, int data) |
| 2388 | : tag(tag), data(data) { |
| 2389 | // Must be non-positive or a PartType value. |
| 2390 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2391 | } |
| 2392 | // Either a value of PartType or a non-positive number that is |
| 2393 | // the negation of an index into the replacement string. |
| 2394 | int tag; |
| 2395 | // The data value's interpretation depends on the value of tag: |
| 2396 | // tag == SUBJECT_PREFIX || |
| 2397 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2398 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2399 | // tag == REPLACEMENT_SUBSTRING || |
| 2400 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2401 | // of the replacement string. |
| 2402 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2403 | // string ranging over -tag .. data. |
| 2404 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2405 | // substring objects. |
| 2406 | int data; |
| 2407 | }; |
| 2408 | |
| 2409 | template<typename Char> |
| 2410 | static void ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
| 2411 | Vector<Char> characters, |
| 2412 | int capture_count, |
| 2413 | int subject_length) { |
| 2414 | int length = characters.length(); |
| 2415 | int last = 0; |
| 2416 | for (int i = 0; i < length; i++) { |
| 2417 | Char c = characters[i]; |
| 2418 | if (c == '$') { |
| 2419 | int next_index = i + 1; |
| 2420 | if (next_index == length) { // No next character! |
| 2421 | break; |
| 2422 | } |
| 2423 | Char c2 = characters[next_index]; |
| 2424 | switch (c2) { |
| 2425 | case '$': |
| 2426 | if (i > last) { |
| 2427 | // There is a substring before. Include the first "$". |
| 2428 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2429 | last = next_index + 1; // Continue after the second "$". |
| 2430 | } else { |
| 2431 | // Let the next substring start with the second "$". |
| 2432 | last = next_index; |
| 2433 | } |
| 2434 | i = next_index; |
| 2435 | break; |
| 2436 | case '`': |
| 2437 | if (i > last) { |
| 2438 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2439 | } |
| 2440 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2441 | i = next_index; |
| 2442 | last = i + 1; |
| 2443 | break; |
| 2444 | case '\'': |
| 2445 | if (i > last) { |
| 2446 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2447 | } |
| 2448 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2449 | i = next_index; |
| 2450 | last = i + 1; |
| 2451 | break; |
| 2452 | case '&': |
| 2453 | if (i > last) { |
| 2454 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2455 | } |
| 2456 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2457 | i = next_index; |
| 2458 | last = i + 1; |
| 2459 | break; |
| 2460 | case '0': |
| 2461 | case '1': |
| 2462 | case '2': |
| 2463 | case '3': |
| 2464 | case '4': |
| 2465 | case '5': |
| 2466 | case '6': |
| 2467 | case '7': |
| 2468 | case '8': |
| 2469 | case '9': { |
| 2470 | int capture_ref = c2 - '0'; |
| 2471 | if (capture_ref > capture_count) { |
| 2472 | i = next_index; |
| 2473 | continue; |
| 2474 | } |
| 2475 | int second_digit_index = next_index + 1; |
| 2476 | if (second_digit_index < length) { |
| 2477 | // Peek ahead to see if we have two digits. |
| 2478 | Char c3 = characters[second_digit_index]; |
| 2479 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2480 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2481 | if (double_digit_ref <= capture_count) { |
| 2482 | next_index = second_digit_index; |
| 2483 | capture_ref = double_digit_ref; |
| 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | if (capture_ref > 0) { |
| 2488 | if (i > last) { |
| 2489 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2490 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2491 | ASSERT(capture_ref <= capture_count); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2492 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2493 | last = next_index + 1; |
| 2494 | } |
| 2495 | i = next_index; |
| 2496 | break; |
| 2497 | } |
| 2498 | default: |
| 2499 | i = next_index; |
| 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | if (length > last) { |
| 2505 | if (last == 0) { |
| 2506 | parts->Add(ReplacementPart::ReplacementString()); |
| 2507 | } else { |
| 2508 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2509 | } |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | ZoneList<ReplacementPart> parts_; |
| 2514 | ZoneList<Handle<String> > replacement_substrings_; |
| 2515 | }; |
| 2516 | |
| 2517 | |
| 2518 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2519 | int capture_count, |
| 2520 | int subject_length) { |
| 2521 | ASSERT(replacement->IsFlat()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2522 | if (replacement->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2523 | AssertNoAllocation no_alloc; |
| 2524 | ParseReplacementPattern(&parts_, |
| 2525 | replacement->ToAsciiVector(), |
| 2526 | capture_count, |
| 2527 | subject_length); |
| 2528 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2529 | ASSERT(replacement->IsTwoByteRepresentation()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2530 | AssertNoAllocation no_alloc; |
| 2531 | |
| 2532 | ParseReplacementPattern(&parts_, |
| 2533 | replacement->ToUC16Vector(), |
| 2534 | capture_count, |
| 2535 | subject_length); |
| 2536 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2537 | Isolate* isolate = replacement->GetIsolate(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2538 | // Find substrings of replacement string and create them as String objects. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2539 | int substring_index = 0; |
| 2540 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2541 | int tag = parts_[i].tag; |
| 2542 | if (tag <= 0) { // A replacement string slice. |
| 2543 | int from = -tag; |
| 2544 | int to = parts_[i].data; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2545 | replacement_substrings_.Add( |
| 2546 | isolate->factory()->NewSubString(replacement, from, to)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2547 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2548 | parts_[i].data = substring_index; |
| 2549 | substring_index++; |
| 2550 | } else if (tag == REPLACEMENT_STRING) { |
| 2551 | replacement_substrings_.Add(replacement); |
| 2552 | parts_[i].data = substring_index; |
| 2553 | substring_index++; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | |
| 2559 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2560 | int match_from, |
| 2561 | int match_to, |
| 2562 | Handle<JSArray> last_match_info) { |
| 2563 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2564 | ReplacementPart part = parts_[i]; |
| 2565 | switch (part.tag) { |
| 2566 | case SUBJECT_PREFIX: |
| 2567 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2568 | break; |
| 2569 | case SUBJECT_SUFFIX: { |
| 2570 | int subject_length = part.data; |
| 2571 | if (match_to < subject_length) { |
| 2572 | builder->AddSubjectSlice(match_to, subject_length); |
| 2573 | } |
| 2574 | break; |
| 2575 | } |
| 2576 | case SUBJECT_CAPTURE: { |
| 2577 | int capture = part.data; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2578 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2579 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2580 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2581 | if (from >= 0 && to > from) { |
| 2582 | builder->AddSubjectSlice(from, to); |
| 2583 | } |
| 2584 | break; |
| 2585 | } |
| 2586 | case REPLACEMENT_SUBSTRING: |
| 2587 | case REPLACEMENT_STRING: |
| 2588 | builder->AddString(replacement_substrings_[part.data]); |
| 2589 | break; |
| 2590 | default: |
| 2591 | UNREACHABLE(); |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | |
| 2597 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2598 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2599 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2600 | String* subject, |
| 2601 | JSRegExp* regexp, |
| 2602 | String* replacement, |
| 2603 | JSArray* last_match_info) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2604 | ASSERT(subject->IsFlat()); |
| 2605 | ASSERT(replacement->IsFlat()); |
| 2606 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2607 | HandleScope handles(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2608 | |
| 2609 | int length = subject->length(); |
| 2610 | Handle<String> subject_handle(subject); |
| 2611 | Handle<JSRegExp> regexp_handle(regexp); |
| 2612 | Handle<String> replacement_handle(replacement); |
| 2613 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2614 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2615 | subject_handle, |
| 2616 | 0, |
| 2617 | last_match_info_handle); |
| 2618 | if (match.is_null()) { |
| 2619 | return Failure::Exception(); |
| 2620 | } |
| 2621 | if (match->IsNull()) { |
| 2622 | return *subject_handle; |
| 2623 | } |
| 2624 | |
| 2625 | int capture_count = regexp_handle->CaptureCount(); |
| 2626 | |
| 2627 | // CompiledReplacement uses zone allocation. |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 2628 | ZoneScope zone(isolate, DELETE_ON_EXIT); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2629 | CompiledReplacement compiled_replacement; |
| 2630 | compiled_replacement.Compile(replacement_handle, |
| 2631 | capture_count, |
| 2632 | length); |
| 2633 | |
| 2634 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 2635 | |
| 2636 | // Guessing the number of parts that the final result string is built |
| 2637 | // from. Global regexps can match any number of times, so we guess |
| 2638 | // conservatively. |
| 2639 | int expected_parts = |
| 2640 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2641 | ReplacementStringBuilder builder(isolate->heap(), |
| 2642 | subject_handle, |
| 2643 | expected_parts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2644 | |
| 2645 | // Index of end of last match. |
| 2646 | int prev = 0; |
| 2647 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2648 | // Number of parts added by compiled replacement plus preceeding |
| 2649 | // string and possibly suffix after last match. It is possible for |
| 2650 | // all components to use two elements when encoded as two smis. |
| 2651 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2652 | bool matched = true; |
| 2653 | do { |
| 2654 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2655 | // Increase the capacity of the builder before entering local handle-scope, |
| 2656 | // so its internal buffer can safely allocate a new handle if it grows. |
| 2657 | builder.EnsureCapacity(parts_added_per_loop); |
| 2658 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2659 | HandleScope loop_scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2660 | int start, end; |
| 2661 | { |
| 2662 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2663 | FixedArray* match_info_array = |
| 2664 | FixedArray::cast(last_match_info_handle->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2665 | |
| 2666 | ASSERT_EQ(capture_count * 2 + 2, |
| 2667 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 2668 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2669 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2670 | } |
| 2671 | |
| 2672 | if (prev < start) { |
| 2673 | builder.AddSubjectSlice(prev, start); |
| 2674 | } |
| 2675 | compiled_replacement.Apply(&builder, |
| 2676 | start, |
| 2677 | end, |
| 2678 | last_match_info_handle); |
| 2679 | prev = end; |
| 2680 | |
| 2681 | // Only continue checking for global regexps. |
| 2682 | if (!is_global) break; |
| 2683 | |
| 2684 | // Continue from where the match ended, unless it was an empty match. |
| 2685 | int next = end; |
| 2686 | if (start == end) { |
| 2687 | next = end + 1; |
| 2688 | if (next > length) break; |
| 2689 | } |
| 2690 | |
| 2691 | match = RegExpImpl::Exec(regexp_handle, |
| 2692 | subject_handle, |
| 2693 | next, |
| 2694 | last_match_info_handle); |
| 2695 | if (match.is_null()) { |
| 2696 | return Failure::Exception(); |
| 2697 | } |
| 2698 | matched = !match->IsNull(); |
| 2699 | } while (matched); |
| 2700 | |
| 2701 | if (prev < length) { |
| 2702 | builder.AddSubjectSlice(prev, length); |
| 2703 | } |
| 2704 | |
| 2705 | return *(builder.ToString()); |
| 2706 | } |
| 2707 | |
| 2708 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2709 | template <typename ResultSeqString> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2710 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2711 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2712 | String* subject, |
| 2713 | JSRegExp* regexp, |
| 2714 | JSArray* last_match_info) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2715 | ASSERT(subject->IsFlat()); |
| 2716 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2717 | HandleScope handles(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2718 | |
| 2719 | Handle<String> subject_handle(subject); |
| 2720 | Handle<JSRegExp> regexp_handle(regexp); |
| 2721 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2722 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2723 | subject_handle, |
| 2724 | 0, |
| 2725 | last_match_info_handle); |
| 2726 | if (match.is_null()) return Failure::Exception(); |
| 2727 | if (match->IsNull()) return *subject_handle; |
| 2728 | |
| 2729 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2730 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2731 | int start, end; |
| 2732 | { |
| 2733 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 2734 | FixedArray* match_info_array = |
| 2735 | FixedArray::cast(last_match_info_handle->elements()); |
| 2736 | |
| 2737 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2738 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2739 | } |
| 2740 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2741 | int length = subject_handle->length(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2742 | int new_length = length - (end - start); |
| 2743 | if (new_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2744 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2745 | } |
| 2746 | Handle<ResultSeqString> answer; |
| 2747 | if (ResultSeqString::kHasAsciiEncoding) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2748 | answer = Handle<ResultSeqString>::cast( |
| 2749 | isolate->factory()->NewRawAsciiString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2750 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2751 | answer = Handle<ResultSeqString>::cast( |
| 2752 | isolate->factory()->NewRawTwoByteString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | // If the regexp isn't global, only match once. |
| 2756 | if (!regexp_handle->GetFlags().is_global()) { |
| 2757 | if (start > 0) { |
| 2758 | String::WriteToFlat(*subject_handle, |
| 2759 | answer->GetChars(), |
| 2760 | 0, |
| 2761 | start); |
| 2762 | } |
| 2763 | if (end < length) { |
| 2764 | String::WriteToFlat(*subject_handle, |
| 2765 | answer->GetChars() + start, |
| 2766 | end, |
| 2767 | length); |
| 2768 | } |
| 2769 | return *answer; |
| 2770 | } |
| 2771 | |
| 2772 | int prev = 0; // Index of end of last match. |
| 2773 | int next = 0; // Start of next search (prev unless last match was empty). |
| 2774 | int position = 0; |
| 2775 | |
| 2776 | do { |
| 2777 | if (prev < start) { |
| 2778 | // Add substring subject[prev;start] to answer string. |
| 2779 | String::WriteToFlat(*subject_handle, |
| 2780 | answer->GetChars() + position, |
| 2781 | prev, |
| 2782 | start); |
| 2783 | position += start - prev; |
| 2784 | } |
| 2785 | prev = end; |
| 2786 | next = end; |
| 2787 | // Continue from where the match ended, unless it was an empty match. |
| 2788 | if (start == end) { |
| 2789 | next++; |
| 2790 | if (next > length) break; |
| 2791 | } |
| 2792 | match = RegExpImpl::Exec(regexp_handle, |
| 2793 | subject_handle, |
| 2794 | next, |
| 2795 | last_match_info_handle); |
| 2796 | if (match.is_null()) return Failure::Exception(); |
| 2797 | if (match->IsNull()) break; |
| 2798 | |
| 2799 | ASSERT(last_match_info_handle->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2800 | HandleScope loop_scope(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2801 | { |
| 2802 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 2803 | FixedArray* match_info_array = |
| 2804 | FixedArray::cast(last_match_info_handle->elements()); |
| 2805 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2806 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2807 | } |
| 2808 | } while (true); |
| 2809 | |
| 2810 | if (prev < length) { |
| 2811 | // Add substring subject[prev;length] to answer string. |
| 2812 | String::WriteToFlat(*subject_handle, |
| 2813 | answer->GetChars() + position, |
| 2814 | prev, |
| 2815 | length); |
| 2816 | position += length - prev; |
| 2817 | } |
| 2818 | |
| 2819 | if (position == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2820 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | // Shorten string and fill |
| 2824 | int string_size = ResultSeqString::SizeFor(position); |
| 2825 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 2826 | int delta = allocated_string_size - string_size; |
| 2827 | |
| 2828 | answer->set_length(position); |
| 2829 | if (delta == 0) return *answer; |
| 2830 | |
| 2831 | Address end_of_string = answer->address() + string_size; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2832 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2833 | |
| 2834 | return *answer; |
| 2835 | } |
| 2836 | |
| 2837 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2838 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceRegExpWithString) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2839 | ASSERT(args.length() == 4); |
| 2840 | |
| 2841 | CONVERT_CHECKED(String, subject, args[0]); |
| 2842 | if (!subject->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2843 | Object* flat_subject; |
| 2844 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 2845 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 2846 | return maybe_flat_subject; |
| 2847 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2848 | } |
| 2849 | subject = String::cast(flat_subject); |
| 2850 | } |
| 2851 | |
| 2852 | CONVERT_CHECKED(String, replacement, args[2]); |
| 2853 | if (!replacement->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2854 | Object* flat_replacement; |
| 2855 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 2856 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 2857 | return maybe_flat_replacement; |
| 2858 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2859 | } |
| 2860 | replacement = String::cast(flat_replacement); |
| 2861 | } |
| 2862 | |
| 2863 | CONVERT_CHECKED(JSRegExp, regexp, args[1]); |
| 2864 | CONVERT_CHECKED(JSArray, last_match_info, args[3]); |
| 2865 | |
| 2866 | ASSERT(last_match_info->HasFastElements()); |
| 2867 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2868 | if (replacement->length() == 0) { |
| 2869 | if (subject->HasOnlyAsciiChars()) { |
| 2870 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2871 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2872 | } else { |
| 2873 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2874 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2875 | } |
| 2876 | } |
| 2877 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2878 | return StringReplaceRegExpWithString(isolate, |
| 2879 | subject, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2880 | regexp, |
| 2881 | replacement, |
| 2882 | last_match_info); |
| 2883 | } |
| 2884 | |
| 2885 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2886 | // Perform string match of pattern on subject, starting at start index. |
| 2887 | // Caller must ensure that 0 <= start_index <= sub->length(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2888 | // and should check that pat->length() + start_index <= sub->length(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2889 | int Runtime::StringMatch(Isolate* isolate, |
| 2890 | Handle<String> sub, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2891 | Handle<String> pat, |
| 2892 | int start_index) { |
| 2893 | ASSERT(0 <= start_index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2894 | ASSERT(start_index <= sub->length()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2895 | |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 2896 | int pattern_length = pat->length(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2897 | if (pattern_length == 0) return start_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2898 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2899 | int subject_length = sub->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2900 | if (start_index + pattern_length > subject_length) return -1; |
| 2901 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2902 | if (!sub->IsFlat()) FlattenString(sub); |
| 2903 | if (!pat->IsFlat()) FlattenString(pat); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2904 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2905 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2906 | // Extract flattened substrings of cons strings before determining asciiness. |
| 2907 | String* seq_sub = *sub; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2908 | if (seq_sub->IsConsString()) seq_sub = ConsString::cast(seq_sub)->first(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2909 | String* seq_pat = *pat; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2910 | if (seq_pat->IsConsString()) seq_pat = ConsString::cast(seq_pat)->first(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2911 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2912 | // dispatch on type of strings |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2913 | if (seq_pat->IsAsciiRepresentation()) { |
| 2914 | Vector<const char> pat_vector = seq_pat->ToAsciiVector(); |
| 2915 | if (seq_sub->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2916 | return SearchString(isolate, |
| 2917 | seq_sub->ToAsciiVector(), |
| 2918 | pat_vector, |
| 2919 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2920 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2921 | return SearchString(isolate, |
| 2922 | seq_sub->ToUC16Vector(), |
| 2923 | pat_vector, |
| 2924 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2925 | } |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2926 | Vector<const uc16> pat_vector = seq_pat->ToUC16Vector(); |
| 2927 | if (seq_sub->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2928 | return SearchString(isolate, |
| 2929 | seq_sub->ToAsciiVector(), |
| 2930 | pat_vector, |
| 2931 | start_index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2932 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2933 | return SearchString(isolate, |
| 2934 | seq_sub->ToUC16Vector(), |
| 2935 | pat_vector, |
| 2936 | start_index); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2940 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2941 | HandleScope scope(isolate); // create a new handle scope |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2942 | ASSERT(args.length() == 3); |
| 2943 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2944 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 2945 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 2946 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2947 | Object* index = args[2]; |
| 2948 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2949 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2950 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2951 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2952 | int position = |
| 2953 | Runtime::StringMatch(isolate, sub, pat, start_index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2954 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2958 | template <typename schar, typename pchar> |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2959 | static int StringMatchBackwards(Vector<const schar> subject, |
| 2960 | Vector<const pchar> pattern, |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2961 | int idx) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2962 | int pattern_length = pattern.length(); |
| 2963 | ASSERT(pattern_length >= 1); |
| 2964 | ASSERT(idx + pattern_length <= subject.length()); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2965 | |
| 2966 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2967 | for (int i = 0; i < pattern_length; i++) { |
| 2968 | uc16 c = pattern[i]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2969 | if (c > String::kMaxAsciiCharCode) { |
| 2970 | return -1; |
| 2971 | } |
| 2972 | } |
| 2973 | } |
| 2974 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2975 | pchar pattern_first_char = pattern[0]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2976 | for (int i = idx; i >= 0; i--) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2977 | if (subject[i] != pattern_first_char) continue; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2978 | int j = 1; |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2979 | while (j < pattern_length) { |
| 2980 | if (pattern[j] != subject[i+j]) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2981 | break; |
| 2982 | } |
| 2983 | j++; |
| 2984 | } |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2985 | if (j == pattern_length) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2986 | return i; |
| 2987 | } |
| 2988 | } |
| 2989 | return -1; |
| 2990 | } |
| 2991 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2992 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLastIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2993 | HandleScope scope(isolate); // create a new handle scope |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2994 | ASSERT(args.length() == 3); |
| 2995 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2996 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 2997 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 2998 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2999 | Object* index = args[2]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3000 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3001 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3002 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3003 | uint32_t pat_length = pat->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3004 | uint32_t sub_length = sub->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3005 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3006 | if (start_index + pat_length > sub_length) { |
| 3007 | start_index = sub_length - pat_length; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 3008 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3009 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3010 | if (pat_length == 0) { |
| 3011 | return Smi::FromInt(start_index); |
| 3012 | } |
| 3013 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3014 | if (!sub->IsFlat()) FlattenString(sub); |
| 3015 | if (!pat->IsFlat()) FlattenString(pat); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3016 | |
| 3017 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3018 | |
| 3019 | int position = -1; |
| 3020 | |
| 3021 | if (pat->IsAsciiRepresentation()) { |
| 3022 | Vector<const char> pat_vector = pat->ToAsciiVector(); |
| 3023 | if (sub->IsAsciiRepresentation()) { |
| 3024 | position = StringMatchBackwards(sub->ToAsciiVector(), |
| 3025 | pat_vector, |
| 3026 | start_index); |
| 3027 | } else { |
| 3028 | position = StringMatchBackwards(sub->ToUC16Vector(), |
| 3029 | pat_vector, |
| 3030 | start_index); |
| 3031 | } |
| 3032 | } else { |
| 3033 | Vector<const uc16> pat_vector = pat->ToUC16Vector(); |
| 3034 | if (sub->IsAsciiRepresentation()) { |
| 3035 | position = StringMatchBackwards(sub->ToAsciiVector(), |
| 3036 | pat_vector, |
| 3037 | start_index); |
| 3038 | } else { |
| 3039 | position = StringMatchBackwards(sub->ToUC16Vector(), |
| 3040 | pat_vector, |
| 3041 | start_index); |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3049 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLocaleCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3050 | NoHandleAllocation ha; |
| 3051 | ASSERT(args.length() == 2); |
| 3052 | |
| 3053 | CONVERT_CHECKED(String, str1, args[0]); |
| 3054 | CONVERT_CHECKED(String, str2, args[1]); |
| 3055 | |
| 3056 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3057 | int str1_length = str1->length(); |
| 3058 | int str2_length = str2->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3059 | |
| 3060 | // Decide trivial cases without flattening. |
| 3061 | if (str1_length == 0) { |
| 3062 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3063 | return Smi::FromInt(-str2_length); |
| 3064 | } else { |
| 3065 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3066 | } |
| 3067 | |
| 3068 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3069 | |
| 3070 | // No need to flatten if we are going to find the answer on the first |
| 3071 | // character. At this point we know there is at least one character |
| 3072 | // in each string, due to the trivial case handling above. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3073 | int d = str1->Get(0) - str2->Get(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3074 | if (d != 0) return Smi::FromInt(d); |
| 3075 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3076 | str1->TryFlatten(); |
| 3077 | str2->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3078 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3079 | StringInputBuffer& buf1 = |
| 3080 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3081 | StringInputBuffer& buf2 = |
| 3082 | *isolate->runtime_state()->string_locale_compare_buf2(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3083 | |
| 3084 | buf1.Reset(str1); |
| 3085 | buf2.Reset(str2); |
| 3086 | |
| 3087 | for (int i = 0; i < end; i++) { |
| 3088 | uint16_t char1 = buf1.GetNext(); |
| 3089 | uint16_t char2 = buf2.GetNext(); |
| 3090 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3091 | } |
| 3092 | |
| 3093 | return Smi::FromInt(str1_length - str2_length); |
| 3094 | } |
| 3095 | |
| 3096 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3097 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SubString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3098 | NoHandleAllocation ha; |
| 3099 | ASSERT(args.length() == 3); |
| 3100 | |
| 3101 | CONVERT_CHECKED(String, value, args[0]); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3102 | int start, end; |
| 3103 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3104 | // the common case where from and to are Smis. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3105 | if (args[1]->IsSmi() && args[2]->IsSmi()) { |
| 3106 | CONVERT_SMI_ARG_CHECKED(from_number, 1); |
| 3107 | CONVERT_SMI_ARG_CHECKED(to_number, 2); |
| 3108 | start = from_number; |
| 3109 | end = to_number; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3110 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3111 | CONVERT_DOUBLE_ARG_CHECKED(from_number, 1); |
| 3112 | CONVERT_DOUBLE_ARG_CHECKED(to_number, 2); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3113 | start = FastD2I(from_number); |
| 3114 | end = FastD2I(to_number); |
| 3115 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3116 | RUNTIME_ASSERT(end >= start); |
| 3117 | RUNTIME_ASSERT(start >= 0); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3118 | RUNTIME_ASSERT(end <= value->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3119 | isolate->counters()->sub_string_runtime()->Increment(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3120 | return value->SubString(start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3124 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringMatch) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3125 | ASSERT_EQ(3, args.length()); |
| 3126 | |
| 3127 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 3128 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3129 | CONVERT_ARG_CHECKED(JSArray, regexp_info, 2); |
| 3130 | HandleScope handles; |
| 3131 | |
| 3132 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3133 | |
| 3134 | if (match.is_null()) { |
| 3135 | return Failure::Exception(); |
| 3136 | } |
| 3137 | if (match->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3138 | return isolate->heap()->null_value(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3139 | } |
| 3140 | int length = subject->length(); |
| 3141 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 3142 | ZoneScope zone_space(isolate, DELETE_ON_EXIT); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3143 | ZoneList<int> offsets(8); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3144 | int start; |
| 3145 | int end; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3146 | do { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3147 | { |
| 3148 | AssertNoAllocation no_alloc; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3149 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3150 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3151 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3152 | } |
| 3153 | offsets.Add(start); |
| 3154 | offsets.Add(end); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3155 | if (start == end) if (++end > length) break; |
| 3156 | match = RegExpImpl::Exec(regexp, subject, end, regexp_info); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3157 | if (match.is_null()) { |
| 3158 | return Failure::Exception(); |
| 3159 | } |
| 3160 | } while (!match->IsNull()); |
| 3161 | int matches = offsets.length() / 2; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3162 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3163 | Handle<String> substring = isolate->factory()-> |
| 3164 | NewSubString(subject, offsets.at(0), offsets.at(1)); |
| 3165 | elements->set(0, *substring); |
| 3166 | for (int i = 1; i < matches ; i++) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3167 | int from = offsets.at(i * 2); |
| 3168 | int to = offsets.at(i * 2 + 1); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3169 | Handle<String> substring = isolate->factory()-> |
| 3170 | NewProperSubString(subject, from, to); |
| 3171 | elements->set(i, *substring); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3172 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3173 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3174 | result->set_length(Smi::FromInt(matches)); |
| 3175 | return *result; |
| 3176 | } |
| 3177 | |
| 3178 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3179 | // Two smis before and after the match, for very long strings. |
| 3180 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3181 | |
| 3182 | |
| 3183 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3184 | Handle<JSArray> last_match_info, |
| 3185 | int match_start, |
| 3186 | int match_end) { |
| 3187 | // Fill last_match_info with a single capture. |
| 3188 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3189 | AssertNoAllocation no_gc; |
| 3190 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3191 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3192 | RegExpImpl::SetLastInput(elements, *subject); |
| 3193 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3194 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3195 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3196 | } |
| 3197 | |
| 3198 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3199 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3200 | static bool SearchStringMultiple(Isolate* isolate, |
| 3201 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3202 | Vector<const PatternChar> pattern, |
| 3203 | String* pattern_string, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3204 | FixedArrayBuilder* builder, |
| 3205 | int* match_pos) { |
| 3206 | int pos = *match_pos; |
| 3207 | int subject_length = subject.length(); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3208 | int pattern_length = pattern.length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3209 | int max_search_start = subject_length - pattern_length; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3210 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3211 | while (pos <= max_search_start) { |
| 3212 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3213 | *match_pos = pos; |
| 3214 | return false; |
| 3215 | } |
| 3216 | // Position of end of previous match. |
| 3217 | int match_end = pos + pattern_length; |
| 3218 | int new_pos = search.Search(subject, match_end); |
| 3219 | if (new_pos >= 0) { |
| 3220 | // A match. |
| 3221 | if (new_pos > match_end) { |
| 3222 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3223 | match_end, |
| 3224 | new_pos); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3225 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3226 | pos = new_pos; |
| 3227 | builder->Add(pattern_string); |
| 3228 | } else { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3229 | break; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3230 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3231 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3232 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3233 | if (pos < max_search_start) { |
| 3234 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3235 | pos + pattern_length, |
| 3236 | subject_length); |
| 3237 | } |
| 3238 | *match_pos = pos; |
| 3239 | return true; |
| 3240 | } |
| 3241 | |
| 3242 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3243 | static bool SearchStringMultiple(Isolate* isolate, |
| 3244 | Handle<String> subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3245 | Handle<String> pattern, |
| 3246 | Handle<JSArray> last_match_info, |
| 3247 | FixedArrayBuilder* builder) { |
| 3248 | ASSERT(subject->IsFlat()); |
| 3249 | ASSERT(pattern->IsFlat()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Treating as if a previous match was before first character. |
| 3252 | int match_pos = -pattern->length(); |
| 3253 | |
| 3254 | for (;;) { // Break when search complete. |
| 3255 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3256 | AssertNoAllocation no_gc; |
| 3257 | if (subject->IsAsciiRepresentation()) { |
| 3258 | Vector<const char> subject_vector = subject->ToAsciiVector(); |
| 3259 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3260 | if (SearchStringMultiple(isolate, |
| 3261 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3262 | pattern->ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3263 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3264 | builder, |
| 3265 | &match_pos)) break; |
| 3266 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3267 | if (SearchStringMultiple(isolate, |
| 3268 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3269 | pattern->ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3270 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3271 | builder, |
| 3272 | &match_pos)) break; |
| 3273 | } |
| 3274 | } else { |
| 3275 | Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
| 3276 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3277 | if (SearchStringMultiple(isolate, |
| 3278 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3279 | pattern->ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3280 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3281 | builder, |
| 3282 | &match_pos)) break; |
| 3283 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3284 | if (SearchStringMultiple(isolate, |
| 3285 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3286 | pattern->ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3287 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3288 | builder, |
| 3289 | &match_pos)) break; |
| 3290 | } |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | if (match_pos >= 0) { |
| 3295 | SetLastMatchInfoNoCaptures(subject, |
| 3296 | last_match_info, |
| 3297 | match_pos, |
| 3298 | match_pos + pattern->length()); |
| 3299 | return true; |
| 3300 | } |
| 3301 | return false; // No matches at all. |
| 3302 | } |
| 3303 | |
| 3304 | |
| 3305 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3306 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3307 | Handle<String> subject, |
| 3308 | Handle<JSRegExp> regexp, |
| 3309 | Handle<JSArray> last_match_array, |
| 3310 | FixedArrayBuilder* builder) { |
| 3311 | ASSERT(subject->IsFlat()); |
| 3312 | int match_start = -1; |
| 3313 | int match_end = 0; |
| 3314 | int pos = 0; |
| 3315 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3316 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3317 | |
| 3318 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3319 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3320 | int subject_length = subject->length(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3321 | bool first = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3322 | |
| 3323 | for (;;) { // Break on failure, return on exception. |
| 3324 | RegExpImpl::IrregexpResult result = |
| 3325 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3326 | subject, |
| 3327 | pos, |
| 3328 | register_vector); |
| 3329 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3330 | match_start = register_vector[0]; |
| 3331 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3332 | if (match_end < match_start) { |
| 3333 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3334 | match_end, |
| 3335 | match_start); |
| 3336 | } |
| 3337 | match_end = register_vector[1]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3338 | HandleScope loop_scope(isolate); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3339 | if (!first) { |
| 3340 | builder->Add(*isolate->factory()->NewProperSubString(subject, |
| 3341 | match_start, |
| 3342 | match_end)); |
| 3343 | } else { |
| 3344 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3345 | match_start, |
| 3346 | match_end)); |
| 3347 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3348 | if (match_start != match_end) { |
| 3349 | pos = match_end; |
| 3350 | } else { |
| 3351 | pos = match_end + 1; |
| 3352 | if (pos > subject_length) break; |
| 3353 | } |
| 3354 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3355 | break; |
| 3356 | } else { |
| 3357 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3358 | return result; |
| 3359 | } |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3360 | first = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | if (match_start >= 0) { |
| 3364 | if (match_end < subject_length) { |
| 3365 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3366 | match_end, |
| 3367 | subject_length); |
| 3368 | } |
| 3369 | SetLastMatchInfoNoCaptures(subject, |
| 3370 | last_match_array, |
| 3371 | match_start, |
| 3372 | match_end); |
| 3373 | return RegExpImpl::RE_SUCCESS; |
| 3374 | } else { |
| 3375 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | |
| 3380 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3381 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3382 | Handle<String> subject, |
| 3383 | Handle<JSRegExp> regexp, |
| 3384 | Handle<JSArray> last_match_array, |
| 3385 | FixedArrayBuilder* builder) { |
| 3386 | |
| 3387 | ASSERT(subject->IsFlat()); |
| 3388 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3389 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3390 | |
| 3391 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3392 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3393 | |
| 3394 | RegExpImpl::IrregexpResult result = |
| 3395 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3396 | subject, |
| 3397 | 0, |
| 3398 | register_vector); |
| 3399 | |
| 3400 | int capture_count = regexp->CaptureCount(); |
| 3401 | int subject_length = subject->length(); |
| 3402 | |
| 3403 | // Position to search from. |
| 3404 | int pos = 0; |
| 3405 | // End of previous match. Differs from pos if match was empty. |
| 3406 | int match_end = 0; |
| 3407 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3408 | // Need to keep a copy of the previous match for creating last_match_info |
| 3409 | // at the end, so we have two vectors that we swap between. |
| 3410 | OffsetsVector registers2(required_registers); |
| 3411 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3412 | bool first = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3413 | do { |
| 3414 | int match_start = register_vector[0]; |
| 3415 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3416 | if (match_end < match_start) { |
| 3417 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3418 | match_end, |
| 3419 | match_start); |
| 3420 | } |
| 3421 | match_end = register_vector[1]; |
| 3422 | |
| 3423 | { |
| 3424 | // Avoid accumulating new handles inside loop. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3425 | HandleScope temp_scope(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3426 | // Arguments array to replace function is match, captures, index and |
| 3427 | // subject, i.e., 3 + capture count in total. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3428 | Handle<FixedArray> elements = |
| 3429 | isolate->factory()->NewFixedArray(3 + capture_count); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3430 | Handle<String> match; |
| 3431 | if (!first) { |
| 3432 | match = isolate->factory()->NewProperSubString(subject, |
| 3433 | match_start, |
| 3434 | match_end); |
| 3435 | } else { |
| 3436 | match = isolate->factory()->NewSubString(subject, |
| 3437 | match_start, |
| 3438 | match_end); |
| 3439 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3440 | elements->set(0, *match); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3441 | for (int i = 1; i <= capture_count; i++) { |
| 3442 | int start = register_vector[i * 2]; |
| 3443 | if (start >= 0) { |
| 3444 | int end = register_vector[i * 2 + 1]; |
| 3445 | ASSERT(start <= end); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3446 | Handle<String> substring; |
| 3447 | if (!first) { |
| 3448 | substring = isolate->factory()->NewProperSubString(subject, |
| 3449 | start, |
| 3450 | end); |
| 3451 | } else { |
| 3452 | substring = isolate->factory()->NewSubString(subject, start, end); |
| 3453 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3454 | elements->set(i, *substring); |
| 3455 | } else { |
| 3456 | ASSERT(register_vector[i * 2 + 1] < 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3457 | elements->set(i, isolate->heap()->undefined_value()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3458 | } |
| 3459 | } |
| 3460 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3461 | elements->set(capture_count + 2, *subject); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3462 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3463 | } |
| 3464 | // Swap register vectors, so the last successful match is in |
| 3465 | // prev_register_vector. |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3466 | Vector<int32_t> tmp = prev_register_vector; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3467 | prev_register_vector = register_vector; |
| 3468 | register_vector = tmp; |
| 3469 | |
| 3470 | if (match_end > match_start) { |
| 3471 | pos = match_end; |
| 3472 | } else { |
| 3473 | pos = match_end + 1; |
| 3474 | if (pos > subject_length) { |
| 3475 | break; |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3480 | subject, |
| 3481 | pos, |
| 3482 | register_vector); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3483 | first = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3484 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3485 | |
| 3486 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3487 | // Finished matching, with at least one match. |
| 3488 | if (match_end < subject_length) { |
| 3489 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3490 | match_end, |
| 3491 | subject_length); |
| 3492 | } |
| 3493 | |
| 3494 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3495 | int last_match_array_size = |
| 3496 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3497 | last_match_array->EnsureSize(last_match_array_size); |
| 3498 | AssertNoAllocation no_gc; |
| 3499 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3500 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3501 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3502 | RegExpImpl::SetLastInput(elements, *subject); |
| 3503 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3504 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3505 | } |
| 3506 | return RegExpImpl::RE_SUCCESS; |
| 3507 | } |
| 3508 | } |
| 3509 | // No matches at all, return failure or exception result directly. |
| 3510 | return result; |
| 3511 | } |
| 3512 | |
| 3513 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3514 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3515 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3516 | HandleScope handles(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3517 | |
| 3518 | CONVERT_ARG_CHECKED(String, subject, 1); |
| 3519 | if (!subject->IsFlat()) { FlattenString(subject); } |
| 3520 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 3521 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 2); |
| 3522 | CONVERT_ARG_CHECKED(JSArray, result_array, 3); |
| 3523 | |
| 3524 | ASSERT(last_match_info->HasFastElements()); |
| 3525 | ASSERT(regexp->GetFlags().is_global()); |
| 3526 | Handle<FixedArray> result_elements; |
| 3527 | if (result_array->HasFastElements()) { |
| 3528 | result_elements = |
| 3529 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 3530 | } |
| 3531 | if (result_elements.is_null() || result_elements->length() < 16) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3532 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3533 | } |
| 3534 | FixedArrayBuilder builder(result_elements); |
| 3535 | |
| 3536 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 3537 | Handle<String> pattern( |
| 3538 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3539 | ASSERT(pattern->IsFlat()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3540 | if (SearchStringMultiple(isolate, subject, pattern, |
| 3541 | last_match_info, &builder)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3542 | return *builder.ToJSArray(result_array); |
| 3543 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3544 | return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 3548 | |
| 3549 | RegExpImpl::IrregexpResult result; |
| 3550 | if (regexp->CaptureCount() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3551 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 3552 | subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3553 | regexp, |
| 3554 | last_match_info, |
| 3555 | &builder); |
| 3556 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3557 | result = SearchRegExpMultiple(isolate, |
| 3558 | subject, |
| 3559 | regexp, |
| 3560 | last_match_info, |
| 3561 | &builder); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3562 | } |
| 3563 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3564 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3565 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3566 | return Failure::Exception(); |
| 3567 | } |
| 3568 | |
| 3569 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3570 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToRadixString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3571 | NoHandleAllocation ha; |
| 3572 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3573 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3574 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3575 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3576 | // Fast case where the result is a one character string. |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 3577 | if (args[0]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3578 | int value = args.smi_at(0); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3579 | if (value >= 0 && value < radix) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3580 | // Character array used for conversion. |
| 3581 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3582 | return isolate->heap()-> |
| 3583 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | // Slow case. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3588 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3589 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3590 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3591 | } |
| 3592 | if (isinf(value)) { |
| 3593 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3594 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3595 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3596 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3597 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3598 | char* str = DoubleToRadixCString(value, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3599 | MaybeObject* result = |
| 3600 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3601 | DeleteArray(str); |
| 3602 | return result; |
| 3603 | } |
| 3604 | |
| 3605 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3606 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToFixed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3607 | NoHandleAllocation ha; |
| 3608 | ASSERT(args.length() == 2); |
| 3609 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3610 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3611 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3612 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3613 | } |
| 3614 | if (isinf(value)) { |
| 3615 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3616 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3617 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3618 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3619 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3620 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3621 | int f = FastD2I(f_number); |
| 3622 | RUNTIME_ASSERT(f >= 0); |
| 3623 | char* str = DoubleToFixedCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3624 | MaybeObject* res = |
| 3625 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3626 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3627 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3631 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToExponential) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3632 | NoHandleAllocation ha; |
| 3633 | ASSERT(args.length() == 2); |
| 3634 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3635 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3636 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3637 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3638 | } |
| 3639 | if (isinf(value)) { |
| 3640 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3641 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3642 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3643 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3644 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3645 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3646 | int f = FastD2I(f_number); |
| 3647 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 3648 | char* str = DoubleToExponentialCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3649 | MaybeObject* res = |
| 3650 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3651 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3652 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3656 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPrecision) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3657 | NoHandleAllocation ha; |
| 3658 | ASSERT(args.length() == 2); |
| 3659 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3660 | CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3661 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3662 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3663 | } |
| 3664 | if (isinf(value)) { |
| 3665 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3666 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3667 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3668 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3669 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3670 | CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3671 | int f = FastD2I(f_number); |
| 3672 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 3673 | char* str = DoubleToPrecisionCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3674 | MaybeObject* res = |
| 3675 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3676 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3677 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | |
| 3681 | // Returns a single character string where first character equals |
| 3682 | // string->Get(index). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3683 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3684 | if (index < static_cast<uint32_t>(string->length())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3685 | string->TryFlatten(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3686 | return LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3687 | string->Get(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3688 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3689 | return Execution::CharAt(string, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3693 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 3694 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3695 | uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3696 | // Handle [] indexing on Strings |
| 3697 | if (object->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3698 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 3699 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | // Handle [] indexing on String objects |
| 3703 | if (object->IsStringObjectWithCharacterAt(index)) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3704 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 3705 | Handle<Object> result = |
| 3706 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 3707 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3711 | Handle<Object> prototype = GetPrototype(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3712 | return prototype->GetElement(index); |
| 3713 | } |
| 3714 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3715 | return GetElement(object, index); |
| 3716 | } |
| 3717 | |
| 3718 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3719 | MaybeObject* Runtime::GetElement(Handle<Object> object, uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3720 | return object->GetElement(index); |
| 3721 | } |
| 3722 | |
| 3723 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3724 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 3725 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3726 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3727 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3728 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3729 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3730 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3731 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3732 | isolate->factory()->NewTypeError("non_object_property_load", |
| 3733 | HandleVector(args, 2)); |
| 3734 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | // Check if the given key is an array index. |
| 3738 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3739 | if (key->ToArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3740 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | // 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] | 3744 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3745 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3746 | name = Handle<String>::cast(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3747 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3748 | bool has_pending_exception = false; |
| 3749 | Handle<Object> converted = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3750 | Execution::ToString(key, &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3751 | if (has_pending_exception) return Failure::Exception(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3752 | name = Handle<String>::cast(converted); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3755 | // 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] | 3756 | // the element if so. |
| 3757 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3758 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3759 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3760 | return object->GetProperty(*name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3765 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3766 | NoHandleAllocation ha; |
| 3767 | ASSERT(args.length() == 2); |
| 3768 | |
| 3769 | Handle<Object> object = args.at<Object>(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3770 | Handle<Object> key = args.at<Object>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3771 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3772 | return Runtime::GetObjectProperty(isolate, object, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3776 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3777 | RUNTIME_FUNCTION(MaybeObject*, Runtime_KeyedGetProperty) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3778 | NoHandleAllocation ha; |
| 3779 | ASSERT(args.length() == 2); |
| 3780 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3781 | // Fast cases for getting named properties of the receiver JSObject |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3782 | // itself. |
| 3783 | // |
| 3784 | // The global proxy objects has to be excluded since LocalLookup on |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3785 | // 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] | 3786 | // global proxy object never has properties. This is the case |
| 3787 | // because the global proxy object forwards everything to its hidden |
| 3788 | // prototype including local lookups. |
| 3789 | // |
| 3790 | // Additionally, we need to make sure that we do not cache results |
| 3791 | // for objects that require access checks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3792 | if (args[0]->IsJSObject() && |
| 3793 | !args[0]->IsJSGlobalProxy() && |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3794 | !args[0]->IsAccessCheckNeeded() && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3795 | args[1]->IsString()) { |
| 3796 | JSObject* receiver = JSObject::cast(args[0]); |
| 3797 | String* key = String::cast(args[1]); |
| 3798 | if (receiver->HasFastProperties()) { |
| 3799 | // Attempt to use lookup cache. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3800 | Map* receiver_map = receiver->map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3801 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 3802 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3803 | if (offset != -1) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3804 | Object* value = receiver->FastPropertyAt(offset); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3805 | return value->IsTheHole() ? isolate->heap()->undefined_value() : value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3806 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3807 | // Lookup cache miss. Perform lookup and update the cache if appropriate. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3808 | LookupResult result; |
| 3809 | receiver->LocalLookup(key, &result); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 3810 | if (result.IsProperty() && result.type() == FIELD) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3811 | int offset = result.GetFieldIndex(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3812 | keyed_lookup_cache->Update(receiver_map, key, offset); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3813 | return receiver->FastPropertyAt(offset); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3814 | } |
| 3815 | } else { |
| 3816 | // Attempt dictionary lookup. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3817 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 3818 | int entry = dictionary->FindEntry(key); |
| 3819 | if ((entry != StringDictionary::kNotFound) && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3820 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3821 | Object* value = dictionary->ValueAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3822 | if (!receiver->IsGlobalObject()) return value; |
| 3823 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 3824 | if (!value->IsTheHole()) return value; |
| 3825 | // If value is the hole do the general lookup. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3826 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3827 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3828 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 3829 | // Fast case for string indexing using [] with a smi index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3830 | HandleScope scope(isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3831 | Handle<String> str = args.at<String>(0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3832 | int index = args.smi_at(1); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3833 | if (index >= 0 && index < str->length()) { |
| 3834 | Handle<Object> result = GetCharAt(str, index); |
| 3835 | return *result; |
| 3836 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3837 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3838 | |
| 3839 | // Fall back to GetObjectProperty. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3840 | return Runtime::GetObjectProperty(isolate, |
| 3841 | args.at<Object>(0), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3842 | args.at<Object>(1)); |
| 3843 | } |
| 3844 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3845 | // Implements part of 8.12.9 DefineOwnProperty. |
| 3846 | // There are 3 cases that lead here: |
| 3847 | // Step 4b - define a new accessor property. |
| 3848 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 3849 | // Step 12 - update an existing accessor property with an accessor or generic |
| 3850 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3851 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineAccessorProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3852 | ASSERT(args.length() == 5); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3853 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3854 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 3855 | CONVERT_CHECKED(String, name, args[1]); |
| 3856 | CONVERT_CHECKED(Smi, flag_setter, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3857 | Object* fun = args[3]; |
| 3858 | RUNTIME_ASSERT(fun->IsJSFunction() || fun->IsUndefined()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3859 | CONVERT_CHECKED(Smi, flag_attr, args[4]); |
| 3860 | int unchecked = flag_attr->value(); |
| 3861 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 3862 | RUNTIME_ASSERT(!obj->IsNull()); |
| 3863 | LookupResult result; |
| 3864 | obj->LocalLookupRealNamedProperty(name, &result); |
| 3865 | |
| 3866 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 3867 | // If an existing property is either FIELD, NORMAL or CONSTANT_FUNCTION |
| 3868 | // delete it to avoid running into trouble in DefineAccessor, which |
| 3869 | // handles this incorrectly if the property is readonly (does nothing) |
| 3870 | if (result.IsProperty() && |
| 3871 | (result.type() == FIELD || result.type() == NORMAL |
| 3872 | || result.type() == CONSTANT_FUNCTION)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3873 | Object* ok; |
| 3874 | { MaybeObject* maybe_ok = |
| 3875 | obj->DeleteProperty(name, JSObject::NORMAL_DELETION); |
| 3876 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3877 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3878 | } |
| 3879 | return obj->DefineAccessor(name, flag_setter->value() == 0, fun, attr); |
| 3880 | } |
| 3881 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3882 | // Implements part of 8.12.9 DefineOwnProperty. |
| 3883 | // There are 3 cases that lead here: |
| 3884 | // Step 4a - define a new data property. |
| 3885 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 3886 | // Step 12 - update an existing data property with a data or generic |
| 3887 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3888 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineDataProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3889 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3890 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3891 | CONVERT_ARG_CHECKED(JSObject, js_object, 0); |
| 3892 | CONVERT_ARG_CHECKED(String, name, 1); |
| 3893 | Handle<Object> obj_value = args.at<Object>(2); |
| 3894 | |
| 3895 | CONVERT_CHECKED(Smi, flag, args[3]); |
| 3896 | int unchecked = flag->value(); |
| 3897 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 3898 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3899 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 3900 | |
| 3901 | // Check if this is an element. |
| 3902 | uint32_t index; |
| 3903 | bool is_element = name->AsArrayIndex(&index); |
| 3904 | |
| 3905 | // Special case for elements if any of the flags are true. |
| 3906 | // If elements are in fast case we always implicitly assume that: |
| 3907 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 3908 | if (((unchecked & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) && |
| 3909 | is_element) { |
| 3910 | // Normalize the elements to enable attributes on the property. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3911 | if (js_object->IsJSGlobalProxy()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3912 | // We do not need to do access checks here since these has already |
| 3913 | // been performed by the call to GetOwnProperty. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3914 | Handle<Object> proto(js_object->GetPrototype()); |
| 3915 | // If proxy is detached, ignore the assignment. Alternatively, |
| 3916 | // we could throw an exception. |
| 3917 | if (proto->IsNull()) return *obj_value; |
| 3918 | js_object = Handle<JSObject>::cast(proto); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3919 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3920 | Handle<NumberDictionary> dictionary = NormalizeElements(js_object); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3921 | // Make sure that we never go back to fast case. |
| 3922 | dictionary->set_requires_slow_elements(); |
| 3923 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 3924 | Handle<NumberDictionary> extended_dictionary = |
| 3925 | NumberDictionarySet(dictionary, index, obj_value, details); |
| 3926 | if (*extended_dictionary != *dictionary) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3927 | if (js_object->GetElementsKind() == |
| 3928 | JSObject::NON_STRICT_ARGUMENTS_ELEMENTS) { |
| 3929 | FixedArray::cast(js_object->elements())->set(1, *extended_dictionary); |
| 3930 | } else { |
| 3931 | js_object->set_elements(*extended_dictionary); |
| 3932 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 3933 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3934 | return *obj_value; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3937 | LookupResult result; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 3938 | js_object->LocalLookupRealNamedProperty(*name, &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3939 | |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 3940 | // To be compatible with safari we do not change the value on API objects |
| 3941 | // in defineProperty. Firefox disagrees here, and actually changes the value. |
| 3942 | if (result.IsProperty() && |
| 3943 | (result.type() == CALLBACKS) && |
| 3944 | result.GetCallbackObject()->IsAccessorInfo()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3945 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3948 | // Take special care when attributes are different and there is already |
| 3949 | // a property. For simplicity we normalize the property which enables us |
| 3950 | // to not worry about changing the instance_descriptor and creating a new |
| 3951 | // map. The current version of SetObjectProperty does not handle attributes |
| 3952 | // correctly in the case where a property is a field and is reset with |
| 3953 | // new attributes. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3954 | if (result.IsProperty() && |
| 3955 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3956 | // New attributes - normalize to avoid writing to instance descriptor |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3957 | if (js_object->IsJSGlobalProxy()) { |
| 3958 | // Since the result is a property, the prototype will exist so |
| 3959 | // we don't have to check for null. |
| 3960 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3961 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3962 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3963 | // Use IgnoreAttributes version since a readonly property may be |
| 3964 | // overridden and SetProperty does not allow this. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3965 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 3966 | *obj_value, |
| 3967 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3968 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3969 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3970 | return Runtime::ForceSetObjectProperty(isolate, |
| 3971 | js_object, |
| 3972 | name, |
| 3973 | obj_value, |
| 3974 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3978 | // Special case for elements if any of the flags are true. |
| 3979 | // If elements are in fast case we always implicitly assume that: |
| 3980 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 3981 | static MaybeObject* NormalizeObjectSetElement(Isolate* isolate, |
| 3982 | Handle<JSObject> js_object, |
| 3983 | uint32_t index, |
| 3984 | Handle<Object> value, |
| 3985 | PropertyAttributes attr) { |
| 3986 | // Normalize the elements to enable attributes on the property. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 3987 | Handle<NumberDictionary> dictionary = NormalizeElements(js_object); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 3988 | // Make sure that we never go back to fast case. |
| 3989 | dictionary->set_requires_slow_elements(); |
| 3990 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
| 3991 | Handle<NumberDictionary> extended_dictionary = |
| 3992 | NumberDictionarySet(dictionary, index, value, details); |
| 3993 | if (*extended_dictionary != *dictionary) { |
| 3994 | js_object->set_elements(*extended_dictionary); |
| 3995 | } |
| 3996 | return *value; |
| 3997 | } |
| 3998 | |
| 3999 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4000 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 4001 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4002 | Handle<Object> key, |
| 4003 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4004 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4005 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4006 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4007 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4008 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4009 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4010 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4011 | isolate->factory()->NewTypeError("non_object_property_store", |
| 4012 | HandleVector(args, 2)); |
| 4013 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | // If the object isn't a JavaScript object, we ignore the store. |
| 4017 | if (!object->IsJSObject()) return *value; |
| 4018 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4019 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 4020 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4021 | // Check if the given key is an array index. |
| 4022 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4023 | if (key->ToArrayIndex(&index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4024 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4025 | // of a string using [] notation. We need to support this too in |
| 4026 | // JavaScript. |
| 4027 | // In the case of a String object we just need to redirect the assignment to |
| 4028 | // the underlying string if the index is in range. Since the underlying |
| 4029 | // string does nothing with the assignment then we can ignore such |
| 4030 | // assignments. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4031 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4032 | return *value; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4033 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4034 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4035 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4036 | return NormalizeObjectSetElement(isolate, js_object, index, value, attr); |
| 4037 | } |
| 4038 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4039 | Handle<Object> result = SetElement(js_object, index, value, strict_mode); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4040 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4041 | return *value; |
| 4042 | } |
| 4043 | |
| 4044 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4045 | Handle<Object> result; |
| 4046 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4047 | if (((attr & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0)) { |
| 4048 | return NormalizeObjectSetElement(isolate, |
| 4049 | js_object, |
| 4050 | index, |
| 4051 | value, |
| 4052 | attr); |
| 4053 | } |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4054 | result = SetElement(js_object, index, value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4055 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4056 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4057 | key_string->TryFlatten(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4058 | result = SetProperty(js_object, key_string, value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4059 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4060 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4061 | return *value; |
| 4062 | } |
| 4063 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4064 | // Call-back into JavaScript to convert the key to a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4065 | bool has_pending_exception = false; |
| 4066 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4067 | if (has_pending_exception) return Failure::Exception(); |
| 4068 | Handle<String> name = Handle<String>::cast(converted); |
| 4069 | |
| 4070 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4071 | return js_object->SetElement(index, *value, strict_mode, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4072 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4073 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4078 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4079 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4080 | Handle<Object> key, |
| 4081 | Handle<Object> value, |
| 4082 | PropertyAttributes attr) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4083 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4084 | |
| 4085 | // Check if the given key is an array index. |
| 4086 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4087 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4088 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4089 | // of a string using [] notation. We need to support this too in |
| 4090 | // JavaScript. |
| 4091 | // In the case of a String object we just need to redirect the assignment to |
| 4092 | // the underlying string if the index is in range. Since the underlying |
| 4093 | // string does nothing with the assignment then we can ignore such |
| 4094 | // assignments. |
| 4095 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4096 | return *value; |
| 4097 | } |
| 4098 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4099 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | if (key->IsString()) { |
| 4103 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4104 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4105 | } else { |
| 4106 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4107 | key_string->TryFlatten(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4108 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4109 | *value, |
| 4110 | attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | // Call-back into JavaScript to convert the key to a string. |
| 4115 | bool has_pending_exception = false; |
| 4116 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4117 | if (has_pending_exception) return Failure::Exception(); |
| 4118 | Handle<String> name = Handle<String>::cast(converted); |
| 4119 | |
| 4120 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 4121 | return js_object->SetElement(index, *value, kNonStrictMode, true); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4122 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4123 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4128 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
| 4129 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4130 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4131 | HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4132 | |
| 4133 | // Check if the given key is an array index. |
| 4134 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4135 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4136 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4137 | // characters of a string using [] notation. In the case of a |
| 4138 | // String object we just need to redirect the deletion to the |
| 4139 | // underlying string if the index is in range. Since the |
| 4140 | // underlying string does nothing with the deletion, we can ignore |
| 4141 | // such deletions. |
| 4142 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4143 | return isolate->heap()->true_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | return js_object->DeleteElement(index, JSObject::FORCE_DELETION); |
| 4147 | } |
| 4148 | |
| 4149 | Handle<String> key_string; |
| 4150 | if (key->IsString()) { |
| 4151 | key_string = Handle<String>::cast(key); |
| 4152 | } else { |
| 4153 | // Call-back into JavaScript to convert the key to a string. |
| 4154 | bool has_pending_exception = false; |
| 4155 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4156 | if (has_pending_exception) return Failure::Exception(); |
| 4157 | key_string = Handle<String>::cast(converted); |
| 4158 | } |
| 4159 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4160 | key_string->TryFlatten(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4161 | return js_object->DeleteProperty(*key_string, JSObject::FORCE_DELETION); |
| 4162 | } |
| 4163 | |
| 4164 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4165 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4166 | NoHandleAllocation ha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4167 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4168 | |
| 4169 | Handle<Object> object = args.at<Object>(0); |
| 4170 | Handle<Object> key = args.at<Object>(1); |
| 4171 | Handle<Object> value = args.at<Object>(2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4172 | CONVERT_SMI_ARG_CHECKED(unchecked_attributes, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4173 | RUNTIME_ASSERT( |
| 4174 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4175 | // Compute attributes. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4176 | PropertyAttributes attributes = |
| 4177 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4178 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4179 | StrictModeFlag strict_mode = kNonStrictMode; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4180 | if (args.length() == 5) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4181 | CONVERT_SMI_ARG_CHECKED(strict_unchecked, 4); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4182 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 4183 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4184 | strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4185 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4186 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4187 | return Runtime::SetObjectProperty(isolate, |
| 4188 | object, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4189 | key, |
| 4190 | value, |
| 4191 | attributes, |
| 4192 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4196 | // Set the native flag on the function. |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4197 | // This is used to decide if we should transform null and undefined |
| 4198 | // into the global object when doing call and apply. |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4199 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNativeFlag) { |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4200 | NoHandleAllocation ha; |
| 4201 | RUNTIME_ASSERT(args.length() == 1); |
| 4202 | |
| 4203 | Handle<Object> object = args.at<Object>(0); |
| 4204 | |
| 4205 | if (object->IsJSFunction()) { |
| 4206 | JSFunction* func = JSFunction::cast(*object); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 4207 | func->shared()->set_native(true); |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4208 | } |
| 4209 | return isolate->heap()->undefined_value(); |
| 4210 | } |
| 4211 | |
| 4212 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4213 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4214 | // exist, it will be added with attributes NONE. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4215 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4216 | NoHandleAllocation ha; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4217 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4218 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4219 | CONVERT_CHECKED(String, name, args[1]); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4220 | // Compute attributes. |
| 4221 | PropertyAttributes attributes = NONE; |
| 4222 | if (args.length() == 4) { |
| 4223 | CONVERT_CHECKED(Smi, value_obj, args[3]); |
| 4224 | int unchecked_value = value_obj->value(); |
| 4225 | // Only attribute bits should be set. |
| 4226 | RUNTIME_ASSERT( |
| 4227 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4228 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4229 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4230 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4231 | return object-> |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4232 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4236 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4237 | NoHandleAllocation ha; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4238 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4239 | |
| 4240 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4241 | CONVERT_CHECKED(String, key, args[1]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 4242 | CONVERT_SMI_ARG_CHECKED(strict, 2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4243 | return object->DeleteProperty(key, (strict == kStrictMode) |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4244 | ? JSObject::STRICT_DELETION |
| 4245 | : JSObject::NORMAL_DELETION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4249 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4250 | Handle<JSObject> object, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4251 | Handle<String> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4252 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4253 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4254 | // then we have to check it for properties, because they are supposed to |
| 4255 | // look like they are on this object. |
| 4256 | Handle<Object> proto(object->GetPrototype()); |
| 4257 | if (proto->IsJSObject() && |
| 4258 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4259 | return HasLocalPropertyImplementation(isolate, |
| 4260 | Handle<JSObject>::cast(proto), |
| 4261 | key); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4262 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4263 | return isolate->heap()->false_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4267 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLocalProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4268 | NoHandleAllocation ha; |
| 4269 | ASSERT(args.length() == 2); |
| 4270 | CONVERT_CHECKED(String, key, args[1]); |
| 4271 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4272 | uint32_t index; |
| 4273 | const bool key_is_array_index = key->AsArrayIndex(&index); |
| 4274 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4275 | Object* obj = args[0]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4276 | // Only JS objects can have properties. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4277 | if (obj->IsJSObject()) { |
| 4278 | JSObject* object = JSObject::cast(obj); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4279 | // Fast case: either the key is a real named property or it is not |
| 4280 | // an array index and there are no interceptors or hidden |
| 4281 | // prototypes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4282 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4283 | Map* map = object->map(); |
| 4284 | if (!key_is_array_index && |
| 4285 | !map->has_named_interceptor() && |
| 4286 | !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) { |
| 4287 | return isolate->heap()->false_value(); |
| 4288 | } |
| 4289 | // Slow case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4290 | HandleScope scope(isolate); |
| 4291 | return HasLocalPropertyImplementation(isolate, |
| 4292 | Handle<JSObject>(object), |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4293 | Handle<String>(key)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4294 | } else if (obj->IsString() && key_is_array_index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4295 | // Well, there is one exception: Handle [] on strings. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4296 | String* string = String::cast(obj); |
| 4297 | if (index < static_cast<uint32_t>(string->length())) { |
| 4298 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4301 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4305 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4306 | NoHandleAllocation na; |
| 4307 | ASSERT(args.length() == 2); |
| 4308 | |
| 4309 | // Only JS objects can have properties. |
| 4310 | if (args[0]->IsJSObject()) { |
| 4311 | JSObject* object = JSObject::cast(args[0]); |
| 4312 | CONVERT_CHECKED(String, key, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4313 | if (object->HasProperty(key)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4314 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4315 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4319 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasElement) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4320 | NoHandleAllocation na; |
| 4321 | ASSERT(args.length() == 2); |
| 4322 | |
| 4323 | // Only JS objects can have elements. |
| 4324 | if (args[0]->IsJSObject()) { |
| 4325 | JSObject* object = JSObject::cast(args[0]); |
| 4326 | CONVERT_CHECKED(Smi, index_obj, args[1]); |
| 4327 | uint32_t index = index_obj->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4328 | if (object->HasElement(index)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4329 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4330 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4334 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsPropertyEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4335 | NoHandleAllocation ha; |
| 4336 | ASSERT(args.length() == 2); |
| 4337 | |
| 4338 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4339 | CONVERT_CHECKED(String, key, args[1]); |
| 4340 | |
| 4341 | uint32_t index; |
| 4342 | if (key->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4343 | return isolate->heap()->ToBoolean(object->HasElement(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4346 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4347 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4351 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4352 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4353 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4354 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4355 | return *GetKeysFor(object); |
| 4356 | } |
| 4357 | |
| 4358 | |
| 4359 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4360 | // or, if the given object has an enum cache that contains |
| 4361 | // all enumerable properties of the object and its prototypes |
| 4362 | // have none, the map of the object. This is used to speed up |
| 4363 | // the check for deletions during a for-in. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4364 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNamesFast) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4365 | ASSERT(args.length() == 1); |
| 4366 | |
| 4367 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
| 4368 | |
| 4369 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4370 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4371 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4372 | Handle<JSObject> object(raw_object); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4373 | Handle<FixedArray> content = GetKeysInFixedArrayFor(object, |
| 4374 | INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4375 | |
| 4376 | // Test again, since cache may have been built by preceding call. |
| 4377 | if (object->IsSimpleEnum()) return object->map(); |
| 4378 | |
| 4379 | return *content; |
| 4380 | } |
| 4381 | |
| 4382 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4383 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4384 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4385 | // is prototype for. |
| 4386 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4387 | int count = 1; |
| 4388 | Object* proto = obj->GetPrototype(); |
| 4389 | while (proto->IsJSObject() && |
| 4390 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4391 | count++; |
| 4392 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4393 | } |
| 4394 | return count; |
| 4395 | } |
| 4396 | |
| 4397 | |
| 4398 | // Return the names of the local named properties. |
| 4399 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4400 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4401 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4402 | ASSERT(args.length() == 1); |
| 4403 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4404 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4405 | } |
| 4406 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4407 | |
| 4408 | // Skip the global proxy as it has no properties and always delegates to the |
| 4409 | // real global object. |
| 4410 | if (obj->IsJSGlobalProxy()) { |
| 4411 | // Only collect names if access is permitted. |
| 4412 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4413 | !isolate->MayNamedAccess(*obj, |
| 4414 | isolate->heap()->undefined_value(), |
| 4415 | v8::ACCESS_KEYS)) { |
| 4416 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4417 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4418 | } |
| 4419 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4420 | } |
| 4421 | |
| 4422 | // Find the number of objects making up this. |
| 4423 | int length = LocalPrototypeChainLength(*obj); |
| 4424 | |
| 4425 | // Find the number of local properties for each of the objects. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 4426 | ScopedVector<int> local_property_count(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4427 | int total_property_count = 0; |
| 4428 | Handle<JSObject> jsproto = obj; |
| 4429 | for (int i = 0; i < length; i++) { |
| 4430 | // Only collect names if access is permitted. |
| 4431 | if (jsproto->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4432 | !isolate->MayNamedAccess(*jsproto, |
| 4433 | isolate->heap()->undefined_value(), |
| 4434 | v8::ACCESS_KEYS)) { |
| 4435 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 4436 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4437 | } |
| 4438 | int n; |
| 4439 | n = jsproto->NumberOfLocalProperties(static_cast<PropertyAttributes>(NONE)); |
| 4440 | local_property_count[i] = n; |
| 4441 | total_property_count += n; |
| 4442 | if (i < length - 1) { |
| 4443 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | // Allocate an array with storage for all the property names. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4448 | Handle<FixedArray> names = |
| 4449 | isolate->factory()->NewFixedArray(total_property_count); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4450 | |
| 4451 | // Get the property names. |
| 4452 | jsproto = obj; |
| 4453 | int proto_with_hidden_properties = 0; |
| 4454 | for (int i = 0; i < length; i++) { |
| 4455 | jsproto->GetLocalPropertyNames(*names, |
| 4456 | i == 0 ? 0 : local_property_count[i - 1]); |
| 4457 | if (!GetHiddenProperties(jsproto, false)->IsUndefined()) { |
| 4458 | proto_with_hidden_properties++; |
| 4459 | } |
| 4460 | if (i < length - 1) { |
| 4461 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | // Filter out name of hidden propeties object. |
| 4466 | if (proto_with_hidden_properties > 0) { |
| 4467 | Handle<FixedArray> old_names = names; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4468 | names = isolate->factory()->NewFixedArray( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4469 | names->length() - proto_with_hidden_properties); |
| 4470 | int dest_pos = 0; |
| 4471 | for (int i = 0; i < total_property_count; i++) { |
| 4472 | Object* name = old_names->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4473 | if (name == isolate->heap()->hidden_symbol()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4474 | continue; |
| 4475 | } |
| 4476 | names->set(dest_pos++, name); |
| 4477 | } |
| 4478 | } |
| 4479 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4480 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | |
| 4484 | // Return the names of the local indexed properties. |
| 4485 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4486 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4487 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4488 | ASSERT(args.length() == 1); |
| 4489 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4490 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4491 | } |
| 4492 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4493 | |
| 4494 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4495 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4496 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4497 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | |
| 4501 | // Return information on whether an object has a named or indexed interceptor. |
| 4502 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4503 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetInterceptorInfo) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4504 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4505 | ASSERT(args.length() == 1); |
| 4506 | if (!args[0]->IsJSObject()) { |
| 4507 | return Smi::FromInt(0); |
| 4508 | } |
| 4509 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4510 | |
| 4511 | int result = 0; |
| 4512 | if (obj->HasNamedInterceptor()) result |= 2; |
| 4513 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 4514 | |
| 4515 | return Smi::FromInt(result); |
| 4516 | } |
| 4517 | |
| 4518 | |
| 4519 | // Return property names from named interceptor. |
| 4520 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4521 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetNamedInterceptorPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4522 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4523 | ASSERT(args.length() == 1); |
| 4524 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4525 | |
| 4526 | if (obj->HasNamedInterceptor()) { |
| 4527 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 4528 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4529 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4530 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | |
| 4534 | // Return element names from indexed interceptor. |
| 4535 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4536 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetIndexedInterceptorElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4537 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4538 | ASSERT(args.length() == 1); |
| 4539 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4540 | |
| 4541 | if (obj->HasIndexedInterceptor()) { |
| 4542 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 4543 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4544 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4545 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4549 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LocalKeys) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4550 | ASSERT_EQ(args.length(), 1); |
| 4551 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4552 | HandleScope scope(isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4553 | Handle<JSObject> object(raw_object); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4554 | |
| 4555 | if (object->IsJSGlobalProxy()) { |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4556 | // Do access checks before going to the global object. |
| 4557 | if (object->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4558 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4559 | v8::ACCESS_KEYS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4560 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 4561 | return *isolate->factory()->NewJSArray(0); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4564 | Handle<Object> proto(object->GetPrototype()); |
| 4565 | // If proxy is detached we simply return an empty array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4566 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4567 | object = Handle<JSObject>::cast(proto); |
| 4568 | } |
| 4569 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4570 | Handle<FixedArray> contents = GetKeysInFixedArrayFor(object, |
| 4571 | LOCAL_ONLY); |
| 4572 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 4573 | // property array and since the result is mutable we have to create |
| 4574 | // a fresh clone on each invocation. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4575 | int length = contents->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4576 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4577 | for (int i = 0; i < length; i++) { |
| 4578 | Object* entry = contents->get(i); |
| 4579 | if (entry->IsString()) { |
| 4580 | copy->set(i, entry); |
| 4581 | } else { |
| 4582 | ASSERT(entry->IsNumber()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4583 | HandleScope scope(isolate); |
| 4584 | Handle<Object> entry_handle(entry, isolate); |
| 4585 | Handle<Object> entry_str = |
| 4586 | isolate->factory()->NumberToString(entry_handle); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4587 | copy->set(i, *entry_str); |
| 4588 | } |
| 4589 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4590 | return *isolate->factory()->NewJSArrayWithElements(copy); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4594 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArgumentsProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4595 | NoHandleAllocation ha; |
| 4596 | ASSERT(args.length() == 1); |
| 4597 | |
| 4598 | // Compute the frame holding the arguments. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4599 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4600 | it.AdvanceToArgumentsFrame(); |
| 4601 | JavaScriptFrame* frame = it.frame(); |
| 4602 | |
| 4603 | // Get the actual number of provided arguments. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4604 | const uint32_t n = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4605 | |
| 4606 | // Try to convert the key to an index. If successful and within |
| 4607 | // index return the the argument from the frame. |
| 4608 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4609 | if (args[0]->ToArrayIndex(&index) && index < n) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4610 | return frame->GetParameter(index); |
| 4611 | } |
| 4612 | |
| 4613 | // Convert the key to a string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4614 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4615 | bool exception = false; |
| 4616 | Handle<Object> converted = |
| 4617 | Execution::ToString(args.at<Object>(0), &exception); |
| 4618 | if (exception) return Failure::Exception(); |
| 4619 | Handle<String> key = Handle<String>::cast(converted); |
| 4620 | |
| 4621 | // Try to convert the string key into an array index. |
| 4622 | if (key->AsArrayIndex(&index)) { |
| 4623 | if (index < n) { |
| 4624 | return frame->GetParameter(index); |
| 4625 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4626 | return isolate->initial_object_prototype()->GetElement(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
| 4629 | |
| 4630 | // Handle special arguments properties. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4631 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 4632 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 4633 | Object* function = frame->function(); |
| 4634 | if (function->IsJSFunction() && |
| 4635 | JSFunction::cast(function)->shared()->strict_mode()) { |
| 4636 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 4637 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 4638 | } |
| 4639 | return function; |
| 4640 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4641 | |
| 4642 | // Lookup in the initial Object.prototype object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4643 | return isolate->initial_object_prototype()->GetProperty(*key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4647 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToFastProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4648 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4649 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4650 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4651 | Handle<Object> object = args.at<Object>(0); |
| 4652 | if (object->IsJSObject()) { |
| 4653 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4654 | if (!js_object->HasFastProperties() && !js_object->IsGlobalObject()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4655 | MaybeObject* ok = js_object->TransformToFastProperties(0); |
| 4656 | if (ok->IsRetryAfterGC()) return ok; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4657 | } |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4658 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4659 | return *object; |
| 4660 | } |
| 4661 | |
| 4662 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4663 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToSlowProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4664 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4665 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4666 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4667 | Handle<Object> object = args.at<Object>(0); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4668 | if (object->IsJSObject() && !object->IsJSGlobalProxy()) { |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4669 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4670 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4671 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4672 | return *object; |
| 4673 | } |
| 4674 | |
| 4675 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4676 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToBool) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4677 | NoHandleAllocation ha; |
| 4678 | ASSERT(args.length() == 1); |
| 4679 | |
| 4680 | return args[0]->ToBoolean(); |
| 4681 | } |
| 4682 | |
| 4683 | |
| 4684 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 4685 | // Possible optimizations: put the type string into the oddballs. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4686 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Typeof) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4687 | NoHandleAllocation ha; |
| 4688 | |
| 4689 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4690 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4691 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 4692 | |
| 4693 | // typeof an undetectable object is 'undefined' |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4694 | if (heap_obj->map()->is_undetectable()) { |
| 4695 | return isolate->heap()->undefined_symbol(); |
| 4696 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4697 | |
| 4698 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 4699 | if (instance_type < FIRST_NONSTRING_TYPE) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4700 | return isolate->heap()->string_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | switch (instance_type) { |
| 4704 | case ODDBALL_TYPE: |
| 4705 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4706 | return isolate->heap()->boolean_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4707 | } |
| 4708 | if (heap_obj->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4709 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4710 | } |
| 4711 | ASSERT(heap_obj->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4712 | return isolate->heap()->undefined_symbol(); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 4713 | case JS_FUNCTION_TYPE: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4714 | return isolate->heap()->function_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4715 | default: |
| 4716 | // For any kind of object not handled above, the spec rule for |
| 4717 | // host objects gives that it is okay to return "object" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4718 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4719 | } |
| 4720 | } |
| 4721 | |
| 4722 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4723 | static bool AreDigits(const char*s, int from, int to) { |
| 4724 | for (int i = from; i < to; i++) { |
| 4725 | if (s[i] < '0' || s[i] > '9') return false; |
| 4726 | } |
| 4727 | |
| 4728 | return true; |
| 4729 | } |
| 4730 | |
| 4731 | |
| 4732 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 4733 | ASSERT(to - from < 10); // Overflow is not possible. |
| 4734 | ASSERT(from < to); |
| 4735 | int d = s[from] - '0'; |
| 4736 | |
| 4737 | for (int i = from + 1; i < to; i++) { |
| 4738 | d = 10 * d + (s[i] - '0'); |
| 4739 | } |
| 4740 | |
| 4741 | return d; |
| 4742 | } |
| 4743 | |
| 4744 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4745 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4746 | NoHandleAllocation ha; |
| 4747 | ASSERT(args.length() == 1); |
| 4748 | CONVERT_CHECKED(String, subject, args[0]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4749 | subject->TryFlatten(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4750 | |
| 4751 | // Fast case: short integer or some sorts of junk values. |
| 4752 | int len = subject->length(); |
| 4753 | if (subject->IsSeqAsciiString()) { |
| 4754 | if (len == 0) return Smi::FromInt(0); |
| 4755 | |
| 4756 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 4757 | bool minus = (data[0] == '-'); |
| 4758 | int start_pos = (minus ? 1 : 0); |
| 4759 | |
| 4760 | if (start_pos == len) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4761 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4762 | } else if (data[start_pos] > '9') { |
| 4763 | // Fast check for a junk value. A valid string may start from a |
| 4764 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 4765 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 4766 | // '9' except 'I'. |
| 4767 | if (data[start_pos] != 'I') { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4768 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4769 | } |
| 4770 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 4771 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 4772 | // know it will fit into the smi-data type. |
| 4773 | int d = ParseDecimalInteger(data, start_pos, len); |
| 4774 | if (minus) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4775 | if (d == 0) return isolate->heap()->minus_zero_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4776 | d = -d; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4777 | } else if (!subject->HasHashCode() && |
| 4778 | len <= String::kMaxArrayIndexSize && |
| 4779 | (len == 1 || data[0] != '0')) { |
| 4780 | // String hash is not calculated yet but all the data are present. |
| 4781 | // Update the hash field to speed up sequential convertions. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4782 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4783 | #ifdef DEBUG |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4784 | subject->Hash(); // Force hash calculation. |
| 4785 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 4786 | static_cast<int>(hash)); |
| 4787 | #endif |
| 4788 | subject->set_hash_field(hash); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4789 | } |
| 4790 | return Smi::FromInt(d); |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | // Slower case. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 4795 | return isolate->heap()->NumberFromDouble( |
| 4796 | StringToDouble(isolate->unicode_cache(), subject, ALLOW_HEX)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4800 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringFromCharCodeArray) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4801 | NoHandleAllocation ha; |
| 4802 | ASSERT(args.length() == 1); |
| 4803 | |
| 4804 | CONVERT_CHECKED(JSArray, codes, args[0]); |
| 4805 | int length = Smi::cast(codes->length())->value(); |
| 4806 | |
| 4807 | // Check if the string can be ASCII. |
| 4808 | int i; |
| 4809 | for (i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4810 | Object* element; |
| 4811 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 4812 | // We probably can't get an exception here, but just in order to enforce |
| 4813 | // the checking of inputs in the runtime calls we check here. |
| 4814 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 4815 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4816 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 4817 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 4818 | break; |
| 4819 | } |
| 4820 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4821 | MaybeObject* maybe_object = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4822 | if (i == length) { // The string is ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4823 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4824 | } else { // The string is not ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4825 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4828 | Object* object = NULL; |
| 4829 | if (!maybe_object->ToObject(&object)) return maybe_object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4830 | String* result = String::cast(object); |
| 4831 | for (int i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4832 | Object* element; |
| 4833 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 4834 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 4835 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4836 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4837 | result->Set(i, chr & 0xffff); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4838 | } |
| 4839 | return result; |
| 4840 | } |
| 4841 | |
| 4842 | |
| 4843 | // kNotEscaped is generated by the following: |
| 4844 | // |
| 4845 | // #!/bin/perl |
| 4846 | // for (my $i = 0; $i < 256; $i++) { |
| 4847 | // print "\n" if $i % 16 == 0; |
| 4848 | // my $c = chr($i); |
| 4849 | // my $escaped = 1; |
| 4850 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 4851 | // print $escaped ? "0, " : "1, "; |
| 4852 | // } |
| 4853 | |
| 4854 | |
| 4855 | static bool IsNotEscaped(uint16_t character) { |
| 4856 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 4857 | ASSERT(character < 256); |
| 4858 | static const char kNotEscaped[256] = { |
| 4859 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4860 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4861 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 4862 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 4863 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4864 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 4865 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4866 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 4867 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4868 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4869 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4870 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4871 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4872 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4873 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4874 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4875 | }; |
| 4876 | return kNotEscaped[character] != 0; |
| 4877 | } |
| 4878 | |
| 4879 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4880 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4881 | const char hex_chars[] = "0123456789ABCDEF"; |
| 4882 | NoHandleAllocation ha; |
| 4883 | ASSERT(args.length() == 1); |
| 4884 | CONVERT_CHECKED(String, source, args[0]); |
| 4885 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4886 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4887 | |
| 4888 | int escaped_length = 0; |
| 4889 | int length = source->length(); |
| 4890 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4891 | Access<StringInputBuffer> buffer( |
| 4892 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4893 | buffer->Reset(source); |
| 4894 | while (buffer->has_more()) { |
| 4895 | uint16_t character = buffer->GetNext(); |
| 4896 | if (character >= 256) { |
| 4897 | escaped_length += 6; |
| 4898 | } else if (IsNotEscaped(character)) { |
| 4899 | escaped_length++; |
| 4900 | } else { |
| 4901 | escaped_length += 3; |
| 4902 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4903 | // 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] | 4904 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4905 | if (escaped_length > String::kMaxLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4906 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4907 | return Failure::OutOfMemoryException(); |
| 4908 | } |
| 4909 | } |
| 4910 | } |
| 4911 | // No length change implies no change. Return original string if no change. |
| 4912 | if (escaped_length == length) { |
| 4913 | return source; |
| 4914 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4915 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4916 | { MaybeObject* maybe_o = |
| 4917 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4918 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 4919 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4920 | String* destination = String::cast(o); |
| 4921 | int dest_position = 0; |
| 4922 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4923 | Access<StringInputBuffer> buffer( |
| 4924 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4925 | buffer->Rewind(); |
| 4926 | while (buffer->has_more()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4927 | uint16_t chr = buffer->GetNext(); |
| 4928 | if (chr >= 256) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4929 | destination->Set(dest_position, '%'); |
| 4930 | destination->Set(dest_position+1, 'u'); |
| 4931 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 4932 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 4933 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 4934 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4935 | dest_position += 6; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4936 | } else if (IsNotEscaped(chr)) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4937 | destination->Set(dest_position, chr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4938 | dest_position++; |
| 4939 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4940 | destination->Set(dest_position, '%'); |
| 4941 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 4942 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4943 | dest_position += 3; |
| 4944 | } |
| 4945 | } |
| 4946 | return destination; |
| 4947 | } |
| 4948 | |
| 4949 | |
| 4950 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 4951 | static const signed char kHexValue['g'] = { |
| 4952 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4953 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4954 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4955 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 4956 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4957 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4958 | -1, 10, 11, 12, 13, 14, 15 }; |
| 4959 | |
| 4960 | if (character1 > 'f') return -1; |
| 4961 | int hi = kHexValue[character1]; |
| 4962 | if (hi == -1) return -1; |
| 4963 | if (character2 > 'f') return -1; |
| 4964 | int lo = kHexValue[character2]; |
| 4965 | if (lo == -1) return -1; |
| 4966 | return (hi << 4) + lo; |
| 4967 | } |
| 4968 | |
| 4969 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4970 | static inline int Unescape(String* source, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4971 | int i, |
| 4972 | int length, |
| 4973 | int* step) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4974 | uint16_t character = source->Get(i); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4975 | int32_t hi = 0; |
| 4976 | int32_t lo = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4977 | if (character == '%' && |
| 4978 | i <= length - 6 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4979 | source->Get(i + 1) == 'u' && |
| 4980 | (hi = TwoDigitHex(source->Get(i + 2), |
| 4981 | source->Get(i + 3))) != -1 && |
| 4982 | (lo = TwoDigitHex(source->Get(i + 4), |
| 4983 | source->Get(i + 5))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4984 | *step = 6; |
| 4985 | return (hi << 8) + lo; |
| 4986 | } else if (character == '%' && |
| 4987 | i <= length - 3 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4988 | (lo = TwoDigitHex(source->Get(i + 1), |
| 4989 | source->Get(i + 2))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4990 | *step = 3; |
| 4991 | return lo; |
| 4992 | } else { |
| 4993 | *step = 1; |
| 4994 | return character; |
| 4995 | } |
| 4996 | } |
| 4997 | |
| 4998 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4999 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5000 | NoHandleAllocation ha; |
| 5001 | ASSERT(args.length() == 1); |
| 5002 | CONVERT_CHECKED(String, source, args[0]); |
| 5003 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5004 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5005 | |
| 5006 | bool ascii = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5007 | int length = source->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5008 | |
| 5009 | int unescaped_length = 0; |
| 5010 | for (int i = 0; i < length; unescaped_length++) { |
| 5011 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5012 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5013 | ascii = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5014 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5015 | i += step; |
| 5016 | } |
| 5017 | |
| 5018 | // No length change implies no change. Return original string if no change. |
| 5019 | if (unescaped_length == length) |
| 5020 | return source; |
| 5021 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5022 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5023 | { MaybeObject* maybe_o = |
| 5024 | ascii ? |
| 5025 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 5026 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5027 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5028 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5029 | String* destination = String::cast(o); |
| 5030 | |
| 5031 | int dest_position = 0; |
| 5032 | for (int i = 0; i < length; dest_position++) { |
| 5033 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5034 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5035 | i += step; |
| 5036 | } |
| 5037 | return destination; |
| 5038 | } |
| 5039 | |
| 5040 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5041 | static const unsigned int kQuoteTableLength = 128u; |
| 5042 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5043 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 5044 | static const char* const JsonQuotes = |
| 5045 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 5046 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 5047 | "\\b \\t \\n \\u000b " |
| 5048 | "\\f \\r \\u000e \\u000f " |
| 5049 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 5050 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 5051 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 5052 | "\\u001c \\u001d \\u001e \\u001f " |
| 5053 | " ! \\\" # " |
| 5054 | "$ % & ' " |
| 5055 | "( ) * + " |
| 5056 | ", - . / " |
| 5057 | "0 1 2 3 " |
| 5058 | "4 5 6 7 " |
| 5059 | "8 9 : ; " |
| 5060 | "< = > ? " |
| 5061 | "@ A B C " |
| 5062 | "D E F G " |
| 5063 | "H I J K " |
| 5064 | "L M N O " |
| 5065 | "P Q R S " |
| 5066 | "T U V W " |
| 5067 | "X Y Z [ " |
| 5068 | "\\\\ ] ^ _ " |
| 5069 | "` a b c " |
| 5070 | "d e f g " |
| 5071 | "h i j k " |
| 5072 | "l m n o " |
| 5073 | "p q r s " |
| 5074 | "t u v w " |
| 5075 | "x y z { " |
| 5076 | "| } ~ \177 "; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5077 | |
| 5078 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5079 | // For a string that is less than 32k characters it should always be |
| 5080 | // possible to allocate it in new space. |
| 5081 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5082 | |
| 5083 | |
| 5084 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5085 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5086 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5087 | static const int kSpaceForQuotesAndComma = 3; |
| 5088 | static const int kSpaceForBrackets = 2; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5089 | |
| 5090 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5091 | // quoting). |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5092 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5093 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5094 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5095 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5096 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5097 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5098 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5099 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5100 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5101 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5102 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5103 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5104 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5105 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5106 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5107 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5108 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5109 | }; |
| 5110 | |
| 5111 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5112 | template <typename StringType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5113 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5114 | |
| 5115 | |
| 5116 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5117 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5118 | return isolate->heap()->AllocateRawTwoByteString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
| 5121 | |
| 5122 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5123 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5124 | return isolate->heap()->AllocateRawAsciiString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5128 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5129 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5130 | Vector<const Char> characters) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5131 | int length = characters.length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5132 | const Char* read_cursor = characters.start(); |
| 5133 | const Char* end = read_cursor + length; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5134 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5135 | int quoted_length = kSpaceForQuotes; |
| 5136 | while (read_cursor < end) { |
| 5137 | Char c = *(read_cursor++); |
| 5138 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5139 | quoted_length++; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5140 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5141 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5142 | } |
| 5143 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5144 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5145 | quoted_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5146 | Object* new_object; |
| 5147 | if (!new_alloc->ToObject(&new_object)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5148 | return new_alloc; |
| 5149 | } |
| 5150 | StringType* new_string = StringType::cast(new_object); |
| 5151 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5152 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5153 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5154 | if (comma) *(write_cursor++) = ','; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5155 | *(write_cursor++) = '"'; |
| 5156 | |
| 5157 | read_cursor = characters.start(); |
| 5158 | while (read_cursor < end) { |
| 5159 | Char c = *(read_cursor++); |
| 5160 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5161 | *(write_cursor++) = c; |
| 5162 | } else { |
| 5163 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5164 | const char* replacement = JsonQuotes + |
| 5165 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5166 | for (int i = 0; i < len; i++) { |
| 5167 | *write_cursor++ = *replacement++; |
| 5168 | } |
| 5169 | } |
| 5170 | } |
| 5171 | *(write_cursor++) = '"'; |
| 5172 | return new_string; |
| 5173 | } |
| 5174 | |
| 5175 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5176 | template <typename SinkChar, typename SourceChar> |
| 5177 | static inline SinkChar* WriteQuoteJsonString( |
| 5178 | Isolate* isolate, |
| 5179 | SinkChar* write_cursor, |
| 5180 | Vector<const SourceChar> characters) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 5181 | // SinkChar is only char if SourceChar is guaranteed to be char. |
| 5182 | ASSERT(sizeof(SinkChar) >= sizeof(SourceChar)); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5183 | const SourceChar* read_cursor = characters.start(); |
| 5184 | const SourceChar* end = read_cursor + characters.length(); |
| 5185 | *(write_cursor++) = '"'; |
| 5186 | while (read_cursor < end) { |
| 5187 | SourceChar c = *(read_cursor++); |
| 5188 | if (sizeof(SourceChar) > 1u && |
| 5189 | static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5190 | *(write_cursor++) = static_cast<SinkChar>(c); |
| 5191 | } else { |
| 5192 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5193 | const char* replacement = JsonQuotes + |
| 5194 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5195 | write_cursor[0] = replacement[0]; |
| 5196 | if (len > 1) { |
| 5197 | write_cursor[1] = replacement[1]; |
| 5198 | if (len > 2) { |
| 5199 | ASSERT(len == 6); |
| 5200 | write_cursor[2] = replacement[2]; |
| 5201 | write_cursor[3] = replacement[3]; |
| 5202 | write_cursor[4] = replacement[4]; |
| 5203 | write_cursor[5] = replacement[5]; |
| 5204 | } |
| 5205 | } |
| 5206 | write_cursor += len; |
| 5207 | } |
| 5208 | } |
| 5209 | *(write_cursor++) = '"'; |
| 5210 | return write_cursor; |
| 5211 | } |
| 5212 | |
| 5213 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5214 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5215 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5216 | Vector<const Char> characters) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5217 | int length = characters.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5218 | isolate->counters()->quote_json_char_count()->Increment(length); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5219 | int worst_case_length = |
| 5220 | length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotesAndComma; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5221 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5222 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5225 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5226 | worst_case_length); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5227 | Object* new_object; |
| 5228 | if (!new_alloc->ToObject(&new_object)) { |
| 5229 | return new_alloc; |
| 5230 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5231 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5232 | // Even if our string is small enough to fit in new space we still have to |
| 5233 | // handle it being allocated in old space as may happen in the third |
| 5234 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5235 | // CEntryStub::GenerateCore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5236 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5237 | } |
| 5238 | StringType* new_string = StringType::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5239 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5240 | |
| 5241 | STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
| 5242 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5243 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5244 | if (comma) *(write_cursor++) = ','; |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5245 | write_cursor = WriteQuoteJsonString<Char, Char>(isolate, |
| 5246 | write_cursor, |
| 5247 | characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5248 | int final_length = static_cast<int>( |
| 5249 | write_cursor - reinterpret_cast<Char*>( |
| 5250 | new_string->address() + SeqAsciiString::kHeaderSize)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5251 | isolate->heap()->new_space()-> |
| 5252 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5253 | new_string, final_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5254 | return new_string; |
| 5255 | } |
| 5256 | |
| 5257 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5258 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5259 | NoHandleAllocation ha; |
| 5260 | CONVERT_CHECKED(String, str, args[0]); |
| 5261 | if (!str->IsFlat()) { |
| 5262 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5263 | Object* flat; |
| 5264 | if (!try_flatten->ToObject(&flat)) { |
| 5265 | return try_flatten; |
| 5266 | } |
| 5267 | str = String::cast(flat); |
| 5268 | ASSERT(str->IsFlat()); |
| 5269 | } |
| 5270 | if (str->IsTwoByteRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5271 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
| 5272 | str->ToUC16Vector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5273 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5274 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
| 5275 | str->ToAsciiVector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | |
| 5279 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5280 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringComma) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5281 | NoHandleAllocation ha; |
| 5282 | CONVERT_CHECKED(String, str, args[0]); |
| 5283 | if (!str->IsFlat()) { |
| 5284 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5285 | Object* flat; |
| 5286 | if (!try_flatten->ToObject(&flat)) { |
| 5287 | return try_flatten; |
| 5288 | } |
| 5289 | str = String::cast(flat); |
| 5290 | ASSERT(str->IsFlat()); |
| 5291 | } |
| 5292 | if (str->IsTwoByteRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5293 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
| 5294 | str->ToUC16Vector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5295 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5296 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
| 5297 | str->ToAsciiVector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 5301 | |
| 5302 | template <typename Char, typename StringType> |
| 5303 | static MaybeObject* QuoteJsonStringArray(Isolate* isolate, |
| 5304 | FixedArray* array, |
| 5305 | int worst_case_length) { |
| 5306 | int length = array->length(); |
| 5307 | |
| 5308 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5309 | worst_case_length); |
| 5310 | Object* new_object; |
| 5311 | if (!new_alloc->ToObject(&new_object)) { |
| 5312 | return new_alloc; |
| 5313 | } |
| 5314 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
| 5315 | // Even if our string is small enough to fit in new space we still have to |
| 5316 | // handle it being allocated in old space as may happen in the third |
| 5317 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5318 | // CEntryStub::GenerateCore. |
| 5319 | return isolate->heap()->undefined_value(); |
| 5320 | } |
| 5321 | AssertNoAllocation no_gc; |
| 5322 | StringType* new_string = StringType::cast(new_object); |
| 5323 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
| 5324 | |
| 5325 | STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
| 5326 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5327 | new_string->address() + SeqAsciiString::kHeaderSize); |
| 5328 | *(write_cursor++) = '['; |
| 5329 | for (int i = 0; i < length; i++) { |
| 5330 | if (i != 0) *(write_cursor++) = ','; |
| 5331 | String* str = String::cast(array->get(i)); |
| 5332 | if (str->IsTwoByteRepresentation()) { |
| 5333 | write_cursor = WriteQuoteJsonString<Char, uc16>(isolate, |
| 5334 | write_cursor, |
| 5335 | str->ToUC16Vector()); |
| 5336 | } else { |
| 5337 | write_cursor = WriteQuoteJsonString<Char, char>(isolate, |
| 5338 | write_cursor, |
| 5339 | str->ToAsciiVector()); |
| 5340 | } |
| 5341 | } |
| 5342 | *(write_cursor++) = ']'; |
| 5343 | |
| 5344 | int final_length = static_cast<int>( |
| 5345 | write_cursor - reinterpret_cast<Char*>( |
| 5346 | new_string->address() + SeqAsciiString::kHeaderSize)); |
| 5347 | isolate->heap()->new_space()-> |
| 5348 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5349 | new_string, final_length); |
| 5350 | return new_string; |
| 5351 | } |
| 5352 | |
| 5353 | |
| 5354 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringArray) { |
| 5355 | NoHandleAllocation ha; |
| 5356 | ASSERT(args.length() == 1); |
| 5357 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 5358 | |
| 5359 | if (!array->HasFastElements()) return isolate->heap()->undefined_value(); |
| 5360 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 5361 | int n = elements->length(); |
| 5362 | bool ascii = true; |
| 5363 | int total_length = 0; |
| 5364 | |
| 5365 | for (int i = 0; i < n; i++) { |
| 5366 | Object* elt = elements->get(i); |
| 5367 | if (!elt->IsString()) return isolate->heap()->undefined_value(); |
| 5368 | String* element = String::cast(elt); |
| 5369 | if (!element->IsFlat()) return isolate->heap()->undefined_value(); |
| 5370 | total_length += element->length(); |
| 5371 | if (ascii && element->IsTwoByteRepresentation()) { |
| 5372 | ascii = false; |
| 5373 | } |
| 5374 | } |
| 5375 | |
| 5376 | int worst_case_length = |
| 5377 | kSpaceForBrackets + n * kSpaceForQuotesAndComma |
| 5378 | + total_length * kJsonQuoteWorstCaseBlowup; |
| 5379 | |
| 5380 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
| 5381 | return isolate->heap()->undefined_value(); |
| 5382 | } |
| 5383 | |
| 5384 | if (ascii) { |
| 5385 | return QuoteJsonStringArray<char, SeqAsciiString>(isolate, |
| 5386 | elements, |
| 5387 | worst_case_length); |
| 5388 | } else { |
| 5389 | return QuoteJsonStringArray<uc16, SeqTwoByteString>(isolate, |
| 5390 | elements, |
| 5391 | worst_case_length); |
| 5392 | } |
| 5393 | } |
| 5394 | |
| 5395 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5396 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseInt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5397 | NoHandleAllocation ha; |
| 5398 | |
| 5399 | CONVERT_CHECKED(String, s, args[0]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5400 | CONVERT_SMI_ARG_CHECKED(radix, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5401 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5402 | s->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5403 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5404 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5405 | double value = StringToInt(isolate->unicode_cache(), s, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5406 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5410 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseFloat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5411 | NoHandleAllocation ha; |
| 5412 | CONVERT_CHECKED(String, str, args[0]); |
| 5413 | |
| 5414 | // ECMA-262 section 15.1.2.3, empty string is NaN |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5415 | double value = StringToDouble(isolate->unicode_cache(), |
| 5416 | str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5417 | |
| 5418 | // Create a number object from the value. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5419 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5423 | template <class Converter> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5424 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5425 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5426 | String* s, |
| 5427 | int length, |
| 5428 | int input_string_length, |
| 5429 | unibrow::Mapping<Converter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5430 | // We try this twice, once with the assumption that the result is no longer |
| 5431 | // than the input and, if that assumption breaks, again with the exact |
| 5432 | // length. This may not be pretty, but it is nicer than what was here before |
| 5433 | // and I hereby claim my vaffel-is. |
| 5434 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5435 | // Allocate the resulting string. |
| 5436 | // |
| 5437 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5438 | // character is also ascii. This is currently the case, but it |
| 5439 | // might break in the future if we implement more context and locale |
| 5440 | // dependent upper/lower conversions. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5441 | Object* o; |
| 5442 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5443 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 5444 | : isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5445 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5446 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5447 | String* result = String::cast(o); |
| 5448 | bool has_changed_character = false; |
| 5449 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5450 | // Convert all characters to upper case, assuming that they will fit |
| 5451 | // in the buffer |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5452 | Access<StringInputBuffer> buffer( |
| 5453 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5454 | buffer->Reset(s); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5455 | unibrow::uchar chars[Converter::kMaxWidth]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5456 | // We can assume that the string is not empty |
| 5457 | uc32 current = buffer->GetNext(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5458 | for (int i = 0; i < length;) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5459 | bool has_next = buffer->has_more(); |
| 5460 | uc32 next = has_next ? buffer->GetNext() : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5461 | int char_length = mapping->get(current, next, chars); |
| 5462 | if (char_length == 0) { |
| 5463 | // The case conversion of this character is the character itself. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5464 | result->Set(i, current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5465 | i++; |
| 5466 | } else if (char_length == 1) { |
| 5467 | // Common case: converting the letter resulted in one character. |
| 5468 | ASSERT(static_cast<uc32>(chars[0]) != current); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5469 | result->Set(i, chars[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5470 | has_changed_character = true; |
| 5471 | i++; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5472 | } else if (length == input_string_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5473 | // We've assumed that the result would be as long as the |
| 5474 | // input but here is a character that converts to several |
| 5475 | // characters. No matter, we calculate the exact length |
| 5476 | // of the result and try the whole thing again. |
| 5477 | // |
| 5478 | // Note that this leaves room for optimization. We could just |
| 5479 | // memcpy what we already have to the result string. Also, |
| 5480 | // the result string is the last object allocated we could |
| 5481 | // "realloc" it and probably, in the vast majority of cases, |
| 5482 | // extend the existing string to be able to hold the full |
| 5483 | // result. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5484 | int next_length = 0; |
| 5485 | if (has_next) { |
| 5486 | next_length = mapping->get(next, 0, chars); |
| 5487 | if (next_length == 0) next_length = 1; |
| 5488 | } |
| 5489 | int current_length = i + char_length + next_length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5490 | while (buffer->has_more()) { |
| 5491 | current = buffer->GetNext(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5492 | // NOTE: we use 0 as the next character here because, while |
| 5493 | // the next character may affect what a character converts to, |
| 5494 | // it does not in any case affect the length of what it convert |
| 5495 | // to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5496 | int char_length = mapping->get(current, 0, chars); |
| 5497 | if (char_length == 0) char_length = 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5498 | current_length += char_length; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5499 | if (current_length > Smi::kMaxValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5500 | isolate->context()->mark_out_of_memory(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5501 | return Failure::OutOfMemoryException(); |
| 5502 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5503 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5504 | // Try again with the real length. |
| 5505 | return Smi::FromInt(current_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5506 | } else { |
| 5507 | for (int j = 0; j < char_length; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5508 | result->Set(i, chars[j]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5509 | i++; |
| 5510 | } |
| 5511 | has_changed_character = true; |
| 5512 | } |
| 5513 | current = next; |
| 5514 | } |
| 5515 | if (has_changed_character) { |
| 5516 | return result; |
| 5517 | } else { |
| 5518 | // If we didn't actually change anything in doing the conversion |
| 5519 | // we simple return the result and let the converted string |
| 5520 | // become garbage; there is no reason to keep two identical strings |
| 5521 | // alive. |
| 5522 | return s; |
| 5523 | } |
| 5524 | } |
| 5525 | |
| 5526 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5527 | namespace { |
| 5528 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5529 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 5530 | |
| 5531 | |
| 5532 | // Given a word and two range boundaries returns a word with high bit |
| 5533 | // set in every byte iff the corresponding input byte was strictly in |
| 5534 | // the range (m, n). All the other bits in the result are cleared. |
| 5535 | // This function is only useful when it can be inlined and the |
| 5536 | // boundaries are statically known. |
| 5537 | // Requires: all bytes in the input word and the boundaries must be |
| 5538 | // ascii (less than 0x7F). |
| 5539 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
| 5540 | // Every byte in an ascii string is less than or equal to 0x7F. |
| 5541 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 5542 | // Use strict inequalities since in edge cases the function could be |
| 5543 | // further simplified. |
| 5544 | ASSERT(0 < m && m < n && n < 0x7F); |
| 5545 | // Has high bit set in every w byte less than n. |
| 5546 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 5547 | // Has high bit set in every w byte greater than m. |
| 5548 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 5549 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 5550 | } |
| 5551 | |
| 5552 | |
| 5553 | enum AsciiCaseConversion { |
| 5554 | ASCII_TO_LOWER, |
| 5555 | ASCII_TO_UPPER |
| 5556 | }; |
| 5557 | |
| 5558 | |
| 5559 | template <AsciiCaseConversion dir> |
| 5560 | struct FastAsciiConverter { |
| 5561 | static bool Convert(char* dst, char* src, int length) { |
| 5562 | #ifdef DEBUG |
| 5563 | char* saved_dst = dst; |
| 5564 | char* saved_src = src; |
| 5565 | #endif |
| 5566 | // We rely on the distance between upper and lower case letters |
| 5567 | // being a known power of 2. |
| 5568 | ASSERT('a' - 'A' == (1 << 5)); |
| 5569 | // Boundaries for the range of input characters than require conversion. |
| 5570 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 5571 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 5572 | bool changed = false; |
| 5573 | char* const limit = src + length; |
| 5574 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 5575 | // Process the prefix of the input that requires no conversion one |
| 5576 | // (machine) word at a time. |
| 5577 | while (src <= limit - sizeof(uintptr_t)) { |
| 5578 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 5579 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 5580 | changed = true; |
| 5581 | break; |
| 5582 | } |
| 5583 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 5584 | src += sizeof(uintptr_t); |
| 5585 | dst += sizeof(uintptr_t); |
| 5586 | } |
| 5587 | // Process the remainder of the input performing conversion when |
| 5588 | // required one word at a time. |
| 5589 | while (src <= limit - sizeof(uintptr_t)) { |
| 5590 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 5591 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 5592 | // The mask has high (7th) bit set in every byte that needs |
| 5593 | // conversion and we know that the distance between cases is |
| 5594 | // 1 << 5. |
| 5595 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 5596 | src += sizeof(uintptr_t); |
| 5597 | dst += sizeof(uintptr_t); |
| 5598 | } |
| 5599 | #endif |
| 5600 | // Process the last few bytes of the input (or the whole input if |
| 5601 | // unaligned access is not supported). |
| 5602 | while (src < limit) { |
| 5603 | char c = *src; |
| 5604 | if (lo < c && c < hi) { |
| 5605 | c ^= (1 << 5); |
| 5606 | changed = true; |
| 5607 | } |
| 5608 | *dst = c; |
| 5609 | ++src; |
| 5610 | ++dst; |
| 5611 | } |
| 5612 | #ifdef DEBUG |
| 5613 | CheckConvert(saved_dst, saved_src, length, changed); |
| 5614 | #endif |
| 5615 | return changed; |
| 5616 | } |
| 5617 | |
| 5618 | #ifdef DEBUG |
| 5619 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 5620 | bool expected_changed = false; |
| 5621 | for (int i = 0; i < length; i++) { |
| 5622 | if (dst[i] == src[i]) continue; |
| 5623 | expected_changed = true; |
| 5624 | if (dir == ASCII_TO_LOWER) { |
| 5625 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 5626 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 5627 | } else { |
| 5628 | ASSERT(dir == ASCII_TO_UPPER); |
| 5629 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 5630 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 5631 | } |
| 5632 | } |
| 5633 | ASSERT(expected_changed == changed); |
| 5634 | } |
| 5635 | #endif |
| 5636 | }; |
| 5637 | |
| 5638 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5639 | struct ToLowerTraits { |
| 5640 | typedef unibrow::ToLowercase UnibrowConverter; |
| 5641 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5642 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5643 | }; |
| 5644 | |
| 5645 | |
| 5646 | struct ToUpperTraits { |
| 5647 | typedef unibrow::ToUppercase UnibrowConverter; |
| 5648 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5649 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5650 | }; |
| 5651 | |
| 5652 | } // namespace |
| 5653 | |
| 5654 | |
| 5655 | template <typename ConvertTraits> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5656 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5657 | Arguments args, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5658 | Isolate* isolate, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5659 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5660 | NoHandleAllocation ha; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5661 | CONVERT_CHECKED(String, s, args[0]); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5662 | s = s->TryFlattenGetString(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5663 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5664 | const int length = s->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5665 | // 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] | 5666 | if (length == 0) return s; |
| 5667 | |
| 5668 | // Simpler handling of ascii strings. |
| 5669 | // |
| 5670 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5671 | // character is also ascii. This is currently the case, but it |
| 5672 | // might break in the future if we implement more context and locale |
| 5673 | // dependent upper/lower conversions. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5674 | if (s->IsSeqAsciiString()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5675 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5676 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5677 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5678 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5679 | SeqAsciiString* result = SeqAsciiString::cast(o); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5680 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5681 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5682 | return has_changed_character ? result : s; |
| 5683 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5684 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5685 | Object* answer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5686 | { MaybeObject* maybe_answer = |
| 5687 | ConvertCaseHelper(isolate, s, length, length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5688 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 5689 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5690 | if (answer->IsSmi()) { |
| 5691 | // Retry with correct length. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5692 | { MaybeObject* maybe_answer = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5693 | ConvertCaseHelper(isolate, |
| 5694 | s, Smi::cast(answer)->value(), length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5695 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 5696 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5697 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5698 | return answer; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5702 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5703 | return ConvertCase<ToLowerTraits>( |
| 5704 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5708 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToUpperCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5709 | return ConvertCase<ToUpperTraits>( |
| 5710 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5713 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5714 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
| 5715 | return unibrow::WhiteSpace::Is(c) || c == 0x200b; |
| 5716 | } |
| 5717 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5718 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5719 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringTrim) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5720 | NoHandleAllocation ha; |
| 5721 | ASSERT(args.length() == 3); |
| 5722 | |
| 5723 | CONVERT_CHECKED(String, s, args[0]); |
| 5724 | CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); |
| 5725 | CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); |
| 5726 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5727 | s->TryFlatten(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5728 | int length = s->length(); |
| 5729 | |
| 5730 | int left = 0; |
| 5731 | if (trimLeft) { |
| 5732 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 5733 | left++; |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | int right = length; |
| 5738 | if (trimRight) { |
| 5739 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 5740 | right--; |
| 5741 | } |
| 5742 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5743 | return s->SubString(left, right); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5744 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5745 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5746 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5747 | void FindAsciiStringIndices(Vector<const char> subject, |
| 5748 | char pattern, |
| 5749 | ZoneList<int>* indices, |
| 5750 | unsigned int limit) { |
| 5751 | ASSERT(limit > 0); |
| 5752 | // Collect indices of pattern in subject using memchr. |
| 5753 | // Stop after finding at most limit values. |
| 5754 | const char* subject_start = reinterpret_cast<const char*>(subject.start()); |
| 5755 | const char* subject_end = subject_start + subject.length(); |
| 5756 | const char* pos = subject_start; |
| 5757 | while (limit > 0) { |
| 5758 | pos = reinterpret_cast<const char*>( |
| 5759 | memchr(pos, pattern, subject_end - pos)); |
| 5760 | if (pos == NULL) return; |
| 5761 | indices->Add(static_cast<int>(pos - subject_start)); |
| 5762 | pos++; |
| 5763 | limit--; |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5768 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5769 | void FindStringIndices(Isolate* isolate, |
| 5770 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5771 | Vector<const PatternChar> pattern, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5772 | ZoneList<int>* indices, |
| 5773 | unsigned int limit) { |
| 5774 | ASSERT(limit > 0); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5775 | // Collect indices of pattern in subject. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5776 | // Stop after finding at most limit values. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5777 | int pattern_length = pattern.length(); |
| 5778 | int index = 0; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5779 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5780 | while (limit > 0) { |
| 5781 | index = search.Search(subject, index); |
| 5782 | if (index < 0) return; |
| 5783 | indices->Add(index); |
| 5784 | index += pattern_length; |
| 5785 | limit--; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5786 | } |
| 5787 | } |
| 5788 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5789 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5790 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5791 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5792 | HandleScope handle_scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5793 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 5794 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 5795 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 5796 | |
| 5797 | int subject_length = subject->length(); |
| 5798 | int pattern_length = pattern->length(); |
| 5799 | RUNTIME_ASSERT(pattern_length > 0); |
| 5800 | |
| 5801 | // The limit can be very large (0xffffffffu), but since the pattern |
| 5802 | // isn't empty, we can never create more parts than ~half the length |
| 5803 | // of the subject. |
| 5804 | |
| 5805 | if (!subject->IsFlat()) FlattenString(subject); |
| 5806 | |
| 5807 | static const int kMaxInitialListCapacity = 16; |
| 5808 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 5809 | ZoneScope scope(isolate, DELETE_ON_EXIT); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5810 | |
| 5811 | // Find (up to limit) indices of separator and end-of-string in subject |
| 5812 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 5813 | ZoneList<int> indices(initial_capacity); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5814 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 5815 | |
| 5816 | // No allocation block. |
| 5817 | { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5818 | AssertNoAllocation nogc; |
| 5819 | if (subject->IsAsciiRepresentation()) { |
| 5820 | Vector<const char> subject_vector = subject->ToAsciiVector(); |
| 5821 | if (pattern->IsAsciiRepresentation()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 5822 | Vector<const char> pattern_vector = pattern->ToAsciiVector(); |
| 5823 | if (pattern_vector.length() == 1) { |
| 5824 | FindAsciiStringIndices(subject_vector, |
| 5825 | pattern_vector[0], |
| 5826 | &indices, |
| 5827 | limit); |
| 5828 | } else { |
| 5829 | FindStringIndices(isolate, |
| 5830 | subject_vector, |
| 5831 | pattern_vector, |
| 5832 | &indices, |
| 5833 | limit); |
| 5834 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5835 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5836 | FindStringIndices(isolate, |
| 5837 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5838 | pattern->ToUC16Vector(), |
| 5839 | &indices, |
| 5840 | limit); |
| 5841 | } |
| 5842 | } else { |
| 5843 | Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
| 5844 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5845 | FindStringIndices(isolate, |
| 5846 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5847 | pattern->ToAsciiVector(), |
| 5848 | &indices, |
| 5849 | limit); |
| 5850 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5851 | FindStringIndices(isolate, |
| 5852 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5853 | pattern->ToUC16Vector(), |
| 5854 | &indices, |
| 5855 | limit); |
| 5856 | } |
| 5857 | } |
| 5858 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5859 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5860 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 5861 | indices.Add(subject_length); |
| 5862 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5863 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5864 | // 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] | 5865 | |
| 5866 | // Create JSArray of substrings separated by separator. |
| 5867 | int part_count = indices.length(); |
| 5868 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5869 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5870 | result->set_length(Smi::FromInt(part_count)); |
| 5871 | |
| 5872 | ASSERT(result->HasFastElements()); |
| 5873 | |
| 5874 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 5875 | FixedArray::cast(result->elements())->set(0, *subject); |
| 5876 | return *result; |
| 5877 | } |
| 5878 | |
| 5879 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 5880 | int part_start = 0; |
| 5881 | for (int i = 0; i < part_count; i++) { |
| 5882 | HandleScope local_loop_handle; |
| 5883 | int part_end = indices.at(i); |
| 5884 | Handle<String> substring = |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 5885 | isolate->factory()->NewProperSubString(subject, part_start, part_end); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5886 | elements->set(i, *substring); |
| 5887 | part_start = part_end + pattern_length; |
| 5888 | } |
| 5889 | |
| 5890 | return *result; |
| 5891 | } |
| 5892 | |
| 5893 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5894 | // Copies ascii characters to the given fixed array looking up |
| 5895 | // one-char strings in the cache. Gives up on the first char that is |
| 5896 | // not in the cache and fills the remainder with smi zeros. Returns |
| 5897 | // the length of the successfully copied prefix. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5898 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 5899 | const char* chars, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5900 | FixedArray* elements, |
| 5901 | int length) { |
| 5902 | AssertNoAllocation nogc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5903 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 5904 | Object* undefined = heap->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5905 | int i; |
| 5906 | for (i = 0; i < length; ++i) { |
| 5907 | Object* value = ascii_cache->get(chars[i]); |
| 5908 | if (value == undefined) break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5909 | ASSERT(!heap->InNewSpace(value)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5910 | elements->set(i, value, SKIP_WRITE_BARRIER); |
| 5911 | } |
| 5912 | if (i < length) { |
| 5913 | ASSERT(Smi::FromInt(0) == 0); |
| 5914 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 5915 | } |
| 5916 | #ifdef DEBUG |
| 5917 | for (int j = 0; j < length; ++j) { |
| 5918 | Object* element = elements->get(j); |
| 5919 | ASSERT(element == Smi::FromInt(0) || |
| 5920 | (element->IsString() && String::cast(element)->LooksValid())); |
| 5921 | } |
| 5922 | #endif |
| 5923 | return i; |
| 5924 | } |
| 5925 | |
| 5926 | |
| 5927 | // Converts a String to JSArray. |
| 5928 | // For example, "foo" => ["f", "o", "o"]. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToArray) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5930 | HandleScope scope(isolate); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5931 | ASSERT(args.length() == 2); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5932 | CONVERT_ARG_CHECKED(String, s, 0); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5933 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5934 | |
| 5935 | s->TryFlatten(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5936 | 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] | 5937 | |
| 5938 | Handle<FixedArray> elements; |
| 5939 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5940 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5941 | { MaybeObject* maybe_obj = |
| 5942 | isolate->heap()->AllocateUninitializedFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5943 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 5944 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5945 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5946 | |
| 5947 | Vector<const char> chars = s->ToAsciiVector(); |
| 5948 | // Note, this will initialize all elements (not only the prefix) |
| 5949 | // to prevent GC from seeing partially initialized array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5950 | int num_copied_from_cache = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 5951 | chars.start(), |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5952 | *elements, |
| 5953 | length); |
| 5954 | |
| 5955 | for (int i = num_copied_from_cache; i < length; ++i) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5956 | Handle<Object> str = LookupSingleCharacterStringFromCode(chars[i]); |
| 5957 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5958 | } |
| 5959 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5960 | elements = isolate->factory()->NewFixedArray(length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5961 | for (int i = 0; i < length; ++i) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5962 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 5963 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | #ifdef DEBUG |
| 5968 | for (int i = 0; i < length; ++i) { |
| 5969 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 5970 | } |
| 5971 | #endif |
| 5972 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5973 | return *isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5977 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStringWrapper) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5978 | NoHandleAllocation ha; |
| 5979 | ASSERT(args.length() == 1); |
| 5980 | CONVERT_CHECKED(String, value, args[0]); |
| 5981 | return value->ToObject(); |
| 5982 | } |
| 5983 | |
| 5984 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5985 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 5986 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5987 | 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] | 5988 | return char_length == 0; |
| 5989 | } |
| 5990 | |
| 5991 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5992 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5993 | NoHandleAllocation ha; |
| 5994 | ASSERT(args.length() == 1); |
| 5995 | |
| 5996 | Object* number = args[0]; |
| 5997 | RUNTIME_ASSERT(number->IsNumber()); |
| 5998 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5999 | return isolate->heap()->NumberToString(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
| 6002 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6003 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToStringSkipCache) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6004 | NoHandleAllocation ha; |
| 6005 | ASSERT(args.length() == 1); |
| 6006 | |
| 6007 | Object* number = args[0]; |
| 6008 | RUNTIME_ASSERT(number->IsNumber()); |
| 6009 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6010 | return isolate->heap()->NumberToString(number, false); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 6011 | } |
| 6012 | |
| 6013 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6014 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToInteger) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6015 | NoHandleAllocation ha; |
| 6016 | ASSERT(args.length() == 1); |
| 6017 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6018 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6019 | |
| 6020 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6021 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6022 | return Smi::FromInt(static_cast<int>(number)); |
| 6023 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6024 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
| 6027 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6028 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6029 | NoHandleAllocation ha; |
| 6030 | ASSERT(args.length() == 1); |
| 6031 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6032 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6033 | |
| 6034 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6035 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6036 | return Smi::FromInt(static_cast<int>(number)); |
| 6037 | } |
| 6038 | |
| 6039 | double double_value = DoubleToInteger(number); |
| 6040 | // Map both -0 and +0 to +0. |
| 6041 | if (double_value == 0) double_value = 0; |
| 6042 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6043 | return isolate->heap()->NumberFromDouble(double_value); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 6044 | } |
| 6045 | |
| 6046 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6047 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSUint32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6048 | NoHandleAllocation ha; |
| 6049 | ASSERT(args.length() == 1); |
| 6050 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6051 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6052 | return isolate->heap()->NumberFromUint32(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6056 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSInt32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6057 | NoHandleAllocation ha; |
| 6058 | ASSERT(args.length() == 1); |
| 6059 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6060 | CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6061 | |
| 6062 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 6063 | if (number > 0 && number <= Smi::kMaxValue) { |
| 6064 | return Smi::FromInt(static_cast<int>(number)); |
| 6065 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6066 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6070 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 6071 | // a small integer. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6072 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToSmi) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6073 | NoHandleAllocation ha; |
| 6074 | ASSERT(args.length() == 1); |
| 6075 | |
| 6076 | Object* obj = args[0]; |
| 6077 | if (obj->IsSmi()) { |
| 6078 | return obj; |
| 6079 | } |
| 6080 | if (obj->IsHeapNumber()) { |
| 6081 | double value = HeapNumber::cast(obj)->value(); |
| 6082 | int int_value = FastD2I(value); |
| 6083 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 6084 | return Smi::FromInt(int_value); |
| 6085 | } |
| 6086 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6087 | return isolate->heap()->nan_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6088 | } |
| 6089 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 6090 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6091 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateHeapNumber) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6092 | NoHandleAllocation ha; |
| 6093 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6094 | return isolate->heap()->AllocateHeapNumber(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
| 6097 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6098 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6099 | NoHandleAllocation ha; |
| 6100 | ASSERT(args.length() == 2); |
| 6101 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6102 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6103 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6104 | return isolate->heap()->NumberFromDouble(x + y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6108 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSub) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6109 | NoHandleAllocation ha; |
| 6110 | ASSERT(args.length() == 2); |
| 6111 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6112 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6113 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6114 | return isolate->heap()->NumberFromDouble(x - y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6115 | } |
| 6116 | |
| 6117 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6118 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMul) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6119 | NoHandleAllocation ha; |
| 6120 | ASSERT(args.length() == 2); |
| 6121 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6122 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6123 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6124 | return isolate->heap()->NumberFromDouble(x * y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6125 | } |
| 6126 | |
| 6127 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6128 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberUnaryMinus) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6129 | NoHandleAllocation ha; |
| 6130 | ASSERT(args.length() == 1); |
| 6131 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6132 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6133 | return isolate->heap()->NumberFromDouble(-x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6137 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAlloc) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6138 | NoHandleAllocation ha; |
| 6139 | ASSERT(args.length() == 0); |
| 6140 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6141 | return isolate->heap()->NumberFromDouble(9876543210.0); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6145 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberDiv) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6146 | NoHandleAllocation ha; |
| 6147 | ASSERT(args.length() == 2); |
| 6148 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6149 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6150 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6151 | return isolate->heap()->NumberFromDouble(x / y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6155 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMod) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6156 | NoHandleAllocation ha; |
| 6157 | ASSERT(args.length() == 2); |
| 6158 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6159 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6160 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6161 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 6162 | x = modulo(x, y); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6163 | // NumberFromDouble may return a Smi instead of a Number object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6164 | return isolate->heap()->NumberFromDouble(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6168 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6169 | NoHandleAllocation ha; |
| 6170 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6171 | CONVERT_CHECKED(String, str1, args[0]); |
| 6172 | CONVERT_CHECKED(String, str2, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6173 | isolate->counters()->string_add_runtime()->Increment(); |
| 6174 | return isolate->heap()->AllocateConsString(str1, str2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6178 | template <typename sinkchar> |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6179 | static inline void StringBuilderConcatHelper(String* special, |
| 6180 | sinkchar* sink, |
| 6181 | FixedArray* fixed_array, |
| 6182 | int array_length) { |
| 6183 | int position = 0; |
| 6184 | for (int i = 0; i < array_length; i++) { |
| 6185 | Object* element = fixed_array->get(i); |
| 6186 | if (element->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6187 | // Smi encoding of position and length. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6188 | int encoded_slice = Smi::cast(element)->value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6189 | int pos; |
| 6190 | int len; |
| 6191 | if (encoded_slice > 0) { |
| 6192 | // Position and length encoded in one smi. |
| 6193 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 6194 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 6195 | } else { |
| 6196 | // Position and length encoded in two smis. |
| 6197 | Object* obj = fixed_array->get(++i); |
| 6198 | ASSERT(obj->IsSmi()); |
| 6199 | pos = Smi::cast(obj)->value(); |
| 6200 | len = -encoded_slice; |
| 6201 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6202 | String::WriteToFlat(special, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 6203 | sink + position, |
| 6204 | pos, |
| 6205 | pos + len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6206 | position += len; |
| 6207 | } else { |
| 6208 | String* string = String::cast(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6209 | int element_length = string->length(); |
| 6210 | String::WriteToFlat(string, sink + position, 0, element_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6211 | position += element_length; |
| 6212 | } |
| 6213 | } |
| 6214 | } |
| 6215 | |
| 6216 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6217 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6218 | NoHandleAllocation ha; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6219 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6220 | CONVERT_CHECKED(JSArray, array, args[0]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6221 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6222 | isolate->context()->mark_out_of_memory(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6223 | return Failure::OutOfMemoryException(); |
| 6224 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6225 | int array_length = args.smi_at(1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6226 | CONVERT_CHECKED(String, special, args[2]); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6227 | |
| 6228 | // This assumption is used by the slice encoding in one or two smis. |
| 6229 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6230 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6231 | int special_length = special->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6232 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6233 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6234 | } |
| 6235 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6236 | if (fixed_array->length() < array_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6237 | array_length = fixed_array->length(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6238 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6239 | |
| 6240 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6241 | return isolate->heap()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6242 | } else if (array_length == 1) { |
| 6243 | Object* first = fixed_array->get(0); |
| 6244 | if (first->IsString()) return first; |
| 6245 | } |
| 6246 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6247 | bool ascii = special->HasOnlyAsciiChars(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6248 | int position = 0; |
| 6249 | for (int i = 0; i < array_length; i++) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6250 | int increment = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6251 | Object* elt = fixed_array->get(i); |
| 6252 | if (elt->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6253 | // Smi encoding of position and length. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6254 | int smi_value = Smi::cast(elt)->value(); |
| 6255 | int pos; |
| 6256 | int len; |
| 6257 | if (smi_value > 0) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6258 | // Position and length encoded in one smi. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6259 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6260 | len = StringBuilderSubstringLength::decode(smi_value); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6261 | } else { |
| 6262 | // Position and length encoded in two smis. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6263 | len = -smi_value; |
| 6264 | // Get the position and check that it is a positive smi. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6265 | i++; |
| 6266 | if (i >= array_length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6267 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6268 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6269 | Object* next_smi = fixed_array->get(i); |
| 6270 | if (!next_smi->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6271 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6272 | } |
| 6273 | pos = Smi::cast(next_smi)->value(); |
| 6274 | if (pos < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6275 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6276 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6277 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6278 | ASSERT(pos >= 0); |
| 6279 | ASSERT(len >= 0); |
| 6280 | if (pos > special_length || len > special_length - pos) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6281 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6282 | } |
| 6283 | increment = len; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6284 | } else if (elt->IsString()) { |
| 6285 | String* element = String::cast(elt); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6286 | int element_length = element->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6287 | increment = element_length; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6288 | if (ascii && !element->HasOnlyAsciiChars()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6289 | ascii = false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6290 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6291 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6292 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6293 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6294 | if (increment > String::kMaxLength - position) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6295 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6296 | return Failure::OutOfMemoryException(); |
| 6297 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6298 | position += increment; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | int length = position; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6302 | Object* object; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6303 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6304 | if (ascii) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6305 | { MaybeObject* maybe_object = |
| 6306 | isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6307 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6308 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6309 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6310 | StringBuilderConcatHelper(special, |
| 6311 | answer->GetChars(), |
| 6312 | fixed_array, |
| 6313 | array_length); |
| 6314 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6315 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6316 | { MaybeObject* maybe_object = |
| 6317 | isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6318 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6319 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6320 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6321 | StringBuilderConcatHelper(special, |
| 6322 | answer->GetChars(), |
| 6323 | fixed_array, |
| 6324 | array_length); |
| 6325 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6326 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6330 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6331 | NoHandleAllocation ha; |
| 6332 | ASSERT(args.length() == 3); |
| 6333 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 6334 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6335 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6336 | return Failure::OutOfMemoryException(); |
| 6337 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6338 | int array_length = args.smi_at(1); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6339 | CONVERT_CHECKED(String, separator, args[2]); |
| 6340 | |
| 6341 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6342 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6343 | } |
| 6344 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6345 | if (fixed_array->length() < array_length) { |
| 6346 | array_length = fixed_array->length(); |
| 6347 | } |
| 6348 | |
| 6349 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6350 | return isolate->heap()->empty_string(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6351 | } else if (array_length == 1) { |
| 6352 | Object* first = fixed_array->get(0); |
| 6353 | if (first->IsString()) return first; |
| 6354 | } |
| 6355 | |
| 6356 | int separator_length = separator->length(); |
| 6357 | int max_nof_separators = |
| 6358 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6359 | if (max_nof_separators < (array_length - 1)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6360 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6361 | return Failure::OutOfMemoryException(); |
| 6362 | } |
| 6363 | int length = (array_length - 1) * separator_length; |
| 6364 | for (int i = 0; i < array_length; i++) { |
| 6365 | Object* element_obj = fixed_array->get(i); |
| 6366 | if (!element_obj->IsString()) { |
| 6367 | // TODO(1161): handle this case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6368 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6369 | } |
| 6370 | String* element = String::cast(element_obj); |
| 6371 | int increment = element->length(); |
| 6372 | if (increment > String::kMaxLength - length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6373 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6374 | return Failure::OutOfMemoryException(); |
| 6375 | } |
| 6376 | length += increment; |
| 6377 | } |
| 6378 | |
| 6379 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6380 | { MaybeObject* maybe_object = |
| 6381 | isolate->heap()->AllocateRawTwoByteString(length); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6382 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6383 | } |
| 6384 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6385 | |
| 6386 | uc16* sink = answer->GetChars(); |
| 6387 | #ifdef DEBUG |
| 6388 | uc16* end = sink + length; |
| 6389 | #endif |
| 6390 | |
| 6391 | String* first = String::cast(fixed_array->get(0)); |
| 6392 | int first_length = first->length(); |
| 6393 | String::WriteToFlat(first, sink, 0, first_length); |
| 6394 | sink += first_length; |
| 6395 | |
| 6396 | for (int i = 1; i < array_length; i++) { |
| 6397 | ASSERT(sink + separator_length <= end); |
| 6398 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6399 | sink += separator_length; |
| 6400 | |
| 6401 | String* element = String::cast(fixed_array->get(i)); |
| 6402 | int element_length = element->length(); |
| 6403 | ASSERT(sink + element_length <= end); |
| 6404 | String::WriteToFlat(element, sink, 0, element_length); |
| 6405 | sink += element_length; |
| 6406 | } |
| 6407 | ASSERT(sink == end); |
| 6408 | |
| 6409 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6410 | return answer; |
| 6411 | } |
| 6412 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6413 | template <typename Char> |
| 6414 | static void JoinSparseArrayWithSeparator(FixedArray* elements, |
| 6415 | int elements_length, |
| 6416 | uint32_t array_length, |
| 6417 | String* separator, |
| 6418 | Vector<Char> buffer) { |
| 6419 | int previous_separator_position = 0; |
| 6420 | int separator_length = separator->length(); |
| 6421 | int cursor = 0; |
| 6422 | for (int i = 0; i < elements_length; i += 2) { |
| 6423 | int position = NumberToInt32(elements->get(i)); |
| 6424 | String* string = String::cast(elements->get(i + 1)); |
| 6425 | int string_length = string->length(); |
| 6426 | if (string->length() > 0) { |
| 6427 | while (previous_separator_position < position) { |
| 6428 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6429 | 0, separator_length); |
| 6430 | cursor += separator_length; |
| 6431 | previous_separator_position++; |
| 6432 | } |
| 6433 | String::WriteToFlat<Char>(string, &buffer[cursor], |
| 6434 | 0, string_length); |
| 6435 | cursor += string->length(); |
| 6436 | } |
| 6437 | } |
| 6438 | if (separator_length > 0) { |
| 6439 | // Array length must be representable as a signed 32-bit number, |
| 6440 | // otherwise the total string length would have been too large. |
| 6441 | ASSERT(array_length <= 0x7fffffff); // Is int32_t. |
| 6442 | int last_array_index = static_cast<int>(array_length - 1); |
| 6443 | while (previous_separator_position < last_array_index) { |
| 6444 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6445 | 0, separator_length); |
| 6446 | cursor += separator_length; |
| 6447 | previous_separator_position++; |
| 6448 | } |
| 6449 | } |
| 6450 | ASSERT(cursor <= buffer.length()); |
| 6451 | } |
| 6452 | |
| 6453 | |
| 6454 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) { |
| 6455 | NoHandleAllocation ha; |
| 6456 | ASSERT(args.length() == 3); |
| 6457 | CONVERT_CHECKED(JSArray, elements_array, args[0]); |
| 6458 | RUNTIME_ASSERT(elements_array->HasFastElements()); |
| 6459 | CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]); |
| 6460 | CONVERT_CHECKED(String, separator, args[2]); |
| 6461 | // elements_array is fast-mode JSarray of alternating positions |
| 6462 | // (increasing order) and strings. |
| 6463 | // array_length is length of original array (used to add separators); |
| 6464 | // separator is string to put between elements. Assumed to be non-empty. |
| 6465 | |
| 6466 | // Find total length of join result. |
| 6467 | int string_length = 0; |
| 6468 | bool is_ascii = true; |
| 6469 | int max_string_length = SeqAsciiString::kMaxLength; |
| 6470 | bool overflow = false; |
| 6471 | CONVERT_NUMBER_CHECKED(int, elements_length, |
| 6472 | Int32, elements_array->length()); |
| 6473 | RUNTIME_ASSERT((elements_length & 1) == 0); // Even length. |
| 6474 | FixedArray* elements = FixedArray::cast(elements_array->elements()); |
| 6475 | for (int i = 0; i < elements_length; i += 2) { |
| 6476 | RUNTIME_ASSERT(elements->get(i)->IsNumber()); |
| 6477 | CONVERT_CHECKED(String, string, elements->get(i + 1)); |
| 6478 | int length = string->length(); |
| 6479 | if (is_ascii && !string->IsAsciiRepresentation()) { |
| 6480 | is_ascii = false; |
| 6481 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6482 | } |
| 6483 | if (length > max_string_length || |
| 6484 | max_string_length - length < string_length) { |
| 6485 | overflow = true; |
| 6486 | break; |
| 6487 | } |
| 6488 | string_length += length; |
| 6489 | } |
| 6490 | int separator_length = separator->length(); |
| 6491 | if (!overflow && separator_length > 0) { |
| 6492 | if (array_length <= 0x7fffffffu) { |
| 6493 | int separator_count = static_cast<int>(array_length) - 1; |
| 6494 | int remaining_length = max_string_length - string_length; |
| 6495 | if ((remaining_length / separator_length) >= separator_count) { |
| 6496 | string_length += separator_length * (array_length - 1); |
| 6497 | } else { |
| 6498 | // Not room for the separators within the maximal string length. |
| 6499 | overflow = true; |
| 6500 | } |
| 6501 | } else { |
| 6502 | // Nonempty separator and at least 2^31-1 separators necessary |
| 6503 | // means that the string is too large to create. |
| 6504 | STATIC_ASSERT(String::kMaxLength < 0x7fffffff); |
| 6505 | overflow = true; |
| 6506 | } |
| 6507 | } |
| 6508 | if (overflow) { |
| 6509 | // Throw OutOfMemory exception for creating too large a string. |
| 6510 | V8::FatalProcessOutOfMemory("Array join result too large."); |
| 6511 | } |
| 6512 | |
| 6513 | if (is_ascii) { |
| 6514 | MaybeObject* result_allocation = |
| 6515 | isolate->heap()->AllocateRawAsciiString(string_length); |
| 6516 | if (result_allocation->IsFailure()) return result_allocation; |
| 6517 | SeqAsciiString* result_string = |
| 6518 | SeqAsciiString::cast(result_allocation->ToObjectUnchecked()); |
| 6519 | JoinSparseArrayWithSeparator<char>(elements, |
| 6520 | elements_length, |
| 6521 | array_length, |
| 6522 | separator, |
| 6523 | Vector<char>(result_string->GetChars(), |
| 6524 | string_length)); |
| 6525 | return result_string; |
| 6526 | } else { |
| 6527 | MaybeObject* result_allocation = |
| 6528 | isolate->heap()->AllocateRawTwoByteString(string_length); |
| 6529 | if (result_allocation->IsFailure()) return result_allocation; |
| 6530 | SeqTwoByteString* result_string = |
| 6531 | SeqTwoByteString::cast(result_allocation->ToObjectUnchecked()); |
| 6532 | JoinSparseArrayWithSeparator<uc16>(elements, |
| 6533 | elements_length, |
| 6534 | array_length, |
| 6535 | separator, |
| 6536 | Vector<uc16>(result_string->GetChars(), |
| 6537 | string_length)); |
| 6538 | return result_string; |
| 6539 | } |
| 6540 | } |
| 6541 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6542 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6543 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberOr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6544 | NoHandleAllocation ha; |
| 6545 | ASSERT(args.length() == 2); |
| 6546 | |
| 6547 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6548 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6549 | return isolate->heap()->NumberFromInt32(x | y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
| 6552 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6553 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAnd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6554 | NoHandleAllocation ha; |
| 6555 | ASSERT(args.length() == 2); |
| 6556 | |
| 6557 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6558 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6559 | return isolate->heap()->NumberFromInt32(x & y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6563 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberXor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6564 | NoHandleAllocation ha; |
| 6565 | ASSERT(args.length() == 2); |
| 6566 | |
| 6567 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6568 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6569 | return isolate->heap()->NumberFromInt32(x ^ y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6570 | } |
| 6571 | |
| 6572 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6573 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberNot) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6574 | NoHandleAllocation ha; |
| 6575 | ASSERT(args.length() == 1); |
| 6576 | |
| 6577 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6578 | return isolate->heap()->NumberFromInt32(~x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6579 | } |
| 6580 | |
| 6581 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6582 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShl) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6583 | NoHandleAllocation ha; |
| 6584 | ASSERT(args.length() == 2); |
| 6585 | |
| 6586 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6587 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6588 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
| 6591 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6592 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6593 | NoHandleAllocation ha; |
| 6594 | ASSERT(args.length() == 2); |
| 6595 | |
| 6596 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 6597 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6598 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6599 | } |
| 6600 | |
| 6601 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6602 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSar) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6603 | NoHandleAllocation ha; |
| 6604 | ASSERT(args.length() == 2); |
| 6605 | |
| 6606 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6607 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6608 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6609 | } |
| 6610 | |
| 6611 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6612 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6613 | NoHandleAllocation ha; |
| 6614 | ASSERT(args.length() == 2); |
| 6615 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6616 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6617 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6618 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 6619 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 6620 | if (x == y) return Smi::FromInt(EQUAL); |
| 6621 | Object* result; |
| 6622 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 6623 | result = Smi::FromInt(EQUAL); |
| 6624 | } else { |
| 6625 | result = Smi::FromInt(NOT_EQUAL); |
| 6626 | } |
| 6627 | return result; |
| 6628 | } |
| 6629 | |
| 6630 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6631 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6632 | NoHandleAllocation ha; |
| 6633 | ASSERT(args.length() == 2); |
| 6634 | |
| 6635 | CONVERT_CHECKED(String, x, args[0]); |
| 6636 | CONVERT_CHECKED(String, y, args[1]); |
| 6637 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6638 | bool not_equal = !x->Equals(y); |
| 6639 | // This is slightly convoluted because the value that signifies |
| 6640 | // equality is 0 and inequality is 1 so we have to negate the result |
| 6641 | // from String::Equals. |
| 6642 | ASSERT(not_equal == 0 || not_equal == 1); |
| 6643 | STATIC_CHECK(EQUAL == 0); |
| 6644 | STATIC_CHECK(NOT_EQUAL == 1); |
| 6645 | return Smi::FromInt(not_equal); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
| 6648 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6649 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6650 | NoHandleAllocation ha; |
| 6651 | ASSERT(args.length() == 3); |
| 6652 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6653 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6654 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6655 | if (isnan(x) || isnan(y)) return args[2]; |
| 6656 | if (x == y) return Smi::FromInt(EQUAL); |
| 6657 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 6658 | return Smi::FromInt(GREATER); |
| 6659 | } |
| 6660 | |
| 6661 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6662 | // Compare two Smis as if they were converted to strings and then |
| 6663 | // compared lexicographically. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6664 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6665 | NoHandleAllocation ha; |
| 6666 | ASSERT(args.length() == 2); |
| 6667 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6668 | // Extract the integer values from the Smis. |
| 6669 | CONVERT_CHECKED(Smi, x, args[0]); |
| 6670 | CONVERT_CHECKED(Smi, y, args[1]); |
| 6671 | int x_value = x->value(); |
| 6672 | int y_value = y->value(); |
| 6673 | |
| 6674 | // If the integers are equal so are the string representations. |
| 6675 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 6676 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6677 | // 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] | 6678 | // same as the lexicographic order of the string representations. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6679 | if (x_value == 0 || y_value == 0) |
| 6680 | return Smi::FromInt(x_value < y_value ? LESS : GREATER); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6681 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6682 | // 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] | 6683 | // smallest because the char code of '-' is less than the char code |
| 6684 | // of any digit. Otherwise, we make both values positive. |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6685 | |
| 6686 | // Use unsigned values otherwise the logic is incorrect for -MIN_INT on |
| 6687 | // architectures using 32-bit Smis. |
| 6688 | uint32_t x_scaled = x_value; |
| 6689 | uint32_t y_scaled = y_value; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6690 | if (x_value < 0 || y_value < 0) { |
| 6691 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 6692 | if (x_value >= 0) return Smi::FromInt(GREATER); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6693 | x_scaled = -x_value; |
| 6694 | y_scaled = -y_value; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6695 | } |
| 6696 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6697 | static const uint32_t kPowersOf10[] = { |
| 6698 | 1, 10, 100, 1000, 10*1000, 100*1000, |
| 6699 | 1000*1000, 10*1000*1000, 100*1000*1000, |
| 6700 | 1000*1000*1000 |
| 6701 | }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6702 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6703 | // If the integers have the same number of decimal digits they can be |
| 6704 | // compared directly as the numeric order is the same as the |
| 6705 | // lexicographic order. If one integer has fewer digits, it is scaled |
| 6706 | // by some power of 10 to have the same number of digits as the longer |
| 6707 | // integer. If the scaled integers are equal it means the shorter |
| 6708 | // integer comes first in the lexicographic order. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6709 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6710 | // From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 |
| 6711 | int x_log2 = IntegerLog2(x_scaled); |
| 6712 | int x_log10 = ((x_log2 + 1) * 1233) >> 12; |
| 6713 | x_log10 -= x_scaled < kPowersOf10[x_log10]; |
| 6714 | |
| 6715 | int y_log2 = IntegerLog2(y_scaled); |
| 6716 | int y_log10 = ((y_log2 + 1) * 1233) >> 12; |
| 6717 | y_log10 -= y_scaled < kPowersOf10[y_log10]; |
| 6718 | |
| 6719 | int tie = EQUAL; |
| 6720 | |
| 6721 | if (x_log10 < y_log10) { |
| 6722 | // X has fewer digits. We would like to simply scale up X but that |
| 6723 | // might overflow, e.g when comparing 9 with 1_000_000_000, 9 would |
| 6724 | // be scaled up to 9_000_000_000. So we scale up by the next |
| 6725 | // smallest power and scale down Y to drop one digit. It is OK to |
| 6726 | // drop one digit from the longer integer since the final digit is |
| 6727 | // past the length of the shorter integer. |
| 6728 | x_scaled *= kPowersOf10[y_log10 - x_log10 - 1]; |
| 6729 | y_scaled /= 10; |
| 6730 | tie = LESS; |
| 6731 | } else if (y_log10 < x_log10) { |
| 6732 | y_scaled *= kPowersOf10[x_log10 - y_log10 - 1]; |
| 6733 | x_scaled /= 10; |
| 6734 | tie = GREATER; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 6737 | if (x_scaled < y_scaled) return Smi::FromInt(LESS); |
| 6738 | if (x_scaled > y_scaled) return Smi::FromInt(GREATER); |
| 6739 | return Smi::FromInt(tie); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
| 6742 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6743 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 6744 | String* x, |
| 6745 | String* y) { |
| 6746 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 6747 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6748 | bufx.Reset(x); |
| 6749 | bufy.Reset(y); |
| 6750 | while (bufx.has_more() && bufy.has_more()) { |
| 6751 | int d = bufx.GetNext() - bufy.GetNext(); |
| 6752 | if (d < 0) return Smi::FromInt(LESS); |
| 6753 | else if (d > 0) return Smi::FromInt(GREATER); |
| 6754 | } |
| 6755 | |
| 6756 | // x is (non-trivial) prefix of y: |
| 6757 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 6758 | // y is prefix of x: |
| 6759 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 6760 | } |
| 6761 | |
| 6762 | |
| 6763 | static Object* FlatStringCompare(String* x, String* y) { |
| 6764 | ASSERT(x->IsFlat()); |
| 6765 | ASSERT(y->IsFlat()); |
| 6766 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 6767 | int prefix_length = x->length(); |
| 6768 | if (y->length() < prefix_length) { |
| 6769 | prefix_length = y->length(); |
| 6770 | equal_prefix_result = Smi::FromInt(GREATER); |
| 6771 | } else if (y->length() > prefix_length) { |
| 6772 | equal_prefix_result = Smi::FromInt(LESS); |
| 6773 | } |
| 6774 | int r; |
| 6775 | if (x->IsAsciiRepresentation()) { |
| 6776 | Vector<const char> x_chars = x->ToAsciiVector(); |
| 6777 | if (y->IsAsciiRepresentation()) { |
| 6778 | Vector<const char> y_chars = y->ToAsciiVector(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6779 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6780 | } else { |
| 6781 | Vector<const uc16> y_chars = y->ToUC16Vector(); |
| 6782 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6783 | } |
| 6784 | } else { |
| 6785 | Vector<const uc16> x_chars = x->ToUC16Vector(); |
| 6786 | if (y->IsAsciiRepresentation()) { |
| 6787 | Vector<const char> y_chars = y->ToAsciiVector(); |
| 6788 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6789 | } else { |
| 6790 | Vector<const uc16> y_chars = y->ToUC16Vector(); |
| 6791 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6792 | } |
| 6793 | } |
| 6794 | Object* result; |
| 6795 | if (r == 0) { |
| 6796 | result = equal_prefix_result; |
| 6797 | } else { |
| 6798 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 6799 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6800 | ASSERT(result == |
| 6801 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6802 | return result; |
| 6803 | } |
| 6804 | |
| 6805 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6806 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6807 | NoHandleAllocation ha; |
| 6808 | ASSERT(args.length() == 2); |
| 6809 | |
| 6810 | CONVERT_CHECKED(String, x, args[0]); |
| 6811 | CONVERT_CHECKED(String, y, args[1]); |
| 6812 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6813 | isolate->counters()->string_compare_runtime()->Increment(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6814 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6815 | // A few fast case tests before we flatten. |
| 6816 | if (x == y) return Smi::FromInt(EQUAL); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6817 | if (y->length() == 0) { |
| 6818 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6819 | return Smi::FromInt(GREATER); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6820 | } else if (x->length() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6821 | return Smi::FromInt(LESS); |
| 6822 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6823 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6824 | int d = x->Get(0) - y->Get(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6825 | if (d < 0) return Smi::FromInt(LESS); |
| 6826 | else if (d > 0) return Smi::FromInt(GREATER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6827 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6828 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6829 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6830 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6831 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6832 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6833 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6834 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6835 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6836 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6837 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6838 | } |
| 6839 | |
| 6840 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6841 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_acos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6842 | NoHandleAllocation ha; |
| 6843 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6844 | isolate->counters()->math_acos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6845 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6846 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6847 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
| 6850 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6851 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_asin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6852 | NoHandleAllocation ha; |
| 6853 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6854 | isolate->counters()->math_asin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6855 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6856 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6857 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
| 6860 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6861 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6862 | NoHandleAllocation ha; |
| 6863 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6864 | isolate->counters()->math_atan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6865 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6866 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6867 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6871 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 6872 | |
| 6873 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6874 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6875 | NoHandleAllocation ha; |
| 6876 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6877 | isolate->counters()->math_atan2()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6878 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6879 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6880 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6881 | double result; |
| 6882 | if (isinf(x) && isinf(y)) { |
| 6883 | // Make sure that the result in case of two infinite arguments |
| 6884 | // is a multiple of Pi / 4. The sign of the result is determined |
| 6885 | // by the first argument (x) and the sign of the second argument |
| 6886 | // determines the multiplier: one or three. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6887 | int multiplier = (x < 0) ? -1 : 1; |
| 6888 | if (y < 0) multiplier *= 3; |
| 6889 | result = multiplier * kPiDividedBy4; |
| 6890 | } else { |
| 6891 | result = atan2(x, y); |
| 6892 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6893 | return isolate->heap()->AllocateHeapNumber(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6897 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_ceil) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6898 | NoHandleAllocation ha; |
| 6899 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6900 | isolate->counters()->math_ceil()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6901 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6902 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6903 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6907 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6908 | NoHandleAllocation ha; |
| 6909 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6910 | isolate->counters()->math_cos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6911 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6912 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6913 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
| 6916 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6917 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_exp) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6918 | NoHandleAllocation ha; |
| 6919 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6920 | isolate->counters()->math_exp()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6921 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6922 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6923 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6927 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_floor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6928 | NoHandleAllocation ha; |
| 6929 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6930 | isolate->counters()->math_floor()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6931 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6932 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6933 | return isolate->heap()->NumberFromDouble(floor(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6934 | } |
| 6935 | |
| 6936 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6937 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6938 | NoHandleAllocation ha; |
| 6939 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6940 | isolate->counters()->math_log()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6941 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6942 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6943 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6947 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6948 | NoHandleAllocation ha; |
| 6949 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6950 | isolate->counters()->math_pow()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6951 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6952 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6953 | |
| 6954 | // If the second argument is a smi, it is much faster to call the |
| 6955 | // custom powi() function than the generic pow(). |
| 6956 | if (args[1]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6957 | int y = args.smi_at(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6958 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6959 | } |
| 6960 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6961 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6962 | return isolate->heap()->AllocateHeapNumber(power_double_double(x, y)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6963 | } |
| 6964 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6965 | // Fast version of Math.pow if we know that y is not an integer and |
| 6966 | // y is not -0.5 or 0.5. Used as slowcase from codegen. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6967 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6968 | NoHandleAllocation ha; |
| 6969 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 6970 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
| 6971 | CONVERT_DOUBLE_ARG_CHECKED(y, 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6972 | if (y == 0) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6973 | return Smi::FromInt(1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6974 | } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6975 | return isolate->heap()->nan_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6976 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6977 | return isolate->heap()->AllocateHeapNumber(pow(x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6978 | } |
| 6979 | } |
| 6980 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6981 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6982 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6983 | NoHandleAllocation ha; |
| 6984 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6985 | isolate->counters()->math_round()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6986 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 6987 | if (!args[0]->IsHeapNumber()) { |
| 6988 | // Must be smi. Return the argument unchanged for all the other types |
| 6989 | // to make fuzz-natives test happy. |
| 6990 | return args[0]; |
| 6991 | } |
| 6992 | |
| 6993 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 6994 | |
| 6995 | double value = number->value(); |
| 6996 | int exponent = number->get_exponent(); |
| 6997 | int sign = number->get_sign(); |
| 6998 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6999 | if (exponent < -1) { |
| 7000 | // Number in range ]-0.5..0.5[. These always round to +/-zero. |
| 7001 | if (sign) return isolate->heap()->minus_zero_value(); |
| 7002 | return Smi::FromInt(0); |
| 7003 | } |
| 7004 | |
| 7005 | // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and |
| 7006 | // should be rounded to 2^30, which is not smi (for 31-bit smis, similar |
| 7007 | // agument holds for 32-bit smis). |
| 7008 | if (!sign && exponent < kSmiValueSize - 2) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7009 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 7010 | } |
| 7011 | |
| 7012 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 7013 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 7014 | if (exponent >= 52) { |
| 7015 | return number; |
| 7016 | } |
| 7017 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7018 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7019 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7020 | // Do not call NumberFromDouble() to avoid extra checks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7021 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
| 7024 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7025 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7026 | NoHandleAllocation ha; |
| 7027 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7028 | isolate->counters()->math_sin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7029 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7030 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7031 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
| 7034 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7035 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sqrt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7036 | NoHandleAllocation ha; |
| 7037 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7038 | isolate->counters()->math_sqrt()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7039 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7040 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7041 | return isolate->heap()->AllocateHeapNumber(sqrt(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7042 | } |
| 7043 | |
| 7044 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7045 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_tan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7046 | NoHandleAllocation ha; |
| 7047 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7048 | isolate->counters()->math_tan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7049 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7050 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7051 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7055 | static int MakeDay(int year, int month, int day) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7056 | static const int day_from_month[] = {0, 31, 59, 90, 120, 151, |
| 7057 | 181, 212, 243, 273, 304, 334}; |
| 7058 | static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152, |
| 7059 | 182, 213, 244, 274, 305, 335}; |
| 7060 | |
| 7061 | year += month / 12; |
| 7062 | month %= 12; |
| 7063 | if (month < 0) { |
| 7064 | year--; |
| 7065 | month += 12; |
| 7066 | } |
| 7067 | |
| 7068 | ASSERT(month >= 0); |
| 7069 | ASSERT(month < 12); |
| 7070 | |
| 7071 | // year_delta is an arbitrary number such that: |
| 7072 | // a) year_delta = -1 (mod 400) |
| 7073 | // b) year + year_delta > 0 for years in the range defined by |
| 7074 | // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
| 7075 | // Jan 1 1970. This is required so that we don't run into integer |
| 7076 | // division of negative numbers. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7077 | // 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] | 7078 | // operations. |
| 7079 | static const int year_delta = 399999; |
| 7080 | static const int base_day = 365 * (1970 + year_delta) + |
| 7081 | (1970 + year_delta) / 4 - |
| 7082 | (1970 + year_delta) / 100 + |
| 7083 | (1970 + year_delta) / 400; |
| 7084 | |
| 7085 | int year1 = year + year_delta; |
| 7086 | int day_from_year = 365 * year1 + |
| 7087 | year1 / 4 - |
| 7088 | year1 / 100 + |
| 7089 | year1 / 400 - |
| 7090 | base_day; |
| 7091 | |
| 7092 | if (year % 4 || (year % 100 == 0 && year % 400 != 0)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7093 | return day_from_year + day_from_month[month] + day - 1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7094 | } |
| 7095 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7096 | return day_from_year + day_from_month_leap[month] + day - 1; |
| 7097 | } |
| 7098 | |
| 7099 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7100 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7101 | NoHandleAllocation ha; |
| 7102 | ASSERT(args.length() == 3); |
| 7103 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7104 | CONVERT_SMI_ARG_CHECKED(year, 0); |
| 7105 | CONVERT_SMI_ARG_CHECKED(month, 1); |
| 7106 | CONVERT_SMI_ARG_CHECKED(date, 2); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7107 | |
| 7108 | return Smi::FromInt(MakeDay(year, month, date)); |
| 7109 | } |
| 7110 | |
| 7111 | |
| 7112 | static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1}; |
| 7113 | static const int kDaysIn4Years = 4 * 365 + 1; |
| 7114 | static const int kDaysIn100Years = 25 * kDaysIn4Years - 1; |
| 7115 | static const int kDaysIn400Years = 4 * kDaysIn100Years + 1; |
| 7116 | static const int kDays1970to2000 = 30 * 365 + 7; |
| 7117 | static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - |
| 7118 | kDays1970to2000; |
| 7119 | static const int kYearsOffset = 400000; |
| 7120 | |
| 7121 | static const char kDayInYear[] = { |
| 7122 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7123 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7124 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7125 | 22, 23, 24, 25, 26, 27, 28, |
| 7126 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7127 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7128 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7129 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7130 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7131 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7132 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7133 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7134 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7135 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7136 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7137 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7138 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7139 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7140 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7141 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7142 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7143 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7144 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7145 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7146 | |
| 7147 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7148 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7149 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7150 | 22, 23, 24, 25, 26, 27, 28, |
| 7151 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7152 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7153 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7154 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7155 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7156 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7157 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7158 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7159 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7160 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7161 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7162 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7163 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7164 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7165 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7166 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7167 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7168 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7169 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7170 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7171 | |
| 7172 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7173 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7174 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7175 | 22, 23, 24, 25, 26, 27, 28, 29, |
| 7176 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7177 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7178 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7179 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7180 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7181 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7182 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7183 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7184 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7185 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7186 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7187 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7188 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7189 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7190 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7191 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7192 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7193 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7194 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7195 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7196 | |
| 7197 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7198 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7199 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7200 | 22, 23, 24, 25, 26, 27, 28, |
| 7201 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7202 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7203 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7204 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7205 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7206 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7207 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7208 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7209 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7210 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7211 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7212 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7213 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7214 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7215 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7216 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 7217 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7218 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 7219 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 7220 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 7221 | |
| 7222 | static const char kMonthInYear[] = { |
| 7223 | 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, |
| 7224 | 0, 0, 0, 0, 0, 0, |
| 7225 | 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, |
| 7226 | 1, 1, 1, |
| 7227 | 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, |
| 7228 | 2, 2, 2, 2, 2, 2, |
| 7229 | 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, |
| 7230 | 3, 3, 3, 3, 3, |
| 7231 | 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, |
| 7232 | 4, 4, 4, 4, 4, 4, |
| 7233 | 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, |
| 7234 | 5, 5, 5, 5, 5, |
| 7235 | 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, |
| 7236 | 6, 6, 6, 6, 6, 6, |
| 7237 | 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, |
| 7238 | 7, 7, 7, 7, 7, 7, |
| 7239 | 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, |
| 7240 | 8, 8, 8, 8, 8, |
| 7241 | 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, |
| 7242 | 9, 9, 9, 9, 9, 9, |
| 7243 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7244 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7245 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7246 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7247 | |
| 7248 | 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, |
| 7249 | 0, 0, 0, 0, 0, 0, |
| 7250 | 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, |
| 7251 | 1, 1, 1, |
| 7252 | 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, |
| 7253 | 2, 2, 2, 2, 2, 2, |
| 7254 | 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, |
| 7255 | 3, 3, 3, 3, 3, |
| 7256 | 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, |
| 7257 | 4, 4, 4, 4, 4, 4, |
| 7258 | 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, |
| 7259 | 5, 5, 5, 5, 5, |
| 7260 | 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, |
| 7261 | 6, 6, 6, 6, 6, 6, |
| 7262 | 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, |
| 7263 | 7, 7, 7, 7, 7, 7, |
| 7264 | 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, |
| 7265 | 8, 8, 8, 8, 8, |
| 7266 | 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, |
| 7267 | 9, 9, 9, 9, 9, 9, |
| 7268 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7269 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7270 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7271 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7272 | |
| 7273 | 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, |
| 7274 | 0, 0, 0, 0, 0, 0, |
| 7275 | 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, |
| 7276 | 1, 1, 1, 1, |
| 7277 | 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, |
| 7278 | 2, 2, 2, 2, 2, 2, |
| 7279 | 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, |
| 7280 | 3, 3, 3, 3, 3, |
| 7281 | 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, |
| 7282 | 4, 4, 4, 4, 4, 4, |
| 7283 | 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, |
| 7284 | 5, 5, 5, 5, 5, |
| 7285 | 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, |
| 7286 | 6, 6, 6, 6, 6, 6, |
| 7287 | 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, |
| 7288 | 7, 7, 7, 7, 7, 7, |
| 7289 | 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, |
| 7290 | 8, 8, 8, 8, 8, |
| 7291 | 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, |
| 7292 | 9, 9, 9, 9, 9, 9, |
| 7293 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7294 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7295 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7296 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7297 | |
| 7298 | 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, |
| 7299 | 0, 0, 0, 0, 0, 0, |
| 7300 | 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, |
| 7301 | 1, 1, 1, |
| 7302 | 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, |
| 7303 | 2, 2, 2, 2, 2, 2, |
| 7304 | 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, |
| 7305 | 3, 3, 3, 3, 3, |
| 7306 | 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, |
| 7307 | 4, 4, 4, 4, 4, 4, |
| 7308 | 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, |
| 7309 | 5, 5, 5, 5, 5, |
| 7310 | 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, |
| 7311 | 6, 6, 6, 6, 6, 6, |
| 7312 | 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, |
| 7313 | 7, 7, 7, 7, 7, 7, |
| 7314 | 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, |
| 7315 | 8, 8, 8, 8, 8, |
| 7316 | 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, |
| 7317 | 9, 9, 9, 9, 9, 9, |
| 7318 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7319 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7320 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7321 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; |
| 7322 | |
| 7323 | |
| 7324 | // This function works for dates from 1970 to 2099. |
| 7325 | static inline void DateYMDFromTimeAfter1970(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7326 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7327 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7328 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7329 | #endif |
| 7330 | |
| 7331 | year = 1970 + (4 * date + 2) / kDaysIn4Years; |
| 7332 | date %= kDaysIn4Years; |
| 7333 | |
| 7334 | month = kMonthInYear[date]; |
| 7335 | day = kDayInYear[date]; |
| 7336 | |
| 7337 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7338 | } |
| 7339 | |
| 7340 | |
| 7341 | static inline void DateYMDFromTimeSlow(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7342 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7343 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7344 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7345 | #endif |
| 7346 | |
| 7347 | date += kDaysOffset; |
| 7348 | year = 400 * (date / kDaysIn400Years) - kYearsOffset; |
| 7349 | date %= kDaysIn400Years; |
| 7350 | |
| 7351 | ASSERT(MakeDay(year, 0, 1) + date == save_date); |
| 7352 | |
| 7353 | date--; |
| 7354 | int yd1 = date / kDaysIn100Years; |
| 7355 | date %= kDaysIn100Years; |
| 7356 | year += 100 * yd1; |
| 7357 | |
| 7358 | date++; |
| 7359 | int yd2 = date / kDaysIn4Years; |
| 7360 | date %= kDaysIn4Years; |
| 7361 | year += 4 * yd2; |
| 7362 | |
| 7363 | date--; |
| 7364 | int yd3 = date / 365; |
| 7365 | date %= 365; |
| 7366 | year += yd3; |
| 7367 | |
| 7368 | bool is_leap = (!yd1 || yd2) && !yd3; |
| 7369 | |
| 7370 | ASSERT(date >= -1); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7371 | ASSERT(is_leap || (date >= 0)); |
| 7372 | ASSERT((date < 365) || (is_leap && (date < 366))); |
| 7373 | ASSERT(is_leap == ((year % 4 == 0) && (year % 100 || (year % 400 == 0)))); |
| 7374 | ASSERT(is_leap || ((MakeDay(year, 0, 1) + date) == save_date)); |
| 7375 | ASSERT(!is_leap || ((MakeDay(year, 0, 1) + date + 1) == save_date)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7376 | |
| 7377 | if (is_leap) { |
| 7378 | day = kDayInYear[2*365 + 1 + date]; |
| 7379 | month = kMonthInYear[2*365 + 1 + date]; |
| 7380 | } else { |
| 7381 | day = kDayInYear[date]; |
| 7382 | month = kMonthInYear[date]; |
| 7383 | } |
| 7384 | |
| 7385 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7386 | } |
| 7387 | |
| 7388 | |
| 7389 | static inline void DateYMDFromTime(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7390 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7391 | if (date >= 0 && date < 32 * kDaysIn4Years) { |
| 7392 | DateYMDFromTimeAfter1970(date, year, month, day); |
| 7393 | } else { |
| 7394 | DateYMDFromTimeSlow(date, year, month, day); |
| 7395 | } |
| 7396 | } |
| 7397 | |
| 7398 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7399 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateYMDFromTime) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7400 | NoHandleAllocation ha; |
| 7401 | ASSERT(args.length() == 2); |
| 7402 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7403 | CONVERT_DOUBLE_ARG_CHECKED(t, 0); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7404 | CONVERT_CHECKED(JSArray, res_array, args[1]); |
| 7405 | |
| 7406 | int year, month, day; |
| 7407 | DateYMDFromTime(static_cast<int>(floor(t / 86400000)), year, month, day); |
| 7408 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7409 | RUNTIME_ASSERT(res_array->elements()->map() == |
| 7410 | isolate->heap()->fixed_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 7411 | FixedArray* elms = FixedArray::cast(res_array->elements()); |
| 7412 | RUNTIME_ASSERT(elms->length() == 3); |
| 7413 | |
| 7414 | elms->set(0, Smi::FromInt(year)); |
| 7415 | elms->set(1, Smi::FromInt(month)); |
| 7416 | elms->set(2, Smi::FromInt(day)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7417 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7418 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7419 | } |
| 7420 | |
| 7421 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7422 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewArgumentsFast) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7423 | HandleScope scope(isolate); |
| 7424 | ASSERT(args.length() == 3); |
| 7425 | |
| 7426 | Handle<JSFunction> callee = args.at<JSFunction>(0); |
| 7427 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7428 | const int argument_count = Smi::cast(args[2])->value(); |
| 7429 | |
| 7430 | Handle<JSObject> result = |
| 7431 | isolate->factory()->NewArgumentsObject(callee, argument_count); |
| 7432 | // Allocate the elements if needed. |
| 7433 | int parameter_count = callee->shared()->formal_parameter_count(); |
| 7434 | if (argument_count > 0) { |
| 7435 | if (parameter_count > 0) { |
| 7436 | int mapped_count = Min(argument_count, parameter_count); |
| 7437 | Handle<FixedArray> parameter_map = |
| 7438 | isolate->factory()->NewFixedArray(mapped_count + 2, NOT_TENURED); |
| 7439 | parameter_map->set_map( |
| 7440 | isolate->heap()->non_strict_arguments_elements_map()); |
| 7441 | |
| 7442 | Handle<Map> old_map(result->map()); |
| 7443 | Handle<Map> new_map = |
| 7444 | isolate->factory()->CopyMapDropTransitions(old_map); |
| 7445 | new_map->set_elements_kind(JSObject::NON_STRICT_ARGUMENTS_ELEMENTS); |
| 7446 | |
| 7447 | result->set_map(*new_map); |
| 7448 | result->set_elements(*parameter_map); |
| 7449 | |
| 7450 | // Store the context and the arguments array at the beginning of the |
| 7451 | // parameter map. |
| 7452 | Handle<Context> context(isolate->context()); |
| 7453 | Handle<FixedArray> arguments = |
| 7454 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7455 | parameter_map->set(0, *context); |
| 7456 | parameter_map->set(1, *arguments); |
| 7457 | |
| 7458 | // Loop over the actual parameters backwards. |
| 7459 | int index = argument_count - 1; |
| 7460 | while (index >= mapped_count) { |
| 7461 | // These go directly in the arguments array and have no |
| 7462 | // corresponding slot in the parameter map. |
| 7463 | arguments->set(index, *(parameters - index - 1)); |
| 7464 | --index; |
| 7465 | } |
| 7466 | |
| 7467 | ScopeInfo<> scope_info(callee->shared()->scope_info()); |
| 7468 | while (index >= 0) { |
| 7469 | // Detect duplicate names to the right in the parameter list. |
| 7470 | Handle<String> name = scope_info.parameter_name(index); |
| 7471 | int context_slot_count = scope_info.number_of_context_slots(); |
| 7472 | bool duplicate = false; |
| 7473 | for (int j = index + 1; j < parameter_count; ++j) { |
| 7474 | if (scope_info.parameter_name(j).is_identical_to(name)) { |
| 7475 | duplicate = true; |
| 7476 | break; |
| 7477 | } |
| 7478 | } |
| 7479 | |
| 7480 | if (duplicate) { |
| 7481 | // This goes directly in the arguments array with a hole in the |
| 7482 | // parameter map. |
| 7483 | arguments->set(index, *(parameters - index - 1)); |
| 7484 | parameter_map->set_the_hole(index + 2); |
| 7485 | } else { |
| 7486 | // The context index goes in the parameter map with a hole in the |
| 7487 | // arguments array. |
| 7488 | int context_index = -1; |
| 7489 | for (int j = Context::MIN_CONTEXT_SLOTS; |
| 7490 | j < context_slot_count; |
| 7491 | ++j) { |
| 7492 | if (scope_info.context_slot_name(j).is_identical_to(name)) { |
| 7493 | context_index = j; |
| 7494 | break; |
| 7495 | } |
| 7496 | } |
| 7497 | ASSERT(context_index >= 0); |
| 7498 | arguments->set_the_hole(index); |
| 7499 | parameter_map->set(index + 2, Smi::FromInt(context_index)); |
| 7500 | } |
| 7501 | |
| 7502 | --index; |
| 7503 | } |
| 7504 | } else { |
| 7505 | // If there is no aliasing, the arguments object elements are not |
| 7506 | // special in any way. |
| 7507 | Handle<FixedArray> elements = |
| 7508 | isolate->factory()->NewFixedArray(argument_count, NOT_TENURED); |
| 7509 | result->set_elements(*elements); |
| 7510 | for (int i = 0; i < argument_count; ++i) { |
| 7511 | elements->set(i, *(parameters - i - 1)); |
| 7512 | } |
| 7513 | } |
| 7514 | } |
| 7515 | return *result; |
| 7516 | } |
| 7517 | |
| 7518 | |
| 7519 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStrictArgumentsFast) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7520 | NoHandleAllocation ha; |
| 7521 | ASSERT(args.length() == 3); |
| 7522 | |
| 7523 | JSFunction* callee = JSFunction::cast(args[0]); |
| 7524 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7525 | const int length = args.smi_at(2); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7526 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7527 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7528 | { MaybeObject* maybe_result = |
| 7529 | isolate->heap()->AllocateArgumentsObject(callee, length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7530 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7531 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7532 | // Allocate the elements if needed. |
| 7533 | if (length > 0) { |
| 7534 | // Allocate the fixed array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7535 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7536 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7537 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7538 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7539 | |
| 7540 | AssertNoAllocation no_gc; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 7541 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7542 | array->set_map(isolate->heap()->fixed_array_map()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7543 | array->set_length(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7544 | |
| 7545 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7546 | for (int i = 0; i < length; i++) { |
| 7547 | array->set(i, *--parameters, mode); |
| 7548 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7549 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7550 | } |
| 7551 | return result; |
| 7552 | } |
| 7553 | |
| 7554 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7555 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewClosure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7556 | HandleScope scope(isolate); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7557 | ASSERT(args.length() == 3); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 7558 | CONVERT_ARG_CHECKED(Context, context, 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7559 | CONVERT_ARG_CHECKED(SharedFunctionInfo, shared, 1); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7560 | CONVERT_BOOLEAN_CHECKED(pretenure, args[2]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7561 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7562 | // The caller ensures that we pretenure closures that are assigned |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7563 | // directly to properties. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7564 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7565 | Handle<JSFunction> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7566 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 7567 | context, |
| 7568 | pretenure_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7569 | return *result; |
| 7570 | } |
| 7571 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7572 | |
| 7573 | static SmartPointer<Object**> GetNonBoundArguments(int bound_argc, |
| 7574 | int* total_argc) { |
| 7575 | // Find frame containing arguments passed to the caller. |
| 7576 | JavaScriptFrameIterator it; |
| 7577 | JavaScriptFrame* frame = it.frame(); |
| 7578 | List<JSFunction*> functions(2); |
| 7579 | frame->GetFunctions(&functions); |
| 7580 | if (functions.length() > 1) { |
| 7581 | int inlined_frame_index = functions.length() - 1; |
| 7582 | JSFunction* inlined_function = functions[inlined_frame_index]; |
| 7583 | int args_count = inlined_function->shared()->formal_parameter_count(); |
| 7584 | ScopedVector<SlotRef> args_slots(args_count); |
| 7585 | SlotRef::ComputeSlotMappingForArguments(frame, |
| 7586 | inlined_frame_index, |
| 7587 | &args_slots); |
| 7588 | |
| 7589 | *total_argc = bound_argc + args_count; |
| 7590 | SmartPointer<Object**> param_data(NewArray<Object**>(*total_argc)); |
| 7591 | for (int i = 0; i < args_count; i++) { |
| 7592 | Handle<Object> val = args_slots[i].GetValue(); |
| 7593 | param_data[bound_argc + i] = val.location(); |
| 7594 | } |
| 7595 | return param_data; |
| 7596 | } else { |
| 7597 | it.AdvanceToArgumentsFrame(); |
| 7598 | frame = it.frame(); |
| 7599 | int args_count = frame->ComputeParametersCount(); |
| 7600 | |
| 7601 | *total_argc = bound_argc + args_count; |
| 7602 | SmartPointer<Object**> param_data(NewArray<Object**>(*total_argc)); |
| 7603 | for (int i = 0; i < args_count; i++) { |
| 7604 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
| 7605 | param_data[bound_argc + i] = val.location(); |
| 7606 | } |
| 7607 | return param_data; |
| 7608 | } |
| 7609 | } |
| 7610 | |
| 7611 | |
| 7612 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObjectFromBound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7613 | HandleScope scope(isolate); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7614 | ASSERT(args.length() == 2); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7615 | // First argument is a function to use as a constructor. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7616 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7617 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7618 | // Second argument is either null or an array of bound arguments. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7619 | Handle<FixedArray> bound_args; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7620 | int bound_argc = 0; |
| 7621 | if (!args[1]->IsNull()) { |
| 7622 | CONVERT_ARG_CHECKED(JSArray, params, 1); |
| 7623 | RUNTIME_ASSERT(params->HasFastElements()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7624 | bound_args = Handle<FixedArray>(FixedArray::cast(params->elements())); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7625 | bound_argc = Smi::cast(params->length())->value(); |
| 7626 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7627 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7628 | int total_argc = 0; |
| 7629 | SmartPointer<Object**> param_data = |
| 7630 | GetNonBoundArguments(bound_argc, &total_argc); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7631 | for (int i = 0; i < bound_argc; i++) { |
| 7632 | Handle<Object> val = Handle<Object>(bound_args->get(i)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7633 | param_data[i] = val.location(); |
| 7634 | } |
| 7635 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7636 | bool exception = false; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7637 | Handle<Object> result = |
| 7638 | Execution::New(function, total_argc, *param_data, &exception); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7639 | if (exception) { |
| 7640 | return Failure::Exception(); |
| 7641 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7642 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7643 | ASSERT(!result.is_null()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7644 | return *result; |
| 7645 | } |
| 7646 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7647 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7648 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 7649 | Handle<JSFunction> function) { |
| 7650 | Handle<Object> prototype = isolate->factory()->null_value(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7651 | if (function->has_instance_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7652 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7653 | } |
| 7654 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7655 | ConstructStubCompiler compiler; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 7656 | MaybeObject* code = compiler.CompileConstructStub(*function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7657 | if (!code->IsFailure()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7658 | function->shared()->set_construct_stub( |
| 7659 | Code::cast(code->ToObjectUnchecked())); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7660 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7661 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7665 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7666 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7667 | ASSERT(args.length() == 1); |
| 7668 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7669 | Handle<Object> constructor = args.at<Object>(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7670 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7671 | // If the constructor isn't a proper function we throw a type error. |
| 7672 | if (!constructor->IsJSFunction()) { |
| 7673 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7674 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7675 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7676 | return isolate->Throw(*type_error); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7680 | |
| 7681 | // If function should not have prototype, construction is not allowed. In this |
| 7682 | // case generated code bailouts here, since function has no initial_map. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7683 | if (!function->should_have_prototype() && !function->shared()->bound()) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7684 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7685 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7686 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7687 | return isolate->Throw(*type_error); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7688 | } |
| 7689 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7690 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7691 | Debug* debug = isolate->debug(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7692 | // Handle stepping into constructors if step into is active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7693 | if (debug->StepInActive()) { |
| 7694 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7695 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7696 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7697 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7698 | if (function->has_initial_map()) { |
| 7699 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7700 | // The 'Function' function ignores the receiver object when |
| 7701 | // called using 'new' and creates a new JSFunction object that |
| 7702 | // is returned. The receiver object is only used for error |
| 7703 | // reporting if an error occurs when constructing the new |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7704 | // JSFunction. FACTORY->NewJSObject() should not be used to |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7705 | // allocate JSFunctions since it does not properly initialize |
| 7706 | // the shared part of the function. Since the receiver is |
| 7707 | // ignored anyway, we use the global object as the receiver |
| 7708 | // instead of a new JSFunction object. This way, errors are |
| 7709 | // reported the same way whether or not 'Function' is called |
| 7710 | // using 'new'. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7711 | return isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7712 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7715 | // The function should be compiled for the optimization hints to be |
| 7716 | // available. We cannot use EnsureCompiled because that forces a |
| 7717 | // compilation through the shared function info which makes it |
| 7718 | // impossible for us to optimize. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7719 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7720 | if (!function->is_compiled()) CompileLazy(function, CLEAR_EXCEPTION); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7721 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7722 | if (!function->has_initial_map() && |
| 7723 | shared->IsInobjectSlackTrackingInProgress()) { |
| 7724 | // The tracking is already in progress for another function. We can only |
| 7725 | // track one initial_map at a time, so we force the completion before the |
| 7726 | // function is called as a constructor for the first time. |
| 7727 | shared->CompleteInobjectSlackTracking(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7728 | } |
| 7729 | |
| 7730 | bool first_allocation = !shared->live_objects_may_exist(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7731 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 7732 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7733 | // Delay setting the stub if inobject slack tracking is in progress. |
| 7734 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7735 | TrySettingInlineConstructStub(isolate, function); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7736 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7737 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7738 | isolate->counters()->constructed_objects()->Increment(); |
| 7739 | isolate->counters()->constructed_objects_runtime()->Increment(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7740 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7741 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7742 | } |
| 7743 | |
| 7744 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7745 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinalizeInstanceSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7746 | HandleScope scope(isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7747 | ASSERT(args.length() == 1); |
| 7748 | |
| 7749 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7750 | function->shared()->CompleteInobjectSlackTracking(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7751 | TrySettingInlineConstructStub(isolate, function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7752 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7753 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7754 | } |
| 7755 | |
| 7756 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7757 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7758 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7759 | ASSERT(args.length() == 1); |
| 7760 | |
| 7761 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 7762 | #ifdef DEBUG |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7763 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7764 | PrintF("[lazy: "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7765 | function->PrintName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7766 | PrintF("]\n"); |
| 7767 | } |
| 7768 | #endif |
| 7769 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 7770 | // Compile the target function. Here we compile using CompileLazyInLoop in |
| 7771 | // order to get the optimized version. This helps code like delta-blue |
| 7772 | // that calls performance-critical routines through constructors. A |
| 7773 | // constructor call doesn't use a CallIC, it uses a LoadIC followed by a |
| 7774 | // direct call. Since the in-loop tracking takes place through CallICs |
| 7775 | // this means that things called through constructors are never known to |
| 7776 | // be in loops. We compile them as if they are in loops here just in case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7777 | ASSERT(!function->is_compiled()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 7778 | if (!CompileLazyInLoop(function, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7779 | return Failure::Exception(); |
| 7780 | } |
| 7781 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7782 | // All done. Return the compiled code. |
| 7783 | ASSERT(function->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7784 | return function->code(); |
| 7785 | } |
| 7786 | |
| 7787 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7788 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7789 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7790 | ASSERT(args.length() == 1); |
| 7791 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 7792 | // If the function is not optimizable or debugger is active continue using the |
| 7793 | // code from the full compiler. |
| 7794 | if (!function->shared()->code()->optimizable() || |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 7795 | isolate->DebuggerHasBreakPoints()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7796 | if (FLAG_trace_opt) { |
| 7797 | PrintF("[failed to optimize "); |
| 7798 | function->PrintName(); |
| 7799 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 7800 | function->shared()->code()->optimizable() ? "T" : "F", |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 7801 | isolate->DebuggerHasBreakPoints() ? "T" : "F"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7802 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7803 | function->ReplaceCode(function->shared()->code()); |
| 7804 | return function->code(); |
| 7805 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7806 | if (CompileOptimized(function, AstNode::kNoNumber, CLEAR_EXCEPTION)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7807 | return function->code(); |
| 7808 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7809 | if (FLAG_trace_opt) { |
| 7810 | PrintF("[failed to optimize "); |
| 7811 | function->PrintName(); |
| 7812 | PrintF(": optimized compilation failed]\n"); |
| 7813 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7814 | function->ReplaceCode(function->shared()->code()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7815 | return function->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7816 | } |
| 7817 | |
| 7818 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7819 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyDeoptimized) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7820 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7821 | ASSERT(args.length() == 1); |
| 7822 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 7823 | Deoptimizer::BailoutType type = |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7824 | static_cast<Deoptimizer::BailoutType>(args.smi_at(0)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7825 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 7826 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7827 | int frames = deoptimizer->output_count(); |
| 7828 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 7829 | deoptimizer->MaterializeHeapNumbers(); |
| 7830 | delete deoptimizer; |
| 7831 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7832 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7833 | JavaScriptFrame* frame = NULL; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 7834 | for (int i = 0; i < frames - 1; i++) it.Advance(); |
| 7835 | frame = it.frame(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7836 | |
| 7837 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7838 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7839 | Handle<Object> arguments; |
| 7840 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7841 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7842 | if (arguments.is_null()) { |
| 7843 | // FunctionGetArguments can't throw an exception, so cast away the |
| 7844 | // doubt with an assert. |
| 7845 | arguments = Handle<Object>( |
| 7846 | Accessors::FunctionGetArguments(*function, |
| 7847 | NULL)->ToObjectUnchecked()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7848 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 7849 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7850 | } |
| 7851 | frame->SetExpression(i, *arguments); |
| 7852 | } |
| 7853 | } |
| 7854 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7855 | if (type == Deoptimizer::EAGER) { |
| 7856 | RUNTIME_ASSERT(function->IsOptimized()); |
| 7857 | } else { |
| 7858 | RUNTIME_ASSERT(!function->IsOptimized()); |
| 7859 | } |
| 7860 | |
| 7861 | // Avoid doing too much work when running with --always-opt and keep |
| 7862 | // the optimized code around. |
| 7863 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7864 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7865 | } |
| 7866 | |
| 7867 | // Count the number of optimized activations of the function. |
| 7868 | int activations = 0; |
| 7869 | while (!it.done()) { |
| 7870 | JavaScriptFrame* frame = it.frame(); |
| 7871 | if (frame->is_optimized() && frame->function() == *function) { |
| 7872 | activations++; |
| 7873 | } |
| 7874 | it.Advance(); |
| 7875 | } |
| 7876 | |
| 7877 | // TODO(kasperl): For now, we cannot support removing the optimized |
| 7878 | // code when we have recursive invocations of the same function. |
| 7879 | if (activations == 0) { |
| 7880 | if (FLAG_trace_deopt) { |
| 7881 | PrintF("[removing optimized code for: "); |
| 7882 | function->PrintName(); |
| 7883 | PrintF("]\n"); |
| 7884 | } |
| 7885 | function->ReplaceCode(function->shared()->code()); |
| 7886 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7887 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7891 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyOSR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7892 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7893 | delete deoptimizer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7894 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7895 | } |
| 7896 | |
| 7897 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7898 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeoptimizeFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7899 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7900 | ASSERT(args.length() == 1); |
| 7901 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7902 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7903 | |
| 7904 | Deoptimizer::DeoptimizeFunction(*function); |
| 7905 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7906 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7907 | } |
| 7908 | |
| 7909 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 7910 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RunningInSimulator) { |
| 7911 | #if defined(USE_SIMULATOR) |
| 7912 | return isolate->heap()->true_value(); |
| 7913 | #else |
| 7914 | return isolate->heap()->false_value(); |
| 7915 | #endif |
| 7916 | } |
| 7917 | |
| 7918 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 7919 | RUNTIME_FUNCTION(MaybeObject*, Runtime_OptimizeFunctionOnNextCall) { |
| 7920 | HandleScope scope(isolate); |
| 7921 | ASSERT(args.length() == 1); |
| 7922 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7923 | if (!function->IsOptimizable()) return isolate->heap()->undefined_value(); |
| 7924 | function->MarkForLazyRecompilation(); |
| 7925 | return isolate->heap()->undefined_value(); |
| 7926 | } |
| 7927 | |
| 7928 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationStatus) { |
| 7930 | HandleScope scope(isolate); |
| 7931 | ASSERT(args.length() == 1); |
| 7932 | if (!V8::UseCrankshaft()) { |
| 7933 | return Smi::FromInt(4); // 4 == "never". |
| 7934 | } |
| 7935 | if (FLAG_always_opt) { |
| 7936 | return Smi::FromInt(3); // 3 == "always". |
| 7937 | } |
| 7938 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7939 | return function->IsOptimized() ? Smi::FromInt(1) // 1 == "yes". |
| 7940 | : Smi::FromInt(2); // 2 == "no". |
| 7941 | } |
| 7942 | |
| 7943 | |
| 7944 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationCount) { |
| 7945 | HandleScope scope(isolate); |
| 7946 | ASSERT(args.length() == 1); |
| 7947 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7948 | return Smi::FromInt(function->shared()->opt_count()); |
| 7949 | } |
| 7950 | |
| 7951 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7952 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileForOnStackReplacement) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7953 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7954 | ASSERT(args.length() == 1); |
| 7955 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7956 | |
| 7957 | // We're not prepared to handle a function with arguments object. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 7958 | ASSERT(!function->shared()->uses_arguments()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7959 | |
| 7960 | // We have hit a back edge in an unoptimized frame for a function that was |
| 7961 | // selected for on-stack replacement. Find the unoptimized code object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7962 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7963 | // Keep track of whether we've succeeded in optimizing. |
| 7964 | bool succeeded = unoptimized->optimizable(); |
| 7965 | if (succeeded) { |
| 7966 | // If we are trying to do OSR when there are already optimized |
| 7967 | // activations of the function, it means (a) the function is directly or |
| 7968 | // indirectly recursive and (b) an optimized invocation has been |
| 7969 | // deoptimized so that we are currently in an unoptimized activation. |
| 7970 | // Check for optimized activations of this function. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7971 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7972 | while (succeeded && !it.done()) { |
| 7973 | JavaScriptFrame* frame = it.frame(); |
| 7974 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 7975 | it.Advance(); |
| 7976 | } |
| 7977 | } |
| 7978 | |
| 7979 | int ast_id = AstNode::kNoNumber; |
| 7980 | if (succeeded) { |
| 7981 | // The top JS function is this one, the PC is somewhere in the |
| 7982 | // unoptimized code. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7983 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7984 | JavaScriptFrame* frame = it.frame(); |
| 7985 | ASSERT(frame->function() == *function); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7986 | ASSERT(frame->LookupCode() == *unoptimized); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7987 | ASSERT(unoptimized->contains(frame->pc())); |
| 7988 | |
| 7989 | // Use linear search of the unoptimized code's stack check table to find |
| 7990 | // the AST id matching the PC. |
| 7991 | Address start = unoptimized->instruction_start(); |
| 7992 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 7993 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7994 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 7995 | table_cursor += kIntSize; |
| 7996 | for (unsigned i = 0; i < table_length; ++i) { |
| 7997 | // Table entries are (AST id, pc offset) pairs. |
| 7998 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 7999 | if (pc_offset == target_pc_offset) { |
| 8000 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 8001 | break; |
| 8002 | } |
| 8003 | table_cursor += 2 * kIntSize; |
| 8004 | } |
| 8005 | ASSERT(ast_id != AstNode::kNoNumber); |
| 8006 | if (FLAG_trace_osr) { |
| 8007 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 8008 | function->PrintName(); |
| 8009 | PrintF("]\n"); |
| 8010 | } |
| 8011 | |
| 8012 | // Try to compile the optimized code. A true return value from |
| 8013 | // CompileOptimized means that compilation succeeded, not necessarily |
| 8014 | // that optimization succeeded. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8015 | if (CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
| 8016 | function->IsOptimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8017 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 8018 | function->code()->deoptimization_data()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8019 | if (data->OsrPcOffset()->value() >= 0) { |
| 8020 | if (FLAG_trace_osr) { |
| 8021 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8022 | data->OsrPcOffset()->value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8023 | } |
| 8024 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 8025 | } else { |
| 8026 | // We may never generate the desired OSR entry if we emit an |
| 8027 | // early deoptimize. |
| 8028 | succeeded = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8029 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8030 | } else { |
| 8031 | succeeded = false; |
| 8032 | } |
| 8033 | } |
| 8034 | |
| 8035 | // Revert to the original stack checks in the original unoptimized code. |
| 8036 | if (FLAG_trace_osr) { |
| 8037 | PrintF("[restoring original stack checks in "); |
| 8038 | function->PrintName(); |
| 8039 | PrintF("]\n"); |
| 8040 | } |
| 8041 | StackCheckStub check_stub; |
| 8042 | Handle<Code> check_code = check_stub.GetCode(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 8043 | Handle<Code> replacement_code = isolate->builtins()->OnStackReplacement(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8044 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 8045 | *check_code, |
| 8046 | *replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8047 | |
| 8048 | // Allow OSR only at nesting level zero again. |
| 8049 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 8050 | |
| 8051 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 8052 | // smi. This tells the builtin that we need to translate the unoptimized |
| 8053 | // frame to an optimized one. |
| 8054 | if (succeeded) { |
| 8055 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 8056 | return Smi::FromInt(ast_id); |
| 8057 | } else { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8058 | if (function->IsMarkedForLazyRecompilation()) { |
| 8059 | function->ReplaceCode(function->shared()->code()); |
| 8060 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 8061 | return Smi::FromInt(-1); |
| 8062 | } |
| 8063 | } |
| 8064 | |
| 8065 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8066 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8067 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8068 | ASSERT(args.length() == 1); |
| 8069 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8070 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 8071 | } |
| 8072 | |
| 8073 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8074 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructorDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8075 | HandleScope scope(isolate); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 8076 | ASSERT(args.length() == 1); |
| 8077 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 8078 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 8079 | } |
| 8080 | |
| 8081 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8082 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewFunctionContext) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8083 | NoHandleAllocation ha; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8084 | ASSERT(args.length() == 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8085 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8086 | CONVERT_CHECKED(JSFunction, function, args[0]); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 8087 | int length = function->shared()->scope_info()->NumberOfContextSlots(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8088 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8089 | { MaybeObject* maybe_result = |
| 8090 | isolate->heap()->AllocateFunctionContext(length, function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8091 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 8092 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8093 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8094 | isolate->set_context(Context::cast(result)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8095 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 8096 | return result; // non-failure |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8097 | } |
| 8098 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8099 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8100 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushWithContext) { |
| 8101 | NoHandleAllocation ha; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8102 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8103 | JSObject* extension_object; |
| 8104 | if (args[0]->IsJSObject()) { |
| 8105 | extension_object = JSObject::cast(args[0]); |
| 8106 | } else { |
| 8107 | // Convert the object to a proper JavaScript object. |
| 8108 | MaybeObject* maybe_js_object = args[0]->ToObject(); |
| 8109 | if (!maybe_js_object->To(&extension_object)) { |
| 8110 | if (Failure::cast(maybe_js_object)->IsInternalError()) { |
| 8111 | HandleScope scope(isolate); |
| 8112 | Handle<Object> handle = args.at<Object>(0); |
| 8113 | Handle<Object> result = |
| 8114 | isolate->factory()->NewTypeError("with_expression", |
| 8115 | HandleVector(&handle, 1)); |
| 8116 | return isolate->Throw(*result); |
| 8117 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8118 | return maybe_js_object; |
| 8119 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8120 | } |
| 8121 | } |
| 8122 | |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8123 | JSFunction* function; |
| 8124 | if (args[1]->IsSmi()) { |
| 8125 | // A smi sentinel indicates a context nested inside global code rather |
| 8126 | // than some function. There is a canonical empty function that can be |
| 8127 | // gotten from the global context. |
| 8128 | function = isolate->context()->global_context()->closure(); |
| 8129 | } else { |
| 8130 | function = JSFunction::cast(args[1]); |
| 8131 | } |
| 8132 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8133 | Context* context; |
| 8134 | MaybeObject* maybe_context = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8135 | isolate->heap()->AllocateWithContext(function, |
| 8136 | isolate->context(), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8137 | extension_object); |
| 8138 | if (!maybe_context->To(&context)) return maybe_context; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8139 | isolate->set_context(context); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8140 | return context; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8141 | } |
| 8142 | |
| 8143 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8144 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushCatchContext) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8145 | NoHandleAllocation ha; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8146 | ASSERT(args.length() == 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8147 | String* name = String::cast(args[0]); |
| 8148 | Object* thrown_object = args[1]; |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8149 | JSFunction* function; |
| 8150 | if (args[2]->IsSmi()) { |
| 8151 | // A smi sentinel indicates a context nested inside global code rather |
| 8152 | // than some function. There is a canonical empty function that can be |
| 8153 | // gotten from the global context. |
| 8154 | function = isolate->context()->global_context()->closure(); |
| 8155 | } else { |
| 8156 | function = JSFunction::cast(args[2]); |
| 8157 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8158 | Context* context; |
| 8159 | MaybeObject* maybe_context = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 8160 | isolate->heap()->AllocateCatchContext(function, |
| 8161 | isolate->context(), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8162 | name, |
| 8163 | thrown_object); |
| 8164 | if (!maybe_context->To(&context)) return maybe_context; |
| 8165 | isolate->set_context(context); |
| 8166 | return context; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8167 | } |
| 8168 | |
| 8169 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8170 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8171 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8172 | ASSERT(args.length() == 2); |
| 8173 | |
| 8174 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 8175 | CONVERT_ARG_CHECKED(String, name, 1); |
| 8176 | |
| 8177 | int index; |
| 8178 | PropertyAttributes attributes; |
| 8179 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8180 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8181 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8182 | // If the slot was not found the result is true. |
| 8183 | if (holder.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8184 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8185 | } |
| 8186 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8187 | // If the slot was found in a context, it should be DONT_DELETE. |
| 8188 | if (holder->IsContext()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8189 | return isolate->heap()->false_value(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8190 | } |
| 8191 | |
| 8192 | // The slot was found in a JSObject, either a context extension object, |
| 8193 | // the global object, or an arguments object. Try to delete it |
| 8194 | // (respecting DONT_DELETE). For consistency with V8's usual behavior, |
| 8195 | // which allows deleting all parameters in functions that mention |
| 8196 | // 'arguments', we do this even for the case of slots found on an |
| 8197 | // arguments object. The slot was found on an arguments object if the |
| 8198 | // index is non-negative. |
| 8199 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 8200 | if (index >= 0) { |
| 8201 | return object->DeleteElement(index, JSObject::NORMAL_DELETION); |
| 8202 | } else { |
| 8203 | return object->DeleteProperty(*name, JSObject::NORMAL_DELETION); |
| 8204 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8205 | } |
| 8206 | |
| 8207 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8208 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 8209 | // How this is achieved is calling convention-dependent. |
| 8210 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 8211 | // variants where a 64-bit value is returned in two 32-bit registers |
| 8212 | // (edx:eax on ia32, r1:r0 on ARM). |
| 8213 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 8214 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 8215 | // 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] | 8216 | #ifdef V8_HOST_ARCH_64_BIT |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8217 | struct ObjectPair { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8218 | MaybeObject* x; |
| 8219 | MaybeObject* y; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8220 | }; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8221 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8222 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8223 | ObjectPair result = {x, y}; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8224 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 8225 | // In Win64 they are assigned to a hidden first argument. |
| 8226 | return result; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8227 | } |
| 8228 | #else |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8229 | typedef uint64_t ObjectPair; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8230 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8231 | return reinterpret_cast<uint32_t>(x) | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8232 | (reinterpret_cast<ObjectPair>(y) << 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8233 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 8234 | #endif |
| 8235 | |
| 8236 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8237 | static inline MaybeObject* Unhole(Heap* heap, |
| 8238 | MaybeObject* x, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8239 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8240 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 8241 | USE(attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8242 | return x->IsTheHole() ? heap->undefined_value() : x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8243 | } |
| 8244 | |
| 8245 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8246 | static Object* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 8247 | JSObject* holder) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8248 | ASSERT(!holder->IsGlobalObject()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8249 | Context* top = isolate->context(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8250 | // Get the context extension function. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8251 | JSFunction* context_extension_function = |
| 8252 | top->global_context()->context_extension_function(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8253 | // If the holder isn't a context extension object, we just return it |
| 8254 | // as the receiver. This allows arguments objects to be used as |
| 8255 | // receivers, but only if they are put in the context scope chain |
| 8256 | // explicitly via a with-statement. |
| 8257 | Object* constructor = holder->map()->constructor(); |
| 8258 | if (constructor != context_extension_function) return holder; |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8259 | // Fall back to using the global object as the implicit receiver if |
| 8260 | // the property turns out to be a local variable allocated in a |
| 8261 | // context extension object - introduced via eval. Implicit global |
| 8262 | // receivers are indicated with the hole value. |
| 8263 | return isolate->heap()->the_hole_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8264 | } |
| 8265 | |
| 8266 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8267 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 8268 | Isolate* isolate, |
| 8269 | bool throw_error) { |
| 8270 | HandleScope scope(isolate); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 8271 | ASSERT_EQ(2, args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8272 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8273 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8274 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8275 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8276 | Handle<Context> context = args.at<Context>(0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8277 | Handle<String> name = args.at<String>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8278 | |
| 8279 | int index; |
| 8280 | PropertyAttributes attributes; |
| 8281 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8282 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8283 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8284 | // If the index is non-negative, the slot has been found in a local |
| 8285 | // variable or a parameter. Read it from the context object or the |
| 8286 | // arguments object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8287 | if (index >= 0) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8288 | // If the "property" we were looking for is a local variable or an |
| 8289 | // argument in a context, the receiver is the global object; see |
| 8290 | // ECMA-262, 3rd., 10.1.6 and 10.2.3. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8291 | // |
| 8292 | // Use the hole as the receiver to signal that the receiver is |
| 8293 | // implicit and that the global receiver should be used. |
| 8294 | Handle<Object> receiver = isolate->factory()->the_hole_value(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8295 | MaybeObject* value = (holder->IsContext()) |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8296 | ? Context::cast(*holder)->get(index) |
| 8297 | : JSObject::cast(*holder)->GetElement(index); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8298 | return MakePair(Unhole(isolate->heap(), value, attributes), *receiver); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8299 | } |
| 8300 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8301 | // If the holder is found, we read the property from it. |
| 8302 | if (!holder.is_null() && holder->IsJSObject()) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8303 | ASSERT(Handle<JSObject>::cast(holder)->HasProperty(*name)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8304 | JSObject* object = JSObject::cast(*holder); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8305 | Object* receiver; |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8306 | if (object->IsGlobalObject()) { |
| 8307 | receiver = GlobalObject::cast(object)->global_receiver(); |
| 8308 | } else if (context->is_exception_holder(*holder)) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8309 | // Use the hole as the receiver to signal that the receiver is |
| 8310 | // implicit and that the global receiver should be used. |
| 8311 | receiver = isolate->heap()->the_hole_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8312 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8313 | receiver = ComputeReceiverForNonGlobal(isolate, object); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8314 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8315 | |
| 8316 | // GetProperty below can cause GC. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8317 | Handle<Object> receiver_handle(receiver); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8318 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8319 | // No need to unhole the value here. This is taken care of by the |
| 8320 | // GetProperty function. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8321 | MaybeObject* value = object->GetProperty(*name); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 8322 | return MakePair(value, *receiver_handle); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
| 8325 | if (throw_error) { |
| 8326 | // The property doesn't exist - throw exception. |
| 8327 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8328 | isolate->factory()->NewReferenceError("not_defined", |
| 8329 | HandleVector(&name, 1)); |
| 8330 | return MakePair(isolate->Throw(*reference_error), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8331 | } else { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8332 | // The property doesn't exist - return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8333 | return MakePair(isolate->heap()->undefined_value(), |
| 8334 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8335 | } |
| 8336 | } |
| 8337 | |
| 8338 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8339 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8340 | return LoadContextSlotHelper(args, isolate, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8341 | } |
| 8342 | |
| 8343 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8344 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlotNoReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8345 | return LoadContextSlotHelper(args, isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
| 8348 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8349 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8350 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8351 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8352 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8353 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8354 | CONVERT_ARG_CHECKED(Context, context, 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8355 | CONVERT_ARG_CHECKED(String, name, 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8356 | CONVERT_SMI_ARG_CHECKED(strict_unchecked, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8357 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 8358 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8359 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8360 | |
| 8361 | int index; |
| 8362 | PropertyAttributes attributes; |
| 8363 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8364 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8365 | |
| 8366 | if (index >= 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8367 | if (holder->IsContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8368 | // Ignore if read_only variable. |
| 8369 | if ((attributes & READ_ONLY) == 0) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8370 | // Context is a fixed array and set cannot fail. |
| 8371 | Context::cast(*holder)->set(index, *value); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8372 | } else if (strict_mode == kStrictMode) { |
| 8373 | // Setting read only property in strict mode. |
| 8374 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8375 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 8376 | HandleVector(&name, 1)); |
| 8377 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8378 | } |
| 8379 | } else { |
| 8380 | ASSERT((attributes & READ_ONLY) == 0); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8381 | Handle<Object> result = |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8382 | SetElement(Handle<JSObject>::cast(holder), index, value, strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8383 | if (result.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8384 | ASSERT(isolate->has_pending_exception()); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8385 | return Failure::Exception(); |
| 8386 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8387 | } |
| 8388 | return *value; |
| 8389 | } |
| 8390 | |
| 8391 | // Slow case: The property is not in a FixedArray context. |
| 8392 | // It is either in an JSObject extension context or it was not found. |
| 8393 | Handle<JSObject> context_ext; |
| 8394 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8395 | if (!holder.is_null()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8396 | // The property exists in the extension context. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8397 | context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8398 | } else { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8399 | // The property was not found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8400 | ASSERT(attributes == ABSENT); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8401 | |
| 8402 | if (strict_mode == kStrictMode) { |
| 8403 | // Throw in strict mode (assignment to undefined variable). |
| 8404 | Handle<Object> error = |
| 8405 | isolate->factory()->NewReferenceError( |
| 8406 | "not_defined", HandleVector(&name, 1)); |
| 8407 | return isolate->Throw(*error); |
| 8408 | } |
| 8409 | // In non-strict mode, the property is stored in the global context. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8410 | attributes = NONE; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8411 | context_ext = Handle<JSObject>(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8412 | } |
| 8413 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8414 | // Set the property, but ignore if read_only variable on the context |
| 8415 | // extension object itself. |
| 8416 | if ((attributes & READ_ONLY) == 0 || |
| 8417 | (context_ext->GetLocalPropertyAttribute(*name) == ABSENT)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8418 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8419 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8420 | SetProperty(context_ext, name, value, NONE, strict_mode)); |
| 8421 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8422 | // Setting read only property in strict mode. |
| 8423 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8424 | isolate->factory()->NewTypeError( |
| 8425 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 8426 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8427 | } |
| 8428 | return *value; |
| 8429 | } |
| 8430 | |
| 8431 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8432 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Throw) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8433 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8434 | ASSERT(args.length() == 1); |
| 8435 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8436 | return isolate->Throw(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8437 | } |
| 8438 | |
| 8439 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8440 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ReThrow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8441 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8442 | ASSERT(args.length() == 1); |
| 8443 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8444 | return isolate->ReThrow(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8445 | } |
| 8446 | |
| 8447 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8448 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PromoteScheduledException) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8449 | ASSERT_EQ(0, args.length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8450 | return isolate->PromoteScheduledException(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8451 | } |
| 8452 | |
| 8453 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8454 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8455 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8456 | ASSERT(args.length() == 1); |
| 8457 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8458 | Handle<Object> name(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8459 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8460 | isolate->factory()->NewReferenceError("not_defined", |
| 8461 | HandleVector(&name, 1)); |
| 8462 | return isolate->Throw(*reference_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
| 8465 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8466 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 8467 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8468 | |
| 8469 | // First check if this is a real stack overflow. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8470 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 8471 | NoHandleAllocation na; |
| 8472 | return isolate->StackOverflow(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 8473 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8474 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8475 | return Execution::HandleStackGuardInterrupt(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | |
| 8479 | // NOTE: These PrintXXX functions are defined for all builds (not just |
| 8480 | // DEBUG builds) because we may want to be able to trace function |
| 8481 | // calls in all modes. |
| 8482 | static void PrintString(String* str) { |
| 8483 | // not uncommon to have empty strings |
| 8484 | if (str->length() > 0) { |
| 8485 | SmartPointer<char> s = |
| 8486 | str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 8487 | PrintF("%s", *s); |
| 8488 | } |
| 8489 | } |
| 8490 | |
| 8491 | |
| 8492 | static void PrintObject(Object* obj) { |
| 8493 | if (obj->IsSmi()) { |
| 8494 | PrintF("%d", Smi::cast(obj)->value()); |
| 8495 | } else if (obj->IsString() || obj->IsSymbol()) { |
| 8496 | PrintString(String::cast(obj)); |
| 8497 | } else if (obj->IsNumber()) { |
| 8498 | PrintF("%g", obj->Number()); |
| 8499 | } else if (obj->IsFailure()) { |
| 8500 | PrintF("<failure>"); |
| 8501 | } else if (obj->IsUndefined()) { |
| 8502 | PrintF("<undefined>"); |
| 8503 | } else if (obj->IsNull()) { |
| 8504 | PrintF("<null>"); |
| 8505 | } else if (obj->IsTrue()) { |
| 8506 | PrintF("<true>"); |
| 8507 | } else if (obj->IsFalse()) { |
| 8508 | PrintF("<false>"); |
| 8509 | } else { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 8510 | PrintF("%p", reinterpret_cast<void*>(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8511 | } |
| 8512 | } |
| 8513 | |
| 8514 | |
| 8515 | static int StackSize() { |
| 8516 | int n = 0; |
| 8517 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 8518 | return n; |
| 8519 | } |
| 8520 | |
| 8521 | |
| 8522 | static void PrintTransition(Object* result) { |
| 8523 | // indentation |
| 8524 | { const int nmax = 80; |
| 8525 | int n = StackSize(); |
| 8526 | if (n <= nmax) |
| 8527 | PrintF("%4d:%*s", n, n, ""); |
| 8528 | else |
| 8529 | PrintF("%4d:%*s", n, nmax, "..."); |
| 8530 | } |
| 8531 | |
| 8532 | if (result == NULL) { |
| 8533 | // constructor calls |
| 8534 | JavaScriptFrameIterator it; |
| 8535 | JavaScriptFrame* frame = it.frame(); |
| 8536 | if (frame->IsConstructor()) PrintF("new "); |
| 8537 | // function name |
| 8538 | Object* fun = frame->function(); |
| 8539 | if (fun->IsJSFunction()) { |
| 8540 | PrintObject(JSFunction::cast(fun)->shared()->name()); |
| 8541 | } else { |
| 8542 | PrintObject(fun); |
| 8543 | } |
| 8544 | // function arguments |
| 8545 | // (we are intentionally only printing the actually |
| 8546 | // supplied parameters, not all parameters required) |
| 8547 | PrintF("(this="); |
| 8548 | PrintObject(frame->receiver()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8549 | const int length = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8550 | for (int i = 0; i < length; i++) { |
| 8551 | PrintF(", "); |
| 8552 | PrintObject(frame->GetParameter(i)); |
| 8553 | } |
| 8554 | PrintF(") {\n"); |
| 8555 | |
| 8556 | } else { |
| 8557 | // function result |
| 8558 | PrintF("} -> "); |
| 8559 | PrintObject(result); |
| 8560 | PrintF("\n"); |
| 8561 | } |
| 8562 | } |
| 8563 | |
| 8564 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8565 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceEnter) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8566 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8567 | NoHandleAllocation ha; |
| 8568 | PrintTransition(NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8569 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8570 | } |
| 8571 | |
| 8572 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8573 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceExit) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8574 | NoHandleAllocation ha; |
| 8575 | PrintTransition(args[0]); |
| 8576 | return args[0]; // return TOS |
| 8577 | } |
| 8578 | |
| 8579 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8580 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8581 | NoHandleAllocation ha; |
| 8582 | ASSERT(args.length() == 1); |
| 8583 | |
| 8584 | #ifdef DEBUG |
| 8585 | if (args[0]->IsString()) { |
| 8586 | // If we have a string, assume it's a code "marker" |
| 8587 | // and print some interesting cpu debugging info. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8588 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8589 | JavaScriptFrame* frame = it.frame(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8590 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 8591 | frame->fp(), frame->sp(), frame->caller_sp()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8592 | } else { |
| 8593 | PrintF("DebugPrint: "); |
| 8594 | } |
| 8595 | args[0]->Print(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8596 | if (args[0]->IsHeapObject()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8597 | PrintF("\n"); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8598 | HeapObject::cast(args[0])->map()->Print(); |
| 8599 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8600 | #else |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8601 | // ShortPrint is available in release mode. Print is not. |
| 8602 | args[0]->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8603 | #endif |
| 8604 | PrintF("\n"); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8605 | Flush(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8606 | |
| 8607 | return args[0]; // return TOS |
| 8608 | } |
| 8609 | |
| 8610 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8611 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugTrace) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8612 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8613 | NoHandleAllocation ha; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8614 | isolate->PrintStack(); |
| 8615 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8616 | } |
| 8617 | |
| 8618 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8619 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateCurrentTime) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8620 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 8621 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8622 | |
| 8623 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 8624 | // the number in a Date object representing a particular instant in |
| 8625 | // time is milliseconds. Therefore, we floor the result of getting |
| 8626 | // the OS time. |
| 8627 | double millis = floor(OS::TimeCurrentMillis()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8628 | return isolate->heap()->NumberFromDouble(millis); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8629 | } |
| 8630 | |
| 8631 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8632 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateParseString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8633 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8634 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8635 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8636 | CONVERT_ARG_CHECKED(String, str, 0); |
| 8637 | FlattenString(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8638 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8639 | CONVERT_ARG_CHECKED(JSArray, output, 1); |
| 8640 | RUNTIME_ASSERT(output->HasFastElements()); |
| 8641 | |
| 8642 | AssertNoAllocation no_allocation; |
| 8643 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8644 | FixedArray* output_array = FixedArray::cast(output->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8645 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 8646 | bool result; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 8647 | if (str->IsAsciiRepresentation()) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8648 | result = DateParser::Parse(str->ToAsciiVector(), |
| 8649 | output_array, |
| 8650 | isolate->unicode_cache()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8651 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 8652 | ASSERT(str->IsTwoByteRepresentation()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8653 | result = DateParser::Parse(str->ToUC16Vector(), |
| 8654 | output_array, |
| 8655 | isolate->unicode_cache()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8656 | } |
| 8657 | |
| 8658 | if (result) { |
| 8659 | return *output; |
| 8660 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8661 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8662 | } |
| 8663 | } |
| 8664 | |
| 8665 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8666 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimezone) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8667 | NoHandleAllocation ha; |
| 8668 | ASSERT(args.length() == 1); |
| 8669 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8670 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 8671 | const char* zone = OS::LocalTimezone(x); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8672 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8676 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimeOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8677 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 8678 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8679 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8680 | return isolate->heap()->NumberFromDouble(OS::LocalTimeOffset()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8681 | } |
| 8682 | |
| 8683 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8684 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateDaylightSavingsOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8685 | NoHandleAllocation ha; |
| 8686 | ASSERT(args.length() == 1); |
| 8687 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8688 | CONVERT_DOUBLE_ARG_CHECKED(x, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8689 | return isolate->heap()->NumberFromDouble(OS::DaylightSavingsOffset(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8690 | } |
| 8691 | |
| 8692 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8693 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalReceiver) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8694 | ASSERT(args.length() == 1); |
| 8695 | Object* global = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8696 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8697 | return JSGlobalObject::cast(global)->global_receiver(); |
| 8698 | } |
| 8699 | |
| 8700 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8701 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8702 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8703 | ASSERT_EQ(1, args.length()); |
| 8704 | CONVERT_ARG_CHECKED(String, source, 0); |
| 8705 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 8706 | source = Handle<String>(source->TryFlattenGetString()); |
| 8707 | // Optimized fast case where we only have ascii characters. |
| 8708 | Handle<Object> result; |
| 8709 | if (source->IsSeqAsciiString()) { |
| 8710 | result = JsonParser<true>::Parse(source); |
| 8711 | } else { |
| 8712 | result = JsonParser<false>::Parse(source); |
| 8713 | } |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8714 | if (result.is_null()) { |
| 8715 | // Syntax error or stack overflow in scanner. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8716 | ASSERT(isolate->has_pending_exception()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8717 | return Failure::Exception(); |
| 8718 | } |
| 8719 | return *result; |
| 8720 | } |
| 8721 | |
| 8722 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8723 | bool CodeGenerationFromStringsAllowed(Isolate* isolate, |
| 8724 | Handle<Context> context) { |
| 8725 | if (context->allow_code_gen_from_strings()->IsFalse()) { |
| 8726 | // Check with callback if set. |
| 8727 | AllowCodeGenerationFromStringsCallback callback = |
| 8728 | isolate->allow_code_gen_callback(); |
| 8729 | if (callback == NULL) { |
| 8730 | // No callback set and code generation disallowed. |
| 8731 | return false; |
| 8732 | } else { |
| 8733 | // Callback set. Let it decide if code generation is allowed. |
| 8734 | VMState state(isolate, EXTERNAL); |
| 8735 | return callback(v8::Utils::ToLocal(context)); |
| 8736 | } |
| 8737 | } |
| 8738 | return true; |
| 8739 | } |
| 8740 | |
| 8741 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8742 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8743 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8744 | ASSERT_EQ(1, args.length()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 8745 | CONVERT_ARG_CHECKED(String, source, 0); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8746 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8747 | // Extract global context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8748 | Handle<Context> context(isolate->context()->global_context()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8749 | |
| 8750 | // Check if global context allows code generation from |
| 8751 | // strings. Throw an exception if it doesn't. |
| 8752 | if (!CodeGenerationFromStringsAllowed(isolate, context)) { |
| 8753 | return isolate->Throw(*isolate->factory()->NewError( |
| 8754 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 8755 | } |
| 8756 | |
| 8757 | // Compile source string in the global context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8758 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval(source, |
| 8759 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8760 | true, |
| 8761 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8762 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8763 | Handle<JSFunction> fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8764 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 8765 | context, |
| 8766 | NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8767 | return *fun; |
| 8768 | } |
| 8769 | |
| 8770 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8771 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 8772 | Handle<String> source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8773 | Handle<Object> receiver, |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 8774 | StrictModeFlag strict_mode) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8775 | Handle<Context> context = Handle<Context>(isolate->context()); |
| 8776 | Handle<Context> global_context = Handle<Context>(context->global_context()); |
| 8777 | |
| 8778 | // Check if global context allows code generation from |
| 8779 | // strings. Throw an exception if it doesn't. |
| 8780 | if (!CodeGenerationFromStringsAllowed(isolate, global_context)) { |
| 8781 | isolate->Throw(*isolate->factory()->NewError( |
| 8782 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 8783 | return MakePair(Failure::Exception(), NULL); |
| 8784 | } |
| 8785 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8786 | // Deal with a normal eval call with a string argument. Compile it |
| 8787 | // and return the compiled function bound in the local context. |
| 8788 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 8789 | source, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8790 | Handle<Context>(isolate->context()), |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8791 | context->IsGlobalContext(), |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 8792 | strict_mode); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8793 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8794 | Handle<JSFunction> compiled = |
| 8795 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8796 | shared, context, NOT_TENURED); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8797 | return MakePair(*compiled, *receiver); |
| 8798 | } |
| 8799 | |
| 8800 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8801 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEval) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8802 | ASSERT(args.length() == 4); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8803 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8804 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8805 | Handle<Object> callee = args.at<Object>(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8806 | Handle<Object> receiver; // Will be overwritten. |
| 8807 | |
| 8808 | // Compute the calling context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8809 | Handle<Context> context = Handle<Context>(isolate->context(), isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8810 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8811 | // Make sure Isolate::context() agrees with the old code that traversed |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8812 | // the stack frames to compute the context. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8813 | StackFrameLocator locator; |
| 8814 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8815 | ASSERT(Context::cast(frame->context()) == *context); |
| 8816 | #endif |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8817 | |
| 8818 | // Find where the 'eval' symbol is bound. It is unaliased only if |
| 8819 | // it is bound in the global context. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8820 | int index = -1; |
| 8821 | PropertyAttributes attributes = ABSENT; |
| 8822 | while (true) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8823 | receiver = context->Lookup(isolate->factory()->eval_symbol(), |
| 8824 | FOLLOW_PROTOTYPE_CHAIN, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8825 | &index, &attributes); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8826 | // Stop search when eval is found or when the global context is |
| 8827 | // reached. |
| 8828 | if (attributes != ABSENT || context->IsGlobalContext()) break; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8829 | context = Handle<Context>(context->previous(), isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8832 | // If eval could not be resolved, it has been deleted and we need to |
| 8833 | // throw a reference error. |
| 8834 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8835 | Handle<Object> name = isolate->factory()->eval_symbol(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8836 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8837 | isolate->factory()->NewReferenceError("not_defined", |
| 8838 | HandleVector(&name, 1)); |
| 8839 | return MakePair(isolate->Throw(*reference_error), NULL); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8840 | } |
| 8841 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8842 | if (!context->IsGlobalContext()) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8843 | // 'eval' is not bound in the global context. Just call the function |
| 8844 | // with the given arguments. This is not necessarily the global eval. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 8845 | if (receiver->IsContext() || receiver->IsJSContextExtensionObject()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8846 | receiver = isolate->factory()->the_hole_value(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8847 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8848 | return MakePair(*callee, *receiver); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8849 | } |
| 8850 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8851 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 8852 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8853 | if (*callee != isolate->global_context()->global_eval_fun() || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8854 | !args[1]->IsString()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8855 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8856 | } |
| 8857 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8858 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8859 | return CompileGlobalEval(isolate, |
| 8860 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8861 | args.at<Object>(2), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8862 | static_cast<StrictModeFlag>(args.smi_at(3))); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8863 | } |
| 8864 | |
| 8865 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8866 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEvalNoLookup) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8867 | ASSERT(args.length() == 4); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8868 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8869 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8870 | Handle<Object> callee = args.at<Object>(0); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8871 | |
| 8872 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 8873 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8874 | if (*callee != isolate->global_context()->global_eval_fun() || |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8875 | !args[1]->IsString()) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 8876 | return MakePair(*callee, isolate->heap()->the_hole_value()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8877 | } |
| 8878 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8879 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8880 | return CompileGlobalEval(isolate, |
| 8881 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8882 | args.at<Object>(2), |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 8883 | static_cast<StrictModeFlag>(args.smi_at(3))); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8884 | } |
| 8885 | |
| 8886 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8887 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNewFunctionAttributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8888 | // This utility adjusts the property attributes for newly created Function |
| 8889 | // object ("new Function(...)") by changing the map. |
| 8890 | // All it does is changing the prototype property to enumerable |
| 8891 | // as specified in ECMA262, 15.3.5.2. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8892 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8893 | ASSERT(args.length() == 1); |
| 8894 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8895 | |
| 8896 | Handle<Map> map = func->shared()->strict_mode() |
| 8897 | ? isolate->strict_mode_function_instance_map() |
| 8898 | : isolate->function_instance_map(); |
| 8899 | |
| 8900 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 8901 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 8902 | func->set_map(*map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8903 | return *func; |
| 8904 | } |
| 8905 | |
| 8906 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8907 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateInNewSpace) { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8908 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 8909 | // Use as fallback for allocation in generated code when NewSpace |
| 8910 | // is full. |
| 8911 | ASSERT(args.length() == 1); |
| 8912 | CONVERT_ARG_CHECKED(Smi, size_smi, 0); |
| 8913 | int size = size_smi->value(); |
| 8914 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 8915 | RUNTIME_ASSERT(size > 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8916 | Heap* heap = isolate->heap(); |
| 8917 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8918 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8919 | Object* allocation; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8920 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8921 | if (maybe_allocation->ToObject(&allocation)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8922 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8923 | } |
| 8924 | return maybe_allocation; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8925 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8926 | } |
| 8927 | |
| 8928 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8929 | // 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] | 8930 | // array. Returns true if the element was pushed on the stack and |
| 8931 | // false otherwise. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8932 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushIfAbsent) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8933 | ASSERT(args.length() == 2); |
| 8934 | CONVERT_CHECKED(JSArray, array, args[0]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8935 | CONVERT_CHECKED(JSObject, element, args[1]); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8936 | RUNTIME_ASSERT(array->HasFastElements()); |
| 8937 | int length = Smi::cast(array->length())->value(); |
| 8938 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 8939 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8940 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8941 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8942 | Object* obj; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8943 | // Strict not needed. Used for cycle detection in Array join implementation. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 8944 | { MaybeObject* maybe_obj = |
| 8945 | array->SetFastElement(length, element, kNonStrictMode, true); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8946 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8947 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8948 | return isolate->heap()->true_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
| 8951 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8952 | /** |
| 8953 | * A simple visitor visits every element of Array's. |
| 8954 | * The backend storage can be a fixed array for fast elements case, |
| 8955 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 8956 | * of FixedArray, the class can be used by both fast and slow cases. |
| 8957 | * The second parameter of the constructor, fast_elements, specifies |
| 8958 | * whether the storage is a FixedArray or Dictionary. |
| 8959 | * |
| 8960 | * An index limit is used to deal with the situation that a result array |
| 8961 | * length overflows 32-bit non-negative integer. |
| 8962 | */ |
| 8963 | class ArrayConcatVisitor { |
| 8964 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8965 | ArrayConcatVisitor(Isolate* isolate, |
| 8966 | Handle<FixedArray> storage, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8967 | bool fast_elements) : |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8968 | isolate_(isolate), |
| 8969 | storage_(Handle<FixedArray>::cast( |
| 8970 | isolate->global_handles()->Create(*storage))), |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8971 | index_offset_(0u), |
| 8972 | fast_elements_(fast_elements) { } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8973 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8974 | ~ArrayConcatVisitor() { |
| 8975 | clear_storage(); |
| 8976 | } |
| 8977 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8978 | void visit(uint32_t i, Handle<Object> elm) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8979 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8980 | uint32_t index = index_offset_ + i; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8981 | |
| 8982 | if (fast_elements_) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8983 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 8984 | storage_->set(index, *elm); |
| 8985 | return; |
| 8986 | } |
| 8987 | // Our initial estimate of length was foiled, possibly by |
| 8988 | // getters on the arrays increasing the length of later arrays |
| 8989 | // during iteration. |
| 8990 | // This shouldn't happen in anything but pathological cases. |
| 8991 | SetDictionaryMode(index); |
| 8992 | // Fall-through to dictionary mode. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8993 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8994 | ASSERT(!fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8995 | Handle<NumberDictionary> dict(NumberDictionary::cast(*storage_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8996 | Handle<NumberDictionary> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8997 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8998 | if (!result.is_identical_to(dict)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8999 | // Dictionary needed to grow. |
| 9000 | clear_storage(); |
| 9001 | set_storage(*result); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9002 | } |
| 9003 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9004 | |
| 9005 | void increase_index_offset(uint32_t delta) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9006 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 9007 | index_offset_ = JSObject::kMaxElementCount; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 9008 | } else { |
| 9009 | index_offset_ += delta; |
| 9010 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9011 | } |
| 9012 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9013 | Handle<JSArray> ToArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9014 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9015 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9016 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9017 | Handle<Map> map; |
| 9018 | if (fast_elements_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9019 | map = isolate_->factory()->GetFastElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9020 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9021 | map = isolate_->factory()->GetSlowElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9022 | } |
| 9023 | array->set_map(*map); |
| 9024 | array->set_length(*length); |
| 9025 | array->set_elements(*storage_); |
| 9026 | return array; |
| 9027 | } |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 9028 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9029 | private: |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9030 | // Convert storage to dictionary mode. |
| 9031 | void SetDictionaryMode(uint32_t index) { |
| 9032 | ASSERT(fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9033 | Handle<FixedArray> current_storage(*storage_); |
| 9034 | Handle<NumberDictionary> slow_storage( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9035 | isolate_->factory()->NewNumberDictionary(current_storage->length())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9036 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 9037 | for (uint32_t i = 0; i < current_length; i++) { |
| 9038 | HandleScope loop_scope; |
| 9039 | Handle<Object> element(current_storage->get(i)); |
| 9040 | if (!element->IsTheHole()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9041 | Handle<NumberDictionary> new_storage = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9042 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9043 | if (!new_storage.is_identical_to(slow_storage)) { |
| 9044 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 9045 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9046 | } |
| 9047 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9048 | clear_storage(); |
| 9049 | set_storage(*slow_storage); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9050 | fast_elements_ = false; |
| 9051 | } |
| 9052 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9053 | inline void clear_storage() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9054 | isolate_->global_handles()->Destroy( |
| 9055 | Handle<Object>::cast(storage_).location()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9056 | } |
| 9057 | |
| 9058 | inline void set_storage(FixedArray* storage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9059 | storage_ = Handle<FixedArray>::cast( |
| 9060 | isolate_->global_handles()->Create(storage)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9061 | } |
| 9062 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9063 | Isolate* isolate_; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9064 | Handle<FixedArray> storage_; // Always a global handle. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9065 | // Index after last seen index. Always less than or equal to |
| 9066 | // JSObject::kMaxElementCount. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9067 | uint32_t index_offset_; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9068 | bool fast_elements_; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9069 | }; |
| 9070 | |
| 9071 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9072 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 9073 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 9074 | int element_count = 0; |
| 9075 | switch (array->GetElementsKind()) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9076 | case JSObject::FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9077 | // Fast elements can't have lengths that are not representable by |
| 9078 | // a 32-bit signed integer. |
| 9079 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 9080 | int fast_length = static_cast<int>(length); |
| 9081 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 9082 | for (int i = 0; i < fast_length; i++) { |
| 9083 | if (!elements->get(i)->IsTheHole()) element_count++; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9084 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 9085 | break; |
| 9086 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9087 | case JSObject::DICTIONARY_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9088 | Handle<NumberDictionary> dictionary( |
| 9089 | NumberDictionary::cast(array->elements())); |
| 9090 | int capacity = dictionary->Capacity(); |
| 9091 | for (int i = 0; i < capacity; i++) { |
| 9092 | Handle<Object> key(dictionary->KeyAt(i)); |
| 9093 | if (dictionary->IsKey(*key)) { |
| 9094 | element_count++; |
| 9095 | } |
| 9096 | } |
| 9097 | break; |
| 9098 | } |
| 9099 | default: |
| 9100 | // External arrays are always dense. |
| 9101 | return length; |
| 9102 | } |
| 9103 | // As an estimate, we assume that the prototype doesn't contain any |
| 9104 | // inherited elements. |
| 9105 | return element_count; |
| 9106 | } |
| 9107 | |
| 9108 | |
| 9109 | |
| 9110 | template<class ExternalArrayClass, class ElementType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9111 | static void IterateExternalArrayElements(Isolate* isolate, |
| 9112 | Handle<JSObject> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9113 | bool elements_are_ints, |
| 9114 | bool elements_are_guaranteed_smis, |
| 9115 | ArrayConcatVisitor* visitor) { |
| 9116 | Handle<ExternalArrayClass> array( |
| 9117 | ExternalArrayClass::cast(receiver->elements())); |
| 9118 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 9119 | |
| 9120 | ASSERT(visitor != NULL); |
| 9121 | if (elements_are_ints) { |
| 9122 | if (elements_are_guaranteed_smis) { |
| 9123 | for (uint32_t j = 0; j < len; j++) { |
| 9124 | HandleScope loop_scope; |
| 9125 | Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get(j)))); |
| 9126 | visitor->visit(j, e); |
| 9127 | } |
| 9128 | } else { |
| 9129 | for (uint32_t j = 0; j < len; j++) { |
| 9130 | HandleScope loop_scope; |
| 9131 | int64_t val = static_cast<int64_t>(array->get(j)); |
| 9132 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 9133 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 9134 | visitor->visit(j, e); |
| 9135 | } else { |
| 9136 | Handle<Object> e = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9137 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9138 | visitor->visit(j, e); |
| 9139 | } |
| 9140 | } |
| 9141 | } |
| 9142 | } else { |
| 9143 | for (uint32_t j = 0; j < len; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9144 | HandleScope loop_scope(isolate); |
| 9145 | Handle<Object> e = isolate->factory()->NewNumber(array->get(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9146 | visitor->visit(j, e); |
| 9147 | } |
| 9148 | } |
| 9149 | } |
| 9150 | |
| 9151 | |
| 9152 | // Used for sorting indices in a List<uint32_t>. |
| 9153 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 9154 | uint32_t a = *ap; |
| 9155 | uint32_t b = *bp; |
| 9156 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 9157 | } |
| 9158 | |
| 9159 | |
| 9160 | static void CollectElementIndices(Handle<JSObject> object, |
| 9161 | uint32_t range, |
| 9162 | List<uint32_t>* indices) { |
| 9163 | JSObject::ElementsKind kind = object->GetElementsKind(); |
| 9164 | switch (kind) { |
| 9165 | case JSObject::FAST_ELEMENTS: { |
| 9166 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 9167 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 9168 | if (range < length) length = range; |
| 9169 | for (uint32_t i = 0; i < length; i++) { |
| 9170 | if (!elements->get(i)->IsTheHole()) { |
| 9171 | indices->Add(i); |
| 9172 | } |
| 9173 | } |
| 9174 | break; |
| 9175 | } |
| 9176 | case JSObject::DICTIONARY_ELEMENTS: { |
| 9177 | Handle<NumberDictionary> dict(NumberDictionary::cast(object->elements())); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9178 | uint32_t capacity = dict->Capacity(); |
| 9179 | for (uint32_t j = 0; j < capacity; j++) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9180 | HandleScope loop_scope; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9181 | Handle<Object> k(dict->KeyAt(j)); |
| 9182 | if (dict->IsKey(*k)) { |
| 9183 | ASSERT(k->IsNumber()); |
| 9184 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 9185 | if (index < range) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9186 | indices->Add(index); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9187 | } |
| 9188 | } |
| 9189 | } |
| 9190 | break; |
| 9191 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9192 | default: { |
| 9193 | int dense_elements_length; |
| 9194 | switch (kind) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9195 | case JSObject::EXTERNAL_PIXEL_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9196 | dense_elements_length = |
| 9197 | ExternalPixelArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9198 | break; |
| 9199 | } |
| 9200 | case JSObject::EXTERNAL_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9201 | dense_elements_length = |
| 9202 | ExternalByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9203 | break; |
| 9204 | } |
| 9205 | case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9206 | dense_elements_length = |
| 9207 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9208 | break; |
| 9209 | } |
| 9210 | case JSObject::EXTERNAL_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9211 | dense_elements_length = |
| 9212 | ExternalShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9213 | break; |
| 9214 | } |
| 9215 | case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9216 | dense_elements_length = |
| 9217 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9218 | break; |
| 9219 | } |
| 9220 | case JSObject::EXTERNAL_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9221 | dense_elements_length = |
| 9222 | ExternalIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9223 | break; |
| 9224 | } |
| 9225 | case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9226 | dense_elements_length = |
| 9227 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9228 | break; |
| 9229 | } |
| 9230 | case JSObject::EXTERNAL_FLOAT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9231 | dense_elements_length = |
| 9232 | ExternalFloatArray::cast(object->elements())->length(); |
| 9233 | break; |
| 9234 | } |
| 9235 | case JSObject::EXTERNAL_DOUBLE_ELEMENTS: { |
| 9236 | dense_elements_length = |
| 9237 | ExternalDoubleArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9238 | break; |
| 9239 | } |
| 9240 | default: |
| 9241 | UNREACHABLE(); |
| 9242 | dense_elements_length = 0; |
| 9243 | break; |
| 9244 | } |
| 9245 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 9246 | if (range <= length) { |
| 9247 | length = range; |
| 9248 | // We will add all indices, so we might as well clear it first |
| 9249 | // and avoid duplicates. |
| 9250 | indices->Clear(); |
| 9251 | } |
| 9252 | for (uint32_t i = 0; i < length; i++) { |
| 9253 | indices->Add(i); |
| 9254 | } |
| 9255 | if (length == range) return; // All indices accounted for already. |
| 9256 | break; |
| 9257 | } |
| 9258 | } |
| 9259 | |
| 9260 | Handle<Object> prototype(object->GetPrototype()); |
| 9261 | if (prototype->IsJSObject()) { |
| 9262 | // The prototype will usually have no inherited element indices, |
| 9263 | // but we have to check. |
| 9264 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 9265 | } |
| 9266 | } |
| 9267 | |
| 9268 | |
| 9269 | /** |
| 9270 | * A helper function that visits elements of a JSArray in numerical |
| 9271 | * order. |
| 9272 | * |
| 9273 | * The visitor argument called for each existing element in the array |
| 9274 | * with the element index and the element's value. |
| 9275 | * Afterwards it increments the base-index of the visitor by the array |
| 9276 | * length. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9277 | * Returns false if any access threw an exception, otherwise true. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9278 | */ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9279 | static bool IterateElements(Isolate* isolate, |
| 9280 | Handle<JSArray> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9281 | ArrayConcatVisitor* visitor) { |
| 9282 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 9283 | switch (receiver->GetElementsKind()) { |
| 9284 | case JSObject::FAST_ELEMENTS: { |
| 9285 | // Run through the elements FixedArray and use HasElement and GetElement |
| 9286 | // to check the prototype for missing elements. |
| 9287 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 9288 | int fast_length = static_cast<int>(length); |
| 9289 | ASSERT(fast_length <= elements->length()); |
| 9290 | for (int j = 0; j < fast_length; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9291 | HandleScope loop_scope(isolate); |
| 9292 | Handle<Object> element_value(elements->get(j), isolate); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9293 | if (!element_value->IsTheHole()) { |
| 9294 | visitor->visit(j, element_value); |
| 9295 | } else if (receiver->HasElement(j)) { |
| 9296 | // Call GetElement on receiver, not its prototype, or getters won't |
| 9297 | // have the correct receiver. |
| 9298 | element_value = GetElement(receiver, j); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9299 | if (element_value.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9300 | visitor->visit(j, element_value); |
| 9301 | } |
| 9302 | } |
| 9303 | break; |
| 9304 | } |
| 9305 | case JSObject::DICTIONARY_ELEMENTS: { |
| 9306 | Handle<NumberDictionary> dict(receiver->element_dictionary()); |
| 9307 | List<uint32_t> indices(dict->Capacity() / 2); |
| 9308 | // Collect all indices in the object and the prototypes less |
| 9309 | // than length. This might introduce duplicates in the indices list. |
| 9310 | CollectElementIndices(receiver, length, &indices); |
| 9311 | indices.Sort(&compareUInt32); |
| 9312 | int j = 0; |
| 9313 | int n = indices.length(); |
| 9314 | while (j < n) { |
| 9315 | HandleScope loop_scope; |
| 9316 | uint32_t index = indices[j]; |
| 9317 | Handle<Object> element = GetElement(receiver, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9318 | if (element.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9319 | visitor->visit(index, element); |
| 9320 | // Skip to next different index (i.e., omit duplicates). |
| 9321 | do { |
| 9322 | j++; |
| 9323 | } while (j < n && indices[j] == index); |
| 9324 | } |
| 9325 | break; |
| 9326 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9327 | case JSObject::EXTERNAL_PIXEL_ELEMENTS: { |
| 9328 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 9329 | receiver->elements())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9330 | for (uint32_t j = 0; j < length; j++) { |
| 9331 | Handle<Smi> e(Smi::FromInt(pixels->get(j))); |
| 9332 | visitor->visit(j, e); |
| 9333 | } |
| 9334 | break; |
| 9335 | } |
| 9336 | case JSObject::EXTERNAL_BYTE_ELEMENTS: { |
| 9337 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9338 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9339 | break; |
| 9340 | } |
| 9341 | case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 9342 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9343 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9344 | break; |
| 9345 | } |
| 9346 | case JSObject::EXTERNAL_SHORT_ELEMENTS: { |
| 9347 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9348 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9349 | break; |
| 9350 | } |
| 9351 | case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 9352 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9353 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9354 | break; |
| 9355 | } |
| 9356 | case JSObject::EXTERNAL_INT_ELEMENTS: { |
| 9357 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9358 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9359 | break; |
| 9360 | } |
| 9361 | case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 9362 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9363 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9364 | break; |
| 9365 | } |
| 9366 | case JSObject::EXTERNAL_FLOAT_ELEMENTS: { |
| 9367 | IterateExternalArrayElements<ExternalFloatArray, float>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9368 | isolate, receiver, false, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9369 | break; |
| 9370 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9371 | case JSObject::EXTERNAL_DOUBLE_ELEMENTS: { |
| 9372 | IterateExternalArrayElements<ExternalDoubleArray, double>( |
| 9373 | isolate, receiver, false, false, visitor); |
| 9374 | break; |
| 9375 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9376 | default: |
| 9377 | UNREACHABLE(); |
| 9378 | break; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9379 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9380 | visitor->increase_index_offset(length); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9381 | return true; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9382 | } |
| 9383 | |
| 9384 | |
| 9385 | /** |
| 9386 | * Array::concat implementation. |
| 9387 | * See ECMAScript 262, 15.4.4.4. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9388 | * 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] | 9389 | * following the ECMAScript 5 specification. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9390 | */ |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9391 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayConcat) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9392 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9393 | HandleScope handle_scope(isolate); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9394 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9395 | CONVERT_ARG_CHECKED(JSArray, arguments, 0); |
| 9396 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 9397 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 9398 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9399 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9400 | // Pass 1: estimate the length and number of elements of the result. |
| 9401 | // The actual length can be larger if any of the arguments have getters |
| 9402 | // that mutate other arguments (but will otherwise be precise). |
| 9403 | // 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] | 9404 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9405 | uint32_t estimate_result_length = 0; |
| 9406 | uint32_t estimate_nof_elements = 0; |
| 9407 | { |
| 9408 | for (int i = 0; i < argument_count; i++) { |
| 9409 | HandleScope loop_scope; |
| 9410 | Handle<Object> obj(elements->get(i)); |
| 9411 | uint32_t length_estimate; |
| 9412 | uint32_t element_estimate; |
| 9413 | if (obj->IsJSArray()) { |
| 9414 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
| 9415 | length_estimate = |
| 9416 | static_cast<uint32_t>(array->length()->Number()); |
| 9417 | element_estimate = |
| 9418 | EstimateElementCount(array); |
| 9419 | } else { |
| 9420 | length_estimate = 1; |
| 9421 | element_estimate = 1; |
| 9422 | } |
| 9423 | // Avoid overflows by capping at kMaxElementCount. |
| 9424 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 9425 | length_estimate) { |
| 9426 | estimate_result_length = JSObject::kMaxElementCount; |
| 9427 | } else { |
| 9428 | estimate_result_length += length_estimate; |
| 9429 | } |
| 9430 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 9431 | element_estimate) { |
| 9432 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 9433 | } else { |
| 9434 | estimate_nof_elements += element_estimate; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9435 | } |
| 9436 | } |
| 9437 | } |
| 9438 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9439 | // If estimated number of elements is more than half of length, a |
| 9440 | // fixed array (fast case) is more time and space-efficient than a |
| 9441 | // dictionary. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9442 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9443 | |
| 9444 | Handle<FixedArray> storage; |
| 9445 | if (fast_case) { |
| 9446 | // The backing storage array must have non-existing elements to |
| 9447 | // preserve holes across concat operations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9448 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 9449 | estimate_result_length); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9450 | } else { |
| 9451 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 9452 | uint32_t at_least_space_for = estimate_nof_elements + |
| 9453 | (estimate_nof_elements >> 2); |
| 9454 | storage = Handle<FixedArray>::cast( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9455 | isolate->factory()->NewNumberDictionary(at_least_space_for)); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9456 | } |
| 9457 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9458 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9459 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9460 | for (int i = 0; i < argument_count; i++) { |
| 9461 | Handle<Object> obj(elements->get(i)); |
| 9462 | if (obj->IsJSArray()) { |
| 9463 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9464 | if (!IterateElements(isolate, array, &visitor)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9465 | return Failure::Exception(); |
| 9466 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9467 | } else { |
| 9468 | visitor.visit(0, obj); |
| 9469 | visitor.increase_index_offset(1); |
| 9470 | } |
| 9471 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9472 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9473 | return *visitor.ToArray(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9474 | } |
| 9475 | |
| 9476 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9477 | // This will not allocate (flatten the string), but it may run |
| 9478 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9479 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9480 | NoHandleAllocation ha; |
| 9481 | ASSERT(args.length() == 1); |
| 9482 | |
| 9483 | CONVERT_CHECKED(String, string, args[0]); |
| 9484 | StringInputBuffer buffer(string); |
| 9485 | while (buffer.has_more()) { |
| 9486 | uint16_t character = buffer.GetNext(); |
| 9487 | PrintF("%c", character); |
| 9488 | } |
| 9489 | return string; |
| 9490 | } |
| 9491 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9492 | // Moves all own elements of an object, that are below a limit, to positions |
| 9493 | // starting at zero. All undefined values are placed after non-undefined values, |
| 9494 | // and are followed by non-existing element. Does not change the length |
| 9495 | // property. |
| 9496 | // Returns the number of non-undefined elements collected. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9497 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RemoveArrayHoles) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9498 | ASSERT(args.length() == 2); |
| 9499 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9500 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 9501 | return object->PrepareElementsForSort(limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9502 | } |
| 9503 | |
| 9504 | |
| 9505 | // 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] | 9506 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MoveArrayContents) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9507 | ASSERT(args.length() == 2); |
| 9508 | CONVERT_CHECKED(JSArray, from, args[0]); |
| 9509 | CONVERT_CHECKED(JSArray, to, args[1]); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9510 | HeapObject* new_elements = from->elements(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9511 | MaybeObject* maybe_new_map; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9512 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 9513 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9514 | maybe_new_map = to->map()->GetFastElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9515 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9516 | maybe_new_map = to->map()->GetSlowElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9517 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9518 | Object* new_map; |
| 9519 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9520 | to->set_map(Map::cast(new_map)); |
| 9521 | to->set_elements(new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9522 | to->set_length(from->length()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9523 | Object* obj; |
| 9524 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 9525 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9526 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9527 | from->set_length(Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9528 | return to; |
| 9529 | } |
| 9530 | |
| 9531 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9532 | // How many elements does this object/array have? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9533 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EstimateNumberOfElements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9534 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9535 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9536 | HeapObject* elements = object->elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9537 | if (elements->IsDictionary()) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9538 | return Smi::FromInt(NumberDictionary::cast(elements)->NumberOfElements()); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9539 | } else if (object->IsJSArray()) { |
| 9540 | return JSArray::cast(object)->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9541 | } else { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9542 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9543 | } |
| 9544 | } |
| 9545 | |
| 9546 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9547 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SwapElements) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9548 | HandleScope handle_scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9549 | |
| 9550 | ASSERT_EQ(3, args.length()); |
| 9551 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9552 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9553 | Handle<Object> key1 = args.at<Object>(1); |
| 9554 | Handle<Object> key2 = args.at<Object>(2); |
| 9555 | |
| 9556 | uint32_t index1, index2; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9557 | if (!key1->ToArrayIndex(&index1) |
| 9558 | || !key2->ToArrayIndex(&index2)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9559 | return isolate->ThrowIllegalOperation(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9560 | } |
| 9561 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9562 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
| 9563 | Handle<Object> tmp1 = GetElement(jsobject, index1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9564 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9565 | Handle<Object> tmp2 = GetElement(jsobject, index2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9566 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9567 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9568 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 9569 | SetElement(jsobject, index1, tmp2, kStrictMode)); |
| 9570 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 9571 | SetElement(jsobject, index2, tmp1, kStrictMode)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9572 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9573 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9574 | } |
| 9575 | |
| 9576 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9577 | // 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] | 9578 | // might have elements. Can either return keys (positive integers) or |
| 9579 | // intervals (pair of a negative integer (-start-1) followed by a |
| 9580 | // positive (length)) or undefined values. |
| 9581 | // 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] | 9582 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArrayKeys) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9583 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9584 | HandleScope scope(isolate); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9585 | CONVERT_ARG_CHECKED(JSObject, array, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9586 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9587 | if (array->elements()->IsDictionary()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9588 | // Create an array and get all the keys into it, then remove all the |
| 9589 | // keys that are not integers in the range 0 to length-1. |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9590 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(array, INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9591 | int keys_length = keys->length(); |
| 9592 | for (int i = 0; i < keys_length; i++) { |
| 9593 | Object* key = keys->get(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9594 | uint32_t index = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9595 | if (!key->ToArrayIndex(&index) || index >= length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9596 | // Zap invalid keys. |
| 9597 | keys->set_undefined(i); |
| 9598 | } |
| 9599 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9600 | return *isolate->factory()->NewJSArrayWithElements(keys); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9601 | } else { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9602 | ASSERT(array->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9603 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9604 | // -1 means start of array. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9605 | single_interval->set(0, Smi::FromInt(-1)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9606 | uint32_t actual_length = |
| 9607 | static_cast<uint32_t>(FixedArray::cast(array->elements())->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9608 | uint32_t min_length = actual_length < length ? actual_length : length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9609 | Handle<Object> length_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9610 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9611 | single_interval->set(1, *length_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9612 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9613 | } |
| 9614 | } |
| 9615 | |
| 9616 | |
| 9617 | // DefineAccessor takes an optional final argument which is the |
| 9618 | // property attributes (eg, DONT_ENUM, DONT_DELETE). IMPORTANT: due |
| 9619 | // to the way accessors are implemented, it is set for both the getter |
| 9620 | // and setter on the first call to DefineAccessor and ignored on |
| 9621 | // subsequent calls. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9622 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9623 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
| 9624 | // Compute attributes. |
| 9625 | PropertyAttributes attributes = NONE; |
| 9626 | if (args.length() == 5) { |
| 9627 | CONVERT_CHECKED(Smi, attrs, args[4]); |
| 9628 | int value = attrs->value(); |
| 9629 | // Only attribute bits should be set. |
| 9630 | ASSERT((value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 9631 | attributes = static_cast<PropertyAttributes>(value); |
| 9632 | } |
| 9633 | |
| 9634 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 9635 | CONVERT_CHECKED(String, name, args[1]); |
| 9636 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 9637 | CONVERT_CHECKED(JSFunction, fun, args[3]); |
| 9638 | return obj->DefineAccessor(name, flag->value() == 0, fun, attributes); |
| 9639 | } |
| 9640 | |
| 9641 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9642 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LookupAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9643 | ASSERT(args.length() == 3); |
| 9644 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 9645 | CONVERT_CHECKED(String, name, args[1]); |
| 9646 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 9647 | return obj->LookupAccessor(name, flag->value() == 0); |
| 9648 | } |
| 9649 | |
| 9650 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9651 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9652 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugBreak) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9653 | ASSERT(args.length() == 0); |
| 9654 | return Execution::DebugBreakHelper(); |
| 9655 | } |
| 9656 | |
| 9657 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9658 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 9659 | static Smi* WrapFrameId(StackFrame::Id id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 9660 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9661 | return Smi::FromInt(id >> 2); |
| 9662 | } |
| 9663 | |
| 9664 | |
| 9665 | static StackFrame::Id UnwrapFrameId(Smi* wrapped) { |
| 9666 | return static_cast<StackFrame::Id>(wrapped->value() << 2); |
| 9667 | } |
| 9668 | |
| 9669 | |
| 9670 | // Adds a JavaScript function as a debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9671 | // args[0]: debug event listener function to set or null or undefined for |
| 9672 | // clearing the event listener function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9673 | // args[1]: object supplied during callback |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9674 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDebugEventListener) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9675 | ASSERT(args.length() == 2); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9676 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 9677 | args[0]->IsUndefined() || |
| 9678 | args[0]->IsNull()); |
| 9679 | Handle<Object> callback = args.at<Object>(0); |
| 9680 | Handle<Object> data = args.at<Object>(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9681 | isolate->debugger()->SetEventListener(callback, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9682 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9683 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9684 | } |
| 9685 | |
| 9686 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9687 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Break) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9688 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9689 | isolate->stack_guard()->DebugBreak(); |
| 9690 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
| 9693 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9694 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 9695 | Object* receiver, |
| 9696 | String* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9697 | LookupResult* result, |
| 9698 | bool* caught_exception) { |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9699 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9700 | switch (result->type()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9701 | case NORMAL: |
| 9702 | value = result->holder()->GetNormalizedProperty(result); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9703 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9704 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9705 | } |
| 9706 | return value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9707 | case FIELD: |
| 9708 | value = |
| 9709 | JSObject::cast( |
| 9710 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 9711 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9712 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9713 | } |
| 9714 | return value; |
| 9715 | case CONSTANT_FUNCTION: |
| 9716 | return result->GetConstantFunction(); |
| 9717 | case CALLBACKS: { |
| 9718 | Object* structure = result->GetCallbackObject(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 9719 | if (structure->IsForeign() || structure->IsAccessorInfo()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9720 | MaybeObject* maybe_value = receiver->GetPropertyWithCallback( |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9721 | receiver, structure, name, result->holder()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9722 | if (!maybe_value->ToObject(&value)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9723 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9724 | ASSERT(maybe_value->IsException()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9725 | maybe_value = heap->isolate()->pending_exception(); |
| 9726 | heap->isolate()->clear_pending_exception(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9727 | if (caught_exception != NULL) { |
| 9728 | *caught_exception = true; |
| 9729 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9730 | return maybe_value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9731 | } |
| 9732 | return value; |
| 9733 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9734 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9735 | } |
| 9736 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9737 | case INTERCEPTOR: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9738 | case MAP_TRANSITION: |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9739 | case EXTERNAL_ARRAY_TRANSITION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9740 | case CONSTANT_TRANSITION: |
| 9741 | case NULL_DESCRIPTOR: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9742 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9743 | default: |
| 9744 | UNREACHABLE(); |
| 9745 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9746 | UNREACHABLE(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9747 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9748 | } |
| 9749 | |
| 9750 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 9751 | // Get debugger related details for an object property. |
| 9752 | // args[0]: object holding property |
| 9753 | // args[1]: name of the property |
| 9754 | // |
| 9755 | // The array returned contains the following information: |
| 9756 | // 0: Property value |
| 9757 | // 1: Property details |
| 9758 | // 2: Property value is exception |
| 9759 | // 3: Getter function if defined |
| 9760 | // 4: Setter function if defined |
| 9761 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 9762 | // defined through __defineGetter__ and/or __defineSetter__. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9763 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPropertyDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9764 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9765 | |
| 9766 | ASSERT(args.length() == 2); |
| 9767 | |
| 9768 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9769 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9770 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9771 | // Make sure to set the current context to the context before the debugger was |
| 9772 | // entered (if the debugger is entered). The reason for switching context here |
| 9773 | // is that for some property lookups (accessors and interceptors) callbacks |
| 9774 | // into the embedding application can occour, and the embedding application |
| 9775 | // could have the assumption that its own global context is the current |
| 9776 | // context and not some internal debugger context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9777 | SaveContext save(isolate); |
| 9778 | if (isolate->debug()->InDebugger()) { |
| 9779 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9780 | } |
| 9781 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9782 | // Skip the global proxy as it has no properties and always delegates to the |
| 9783 | // real global object. |
| 9784 | if (obj->IsJSGlobalProxy()) { |
| 9785 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 9786 | } |
| 9787 | |
| 9788 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9789 | // Check if the name is trivially convertible to an index and get the element |
| 9790 | // if so. |
| 9791 | uint32_t index; |
| 9792 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9793 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9794 | Object* element_or_char; |
| 9795 | { MaybeObject* maybe_element_or_char = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9796 | Runtime::GetElementOrCharAt(isolate, obj, index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9797 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 9798 | return maybe_element_or_char; |
| 9799 | } |
| 9800 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9801 | details->set(0, element_or_char); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9802 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9803 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9804 | } |
| 9805 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9806 | // Find the number of objects making up this. |
| 9807 | int length = LocalPrototypeChainLength(*obj); |
| 9808 | |
| 9809 | // Try local lookup on each of the objects. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9810 | Handle<JSObject> jsproto = obj; |
| 9811 | for (int i = 0; i < length; i++) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9812 | LookupResult result; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9813 | jsproto->LocalLookup(*name, &result); |
| 9814 | if (result.IsProperty()) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9815 | // LookupResult is not GC safe as it holds raw object pointers. |
| 9816 | // GC can happen later in this code so put the required fields into |
| 9817 | // local variables using handles when required for later use. |
| 9818 | PropertyType result_type = result.type(); |
| 9819 | Handle<Object> result_callback_obj; |
| 9820 | if (result_type == CALLBACKS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9821 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 9822 | isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9823 | } |
| 9824 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 9825 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 9826 | // to be copied to handles before this. |
| 9827 | bool caught_exception = false; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9828 | Object* raw_value; |
| 9829 | { MaybeObject* maybe_raw_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9830 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 9831 | &result, &caught_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9832 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 9833 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9834 | Handle<Object> value(raw_value, isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9835 | |
| 9836 | // If the callback object is a fixed array then it contains JavaScript |
| 9837 | // getter and/or setter. |
| 9838 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
| 9839 | result_callback_obj->IsFixedArray(); |
| 9840 | Handle<FixedArray> details = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9841 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9842 | details->set(0, *value); |
| 9843 | details->set(1, property_details); |
| 9844 | if (hasJavaScriptAccessors) { |
| 9845 | details->set(2, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9846 | caught_exception ? isolate->heap()->true_value() |
| 9847 | : isolate->heap()->false_value()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9848 | details->set(3, FixedArray::cast(*result_callback_obj)->get(0)); |
| 9849 | details->set(4, FixedArray::cast(*result_callback_obj)->get(1)); |
| 9850 | } |
| 9851 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9852 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9853 | } |
| 9854 | if (i < length - 1) { |
| 9855 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 9856 | } |
| 9857 | } |
| 9858 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9859 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9860 | } |
| 9861 | |
| 9862 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9863 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetProperty) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9864 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9865 | |
| 9866 | ASSERT(args.length() == 2); |
| 9867 | |
| 9868 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9869 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9870 | |
| 9871 | LookupResult result; |
| 9872 | obj->Lookup(*name, &result); |
| 9873 | if (result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9874 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9875 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9876 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9877 | } |
| 9878 | |
| 9879 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9880 | // Return the property type calculated from the property details. |
| 9881 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9882 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyTypeFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9883 | ASSERT(args.length() == 1); |
| 9884 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9885 | PropertyType type = PropertyDetails(details).type(); |
| 9886 | return Smi::FromInt(static_cast<int>(type)); |
| 9887 | } |
| 9888 | |
| 9889 | |
| 9890 | // Return the property attribute calculated from the property details. |
| 9891 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9892 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyAttributesFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9893 | ASSERT(args.length() == 1); |
| 9894 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9895 | PropertyAttributes attributes = PropertyDetails(details).attributes(); |
| 9896 | return Smi::FromInt(static_cast<int>(attributes)); |
| 9897 | } |
| 9898 | |
| 9899 | |
| 9900 | // Return the property insertion index calculated from the property details. |
| 9901 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9902 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyIndexFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9903 | ASSERT(args.length() == 1); |
| 9904 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9905 | int index = PropertyDetails(details).index(); |
| 9906 | return Smi::FromInt(index); |
| 9907 | } |
| 9908 | |
| 9909 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9910 | // Return property value from named interceptor. |
| 9911 | // args[0]: object |
| 9912 | // args[1]: property name |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9913 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugNamedInterceptorPropertyValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9914 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9915 | ASSERT(args.length() == 2); |
| 9916 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9917 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
| 9918 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9919 | |
| 9920 | PropertyAttributes attributes; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9921 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9922 | } |
| 9923 | |
| 9924 | |
| 9925 | // Return element value from indexed interceptor. |
| 9926 | // args[0]: object |
| 9927 | // args[1]: index |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9928 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugIndexedInterceptorElementValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9929 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9930 | ASSERT(args.length() == 2); |
| 9931 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9932 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 9933 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 9934 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9935 | return obj->GetElementWithInterceptor(*obj, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9936 | } |
| 9937 | |
| 9938 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9939 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckExecutionState) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9940 | ASSERT(args.length() >= 1); |
| 9941 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9942 | // Check that the break id is valid. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9943 | if (isolate->debug()->break_id() == 0 || |
| 9944 | break_id != isolate->debug()->break_id()) { |
| 9945 | return isolate->Throw( |
| 9946 | isolate->heap()->illegal_execution_state_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9947 | } |
| 9948 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9949 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9950 | } |
| 9951 | |
| 9952 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9953 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9954 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9955 | ASSERT(args.length() == 1); |
| 9956 | |
| 9957 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9958 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9959 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 9960 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9961 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 9962 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9963 | |
| 9964 | // Count all frames which are relevant to debugging stack trace. |
| 9965 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9966 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9967 | if (id == StackFrame::NO_ID) { |
| 9968 | // If there is no JavaScript stack frame count is 0. |
| 9969 | return Smi::FromInt(0); |
| 9970 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 9971 | |
| 9972 | for (JavaScriptFrameIterator it(isolate, id); !it.done(); it.Advance()) { |
| 9973 | n += it.frame()->GetInlineCount(); |
| 9974 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9975 | return Smi::FromInt(n); |
| 9976 | } |
| 9977 | |
| 9978 | |
| 9979 | static const int kFrameDetailsFrameIdIndex = 0; |
| 9980 | static const int kFrameDetailsReceiverIndex = 1; |
| 9981 | static const int kFrameDetailsFunctionIndex = 2; |
| 9982 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 9983 | static const int kFrameDetailsLocalCountIndex = 4; |
| 9984 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 9985 | static const int kFrameDetailsConstructCallIndex = 6; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9986 | static const int kFrameDetailsAtReturnIndex = 7; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 9987 | static const int kFrameDetailsFlagsIndex = 8; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9988 | static const int kFrameDetailsFirstDynamicIndex = 9; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9989 | |
| 9990 | // Return an array with frame details |
| 9991 | // args[0]: number: break id |
| 9992 | // args[1]: number: frame index |
| 9993 | // |
| 9994 | // The array returned contains the following information: |
| 9995 | // 0: Frame id |
| 9996 | // 1: Receiver |
| 9997 | // 2: Function |
| 9998 | // 3: Argument count |
| 9999 | // 4: Local count |
| 10000 | // 5: Source position |
| 10001 | // 6: Constructor call |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10002 | // 7: Is at return |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10003 | // 8: Flags |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10004 | // Arguments name, value |
| 10005 | // Locals name, value |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10006 | // Return value if any |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10007 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10008 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10009 | ASSERT(args.length() == 2); |
| 10010 | |
| 10011 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10012 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10013 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10014 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10015 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10016 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10017 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10018 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10019 | |
| 10020 | // Find the relevant frame with the requested index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10021 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10022 | if (id == StackFrame::NO_ID) { |
| 10023 | // If there are no JavaScript stack frames return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10024 | return heap->undefined_value(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 10025 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10026 | |
| 10027 | int deoptimized_frame_index = -1; // Frame index in optimized frame. |
| 10028 | DeoptimizedFrameInfo* deoptimized_frame = NULL; |
| 10029 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10030 | int count = 0; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10031 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10032 | for (; !it.done(); it.Advance()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10033 | if (index < count + it.frame()->GetInlineCount()) break; |
| 10034 | count += it.frame()->GetInlineCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10035 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10036 | if (it.done()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10037 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10038 | if (it.frame()->is_optimized()) { |
| 10039 | deoptimized_frame_index = |
| 10040 | it.frame()->GetInlineCount() - (index - count) - 1; |
| 10041 | deoptimized_frame = Deoptimizer::DebuggerInspectableFrame( |
| 10042 | it.frame(), |
| 10043 | deoptimized_frame_index, |
| 10044 | isolate); |
| 10045 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10046 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10047 | // Traverse the saved contexts chain to find the active context for the |
| 10048 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10049 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10050 | while (save != NULL && !save->below(it.frame())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10051 | save = save->prev(); |
| 10052 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10053 | ASSERT(save != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10054 | |
| 10055 | // Get the frame id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10056 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10057 | |
| 10058 | // Find source position. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10059 | int position = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10060 | it.frame()->LookupCode()->SourcePosition(it.frame()->pc()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10061 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10062 | // Check for constructor frame. Inlined frames cannot be construct calls. |
| 10063 | bool inlined_frame = |
| 10064 | it.frame()->is_optimized() && deoptimized_frame_index != 0; |
| 10065 | bool constructor = !inlined_frame && it.frame()->IsConstructor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10066 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10067 | // Get scope info and read from it for local variable information. |
| 10068 | Handle<JSFunction> function(JSFunction::cast(it.frame()->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10069 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10070 | ASSERT(*scope_info != SerializedScopeInfo::Empty()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10071 | ScopeInfo<> info(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10072 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10073 | // Get the locals names and values into a temporary array. |
| 10074 | // |
| 10075 | // TODO(1240907): Hide compiler-introduced stack variables |
| 10076 | // (e.g. .result)? For users of the debugger, they will probably be |
| 10077 | // confusing. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10078 | Handle<FixedArray> locals = |
| 10079 | isolate->factory()->NewFixedArray(info.NumberOfLocals() * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10080 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10081 | // Fill in the values of the locals. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10082 | int i = 0; |
| 10083 | for (; i < info.number_of_stack_slots(); ++i) { |
| 10084 | // Use the value from the stack. |
| 10085 | locals->set(i * 2, *info.LocalName(i)); |
| 10086 | if (it.frame()->is_optimized()) { |
| 10087 | // Get the value from the deoptimized frame. |
| 10088 | locals->set(i * 2 + 1, |
| 10089 | deoptimized_frame->GetExpression(i)); |
| 10090 | } else { |
| 10091 | // Get the value from the stack. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10092 | locals->set(i * 2 + 1, it.frame()->GetExpression(i)); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10093 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10094 | } |
| 10095 | if (i < info.NumberOfLocals()) { |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 10096 | // Get the context containing declarations. |
| 10097 | Handle<Context> context( |
| 10098 | Context::cast(it.frame()->context())->declaration_context()); |
| 10099 | for (; i < info.NumberOfLocals(); ++i) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10100 | Handle<String> name = info.LocalName(i); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 10101 | locals->set(i * 2, *name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10102 | locals->set(i * 2 + 1, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10103 | context->get(scope_info->ContextSlotIndex(*name, NULL))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10104 | } |
| 10105 | } |
| 10106 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10107 | // Check whether this frame is positioned at return. If not top |
| 10108 | // frame or if the frame is optimized it cannot be at a return. |
| 10109 | bool at_return = false; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10110 | if (!it.frame()->is_optimized() && index == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10111 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10112 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10113 | |
| 10114 | // If positioned just before return find the value to be returned and add it |
| 10115 | // to the frame information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10116 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10117 | if (at_return) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10118 | StackFrameIterator it2(isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10119 | Address internal_frame_sp = NULL; |
| 10120 | while (!it2.done()) { |
| 10121 | if (it2.frame()->is_internal()) { |
| 10122 | internal_frame_sp = it2.frame()->sp(); |
| 10123 | } else { |
| 10124 | if (it2.frame()->is_java_script()) { |
| 10125 | if (it2.frame()->id() == it.frame()->id()) { |
| 10126 | // The internal frame just before the JavaScript frame contains the |
| 10127 | // value to return on top. A debug break at return will create an |
| 10128 | // internal frame to store the return value (eax/rax/r0) before |
| 10129 | // entering the debug break exit frame. |
| 10130 | if (internal_frame_sp != NULL) { |
| 10131 | return_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10132 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 10133 | isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10134 | break; |
| 10135 | } |
| 10136 | } |
| 10137 | } |
| 10138 | |
| 10139 | // Indicate that the previous frame was not an internal frame. |
| 10140 | internal_frame_sp = NULL; |
| 10141 | } |
| 10142 | it2.Advance(); |
| 10143 | } |
| 10144 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10145 | |
| 10146 | // Now advance to the arguments adapter frame (if any). It contains all |
| 10147 | // the provided parameters whereas the function frame always have the number |
| 10148 | // of arguments matching the functions parameters. The rest of the |
| 10149 | // information (except for what is collected above) is the same. |
| 10150 | it.AdvanceToArgumentsFrame(); |
| 10151 | |
| 10152 | // Find the number of arguments to fill. At least fill the number of |
| 10153 | // parameters for the function and fill more if more parameters are provided. |
| 10154 | int argument_count = info.number_of_parameters(); |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10155 | if (it.frame()->is_optimized()) { |
| 10156 | ASSERT_EQ(argument_count, deoptimized_frame->parameters_count()); |
| 10157 | } else { |
| 10158 | if (argument_count < it.frame()->ComputeParametersCount()) { |
| 10159 | argument_count = it.frame()->ComputeParametersCount(); |
| 10160 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10161 | } |
| 10162 | |
| 10163 | // Calculate the size of the result. |
| 10164 | int details_size = kFrameDetailsFirstDynamicIndex + |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10165 | 2 * (argument_count + info.NumberOfLocals()) + |
| 10166 | (at_return ? 1 : 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10167 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10168 | |
| 10169 | // Add the frame id. |
| 10170 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 10171 | |
| 10172 | // Add the function (same as in function frame). |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10173 | if (it.frame()->is_optimized()) { |
| 10174 | // Get the function from the deoptimized frame. |
| 10175 | details->set(kFrameDetailsFunctionIndex, deoptimized_frame->GetFunction()); |
| 10176 | } else { |
| 10177 | // Get the function from the stack. |
| 10178 | details->set(kFrameDetailsFunctionIndex, it.frame()->function()); |
| 10179 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10180 | |
| 10181 | // Add the arguments count. |
| 10182 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 10183 | |
| 10184 | // Add the locals count |
| 10185 | details->set(kFrameDetailsLocalCountIndex, |
| 10186 | Smi::FromInt(info.NumberOfLocals())); |
| 10187 | |
| 10188 | // Add the source position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10189 | if (position != RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10190 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 10191 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10192 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10193 | } |
| 10194 | |
| 10195 | // Add the constructor information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10196 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10197 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10198 | // Add the at return information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10199 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10200 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10201 | // Add flags to indicate information on whether this frame is |
| 10202 | // bit 0: invoked in the debugger context. |
| 10203 | // bit 1: optimized frame. |
| 10204 | // bit 2: inlined in optimized frame |
| 10205 | int flags = 0; |
| 10206 | if (*save->context() == *isolate->debug()->debug_context()) { |
| 10207 | flags |= 1 << 0; |
| 10208 | } |
| 10209 | if (it.frame()->is_optimized()) { |
| 10210 | flags |= 1 << 1; |
| 10211 | if (deoptimized_frame_index > 0) { |
| 10212 | flags |= 1 << 2; |
| 10213 | } |
| 10214 | } |
| 10215 | details->set(kFrameDetailsFlagsIndex, Smi::FromInt(flags)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10216 | |
| 10217 | // Fill the dynamic part. |
| 10218 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 10219 | |
| 10220 | // Add arguments name and value. |
| 10221 | for (int i = 0; i < argument_count; i++) { |
| 10222 | // Name of the argument. |
| 10223 | if (i < info.number_of_parameters()) { |
| 10224 | details->set(details_index++, *info.parameter_name(i)); |
| 10225 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10226 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10227 | } |
| 10228 | |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10229 | // Parameter value. |
| 10230 | if (it.frame()->is_optimized()) { |
| 10231 | // Get the value from the deoptimized frame. |
| 10232 | details->set(details_index++, deoptimized_frame->GetParameter(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10233 | } else { |
sgjesse@chromium.org | 6db8871 | 2011-07-11 11:41:22 +0000 | [diff] [blame] | 10234 | if (i < it.frame()->ComputeParametersCount()) { |
| 10235 | // Get the value from the stack. |
| 10236 | details->set(details_index++, it.frame()->GetParameter(i)); |
| 10237 | } else { |
| 10238 | details->set(details_index++, heap->undefined_value()); |
| 10239 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10240 | } |
| 10241 | } |
| 10242 | |
| 10243 | // Add locals name and value from the temporary copy from the function frame. |
| 10244 | for (int i = 0; i < info.NumberOfLocals() * 2; i++) { |
| 10245 | details->set(details_index++, locals->get(i)); |
| 10246 | } |
| 10247 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 10248 | // Add the value being returned. |
| 10249 | if (at_return) { |
| 10250 | details->set(details_index++, *return_value); |
| 10251 | } |
| 10252 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10253 | // Add the receiver (same as in function frame). |
| 10254 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 10255 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10256 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10257 | if (!receiver->IsJSObject()) { |
| 10258 | // If the receiver is NOT a JSObject we have hit an optimization |
| 10259 | // where a value object is not converted into a wrapped JS objects. |
| 10260 | // To hide this optimization from the debugger, we wrap the receiver |
| 10261 | // by creating correct wrapper object based on the calling frame's |
| 10262 | // global context. |
| 10263 | it.Advance(); |
| 10264 | Handle<Context> calling_frames_global_context( |
| 10265 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10266 | receiver = |
| 10267 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10268 | } |
| 10269 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 10270 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10271 | // Get rid of the calculated deoptimized frame if any. |
| 10272 | if (deoptimized_frame != NULL) { |
| 10273 | Deoptimizer::DeleteDebuggerInspectableFrame(deoptimized_frame, |
| 10274 | isolate); |
| 10275 | } |
| 10276 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10277 | ASSERT_EQ(details_size, details_index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10278 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10279 | } |
| 10280 | |
| 10281 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10282 | // 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] | 10283 | static bool CopyContextLocalsToScopeObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10284 | Isolate* isolate, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10285 | Handle<SerializedScopeInfo> serialized_scope_info, |
| 10286 | ScopeInfo<>& scope_info, |
| 10287 | Handle<Context> context, |
| 10288 | Handle<JSObject> scope_object) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10289 | // Fill all context locals to the context extension. |
| 10290 | for (int i = Context::MIN_CONTEXT_SLOTS; |
| 10291 | i < scope_info.number_of_context_slots(); |
| 10292 | i++) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10293 | int context_index = serialized_scope_info->ContextSlotIndex( |
| 10294 | *scope_info.context_slot_name(i), NULL); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10295 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 10296 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10297 | isolate, |
| 10298 | SetProperty(scope_object, |
| 10299 | scope_info.context_slot_name(i), |
| 10300 | Handle<Object>(context->get(context_index), isolate), |
| 10301 | NONE, |
| 10302 | kNonStrictMode), |
| 10303 | false); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10304 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10305 | |
| 10306 | return true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10307 | } |
| 10308 | |
| 10309 | |
| 10310 | // Create a plain JSObject which materializes the local scope for the specified |
| 10311 | // frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10312 | static Handle<JSObject> MaterializeLocalScope(Isolate* isolate, |
| 10313 | JavaScriptFrame* frame) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10314 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10315 | Handle<SharedFunctionInfo> shared(function->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10316 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 10317 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10318 | |
| 10319 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 10320 | // heap locals and extension properties of the debugged function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10321 | Handle<JSObject> local_scope = |
| 10322 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10323 | |
| 10324 | // First fill all parameters. |
| 10325 | for (int i = 0; i < scope_info.number_of_parameters(); ++i) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10326 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10327 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10328 | SetProperty(local_scope, |
| 10329 | scope_info.parameter_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10330 | Handle<Object>(frame->GetParameter(i), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10331 | NONE, |
| 10332 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10333 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10334 | } |
| 10335 | |
| 10336 | // Second fill all stack locals. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10337 | for (int i = 0; i < scope_info.number_of_stack_slots(); ++i) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10338 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10339 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10340 | SetProperty(local_scope, |
| 10341 | scope_info.stack_slot_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10342 | Handle<Object>(frame->GetExpression(i), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10343 | NONE, |
| 10344 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10345 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10346 | } |
| 10347 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10348 | if (scope_info.number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
| 10349 | // Third fill all context locals. |
| 10350 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 10351 | Handle<Context> function_context(frame_context->declaration_context()); |
| 10352 | if (!CopyContextLocalsToScopeObject(isolate, |
| 10353 | serialized_scope_info, scope_info, |
| 10354 | function_context, local_scope)) { |
| 10355 | return Handle<JSObject>(); |
| 10356 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10357 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10358 | // Finally copy any properties from the function context extension. |
| 10359 | // These will be variables introduced by eval. |
| 10360 | if (function_context->closure() == *function) { |
| 10361 | if (function_context->has_extension() && |
| 10362 | !function_context->IsGlobalContext()) { |
| 10363 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
| 10364 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
| 10365 | for (int i = 0; i < keys->length(); i++) { |
| 10366 | // Names of variables introduced by eval are strings. |
| 10367 | ASSERT(keys->get(i)->IsString()); |
| 10368 | Handle<String> key(String::cast(keys->get(i))); |
| 10369 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10370 | isolate, |
| 10371 | SetProperty(local_scope, |
| 10372 | key, |
| 10373 | GetProperty(ext, key), |
| 10374 | NONE, |
| 10375 | kNonStrictMode), |
| 10376 | Handle<JSObject>()); |
| 10377 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10378 | } |
| 10379 | } |
| 10380 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 10381 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10382 | return local_scope; |
| 10383 | } |
| 10384 | |
| 10385 | |
| 10386 | // Create a plain JSObject which materializes the closure content for the |
| 10387 | // context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10388 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 10389 | Handle<Context> context) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10390 | ASSERT(context->IsFunctionContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10391 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10392 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10393 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 10394 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10395 | |
| 10396 | // Allocate and initialize a JSObject with all the content of theis function |
| 10397 | // closure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10398 | Handle<JSObject> closure_scope = |
| 10399 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10400 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10401 | // Fill all context locals to the context extension. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10402 | if (!CopyContextLocalsToScopeObject(isolate, |
| 10403 | serialized_scope_info, scope_info, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10404 | context, closure_scope)) { |
| 10405 | return Handle<JSObject>(); |
| 10406 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10407 | |
| 10408 | // Finally copy any properties from the function context extension. This will |
| 10409 | // be variables introduced by eval. |
| 10410 | if (context->has_extension()) { |
| 10411 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10412 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10413 | for (int i = 0; i < keys->length(); i++) { |
| 10414 | // Names of variables introduced by eval are strings. |
| 10415 | ASSERT(keys->get(i)->IsString()); |
| 10416 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10417 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10418 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10419 | SetProperty(closure_scope, |
| 10420 | key, |
| 10421 | GetProperty(ext, key), |
| 10422 | NONE, |
| 10423 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10424 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10425 | } |
| 10426 | } |
| 10427 | |
| 10428 | return closure_scope; |
| 10429 | } |
| 10430 | |
| 10431 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10432 | // Create a plain JSObject which materializes the scope for the specified |
| 10433 | // catch context. |
| 10434 | static Handle<JSObject> MaterializeCatchScope(Isolate* isolate, |
| 10435 | Handle<Context> context) { |
| 10436 | ASSERT(context->IsCatchContext()); |
| 10437 | Handle<String> name(String::cast(context->extension())); |
| 10438 | Handle<Object> thrown_object(context->get(Context::THROWN_OBJECT_INDEX)); |
| 10439 | Handle<JSObject> catch_scope = |
| 10440 | isolate->factory()->NewJSObject(isolate->object_function()); |
| 10441 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10442 | isolate, |
| 10443 | SetProperty(catch_scope, name, thrown_object, NONE, kNonStrictMode), |
| 10444 | Handle<JSObject>()); |
| 10445 | return catch_scope; |
| 10446 | } |
| 10447 | |
| 10448 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10449 | // Iterate over the actual scopes visible from a stack frame. All scopes are |
| 10450 | // backed by an actual context except the local scope, which is inserted |
| 10451 | // "artifically" in the context chain. |
| 10452 | class ScopeIterator { |
| 10453 | public: |
| 10454 | enum ScopeType { |
| 10455 | ScopeTypeGlobal = 0, |
| 10456 | ScopeTypeLocal, |
| 10457 | ScopeTypeWith, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10458 | ScopeTypeClosure, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10459 | ScopeTypeCatch |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10460 | }; |
| 10461 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10462 | ScopeIterator(Isolate* isolate, JavaScriptFrame* frame) |
| 10463 | : isolate_(isolate), |
| 10464 | frame_(frame), |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10465 | function_(JSFunction::cast(frame->function())), |
| 10466 | context_(Context::cast(frame->context())), |
| 10467 | local_done_(false), |
| 10468 | at_local_(false) { |
| 10469 | |
| 10470 | // Check whether the first scope is actually a local scope. |
| 10471 | if (context_->IsGlobalContext()) { |
| 10472 | // If there is a stack slot for .result then this local scope has been |
| 10473 | // created for evaluating top level code and it is not a real local scope. |
| 10474 | // Checking for the existence of .result seems fragile, but the scope info |
| 10475 | // saved with the code object does not otherwise have that information. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10476 | int index = function_->shared()->scope_info()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10477 | StackSlotIndex(isolate_->heap()->result_symbol()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10478 | at_local_ = index < 0; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10479 | } else if (context_->IsFunctionContext()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10480 | at_local_ = true; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 10481 | } else if (context_->closure() != *function_) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10482 | // The context_ is a with or catch block from the outer function. |
| 10483 | ASSERT(context_->IsWithContext() || context_->IsCatchContext()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 10484 | at_local_ = true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10485 | } |
| 10486 | } |
| 10487 | |
| 10488 | // More scopes? |
| 10489 | bool Done() { return context_.is_null(); } |
| 10490 | |
| 10491 | // Move to the next scope. |
| 10492 | void Next() { |
| 10493 | // If at a local scope mark the local scope as passed. |
| 10494 | if (at_local_) { |
| 10495 | at_local_ = false; |
| 10496 | local_done_ = true; |
| 10497 | |
| 10498 | // If the current context is not associated with the local scope the |
| 10499 | // current context is the next real scope, so don't move to the next |
| 10500 | // context in this case. |
| 10501 | if (context_->closure() != *function_) { |
| 10502 | return; |
| 10503 | } |
| 10504 | } |
| 10505 | |
| 10506 | // The global scope is always the last in the chain. |
| 10507 | if (context_->IsGlobalContext()) { |
| 10508 | context_ = Handle<Context>(); |
| 10509 | return; |
| 10510 | } |
| 10511 | |
| 10512 | // Move to the next context. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10513 | context_ = Handle<Context>(context_->previous(), isolate_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10514 | |
| 10515 | // If passing the local scope indicate that the current scope is now the |
| 10516 | // local scope. |
| 10517 | if (!local_done_ && |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10518 | (context_->IsGlobalContext() || context_->IsFunctionContext())) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10519 | at_local_ = true; |
| 10520 | } |
| 10521 | } |
| 10522 | |
| 10523 | // Return the type of the current scope. |
| 10524 | int Type() { |
| 10525 | if (at_local_) { |
| 10526 | return ScopeTypeLocal; |
| 10527 | } |
| 10528 | if (context_->IsGlobalContext()) { |
| 10529 | ASSERT(context_->global()->IsGlobalObject()); |
| 10530 | return ScopeTypeGlobal; |
| 10531 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10532 | if (context_->IsFunctionContext()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10533 | return ScopeTypeClosure; |
| 10534 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10535 | if (context_->IsCatchContext()) { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10536 | return ScopeTypeCatch; |
| 10537 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10538 | ASSERT(context_->IsWithContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10539 | return ScopeTypeWith; |
| 10540 | } |
| 10541 | |
| 10542 | // Return the JavaScript object with the content of the current scope. |
| 10543 | Handle<JSObject> ScopeObject() { |
| 10544 | switch (Type()) { |
| 10545 | case ScopeIterator::ScopeTypeGlobal: |
| 10546 | return Handle<JSObject>(CurrentContext()->global()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10547 | case ScopeIterator::ScopeTypeLocal: |
| 10548 | // Materialize the content of the local scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10549 | return MaterializeLocalScope(isolate_, frame_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10550 | case ScopeIterator::ScopeTypeWith: |
| 10551 | // Return the with object. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10552 | return Handle<JSObject>(JSObject::cast(CurrentContext()->extension())); |
| 10553 | case ScopeIterator::ScopeTypeCatch: |
| 10554 | return MaterializeCatchScope(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10555 | case ScopeIterator::ScopeTypeClosure: |
| 10556 | // Materialize the content of the closure scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10557 | return MaterializeClosure(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10558 | } |
| 10559 | UNREACHABLE(); |
| 10560 | return Handle<JSObject>(); |
| 10561 | } |
| 10562 | |
| 10563 | // Return the context for this scope. For the local context there might not |
| 10564 | // be an actual context. |
| 10565 | Handle<Context> CurrentContext() { |
| 10566 | if (at_local_ && context_->closure() != *function_) { |
| 10567 | return Handle<Context>(); |
| 10568 | } |
| 10569 | return context_; |
| 10570 | } |
| 10571 | |
| 10572 | #ifdef DEBUG |
| 10573 | // Debug print of the content of the current scope. |
| 10574 | void DebugPrint() { |
| 10575 | switch (Type()) { |
| 10576 | case ScopeIterator::ScopeTypeGlobal: |
| 10577 | PrintF("Global:\n"); |
| 10578 | CurrentContext()->Print(); |
| 10579 | break; |
| 10580 | |
| 10581 | case ScopeIterator::ScopeTypeLocal: { |
| 10582 | PrintF("Local:\n"); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10583 | ScopeInfo<> scope_info(function_->shared()->scope_info()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10584 | scope_info.Print(); |
| 10585 | if (!CurrentContext().is_null()) { |
| 10586 | CurrentContext()->Print(); |
| 10587 | if (CurrentContext()->has_extension()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10588 | Handle<Object> extension(CurrentContext()->extension()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10589 | if (extension->IsJSContextExtensionObject()) { |
| 10590 | extension->Print(); |
| 10591 | } |
| 10592 | } |
| 10593 | } |
| 10594 | break; |
| 10595 | } |
| 10596 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10597 | case ScopeIterator::ScopeTypeWith: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10598 | PrintF("With:\n"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10599 | CurrentContext()->extension()->Print(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10600 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10601 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10602 | case ScopeIterator::ScopeTypeCatch: |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10603 | PrintF("Catch:\n"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10604 | CurrentContext()->extension()->Print(); |
| 10605 | CurrentContext()->get(Context::THROWN_OBJECT_INDEX)->Print(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10606 | break; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10607 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10608 | case ScopeIterator::ScopeTypeClosure: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10609 | PrintF("Closure:\n"); |
| 10610 | CurrentContext()->Print(); |
| 10611 | if (CurrentContext()->has_extension()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 10612 | Handle<Object> extension(CurrentContext()->extension()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10613 | if (extension->IsJSContextExtensionObject()) { |
| 10614 | extension->Print(); |
| 10615 | } |
| 10616 | } |
| 10617 | break; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10618 | |
| 10619 | default: |
| 10620 | UNREACHABLE(); |
| 10621 | } |
| 10622 | PrintF("\n"); |
| 10623 | } |
| 10624 | #endif |
| 10625 | |
| 10626 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10627 | Isolate* isolate_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10628 | JavaScriptFrame* frame_; |
| 10629 | Handle<JSFunction> function_; |
| 10630 | Handle<Context> context_; |
| 10631 | bool local_done_; |
| 10632 | bool at_local_; |
| 10633 | |
| 10634 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 10635 | }; |
| 10636 | |
| 10637 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10638 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10639 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10640 | ASSERT(args.length() == 2); |
| 10641 | |
| 10642 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10643 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10644 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10645 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10646 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10647 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10648 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10649 | |
| 10650 | // Get the frame where the debugging is performed. |
| 10651 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10652 | JavaScriptFrameIterator it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10653 | JavaScriptFrame* frame = it.frame(); |
| 10654 | |
| 10655 | // Count the visible scopes. |
| 10656 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10657 | for (ScopeIterator it(isolate, frame); !it.Done(); it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10658 | n++; |
| 10659 | } |
| 10660 | |
| 10661 | return Smi::FromInt(n); |
| 10662 | } |
| 10663 | |
| 10664 | |
| 10665 | static const int kScopeDetailsTypeIndex = 0; |
| 10666 | static const int kScopeDetailsObjectIndex = 1; |
| 10667 | static const int kScopeDetailsSize = 2; |
| 10668 | |
| 10669 | // Return an array with scope details |
| 10670 | // args[0]: number: break id |
| 10671 | // args[1]: number: frame index |
| 10672 | // args[2]: number: scope index |
| 10673 | // |
| 10674 | // The array returned contains the following information: |
| 10675 | // 0: Scope type |
| 10676 | // 1: Scope object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10677 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10678 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10679 | ASSERT(args.length() == 3); |
| 10680 | |
| 10681 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10682 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10683 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10684 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10685 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10686 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10687 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10688 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[2]); |
| 10689 | |
| 10690 | // Get the frame where the debugging is performed. |
| 10691 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10692 | JavaScriptFrameIterator frame_it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10693 | JavaScriptFrame* frame = frame_it.frame(); |
| 10694 | |
| 10695 | // Find the requested scope. |
| 10696 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10697 | ScopeIterator it(isolate, frame); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10698 | for (; !it.Done() && n < index; it.Next()) { |
| 10699 | n++; |
| 10700 | } |
| 10701 | if (it.Done()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10702 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10703 | } |
| 10704 | |
| 10705 | // Calculate the size of the result. |
| 10706 | int details_size = kScopeDetailsSize; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10707 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10708 | |
| 10709 | // Fill in scope details. |
| 10710 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10711 | Handle<JSObject> scope_object = it.ScopeObject(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10712 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10713 | details->set(kScopeDetailsObjectIndex, *scope_object); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10714 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10715 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10716 | } |
| 10717 | |
| 10718 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10719 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrintScopes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10720 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10721 | ASSERT(args.length() == 0); |
| 10722 | |
| 10723 | #ifdef DEBUG |
| 10724 | // Print the scopes for the top frame. |
| 10725 | StackFrameLocator locator; |
| 10726 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10727 | for (ScopeIterator it(isolate, frame); !it.Done(); it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10728 | it.DebugPrint(); |
| 10729 | } |
| 10730 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10731 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10732 | } |
| 10733 | |
| 10734 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10735 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10736 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10737 | ASSERT(args.length() == 1); |
| 10738 | |
| 10739 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10740 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10741 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 10742 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10743 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10744 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10745 | |
| 10746 | // Count all archived V8 threads. |
| 10747 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10748 | for (ThreadState* thread = |
| 10749 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10750 | thread != NULL; |
| 10751 | thread = thread->Next()) { |
| 10752 | n++; |
| 10753 | } |
| 10754 | |
| 10755 | // Total number of threads is current thread and archived threads. |
| 10756 | return Smi::FromInt(n + 1); |
| 10757 | } |
| 10758 | |
| 10759 | |
| 10760 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 10761 | static const int kThreadDetailsThreadIdIndex = 1; |
| 10762 | static const int kThreadDetailsSize = 2; |
| 10763 | |
| 10764 | // Return an array with thread details |
| 10765 | // args[0]: number: break id |
| 10766 | // args[1]: number: thread index |
| 10767 | // |
| 10768 | // The array returned contains the following information: |
| 10769 | // 0: Is current thread? |
| 10770 | // 1: Thread id |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10771 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10772 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10773 | ASSERT(args.length() == 2); |
| 10774 | |
| 10775 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10776 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10777 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10778 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10779 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10780 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10781 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 10782 | |
| 10783 | // Allocate array for result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10784 | Handle<FixedArray> details = |
| 10785 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10786 | |
| 10787 | // Thread index 0 is current thread. |
| 10788 | if (index == 0) { |
| 10789 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10790 | details->set(kThreadDetailsCurrentThreadIndex, |
| 10791 | isolate->heap()->true_value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10792 | details->set(kThreadDetailsThreadIdIndex, |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 10793 | Smi::FromInt(ThreadId::Current().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10794 | } else { |
| 10795 | // Find the thread with the requested index. |
| 10796 | int n = 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10797 | ThreadState* thread = |
| 10798 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10799 | while (index != n && thread != NULL) { |
| 10800 | thread = thread->Next(); |
| 10801 | n++; |
| 10802 | } |
| 10803 | if (thread == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10804 | return isolate->heap()->undefined_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10805 | } |
| 10806 | |
| 10807 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10808 | details->set(kThreadDetailsCurrentThreadIndex, |
| 10809 | isolate->heap()->false_value()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 10810 | details->set(kThreadDetailsThreadIdIndex, |
| 10811 | Smi::FromInt(thread->id().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10812 | } |
| 10813 | |
| 10814 | // Convert to JS array and return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10815 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10816 | } |
| 10817 | |
| 10818 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10819 | // Sets the disable break state |
| 10820 | // args[0]: disable break state |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10821 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDisableBreak) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10822 | HandleScope scope(isolate); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10823 | ASSERT(args.length() == 1); |
| 10824 | CONVERT_BOOLEAN_CHECKED(disable_break, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10825 | isolate->debug()->set_disable_break(disable_break); |
| 10826 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10827 | } |
| 10828 | |
| 10829 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10830 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetBreakLocations) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10831 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10832 | ASSERT(args.length() == 1); |
| 10833 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10834 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 10835 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10836 | // Find the number of break points |
| 10837 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10838 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10839 | // Return array as JS array |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10840 | return *isolate->factory()->NewJSArrayWithElements( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10841 | Handle<FixedArray>::cast(break_locations)); |
| 10842 | } |
| 10843 | |
| 10844 | |
| 10845 | // Set a break point in a function |
| 10846 | // args[0]: function |
| 10847 | // args[1]: number: break source position (within the function source) |
| 10848 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10849 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFunctionBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10850 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10851 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10852 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 10853 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10854 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 10855 | RUNTIME_ASSERT(source_position >= 0); |
| 10856 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 10857 | |
| 10858 | // Set break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10859 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 10860 | &source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10861 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 10862 | return Smi::FromInt(source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10863 | } |
| 10864 | |
| 10865 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10866 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 10867 | Handle<Script> script, |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 10868 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10869 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 10870 | // script. The inner most SharedFunctionInfo containing the source position |
| 10871 | // for the requested break point is found. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10872 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10873 | // which is found is not compiled it is compiled and the heap is iterated |
| 10874 | // again as the compilation might create inner functions from the newly |
| 10875 | // compiled function and the actual requested break point might be in one of |
| 10876 | // these functions. |
| 10877 | bool done = false; |
| 10878 | // The current candidate for the source position: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10879 | int target_start_position = RelocInfo::kNoPosition; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10880 | Handle<SharedFunctionInfo> target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10881 | while (!done) { |
| 10882 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10883 | for (HeapObject* obj = iterator.next(); |
| 10884 | obj != NULL; obj = iterator.next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10885 | if (obj->IsSharedFunctionInfo()) { |
| 10886 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 10887 | if (shared->script() == *script) { |
| 10888 | // If the SharedFunctionInfo found has the requested script data and |
| 10889 | // contains the source position it is a candidate. |
| 10890 | int start_position = shared->function_token_position(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10891 | if (start_position == RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10892 | start_position = shared->start_position(); |
| 10893 | } |
| 10894 | if (start_position <= position && |
| 10895 | position <= shared->end_position()) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10896 | // If there is no candidate or this function is within the current |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10897 | // candidate this is the new candidate. |
| 10898 | if (target.is_null()) { |
| 10899 | target_start_position = start_position; |
| 10900 | target = shared; |
| 10901 | } else { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10902 | if (target_start_position == start_position && |
| 10903 | shared->end_position() == target->end_position()) { |
| 10904 | // If a top-level function contain only one function |
| 10905 | // declartion the source for the top-level and the function is |
| 10906 | // the same. In that case prefer the non top-level function. |
| 10907 | if (!shared->is_toplevel()) { |
| 10908 | target_start_position = start_position; |
| 10909 | target = shared; |
| 10910 | } |
| 10911 | } else if (target_start_position <= start_position && |
| 10912 | shared->end_position() <= target->end_position()) { |
| 10913 | // This containment check includes equality as a function inside |
| 10914 | // a top-level function can share either start or end position |
| 10915 | // with the top-level function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10916 | target_start_position = start_position; |
| 10917 | target = shared; |
| 10918 | } |
| 10919 | } |
| 10920 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10921 | } |
| 10922 | } |
| 10923 | } |
| 10924 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10925 | if (target.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10926 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10927 | } |
| 10928 | |
| 10929 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 10930 | // compilation of inner functions is introduced some additional checking |
| 10931 | // needs to be done here to compile inner functions. |
| 10932 | done = target->is_compiled(); |
| 10933 | if (!done) { |
| 10934 | // If the candidate is not compiled compile it to reveal any inner |
| 10935 | // functions which might contain the requested source position. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10936 | CompileLazyShared(target, KEEP_EXCEPTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10937 | } |
| 10938 | } |
| 10939 | |
| 10940 | return *target; |
| 10941 | } |
| 10942 | |
| 10943 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10944 | // Changes the state of a break point in a script and returns source position |
| 10945 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 10946 | // GetScriptFromScriptData. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10947 | // args[0]: script to set break point in |
| 10948 | // args[1]: number: break source position (within the script source) |
| 10949 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10950 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetScriptBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10951 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10952 | ASSERT(args.length() == 3); |
| 10953 | CONVERT_ARG_CHECKED(JSValue, wrapper, 0); |
| 10954 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 10955 | RUNTIME_ASSERT(source_position >= 0); |
| 10956 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 10957 | |
| 10958 | // Get the script from the script wrapper. |
| 10959 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 10960 | Handle<Script> script(Script::cast(wrapper->value())); |
| 10961 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 10962 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10963 | isolate, script, source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10964 | if (!result->IsUndefined()) { |
| 10965 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 10966 | // Find position within function. The script position might be before the |
| 10967 | // source position of the first function. |
| 10968 | int position; |
| 10969 | if (shared->start_position() > source_position) { |
| 10970 | position = 0; |
| 10971 | } else { |
| 10972 | position = source_position - shared->start_position(); |
| 10973 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10974 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10975 | position += shared->start_position(); |
| 10976 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10977 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10978 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10979 | } |
| 10980 | |
| 10981 | |
| 10982 | // Clear a break point |
| 10983 | // args[0]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10984 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10985 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10986 | ASSERT(args.length() == 1); |
| 10987 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 10988 | |
| 10989 | // Clear break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10990 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10991 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10992 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10993 | } |
| 10994 | |
| 10995 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10996 | // Change the state of break on exceptions. |
| 10997 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 10998 | // args[1]: Boolean indicating on/off. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10999 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ChangeBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11000 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11001 | ASSERT(args.length() == 2); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11002 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11003 | CONVERT_BOOLEAN_CHECKED(enable, args[1]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11004 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11005 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 11006 | // function will default to affecting caught exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11007 | ExceptionBreakType type = |
| 11008 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11009 | // Update break point state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11010 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 11011 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11012 | } |
| 11013 | |
| 11014 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11015 | // Returns the state of break on exceptions |
| 11016 | // args[0]: boolean indicating uncaught exceptions |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11017 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11018 | HandleScope scope(isolate); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11019 | ASSERT(args.length() == 1); |
| 11020 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 11021 | |
| 11022 | ExceptionBreakType type = |
| 11023 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11024 | bool result = isolate->debug()->IsBreakOnException(type); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 11025 | return Smi::FromInt(result); |
| 11026 | } |
| 11027 | |
| 11028 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11029 | // Prepare for stepping |
| 11030 | // args[0]: break id for checking execution state |
| 11031 | // args[1]: step action from the enumeration StepAction |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11032 | // args[2]: number of times to perform the step, for step out it is the number |
| 11033 | // of frames to step down. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11034 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrepareStep) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11035 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11036 | ASSERT(args.length() == 3); |
| 11037 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11038 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11039 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 11040 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11041 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 11042 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11043 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11044 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11045 | } |
| 11046 | |
| 11047 | // Get the step action and check validity. |
| 11048 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 11049 | if (step_action != StepIn && |
| 11050 | step_action != StepNext && |
| 11051 | step_action != StepOut && |
| 11052 | step_action != StepInMin && |
| 11053 | step_action != StepMin) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11054 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11055 | } |
| 11056 | |
| 11057 | // Get the number of steps. |
| 11058 | int step_count = NumberToInt32(args[2]); |
| 11059 | if (step_count < 1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11060 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11061 | } |
| 11062 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11063 | // Clear all current stepping setup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11064 | isolate->debug()->ClearStepping(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 11065 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11066 | // Prepare step. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11067 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 11068 | step_count); |
| 11069 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11070 | } |
| 11071 | |
| 11072 | |
| 11073 | // Clear all stepping set by PrepareStep. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11074 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearStepping) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11075 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11076 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11077 | isolate->debug()->ClearStepping(); |
| 11078 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11079 | } |
| 11080 | |
| 11081 | |
| 11082 | // Creates a copy of the with context chain. The copy of the context chain is |
| 11083 | // is linked to the function context supplied. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11084 | static Handle<Context> CopyWithContextChain(Isolate* isolate, |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11085 | Handle<JSFunction> function, |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11086 | Handle<Context> current, |
| 11087 | Handle<Context> base) { |
| 11088 | // At the end of the chain. Return the base context to link to. |
| 11089 | if (current->IsFunctionContext() || current->IsGlobalContext()) { |
| 11090 | return base; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11091 | } |
| 11092 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11093 | // Recursively copy the with and catch contexts. |
| 11094 | HandleScope scope(isolate); |
| 11095 | Handle<Context> previous(current->previous()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11096 | Handle<Context> new_previous = |
| 11097 | CopyWithContextChain(isolate, function, previous, base); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11098 | Handle<Context> new_current; |
| 11099 | if (current->IsCatchContext()) { |
| 11100 | Handle<String> name(String::cast(current->extension())); |
| 11101 | Handle<Object> thrown_object(current->get(Context::THROWN_OBJECT_INDEX)); |
| 11102 | new_current = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11103 | isolate->factory()->NewCatchContext(function, |
| 11104 | new_previous, |
| 11105 | name, |
| 11106 | thrown_object); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11107 | } else { |
| 11108 | Handle<JSObject> extension(JSObject::cast(current->extension())); |
| 11109 | new_current = |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11110 | isolate->factory()->NewWithContext(function, new_previous, extension); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11111 | } |
| 11112 | return scope.CloseAndEscape(new_current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11113 | } |
| 11114 | |
| 11115 | |
| 11116 | // Helper function to find or create the arguments object for |
| 11117 | // Runtime_DebugEvaluate. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11118 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 11119 | JavaScriptFrame* frame, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11120 | Handle<JSFunction> function, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 11121 | Handle<SerializedScopeInfo> scope_info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11122 | const ScopeInfo<>* sinfo, |
| 11123 | Handle<Context> function_context) { |
| 11124 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 11125 | // found (that is the debugged function does not reference 'arguments' and |
| 11126 | // does not support eval) then create an 'arguments' object. |
| 11127 | int index; |
| 11128 | if (sinfo->number_of_stack_slots() > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11129 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11130 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11131 | return Handle<Object>(frame->GetExpression(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11132 | } |
| 11133 | } |
| 11134 | |
| 11135 | if (sinfo->number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11136 | index = scope_info->ContextSlotIndex(isolate->heap()->arguments_symbol(), |
| 11137 | NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11138 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11139 | return Handle<Object>(function_context->get(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11140 | } |
| 11141 | } |
| 11142 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 11143 | const int length = frame->ComputeParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11144 | Handle<JSObject> arguments = |
| 11145 | isolate->factory()->NewArgumentsObject(function, length); |
| 11146 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11147 | |
| 11148 | AssertNoAllocation no_gc; |
| 11149 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11150 | for (int i = 0; i < length; i++) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 11151 | array->set(i, frame->GetParameter(i), mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11152 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 11153 | arguments->set_elements(*array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11154 | return arguments; |
| 11155 | } |
| 11156 | |
| 11157 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11158 | static const char kSourceStr[] = |
| 11159 | "(function(arguments,__source__){return eval(__source__);})"; |
| 11160 | |
| 11161 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11162 | // 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] | 11163 | // 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] | 11164 | // extension part has all the parameters and locals of the function on the |
| 11165 | // stack frame. A function which calls eval with the code to evaluate is then |
| 11166 | // compiled in this context and called in this context. As this context |
| 11167 | // replaces the context of the function on the stack frame a new (empty) |
| 11168 | // function is created as well to be used as the closure for the context. |
| 11169 | // This function and the context acts as replacements for the function on the |
| 11170 | // stack frame presenting the same view of the values of parameters and |
| 11171 | // local variables as if the piece of JavaScript was evaluated at the point |
| 11172 | // where the function on the stack frame is currently stopped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11173 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11174 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11175 | |
| 11176 | // Check the execution state and decode arguments frame and source to be |
| 11177 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11178 | ASSERT(args.length() == 5); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11179 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11180 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 11181 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11182 | if (!maybe_check_result->ToObject(&check_result)) { |
| 11183 | return maybe_check_result; |
| 11184 | } |
| 11185 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11186 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 11187 | CONVERT_ARG_CHECKED(String, source, 2); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11188 | CONVERT_BOOLEAN_CHECKED(disable_break, args[3]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11189 | Handle<Object> additional_context(args[4]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11190 | |
| 11191 | // Handle the processing of break. |
| 11192 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11193 | |
| 11194 | // Get the frame where the debugging is performed. |
| 11195 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11196 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11197 | JavaScriptFrame* frame = it.frame(); |
| 11198 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 11199 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11200 | ScopeInfo<> sinfo(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11201 | |
| 11202 | // Traverse the saved contexts chain to find the active context for the |
| 11203 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11204 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 11205 | while (save != NULL && !save->below(frame)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11206 | save = save->prev(); |
| 11207 | } |
| 11208 | ASSERT(save != NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11209 | SaveContext savex(isolate); |
| 11210 | isolate->set_context(*(save->context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11211 | |
| 11212 | // Create the (empty) function replacing the function on the stack frame for |
| 11213 | // the purpose of evaluating in the context created below. It is important |
| 11214 | // that this function does not describe any parameters and local variables |
| 11215 | // in the context. If it does then this will cause problems with the lookup |
| 11216 | // in Context::Lookup, where context slots for parameters and local variables |
| 11217 | // are looked at before the extension object. |
| 11218 | Handle<JSFunction> go_between = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11219 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 11220 | isolate->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11221 | go_between->set_context(function->context()); |
| 11222 | #ifdef DEBUG |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11223 | ScopeInfo<> go_between_sinfo(go_between->shared()->scope_info()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11224 | ASSERT(go_between_sinfo.number_of_parameters() == 0); |
| 11225 | ASSERT(go_between_sinfo.number_of_context_slots() == 0); |
| 11226 | #endif |
| 11227 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11228 | // Materialize the content of the local scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11229 | Handle<JSObject> local_scope = MaterializeLocalScope(isolate, frame); |
| 11230 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11231 | |
| 11232 | // Allocate a new context for the debug evaluation and set the extension |
| 11233 | // object build. |
| 11234 | Handle<Context> context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11235 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 11236 | go_between); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11237 | context->set_extension(*local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11238 | // 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] | 11239 | Handle<Context> frame_context(Context::cast(frame->context())); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11240 | Handle<Context> function_context(frame_context->declaration_context()); |
| 11241 | context = CopyWithContextChain(isolate, go_between, frame_context, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11242 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11243 | if (additional_context->IsJSObject()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11244 | Handle<JSObject> extension = Handle<JSObject>::cast(additional_context); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 11245 | context = |
| 11246 | isolate->factory()->NewWithContext(go_between, context, extension); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11247 | } |
| 11248 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11249 | // Wrap the evaluation statement in a new function compiled in the newly |
| 11250 | // created context. The function has one parameter which has to be called |
| 11251 | // '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] | 11252 | // the function being debugged. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11253 | // function(arguments,__source__) {return eval(__source__);} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11254 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11255 | Handle<String> function_source = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11256 | isolate->factory()->NewStringFromAscii( |
| 11257 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11258 | |
| 11259 | // Currently, the eval code will be executed in non-strict mode, |
| 11260 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11261 | Handle<SharedFunctionInfo> shared = |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 11262 | Compiler::CompileEval(function_source, |
| 11263 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11264 | context->IsGlobalContext(), |
| 11265 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11266 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11267 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11268 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11269 | |
| 11270 | // Invoke the result of the compilation to get the evaluation function. |
| 11271 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11272 | Handle<Object> receiver(frame->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11273 | Handle<Object> evaluation_function = |
| 11274 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 11275 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 11276 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11277 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11278 | Handle<Object> arguments = GetArgumentsObject(isolate, frame, |
| 11279 | function, scope_info, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 11280 | &sinfo, function_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11281 | |
| 11282 | // Invoke the evaluation function and return the result. |
| 11283 | const int argc = 2; |
| 11284 | Object** argv[argc] = { arguments.location(), |
| 11285 | Handle<Object>::cast(source).location() }; |
| 11286 | Handle<Object> result = |
| 11287 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), receiver, |
| 11288 | argc, argv, &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 11289 | if (has_pending_exception) return Failure::Exception(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 11290 | |
| 11291 | // Skip the global proxy as it has no properties and always delegates to the |
| 11292 | // real global object. |
| 11293 | if (result->IsJSGlobalProxy()) { |
| 11294 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 11295 | } |
| 11296 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11297 | return *result; |
| 11298 | } |
| 11299 | |
| 11300 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11301 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluateGlobal) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11302 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11303 | |
| 11304 | // Check the execution state and decode arguments frame and source to be |
| 11305 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11306 | ASSERT(args.length() == 4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11307 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11308 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 11309 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11310 | if (!maybe_check_result->ToObject(&check_result)) { |
| 11311 | return maybe_check_result; |
| 11312 | } |
| 11313 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11314 | CONVERT_ARG_CHECKED(String, source, 1); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11315 | CONVERT_BOOLEAN_CHECKED(disable_break, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11316 | Handle<Object> additional_context(args[3]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 11317 | |
| 11318 | // Handle the processing of break. |
| 11319 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11320 | |
| 11321 | // Enter the top context from before the debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11322 | SaveContext save(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11323 | SaveContext* top = &save; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11324 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11325 | top = top->prev(); |
| 11326 | } |
| 11327 | if (top != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11328 | isolate->set_context(*top->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11329 | } |
| 11330 | |
| 11331 | // Get the global context now set to the top context from before the |
| 11332 | // debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11333 | Handle<Context> context = isolate->global_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11334 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11335 | bool is_global = true; |
| 11336 | |
| 11337 | if (additional_context->IsJSObject()) { |
| 11338 | // Create a function context first, than put 'with' context on top of it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11339 | Handle<JSFunction> go_between = isolate->factory()->NewFunction( |
| 11340 | isolate->factory()->empty_string(), |
| 11341 | isolate->factory()->undefined_value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11342 | go_between->set_context(*context); |
| 11343 | context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11344 | isolate->factory()->NewFunctionContext( |
| 11345 | Context::MIN_CONTEXT_SLOTS, go_between); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 11346 | context->set_extension(JSObject::cast(*additional_context)); |
| 11347 | is_global = false; |
| 11348 | } |
| 11349 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11350 | // Compile the source to be evaluated. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11351 | // Currently, the eval code will be executed in non-strict mode, |
| 11352 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11353 | Handle<SharedFunctionInfo> shared = |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11354 | Compiler::CompileEval(source, context, is_global, kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 11355 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11356 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11357 | Handle<JSFunction>( |
| 11358 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 11359 | context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11360 | |
| 11361 | // Invoke the result of the compilation to get the evaluation function. |
| 11362 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11363 | Handle<Object> receiver = isolate->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11364 | Handle<Object> result = |
| 11365 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 11366 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 11367 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11368 | return *result; |
| 11369 | } |
| 11370 | |
| 11371 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11372 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetLoadedScripts) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11373 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11374 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11375 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11376 | // Fill the script objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11377 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11378 | |
| 11379 | // Convert the script objects to proper JS objects. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 11380 | for (int i = 0; i < instances->length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 11381 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 11382 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 11383 | // because using |
| 11384 | // instances->set(i, *GetScriptWrapper(script)) |
| 11385 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
| 11386 | // already have deferenced the instances handle. |
| 11387 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 11388 | instances->set(i, *wrapper); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11389 | } |
| 11390 | |
| 11391 | // Return result as a JS array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11392 | Handle<JSObject> result = |
| 11393 | isolate->factory()->NewJSObject(isolate->array_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11394 | Handle<JSArray>::cast(result)->SetContent(*instances); |
| 11395 | return *result; |
| 11396 | } |
| 11397 | |
| 11398 | |
| 11399 | // Helper function used by Runtime_DebugReferencedBy below. |
| 11400 | static int DebugReferencedBy(JSObject* target, |
| 11401 | Object* instance_filter, int max_references, |
| 11402 | FixedArray* instances, int instances_size, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11403 | JSFunction* arguments_function) { |
| 11404 | NoHandleAllocation ha; |
| 11405 | AssertNoAllocation no_alloc; |
| 11406 | |
| 11407 | // Iterate the heap. |
| 11408 | int count = 0; |
| 11409 | JSObject* last = NULL; |
| 11410 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11411 | HeapObject* heap_obj = NULL; |
| 11412 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11413 | (max_references == 0 || count < max_references)) { |
| 11414 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11415 | if (heap_obj->IsJSObject()) { |
| 11416 | // Skip context extension objects and argument arrays as these are |
| 11417 | // checked in the context of functions using them. |
| 11418 | JSObject* obj = JSObject::cast(heap_obj); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11419 | if (obj->IsJSContextExtensionObject() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11420 | obj->map()->constructor() == arguments_function) { |
| 11421 | continue; |
| 11422 | } |
| 11423 | |
| 11424 | // Check if the JS object has a reference to the object looked for. |
| 11425 | if (obj->ReferencesObject(target)) { |
| 11426 | // Check instance filter if supplied. This is normally used to avoid |
| 11427 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 11428 | if (!instance_filter->IsUndefined()) { |
| 11429 | Object* V = obj; |
| 11430 | while (true) { |
| 11431 | Object* prototype = V->GetPrototype(); |
| 11432 | if (prototype->IsNull()) { |
| 11433 | break; |
| 11434 | } |
| 11435 | if (instance_filter == prototype) { |
| 11436 | obj = NULL; // Don't add this object. |
| 11437 | break; |
| 11438 | } |
| 11439 | V = prototype; |
| 11440 | } |
| 11441 | } |
| 11442 | |
| 11443 | if (obj != NULL) { |
| 11444 | // Valid reference found add to instance array if supplied an update |
| 11445 | // count. |
| 11446 | if (instances != NULL && count < instances_size) { |
| 11447 | instances->set(count, obj); |
| 11448 | } |
| 11449 | last = obj; |
| 11450 | count++; |
| 11451 | } |
| 11452 | } |
| 11453 | } |
| 11454 | } |
| 11455 | |
| 11456 | // Check for circular reference only. This can happen when the object is only |
| 11457 | // referenced from mirrors and has a circular reference in which case the |
| 11458 | // object is not really alive and would have been garbage collected if not |
| 11459 | // referenced from the mirror. |
| 11460 | if (count == 1 && last == target) { |
| 11461 | count = 0; |
| 11462 | } |
| 11463 | |
| 11464 | // Return the number of referencing objects found. |
| 11465 | return count; |
| 11466 | } |
| 11467 | |
| 11468 | |
| 11469 | // Scan the heap for objects with direct references to an object |
| 11470 | // args[0]: the object to find references to |
| 11471 | // args[1]: constructor function for instances to exclude (Mirror) |
| 11472 | // args[2]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11473 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugReferencedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11474 | ASSERT(args.length() == 3); |
| 11475 | |
| 11476 | // First perform a full GC in order to avoid references from dead objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11477 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11478 | |
| 11479 | // Check parameters. |
| 11480 | CONVERT_CHECKED(JSObject, target, args[0]); |
| 11481 | Object* instance_filter = args[1]; |
| 11482 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 11483 | instance_filter->IsJSObject()); |
| 11484 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 11485 | RUNTIME_ASSERT(max_references >= 0); |
| 11486 | |
| 11487 | // Get the constructor function for context extension and arguments array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11488 | JSObject* arguments_boilerplate = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11489 | isolate->context()->global_context()->arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11490 | JSFunction* arguments_function = |
| 11491 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 11492 | |
| 11493 | // Get the number of referencing objects. |
| 11494 | int count; |
| 11495 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11496 | NULL, 0, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11497 | |
| 11498 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11499 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11500 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11501 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 11502 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11503 | FixedArray* instances = FixedArray::cast(object); |
| 11504 | |
| 11505 | // Fill the referencing objects. |
| 11506 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11507 | instances, count, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11508 | |
| 11509 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11510 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11511 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 11512 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11513 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11514 | } |
| 11515 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11516 | return result; |
| 11517 | } |
| 11518 | |
| 11519 | |
| 11520 | // Helper function used by Runtime_DebugConstructedBy below. |
| 11521 | static int DebugConstructedBy(JSFunction* constructor, int max_references, |
| 11522 | FixedArray* instances, int instances_size) { |
| 11523 | AssertNoAllocation no_alloc; |
| 11524 | |
| 11525 | // Iterate the heap. |
| 11526 | int count = 0; |
| 11527 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11528 | HeapObject* heap_obj = NULL; |
| 11529 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11530 | (max_references == 0 || count < max_references)) { |
| 11531 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11532 | if (heap_obj->IsJSObject()) { |
| 11533 | JSObject* obj = JSObject::cast(heap_obj); |
| 11534 | if (obj->map()->constructor() == constructor) { |
| 11535 | // Valid reference found add to instance array if supplied an update |
| 11536 | // count. |
| 11537 | if (instances != NULL && count < instances_size) { |
| 11538 | instances->set(count, obj); |
| 11539 | } |
| 11540 | count++; |
| 11541 | } |
| 11542 | } |
| 11543 | } |
| 11544 | |
| 11545 | // Return the number of referencing objects found. |
| 11546 | return count; |
| 11547 | } |
| 11548 | |
| 11549 | |
| 11550 | // Scan the heap for objects constructed by a specific function. |
| 11551 | // args[0]: the constructor to find instances of |
| 11552 | // args[1]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11553 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugConstructedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11554 | ASSERT(args.length() == 2); |
| 11555 | |
| 11556 | // First perform a full GC in order to avoid dead objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11557 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11558 | |
| 11559 | // Check parameters. |
| 11560 | CONVERT_CHECKED(JSFunction, constructor, args[0]); |
| 11561 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 11562 | RUNTIME_ASSERT(max_references >= 0); |
| 11563 | |
| 11564 | // Get the number of referencing objects. |
| 11565 | int count; |
| 11566 | count = DebugConstructedBy(constructor, max_references, NULL, 0); |
| 11567 | |
| 11568 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11569 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11570 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11571 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 11572 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11573 | FixedArray* instances = FixedArray::cast(object); |
| 11574 | |
| 11575 | // Fill the referencing objects. |
| 11576 | count = DebugConstructedBy(constructor, max_references, instances, count); |
| 11577 | |
| 11578 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11579 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11580 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 11581 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11582 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11583 | } |
| 11584 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11585 | return result; |
| 11586 | } |
| 11587 | |
| 11588 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 11589 | // Find the effective prototype object as returned by __proto__. |
| 11590 | // args[0]: the object to find the prototype for. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11591 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPrototype) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11592 | ASSERT(args.length() == 1); |
| 11593 | |
| 11594 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 11595 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 11596 | // Use the __proto__ accessor. |
| 11597 | return Accessors::ObjectPrototype.getter(obj, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11598 | } |
| 11599 | |
| 11600 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11601 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SystemBreak) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11602 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11603 | CPU::DebugBreak(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11604 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11605 | } |
| 11606 | |
| 11607 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11608 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleFunction) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11609 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11610 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11611 | ASSERT(args.length() == 1); |
| 11612 | // Get the function and make sure it is compiled. |
| 11613 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11614 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 11615 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11616 | return Failure::Exception(); |
| 11617 | } |
| 11618 | func->code()->PrintLn(); |
| 11619 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11620 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11621 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11622 | |
| 11623 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11624 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleConstructor) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11625 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11626 | HandleScope scope(isolate); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11627 | ASSERT(args.length() == 1); |
| 11628 | // Get the function and make sure it is compiled. |
| 11629 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11630 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 11631 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11632 | return Failure::Exception(); |
| 11633 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11634 | shared->construct_stub()->PrintLn(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11635 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11636 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11637 | } |
| 11638 | |
| 11639 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11640 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetInferredName) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11641 | NoHandleAllocation ha; |
| 11642 | ASSERT(args.length() == 1); |
| 11643 | |
| 11644 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 11645 | return f->shared()->inferred_name(); |
| 11646 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11647 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11648 | |
| 11649 | static int FindSharedFunctionInfosForScript(Script* script, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11650 | FixedArray* buffer) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11651 | AssertNoAllocation no_allocations; |
| 11652 | |
| 11653 | int counter = 0; |
| 11654 | int buffer_size = buffer->length(); |
| 11655 | HeapIterator iterator; |
| 11656 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 11657 | ASSERT(obj != NULL); |
| 11658 | if (!obj->IsSharedFunctionInfo()) { |
| 11659 | continue; |
| 11660 | } |
| 11661 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 11662 | if (shared->script() != script) { |
| 11663 | continue; |
| 11664 | } |
| 11665 | if (counter < buffer_size) { |
| 11666 | buffer->set(counter, shared); |
| 11667 | } |
| 11668 | counter++; |
| 11669 | } |
| 11670 | return counter; |
| 11671 | } |
| 11672 | |
| 11673 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 11674 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 11675 | // in OpaqueReferences. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11676 | RUNTIME_FUNCTION(MaybeObject*, |
| 11677 | Runtime_LiveEditFindSharedFunctionInfosForScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11678 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11679 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11680 | CONVERT_CHECKED(JSValue, script_value, args[0]); |
| 11681 | |
| 11682 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 11683 | |
| 11684 | const int kBufferSize = 32; |
| 11685 | |
| 11686 | Handle<FixedArray> array; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11687 | array = isolate->factory()->NewFixedArray(kBufferSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11688 | int number = FindSharedFunctionInfosForScript(*script, *array); |
| 11689 | if (number > kBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11690 | array = isolate->factory()->NewFixedArray(number); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11691 | FindSharedFunctionInfosForScript(*script, *array); |
| 11692 | } |
| 11693 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11694 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11695 | result->set_length(Smi::FromInt(number)); |
| 11696 | |
| 11697 | LiveEdit::WrapSharedFunctionInfos(result); |
| 11698 | |
| 11699 | return *result; |
| 11700 | } |
| 11701 | |
| 11702 | // For a script calculates compilation information about all its functions. |
| 11703 | // The script source is explicitly specified by the second argument. |
| 11704 | // The source of the actual script is not used, however it is important that |
| 11705 | // all generated code keeps references to this particular instance of script. |
| 11706 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 11707 | // each function with all its descendant is always stored in a continues range |
| 11708 | // 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] | 11709 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditGatherCompileInfo) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11710 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11711 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11712 | CONVERT_CHECKED(JSValue, script, args[0]); |
| 11713 | CONVERT_ARG_CHECKED(String, source, 1); |
| 11714 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 11715 | |
| 11716 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 11717 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11718 | if (isolate->has_pending_exception()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11719 | return Failure::Exception(); |
| 11720 | } |
| 11721 | |
| 11722 | return result; |
| 11723 | } |
| 11724 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11725 | // Changes the source of the script to a new_source. |
| 11726 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 11727 | // the script with its original source and sends notification to debugger. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11728 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11729 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11730 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11731 | CONVERT_CHECKED(JSValue, original_script_value, args[0]); |
| 11732 | CONVERT_ARG_CHECKED(String, new_source, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11733 | Handle<Object> old_script_name(args[2], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11734 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11735 | CONVERT_CHECKED(Script, original_script_pointer, |
| 11736 | original_script_value->value()); |
| 11737 | Handle<Script> original_script(original_script_pointer); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11738 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11739 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 11740 | new_source, |
| 11741 | old_script_name); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11742 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11743 | if (old_script->IsScript()) { |
| 11744 | Handle<Script> script_handle(Script::cast(old_script)); |
| 11745 | return *(GetScriptWrapper(script_handle)); |
| 11746 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11747 | return isolate->heap()->null_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11748 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11749 | } |
| 11750 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11751 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11752 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSourceUpdated) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11753 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11754 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11755 | CONVERT_ARG_CHECKED(JSArray, shared_info, 0); |
| 11756 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 11757 | } |
| 11758 | |
| 11759 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11760 | // Replaces code of SharedFunctionInfo with a new one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11761 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceFunctionCode) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11762 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11763 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11764 | CONVERT_ARG_CHECKED(JSArray, new_compile_info, 0); |
| 11765 | CONVERT_ARG_CHECKED(JSArray, shared_info, 1); |
| 11766 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11767 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11768 | } |
| 11769 | |
| 11770 | // Connects SharedFunctionInfo to another script. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11771 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSetScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11772 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11773 | HandleScope scope(isolate); |
| 11774 | Handle<Object> function_object(args[0], isolate); |
| 11775 | Handle<Object> script_object(args[1], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11776 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11777 | if (function_object->IsJSValue()) { |
| 11778 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 11779 | if (script_object->IsJSValue()) { |
| 11780 | CONVERT_CHECKED(Script, script, JSValue::cast(*script_object)->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11781 | script_object = Handle<Object>(script, isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11782 | } |
| 11783 | |
| 11784 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 11785 | } else { |
| 11786 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 11787 | // and we check it in this function. |
| 11788 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11789 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11790 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11791 | } |
| 11792 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11793 | |
| 11794 | // In a code of a parent function replaces original function as embedded object |
| 11795 | // with a substitution one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11796 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceRefToNestedFunction) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11797 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11798 | HandleScope scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11799 | |
| 11800 | CONVERT_ARG_CHECKED(JSValue, parent_wrapper, 0); |
| 11801 | CONVERT_ARG_CHECKED(JSValue, orig_wrapper, 1); |
| 11802 | CONVERT_ARG_CHECKED(JSValue, subst_wrapper, 2); |
| 11803 | |
| 11804 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 11805 | subst_wrapper); |
| 11806 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11807 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11808 | } |
| 11809 | |
| 11810 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11811 | // Updates positions of a shared function info (first parameter) according |
| 11812 | // to script source change. Text change is described in second parameter as |
| 11813 | // array of groups of 3 numbers: |
| 11814 | // (change_begin, change_end, change_end_new_position). |
| 11815 | // 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] | 11816 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditPatchFunctionPositions) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11817 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11818 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11819 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
| 11820 | CONVERT_ARG_CHECKED(JSArray, position_change_array, 1); |
| 11821 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11822 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11823 | } |
| 11824 | |
| 11825 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11826 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 11827 | // checks that none of them have activations on stacks (of any thread). |
| 11828 | // Returns array of the same length with corresponding results of |
| 11829 | // LiveEdit::FunctionPatchabilityStatus type. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11830 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCheckAndDropActivations) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11831 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11832 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11833 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11834 | CONVERT_BOOLEAN_CHECKED(do_drop, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11835 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11836 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11837 | } |
| 11838 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11839 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 11840 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 11841 | // of diff chunks. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11842 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCompareStrings) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11843 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11844 | HandleScope scope(isolate); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11845 | CONVERT_ARG_CHECKED(String, s1, 0); |
| 11846 | CONVERT_ARG_CHECKED(String, s2, 1); |
| 11847 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11848 | return *LiveEdit::CompareStrings(s1, s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11849 | } |
| 11850 | |
| 11851 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11852 | // A testing entry. Returns statement position which is the closest to |
| 11853 | // source_position. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11854 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionCodePositionFromSource) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11855 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11856 | HandleScope scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11857 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 11858 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11859 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11860 | Handle<Code> code(function->code(), isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11861 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11862 | if (code->kind() != Code::FUNCTION && |
| 11863 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11864 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11865 | } |
| 11866 | |
| 11867 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11868 | int closest_pc = 0; |
| 11869 | int distance = kMaxInt; |
| 11870 | while (!it.done()) { |
| 11871 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 11872 | // Check if this break point is closer that what was previously found. |
| 11873 | if (source_position <= statement_position && |
| 11874 | statement_position - source_position < distance) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 11875 | closest_pc = |
| 11876 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11877 | distance = statement_position - source_position; |
| 11878 | // Check whether we can't get any closer. |
| 11879 | if (distance == 0) break; |
| 11880 | } |
| 11881 | it.next(); |
| 11882 | } |
| 11883 | |
| 11884 | return Smi::FromInt(closest_pc); |
| 11885 | } |
| 11886 | |
| 11887 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11888 | // Calls specified function with or without entering the debugger. |
| 11889 | // This is used in unit tests to run code as if debugger is entered or simply |
| 11890 | // to have a stack with C++ frame in the middle. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11891 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ExecuteInDebugContext) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11892 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11893 | HandleScope scope(isolate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11894 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 11895 | CONVERT_BOOLEAN_CHECKED(without_debugger, args[1]); |
| 11896 | |
| 11897 | Handle<Object> result; |
| 11898 | bool pending_exception; |
| 11899 | { |
| 11900 | if (without_debugger) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11901 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11902 | &pending_exception); |
| 11903 | } else { |
| 11904 | EnterDebugger enter_debugger; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11905 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11906 | &pending_exception); |
| 11907 | } |
| 11908 | } |
| 11909 | if (!pending_exception) { |
| 11910 | return *result; |
| 11911 | } else { |
| 11912 | return Failure::Exception(); |
| 11913 | } |
| 11914 | } |
| 11915 | |
| 11916 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11917 | // Sets a v8 flag. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11918 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFlags) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11919 | CONVERT_CHECKED(String, arg, args[0]); |
| 11920 | SmartPointer<char> flags = |
| 11921 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 11922 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11923 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11924 | } |
| 11925 | |
| 11926 | |
| 11927 | // Performs a GC. |
| 11928 | // Presently, it only does a full GC. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectGarbage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11930 | isolate->heap()->CollectAllGarbage(true); |
| 11931 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11932 | } |
| 11933 | |
| 11934 | |
| 11935 | // Gets the current heap usage. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11936 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHeapUsage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11937 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11938 | if (!Smi::IsValid(usage)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11939 | return *isolate->factory()->NewNumberFromInt(usage); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11940 | } |
| 11941 | return Smi::FromInt(usage); |
| 11942 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11943 | |
| 11944 | |
| 11945 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11946 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLOLEnabled) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11947 | #ifdef LIVE_OBJECT_LIST |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11948 | return isolate->heap()->true_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11949 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11950 | return isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11951 | #endif |
| 11952 | } |
| 11953 | |
| 11954 | |
| 11955 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11956 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CaptureLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11957 | #ifdef LIVE_OBJECT_LIST |
| 11958 | return LiveObjectList::Capture(); |
| 11959 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11960 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11961 | #endif |
| 11962 | } |
| 11963 | |
| 11964 | |
| 11965 | // Deletes the specified live object list. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11966 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11967 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11968 | CONVERT_SMI_ARG_CHECKED(id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11969 | bool success = LiveObjectList::Delete(id); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11970 | return success ? isolate->heap()->true_value() : |
| 11971 | isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11972 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11973 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11974 | #endif |
| 11975 | } |
| 11976 | |
| 11977 | |
| 11978 | // Generates the response to a debugger request for a dump of the objects |
| 11979 | // contained in the difference between the captured live object lists |
| 11980 | // specified by id1 and id2. |
| 11981 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 11982 | // dumped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11983 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DumpLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11984 | #ifdef LIVE_OBJECT_LIST |
| 11985 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 11986 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 11987 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
| 11988 | CONVERT_SMI_ARG_CHECKED(start, 2); |
| 11989 | CONVERT_SMI_ARG_CHECKED(count, 3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11990 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 4); |
| 11991 | EnterDebugger enter_debugger; |
| 11992 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 11993 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11994 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11995 | #endif |
| 11996 | } |
| 11997 | |
| 11998 | |
| 11999 | // Gets the specified object as requested by the debugger. |
| 12000 | // 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] | 12001 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12002 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12003 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12004 | Object* result = LiveObjectList::GetObj(obj_id); |
| 12005 | return result; |
| 12006 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12007 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12008 | #endif |
| 12009 | } |
| 12010 | |
| 12011 | |
| 12012 | // Gets the obj id for the specified address if valid. |
| 12013 | // 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] | 12014 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjId) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12015 | #ifdef LIVE_OBJECT_LIST |
| 12016 | HandleScope scope; |
| 12017 | CONVERT_ARG_CHECKED(String, address, 0); |
| 12018 | Object* result = LiveObjectList::GetObjId(address); |
| 12019 | return result; |
| 12020 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12021 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12022 | #endif |
| 12023 | } |
| 12024 | |
| 12025 | |
| 12026 | // Gets the retainers that references the specified object alive. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12027 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjRetainers) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12028 | #ifdef LIVE_OBJECT_LIST |
| 12029 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12030 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12031 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 12032 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 12033 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 12034 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
| 12035 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 5); |
| 12036 | |
| 12037 | Handle<JSObject> instance_filter; |
| 12038 | if (args[1]->IsJSObject()) { |
| 12039 | instance_filter = args.at<JSObject>(1); |
| 12040 | } |
| 12041 | bool verbose = false; |
| 12042 | if (args[2]->IsBoolean()) { |
| 12043 | verbose = args[2]->IsTrue(); |
| 12044 | } |
| 12045 | int start = 0; |
| 12046 | if (args[3]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12047 | start = args.smi_at(3); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12048 | } |
| 12049 | int limit = Smi::kMaxValue; |
| 12050 | if (args[4]->IsSmi()) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12051 | limit = args.smi_at(4); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12052 | } |
| 12053 | |
| 12054 | return LiveObjectList::GetObjRetainers(obj_id, |
| 12055 | instance_filter, |
| 12056 | verbose, |
| 12057 | start, |
| 12058 | limit, |
| 12059 | filter_obj); |
| 12060 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12061 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12062 | #endif |
| 12063 | } |
| 12064 | |
| 12065 | |
| 12066 | // Gets the reference path between 2 objects. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12067 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLPath) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12068 | #ifdef LIVE_OBJECT_LIST |
| 12069 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12070 | CONVERT_SMI_ARG_CHECKED(obj_id1, 0); |
| 12071 | CONVERT_SMI_ARG_CHECKED(obj_id2, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12072 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 12073 | |
| 12074 | Handle<JSObject> instance_filter; |
| 12075 | if (args[2]->IsJSObject()) { |
| 12076 | instance_filter = args.at<JSObject>(2); |
| 12077 | } |
| 12078 | |
| 12079 | Object* result = |
| 12080 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 12081 | return result; |
| 12082 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12083 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12084 | #endif |
| 12085 | } |
| 12086 | |
| 12087 | |
| 12088 | // Generates the response to a debugger request for a list of all |
| 12089 | // previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12090 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InfoLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12091 | #ifdef LIVE_OBJECT_LIST |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12092 | CONVERT_SMI_ARG_CHECKED(start, 0); |
| 12093 | CONVERT_SMI_ARG_CHECKED(count, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12094 | return LiveObjectList::Info(start, count); |
| 12095 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12096 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12097 | #endif |
| 12098 | } |
| 12099 | |
| 12100 | |
| 12101 | // Gets a dump of the specified object as requested by the debugger. |
| 12102 | // 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] | 12103 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrintLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12104 | #ifdef LIVE_OBJECT_LIST |
| 12105 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12106 | CONVERT_SMI_ARG_CHECKED(obj_id, 0); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12107 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 12108 | return result; |
| 12109 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12110 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12111 | #endif |
| 12112 | } |
| 12113 | |
| 12114 | |
| 12115 | // Resets and releases all previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12116 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ResetLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12117 | #ifdef LIVE_OBJECT_LIST |
| 12118 | LiveObjectList::Reset(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12119 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12120 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12121 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12122 | #endif |
| 12123 | } |
| 12124 | |
| 12125 | |
| 12126 | // Generates the response to a debugger request for a summary of the types |
| 12127 | // of objects in the difference between the captured live object lists |
| 12128 | // specified by id1 and id2. |
| 12129 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 12130 | // summarized. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12131 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SummarizeLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12132 | #ifdef LIVE_OBJECT_LIST |
| 12133 | HandleScope scope; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12134 | CONVERT_SMI_ARG_CHECKED(id1, 0); |
| 12135 | CONVERT_SMI_ARG_CHECKED(id2, 1); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12136 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 2); |
| 12137 | |
| 12138 | EnterDebugger enter_debugger; |
| 12139 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 12140 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12141 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 12142 | #endif |
| 12143 | } |
| 12144 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12145 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 12146 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12147 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12148 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerResume) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12149 | NoHandleAllocation ha; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 12150 | v8::V8::ResumeProfiler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12151 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12152 | } |
| 12153 | |
| 12154 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12155 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerPause) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12156 | NoHandleAllocation ha; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 12157 | v8::V8::PauseProfiler(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12158 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 12159 | } |
| 12160 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 12161 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12162 | // Finds the script object from the script data. NOTE: This operation uses |
| 12163 | // heap traversal to find the function generated for the source position |
| 12164 | // for the requested break point. For lazily compiled functions several heap |
| 12165 | // traversals might be required rendering this operation as a rather slow |
| 12166 | // operation. However for setting break points which is normally done through |
| 12167 | // some kind of user interaction the performance is not crucial. |
| 12168 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 12169 | Handle<String> script_name) { |
| 12170 | // Scan the heap for Script objects to find the script with the requested |
| 12171 | // script data. |
| 12172 | Handle<Script> script; |
| 12173 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12174 | HeapObject* obj = NULL; |
| 12175 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12176 | // If a script is found check if it has the script data requested. |
| 12177 | if (obj->IsScript()) { |
| 12178 | if (Script::cast(obj)->name()->IsString()) { |
| 12179 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 12180 | script = Handle<Script>(Script::cast(obj)); |
| 12181 | } |
| 12182 | } |
| 12183 | } |
| 12184 | } |
| 12185 | |
| 12186 | // 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] | 12187 | if (script.is_null()) return FACTORY->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12188 | |
| 12189 | // Return the script found. |
| 12190 | return GetScriptWrapper(script); |
| 12191 | } |
| 12192 | |
| 12193 | |
| 12194 | // Get the script object from script data. NOTE: Regarding performance |
| 12195 | // see the NOTE for GetScriptFromScriptData. |
| 12196 | // 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] | 12197 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12198 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12199 | |
| 12200 | ASSERT(args.length() == 1); |
| 12201 | |
| 12202 | CONVERT_CHECKED(String, script_name, args[0]); |
| 12203 | |
| 12204 | // Find the requested script. |
| 12205 | Handle<Object> result = |
| 12206 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 12207 | return *result; |
| 12208 | } |
| 12209 | |
| 12210 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12211 | // Determines whether the given stack frame should be displayed in |
| 12212 | // a stack trace. The caller is the error constructor that asked |
| 12213 | // for the stack trace to be collected. The first time a construct |
| 12214 | // call to this function is encountered it is skipped. The seen_caller |
| 12215 | // in/out parameter is used to remember if the caller has been seen |
| 12216 | // yet. |
| 12217 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, Object* caller, |
| 12218 | bool* seen_caller) { |
| 12219 | // Only display JS frames. |
| 12220 | if (!raw_frame->is_java_script()) |
| 12221 | return false; |
| 12222 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 12223 | Object* raw_fun = frame->function(); |
| 12224 | // Not sure when this can happen but skip it just in case. |
| 12225 | if (!raw_fun->IsJSFunction()) |
| 12226 | return false; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12227 | if ((raw_fun == caller) && !(*seen_caller)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12228 | *seen_caller = true; |
| 12229 | return false; |
| 12230 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12231 | // Skip all frames until we've seen the caller. Also, skip the most |
| 12232 | // obvious builtin calls. Some builtin calls (such as Number.ADD |
| 12233 | // which is invoked using 'call') are very difficult to recognize |
| 12234 | // so we're leaving them in for now. |
| 12235 | return *seen_caller && !frame->receiver()->IsJSBuiltinsObject(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12236 | } |
| 12237 | |
| 12238 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12239 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 12240 | // element segments each containing a receiver, function, code and |
| 12241 | // native code offset. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12242 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectStackTrace) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12243 | ASSERT_EQ(args.length(), 2); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12244 | Handle<Object> caller = args.at<Object>(0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12245 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[1]); |
| 12246 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12247 | HandleScope scope(isolate); |
| 12248 | Factory* factory = isolate->factory(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12249 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 12250 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 12251 | int initial_size = Min(limit, 10); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12252 | Handle<FixedArray> elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12253 | factory->NewFixedArrayWithHoles(initial_size * 4); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12254 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 12255 | StackFrameIterator iter(isolate); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12256 | // If the caller parameter is a function we skip frames until we're |
| 12257 | // under it before starting to collect. |
| 12258 | bool seen_caller = !caller->IsJSFunction(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12259 | int cursor = 0; |
| 12260 | int frames_seen = 0; |
| 12261 | while (!iter.done() && frames_seen < limit) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12262 | StackFrame* raw_frame = iter.frame(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 12263 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12264 | frames_seen++; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12265 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 12266 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 12267 | // function. |
| 12268 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12269 | frame->Summarize(&frames); |
| 12270 | for (int i = frames.length() - 1; i >= 0; i--) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12271 | if (cursor + 4 > elements->length()) { |
| 12272 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 12273 | Handle<FixedArray> new_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12274 | factory->NewFixedArrayWithHoles(new_capacity); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12275 | for (int i = 0; i < cursor; i++) { |
| 12276 | new_elements->set(i, elements->get(i)); |
| 12277 | } |
| 12278 | elements = new_elements; |
| 12279 | } |
| 12280 | ASSERT(cursor + 4 <= elements->length()); |
| 12281 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 12282 | Handle<Object> recv = frames[i].receiver(); |
| 12283 | Handle<JSFunction> fun = frames[i].function(); |
| 12284 | Handle<Code> code = frames[i].code(); |
| 12285 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12286 | elements->set(cursor++, *recv); |
| 12287 | elements->set(cursor++, *fun); |
| 12288 | elements->set(cursor++, *code); |
| 12289 | elements->set(cursor++, *offset); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12290 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12291 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 12292 | iter.Advance(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12293 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12294 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12295 | result->set_length(Smi::FromInt(cursor)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 12296 | return *result; |
| 12297 | } |
| 12298 | |
| 12299 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12300 | // Returns V8 version as a string. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12301 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetV8Version) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12302 | ASSERT_EQ(args.length(), 0); |
| 12303 | |
| 12304 | NoHandleAllocation ha; |
| 12305 | |
| 12306 | const char* version_string = v8::V8::GetVersion(); |
| 12307 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12308 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 12309 | NOT_TENURED); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 12310 | } |
| 12311 | |
| 12312 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12313 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Abort) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12314 | ASSERT(args.length() == 2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12315 | OS::PrintError("abort: %s\n", |
| 12316 | reinterpret_cast<char*>(args[0]) + args.smi_at(1)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12317 | isolate->PrintStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12318 | OS::Abort(); |
| 12319 | UNREACHABLE(); |
| 12320 | return NULL; |
| 12321 | } |
| 12322 | |
| 12323 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12324 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFromCache) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12325 | // This is only called from codegen, so checks might be more lax. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12326 | CONVERT_CHECKED(JSFunctionResultCache, cache, args[0]); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12327 | Object* key = args[1]; |
| 12328 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12329 | int finger_index = cache->finger_index(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12330 | Object* o = cache->get(finger_index); |
| 12331 | if (o == key) { |
| 12332 | // The fastest case: hit the same place again. |
| 12333 | return cache->get(finger_index + 1); |
| 12334 | } |
| 12335 | |
| 12336 | for (int i = finger_index - 2; |
| 12337 | i >= JSFunctionResultCache::kEntriesIndex; |
| 12338 | i -= 2) { |
| 12339 | o = cache->get(i); |
| 12340 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12341 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12342 | return cache->get(i + 1); |
| 12343 | } |
| 12344 | } |
| 12345 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12346 | int size = cache->size(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12347 | ASSERT(size <= cache->length()); |
| 12348 | |
| 12349 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 12350 | o = cache->get(i); |
| 12351 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12352 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12353 | return cache->get(i + 1); |
| 12354 | } |
| 12355 | } |
| 12356 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12357 | // 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] | 12358 | HandleScope scope(isolate); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12359 | |
| 12360 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 12361 | Handle<Object> key_handle(key); |
| 12362 | Handle<Object> value; |
| 12363 | { |
| 12364 | Handle<JSFunction> factory(JSFunction::cast( |
| 12365 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 12366 | // TODO(antonm): consider passing a receiver when constructing a cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12367 | Handle<Object> receiver(isolate->global_context()->global()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12368 | // This handle is nor shared, nor used later, so it's safe. |
| 12369 | Object** argv[] = { key_handle.location() }; |
| 12370 | bool pending_exception = false; |
| 12371 | value = Execution::Call(factory, |
| 12372 | receiver, |
| 12373 | 1, |
| 12374 | argv, |
| 12375 | &pending_exception); |
| 12376 | if (pending_exception) return Failure::Exception(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12377 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12378 | |
| 12379 | #ifdef DEBUG |
| 12380 | cache_handle->JSFunctionResultCacheVerify(); |
| 12381 | #endif |
| 12382 | |
| 12383 | // Function invocation may have cleared the cache. Reread all the data. |
| 12384 | finger_index = cache_handle->finger_index(); |
| 12385 | size = cache_handle->size(); |
| 12386 | |
| 12387 | // If we have spare room, put new data into it, otherwise evict post finger |
| 12388 | // entry which is likely to be the least recently used. |
| 12389 | int index = -1; |
| 12390 | if (size < cache_handle->length()) { |
| 12391 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 12392 | index = size; |
| 12393 | } else { |
| 12394 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 12395 | if (index == cache_handle->length()) { |
| 12396 | index = JSFunctionResultCache::kEntriesIndex; |
| 12397 | } |
| 12398 | } |
| 12399 | |
| 12400 | ASSERT(index % 2 == 0); |
| 12401 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 12402 | ASSERT(index < cache_handle->length()); |
| 12403 | |
| 12404 | cache_handle->set(index, *key_handle); |
| 12405 | cache_handle->set(index + 1, *value); |
| 12406 | cache_handle->set_finger_index(index); |
| 12407 | |
| 12408 | #ifdef DEBUG |
| 12409 | cache_handle->JSFunctionResultCacheVerify(); |
| 12410 | #endif |
| 12411 | |
| 12412 | return *value; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12413 | } |
| 12414 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12415 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12416 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewMessageObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12417 | HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12418 | CONVERT_ARG_CHECKED(String, type, 0); |
| 12419 | CONVERT_ARG_CHECKED(JSArray, arguments, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12420 | return *isolate->factory()->NewJSMessageObject( |
| 12421 | type, |
| 12422 | arguments, |
| 12423 | 0, |
| 12424 | 0, |
| 12425 | isolate->factory()->undefined_value(), |
| 12426 | isolate->factory()->undefined_value(), |
| 12427 | isolate->factory()->undefined_value()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12428 | } |
| 12429 | |
| 12430 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12431 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetType) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12432 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12433 | return message->type(); |
| 12434 | } |
| 12435 | |
| 12436 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12437 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetArguments) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12438 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12439 | return message->arguments(); |
| 12440 | } |
| 12441 | |
| 12442 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12443 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetStartPosition) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12444 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12445 | return Smi::FromInt(message->start_position()); |
| 12446 | } |
| 12447 | |
| 12448 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12449 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetScript) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12450 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12451 | return message->script(); |
| 12452 | } |
| 12453 | |
| 12454 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 12455 | #ifdef DEBUG |
| 12456 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 12457 | // Exclude the code in release mode. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12458 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ListNatives) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12459 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12460 | HandleScope scope; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12461 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 12462 | int entry_count = 0 |
| 12463 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 12464 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 12465 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 12466 | #undef COUNT_ENTRY |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12467 | Factory* factory = isolate->factory(); |
| 12468 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12469 | int index = 0; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12470 | bool inline_runtime_functions = false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 12471 | #define ADD_ENTRY(Name, argc, ressize) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12472 | { \ |
| 12473 | HandleScope inner; \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12474 | Handle<String> name; \ |
| 12475 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 12476 | if (inline_runtime_functions) { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12477 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12478 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 12479 | } else { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12480 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12481 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 12482 | } \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12483 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12484 | pair_elements->set(0, *name); \ |
| 12485 | pair_elements->set(1, Smi::FromInt(argc)); \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12486 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12487 | elements->set(index++, *pair); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12488 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12489 | inline_runtime_functions = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12490 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12491 | inline_runtime_functions = true; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12492 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12493 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12494 | #undef ADD_ENTRY |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12495 | ASSERT_EQ(index, entry_count); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12496 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12497 | return *result; |
| 12498 | } |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 12499 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12500 | |
| 12501 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12502 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Log) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12503 | ASSERT(args.length() == 2); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12504 | CONVERT_CHECKED(String, format, args[0]); |
| 12505 | CONVERT_CHECKED(JSArray, elms, args[1]); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12506 | Vector<const char> chars = format->ToAsciiVector(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12507 | LOGGER->LogRuntime(chars, elms); |
| 12508 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12509 | } |
| 12510 | |
| 12511 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IS_VAR) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12513 | UNREACHABLE(); // implemented as macro in the parser |
| 12514 | return NULL; |
| 12515 | } |
| 12516 | |
| 12517 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 12518 | #define ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(Name) \ |
| 12519 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Has##Name) { \ |
| 12520 | CONVERT_CHECKED(JSObject, obj, args[0]); \ |
| 12521 | return isolate->heap()->ToBoolean(obj->Has##Name()); \ |
| 12522 | } |
| 12523 | |
| 12524 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastElements) |
| 12525 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(FastDoubleElements) |
| 12526 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(DictionaryElements) |
| 12527 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalPixelElements) |
| 12528 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalArrayElements) |
| 12529 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalByteElements) |
| 12530 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedByteElements) |
| 12531 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalShortElements) |
| 12532 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedShortElements) |
| 12533 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalIntElements) |
| 12534 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalUnsignedIntElements) |
| 12535 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalFloatElements) |
| 12536 | ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION(ExternalDoubleElements) |
| 12537 | |
| 12538 | #undef ELEMENTS_KIND_CHECK_RUNTIME_FUNCTION |
| 12539 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12540 | // ---------------------------------------------------------------------------- |
| 12541 | // Implementation of Runtime |
| 12542 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12543 | #define F(name, number_of_args, result_size) \ |
| 12544 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 12545 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12546 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12547 | |
| 12548 | #define I(name, number_of_args, result_size) \ |
| 12549 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 12550 | "_" #name, NULL, number_of_args, result_size }, |
| 12551 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12552 | static const Runtime::Function kIntrinsicFunctions[] = { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12553 | RUNTIME_FUNCTION_LIST(F) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12554 | INLINE_FUNCTION_LIST(I) |
| 12555 | INLINE_RUNTIME_FUNCTION_LIST(I) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12556 | }; |
| 12557 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12558 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12559 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 12560 | Object* dictionary) { |
| 12561 | ASSERT(Isolate::Current()->heap() == heap); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12562 | ASSERT(dictionary != NULL); |
| 12563 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 12564 | for (int i = 0; i < kNumFunctions; ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12565 | Object* name_symbol; |
| 12566 | { MaybeObject* maybe_name_symbol = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12567 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12568 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 12569 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12570 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12571 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 12572 | String::cast(name_symbol), |
| 12573 | Smi::FromInt(i), |
| 12574 | PropertyDetails(NONE, NORMAL)); |
| 12575 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 12576 | // Non-recoverable failure. Calling code must restart heap |
| 12577 | // initialization. |
| 12578 | return maybe_dictionary; |
| 12579 | } |
| 12580 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12581 | } |
| 12582 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12583 | } |
| 12584 | |
| 12585 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12586 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 12587 | Heap* heap = name->GetHeap(); |
| 12588 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12589 | if (entry != kNotFound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12590 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12591 | int function_index = Smi::cast(smi_index)->value(); |
| 12592 | return &(kIntrinsicFunctions[function_index]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12593 | } |
| 12594 | return NULL; |
| 12595 | } |
| 12596 | |
| 12597 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12598 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12599 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 12600 | } |
| 12601 | |
| 12602 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12603 | void Runtime::PerformGC(Object* result) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 12604 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12605 | Failure* failure = Failure::cast(result); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12606 | if (failure->IsRetryAfterGC()) { |
| 12607 | // Try to do a garbage collection; ignore it if it fails. The C |
| 12608 | // entry stub will throw an out-of-memory exception in that case. |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 12609 | isolate->heap()->CollectGarbage(failure->allocation_space()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12610 | } else { |
| 12611 | // Handle last resort GC and make sure to allow future allocations |
| 12612 | // to grow the heap without causing GCs (if possible). |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 12613 | isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 12614 | isolate->heap()->CollectAllGarbage(false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12615 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12616 | } |
| 12617 | |
| 12618 | |
| 12619 | } } // namespace v8::internal |