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" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 45 | #include "liveedit.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 46 | #include "liveobjectlist-inl.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 47 | #include "parser.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 49 | #include "runtime-profiler.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 50 | #include "runtime.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | #include "scopeinfo.h" |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 52 | #include "smart-pointer.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 53 | #include "string-search.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 54 | #include "stub-cache.h" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 55 | #include "v8threads.h" |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 56 | #include "vm-state-inl.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 58 | namespace v8 { |
| 59 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | |
| 61 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 62 | #define RUNTIME_ASSERT(value) \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 63 | if (!(value)) return isolate->ThrowIllegalOperation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 64 | |
| 65 | // Cast the given object to a value of the specified type and store |
| 66 | // it in a variable with the given name. If the object is not of the |
| 67 | // expected type call IllegalOperation and return. |
| 68 | #define CONVERT_CHECKED(Type, name, obj) \ |
| 69 | RUNTIME_ASSERT(obj->Is##Type()); \ |
| 70 | Type* name = Type::cast(obj); |
| 71 | |
| 72 | #define CONVERT_ARG_CHECKED(Type, name, index) \ |
| 73 | RUNTIME_ASSERT(args[index]->Is##Type()); \ |
| 74 | Handle<Type> name = args.at<Type>(index); |
| 75 | |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 76 | // Cast the given object to a boolean and store it in a variable with |
| 77 | // the given name. If the object is not a boolean call IllegalOperation |
| 78 | // and return. |
| 79 | #define CONVERT_BOOLEAN_CHECKED(name, obj) \ |
| 80 | RUNTIME_ASSERT(obj->IsBoolean()); \ |
| 81 | bool name = (obj)->IsTrue(); |
| 82 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 83 | // Cast the given object to a Smi and store its value in an int variable |
| 84 | // with the given name. If the object is not a Smi call IllegalOperation |
| 85 | // and return. |
| 86 | #define CONVERT_SMI_CHECKED(name, obj) \ |
| 87 | RUNTIME_ASSERT(obj->IsSmi()); \ |
| 88 | int name = Smi::cast(obj)->value(); |
| 89 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | // Cast the given object to a double and store it in a variable with |
| 91 | // the given name. If the object is not a number (as opposed to |
| 92 | // the number not-a-number) call IllegalOperation and return. |
| 93 | #define CONVERT_DOUBLE_CHECKED(name, obj) \ |
| 94 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 95 | double name = (obj)->Number(); |
| 96 | |
| 97 | // Call the specified converter on the object *comand store the result in |
| 98 | // a variable of the specified type with the given name. If the |
| 99 | // object is not a Number call IllegalOperation and return. |
| 100 | #define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \ |
| 101 | RUNTIME_ASSERT(obj->IsNumber()); \ |
| 102 | type name = NumberTo##Type(obj); |
| 103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 105 | MUST_USE_RESULT static MaybeObject* DeepCopyBoilerplate(Isolate* isolate, |
| 106 | JSObject* boilerplate) { |
| 107 | StackLimitCheck check(isolate); |
| 108 | if (check.HasOverflowed()) return isolate->StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 110 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 111 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | { MaybeObject* maybe_result = heap->CopyJSObject(boilerplate); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 113 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 114 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 115 | JSObject* copy = JSObject::cast(result); |
| 116 | |
| 117 | // Deep copy local properties. |
| 118 | if (copy->HasFastProperties()) { |
| 119 | FixedArray* properties = copy->properties(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 120 | for (int i = 0; i < properties->length(); i++) { |
| 121 | Object* value = properties->get(i); |
| 122 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 123 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 124 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 125 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 126 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 127 | properties->set(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 130 | int nof = copy->map()->inobject_properties(); |
| 131 | for (int i = 0; i < nof; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 132 | Object* value = copy->InObjectPropertyAt(i); |
| 133 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 134 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 135 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 136 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 137 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 138 | copy->InObjectPropertyAtPut(i, result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 142 | { MaybeObject* maybe_result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 143 | heap->AllocateFixedArray(copy->NumberOfLocalProperties(NONE)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 144 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 145 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 146 | FixedArray* names = FixedArray::cast(result); |
| 147 | copy->GetLocalPropertyNames(names, 0); |
| 148 | for (int i = 0; i < names->length(); i++) { |
| 149 | ASSERT(names->get(i)->IsString()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 150 | String* key_string = String::cast(names->get(i)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 151 | PropertyAttributes attributes = |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 152 | copy->GetLocalPropertyAttribute(key_string); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 153 | // Only deep copy fields from the object literal expression. |
| 154 | // In particular, don't try to copy the length attribute of |
| 155 | // an array. |
| 156 | if (attributes != NONE) continue; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 157 | Object* value = |
| 158 | copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 159 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 160 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 161 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 162 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 163 | } |
| 164 | { MaybeObject* maybe_result = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 165 | // Creating object copy for literals. No strict mode needed. |
| 166 | copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 167 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 168 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Deep copy local elements. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 174 | // Pixel elements cannot be created using an object literal. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 175 | ASSERT(!copy->HasExternalArrayElements()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 176 | switch (copy->GetElementsKind()) { |
| 177 | case JSObject::FAST_ELEMENTS: { |
| 178 | FixedArray* elements = FixedArray::cast(copy->elements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 179 | if (elements->map() == heap->fixed_cow_array_map()) { |
| 180 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 181 | #ifdef DEBUG |
| 182 | for (int i = 0; i < elements->length(); i++) { |
| 183 | ASSERT(!elements->get(i)->IsJSObject()); |
| 184 | } |
| 185 | #endif |
| 186 | } else { |
| 187 | for (int i = 0; i < elements->length(); i++) { |
| 188 | Object* value = elements->get(i); |
| 189 | if (value->IsJSObject()) { |
| 190 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 191 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 192 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 193 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 194 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 195 | elements->set(i, result); |
| 196 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 199 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 200 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 201 | case JSObject::DICTIONARY_ELEMENTS: { |
| 202 | NumberDictionary* element_dictionary = copy->element_dictionary(); |
| 203 | int capacity = element_dictionary->Capacity(); |
| 204 | for (int i = 0; i < capacity; i++) { |
| 205 | Object* k = element_dictionary->KeyAt(i); |
| 206 | if (element_dictionary->IsKey(k)) { |
| 207 | Object* value = element_dictionary->ValueAt(i); |
| 208 | if (value->IsJSObject()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 209 | JSObject* js_object = JSObject::cast(value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 210 | { MaybeObject* maybe_result = DeepCopyBoilerplate(isolate, |
| 211 | js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 212 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 213 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 214 | element_dictionary->ValueAtPut(i, result); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | default: |
| 221 | UNREACHABLE(); |
| 222 | break; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 223 | } |
| 224 | return copy; |
| 225 | } |
| 226 | |
| 227 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 228 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CloneLiteralBoilerplate) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 229 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 230 | return DeepCopyBoilerplate(isolate, boilerplate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 234 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CloneShallowLiteralBoilerplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | CONVERT_CHECKED(JSObject, boilerplate, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 236 | return isolate->heap()->CopyJSObject(boilerplate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 240 | static Handle<Map> ComputeObjectLiteralMap( |
| 241 | Handle<Context> context, |
| 242 | Handle<FixedArray> constant_properties, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 243 | bool* is_result_from_cache) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 244 | Isolate* isolate = context->GetIsolate(); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 245 | int properties_length = constant_properties->length(); |
| 246 | int number_of_properties = properties_length / 2; |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 247 | if (FLAG_canonicalize_object_literal_maps) { |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 248 | // Check that there are only symbols and array indices among keys. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 249 | int number_of_symbol_keys = 0; |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 250 | for (int p = 0; p != properties_length; p += 2) { |
| 251 | Object* key = constant_properties->get(p); |
| 252 | uint32_t element_index = 0; |
| 253 | if (key->IsSymbol()) { |
| 254 | number_of_symbol_keys++; |
| 255 | } else if (key->ToArrayIndex(&element_index)) { |
| 256 | // An index key does not require space in the property backing store. |
| 257 | number_of_properties--; |
| 258 | } else { |
| 259 | // Bail out as a non-symbol non-index key makes caching impossible. |
| 260 | // ASSERT to make sure that the if condition after the loop is false. |
| 261 | ASSERT(number_of_symbol_keys != number_of_properties); |
| 262 | break; |
| 263 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 264 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 265 | // If we only have symbols and array indices among keys then we can |
| 266 | // use the map cache in the global context. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 267 | const int kMaxKeys = 10; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 268 | if ((number_of_symbol_keys == number_of_properties) && |
| 269 | (number_of_symbol_keys < kMaxKeys)) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 270 | // Create the fixed array with the key. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 271 | Handle<FixedArray> keys = |
| 272 | isolate->factory()->NewFixedArray(number_of_symbol_keys); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 273 | if (number_of_symbol_keys > 0) { |
| 274 | int index = 0; |
| 275 | for (int p = 0; p < properties_length; p += 2) { |
| 276 | Object* key = constant_properties->get(p); |
| 277 | if (key->IsSymbol()) { |
| 278 | keys->set(index++, key); |
| 279 | } |
| 280 | } |
| 281 | ASSERT(index == number_of_symbol_keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 282 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 283 | *is_result_from_cache = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 284 | return isolate->factory()->ObjectLiteralMapFromCache(context, keys); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 287 | *is_result_from_cache = false; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 288 | return isolate->factory()->CopyMap( |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 289 | Handle<Map>(context->object_function()->initial_map()), |
| 290 | number_of_properties); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 294 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 295 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 296 | Handle<FixedArray> literals, |
| 297 | Handle<FixedArray> constant_properties); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 298 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 299 | |
| 300 | static Handle<Object> CreateObjectLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 301 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 302 | Handle<FixedArray> literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 303 | Handle<FixedArray> constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 304 | bool should_have_fast_elements, |
| 305 | bool has_function_literal) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 306 | // Get the global context from the literals array. This is the |
| 307 | // context in which the function was created and we use the object |
| 308 | // function from this context to create the object literal. We do |
| 309 | // not use the object function from the current global context |
| 310 | // because this might be the object function from another context |
| 311 | // which we should not have access to. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 312 | Handle<Context> context = |
| 313 | Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals)); |
| 314 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 315 | // In case we have function literals, we want the object to be in |
| 316 | // slow properties mode for now. We don't go in the map cache because |
| 317 | // maps with constant functions can't be shared if the functions are |
| 318 | // not the same (which is the common case). |
| 319 | bool is_result_from_cache = false; |
| 320 | Handle<Map> map = has_function_literal |
| 321 | ? Handle<Map>(context->object_function()->initial_map()) |
| 322 | : ComputeObjectLiteralMap(context, |
| 323 | constant_properties, |
| 324 | &is_result_from_cache); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 326 | Handle<JSObject> boilerplate = isolate->factory()->NewJSObjectFromMap(map); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 327 | |
| 328 | // Normalize the elements of the boilerplate to save space if needed. |
| 329 | if (!should_have_fast_elements) NormalizeElements(boilerplate); |
| 330 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 331 | // Add the constant properties to the boilerplate. |
| 332 | int length = constant_properties->length(); |
| 333 | bool should_transform = |
| 334 | !is_result_from_cache && boilerplate->HasFastProperties(); |
| 335 | if (should_transform || has_function_literal) { |
| 336 | // Normalize the properties of object to avoid n^2 behavior |
| 337 | // when extending the object multiple properties. Indicate the number of |
| 338 | // properties to be added. |
| 339 | NormalizeProperties(boilerplate, KEEP_INOBJECT_PROPERTIES, length / 2); |
| 340 | } |
| 341 | |
| 342 | for (int index = 0; index < length; index +=2) { |
| 343 | Handle<Object> key(constant_properties->get(index+0), isolate); |
| 344 | Handle<Object> value(constant_properties->get(index+1), isolate); |
| 345 | if (value->IsFixedArray()) { |
| 346 | // The value contains the constant_properties of a |
| 347 | // simple object or array literal. |
| 348 | Handle<FixedArray> array = Handle<FixedArray>::cast(value); |
| 349 | value = CreateLiteralBoilerplate(isolate, literals, array); |
| 350 | if (value.is_null()) return value; |
| 351 | } |
| 352 | Handle<Object> result; |
| 353 | uint32_t element_index = 0; |
| 354 | if (key->IsSymbol()) { |
| 355 | if (Handle<String>::cast(key)->AsArrayIndex(&element_index)) { |
| 356 | // Array index as string (uint32). |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 357 | result = SetOwnElement(boilerplate, |
| 358 | element_index, |
| 359 | value, |
| 360 | kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 362 | Handle<String> name(String::cast(*key)); |
| 363 | ASSERT(!name->AsArrayIndex(&element_index)); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 364 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 365 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 367 | } else if (key->ToArrayIndex(&element_index)) { |
| 368 | // Array index (uint32). |
| 369 | result = SetOwnElement(boilerplate, |
| 370 | element_index, |
| 371 | value, |
| 372 | kNonStrictMode); |
| 373 | } else { |
| 374 | // Non-uint32 number. |
| 375 | ASSERT(key->IsNumber()); |
| 376 | double num = key->Number(); |
| 377 | char arr[100]; |
| 378 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 379 | const char* str = DoubleToCString(num, buffer); |
| 380 | Handle<String> name = |
| 381 | isolate->factory()->NewStringFromAscii(CStrVector(str)); |
| 382 | result = SetLocalPropertyIgnoreAttributes(boilerplate, name, |
| 383 | value, NONE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 385 | // If setting the property on the boilerplate throws an |
| 386 | // exception, the exception is converted to an empty handle in |
| 387 | // the handle based operations. In that case, we need to |
| 388 | // convert back to an exception. |
| 389 | if (result.is_null()) return result; |
| 390 | } |
| 391 | |
| 392 | // Transform to fast properties if necessary. For object literals with |
| 393 | // containing function literals we defer this operation until after all |
| 394 | // computed properties have been assigned so that we can generate |
| 395 | // constant function properties. |
| 396 | if (should_transform && !has_function_literal) { |
| 397 | TransformToFastProperties(boilerplate, |
| 398 | boilerplate->map()->unused_property_fields()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 401 | return boilerplate; |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 405 | static Handle<Object> CreateArrayLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 406 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 407 | Handle<FixedArray> literals, |
| 408 | Handle<FixedArray> elements) { |
| 409 | // Create the JSArray. |
| 410 | Handle<JSFunction> constructor( |
| 411 | JSFunction::GlobalContextFromLiterals(*literals)->array_function()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 412 | Handle<Object> object = isolate->factory()->NewJSObject(constructor); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 413 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 414 | const bool is_cow = |
| 415 | (elements->map() == isolate->heap()->fixed_cow_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 416 | Handle<FixedArray> copied_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 417 | is_cow ? elements : isolate->factory()->CopyFixedArray(elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 418 | |
| 419 | Handle<FixedArray> content = Handle<FixedArray>::cast(copied_elements); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 420 | if (is_cow) { |
| 421 | #ifdef DEBUG |
| 422 | // Copy-on-write arrays must be shallow (and simple). |
| 423 | for (int i = 0; i < content->length(); i++) { |
| 424 | ASSERT(!content->get(i)->IsFixedArray()); |
| 425 | } |
| 426 | #endif |
| 427 | } else { |
| 428 | for (int i = 0; i < content->length(); i++) { |
| 429 | if (content->get(i)->IsFixedArray()) { |
| 430 | // The value contains the constant_properties of a |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 431 | // simple object or array literal. |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 432 | Handle<FixedArray> fa(FixedArray::cast(content->get(i))); |
| 433 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 434 | CreateLiteralBoilerplate(isolate, literals, fa); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 435 | if (result.is_null()) return result; |
| 436 | content->set(i, *result); |
| 437 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | // Set the elements. |
| 442 | Handle<JSArray>::cast(object)->SetContent(*content); |
| 443 | return object; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | static Handle<Object> CreateLiteralBoilerplate( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 448 | Isolate* isolate, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 449 | Handle<FixedArray> literals, |
| 450 | Handle<FixedArray> array) { |
| 451 | Handle<FixedArray> elements = CompileTimeValue::GetElements(array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 452 | const bool kHasNoFunctionLiteral = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 453 | switch (CompileTimeValue::GetType(array)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 454 | case CompileTimeValue::OBJECT_LITERAL_FAST_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 455 | return CreateObjectLiteralBoilerplate(isolate, |
| 456 | literals, |
| 457 | elements, |
| 458 | true, |
| 459 | kHasNoFunctionLiteral); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 460 | case CompileTimeValue::OBJECT_LITERAL_SLOW_ELEMENTS: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 461 | return CreateObjectLiteralBoilerplate(isolate, |
| 462 | literals, |
| 463 | elements, |
| 464 | false, |
| 465 | kHasNoFunctionLiteral); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 466 | case CompileTimeValue::ARRAY_LITERAL: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 467 | return CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 468 | default: |
| 469 | UNREACHABLE(); |
| 470 | return Handle<Object>::null(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 475 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralBoilerplate) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 476 | // Takes a FixedArray of elements containing the literal elements of |
| 477 | // the array literal and produces JSArray with those elements. |
| 478 | // Additionally takes the literals array of the surrounding function |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 479 | // which contains the context from which to get the Array function |
| 480 | // to use for creating the array literal. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 481 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 482 | ASSERT(args.length() == 3); |
| 483 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 484 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 485 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 486 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 487 | Handle<Object> object = |
| 488 | CreateArrayLiteralBoilerplate(isolate, literals, elements); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 489 | if (object.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 490 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 491 | // Update the functions literal and return the boilerplate. |
| 492 | literals->set(literals_index, *object); |
| 493 | return *object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 497 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 498 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 499 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 500 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 501 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 502 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 503 | CONVERT_SMI_CHECKED(flags, args[3]); |
| 504 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 505 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 506 | |
| 507 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 508 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 509 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 510 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 511 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 512 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 513 | should_have_fast_elements, |
| 514 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 515 | if (boilerplate.is_null()) return Failure::Exception(); |
| 516 | // Update the functions literal and return the boilerplate. |
| 517 | literals->set(literals_index, *boilerplate); |
| 518 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 519 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 523 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateObjectLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 524 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 525 | ASSERT(args.length() == 4); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 526 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 527 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 528 | CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 529 | CONVERT_SMI_CHECKED(flags, args[3]); |
| 530 | bool should_have_fast_elements = (flags & ObjectLiteral::kFastElements) != 0; |
| 531 | bool has_function_literal = (flags & ObjectLiteral::kHasFunction) != 0; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 532 | |
| 533 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 534 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 535 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 536 | boilerplate = CreateObjectLiteralBoilerplate(isolate, |
| 537 | literals, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 538 | constant_properties, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 539 | should_have_fast_elements, |
| 540 | has_function_literal); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 541 | if (boilerplate.is_null()) return Failure::Exception(); |
| 542 | // Update the functions literal and return the boilerplate. |
| 543 | literals->set(literals_index, *boilerplate); |
| 544 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 545 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 549 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 550 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 551 | ASSERT(args.length() == 3); |
| 552 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 553 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 554 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 555 | |
| 556 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 557 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 558 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 559 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 560 | if (boilerplate.is_null()) return Failure::Exception(); |
| 561 | // Update the functions literal and return the boilerplate. |
| 562 | literals->set(literals_index, *boilerplate); |
| 563 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 564 | return DeepCopyBoilerplate(isolate, JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 568 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateArrayLiteralShallow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 569 | HandleScope scope(isolate); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 570 | ASSERT(args.length() == 3); |
| 571 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 572 | CONVERT_SMI_CHECKED(literals_index, args[1]); |
| 573 | CONVERT_ARG_CHECKED(FixedArray, elements, 2); |
| 574 | |
| 575 | // Check if boilerplate exists. If not, create it first. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 576 | Handle<Object> boilerplate(literals->get(literals_index), isolate); |
| 577 | if (*boilerplate == isolate->heap()->undefined_value()) { |
| 578 | boilerplate = CreateArrayLiteralBoilerplate(isolate, literals, elements); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 579 | if (boilerplate.is_null()) return Failure::Exception(); |
| 580 | // Update the functions literal and return the boilerplate. |
| 581 | literals->set(literals_index, *boilerplate); |
| 582 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 583 | if (JSObject::cast(*boilerplate)->elements()->map() == |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 584 | isolate->heap()->fixed_cow_array_map()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 585 | isolate->counters()->cow_arrays_created_runtime()->Increment(); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 586 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 587 | return isolate->heap()->CopyJSObject(JSObject::cast(*boilerplate)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 591 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateJSProxy) { |
| 592 | ASSERT(args.length() == 2); |
| 593 | Object* handler = args[0]; |
| 594 | Object* prototype = args[1]; |
| 595 | Object* used_prototype = |
| 596 | (prototype->IsJSObject() || prototype->IsJSProxy()) ? prototype |
| 597 | : isolate->heap()->null_value(); |
| 598 | return isolate->heap()->AllocateJSProxy(handler, used_prototype); |
| 599 | } |
| 600 | |
| 601 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 602 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateCatchExtensionObject) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 603 | ASSERT(args.length() == 2); |
| 604 | CONVERT_CHECKED(String, key, args[0]); |
| 605 | Object* value = args[1]; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame^] | 606 | ASSERT(!value->IsFailure()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 607 | // Create a catch context extension object. |
| 608 | JSFunction* constructor = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 609 | isolate->context()->global_context()-> |
| 610 | context_extension_function(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 611 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 612 | { MaybeObject* maybe_object = isolate->heap()->AllocateJSObject(constructor); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 613 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 614 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 615 | // Assign the exception value to the catch variable and make sure |
| 616 | // that the catch variable is DontDelete. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 617 | { MaybeObject* maybe_value = |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 618 | // Passing non-strict per ECMA-262 5th Ed. 12.14. Catch, bullet #4. |
| 619 | JSObject::cast(object)->SetProperty( |
| 620 | key, value, DONT_DELETE, kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 621 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 622 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 623 | return object; |
| 624 | } |
| 625 | |
| 626 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 627 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClassOf) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | NoHandleAllocation ha; |
| 629 | ASSERT(args.length() == 1); |
| 630 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 631 | if (!obj->IsJSObject()) return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 632 | return JSObject::cast(obj)->class_name(); |
| 633 | } |
| 634 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 635 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 636 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsInPrototypeChain) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 637 | NoHandleAllocation ha; |
| 638 | ASSERT(args.length() == 2); |
| 639 | // See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8). |
| 640 | Object* O = args[0]; |
| 641 | Object* V = args[1]; |
| 642 | while (true) { |
| 643 | Object* prototype = V->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 644 | if (prototype->IsNull()) return isolate->heap()->false_value(); |
| 645 | if (O == prototype) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 646 | V = prototype; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 651 | // Inserts an object as the hidden prototype of another object. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 652 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetHiddenPrototype) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 653 | NoHandleAllocation ha; |
| 654 | ASSERT(args.length() == 2); |
| 655 | CONVERT_CHECKED(JSObject, jsobject, args[0]); |
| 656 | CONVERT_CHECKED(JSObject, proto, args[1]); |
| 657 | |
| 658 | // Sanity checks. The old prototype (that we are replacing) could |
| 659 | // theoretically be null, but if it is not null then check that we |
| 660 | // didn't already install a hidden prototype here. |
| 661 | RUNTIME_ASSERT(!jsobject->GetPrototype()->IsHeapObject() || |
| 662 | !HeapObject::cast(jsobject->GetPrototype())->map()->is_hidden_prototype()); |
| 663 | RUNTIME_ASSERT(!proto->map()->is_hidden_prototype()); |
| 664 | |
| 665 | // 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] | 666 | Object* map_or_failure; |
| 667 | { MaybeObject* maybe_map_or_failure = proto->map()->CopyDropTransitions(); |
| 668 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 669 | return maybe_map_or_failure; |
| 670 | } |
| 671 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 672 | Map* new_proto_map = Map::cast(map_or_failure); |
| 673 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 674 | { MaybeObject* maybe_map_or_failure = jsobject->map()->CopyDropTransitions(); |
| 675 | if (!maybe_map_or_failure->ToObject(&map_or_failure)) { |
| 676 | return maybe_map_or_failure; |
| 677 | } |
| 678 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 679 | Map* new_map = Map::cast(map_or_failure); |
| 680 | |
| 681 | // Set proto's prototype to be the old prototype of the object. |
| 682 | new_proto_map->set_prototype(jsobject->GetPrototype()); |
| 683 | proto->set_map(new_proto_map); |
| 684 | new_proto_map->set_is_hidden_prototype(); |
| 685 | |
| 686 | // Set the object's prototype to proto. |
| 687 | new_map->set_prototype(proto); |
| 688 | jsobject->set_map(new_map); |
| 689 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 690 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 694 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsConstructCall) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 696 | ASSERT(args.length() == 0); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 697 | JavaScriptFrameIterator it(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 698 | return isolate->heap()->ToBoolean(it.frame()->IsConstructor()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 702 | // Recursively traverses hidden prototypes if property is not found |
| 703 | static void GetOwnPropertyImplementation(JSObject* obj, |
| 704 | String* name, |
| 705 | LookupResult* result) { |
| 706 | obj->LocalLookupRealNamedProperty(name, result); |
| 707 | |
| 708 | if (!result->IsProperty()) { |
| 709 | Object* proto = obj->GetPrototype(); |
| 710 | if (proto->IsJSObject() && |
| 711 | JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 712 | GetOwnPropertyImplementation(JSObject::cast(proto), |
| 713 | name, result); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 718 | static bool CheckAccessException(LookupResult* result, |
| 719 | v8::AccessType access_type) { |
| 720 | if (result->type() == CALLBACKS) { |
| 721 | Object* callback = result->GetCallbackObject(); |
| 722 | if (callback->IsAccessorInfo()) { |
| 723 | AccessorInfo* info = AccessorInfo::cast(callback); |
| 724 | bool can_access = |
| 725 | (access_type == v8::ACCESS_HAS && |
| 726 | (info->all_can_read() || info->all_can_write())) || |
| 727 | (access_type == v8::ACCESS_GET && info->all_can_read()) || |
| 728 | (access_type == v8::ACCESS_SET && info->all_can_write()); |
| 729 | return can_access; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | |
| 737 | static bool CheckAccess(JSObject* obj, |
| 738 | String* name, |
| 739 | LookupResult* result, |
| 740 | v8::AccessType access_type) { |
| 741 | ASSERT(result->IsProperty()); |
| 742 | |
| 743 | JSObject* holder = result->holder(); |
| 744 | JSObject* current = obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 745 | Isolate* isolate = obj->GetIsolate(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 746 | while (true) { |
| 747 | if (current->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 748 | !isolate->MayNamedAccess(current, name, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 749 | // Access check callback denied the access, but some properties |
| 750 | // can have a special permissions which override callbacks descision |
| 751 | // (currently see v8::AccessControl). |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | if (current == holder) { |
| 756 | return true; |
| 757 | } |
| 758 | |
| 759 | current = JSObject::cast(current->GetPrototype()); |
| 760 | } |
| 761 | |
| 762 | // API callbacks can have per callback access exceptions. |
| 763 | switch (result->type()) { |
| 764 | case CALLBACKS: { |
| 765 | if (CheckAccessException(result, access_type)) { |
| 766 | return true; |
| 767 | } |
| 768 | break; |
| 769 | } |
| 770 | case INTERCEPTOR: { |
| 771 | // If the object has an interceptor, try real named properties. |
| 772 | // Overwrite the result to fetch the correct property later. |
| 773 | holder->LookupRealNamedProperty(name, result); |
| 774 | if (result->IsProperty()) { |
| 775 | if (CheckAccessException(result, access_type)) { |
| 776 | return true; |
| 777 | } |
| 778 | } |
| 779 | break; |
| 780 | } |
| 781 | default: |
| 782 | break; |
| 783 | } |
| 784 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 785 | isolate->ReportFailedAccessCheck(current, access_type); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | // TODO(1095): we should traverse hidden prototype hierachy as well. |
| 791 | static bool CheckElementAccess(JSObject* obj, |
| 792 | uint32_t index, |
| 793 | v8::AccessType access_type) { |
| 794 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 795 | !obj->GetIsolate()->MayIndexedAccess(obj, index, access_type)) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 796 | return false; |
| 797 | } |
| 798 | |
| 799 | return true; |
| 800 | } |
| 801 | |
| 802 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 803 | // Enumerator used as indices into the array returned from GetOwnProperty |
| 804 | enum PropertyDescriptorIndices { |
| 805 | IS_ACCESSOR_INDEX, |
| 806 | VALUE_INDEX, |
| 807 | GETTER_INDEX, |
| 808 | SETTER_INDEX, |
| 809 | WRITABLE_INDEX, |
| 810 | ENUMERABLE_INDEX, |
| 811 | CONFIGURABLE_INDEX, |
| 812 | DESCRIPTOR_SIZE |
| 813 | }; |
| 814 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 815 | // Returns an array with the property description: |
| 816 | // if args[1] is not a property on args[0] |
| 817 | // returns undefined |
| 818 | // if args[1] is a data property on args[0] |
| 819 | // [false, value, Writeable, Enumerable, Configurable] |
| 820 | // if args[1] is an accessor on args[0] |
| 821 | // [true, GetFunction, SetFunction, Enumerable, Configurable] |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 822 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOwnProperty) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 823 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 824 | Heap* heap = isolate->heap(); |
| 825 | HandleScope scope(isolate); |
| 826 | Handle<FixedArray> elms = isolate->factory()->NewFixedArray(DESCRIPTOR_SIZE); |
| 827 | Handle<JSArray> desc = isolate->factory()->NewJSArrayWithElements(elms); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 828 | LookupResult result; |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 829 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 830 | CONVERT_ARG_CHECKED(String, name, 1); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 831 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 832 | // This could be an element. |
| 833 | uint32_t index; |
| 834 | if (name->AsArrayIndex(&index)) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 835 | switch (obj->HasLocalElement(index)) { |
| 836 | case JSObject::UNDEFINED_ELEMENT: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 837 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 838 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 839 | case JSObject::STRING_CHARACTER_ELEMENT: { |
| 840 | // Special handling of string objects according to ECMAScript 5 |
| 841 | // 15.5.5.2. Note that this might be a string object with elements |
| 842 | // other than the actual string value. This is covered by the |
| 843 | // subsequent cases. |
| 844 | Handle<JSValue> js_value = Handle<JSValue>::cast(obj); |
| 845 | Handle<String> str(String::cast(js_value->value())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 846 | Handle<String> substr = SubString(str, index, index + 1, NOT_TENURED); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 847 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 848 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 849 | elms->set(VALUE_INDEX, *substr); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 850 | elms->set(WRITABLE_INDEX, heap->false_value()); |
| 851 | elms->set(ENUMERABLE_INDEX, heap->false_value()); |
| 852 | elms->set(CONFIGURABLE_INDEX, heap->false_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 853 | return *desc; |
| 854 | } |
| 855 | |
| 856 | case JSObject::INTERCEPTED_ELEMENT: |
| 857 | case JSObject::FAST_ELEMENT: { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 858 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 859 | Handle<Object> value = GetElement(obj, index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 860 | RETURN_IF_EMPTY_HANDLE(isolate, value); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 861 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 862 | elms->set(WRITABLE_INDEX, heap->true_value()); |
| 863 | elms->set(ENUMERABLE_INDEX, heap->true_value()); |
| 864 | elms->set(CONFIGURABLE_INDEX, heap->true_value()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 865 | return *desc; |
| 866 | } |
| 867 | |
| 868 | case JSObject::DICTIONARY_ELEMENT: { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 869 | Handle<JSObject> holder = obj; |
| 870 | if (obj->IsJSGlobalProxy()) { |
| 871 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 872 | if (proto->IsNull()) return heap->undefined_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 873 | ASSERT(proto->IsJSGlobalObject()); |
| 874 | holder = Handle<JSObject>(JSObject::cast(proto)); |
| 875 | } |
| 876 | NumberDictionary* dictionary = holder->element_dictionary(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 877 | int entry = dictionary->FindEntry(index); |
| 878 | ASSERT(entry != NumberDictionary::kNotFound); |
| 879 | PropertyDetails details = dictionary->DetailsAt(entry); |
| 880 | switch (details.type()) { |
| 881 | case CALLBACKS: { |
| 882 | // This is an accessor property with getter and/or setter. |
| 883 | FixedArray* callbacks = |
| 884 | FixedArray::cast(dictionary->ValueAt(entry)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 885 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 886 | if (CheckElementAccess(*obj, index, v8::ACCESS_GET)) { |
| 887 | elms->set(GETTER_INDEX, callbacks->get(0)); |
| 888 | } |
| 889 | if (CheckElementAccess(*obj, index, v8::ACCESS_SET)) { |
| 890 | elms->set(SETTER_INDEX, callbacks->get(1)); |
| 891 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 892 | break; |
| 893 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 894 | case NORMAL: { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 895 | // This is a data property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 896 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 897 | Handle<Object> value = GetElement(obj, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 898 | ASSERT(!value.is_null()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 899 | elms->set(VALUE_INDEX, *value); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 900 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!details.IsReadOnly())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 901 | break; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 902 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 903 | default: |
| 904 | UNREACHABLE(); |
| 905 | break; |
| 906 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 907 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!details.IsDontEnum())); |
| 908 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!details.IsDontDelete())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 909 | return *desc; |
| 910 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 914 | // Use recursive implementation to also traverse hidden prototypes |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 915 | GetOwnPropertyImplementation(*obj, *name, &result); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 916 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 917 | if (!result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 918 | return heap->undefined_value(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 919 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 920 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 921 | if (!CheckAccess(*obj, *name, &result, v8::ACCESS_HAS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 922 | return heap->false_value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 923 | } |
| 924 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 925 | elms->set(ENUMERABLE_INDEX, heap->ToBoolean(!result.IsDontEnum())); |
| 926 | elms->set(CONFIGURABLE_INDEX, heap->ToBoolean(!result.IsDontDelete())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 927 | |
| 928 | bool is_js_accessor = (result.type() == CALLBACKS) && |
| 929 | (result.GetCallbackObject()->IsFixedArray()); |
| 930 | |
| 931 | if (is_js_accessor) { |
| 932 | // __defineGetter__/__defineSetter__ callback. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 933 | elms->set(IS_ACCESSOR_INDEX, heap->true_value()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 934 | |
| 935 | FixedArray* structure = FixedArray::cast(result.GetCallbackObject()); |
| 936 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_GET)) { |
| 937 | elms->set(GETTER_INDEX, structure->get(0)); |
| 938 | } |
| 939 | if (CheckAccess(*obj, *name, &result, v8::ACCESS_SET)) { |
| 940 | elms->set(SETTER_INDEX, structure->get(1)); |
| 941 | } |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 942 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 943 | elms->set(IS_ACCESSOR_INDEX, heap->false_value()); |
| 944 | elms->set(WRITABLE_INDEX, heap->ToBoolean(!result.IsReadOnly())); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 945 | |
| 946 | PropertyAttributes attrs; |
| 947 | Object* value; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 948 | // GetProperty will check access and report any violations. |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 949 | { MaybeObject* maybe_value = obj->GetProperty(*obj, &result, *name, &attrs); |
| 950 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 951 | } |
| 952 | elms->set(VALUE_INDEX, value); |
| 953 | } |
| 954 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 955 | return *desc; |
| 956 | } |
| 957 | |
| 958 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 959 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PreventExtensions) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 960 | ASSERT(args.length() == 1); |
| 961 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 962 | return obj->PreventExtensions(); |
| 963 | } |
| 964 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 965 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 966 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsExtensible) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 967 | ASSERT(args.length() == 1); |
| 968 | CONVERT_CHECKED(JSObject, obj, args[0]); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 969 | if (obj->IsJSGlobalProxy()) { |
| 970 | Object* proto = obj->GetPrototype(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 971 | if (proto->IsNull()) return isolate->heap()->false_value(); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 972 | ASSERT(proto->IsJSGlobalObject()); |
| 973 | obj = JSObject::cast(proto); |
| 974 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 975 | return obj->map()->is_extensible() ? isolate->heap()->true_value() |
| 976 | : isolate->heap()->false_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 980 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 981 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 982 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 983 | CONVERT_ARG_CHECKED(JSRegExp, re, 0); |
| 984 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 985 | CONVERT_ARG_CHECKED(String, flags, 2); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 986 | Handle<Object> result = RegExpImpl::Compile(re, pattern, flags); |
| 987 | if (result.is_null()) return Failure::Exception(); |
| 988 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 992 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CreateApiFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 993 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 994 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 995 | CONVERT_ARG_CHECKED(FunctionTemplateInfo, data, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 996 | return *isolate->factory()->CreateApiFunction(data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1000 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsTemplate) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1001 | ASSERT(args.length() == 1); |
| 1002 | Object* arg = args[0]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1003 | bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1004 | return isolate->heap()->ToBoolean(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1008 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetTemplateField) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1009 | ASSERT(args.length() == 2); |
| 1010 | CONVERT_CHECKED(HeapObject, templ, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1011 | CONVERT_CHECKED(Smi, field, args[1]); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1012 | int index = field->value(); |
| 1013 | int offset = index * kPointerSize + HeapObject::kHeaderSize; |
| 1014 | InstanceType type = templ->map()->instance_type(); |
| 1015 | RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE || |
| 1016 | type == OBJECT_TEMPLATE_INFO_TYPE); |
| 1017 | RUNTIME_ASSERT(offset > 0); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1018 | if (type == FUNCTION_TEMPLATE_INFO_TYPE) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1019 | RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize); |
| 1020 | } else { |
| 1021 | RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize); |
| 1022 | } |
| 1023 | return *HeapObject::RawField(templ, offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1027 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DisableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1028 | ASSERT(args.length() == 1); |
| 1029 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1030 | Map* old_map = object->map(); |
| 1031 | bool needs_access_checks = old_map->is_access_check_needed(); |
| 1032 | if (needs_access_checks) { |
| 1033 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1034 | Object* new_map; |
| 1035 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1036 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1037 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1038 | |
| 1039 | Map::cast(new_map)->set_is_access_check_needed(false); |
| 1040 | object->set_map(Map::cast(new_map)); |
| 1041 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1042 | return needs_access_checks ? isolate->heap()->true_value() |
| 1043 | : isolate->heap()->false_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1047 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EnableAccessChecks) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1048 | ASSERT(args.length() == 1); |
| 1049 | CONVERT_CHECKED(HeapObject, object, args[0]); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1050 | Map* old_map = object->map(); |
| 1051 | if (!old_map->is_access_check_needed()) { |
| 1052 | // Copy map so it won't interfere constructor's initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1053 | Object* new_map; |
| 1054 | { MaybeObject* maybe_new_map = old_map->CopyDropTransitions(); |
| 1055 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
| 1056 | } |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 1057 | |
| 1058 | Map::cast(new_map)->set_is_access_check_needed(true); |
| 1059 | object->set_map(Map::cast(new_map)); |
| 1060 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1061 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1065 | static Failure* ThrowRedeclarationError(Isolate* isolate, |
| 1066 | const char* type, |
| 1067 | Handle<String> name) { |
| 1068 | HandleScope scope(isolate); |
| 1069 | Handle<Object> type_handle = |
| 1070 | isolate->factory()->NewStringFromAscii(CStrVector(type)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1071 | Handle<Object> args[2] = { type_handle, name }; |
| 1072 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1073 | isolate->factory()->NewTypeError("redeclaration", HandleVector(args, 2)); |
| 1074 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1078 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareGlobals) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1079 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1080 | HandleScope scope(isolate); |
| 1081 | Handle<GlobalObject> global = Handle<GlobalObject>( |
| 1082 | isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1083 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1084 | Handle<Context> context = args.at<Context>(0); |
| 1085 | CONVERT_ARG_CHECKED(FixedArray, pairs, 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1086 | bool is_eval = Smi::cast(args[2])->value() == 1; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1087 | StrictModeFlag strict_mode = |
| 1088 | static_cast<StrictModeFlag>(Smi::cast(args[3])->value()); |
| 1089 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1090 | |
| 1091 | // Compute the property attributes. According to ECMA-262, section |
| 1092 | // 13, page 71, the property must be read-only and |
| 1093 | // non-deletable. However, neither SpiderMonkey nor KJS creates the |
| 1094 | // property as read-only, so we don't either. |
| 1095 | PropertyAttributes base = is_eval ? NONE : DONT_DELETE; |
| 1096 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1097 | // Traverse the name/value pairs and set the properties. |
| 1098 | int length = pairs->length(); |
| 1099 | for (int i = 0; i < length; i += 2) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1100 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1101 | Handle<String> name(String::cast(pairs->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1102 | Handle<Object> value(pairs->get(i + 1), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1103 | |
| 1104 | // We have to declare a global const property. To capture we only |
| 1105 | // assign to it when evaluating the assignment for "const x = |
| 1106 | // <expr>" the initial value is the hole. |
| 1107 | bool is_const_property = value->IsTheHole(); |
| 1108 | |
| 1109 | if (value->IsUndefined() || is_const_property) { |
| 1110 | // Lookup the property in the global object, and don't set the |
| 1111 | // value of the variable if the property is already there. |
| 1112 | LookupResult lookup; |
| 1113 | global->Lookup(*name, &lookup); |
| 1114 | if (lookup.IsProperty()) { |
| 1115 | // Determine if the property is local by comparing the holder |
| 1116 | // against the global object. The information will be used to |
| 1117 | // avoid throwing re-declaration errors when declaring |
| 1118 | // variables or constants that exist in the prototype chain. |
| 1119 | bool is_local = (*global == lookup.holder()); |
| 1120 | // Get the property attributes and determine if the property is |
| 1121 | // read-only. |
| 1122 | PropertyAttributes attributes = global->GetPropertyAttribute(*name); |
| 1123 | bool is_read_only = (attributes & READ_ONLY) != 0; |
| 1124 | if (lookup.type() == INTERCEPTOR) { |
| 1125 | // If the interceptor says the property is there, we |
| 1126 | // just return undefined without overwriting the property. |
| 1127 | // Otherwise, we continue to setting the property. |
| 1128 | if (attributes != ABSENT) { |
| 1129 | // Check if the existing property conflicts with regards to const. |
| 1130 | if (is_local && (is_read_only || is_const_property)) { |
| 1131 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1132 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | }; |
| 1134 | // The property already exists without conflicting: Go to |
| 1135 | // the next declaration. |
| 1136 | continue; |
| 1137 | } |
| 1138 | // Fall-through and introduce the absent property by using |
| 1139 | // SetProperty. |
| 1140 | } else { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1141 | // For const properties, we treat a callback with this name |
| 1142 | // even in the prototype as a conflicting declaration. |
| 1143 | if (is_const_property && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1144 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1145 | } |
| 1146 | // Otherwise, we check for locally conflicting declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1147 | if (is_local && (is_read_only || is_const_property)) { |
| 1148 | const char* type = (is_read_only) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1149 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1150 | } |
| 1151 | // The property already exists without conflicting: Go to |
| 1152 | // the next declaration. |
| 1153 | continue; |
| 1154 | } |
| 1155 | } |
| 1156 | } else { |
| 1157 | // Copy the function and update its context. Use it as value. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1158 | Handle<SharedFunctionInfo> shared = |
| 1159 | Handle<SharedFunctionInfo>::cast(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1161 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 1162 | context, |
| 1163 | TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | value = function; |
| 1165 | } |
| 1166 | |
| 1167 | LookupResult lookup; |
| 1168 | global->LocalLookup(*name, &lookup); |
| 1169 | |
| 1170 | PropertyAttributes attributes = is_const_property |
| 1171 | ? static_cast<PropertyAttributes>(base | READ_ONLY) |
| 1172 | : base; |
| 1173 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1174 | // There's a local property that we need to overwrite because |
| 1175 | // we're either declaring a function or there's an interceptor |
| 1176 | // that claims the property is absent. |
| 1177 | // |
| 1178 | // Check for conflicting re-declarations. We cannot have |
| 1179 | // conflicting types in case of intercepted properties because |
| 1180 | // they are absent. |
| 1181 | if (lookup.IsProperty() && |
| 1182 | (lookup.type() != INTERCEPTOR) && |
| 1183 | (lookup.IsReadOnly() || is_const_property)) { |
| 1184 | const char* type = (lookup.IsReadOnly()) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1185 | return ThrowRedeclarationError(isolate, type, name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1186 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1187 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1188 | // Safari does not allow the invocation of callback setters for |
| 1189 | // function declarations. To mimic this behavior, we do not allow |
| 1190 | // the invocation of setters for function values. This makes a |
| 1191 | // difference for global functions with the same names as event |
| 1192 | // handlers such as "function onload() {}". Firefox does call the |
| 1193 | // onload setter in those case and Safari does not. We follow |
| 1194 | // Safari for compatibility. |
| 1195 | if (value->IsJSFunction()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1196 | // Do not change DONT_DELETE to false from true. |
| 1197 | if (lookup.IsProperty() && (lookup.type() != INTERCEPTOR)) { |
| 1198 | attributes = static_cast<PropertyAttributes>( |
| 1199 | attributes | (lookup.GetAttributes() & DONT_DELETE)); |
| 1200 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1201 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1202 | SetLocalPropertyIgnoreAttributes(global, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1203 | name, |
| 1204 | value, |
| 1205 | attributes)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1206 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1207 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1208 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1209 | name, |
| 1210 | value, |
| 1211 | attributes, |
| 1212 | strict_mode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1215 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1216 | ASSERT(!isolate->has_pending_exception()); |
| 1217 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1221 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeclareContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1222 | HandleScope scope(isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1223 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1224 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1225 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 1226 | Handle<String> name(String::cast(args[1])); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1227 | PropertyAttributes mode = |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1228 | static_cast<PropertyAttributes>(Smi::cast(args[2])->value()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 1229 | RUNTIME_ASSERT(mode == READ_ONLY || mode == NONE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1230 | Handle<Object> initial_value(args[3], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1231 | |
| 1232 | // Declarations are always done in the function context. |
| 1233 | context = Handle<Context>(context->fcontext()); |
| 1234 | |
| 1235 | int index; |
| 1236 | PropertyAttributes attributes; |
| 1237 | ContextLookupFlags flags = DONT_FOLLOW_CHAINS; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1238 | Handle<Object> holder = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1239 | context->Lookup(name, flags, &index, &attributes); |
| 1240 | |
| 1241 | if (attributes != ABSENT) { |
| 1242 | // The name was declared before; check for conflicting |
| 1243 | // re-declarations: This is similar to the code in parser.cc in |
| 1244 | // the AstBuildingParser::Declare function. |
| 1245 | if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) { |
| 1246 | // Functions are not read-only. |
| 1247 | ASSERT(mode != READ_ONLY || initial_value->IsTheHole()); |
| 1248 | const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1249 | return ThrowRedeclarationError(isolate, type, name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | // Initialize it if necessary. |
| 1253 | if (*initial_value != NULL) { |
| 1254 | if (index >= 0) { |
| 1255 | // The variable or constant context slot should always be in |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1256 | // the function context or the arguments object. |
| 1257 | if (holder->IsContext()) { |
| 1258 | ASSERT(holder.is_identical_to(context)); |
| 1259 | if (((attributes & READ_ONLY) == 0) || |
| 1260 | context->get(index)->IsTheHole()) { |
| 1261 | context->set(index, *initial_value); |
| 1262 | } |
| 1263 | } else { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1264 | // The holder is an arguments object. |
| 1265 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1266 | Handle<Object> result = SetElement(arguments, index, initial_value, |
| 1267 | kNonStrictMode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1268 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1269 | } |
| 1270 | } else { |
| 1271 | // 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] | 1272 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1273 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1274 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1275 | SetProperty(context_ext, name, initial_value, |
| 1276 | mode, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1279 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1280 | } else { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1281 | // The property is not in the function context. It needs to be |
| 1282 | // "declared" in the function context's extension context, or in the |
| 1283 | // global context. |
| 1284 | Handle<JSObject> context_ext; |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 1285 | if (context->has_extension()) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1286 | // The function context's extension context exists - use it. |
| 1287 | context_ext = Handle<JSObject>(context->extension()); |
| 1288 | } else { |
| 1289 | // The function context's extension context does not exists - allocate |
| 1290 | // it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1291 | context_ext = isolate->factory()->NewJSObject( |
| 1292 | isolate->context_extension_function()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1293 | // And store it in the extension slot. |
| 1294 | context->set_extension(*context_ext); |
| 1295 | } |
| 1296 | ASSERT(*context_ext != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1297 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1298 | // Declare the property by setting it to the initial value if provided, |
| 1299 | // or undefined, and use the correct mode (e.g. READ_ONLY attribute for |
| 1300 | // constant declarations). |
| 1301 | ASSERT(!context_ext->HasLocalProperty(*name)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1302 | Handle<Object> value(isolate->heap()->undefined_value(), isolate); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1303 | if (*initial_value != NULL) value = initial_value; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1304 | // Declaring a const context slot is a conflicting declaration if |
| 1305 | // there is a callback with that name in a prototype. It is |
| 1306 | // allowed to introduce const variables in |
| 1307 | // JSContextExtensionObjects. They are treated specially in |
| 1308 | // SetProperty and no setters are invoked for those since they are |
| 1309 | // not real JSObjects. |
| 1310 | if (initial_value->IsTheHole() && |
| 1311 | !context_ext->IsJSContextExtensionObject()) { |
| 1312 | LookupResult lookup; |
| 1313 | context_ext->Lookup(*name, &lookup); |
| 1314 | if (lookup.IsProperty() && (lookup.type() == CALLBACKS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1315 | return ThrowRedeclarationError(isolate, "const", name); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1318 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1319 | SetProperty(context_ext, name, value, mode, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1320 | kNonStrictMode)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1323 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1327 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeVarGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1328 | NoHandleAllocation nha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1329 | // args[0] == name |
| 1330 | // args[1] == strict_mode |
| 1331 | // args[2] == value (optional) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Determine if we need to assign to the variable if it already |
| 1334 | // exists (based on the number of arguments). |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1335 | RUNTIME_ASSERT(args.length() == 2 || args.length() == 3); |
| 1336 | bool assign = args.length() == 3; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | |
| 1338 | CONVERT_ARG_CHECKED(String, name, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1339 | GlobalObject* global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1340 | RUNTIME_ASSERT(args[1]->IsSmi()); |
| 1341 | StrictModeFlag strict_mode = |
| 1342 | static_cast<StrictModeFlag>(Smi::cast(args[1])->value()); |
| 1343 | ASSERT(strict_mode == kStrictMode || strict_mode == kNonStrictMode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1344 | |
| 1345 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1346 | // not be deletable. |
| 1347 | PropertyAttributes attributes = DONT_DELETE; |
| 1348 | |
| 1349 | // 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] | 1350 | // there, there is a property with this name in the prototype chain. |
| 1351 | // We follow Safari and Firefox behavior and only set the property |
| 1352 | // locally if there is an explicit initialization value that we have |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 1353 | // to assign to the property. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1354 | // Note that objects can have hidden prototypes, so we need to traverse |
| 1355 | // the whole chain of hidden prototypes to do a 'local' lookup. |
| 1356 | JSObject* real_holder = global; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1357 | LookupResult lookup; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1358 | while (true) { |
| 1359 | real_holder->LocalLookup(*name, &lookup); |
| 1360 | if (lookup.IsProperty()) { |
| 1361 | // Determine if this is a redeclaration of something read-only. |
| 1362 | if (lookup.IsReadOnly()) { |
| 1363 | // If we found readonly property on one of hidden prototypes, |
| 1364 | // just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1365 | if (real_holder != isolate->context()->global()) break; |
| 1366 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | // Determine if this is a redeclaration of an intercepted read-only |
| 1370 | // property and figure out if the property exists at all. |
| 1371 | bool found = true; |
| 1372 | PropertyType type = lookup.type(); |
| 1373 | if (type == INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1374 | HandleScope handle_scope(isolate); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1375 | Handle<JSObject> holder(real_holder); |
| 1376 | PropertyAttributes intercepted = holder->GetPropertyAttribute(*name); |
| 1377 | real_holder = *holder; |
| 1378 | if (intercepted == ABSENT) { |
| 1379 | // The interceptor claims the property isn't there. We need to |
| 1380 | // make sure to introduce it. |
| 1381 | found = false; |
| 1382 | } else if ((intercepted & READ_ONLY) != 0) { |
| 1383 | // The property is present, but read-only. Since we're trying to |
| 1384 | // overwrite it with a variable declaration we must throw a |
| 1385 | // re-declaration error. However if we found readonly property |
| 1386 | // on one of hidden prototypes, just shadow it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1387 | if (real_holder != isolate->context()->global()) break; |
| 1388 | return ThrowRedeclarationError(isolate, "const", name); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | if (found && !assign) { |
| 1393 | // The global property is there and we're not assigning any value |
| 1394 | // to it. Just return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1395 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | // Assign the value (or undefined) to the property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1399 | Object* value = (assign) ? args[2] : isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1400 | return real_holder->SetProperty( |
| 1401 | &lookup, *name, value, attributes, strict_mode); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1402 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1403 | |
| 1404 | Object* proto = real_holder->GetPrototype(); |
| 1405 | if (!proto->IsJSObject()) |
| 1406 | break; |
| 1407 | |
| 1408 | if (!JSObject::cast(proto)->map()->is_hidden_prototype()) |
| 1409 | break; |
| 1410 | |
| 1411 | real_holder = JSObject::cast(proto); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1414 | global = isolate->context()->global(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1415 | if (assign) { |
| 1416 | return global->SetProperty(*name, args[2], attributes, strict_mode); |
| 1417 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1418 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1422 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstGlobal) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1423 | // All constants are declared with an initial value. The name |
| 1424 | // of the constant is the first argument and the initial value |
| 1425 | // is the second. |
| 1426 | RUNTIME_ASSERT(args.length() == 2); |
| 1427 | CONVERT_ARG_CHECKED(String, name, 0); |
| 1428 | Handle<Object> value = args.at<Object>(1); |
| 1429 | |
| 1430 | // Get the current global object from top. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1431 | GlobalObject* global = isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1432 | |
| 1433 | // According to ECMA-262, section 12.2, page 62, the property must |
| 1434 | // not be deletable. Since it's a const, it must be READ_ONLY too. |
| 1435 | PropertyAttributes attributes = |
| 1436 | static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY); |
| 1437 | |
| 1438 | // Lookup the property locally in the global object. If it isn't |
| 1439 | // there, we add the property and take special precautions to always |
| 1440 | // add it as a local property even in case of callbacks in the |
| 1441 | // prototype chain (this rules out using SetProperty). |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1442 | // We use SetLocalPropertyIgnoreAttributes instead |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1443 | LookupResult lookup; |
| 1444 | global->LocalLookup(*name, &lookup); |
| 1445 | if (!lookup.IsProperty()) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1446 | return global->SetLocalPropertyIgnoreAttributes(*name, |
| 1447 | *value, |
| 1448 | attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | // Determine if this is a redeclaration of something not |
| 1452 | // read-only. In case the result is hidden behind an interceptor we |
| 1453 | // need to ask it for the property attributes. |
| 1454 | if (!lookup.IsReadOnly()) { |
| 1455 | if (lookup.type() != INTERCEPTOR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1456 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | PropertyAttributes intercepted = global->GetPropertyAttribute(*name); |
| 1460 | |
| 1461 | // Throw re-declaration error if the intercepted property is present |
| 1462 | // but not read-only. |
| 1463 | if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1464 | return ThrowRedeclarationError(isolate, "var", name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | // Restore global object from context (in case of GC) and continue |
| 1468 | // with setting the value because the property is either absent or |
| 1469 | // read-only. We also have to do redo the lookup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1470 | HandleScope handle_scope(isolate); |
| 1471 | Handle<GlobalObject> global(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1472 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1473 | // 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] | 1474 | // property through an interceptor and only do it if it's |
| 1475 | // uninitialized, e.g. the hole. Nirk... |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1476 | // Passing non-strict mode because the property is writable. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1477 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 1478 | SetProperty(global, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1479 | name, |
| 1480 | value, |
| 1481 | attributes, |
| 1482 | kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1483 | return *value; |
| 1484 | } |
| 1485 | |
| 1486 | // Set the value, but only we're assigning the initial value to a |
| 1487 | // constant. For now, we determine this by checking if the |
| 1488 | // current value is the hole. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1489 | // Strict mode handling not needed (const disallowed in strict mode). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1490 | PropertyType type = lookup.type(); |
| 1491 | if (type == FIELD) { |
| 1492 | FixedArray* properties = global->properties(); |
| 1493 | int index = lookup.GetFieldIndex(); |
| 1494 | if (properties->get(index)->IsTheHole()) { |
| 1495 | properties->set(index, *value); |
| 1496 | } |
| 1497 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1498 | if (global->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1499 | global->SetNormalizedProperty(&lookup, *value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1500 | } |
| 1501 | } else { |
| 1502 | // Ignore re-initialization of constants that have already been |
| 1503 | // assigned a function value. |
| 1504 | ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION); |
| 1505 | } |
| 1506 | |
| 1507 | // Use the set value as the result of the operation. |
| 1508 | return *value; |
| 1509 | } |
| 1510 | |
| 1511 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1512 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InitializeConstContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1513 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1514 | ASSERT(args.length() == 3); |
| 1515 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1516 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1517 | ASSERT(!value->IsTheHole()); |
| 1518 | CONVERT_ARG_CHECKED(Context, context, 1); |
| 1519 | Handle<String> name(String::cast(args[2])); |
| 1520 | |
| 1521 | // Initializations are always done in the function context. |
| 1522 | context = Handle<Context>(context->fcontext()); |
| 1523 | |
| 1524 | int index; |
| 1525 | PropertyAttributes attributes; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1526 | ContextLookupFlags flags = FOLLOW_CHAINS; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1527 | Handle<Object> holder = |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1528 | context->Lookup(name, flags, &index, &attributes); |
| 1529 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1530 | // In most situations, the property introduced by the const |
| 1531 | // declaration should be present in the context extension object. |
| 1532 | // However, because declaration and initialization are separate, the |
| 1533 | // property might have been deleted (if it was introduced by eval) |
| 1534 | // before we reach the initialization point. |
| 1535 | // |
| 1536 | // Example: |
| 1537 | // |
| 1538 | // function f() { eval("delete x; const x;"); } |
| 1539 | // |
| 1540 | // In that case, the initialization behaves like a normal assignment |
| 1541 | // to property 'x'. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1542 | if (index >= 0) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1543 | // Property was found in a context. |
| 1544 | if (holder->IsContext()) { |
| 1545 | // The holder cannot be the function context. If it is, there |
| 1546 | // should have been a const redeclaration error when declaring |
| 1547 | // the const property. |
| 1548 | ASSERT(!holder.is_identical_to(context)); |
| 1549 | if ((attributes & READ_ONLY) == 0) { |
| 1550 | Handle<Context>::cast(holder)->set(index, *value); |
| 1551 | } |
| 1552 | } else { |
| 1553 | // The holder is an arguments object. |
| 1554 | ASSERT((attributes & READ_ONLY) == 0); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1555 | Handle<JSObject> arguments(Handle<JSObject>::cast(holder)); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1556 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1557 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 1558 | SetElement(arguments, index, value, kNonStrictMode)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1559 | } |
| 1560 | return *value; |
| 1561 | } |
| 1562 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1563 | // The property could not be found, we introduce it in the global |
| 1564 | // context. |
| 1565 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1566 | Handle<JSObject> global = Handle<JSObject>( |
| 1567 | isolate->context()->global()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1568 | // Strict mode not needed (const disallowed in strict mode). |
| 1569 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1570 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1571 | SetProperty(global, name, value, NONE, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1572 | return *value; |
| 1573 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1574 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1575 | // The property was present in a context extension object. |
| 1576 | Handle<JSObject> context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1577 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1578 | if (*context_ext == context->extension()) { |
| 1579 | // This is the property that was introduced by the const |
| 1580 | // declaration. Set it if it hasn't been set before. NOTE: We |
| 1581 | // cannot use GetProperty() to get the current value as it |
| 1582 | // 'unholes' the value. |
| 1583 | LookupResult lookup; |
| 1584 | context_ext->LocalLookupRealNamedProperty(*name, &lookup); |
| 1585 | ASSERT(lookup.IsProperty()); // the property was declared |
| 1586 | ASSERT(lookup.IsReadOnly()); // and it was declared as read-only |
| 1587 | |
| 1588 | PropertyType type = lookup.type(); |
| 1589 | if (type == FIELD) { |
| 1590 | FixedArray* properties = context_ext->properties(); |
| 1591 | int index = lookup.GetFieldIndex(); |
| 1592 | if (properties->get(index)->IsTheHole()) { |
| 1593 | properties->set(index, *value); |
| 1594 | } |
| 1595 | } else if (type == NORMAL) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1596 | if (context_ext->GetNormalizedProperty(&lookup)->IsTheHole()) { |
| 1597 | context_ext->SetNormalizedProperty(&lookup, *value); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } else { |
| 1600 | // We should not reach here. Any real, named property should be |
| 1601 | // either a field or a dictionary slot. |
| 1602 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } else { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1605 | // The property was found in a different context extension object. |
| 1606 | // Set it if it is not a read-only property. |
| 1607 | if ((attributes & READ_ONLY) == 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1608 | // Strict mode not needed (const disallowed in strict mode). |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 1609 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1610 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1611 | SetProperty(context_ext, name, value, attributes, kNonStrictMode)); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1612 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1613 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 1614 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1615 | return *value; |
| 1616 | } |
| 1617 | |
| 1618 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1619 | RUNTIME_FUNCTION(MaybeObject*, |
| 1620 | Runtime_OptimizeObjectForAddingMultipleProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1621 | HandleScope scope(isolate); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1622 | ASSERT(args.length() == 2); |
| 1623 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
| 1624 | CONVERT_SMI_CHECKED(properties, args[1]); |
| 1625 | if (object->HasFastProperties()) { |
| 1626 | NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties); |
| 1627 | } |
| 1628 | return *object; |
| 1629 | } |
| 1630 | |
| 1631 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1632 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExec) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1633 | HandleScope scope(isolate); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1634 | ASSERT(args.length() == 4); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1635 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 1636 | CONVERT_ARG_CHECKED(String, subject, 1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1637 | // 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] | 1638 | // length of a string, i.e. it is always a Smi. We check anyway for security. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1639 | CONVERT_SMI_CHECKED(index, args[2]); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1640 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 3); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 1641 | RUNTIME_ASSERT(last_match_info->HasFastElements()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1642 | RUNTIME_ASSERT(index >= 0); |
| 1643 | RUNTIME_ASSERT(index <= subject->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1644 | isolate->counters()->regexp_entry_runtime()->Increment(); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1645 | Handle<Object> result = RegExpImpl::Exec(regexp, |
| 1646 | subject, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1647 | index, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1648 | last_match_info); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 1649 | if (result.is_null()) return Failure::Exception(); |
| 1650 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1654 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpConstructResult) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1655 | ASSERT(args.length() == 3); |
| 1656 | CONVERT_SMI_CHECKED(elements_count, args[0]); |
| 1657 | if (elements_count > JSArray::kMaxFastElementsLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1658 | return isolate->ThrowIllegalOperation(); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1659 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1660 | Object* new_object; |
| 1661 | { MaybeObject* maybe_new_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1662 | isolate->heap()->AllocateFixedArrayWithHoles(elements_count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1663 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1664 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1665 | FixedArray* elements = FixedArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1666 | { MaybeObject* maybe_new_object = isolate->heap()->AllocateRaw( |
| 1667 | JSRegExpResult::kSize, NEW_SPACE, OLD_POINTER_SPACE); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1668 | if (!maybe_new_object->ToObject(&new_object)) return maybe_new_object; |
| 1669 | } |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1670 | { |
| 1671 | AssertNoAllocation no_gc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1672 | HandleScope scope(isolate); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1673 | reinterpret_cast<HeapObject*>(new_object)-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1674 | set_map(isolate->global_context()->regexp_result_map()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1675 | } |
| 1676 | JSArray* array = JSArray::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1677 | array->set_properties(isolate->heap()->empty_fixed_array()); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1678 | array->set_elements(elements); |
| 1679 | array->set_length(Smi::FromInt(elements_count)); |
| 1680 | // Write in-object properties after the length of the array. |
| 1681 | array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, args[1]); |
| 1682 | array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, args[2]); |
| 1683 | return array; |
| 1684 | } |
| 1685 | |
| 1686 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1687 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpInitializeObject) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1688 | AssertNoAllocation no_alloc; |
| 1689 | ASSERT(args.length() == 5); |
| 1690 | CONVERT_CHECKED(JSRegExp, regexp, args[0]); |
| 1691 | CONVERT_CHECKED(String, source, args[1]); |
| 1692 | |
| 1693 | Object* global = args[2]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1694 | if (!global->IsTrue()) global = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1695 | |
| 1696 | Object* ignoreCase = args[3]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1697 | if (!ignoreCase->IsTrue()) ignoreCase = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1698 | |
| 1699 | Object* multiline = args[4]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1700 | if (!multiline->IsTrue()) multiline = isolate->heap()->false_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1701 | |
| 1702 | Map* map = regexp->map(); |
| 1703 | Object* constructor = map->constructor(); |
| 1704 | if (constructor->IsJSFunction() && |
| 1705 | JSFunction::cast(constructor)->initial_map() == map) { |
| 1706 | // If we still have the original map, set in-object properties directly. |
| 1707 | regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, source); |
| 1708 | // TODO(lrn): Consider skipping write barrier on booleans as well. |
| 1709 | // Both true and false should be in oldspace at all times. |
| 1710 | regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, global); |
| 1711 | regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, ignoreCase); |
| 1712 | regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, multiline); |
| 1713 | regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, |
| 1714 | Smi::FromInt(0), |
| 1715 | SKIP_WRITE_BARRIER); |
| 1716 | return regexp; |
| 1717 | } |
| 1718 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1719 | // Map has changed, so use generic, but slower, method. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1720 | PropertyAttributes final = |
| 1721 | static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 1722 | PropertyAttributes writable = |
| 1723 | static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1724 | Heap* heap = isolate->heap(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1725 | MaybeObject* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1726 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->source_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1727 | source, |
| 1728 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1729 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1730 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->global_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1731 | global, |
| 1732 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1733 | ASSERT(!result->IsFailure()); |
| 1734 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1735 | regexp->SetLocalPropertyIgnoreAttributes(heap->ignore_case_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1736 | ignoreCase, |
| 1737 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1738 | ASSERT(!result->IsFailure()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1739 | result = regexp->SetLocalPropertyIgnoreAttributes(heap->multiline_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1740 | multiline, |
| 1741 | final); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1742 | ASSERT(!result->IsFailure()); |
| 1743 | result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1744 | regexp->SetLocalPropertyIgnoreAttributes(heap->last_index_symbol(), |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 1745 | Smi::FromInt(0), |
| 1746 | writable); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1747 | ASSERT(!result->IsFailure()); |
| 1748 | USE(result); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1749 | return regexp; |
| 1750 | } |
| 1751 | |
| 1752 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1753 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinishArrayPrototypeSetup) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1754 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1755 | ASSERT(args.length() == 1); |
| 1756 | CONVERT_ARG_CHECKED(JSArray, prototype, 0); |
| 1757 | // This is necessary to enable fast checks for absence of elements |
| 1758 | // on Array.prototype and below. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1759 | prototype->set_elements(isolate->heap()->empty_fixed_array()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1760 | return Smi::FromInt(0); |
| 1761 | } |
| 1762 | |
| 1763 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1764 | static Handle<JSFunction> InstallBuiltin(Isolate* isolate, |
| 1765 | Handle<JSObject> holder, |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1766 | const char* name, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1767 | Builtins::Name builtin_name) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1768 | Handle<String> key = isolate->factory()->LookupAsciiSymbol(name); |
| 1769 | Handle<Code> code(isolate->builtins()->builtin(builtin_name)); |
| 1770 | Handle<JSFunction> optimized = |
| 1771 | isolate->factory()->NewFunction(key, |
| 1772 | JS_OBJECT_TYPE, |
| 1773 | JSObject::kHeaderSize, |
| 1774 | code, |
| 1775 | false); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1776 | optimized->shared()->DontAdaptArguments(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1777 | SetProperty(holder, key, optimized, NONE, kStrictMode); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1778 | return optimized; |
| 1779 | } |
| 1780 | |
| 1781 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1782 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SpecialArrayFunctions) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1783 | HandleScope scope(isolate); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1784 | ASSERT(args.length() == 1); |
| 1785 | CONVERT_ARG_CHECKED(JSObject, holder, 0); |
| 1786 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1787 | InstallBuiltin(isolate, holder, "pop", Builtins::kArrayPop); |
| 1788 | InstallBuiltin(isolate, holder, "push", Builtins::kArrayPush); |
| 1789 | InstallBuiltin(isolate, holder, "shift", Builtins::kArrayShift); |
| 1790 | InstallBuiltin(isolate, holder, "unshift", Builtins::kArrayUnshift); |
| 1791 | InstallBuiltin(isolate, holder, "slice", Builtins::kArraySlice); |
| 1792 | InstallBuiltin(isolate, holder, "splice", Builtins::kArraySplice); |
| 1793 | InstallBuiltin(isolate, holder, "concat", Builtins::kArrayConcat); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 1794 | |
| 1795 | return *holder; |
| 1796 | } |
| 1797 | |
| 1798 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1799 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetGlobalReceiver) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1800 | // Returns a real global receiver, not one of builtins object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1801 | Context* global_context = |
| 1802 | isolate->context()->global()->global_context(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1803 | return global_context->global()->global_receiver(); |
| 1804 | } |
| 1805 | |
| 1806 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1807 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MaterializeRegExpLiteral) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1808 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1809 | ASSERT(args.length() == 4); |
| 1810 | CONVERT_ARG_CHECKED(FixedArray, literals, 0); |
| 1811 | int index = Smi::cast(args[1])->value(); |
| 1812 | Handle<String> pattern = args.at<String>(2); |
| 1813 | Handle<String> flags = args.at<String>(3); |
| 1814 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 1815 | // Get the RegExp function from the context in the literals array. |
| 1816 | // This is the RegExp function from the context in which the |
| 1817 | // function was created. We do not use the RegExp function from the |
| 1818 | // current global context because this might be the RegExp function |
| 1819 | // 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] | 1820 | Handle<JSFunction> constructor = |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1821 | Handle<JSFunction>( |
| 1822 | JSFunction::GlobalContextFromLiterals(*literals)->regexp_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1823 | // Compute the regular expression literal. |
| 1824 | bool has_pending_exception; |
| 1825 | Handle<Object> regexp = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1826 | RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags, |
| 1827 | &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1828 | if (has_pending_exception) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1829 | ASSERT(isolate->has_pending_exception()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1830 | return Failure::Exception(); |
| 1831 | } |
| 1832 | literals->set(index, *regexp); |
| 1833 | return *regexp; |
| 1834 | } |
| 1835 | |
| 1836 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1837 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1838 | NoHandleAllocation ha; |
| 1839 | ASSERT(args.length() == 1); |
| 1840 | |
| 1841 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1842 | return f->shared()->name(); |
| 1843 | } |
| 1844 | |
| 1845 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1846 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetName) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1847 | NoHandleAllocation ha; |
| 1848 | ASSERT(args.length() == 2); |
| 1849 | |
| 1850 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1851 | CONVERT_CHECKED(String, name, args[1]); |
| 1852 | f->shared()->set_name(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1853 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1857 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionRemovePrototype) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1858 | NoHandleAllocation ha; |
| 1859 | ASSERT(args.length() == 1); |
| 1860 | |
| 1861 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1862 | Object* obj = f->RemovePrototype(); |
| 1863 | if (obj->IsFailure()) return obj; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1864 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1865 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1869 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1870 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1871 | ASSERT(args.length() == 1); |
| 1872 | |
| 1873 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1874 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 1875 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | |
| 1877 | return *GetScriptWrapper(Handle<Script>::cast(script)); |
| 1878 | } |
| 1879 | |
| 1880 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1881 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetSourceCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1882 | NoHandleAllocation ha; |
| 1883 | ASSERT(args.length() == 1); |
| 1884 | |
| 1885 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 1886 | return f->shared()->GetSourceCode(); |
| 1887 | } |
| 1888 | |
| 1889 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1890 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetScriptSourcePosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | NoHandleAllocation ha; |
| 1892 | ASSERT(args.length() == 1); |
| 1893 | |
| 1894 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1895 | int pos = fun->shared()->start_position(); |
| 1896 | return Smi::FromInt(pos); |
| 1897 | } |
| 1898 | |
| 1899 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1900 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetPositionForOffset) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1901 | ASSERT(args.length() == 2); |
| 1902 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1903 | CONVERT_CHECKED(Code, code, args[0]); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1904 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 1905 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1906 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 1907 | |
| 1908 | Address pc = code->address() + offset; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1909 | return Smi::FromInt(code->SourcePosition(pc)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1913 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetInstanceClassName) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1914 | NoHandleAllocation ha; |
| 1915 | ASSERT(args.length() == 2); |
| 1916 | |
| 1917 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1918 | CONVERT_CHECKED(String, name, args[1]); |
| 1919 | fun->SetInstanceClassName(name); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1920 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1924 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetLength) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1925 | NoHandleAllocation ha; |
| 1926 | ASSERT(args.length() == 2); |
| 1927 | |
| 1928 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
| 1929 | CONVERT_CHECKED(Smi, length, args[1]); |
| 1930 | fun->shared()->set_length(length->value()); |
| 1931 | return length; |
| 1932 | } |
| 1933 | |
| 1934 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1935 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionSetPrototype) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1936 | NoHandleAllocation ha; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1937 | ASSERT(args.length() == 2); |
| 1938 | |
| 1939 | CONVERT_CHECKED(JSFunction, fun, args[0]); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1940 | ASSERT(fun->should_have_prototype()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1941 | Object* obj; |
| 1942 | { MaybeObject* maybe_obj = |
| 1943 | Accessors::FunctionSetPrototype(fun, args[1], NULL); |
| 1944 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 1945 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1946 | return args[0]; // return TOS |
| 1947 | } |
| 1948 | |
| 1949 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1950 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsAPIFunction) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1951 | NoHandleAllocation ha; |
| 1952 | ASSERT(args.length() == 1); |
| 1953 | |
| 1954 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1955 | return f->shared()->IsApiFunction() ? isolate->heap()->true_value() |
| 1956 | : isolate->heap()->false_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1959 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1960 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionIsBuiltin) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1961 | NoHandleAllocation ha; |
| 1962 | ASSERT(args.length() == 1); |
| 1963 | |
| 1964 | CONVERT_CHECKED(JSFunction, f, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1965 | return f->IsBuiltin() ? isolate->heap()->true_value() : |
| 1966 | isolate->heap()->false_value(); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1969 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1970 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetCode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1971 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1972 | ASSERT(args.length() == 2); |
| 1973 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1974 | CONVERT_ARG_CHECKED(JSFunction, target, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1975 | Handle<Object> code = args.at<Object>(1); |
| 1976 | |
| 1977 | Handle<Context> context(target->context()); |
| 1978 | |
| 1979 | if (!code->IsNull()) { |
| 1980 | RUNTIME_ASSERT(code->IsJSFunction()); |
| 1981 | Handle<JSFunction> fun = Handle<JSFunction>::cast(code); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1982 | Handle<SharedFunctionInfo> shared(fun->shared()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | return Failure::Exception(); |
| 1986 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1987 | // Since we don't store the source for this we should never |
| 1988 | // optimize this. |
| 1989 | shared->code()->set_optimizable(false); |
| 1990 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1991 | // Set the code, scope info, formal parameter count, |
| 1992 | // and the length of the target function. |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1993 | target->shared()->set_code(shared->code()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1994 | target->ReplaceCode(shared->code()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1995 | target->shared()->set_scope_info(shared->scope_info()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1996 | target->shared()->set_length(shared->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1997 | target->shared()->set_formal_parameter_count( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1998 | shared->formal_parameter_count()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1999 | // Set the source code of the target function to undefined. |
| 2000 | // SetCode is only used for built-in constructors like String, |
| 2001 | // Array, and Object, and some web code |
| 2002 | // doesn't like seeing source code for constructors. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2003 | target->shared()->set_script(isolate->heap()->undefined_value()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2004 | target->shared()->code()->set_optimizable(false); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 2005 | // Clear the optimization hints related to the compiled code as these are no |
| 2006 | // longer valid when the code is overwritten. |
| 2007 | target->shared()->ClearThisPropertyAssignmentsInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2008 | context = Handle<Context>(fun->context()); |
| 2009 | |
| 2010 | // Make sure we get a fresh copy of the literal vector to avoid |
| 2011 | // cross context contamination. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2012 | int number_of_literals = fun->NumberOfLiterals(); |
| 2013 | Handle<FixedArray> literals = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2014 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2015 | if (number_of_literals > 0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2016 | // Insert the object, regexp and array functions in the literals |
| 2017 | // array prefix. These are the functions that will be used when |
| 2018 | // creating object, regexp and array literals. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2019 | literals->set(JSFunction::kLiteralGlobalContextIndex, |
| 2020 | context->global_context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2021 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2022 | // It's okay to skip the write barrier here because the literals |
| 2023 | // are guaranteed to be in old space. |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 2024 | target->set_literals(*literals, SKIP_WRITE_BARRIER); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2025 | target->set_next_function_link(isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | target->set_context(*context); |
| 2029 | return *target; |
| 2030 | } |
| 2031 | |
| 2032 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2033 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetExpectedNumberOfProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2034 | HandleScope scope(isolate); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2035 | ASSERT(args.length() == 2); |
| 2036 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 2037 | CONVERT_SMI_CHECKED(num, args[1]); |
| 2038 | RUNTIME_ASSERT(num >= 0); |
| 2039 | SetExpectedNofProperties(function, num); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2040 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2044 | MUST_USE_RESULT static MaybeObject* CharFromCode(Isolate* isolate, |
| 2045 | Object* char_code) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2046 | uint32_t code; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2047 | if (char_code->ToArrayIndex(&code)) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2048 | if (code <= 0xffff) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2049 | return isolate->heap()->LookupSingleCharacterStringFromCode(code); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2050 | } |
| 2051 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2052 | return isolate->heap()->empty_string(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2056 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCharCodeAt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | NoHandleAllocation ha; |
| 2058 | ASSERT(args.length() == 2); |
| 2059 | |
| 2060 | CONVERT_CHECKED(String, subject, args[0]); |
| 2061 | Object* index = args[1]; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2062 | RUNTIME_ASSERT(index->IsNumber()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2063 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2064 | uint32_t i = 0; |
| 2065 | if (index->IsSmi()) { |
| 2066 | int value = Smi::cast(index)->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2067 | if (value < 0) return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2068 | i = value; |
| 2069 | } else { |
| 2070 | ASSERT(index->IsHeapNumber()); |
| 2071 | double value = HeapNumber::cast(index)->value(); |
| 2072 | i = static_cast<uint32_t>(DoubleToInteger(value)); |
kasperl@chromium.org | 74e4e5e | 2010-01-25 10:15:52 +0000 | [diff] [blame] | 2073 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Flatten the string. If someone wants to get a char at an index |
| 2076 | // in a cons string, it is likely that more indices will be |
| 2077 | // accessed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2078 | Object* flat; |
| 2079 | { MaybeObject* maybe_flat = subject->TryFlatten(); |
| 2080 | if (!maybe_flat->ToObject(&flat)) return maybe_flat; |
| 2081 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2082 | subject = String::cast(flat); |
| 2083 | |
| 2084 | if (i >= static_cast<uint32_t>(subject->length())) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2085 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | return Smi::FromInt(subject->Get(i)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2092 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CharFromCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2093 | NoHandleAllocation ha; |
| 2094 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2095 | return CharFromCode(isolate, args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2098 | |
| 2099 | class FixedArrayBuilder { |
| 2100 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2101 | explicit FixedArrayBuilder(Isolate* isolate, int initial_capacity) |
| 2102 | : array_(isolate->factory()->NewFixedArrayWithHoles(initial_capacity)), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2103 | length_(0) { |
| 2104 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2105 | // extend the array will work. |
| 2106 | ASSERT(initial_capacity > 0); |
| 2107 | } |
| 2108 | |
| 2109 | explicit FixedArrayBuilder(Handle<FixedArray> backing_store) |
| 2110 | : array_(backing_store), |
| 2111 | length_(0) { |
| 2112 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2113 | // extend the array will work. |
| 2114 | ASSERT(backing_store->length() > 0); |
| 2115 | } |
| 2116 | |
| 2117 | bool HasCapacity(int elements) { |
| 2118 | int length = array_->length(); |
| 2119 | int required_length = length_ + elements; |
| 2120 | return (length >= required_length); |
| 2121 | } |
| 2122 | |
| 2123 | void EnsureCapacity(int elements) { |
| 2124 | int length = array_->length(); |
| 2125 | int required_length = length_ + elements; |
| 2126 | if (length < required_length) { |
| 2127 | int new_length = length; |
| 2128 | do { |
| 2129 | new_length *= 2; |
| 2130 | } while (new_length < required_length); |
| 2131 | Handle<FixedArray> extended_array = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2132 | array_->GetIsolate()->factory()->NewFixedArrayWithHoles(new_length); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2133 | array_->CopyTo(0, *extended_array, 0, length_); |
| 2134 | array_ = extended_array; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | void Add(Object* value) { |
| 2139 | ASSERT(length_ < capacity()); |
| 2140 | array_->set(length_, value); |
| 2141 | length_++; |
| 2142 | } |
| 2143 | |
| 2144 | void Add(Smi* value) { |
| 2145 | ASSERT(length_ < capacity()); |
| 2146 | array_->set(length_, value); |
| 2147 | length_++; |
| 2148 | } |
| 2149 | |
| 2150 | Handle<FixedArray> array() { |
| 2151 | return array_; |
| 2152 | } |
| 2153 | |
| 2154 | int length() { |
| 2155 | return length_; |
| 2156 | } |
| 2157 | |
| 2158 | int capacity() { |
| 2159 | return array_->length(); |
| 2160 | } |
| 2161 | |
| 2162 | Handle<JSArray> ToJSArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2163 | Handle<JSArray> result_array = FACTORY->NewJSArrayWithElements(array_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2164 | result_array->set_length(Smi::FromInt(length_)); |
| 2165 | return result_array; |
| 2166 | } |
| 2167 | |
| 2168 | Handle<JSArray> ToJSArray(Handle<JSArray> target_array) { |
| 2169 | target_array->set_elements(*array_); |
| 2170 | target_array->set_length(Smi::FromInt(length_)); |
| 2171 | return target_array; |
| 2172 | } |
| 2173 | |
| 2174 | private: |
| 2175 | Handle<FixedArray> array_; |
| 2176 | int length_; |
| 2177 | }; |
| 2178 | |
| 2179 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2180 | // Forward declarations. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2181 | const int kStringBuilderConcatHelperLengthBits = 11; |
| 2182 | const int kStringBuilderConcatHelperPositionBits = 19; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2183 | |
| 2184 | template <typename schar> |
| 2185 | static inline void StringBuilderConcatHelper(String*, |
| 2186 | schar*, |
| 2187 | FixedArray*, |
| 2188 | int); |
| 2189 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2190 | typedef BitField<int, 0, kStringBuilderConcatHelperLengthBits> |
| 2191 | StringBuilderSubstringLength; |
| 2192 | typedef BitField<int, |
| 2193 | kStringBuilderConcatHelperLengthBits, |
| 2194 | kStringBuilderConcatHelperPositionBits> |
| 2195 | StringBuilderSubstringPosition; |
| 2196 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2197 | |
| 2198 | class ReplacementStringBuilder { |
| 2199 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2200 | ReplacementStringBuilder(Heap* heap, |
| 2201 | Handle<String> subject, |
| 2202 | int estimated_part_count) |
| 2203 | : heap_(heap), |
| 2204 | array_builder_(heap->isolate(), estimated_part_count), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2205 | subject_(subject), |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2206 | character_count_(0), |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2207 | is_ascii_(subject->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2208 | // Require a non-zero initial size. Ensures that doubling the size to |
| 2209 | // extend the array will work. |
| 2210 | ASSERT(estimated_part_count > 0); |
| 2211 | } |
| 2212 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2213 | static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
| 2214 | int from, |
| 2215 | int to) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2216 | ASSERT(from >= 0); |
| 2217 | int length = to - from; |
| 2218 | ASSERT(length > 0); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2219 | if (StringBuilderSubstringLength::is_valid(length) && |
| 2220 | StringBuilderSubstringPosition::is_valid(from)) { |
| 2221 | int encoded_slice = StringBuilderSubstringLength::encode(length) | |
| 2222 | StringBuilderSubstringPosition::encode(from); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2223 | builder->Add(Smi::FromInt(encoded_slice)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2224 | } else { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2225 | // Otherwise encode as two smis. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2226 | builder->Add(Smi::FromInt(-length)); |
| 2227 | builder->Add(Smi::FromInt(from)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2228 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | void EnsureCapacity(int elements) { |
| 2233 | array_builder_.EnsureCapacity(elements); |
| 2234 | } |
| 2235 | |
| 2236 | |
| 2237 | void AddSubjectSlice(int from, int to) { |
| 2238 | AddSubjectSlice(&array_builder_, from, to); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2239 | IncrementCharacterCount(to - from); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | |
| 2243 | void AddString(Handle<String> string) { |
| 2244 | int length = string->length(); |
| 2245 | ASSERT(length > 0); |
| 2246 | AddElement(*string); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2247 | if (!string->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2248 | is_ascii_ = false; |
| 2249 | } |
| 2250 | IncrementCharacterCount(length); |
| 2251 | } |
| 2252 | |
| 2253 | |
| 2254 | Handle<String> ToString() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2255 | if (array_builder_.length() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2256 | return heap_->isolate()->factory()->empty_string(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | Handle<String> joined_string; |
| 2260 | if (is_ascii_) { |
| 2261 | joined_string = NewRawAsciiString(character_count_); |
| 2262 | AssertNoAllocation no_alloc; |
| 2263 | SeqAsciiString* seq = SeqAsciiString::cast(*joined_string); |
| 2264 | char* char_buffer = seq->GetChars(); |
| 2265 | StringBuilderConcatHelper(*subject_, |
| 2266 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2267 | *array_builder_.array(), |
| 2268 | array_builder_.length()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2269 | } else { |
| 2270 | // Non-ASCII. |
| 2271 | joined_string = NewRawTwoByteString(character_count_); |
| 2272 | AssertNoAllocation no_alloc; |
| 2273 | SeqTwoByteString* seq = SeqTwoByteString::cast(*joined_string); |
| 2274 | uc16* char_buffer = seq->GetChars(); |
| 2275 | StringBuilderConcatHelper(*subject_, |
| 2276 | char_buffer, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2277 | *array_builder_.array(), |
| 2278 | array_builder_.length()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2279 | } |
| 2280 | return joined_string; |
| 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | void IncrementCharacterCount(int by) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2285 | if (character_count_ > String::kMaxLength - by) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2286 | V8::FatalProcessOutOfMemory("String.replace result too large."); |
| 2287 | } |
| 2288 | character_count_ += by; |
| 2289 | } |
| 2290 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2291 | Handle<JSArray> GetParts() { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2292 | return array_builder_.ToJSArray(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2293 | } |
kmillikin@chromium.org | d982519 | 2010-03-30 08:36:16 +0000 | [diff] [blame] | 2294 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2295 | private: |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2296 | Handle<String> NewRawAsciiString(int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2297 | CALL_HEAP_FUNCTION(heap_->isolate(), |
| 2298 | heap_->AllocateRawAsciiString(size), String); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | |
| 2302 | Handle<String> NewRawTwoByteString(int size) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2303 | CALL_HEAP_FUNCTION(heap_->isolate(), |
| 2304 | heap_->AllocateRawTwoByteString(size), String); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | |
| 2308 | void AddElement(Object* element) { |
| 2309 | ASSERT(element->IsSmi() || element->IsString()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2310 | ASSERT(array_builder_.capacity() > array_builder_.length()); |
| 2311 | array_builder_.Add(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2314 | Heap* heap_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2315 | FixedArrayBuilder array_builder_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2316 | Handle<String> subject_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2317 | int character_count_; |
| 2318 | bool is_ascii_; |
| 2319 | }; |
| 2320 | |
| 2321 | |
| 2322 | class CompiledReplacement { |
| 2323 | public: |
| 2324 | CompiledReplacement() |
| 2325 | : parts_(1), replacement_substrings_(0) {} |
| 2326 | |
| 2327 | void Compile(Handle<String> replacement, |
| 2328 | int capture_count, |
| 2329 | int subject_length); |
| 2330 | |
| 2331 | void Apply(ReplacementStringBuilder* builder, |
| 2332 | int match_from, |
| 2333 | int match_to, |
| 2334 | Handle<JSArray> last_match_info); |
| 2335 | |
| 2336 | // Number of distinct parts of the replacement pattern. |
| 2337 | int parts() { |
| 2338 | return parts_.length(); |
| 2339 | } |
| 2340 | private: |
| 2341 | enum PartType { |
| 2342 | SUBJECT_PREFIX = 1, |
| 2343 | SUBJECT_SUFFIX, |
| 2344 | SUBJECT_CAPTURE, |
| 2345 | REPLACEMENT_SUBSTRING, |
| 2346 | REPLACEMENT_STRING, |
| 2347 | |
| 2348 | NUMBER_OF_PART_TYPES |
| 2349 | }; |
| 2350 | |
| 2351 | struct ReplacementPart { |
| 2352 | static inline ReplacementPart SubjectMatch() { |
| 2353 | return ReplacementPart(SUBJECT_CAPTURE, 0); |
| 2354 | } |
| 2355 | static inline ReplacementPart SubjectCapture(int capture_index) { |
| 2356 | return ReplacementPart(SUBJECT_CAPTURE, capture_index); |
| 2357 | } |
| 2358 | static inline ReplacementPart SubjectPrefix() { |
| 2359 | return ReplacementPart(SUBJECT_PREFIX, 0); |
| 2360 | } |
| 2361 | static inline ReplacementPart SubjectSuffix(int subject_length) { |
| 2362 | return ReplacementPart(SUBJECT_SUFFIX, subject_length); |
| 2363 | } |
| 2364 | static inline ReplacementPart ReplacementString() { |
| 2365 | return ReplacementPart(REPLACEMENT_STRING, 0); |
| 2366 | } |
| 2367 | static inline ReplacementPart ReplacementSubString(int from, int to) { |
| 2368 | ASSERT(from >= 0); |
| 2369 | ASSERT(to > from); |
| 2370 | return ReplacementPart(-from, to); |
| 2371 | } |
| 2372 | |
| 2373 | // If tag <= 0 then it is the negation of a start index of a substring of |
| 2374 | // the replacement pattern, otherwise it's a value from PartType. |
| 2375 | ReplacementPart(int tag, int data) |
| 2376 | : tag(tag), data(data) { |
| 2377 | // Must be non-positive or a PartType value. |
| 2378 | ASSERT(tag < NUMBER_OF_PART_TYPES); |
| 2379 | } |
| 2380 | // Either a value of PartType or a non-positive number that is |
| 2381 | // the negation of an index into the replacement string. |
| 2382 | int tag; |
| 2383 | // The data value's interpretation depends on the value of tag: |
| 2384 | // tag == SUBJECT_PREFIX || |
| 2385 | // tag == SUBJECT_SUFFIX: data is unused. |
| 2386 | // tag == SUBJECT_CAPTURE: data is the number of the capture. |
| 2387 | // tag == REPLACEMENT_SUBSTRING || |
| 2388 | // tag == REPLACEMENT_STRING: data is index into array of substrings |
| 2389 | // of the replacement string. |
| 2390 | // tag <= 0: Temporary representation of the substring of the replacement |
| 2391 | // string ranging over -tag .. data. |
| 2392 | // Is replaced by REPLACEMENT_{SUB,}STRING when we create the |
| 2393 | // substring objects. |
| 2394 | int data; |
| 2395 | }; |
| 2396 | |
| 2397 | template<typename Char> |
| 2398 | static void ParseReplacementPattern(ZoneList<ReplacementPart>* parts, |
| 2399 | Vector<Char> characters, |
| 2400 | int capture_count, |
| 2401 | int subject_length) { |
| 2402 | int length = characters.length(); |
| 2403 | int last = 0; |
| 2404 | for (int i = 0; i < length; i++) { |
| 2405 | Char c = characters[i]; |
| 2406 | if (c == '$') { |
| 2407 | int next_index = i + 1; |
| 2408 | if (next_index == length) { // No next character! |
| 2409 | break; |
| 2410 | } |
| 2411 | Char c2 = characters[next_index]; |
| 2412 | switch (c2) { |
| 2413 | case '$': |
| 2414 | if (i > last) { |
| 2415 | // There is a substring before. Include the first "$". |
| 2416 | parts->Add(ReplacementPart::ReplacementSubString(last, next_index)); |
| 2417 | last = next_index + 1; // Continue after the second "$". |
| 2418 | } else { |
| 2419 | // Let the next substring start with the second "$". |
| 2420 | last = next_index; |
| 2421 | } |
| 2422 | i = next_index; |
| 2423 | break; |
| 2424 | case '`': |
| 2425 | if (i > last) { |
| 2426 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2427 | } |
| 2428 | parts->Add(ReplacementPart::SubjectPrefix()); |
| 2429 | i = next_index; |
| 2430 | last = i + 1; |
| 2431 | break; |
| 2432 | case '\'': |
| 2433 | if (i > last) { |
| 2434 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2435 | } |
| 2436 | parts->Add(ReplacementPart::SubjectSuffix(subject_length)); |
| 2437 | i = next_index; |
| 2438 | last = i + 1; |
| 2439 | break; |
| 2440 | case '&': |
| 2441 | if (i > last) { |
| 2442 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2443 | } |
| 2444 | parts->Add(ReplacementPart::SubjectMatch()); |
| 2445 | i = next_index; |
| 2446 | last = i + 1; |
| 2447 | break; |
| 2448 | case '0': |
| 2449 | case '1': |
| 2450 | case '2': |
| 2451 | case '3': |
| 2452 | case '4': |
| 2453 | case '5': |
| 2454 | case '6': |
| 2455 | case '7': |
| 2456 | case '8': |
| 2457 | case '9': { |
| 2458 | int capture_ref = c2 - '0'; |
| 2459 | if (capture_ref > capture_count) { |
| 2460 | i = next_index; |
| 2461 | continue; |
| 2462 | } |
| 2463 | int second_digit_index = next_index + 1; |
| 2464 | if (second_digit_index < length) { |
| 2465 | // Peek ahead to see if we have two digits. |
| 2466 | Char c3 = characters[second_digit_index]; |
| 2467 | if ('0' <= c3 && c3 <= '9') { // Double digits. |
| 2468 | int double_digit_ref = capture_ref * 10 + c3 - '0'; |
| 2469 | if (double_digit_ref <= capture_count) { |
| 2470 | next_index = second_digit_index; |
| 2471 | capture_ref = double_digit_ref; |
| 2472 | } |
| 2473 | } |
| 2474 | } |
| 2475 | if (capture_ref > 0) { |
| 2476 | if (i > last) { |
| 2477 | parts->Add(ReplacementPart::ReplacementSubString(last, i)); |
| 2478 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2479 | ASSERT(capture_ref <= capture_count); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2480 | parts->Add(ReplacementPart::SubjectCapture(capture_ref)); |
| 2481 | last = next_index + 1; |
| 2482 | } |
| 2483 | i = next_index; |
| 2484 | break; |
| 2485 | } |
| 2486 | default: |
| 2487 | i = next_index; |
| 2488 | break; |
| 2489 | } |
| 2490 | } |
| 2491 | } |
| 2492 | if (length > last) { |
| 2493 | if (last == 0) { |
| 2494 | parts->Add(ReplacementPart::ReplacementString()); |
| 2495 | } else { |
| 2496 | parts->Add(ReplacementPart::ReplacementSubString(last, length)); |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | ZoneList<ReplacementPart> parts_; |
| 2502 | ZoneList<Handle<String> > replacement_substrings_; |
| 2503 | }; |
| 2504 | |
| 2505 | |
| 2506 | void CompiledReplacement::Compile(Handle<String> replacement, |
| 2507 | int capture_count, |
| 2508 | int subject_length) { |
| 2509 | ASSERT(replacement->IsFlat()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2510 | if (replacement->IsAsciiRepresentation()) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2511 | AssertNoAllocation no_alloc; |
| 2512 | ParseReplacementPattern(&parts_, |
| 2513 | replacement->ToAsciiVector(), |
| 2514 | capture_count, |
| 2515 | subject_length); |
| 2516 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2517 | ASSERT(replacement->IsTwoByteRepresentation()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2518 | AssertNoAllocation no_alloc; |
| 2519 | |
| 2520 | ParseReplacementPattern(&parts_, |
| 2521 | replacement->ToUC16Vector(), |
| 2522 | capture_count, |
| 2523 | subject_length); |
| 2524 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2525 | Isolate* isolate = replacement->GetIsolate(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2526 | // Find substrings of replacement string and create them as String objects. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2527 | int substring_index = 0; |
| 2528 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2529 | int tag = parts_[i].tag; |
| 2530 | if (tag <= 0) { // A replacement string slice. |
| 2531 | int from = -tag; |
| 2532 | int to = parts_[i].data; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2533 | replacement_substrings_.Add( |
| 2534 | isolate->factory()->NewSubString(replacement, from, to)); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2535 | parts_[i].tag = REPLACEMENT_SUBSTRING; |
| 2536 | parts_[i].data = substring_index; |
| 2537 | substring_index++; |
| 2538 | } else if (tag == REPLACEMENT_STRING) { |
| 2539 | replacement_substrings_.Add(replacement); |
| 2540 | parts_[i].data = substring_index; |
| 2541 | substring_index++; |
| 2542 | } |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | |
| 2547 | void CompiledReplacement::Apply(ReplacementStringBuilder* builder, |
| 2548 | int match_from, |
| 2549 | int match_to, |
| 2550 | Handle<JSArray> last_match_info) { |
| 2551 | for (int i = 0, n = parts_.length(); i < n; i++) { |
| 2552 | ReplacementPart part = parts_[i]; |
| 2553 | switch (part.tag) { |
| 2554 | case SUBJECT_PREFIX: |
| 2555 | if (match_from > 0) builder->AddSubjectSlice(0, match_from); |
| 2556 | break; |
| 2557 | case SUBJECT_SUFFIX: { |
| 2558 | int subject_length = part.data; |
| 2559 | if (match_to < subject_length) { |
| 2560 | builder->AddSubjectSlice(match_to, subject_length); |
| 2561 | } |
| 2562 | break; |
| 2563 | } |
| 2564 | case SUBJECT_CAPTURE: { |
| 2565 | int capture = part.data; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2566 | FixedArray* match_info = FixedArray::cast(last_match_info->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2567 | int from = RegExpImpl::GetCapture(match_info, capture * 2); |
| 2568 | int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1); |
| 2569 | if (from >= 0 && to > from) { |
| 2570 | builder->AddSubjectSlice(from, to); |
| 2571 | } |
| 2572 | break; |
| 2573 | } |
| 2574 | case REPLACEMENT_SUBSTRING: |
| 2575 | case REPLACEMENT_STRING: |
| 2576 | builder->AddString(replacement_substrings_[part.data]); |
| 2577 | break; |
| 2578 | default: |
| 2579 | UNREACHABLE(); |
| 2580 | } |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | |
| 2585 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2586 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2587 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2588 | String* subject, |
| 2589 | JSRegExp* regexp, |
| 2590 | String* replacement, |
| 2591 | JSArray* last_match_info) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2592 | ASSERT(subject->IsFlat()); |
| 2593 | ASSERT(replacement->IsFlat()); |
| 2594 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2595 | HandleScope handles(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2596 | |
| 2597 | int length = subject->length(); |
| 2598 | Handle<String> subject_handle(subject); |
| 2599 | Handle<JSRegExp> regexp_handle(regexp); |
| 2600 | Handle<String> replacement_handle(replacement); |
| 2601 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2602 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2603 | subject_handle, |
| 2604 | 0, |
| 2605 | last_match_info_handle); |
| 2606 | if (match.is_null()) { |
| 2607 | return Failure::Exception(); |
| 2608 | } |
| 2609 | if (match->IsNull()) { |
| 2610 | return *subject_handle; |
| 2611 | } |
| 2612 | |
| 2613 | int capture_count = regexp_handle->CaptureCount(); |
| 2614 | |
| 2615 | // CompiledReplacement uses zone allocation. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 2616 | CompilationZoneScope zone(DELETE_ON_EXIT); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2617 | CompiledReplacement compiled_replacement; |
| 2618 | compiled_replacement.Compile(replacement_handle, |
| 2619 | capture_count, |
| 2620 | length); |
| 2621 | |
| 2622 | bool is_global = regexp_handle->GetFlags().is_global(); |
| 2623 | |
| 2624 | // Guessing the number of parts that the final result string is built |
| 2625 | // from. Global regexps can match any number of times, so we guess |
| 2626 | // conservatively. |
| 2627 | int expected_parts = |
| 2628 | (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2629 | ReplacementStringBuilder builder(isolate->heap(), |
| 2630 | subject_handle, |
| 2631 | expected_parts); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2632 | |
| 2633 | // Index of end of last match. |
| 2634 | int prev = 0; |
| 2635 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2636 | // Number of parts added by compiled replacement plus preceeding |
| 2637 | // string and possibly suffix after last match. It is possible for |
| 2638 | // all components to use two elements when encoded as two smis. |
| 2639 | const int parts_added_per_loop = 2 * (compiled_replacement.parts() + 2); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2640 | bool matched = true; |
| 2641 | do { |
| 2642 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2643 | // Increase the capacity of the builder before entering local handle-scope, |
| 2644 | // so its internal buffer can safely allocate a new handle if it grows. |
| 2645 | builder.EnsureCapacity(parts_added_per_loop); |
| 2646 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2647 | HandleScope loop_scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2648 | int start, end; |
| 2649 | { |
| 2650 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2651 | FixedArray* match_info_array = |
| 2652 | FixedArray::cast(last_match_info_handle->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2653 | |
| 2654 | ASSERT_EQ(capture_count * 2 + 2, |
| 2655 | RegExpImpl::GetLastCaptureCount(match_info_array)); |
| 2656 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2657 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2658 | } |
| 2659 | |
| 2660 | if (prev < start) { |
| 2661 | builder.AddSubjectSlice(prev, start); |
| 2662 | } |
| 2663 | compiled_replacement.Apply(&builder, |
| 2664 | start, |
| 2665 | end, |
| 2666 | last_match_info_handle); |
| 2667 | prev = end; |
| 2668 | |
| 2669 | // Only continue checking for global regexps. |
| 2670 | if (!is_global) break; |
| 2671 | |
| 2672 | // Continue from where the match ended, unless it was an empty match. |
| 2673 | int next = end; |
| 2674 | if (start == end) { |
| 2675 | next = end + 1; |
| 2676 | if (next > length) break; |
| 2677 | } |
| 2678 | |
| 2679 | match = RegExpImpl::Exec(regexp_handle, |
| 2680 | subject_handle, |
| 2681 | next, |
| 2682 | last_match_info_handle); |
| 2683 | if (match.is_null()) { |
| 2684 | return Failure::Exception(); |
| 2685 | } |
| 2686 | matched = !match->IsNull(); |
| 2687 | } while (matched); |
| 2688 | |
| 2689 | if (prev < length) { |
| 2690 | builder.AddSubjectSlice(prev, length); |
| 2691 | } |
| 2692 | |
| 2693 | return *(builder.ToString()); |
| 2694 | } |
| 2695 | |
| 2696 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2697 | template <typename ResultSeqString> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2698 | MUST_USE_RESULT static MaybeObject* StringReplaceRegExpWithEmptyString( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2699 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2700 | String* subject, |
| 2701 | JSRegExp* regexp, |
| 2702 | JSArray* last_match_info) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2703 | ASSERT(subject->IsFlat()); |
| 2704 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2705 | HandleScope handles(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2706 | |
| 2707 | Handle<String> subject_handle(subject); |
| 2708 | Handle<JSRegExp> regexp_handle(regexp); |
| 2709 | Handle<JSArray> last_match_info_handle(last_match_info); |
| 2710 | Handle<Object> match = RegExpImpl::Exec(regexp_handle, |
| 2711 | subject_handle, |
| 2712 | 0, |
| 2713 | last_match_info_handle); |
| 2714 | if (match.is_null()) return Failure::Exception(); |
| 2715 | if (match->IsNull()) return *subject_handle; |
| 2716 | |
| 2717 | ASSERT(last_match_info_handle->HasFastElements()); |
| 2718 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2719 | int start, end; |
| 2720 | { |
| 2721 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 2722 | FixedArray* match_info_array = |
| 2723 | FixedArray::cast(last_match_info_handle->elements()); |
| 2724 | |
| 2725 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2726 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2727 | } |
| 2728 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 2729 | int length = subject_handle->length(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2730 | int new_length = length - (end - start); |
| 2731 | if (new_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2732 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2733 | } |
| 2734 | Handle<ResultSeqString> answer; |
| 2735 | if (ResultSeqString::kHasAsciiEncoding) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2736 | answer = Handle<ResultSeqString>::cast( |
| 2737 | isolate->factory()->NewRawAsciiString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2738 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2739 | answer = Handle<ResultSeqString>::cast( |
| 2740 | isolate->factory()->NewRawTwoByteString(new_length)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | // If the regexp isn't global, only match once. |
| 2744 | if (!regexp_handle->GetFlags().is_global()) { |
| 2745 | if (start > 0) { |
| 2746 | String::WriteToFlat(*subject_handle, |
| 2747 | answer->GetChars(), |
| 2748 | 0, |
| 2749 | start); |
| 2750 | } |
| 2751 | if (end < length) { |
| 2752 | String::WriteToFlat(*subject_handle, |
| 2753 | answer->GetChars() + start, |
| 2754 | end, |
| 2755 | length); |
| 2756 | } |
| 2757 | return *answer; |
| 2758 | } |
| 2759 | |
| 2760 | int prev = 0; // Index of end of last match. |
| 2761 | int next = 0; // Start of next search (prev unless last match was empty). |
| 2762 | int position = 0; |
| 2763 | |
| 2764 | do { |
| 2765 | if (prev < start) { |
| 2766 | // Add substring subject[prev;start] to answer string. |
| 2767 | String::WriteToFlat(*subject_handle, |
| 2768 | answer->GetChars() + position, |
| 2769 | prev, |
| 2770 | start); |
| 2771 | position += start - prev; |
| 2772 | } |
| 2773 | prev = end; |
| 2774 | next = end; |
| 2775 | // Continue from where the match ended, unless it was an empty match. |
| 2776 | if (start == end) { |
| 2777 | next++; |
| 2778 | if (next > length) break; |
| 2779 | } |
| 2780 | match = RegExpImpl::Exec(regexp_handle, |
| 2781 | subject_handle, |
| 2782 | next, |
| 2783 | last_match_info_handle); |
| 2784 | if (match.is_null()) return Failure::Exception(); |
| 2785 | if (match->IsNull()) break; |
| 2786 | |
| 2787 | ASSERT(last_match_info_handle->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2788 | HandleScope loop_scope(isolate); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2789 | { |
| 2790 | AssertNoAllocation match_info_array_is_not_in_a_handle; |
| 2791 | FixedArray* match_info_array = |
| 2792 | FixedArray::cast(last_match_info_handle->elements()); |
| 2793 | start = RegExpImpl::GetCapture(match_info_array, 0); |
| 2794 | end = RegExpImpl::GetCapture(match_info_array, 1); |
| 2795 | } |
| 2796 | } while (true); |
| 2797 | |
| 2798 | if (prev < length) { |
| 2799 | // Add substring subject[prev;length] to answer string. |
| 2800 | String::WriteToFlat(*subject_handle, |
| 2801 | answer->GetChars() + position, |
| 2802 | prev, |
| 2803 | length); |
| 2804 | position += length - prev; |
| 2805 | } |
| 2806 | |
| 2807 | if (position == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2808 | return isolate->heap()->empty_string(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | // Shorten string and fill |
| 2812 | int string_size = ResultSeqString::SizeFor(position); |
| 2813 | int allocated_string_size = ResultSeqString::SizeFor(new_length); |
| 2814 | int delta = allocated_string_size - string_size; |
| 2815 | |
| 2816 | answer->set_length(position); |
| 2817 | if (delta == 0) return *answer; |
| 2818 | |
| 2819 | Address end_of_string = answer->address() + string_size; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2820 | isolate->heap()->CreateFillerObjectAt(end_of_string, delta); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2821 | |
| 2822 | return *answer; |
| 2823 | } |
| 2824 | |
| 2825 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2826 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringReplaceRegExpWithString) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2827 | ASSERT(args.length() == 4); |
| 2828 | |
| 2829 | CONVERT_CHECKED(String, subject, args[0]); |
| 2830 | if (!subject->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2831 | Object* flat_subject; |
| 2832 | { MaybeObject* maybe_flat_subject = subject->TryFlatten(); |
| 2833 | if (!maybe_flat_subject->ToObject(&flat_subject)) { |
| 2834 | return maybe_flat_subject; |
| 2835 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2836 | } |
| 2837 | subject = String::cast(flat_subject); |
| 2838 | } |
| 2839 | |
| 2840 | CONVERT_CHECKED(String, replacement, args[2]); |
| 2841 | if (!replacement->IsFlat()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 2842 | Object* flat_replacement; |
| 2843 | { MaybeObject* maybe_flat_replacement = replacement->TryFlatten(); |
| 2844 | if (!maybe_flat_replacement->ToObject(&flat_replacement)) { |
| 2845 | return maybe_flat_replacement; |
| 2846 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2847 | } |
| 2848 | replacement = String::cast(flat_replacement); |
| 2849 | } |
| 2850 | |
| 2851 | CONVERT_CHECKED(JSRegExp, regexp, args[1]); |
| 2852 | CONVERT_CHECKED(JSArray, last_match_info, args[3]); |
| 2853 | |
| 2854 | ASSERT(last_match_info->HasFastElements()); |
| 2855 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2856 | if (replacement->length() == 0) { |
| 2857 | if (subject->HasOnlyAsciiChars()) { |
| 2858 | return StringReplaceRegExpWithEmptyString<SeqAsciiString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2859 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2860 | } else { |
| 2861 | return StringReplaceRegExpWithEmptyString<SeqTwoByteString>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2862 | isolate, subject, regexp, last_match_info); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2866 | return StringReplaceRegExpWithString(isolate, |
| 2867 | subject, |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2868 | regexp, |
| 2869 | replacement, |
| 2870 | last_match_info); |
| 2871 | } |
| 2872 | |
| 2873 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2874 | // Perform string match of pattern on subject, starting at start index. |
| 2875 | // Caller must ensure that 0 <= start_index <= sub->length(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 2876 | // and should check that pat->length() + start_index <= sub->length(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2877 | int Runtime::StringMatch(Isolate* isolate, |
| 2878 | Handle<String> sub, |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2879 | Handle<String> pat, |
| 2880 | int start_index) { |
| 2881 | ASSERT(0 <= start_index); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2882 | ASSERT(start_index <= sub->length()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2883 | |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 2884 | int pattern_length = pat->length(); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2885 | if (pattern_length == 0) return start_index; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2886 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2887 | int subject_length = sub->length(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2888 | if (start_index + pattern_length > subject_length) return -1; |
| 2889 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2890 | if (!sub->IsFlat()) FlattenString(sub); |
| 2891 | if (!pat->IsFlat()) FlattenString(pat); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2892 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2893 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2894 | // Extract flattened substrings of cons strings before determining asciiness. |
| 2895 | String* seq_sub = *sub; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2896 | if (seq_sub->IsConsString()) seq_sub = ConsString::cast(seq_sub)->first(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2897 | String* seq_pat = *pat; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 2898 | if (seq_pat->IsConsString()) seq_pat = ConsString::cast(seq_pat)->first(); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2899 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2900 | // dispatch on type of strings |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2901 | if (seq_pat->IsAsciiRepresentation()) { |
| 2902 | Vector<const char> pat_vector = seq_pat->ToAsciiVector(); |
| 2903 | if (seq_sub->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2904 | return SearchString(isolate, |
| 2905 | seq_sub->ToAsciiVector(), |
| 2906 | pat_vector, |
| 2907 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2908 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2909 | return SearchString(isolate, |
| 2910 | seq_sub->ToUC16Vector(), |
| 2911 | pat_vector, |
| 2912 | start_index); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 2913 | } |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 2914 | Vector<const uc16> pat_vector = seq_pat->ToUC16Vector(); |
| 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); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2928 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2929 | HandleScope scope(isolate); // create a new handle scope |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2930 | ASSERT(args.length() == 3); |
| 2931 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2932 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 2933 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 2934 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2935 | Object* index = args[2]; |
| 2936 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2937 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 2938 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2939 | RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2940 | int position = |
| 2941 | Runtime::StringMatch(isolate, sub, pat, start_index); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2942 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2946 | template <typename schar, typename pchar> |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2947 | static int StringMatchBackwards(Vector<const schar> subject, |
| 2948 | Vector<const pchar> pattern, |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2949 | int idx) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2950 | int pattern_length = pattern.length(); |
| 2951 | ASSERT(pattern_length >= 1); |
| 2952 | ASSERT(idx + pattern_length <= subject.length()); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2953 | |
| 2954 | if (sizeof(schar) == 1 && sizeof(pchar) > 1) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2955 | for (int i = 0; i < pattern_length; i++) { |
| 2956 | uc16 c = pattern[i]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2957 | if (c > String::kMaxAsciiCharCode) { |
| 2958 | return -1; |
| 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2963 | pchar pattern_first_char = pattern[0]; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2964 | for (int i = idx; i >= 0; i--) { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2965 | if (subject[i] != pattern_first_char) continue; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2966 | int j = 1; |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2967 | while (j < pattern_length) { |
| 2968 | if (pattern[j] != subject[i+j]) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2969 | break; |
| 2970 | } |
| 2971 | j++; |
| 2972 | } |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 2973 | if (j == pattern_length) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2974 | return i; |
| 2975 | } |
| 2976 | } |
| 2977 | return -1; |
| 2978 | } |
| 2979 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2980 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLastIndexOf) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2981 | HandleScope scope(isolate); // create a new handle scope |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2982 | ASSERT(args.length() == 3); |
| 2983 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2984 | CONVERT_ARG_CHECKED(String, sub, 0); |
| 2985 | CONVERT_ARG_CHECKED(String, pat, 1); |
| 2986 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2987 | Object* index = args[2]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2988 | uint32_t start_index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2989 | if (!index->ToArrayIndex(&start_index)) return Smi::FromInt(-1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2990 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2991 | uint32_t pat_length = pat->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2992 | uint32_t sub_length = sub->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2993 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2994 | if (start_index + pat_length > sub_length) { |
| 2995 | start_index = sub_length - pat_length; |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 2996 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2997 | |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 2998 | if (pat_length == 0) { |
| 2999 | return Smi::FromInt(start_index); |
| 3000 | } |
| 3001 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 3002 | if (!sub->IsFlat()) FlattenString(sub); |
| 3003 | if (!pat->IsFlat()) FlattenString(pat); |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 3004 | |
| 3005 | AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 3006 | |
| 3007 | int position = -1; |
| 3008 | |
| 3009 | if (pat->IsAsciiRepresentation()) { |
| 3010 | Vector<const char> pat_vector = pat->ToAsciiVector(); |
| 3011 | if (sub->IsAsciiRepresentation()) { |
| 3012 | position = StringMatchBackwards(sub->ToAsciiVector(), |
| 3013 | pat_vector, |
| 3014 | start_index); |
| 3015 | } else { |
| 3016 | position = StringMatchBackwards(sub->ToUC16Vector(), |
| 3017 | pat_vector, |
| 3018 | start_index); |
| 3019 | } |
| 3020 | } else { |
| 3021 | Vector<const uc16> pat_vector = pat->ToUC16Vector(); |
| 3022 | if (sub->IsAsciiRepresentation()) { |
| 3023 | position = StringMatchBackwards(sub->ToAsciiVector(), |
| 3024 | pat_vector, |
| 3025 | start_index); |
| 3026 | } else { |
| 3027 | position = StringMatchBackwards(sub->ToUC16Vector(), |
| 3028 | pat_vector, |
| 3029 | start_index); |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3037 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringLocaleCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3038 | NoHandleAllocation ha; |
| 3039 | ASSERT(args.length() == 2); |
| 3040 | |
| 3041 | CONVERT_CHECKED(String, str1, args[0]); |
| 3042 | CONVERT_CHECKED(String, str2, args[1]); |
| 3043 | |
| 3044 | if (str1 == str2) return Smi::FromInt(0); // Equal. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3045 | int str1_length = str1->length(); |
| 3046 | int str2_length = str2->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3047 | |
| 3048 | // Decide trivial cases without flattening. |
| 3049 | if (str1_length == 0) { |
| 3050 | if (str2_length == 0) return Smi::FromInt(0); // Equal. |
| 3051 | return Smi::FromInt(-str2_length); |
| 3052 | } else { |
| 3053 | if (str2_length == 0) return Smi::FromInt(str1_length); |
| 3054 | } |
| 3055 | |
| 3056 | int end = str1_length < str2_length ? str1_length : str2_length; |
| 3057 | |
| 3058 | // No need to flatten if we are going to find the answer on the first |
| 3059 | // character. At this point we know there is at least one character |
| 3060 | // in each string, due to the trivial case handling above. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3061 | int d = str1->Get(0) - str2->Get(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3062 | if (d != 0) return Smi::FromInt(d); |
| 3063 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3064 | str1->TryFlatten(); |
| 3065 | str2->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3066 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3067 | StringInputBuffer& buf1 = |
| 3068 | *isolate->runtime_state()->string_locale_compare_buf1(); |
| 3069 | StringInputBuffer& buf2 = |
| 3070 | *isolate->runtime_state()->string_locale_compare_buf2(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3071 | |
| 3072 | buf1.Reset(str1); |
| 3073 | buf2.Reset(str2); |
| 3074 | |
| 3075 | for (int i = 0; i < end; i++) { |
| 3076 | uint16_t char1 = buf1.GetNext(); |
| 3077 | uint16_t char2 = buf2.GetNext(); |
| 3078 | if (char1 != char2) return Smi::FromInt(char1 - char2); |
| 3079 | } |
| 3080 | |
| 3081 | return Smi::FromInt(str1_length - str2_length); |
| 3082 | } |
| 3083 | |
| 3084 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3085 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SubString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3086 | NoHandleAllocation ha; |
| 3087 | ASSERT(args.length() == 3); |
| 3088 | |
| 3089 | CONVERT_CHECKED(String, value, args[0]); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 3090 | Object* from = args[1]; |
| 3091 | Object* to = args[2]; |
| 3092 | int start, end; |
| 3093 | // We have a fast integer-only case here to avoid a conversion to double in |
| 3094 | // the common case where from and to are Smis. |
| 3095 | if (from->IsSmi() && to->IsSmi()) { |
| 3096 | start = Smi::cast(from)->value(); |
| 3097 | end = Smi::cast(to)->value(); |
| 3098 | } else { |
| 3099 | CONVERT_DOUBLE_CHECKED(from_number, from); |
| 3100 | CONVERT_DOUBLE_CHECKED(to_number, to); |
| 3101 | start = FastD2I(from_number); |
| 3102 | end = FastD2I(to_number); |
| 3103 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3104 | RUNTIME_ASSERT(end >= start); |
| 3105 | RUNTIME_ASSERT(start >= 0); |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 3106 | RUNTIME_ASSERT(end <= value->length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3107 | isolate->counters()->sub_string_runtime()->Increment(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3108 | return value->SubString(start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3112 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringMatch) { |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3113 | ASSERT_EQ(3, args.length()); |
| 3114 | |
| 3115 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 3116 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 1); |
| 3117 | CONVERT_ARG_CHECKED(JSArray, regexp_info, 2); |
| 3118 | HandleScope handles; |
| 3119 | |
| 3120 | Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info); |
| 3121 | |
| 3122 | if (match.is_null()) { |
| 3123 | return Failure::Exception(); |
| 3124 | } |
| 3125 | if (match->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3126 | return isolate->heap()->null_value(); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3127 | } |
| 3128 | int length = subject->length(); |
| 3129 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3130 | CompilationZoneScope zone_space(DELETE_ON_EXIT); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3131 | ZoneList<int> offsets(8); |
| 3132 | do { |
| 3133 | int start; |
| 3134 | int end; |
| 3135 | { |
| 3136 | AssertNoAllocation no_alloc; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3137 | FixedArray* elements = FixedArray::cast(regexp_info->elements()); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3138 | start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value(); |
| 3139 | end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value(); |
| 3140 | } |
| 3141 | offsets.Add(start); |
| 3142 | offsets.Add(end); |
| 3143 | int index = start < end ? end : end + 1; |
| 3144 | if (index > length) break; |
| 3145 | match = RegExpImpl::Exec(regexp, subject, index, regexp_info); |
| 3146 | if (match.is_null()) { |
| 3147 | return Failure::Exception(); |
| 3148 | } |
| 3149 | } while (!match->IsNull()); |
| 3150 | int matches = offsets.length() / 2; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3151 | Handle<FixedArray> elements = isolate->factory()->NewFixedArray(matches); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3152 | for (int i = 0; i < matches ; i++) { |
| 3153 | int from = offsets.at(i * 2); |
| 3154 | int to = offsets.at(i * 2 + 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3155 | Handle<String> match = isolate->factory()->NewSubString(subject, from, to); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3156 | elements->set(i, *match); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3157 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3158 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 3159 | result->set_length(Smi::FromInt(matches)); |
| 3160 | return *result; |
| 3161 | } |
| 3162 | |
| 3163 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3164 | // Two smis before and after the match, for very long strings. |
| 3165 | const int kMaxBuilderEntriesPerRegExpMatch = 5; |
| 3166 | |
| 3167 | |
| 3168 | static void SetLastMatchInfoNoCaptures(Handle<String> subject, |
| 3169 | Handle<JSArray> last_match_info, |
| 3170 | int match_start, |
| 3171 | int match_end) { |
| 3172 | // Fill last_match_info with a single capture. |
| 3173 | last_match_info->EnsureSize(2 + RegExpImpl::kLastMatchOverhead); |
| 3174 | AssertNoAllocation no_gc; |
| 3175 | FixedArray* elements = FixedArray::cast(last_match_info->elements()); |
| 3176 | RegExpImpl::SetLastCaptureCount(elements, 2); |
| 3177 | RegExpImpl::SetLastInput(elements, *subject); |
| 3178 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3179 | RegExpImpl::SetCapture(elements, 0, match_start); |
| 3180 | RegExpImpl::SetCapture(elements, 1, match_end); |
| 3181 | } |
| 3182 | |
| 3183 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3184 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3185 | static bool SearchStringMultiple(Isolate* isolate, |
| 3186 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3187 | Vector<const PatternChar> pattern, |
| 3188 | String* pattern_string, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3189 | FixedArrayBuilder* builder, |
| 3190 | int* match_pos) { |
| 3191 | int pos = *match_pos; |
| 3192 | int subject_length = subject.length(); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3193 | int pattern_length = pattern.length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3194 | int max_search_start = subject_length - pattern_length; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3195 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3196 | while (pos <= max_search_start) { |
| 3197 | if (!builder->HasCapacity(kMaxBuilderEntriesPerRegExpMatch)) { |
| 3198 | *match_pos = pos; |
| 3199 | return false; |
| 3200 | } |
| 3201 | // Position of end of previous match. |
| 3202 | int match_end = pos + pattern_length; |
| 3203 | int new_pos = search.Search(subject, match_end); |
| 3204 | if (new_pos >= 0) { |
| 3205 | // A match. |
| 3206 | if (new_pos > match_end) { |
| 3207 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3208 | match_end, |
| 3209 | new_pos); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3210 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3211 | pos = new_pos; |
| 3212 | builder->Add(pattern_string); |
| 3213 | } else { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3214 | break; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3215 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3216 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3217 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3218 | if (pos < max_search_start) { |
| 3219 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3220 | pos + pattern_length, |
| 3221 | subject_length); |
| 3222 | } |
| 3223 | *match_pos = pos; |
| 3224 | return true; |
| 3225 | } |
| 3226 | |
| 3227 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3228 | static bool SearchStringMultiple(Isolate* isolate, |
| 3229 | Handle<String> subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3230 | Handle<String> pattern, |
| 3231 | Handle<JSArray> last_match_info, |
| 3232 | FixedArrayBuilder* builder) { |
| 3233 | ASSERT(subject->IsFlat()); |
| 3234 | ASSERT(pattern->IsFlat()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3235 | |
| 3236 | // Treating as if a previous match was before first character. |
| 3237 | int match_pos = -pattern->length(); |
| 3238 | |
| 3239 | for (;;) { // Break when search complete. |
| 3240 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3241 | AssertNoAllocation no_gc; |
| 3242 | if (subject->IsAsciiRepresentation()) { |
| 3243 | Vector<const char> subject_vector = subject->ToAsciiVector(); |
| 3244 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3245 | if (SearchStringMultiple(isolate, |
| 3246 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3247 | pattern->ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3248 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3249 | builder, |
| 3250 | &match_pos)) break; |
| 3251 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3252 | if (SearchStringMultiple(isolate, |
| 3253 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3254 | pattern->ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3255 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3256 | builder, |
| 3257 | &match_pos)) break; |
| 3258 | } |
| 3259 | } else { |
| 3260 | Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
| 3261 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3262 | if (SearchStringMultiple(isolate, |
| 3263 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3264 | pattern->ToAsciiVector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3265 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3266 | builder, |
| 3267 | &match_pos)) break; |
| 3268 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3269 | if (SearchStringMultiple(isolate, |
| 3270 | subject_vector, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3271 | pattern->ToUC16Vector(), |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3272 | *pattern, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3273 | builder, |
| 3274 | &match_pos)) break; |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | if (match_pos >= 0) { |
| 3280 | SetLastMatchInfoNoCaptures(subject, |
| 3281 | last_match_info, |
| 3282 | match_pos, |
| 3283 | match_pos + pattern->length()); |
| 3284 | return true; |
| 3285 | } |
| 3286 | return false; // No matches at all. |
| 3287 | } |
| 3288 | |
| 3289 | |
| 3290 | static RegExpImpl::IrregexpResult SearchRegExpNoCaptureMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3291 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3292 | Handle<String> subject, |
| 3293 | Handle<JSRegExp> regexp, |
| 3294 | Handle<JSArray> last_match_array, |
| 3295 | FixedArrayBuilder* builder) { |
| 3296 | ASSERT(subject->IsFlat()); |
| 3297 | int match_start = -1; |
| 3298 | int match_end = 0; |
| 3299 | int pos = 0; |
| 3300 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3301 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3302 | |
| 3303 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3304 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3305 | int subject_length = subject->length(); |
| 3306 | |
| 3307 | for (;;) { // Break on failure, return on exception. |
| 3308 | RegExpImpl::IrregexpResult result = |
| 3309 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3310 | subject, |
| 3311 | pos, |
| 3312 | register_vector); |
| 3313 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3314 | match_start = register_vector[0]; |
| 3315 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3316 | if (match_end < match_start) { |
| 3317 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3318 | match_end, |
| 3319 | match_start); |
| 3320 | } |
| 3321 | match_end = register_vector[1]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3322 | HandleScope loop_scope(isolate); |
| 3323 | builder->Add(*isolate->factory()->NewSubString(subject, |
| 3324 | match_start, |
| 3325 | match_end)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3326 | if (match_start != match_end) { |
| 3327 | pos = match_end; |
| 3328 | } else { |
| 3329 | pos = match_end + 1; |
| 3330 | if (pos > subject_length) break; |
| 3331 | } |
| 3332 | } else if (result == RegExpImpl::RE_FAILURE) { |
| 3333 | break; |
| 3334 | } else { |
| 3335 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3336 | return result; |
| 3337 | } |
| 3338 | } |
| 3339 | |
| 3340 | if (match_start >= 0) { |
| 3341 | if (match_end < subject_length) { |
| 3342 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3343 | match_end, |
| 3344 | subject_length); |
| 3345 | } |
| 3346 | SetLastMatchInfoNoCaptures(subject, |
| 3347 | last_match_array, |
| 3348 | match_start, |
| 3349 | match_end); |
| 3350 | return RegExpImpl::RE_SUCCESS; |
| 3351 | } else { |
| 3352 | return RegExpImpl::RE_FAILURE; // No matches at all. |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | |
| 3357 | static RegExpImpl::IrregexpResult SearchRegExpMultiple( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3358 | Isolate* isolate, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3359 | Handle<String> subject, |
| 3360 | Handle<JSRegExp> regexp, |
| 3361 | Handle<JSArray> last_match_array, |
| 3362 | FixedArrayBuilder* builder) { |
| 3363 | |
| 3364 | ASSERT(subject->IsFlat()); |
| 3365 | int required_registers = RegExpImpl::IrregexpPrepare(regexp, subject); |
| 3366 | if (required_registers < 0) return RegExpImpl::RE_EXCEPTION; |
| 3367 | |
| 3368 | OffsetsVector registers(required_registers); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3369 | Vector<int32_t> register_vector(registers.vector(), registers.length()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3370 | |
| 3371 | RegExpImpl::IrregexpResult result = |
| 3372 | RegExpImpl::IrregexpExecOnce(regexp, |
| 3373 | subject, |
| 3374 | 0, |
| 3375 | register_vector); |
| 3376 | |
| 3377 | int capture_count = regexp->CaptureCount(); |
| 3378 | int subject_length = subject->length(); |
| 3379 | |
| 3380 | // Position to search from. |
| 3381 | int pos = 0; |
| 3382 | // End of previous match. Differs from pos if match was empty. |
| 3383 | int match_end = 0; |
| 3384 | if (result == RegExpImpl::RE_SUCCESS) { |
| 3385 | // Need to keep a copy of the previous match for creating last_match_info |
| 3386 | // at the end, so we have two vectors that we swap between. |
| 3387 | OffsetsVector registers2(required_registers); |
| 3388 | Vector<int> prev_register_vector(registers2.vector(), registers2.length()); |
| 3389 | |
| 3390 | do { |
| 3391 | int match_start = register_vector[0]; |
| 3392 | builder->EnsureCapacity(kMaxBuilderEntriesPerRegExpMatch); |
| 3393 | if (match_end < match_start) { |
| 3394 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3395 | match_end, |
| 3396 | match_start); |
| 3397 | } |
| 3398 | match_end = register_vector[1]; |
| 3399 | |
| 3400 | { |
| 3401 | // Avoid accumulating new handles inside loop. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3402 | HandleScope temp_scope(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3403 | // Arguments array to replace function is match, captures, index and |
| 3404 | // subject, i.e., 3 + capture count in total. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3405 | Handle<FixedArray> elements = |
| 3406 | isolate->factory()->NewFixedArray(3 + capture_count); |
| 3407 | Handle<String> match = isolate->factory()->NewSubString(subject, |
| 3408 | match_start, |
| 3409 | match_end); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3410 | elements->set(0, *match); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3411 | for (int i = 1; i <= capture_count; i++) { |
| 3412 | int start = register_vector[i * 2]; |
| 3413 | if (start >= 0) { |
| 3414 | int end = register_vector[i * 2 + 1]; |
| 3415 | ASSERT(start <= end); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3416 | Handle<String> substring = isolate->factory()->NewSubString(subject, |
| 3417 | start, |
| 3418 | end); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3419 | elements->set(i, *substring); |
| 3420 | } else { |
| 3421 | ASSERT(register_vector[i * 2 + 1] < 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3422 | elements->set(i, isolate->heap()->undefined_value()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3423 | } |
| 3424 | } |
| 3425 | elements->set(capture_count + 1, Smi::FromInt(match_start)); |
| 3426 | elements->set(capture_count + 2, *subject); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3427 | builder->Add(*isolate->factory()->NewJSArrayWithElements(elements)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3428 | } |
| 3429 | // Swap register vectors, so the last successful match is in |
| 3430 | // prev_register_vector. |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 3431 | Vector<int32_t> tmp = prev_register_vector; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3432 | prev_register_vector = register_vector; |
| 3433 | register_vector = tmp; |
| 3434 | |
| 3435 | if (match_end > match_start) { |
| 3436 | pos = match_end; |
| 3437 | } else { |
| 3438 | pos = match_end + 1; |
| 3439 | if (pos > subject_length) { |
| 3440 | break; |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | result = RegExpImpl::IrregexpExecOnce(regexp, |
| 3445 | subject, |
| 3446 | pos, |
| 3447 | register_vector); |
| 3448 | } while (result == RegExpImpl::RE_SUCCESS); |
| 3449 | |
| 3450 | if (result != RegExpImpl::RE_EXCEPTION) { |
| 3451 | // Finished matching, with at least one match. |
| 3452 | if (match_end < subject_length) { |
| 3453 | ReplacementStringBuilder::AddSubjectSlice(builder, |
| 3454 | match_end, |
| 3455 | subject_length); |
| 3456 | } |
| 3457 | |
| 3458 | int last_match_capture_count = (capture_count + 1) * 2; |
| 3459 | int last_match_array_size = |
| 3460 | last_match_capture_count + RegExpImpl::kLastMatchOverhead; |
| 3461 | last_match_array->EnsureSize(last_match_array_size); |
| 3462 | AssertNoAllocation no_gc; |
| 3463 | FixedArray* elements = FixedArray::cast(last_match_array->elements()); |
| 3464 | RegExpImpl::SetLastCaptureCount(elements, last_match_capture_count); |
| 3465 | RegExpImpl::SetLastSubject(elements, *subject); |
| 3466 | RegExpImpl::SetLastInput(elements, *subject); |
| 3467 | for (int i = 0; i < last_match_capture_count; i++) { |
| 3468 | RegExpImpl::SetCapture(elements, i, prev_register_vector[i]); |
| 3469 | } |
| 3470 | return RegExpImpl::RE_SUCCESS; |
| 3471 | } |
| 3472 | } |
| 3473 | // No matches at all, return failure or exception result directly. |
| 3474 | return result; |
| 3475 | } |
| 3476 | |
| 3477 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3478 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RegExpExecMultiple) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3479 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3480 | HandleScope handles(isolate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3481 | |
| 3482 | CONVERT_ARG_CHECKED(String, subject, 1); |
| 3483 | if (!subject->IsFlat()) { FlattenString(subject); } |
| 3484 | CONVERT_ARG_CHECKED(JSRegExp, regexp, 0); |
| 3485 | CONVERT_ARG_CHECKED(JSArray, last_match_info, 2); |
| 3486 | CONVERT_ARG_CHECKED(JSArray, result_array, 3); |
| 3487 | |
| 3488 | ASSERT(last_match_info->HasFastElements()); |
| 3489 | ASSERT(regexp->GetFlags().is_global()); |
| 3490 | Handle<FixedArray> result_elements; |
| 3491 | if (result_array->HasFastElements()) { |
| 3492 | result_elements = |
| 3493 | Handle<FixedArray>(FixedArray::cast(result_array->elements())); |
| 3494 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3495 | result_elements = isolate->factory()->NewFixedArrayWithHoles(16); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3496 | } |
| 3497 | FixedArrayBuilder builder(result_elements); |
| 3498 | |
| 3499 | if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 3500 | Handle<String> pattern( |
| 3501 | String::cast(regexp->DataAt(JSRegExp::kAtomPatternIndex))); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 3502 | ASSERT(pattern->IsFlat()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3503 | if (SearchStringMultiple(isolate, subject, pattern, |
| 3504 | last_match_info, &builder)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3505 | return *builder.ToJSArray(result_array); |
| 3506 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3507 | return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | ASSERT_EQ(regexp->TypeTag(), JSRegExp::IRREGEXP); |
| 3511 | |
| 3512 | RegExpImpl::IrregexpResult result; |
| 3513 | if (regexp->CaptureCount() == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3514 | result = SearchRegExpNoCaptureMultiple(isolate, |
| 3515 | subject, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3516 | regexp, |
| 3517 | last_match_info, |
| 3518 | &builder); |
| 3519 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3520 | result = SearchRegExpMultiple(isolate, |
| 3521 | subject, |
| 3522 | regexp, |
| 3523 | last_match_info, |
| 3524 | &builder); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3525 | } |
| 3526 | if (result == RegExpImpl::RE_SUCCESS) return *builder.ToJSArray(result_array); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3527 | if (result == RegExpImpl::RE_FAILURE) return isolate->heap()->null_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 3528 | ASSERT_EQ(result, RegExpImpl::RE_EXCEPTION); |
| 3529 | return Failure::Exception(); |
| 3530 | } |
| 3531 | |
| 3532 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3533 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToRadixString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3534 | NoHandleAllocation ha; |
| 3535 | ASSERT(args.length() == 2); |
| 3536 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3537 | // Fast case where the result is a one character string. |
| 3538 | if (args[0]->IsSmi() && args[1]->IsSmi()) { |
| 3539 | int value = Smi::cast(args[0])->value(); |
| 3540 | int radix = Smi::cast(args[1])->value(); |
| 3541 | if (value >= 0 && value < radix) { |
| 3542 | RUNTIME_ASSERT(radix <= 36); |
| 3543 | // Character array used for conversion. |
| 3544 | static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3545 | return isolate->heap()-> |
| 3546 | LookupSingleCharacterStringFromCode(kCharTable[value]); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | // Slow case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3551 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3552 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3553 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3554 | } |
| 3555 | if (isinf(value)) { |
| 3556 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3557 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3558 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3559 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3560 | } |
| 3561 | CONVERT_DOUBLE_CHECKED(radix_number, args[1]); |
| 3562 | int radix = FastD2I(radix_number); |
| 3563 | RUNTIME_ASSERT(2 <= radix && radix <= 36); |
| 3564 | char* str = DoubleToRadixCString(value, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3565 | MaybeObject* result = |
| 3566 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3567 | DeleteArray(str); |
| 3568 | return result; |
| 3569 | } |
| 3570 | |
| 3571 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3572 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToFixed) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3573 | NoHandleAllocation ha; |
| 3574 | ASSERT(args.length() == 2); |
| 3575 | |
| 3576 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3577 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3578 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3579 | } |
| 3580 | if (isinf(value)) { |
| 3581 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3582 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3583 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3584 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3585 | } |
| 3586 | CONVERT_DOUBLE_CHECKED(f_number, args[1]); |
| 3587 | int f = FastD2I(f_number); |
| 3588 | RUNTIME_ASSERT(f >= 0); |
| 3589 | char* str = DoubleToFixedCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3590 | MaybeObject* res = |
| 3591 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3592 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3593 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3597 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToExponential) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3598 | NoHandleAllocation ha; |
| 3599 | ASSERT(args.length() == 2); |
| 3600 | |
| 3601 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3602 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3603 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3604 | } |
| 3605 | if (isinf(value)) { |
| 3606 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3607 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3608 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3609 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3610 | } |
| 3611 | CONVERT_DOUBLE_CHECKED(f_number, args[1]); |
| 3612 | int f = FastD2I(f_number); |
| 3613 | RUNTIME_ASSERT(f >= -1 && f <= 20); |
| 3614 | char* str = DoubleToExponentialCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3615 | MaybeObject* res = |
| 3616 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3617 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3618 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3622 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPrecision) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3623 | NoHandleAllocation ha; |
| 3624 | ASSERT(args.length() == 2); |
| 3625 | |
| 3626 | CONVERT_DOUBLE_CHECKED(value, args[0]); |
| 3627 | if (isnan(value)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3628 | return isolate->heap()->AllocateStringFromAscii(CStrVector("NaN")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3629 | } |
| 3630 | if (isinf(value)) { |
| 3631 | if (value < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3632 | return isolate->heap()->AllocateStringFromAscii(CStrVector("-Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3633 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3634 | return isolate->heap()->AllocateStringFromAscii(CStrVector("Infinity")); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3635 | } |
| 3636 | CONVERT_DOUBLE_CHECKED(f_number, args[1]); |
| 3637 | int f = FastD2I(f_number); |
| 3638 | RUNTIME_ASSERT(f >= 1 && f <= 21); |
| 3639 | char* str = DoubleToPrecisionCString(value, f); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3640 | MaybeObject* res = |
| 3641 | isolate->heap()->AllocateStringFromAscii(CStrVector(str)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3642 | DeleteArray(str); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3643 | return res; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | |
| 3647 | // Returns a single character string where first character equals |
| 3648 | // string->Get(index). |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3649 | static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3650 | if (index < static_cast<uint32_t>(string->length())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3651 | string->TryFlatten(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3652 | return LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3653 | string->Get(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3654 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3655 | return Execution::CharAt(string, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3659 | MaybeObject* Runtime::GetElementOrCharAt(Isolate* isolate, |
| 3660 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3661 | uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3662 | // Handle [] indexing on Strings |
| 3663 | if (object->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3664 | Handle<Object> result = GetCharAt(Handle<String>::cast(object), index); |
| 3665 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | // Handle [] indexing on String objects |
| 3669 | if (object->IsStringObjectWithCharacterAt(index)) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3670 | Handle<JSValue> js_value = Handle<JSValue>::cast(object); |
| 3671 | Handle<Object> result = |
| 3672 | GetCharAt(Handle<String>(String::cast(js_value->value())), index); |
| 3673 | if (!result->IsUndefined()) return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | if (object->IsString() || object->IsNumber() || object->IsBoolean()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3677 | Handle<Object> prototype = GetPrototype(object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3678 | return prototype->GetElement(index); |
| 3679 | } |
| 3680 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3681 | return GetElement(object, index); |
| 3682 | } |
| 3683 | |
| 3684 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3685 | MaybeObject* Runtime::GetElement(Handle<Object> object, uint32_t index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3686 | return object->GetElement(index); |
| 3687 | } |
| 3688 | |
| 3689 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3690 | MaybeObject* Runtime::GetObjectProperty(Isolate* isolate, |
| 3691 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3692 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3693 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3694 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3695 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3696 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3697 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3698 | isolate->factory()->NewTypeError("non_object_property_load", |
| 3699 | HandleVector(args, 2)); |
| 3700 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | // Check if the given key is an array index. |
| 3704 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3705 | if (key->ToArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3706 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | // 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] | 3710 | Handle<String> name; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3711 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3712 | name = Handle<String>::cast(key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3713 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3714 | bool has_pending_exception = false; |
| 3715 | Handle<Object> converted = |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3716 | Execution::ToString(key, &has_pending_exception); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3717 | if (has_pending_exception) return Failure::Exception(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3718 | name = Handle<String>::cast(converted); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3721 | // 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] | 3722 | // the element if so. |
| 3723 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3724 | return GetElementOrCharAt(isolate, object, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3725 | } else { |
| 3726 | PropertyAttributes attr; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3727 | return object->GetProperty(*name, &attr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3732 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3733 | NoHandleAllocation ha; |
| 3734 | ASSERT(args.length() == 2); |
| 3735 | |
| 3736 | Handle<Object> object = args.at<Object>(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3737 | Handle<Object> key = args.at<Object>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3738 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3739 | return Runtime::GetObjectProperty(isolate, object, key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3743 | // KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3744 | RUNTIME_FUNCTION(MaybeObject*, Runtime_KeyedGetProperty) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3745 | NoHandleAllocation ha; |
| 3746 | ASSERT(args.length() == 2); |
| 3747 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3748 | // Fast cases for getting named properties of the receiver JSObject |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3749 | // itself. |
| 3750 | // |
| 3751 | // The global proxy objects has to be excluded since LocalLookup on |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3752 | // 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] | 3753 | // global proxy object never has properties. This is the case |
| 3754 | // because the global proxy object forwards everything to its hidden |
| 3755 | // prototype including local lookups. |
| 3756 | // |
| 3757 | // Additionally, we need to make sure that we do not cache results |
| 3758 | // for objects that require access checks. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3759 | if (args[0]->IsJSObject() && |
| 3760 | !args[0]->IsJSGlobalProxy() && |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 3761 | !args[0]->IsAccessCheckNeeded() && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3762 | args[1]->IsString()) { |
| 3763 | JSObject* receiver = JSObject::cast(args[0]); |
| 3764 | String* key = String::cast(args[1]); |
| 3765 | if (receiver->HasFastProperties()) { |
| 3766 | // Attempt to use lookup cache. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3767 | Map* receiver_map = receiver->map(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3768 | KeyedLookupCache* keyed_lookup_cache = isolate->keyed_lookup_cache(); |
| 3769 | int offset = keyed_lookup_cache->Lookup(receiver_map, key); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3770 | if (offset != -1) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3771 | Object* value = receiver->FastPropertyAt(offset); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3772 | return value->IsTheHole() ? isolate->heap()->undefined_value() : value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3773 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3774 | // Lookup cache miss. Perform lookup and update the cache if appropriate. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3775 | LookupResult result; |
| 3776 | receiver->LocalLookup(key, &result); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 3777 | if (result.IsProperty() && result.type() == FIELD) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3778 | int offset = result.GetFieldIndex(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3779 | keyed_lookup_cache->Update(receiver_map, key, offset); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3780 | return receiver->FastPropertyAt(offset); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3781 | } |
| 3782 | } else { |
| 3783 | // Attempt dictionary lookup. |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3784 | StringDictionary* dictionary = receiver->property_dictionary(); |
| 3785 | int entry = dictionary->FindEntry(key); |
| 3786 | if ((entry != StringDictionary::kNotFound) && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3787 | (dictionary->DetailsAt(entry).type() == NORMAL)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3788 | Object* value = dictionary->ValueAt(entry); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3789 | if (!receiver->IsGlobalObject()) return value; |
| 3790 | value = JSGlobalPropertyCell::cast(value)->value(); |
| 3791 | if (!value->IsTheHole()) return value; |
| 3792 | // If value is the hole do the general lookup. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3793 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3794 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3795 | } else if (args[0]->IsString() && args[1]->IsSmi()) { |
| 3796 | // Fast case for string indexing using [] with a smi index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3797 | HandleScope scope(isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3798 | Handle<String> str = args.at<String>(0); |
| 3799 | int index = Smi::cast(args[1])->value(); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3800 | if (index >= 0 && index < str->length()) { |
| 3801 | Handle<Object> result = GetCharAt(str, index); |
| 3802 | return *result; |
| 3803 | } |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3804 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3805 | |
| 3806 | // Fall back to GetObjectProperty. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3807 | return Runtime::GetObjectProperty(isolate, |
| 3808 | args.at<Object>(0), |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3809 | args.at<Object>(1)); |
| 3810 | } |
| 3811 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3812 | // Implements part of 8.12.9 DefineOwnProperty. |
| 3813 | // There are 3 cases that lead here: |
| 3814 | // Step 4b - define a new accessor property. |
| 3815 | // Steps 9c & 12 - replace an existing data property with an accessor property. |
| 3816 | // Step 12 - update an existing accessor property with an accessor or generic |
| 3817 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3818 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineAccessorProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3819 | ASSERT(args.length() == 5); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3820 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3821 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 3822 | CONVERT_CHECKED(String, name, args[1]); |
| 3823 | CONVERT_CHECKED(Smi, flag_setter, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3824 | Object* fun = args[3]; |
| 3825 | RUNTIME_ASSERT(fun->IsJSFunction() || fun->IsUndefined()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3826 | CONVERT_CHECKED(Smi, flag_attr, args[4]); |
| 3827 | int unchecked = flag_attr->value(); |
| 3828 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 3829 | RUNTIME_ASSERT(!obj->IsNull()); |
| 3830 | LookupResult result; |
| 3831 | obj->LocalLookupRealNamedProperty(name, &result); |
| 3832 | |
| 3833 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 3834 | // If an existing property is either FIELD, NORMAL or CONSTANT_FUNCTION |
| 3835 | // delete it to avoid running into trouble in DefineAccessor, which |
| 3836 | // handles this incorrectly if the property is readonly (does nothing) |
| 3837 | if (result.IsProperty() && |
| 3838 | (result.type() == FIELD || result.type() == NORMAL |
| 3839 | || result.type() == CONSTANT_FUNCTION)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3840 | Object* ok; |
| 3841 | { MaybeObject* maybe_ok = |
| 3842 | obj->DeleteProperty(name, JSObject::NORMAL_DELETION); |
| 3843 | if (!maybe_ok->ToObject(&ok)) return maybe_ok; |
| 3844 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3845 | } |
| 3846 | return obj->DefineAccessor(name, flag_setter->value() == 0, fun, attr); |
| 3847 | } |
| 3848 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3849 | // Implements part of 8.12.9 DefineOwnProperty. |
| 3850 | // There are 3 cases that lead here: |
| 3851 | // Step 4a - define a new data property. |
| 3852 | // Steps 9b & 12 - replace an existing accessor property with a data property. |
| 3853 | // Step 12 - update an existing data property with a data or generic |
| 3854 | // descriptor. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 3855 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineOrRedefineDataProperty) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3856 | ASSERT(args.length() == 4); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3857 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3858 | CONVERT_ARG_CHECKED(JSObject, js_object, 0); |
| 3859 | CONVERT_ARG_CHECKED(String, name, 1); |
| 3860 | Handle<Object> obj_value = args.at<Object>(2); |
| 3861 | |
| 3862 | CONVERT_CHECKED(Smi, flag, args[3]); |
| 3863 | int unchecked = flag->value(); |
| 3864 | RUNTIME_ASSERT((unchecked & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 3865 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3866 | PropertyAttributes attr = static_cast<PropertyAttributes>(unchecked); |
| 3867 | |
| 3868 | // Check if this is an element. |
| 3869 | uint32_t index; |
| 3870 | bool is_element = name->AsArrayIndex(&index); |
| 3871 | |
| 3872 | // Special case for elements if any of the flags are true. |
| 3873 | // If elements are in fast case we always implicitly assume that: |
| 3874 | // DONT_DELETE: false, DONT_ENUM: false, READ_ONLY: false. |
| 3875 | if (((unchecked & (DONT_DELETE | DONT_ENUM | READ_ONLY)) != 0) && |
| 3876 | is_element) { |
| 3877 | // Normalize the elements to enable attributes on the property. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3878 | if (js_object->IsJSGlobalProxy()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 3879 | // We do not need to do access checks here since these has already |
| 3880 | // been performed by the call to GetOwnProperty. |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3881 | Handle<Object> proto(js_object->GetPrototype()); |
| 3882 | // If proxy is detached, ignore the assignment. Alternatively, |
| 3883 | // we could throw an exception. |
| 3884 | if (proto->IsNull()) return *obj_value; |
| 3885 | js_object = Handle<JSObject>::cast(proto); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3886 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3887 | NormalizeElements(js_object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3888 | Handle<NumberDictionary> dictionary(js_object->element_dictionary()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3889 | // Make sure that we never go back to fast case. |
| 3890 | dictionary->set_requires_slow_elements(); |
| 3891 | PropertyDetails details = PropertyDetails(attr, NORMAL); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3892 | NumberDictionarySet(dictionary, index, obj_value, details); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3893 | return *obj_value; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3896 | LookupResult result; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3897 | js_object->LookupRealNamedProperty(*name, &result); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3898 | |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 3899 | // To be compatible with safari we do not change the value on API objects |
| 3900 | // in defineProperty. Firefox disagrees here, and actually changes the value. |
| 3901 | if (result.IsProperty() && |
| 3902 | (result.type() == CALLBACKS) && |
| 3903 | result.GetCallbackObject()->IsAccessorInfo()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3904 | return isolate->heap()->undefined_value(); |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3907 | // Take special care when attributes are different and there is already |
| 3908 | // a property. For simplicity we normalize the property which enables us |
| 3909 | // to not worry about changing the instance_descriptor and creating a new |
| 3910 | // map. The current version of SetObjectProperty does not handle attributes |
| 3911 | // correctly in the case where a property is a field and is reset with |
| 3912 | // new attributes. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3913 | if (result.IsProperty() && |
| 3914 | (attr != result.GetAttributes() || result.type() == CALLBACKS)) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3915 | // New attributes - normalize to avoid writing to instance descriptor |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3916 | if (js_object->IsJSGlobalProxy()) { |
| 3917 | // Since the result is a property, the prototype will exist so |
| 3918 | // we don't have to check for null. |
| 3919 | js_object = Handle<JSObject>(JSObject::cast(js_object->GetPrototype())); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 3920 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 3921 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3922 | // Use IgnoreAttributes version since a readonly property may be |
| 3923 | // overridden and SetProperty does not allow this. |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 3924 | return js_object->SetLocalPropertyIgnoreAttributes(*name, |
| 3925 | *obj_value, |
| 3926 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3927 | } |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 3928 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3929 | return Runtime::ForceSetObjectProperty(isolate, |
| 3930 | js_object, |
| 3931 | name, |
| 3932 | obj_value, |
| 3933 | attr); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3937 | MaybeObject* Runtime::SetObjectProperty(Isolate* isolate, |
| 3938 | Handle<Object> object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 3939 | Handle<Object> key, |
| 3940 | Handle<Object> value, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3941 | PropertyAttributes attr, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3942 | StrictModeFlag strict_mode) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3943 | HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3944 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3945 | if (object->IsUndefined() || object->IsNull()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3946 | Handle<Object> args[2] = { key, object }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3947 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 3948 | isolate->factory()->NewTypeError("non_object_property_store", |
| 3949 | HandleVector(args, 2)); |
| 3950 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | // If the object isn't a JavaScript object, we ignore the store. |
| 3954 | if (!object->IsJSObject()) return *value; |
| 3955 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3956 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
| 3957 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3958 | // Check if the given key is an array index. |
| 3959 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3960 | if (key->ToArrayIndex(&index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3961 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 3962 | // of a string using [] notation. We need to support this too in |
| 3963 | // JavaScript. |
| 3964 | // In the case of a String object we just need to redirect the assignment to |
| 3965 | // the underlying string if the index is in range. Since the underlying |
| 3966 | // string does nothing with the assignment then we can ignore such |
| 3967 | // assignments. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3968 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3969 | return *value; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3970 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3971 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3972 | 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] | 3973 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3974 | return *value; |
| 3975 | } |
| 3976 | |
| 3977 | if (key->IsString()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3978 | Handle<Object> result; |
| 3979 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3980 | result = SetElement(js_object, index, value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3981 | } else { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3982 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3983 | key_string->TryFlatten(); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3984 | result = SetProperty(js_object, key_string, value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3985 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3986 | if (result.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3987 | return *value; |
| 3988 | } |
| 3989 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3990 | // Call-back into JavaScript to convert the key to a string. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3991 | bool has_pending_exception = false; |
| 3992 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 3993 | if (has_pending_exception) return Failure::Exception(); |
| 3994 | Handle<String> name = Handle<String>::cast(converted); |
| 3995 | |
| 3996 | if (name->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3997 | return js_object->SetElement(index, *value, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3998 | } else { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 3999 | return js_object->SetProperty(*name, *value, attr, strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4000 | } |
| 4001 | } |
| 4002 | |
| 4003 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4004 | MaybeObject* Runtime::ForceSetObjectProperty(Isolate* isolate, |
| 4005 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4006 | Handle<Object> key, |
| 4007 | Handle<Object> value, |
| 4008 | PropertyAttributes attr) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4009 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4010 | |
| 4011 | // Check if the given key is an array index. |
| 4012 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4013 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4014 | // In Firefox/SpiderMonkey, Safari and Opera you can access the characters |
| 4015 | // of a string using [] notation. We need to support this too in |
| 4016 | // JavaScript. |
| 4017 | // In the case of a String object we just need to redirect the assignment to |
| 4018 | // the underlying string if the index is in range. Since the underlying |
| 4019 | // string does nothing with the assignment then we can ignore such |
| 4020 | // assignments. |
| 4021 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
| 4022 | return *value; |
| 4023 | } |
| 4024 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4025 | return js_object->SetElement(index, *value, kNonStrictMode); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | if (key->IsString()) { |
| 4029 | if (Handle<String>::cast(key)->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4030 | return js_object->SetElement(index, *value, kNonStrictMode); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4031 | } else { |
| 4032 | Handle<String> key_string = Handle<String>::cast(key); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4033 | key_string->TryFlatten(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4034 | return js_object->SetLocalPropertyIgnoreAttributes(*key_string, |
| 4035 | *value, |
| 4036 | attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | // Call-back into JavaScript to convert the key to a string. |
| 4041 | bool has_pending_exception = false; |
| 4042 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4043 | if (has_pending_exception) return Failure::Exception(); |
| 4044 | Handle<String> name = Handle<String>::cast(converted); |
| 4045 | |
| 4046 | if (name->AsArrayIndex(&index)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4047 | return js_object->SetElement(index, *value, kNonStrictMode); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4048 | } else { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4049 | return js_object->SetLocalPropertyIgnoreAttributes(*name, *value, attr); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4054 | MaybeObject* Runtime::ForceDeleteObjectProperty(Isolate* isolate, |
| 4055 | Handle<JSObject> js_object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4056 | Handle<Object> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4057 | HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4058 | |
| 4059 | // Check if the given key is an array index. |
| 4060 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4061 | if (key->ToArrayIndex(&index)) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4062 | // In Firefox/SpiderMonkey, Safari and Opera you can access the |
| 4063 | // characters of a string using [] notation. In the case of a |
| 4064 | // String object we just need to redirect the deletion to the |
| 4065 | // underlying string if the index is in range. Since the |
| 4066 | // underlying string does nothing with the deletion, we can ignore |
| 4067 | // such deletions. |
| 4068 | if (js_object->IsStringObjectWithCharacterAt(index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4069 | return isolate->heap()->true_value(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
| 4072 | return js_object->DeleteElement(index, JSObject::FORCE_DELETION); |
| 4073 | } |
| 4074 | |
| 4075 | Handle<String> key_string; |
| 4076 | if (key->IsString()) { |
| 4077 | key_string = Handle<String>::cast(key); |
| 4078 | } else { |
| 4079 | // Call-back into JavaScript to convert the key to a string. |
| 4080 | bool has_pending_exception = false; |
| 4081 | Handle<Object> converted = Execution::ToString(key, &has_pending_exception); |
| 4082 | if (has_pending_exception) return Failure::Exception(); |
| 4083 | key_string = Handle<String>::cast(converted); |
| 4084 | } |
| 4085 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4086 | key_string->TryFlatten(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 4087 | return js_object->DeleteProperty(*key_string, JSObject::FORCE_DELETION); |
| 4088 | } |
| 4089 | |
| 4090 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4091 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4092 | NoHandleAllocation ha; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4093 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4094 | |
| 4095 | Handle<Object> object = args.at<Object>(0); |
| 4096 | Handle<Object> key = args.at<Object>(1); |
| 4097 | Handle<Object> value = args.at<Object>(2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4098 | CONVERT_SMI_CHECKED(unchecked_attributes, args[3]); |
| 4099 | RUNTIME_ASSERT( |
| 4100 | (unchecked_attributes & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4101 | // Compute attributes. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4102 | PropertyAttributes attributes = |
| 4103 | static_cast<PropertyAttributes>(unchecked_attributes); |
| 4104 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4105 | StrictModeFlag strict_mode = kNonStrictMode; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4106 | if (args.length() == 5) { |
| 4107 | CONVERT_SMI_CHECKED(strict_unchecked, args[4]); |
| 4108 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 4109 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4110 | strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4111 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4112 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4113 | return Runtime::SetObjectProperty(isolate, |
| 4114 | object, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 4115 | key, |
| 4116 | value, |
| 4117 | attributes, |
| 4118 | strict_mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 4122 | // Set the ES5 native flag on the function. |
| 4123 | // This is used to decide if we should transform null and undefined |
| 4124 | // into the global object when doing call and apply. |
| 4125 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetES5Flag) { |
| 4126 | NoHandleAllocation ha; |
| 4127 | RUNTIME_ASSERT(args.length() == 1); |
| 4128 | |
| 4129 | Handle<Object> object = args.at<Object>(0); |
| 4130 | |
| 4131 | if (object->IsJSFunction()) { |
| 4132 | JSFunction* func = JSFunction::cast(*object); |
| 4133 | func->shared()->set_es5_native(true); |
| 4134 | } |
| 4135 | return isolate->heap()->undefined_value(); |
| 4136 | } |
| 4137 | |
| 4138 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4139 | // Set a local property, even if it is READ_ONLY. If the property does not |
| 4140 | // exist, it will be added with attributes NONE. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4141 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IgnoreAttributesAndSetProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4142 | NoHandleAllocation ha; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4143 | RUNTIME_ASSERT(args.length() == 3 || args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4144 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4145 | CONVERT_CHECKED(String, name, args[1]); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4146 | // Compute attributes. |
| 4147 | PropertyAttributes attributes = NONE; |
| 4148 | if (args.length() == 4) { |
| 4149 | CONVERT_CHECKED(Smi, value_obj, args[3]); |
| 4150 | int unchecked_value = value_obj->value(); |
| 4151 | // Only attribute bits should be set. |
| 4152 | RUNTIME_ASSERT( |
| 4153 | (unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 4154 | attributes = static_cast<PropertyAttributes>(unchecked_value); |
| 4155 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4156 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 4157 | return object-> |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 4158 | SetLocalPropertyIgnoreAttributes(name, args[2], attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4162 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4163 | NoHandleAllocation ha; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4164 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4165 | |
| 4166 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4167 | CONVERT_CHECKED(String, key, args[1]); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4168 | CONVERT_SMI_CHECKED(strict, args[2]); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 4169 | return object->DeleteProperty(key, (strict == kStrictMode) |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4170 | ? JSObject::STRICT_DELETION |
| 4171 | : JSObject::NORMAL_DELETION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4175 | static Object* HasLocalPropertyImplementation(Isolate* isolate, |
| 4176 | Handle<JSObject> object, |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4177 | Handle<String> key) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4178 | if (object->HasLocalProperty(*key)) return isolate->heap()->true_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4179 | // Handle hidden prototypes. If there's a hidden prototype above this thing |
| 4180 | // then we have to check it for properties, because they are supposed to |
| 4181 | // look like they are on this object. |
| 4182 | Handle<Object> proto(object->GetPrototype()); |
| 4183 | if (proto->IsJSObject() && |
| 4184 | Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4185 | return HasLocalPropertyImplementation(isolate, |
| 4186 | Handle<JSObject>::cast(proto), |
| 4187 | key); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4188 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4189 | return isolate->heap()->false_value(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4193 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLocalProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4194 | NoHandleAllocation ha; |
| 4195 | ASSERT(args.length() == 2); |
| 4196 | CONVERT_CHECKED(String, key, args[1]); |
| 4197 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4198 | uint32_t index; |
| 4199 | const bool key_is_array_index = key->AsArrayIndex(&index); |
| 4200 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4201 | Object* obj = args[0]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4202 | // Only JS objects can have properties. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4203 | if (obj->IsJSObject()) { |
| 4204 | JSObject* object = JSObject::cast(obj); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4205 | // Fast case: either the key is a real named property or it is not |
| 4206 | // an array index and there are no interceptors or hidden |
| 4207 | // prototypes. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4208 | if (object->HasRealNamedProperty(key)) return isolate->heap()->true_value(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4209 | Map* map = object->map(); |
| 4210 | if (!key_is_array_index && |
| 4211 | !map->has_named_interceptor() && |
| 4212 | !HeapObject::cast(map->prototype())->map()->is_hidden_prototype()) { |
| 4213 | return isolate->heap()->false_value(); |
| 4214 | } |
| 4215 | // Slow case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4216 | HandleScope scope(isolate); |
| 4217 | return HasLocalPropertyImplementation(isolate, |
| 4218 | Handle<JSObject>(object), |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 4219 | Handle<String>(key)); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4220 | } else if (obj->IsString() && key_is_array_index) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4221 | // Well, there is one exception: Handle [] on strings. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 4222 | String* string = String::cast(obj); |
| 4223 | if (index < static_cast<uint32_t>(string->length())) { |
| 4224 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4225 | } |
| 4226 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4227 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4231 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4232 | NoHandleAllocation na; |
| 4233 | ASSERT(args.length() == 2); |
| 4234 | |
| 4235 | // Only JS objects can have properties. |
| 4236 | if (args[0]->IsJSObject()) { |
| 4237 | JSObject* object = JSObject::cast(args[0]); |
| 4238 | CONVERT_CHECKED(String, key, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4239 | if (object->HasProperty(key)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4240 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4241 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4245 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasElement) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4246 | NoHandleAllocation na; |
| 4247 | ASSERT(args.length() == 2); |
| 4248 | |
| 4249 | // Only JS objects can have elements. |
| 4250 | if (args[0]->IsJSObject()) { |
| 4251 | JSObject* object = JSObject::cast(args[0]); |
| 4252 | CONVERT_CHECKED(Smi, index_obj, args[1]); |
| 4253 | uint32_t index = index_obj->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4254 | if (object->HasElement(index)) return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4255 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4256 | return isolate->heap()->false_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4260 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsPropertyEnumerable) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4261 | NoHandleAllocation ha; |
| 4262 | ASSERT(args.length() == 2); |
| 4263 | |
| 4264 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 4265 | CONVERT_CHECKED(String, key, args[1]); |
| 4266 | |
| 4267 | uint32_t index; |
| 4268 | if (key->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4269 | return isolate->heap()->ToBoolean(object->HasElement(index)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4272 | PropertyAttributes att = object->GetLocalPropertyAttribute(key); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4273 | return isolate->heap()->ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4277 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4278 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4279 | ASSERT(args.length() == 1); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4280 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4281 | return *GetKeysFor(object); |
| 4282 | } |
| 4283 | |
| 4284 | |
| 4285 | // Returns either a FixedArray as Runtime_GetPropertyNames, |
| 4286 | // or, if the given object has an enum cache that contains |
| 4287 | // all enumerable properties of the object and its prototypes |
| 4288 | // have none, the map of the object. This is used to speed up |
| 4289 | // the check for deletions during a for-in. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4290 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetPropertyNamesFast) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4291 | ASSERT(args.length() == 1); |
| 4292 | |
| 4293 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
| 4294 | |
| 4295 | if (raw_object->IsSimpleEnum()) return raw_object->map(); |
| 4296 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4297 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4298 | Handle<JSObject> object(raw_object); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4299 | Handle<FixedArray> content = GetKeysInFixedArrayFor(object, |
| 4300 | INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4301 | |
| 4302 | // Test again, since cache may have been built by preceding call. |
| 4303 | if (object->IsSimpleEnum()) return object->map(); |
| 4304 | |
| 4305 | return *content; |
| 4306 | } |
| 4307 | |
| 4308 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4309 | // Find the length of the prototype chain that is to to handled as one. If a |
| 4310 | // prototype object is hidden it is to be viewed as part of the the object it |
| 4311 | // is prototype for. |
| 4312 | static int LocalPrototypeChainLength(JSObject* obj) { |
| 4313 | int count = 1; |
| 4314 | Object* proto = obj->GetPrototype(); |
| 4315 | while (proto->IsJSObject() && |
| 4316 | JSObject::cast(proto)->map()->is_hidden_prototype()) { |
| 4317 | count++; |
| 4318 | proto = JSObject::cast(proto)->GetPrototype(); |
| 4319 | } |
| 4320 | return count; |
| 4321 | } |
| 4322 | |
| 4323 | |
| 4324 | // Return the names of the local named properties. |
| 4325 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4326 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4327 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4328 | ASSERT(args.length() == 1); |
| 4329 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4330 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4331 | } |
| 4332 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4333 | |
| 4334 | // Skip the global proxy as it has no properties and always delegates to the |
| 4335 | // real global object. |
| 4336 | if (obj->IsJSGlobalProxy()) { |
| 4337 | // Only collect names if access is permitted. |
| 4338 | if (obj->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4339 | !isolate->MayNamedAccess(*obj, |
| 4340 | isolate->heap()->undefined_value(), |
| 4341 | v8::ACCESS_KEYS)) { |
| 4342 | isolate->ReportFailedAccessCheck(*obj, v8::ACCESS_KEYS); |
| 4343 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4344 | } |
| 4345 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 4346 | } |
| 4347 | |
| 4348 | // Find the number of objects making up this. |
| 4349 | int length = LocalPrototypeChainLength(*obj); |
| 4350 | |
| 4351 | // Find the number of local properties for each of the objects. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 4352 | ScopedVector<int> local_property_count(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4353 | int total_property_count = 0; |
| 4354 | Handle<JSObject> jsproto = obj; |
| 4355 | for (int i = 0; i < length; i++) { |
| 4356 | // Only collect names if access is permitted. |
| 4357 | if (jsproto->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4358 | !isolate->MayNamedAccess(*jsproto, |
| 4359 | isolate->heap()->undefined_value(), |
| 4360 | v8::ACCESS_KEYS)) { |
| 4361 | isolate->ReportFailedAccessCheck(*jsproto, v8::ACCESS_KEYS); |
| 4362 | return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4363 | } |
| 4364 | int n; |
| 4365 | n = jsproto->NumberOfLocalProperties(static_cast<PropertyAttributes>(NONE)); |
| 4366 | local_property_count[i] = n; |
| 4367 | total_property_count += n; |
| 4368 | if (i < length - 1) { |
| 4369 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | // Allocate an array with storage for all the property names. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4374 | Handle<FixedArray> names = |
| 4375 | isolate->factory()->NewFixedArray(total_property_count); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4376 | |
| 4377 | // Get the property names. |
| 4378 | jsproto = obj; |
| 4379 | int proto_with_hidden_properties = 0; |
| 4380 | for (int i = 0; i < length; i++) { |
| 4381 | jsproto->GetLocalPropertyNames(*names, |
| 4382 | i == 0 ? 0 : local_property_count[i - 1]); |
| 4383 | if (!GetHiddenProperties(jsproto, false)->IsUndefined()) { |
| 4384 | proto_with_hidden_properties++; |
| 4385 | } |
| 4386 | if (i < length - 1) { |
| 4387 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | // Filter out name of hidden propeties object. |
| 4392 | if (proto_with_hidden_properties > 0) { |
| 4393 | Handle<FixedArray> old_names = names; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4394 | names = isolate->factory()->NewFixedArray( |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4395 | names->length() - proto_with_hidden_properties); |
| 4396 | int dest_pos = 0; |
| 4397 | for (int i = 0; i < total_property_count; i++) { |
| 4398 | Object* name = old_names->get(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4399 | if (name == isolate->heap()->hidden_symbol()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4400 | continue; |
| 4401 | } |
| 4402 | names->set(dest_pos++, name); |
| 4403 | } |
| 4404 | } |
| 4405 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4406 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | |
| 4410 | // Return the names of the local indexed properties. |
| 4411 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4412 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLocalElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4413 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4414 | ASSERT(args.length() == 1); |
| 4415 | if (!args[0]->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4416 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4417 | } |
| 4418 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4419 | |
| 4420 | int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4421 | Handle<FixedArray> names = isolate->factory()->NewFixedArray(n); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4422 | obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4423 | return *isolate->factory()->NewJSArrayWithElements(names); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | |
| 4427 | // Return information on whether an object has a named or indexed interceptor. |
| 4428 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4429 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetInterceptorInfo) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4430 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4431 | ASSERT(args.length() == 1); |
| 4432 | if (!args[0]->IsJSObject()) { |
| 4433 | return Smi::FromInt(0); |
| 4434 | } |
| 4435 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4436 | |
| 4437 | int result = 0; |
| 4438 | if (obj->HasNamedInterceptor()) result |= 2; |
| 4439 | if (obj->HasIndexedInterceptor()) result |= 1; |
| 4440 | |
| 4441 | return Smi::FromInt(result); |
| 4442 | } |
| 4443 | |
| 4444 | |
| 4445 | // Return property names from named interceptor. |
| 4446 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4447 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetNamedInterceptorPropertyNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4448 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4449 | ASSERT(args.length() == 1); |
| 4450 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4451 | |
| 4452 | if (obj->HasNamedInterceptor()) { |
| 4453 | v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj); |
| 4454 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4455 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4456 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | |
| 4460 | // Return element names from indexed interceptor. |
| 4461 | // args[0]: object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4462 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetIndexedInterceptorElementNames) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4463 | HandleScope scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4464 | ASSERT(args.length() == 1); |
| 4465 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 4466 | |
| 4467 | if (obj->HasIndexedInterceptor()) { |
| 4468 | v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj); |
| 4469 | if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result); |
| 4470 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4471 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4475 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LocalKeys) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4476 | ASSERT_EQ(args.length(), 1); |
| 4477 | CONVERT_CHECKED(JSObject, raw_object, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4478 | HandleScope scope(isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4479 | Handle<JSObject> object(raw_object); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4480 | |
| 4481 | if (object->IsJSGlobalProxy()) { |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4482 | // Do access checks before going to the global object. |
| 4483 | if (object->IsAccessCheckNeeded() && |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4484 | !isolate->MayNamedAccess(*object, isolate->heap()->undefined_value(), |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4485 | v8::ACCESS_KEYS)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4486 | isolate->ReportFailedAccessCheck(*object, v8::ACCESS_KEYS); |
| 4487 | return *isolate->factory()->NewJSArray(0); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4490 | Handle<Object> proto(object->GetPrototype()); |
| 4491 | // If proxy is detached we simply return an empty array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4492 | if (proto->IsNull()) return *isolate->factory()->NewJSArray(0); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 4493 | object = Handle<JSObject>::cast(proto); |
| 4494 | } |
| 4495 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4496 | Handle<FixedArray> contents = GetKeysInFixedArrayFor(object, |
| 4497 | LOCAL_ONLY); |
| 4498 | // Some fast paths through GetKeysInFixedArrayFor reuse a cached |
| 4499 | // property array and since the result is mutable we have to create |
| 4500 | // a fresh clone on each invocation. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4501 | int length = contents->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4502 | Handle<FixedArray> copy = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4503 | for (int i = 0; i < length; i++) { |
| 4504 | Object* entry = contents->get(i); |
| 4505 | if (entry->IsString()) { |
| 4506 | copy->set(i, entry); |
| 4507 | } else { |
| 4508 | ASSERT(entry->IsNumber()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4509 | HandleScope scope(isolate); |
| 4510 | Handle<Object> entry_handle(entry, isolate); |
| 4511 | Handle<Object> entry_str = |
| 4512 | isolate->factory()->NumberToString(entry_handle); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4513 | copy->set(i, *entry_str); |
| 4514 | } |
| 4515 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4516 | return *isolate->factory()->NewJSArrayWithElements(copy); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4520 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArgumentsProperty) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4521 | NoHandleAllocation ha; |
| 4522 | ASSERT(args.length() == 1); |
| 4523 | |
| 4524 | // Compute the frame holding the arguments. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 4525 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4526 | it.AdvanceToArgumentsFrame(); |
| 4527 | JavaScriptFrame* frame = it.frame(); |
| 4528 | |
| 4529 | // Get the actual number of provided arguments. |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 4530 | const uint32_t n = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4531 | |
| 4532 | // Try to convert the key to an index. If successful and within |
| 4533 | // index return the the argument from the frame. |
| 4534 | uint32_t index; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4535 | if (args[0]->ToArrayIndex(&index) && index < n) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4536 | return frame->GetParameter(index); |
| 4537 | } |
| 4538 | |
| 4539 | // Convert the key to a string. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4540 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4541 | bool exception = false; |
| 4542 | Handle<Object> converted = |
| 4543 | Execution::ToString(args.at<Object>(0), &exception); |
| 4544 | if (exception) return Failure::Exception(); |
| 4545 | Handle<String> key = Handle<String>::cast(converted); |
| 4546 | |
| 4547 | // Try to convert the string key into an array index. |
| 4548 | if (key->AsArrayIndex(&index)) { |
| 4549 | if (index < n) { |
| 4550 | return frame->GetParameter(index); |
| 4551 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4552 | return isolate->initial_object_prototype()->GetElement(index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4553 | } |
| 4554 | } |
| 4555 | |
| 4556 | // Handle special arguments properties. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4557 | if (key->Equals(isolate->heap()->length_symbol())) return Smi::FromInt(n); |
| 4558 | if (key->Equals(isolate->heap()->callee_symbol())) { |
| 4559 | Object* function = frame->function(); |
| 4560 | if (function->IsJSFunction() && |
| 4561 | JSFunction::cast(function)->shared()->strict_mode()) { |
| 4562 | return isolate->Throw(*isolate->factory()->NewTypeError( |
| 4563 | "strict_arguments_callee", HandleVector<Object>(NULL, 0))); |
| 4564 | } |
| 4565 | return function; |
| 4566 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4567 | |
| 4568 | // Lookup in the initial Object.prototype object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4569 | return isolate->initial_object_prototype()->GetProperty(*key); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4573 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToFastProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4574 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4575 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4576 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4577 | Handle<Object> object = args.at<Object>(0); |
| 4578 | if (object->IsJSObject()) { |
| 4579 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4580 | if (!js_object->HasFastProperties() && !js_object->IsGlobalObject()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4581 | MaybeObject* ok = js_object->TransformToFastProperties(0); |
| 4582 | if (ok->IsRetryAfterGC()) return ok; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4583 | } |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4584 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4585 | return *object; |
| 4586 | } |
| 4587 | |
| 4588 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4589 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToSlowProperties) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4590 | HandleScope scope(isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 4591 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4592 | ASSERT(args.length() == 1); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4593 | Handle<Object> object = args.at<Object>(0); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 4594 | if (object->IsJSObject() && !object->IsJSGlobalProxy()) { |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4595 | Handle<JSObject> js_object = Handle<JSObject>::cast(object); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4596 | NormalizeProperties(js_object, CLEAR_INOBJECT_PROPERTIES, 0); |
kasperl@chromium.org | 7ccf024 | 2009-03-04 12:22:05 +0000 | [diff] [blame] | 4597 | } |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 4598 | return *object; |
| 4599 | } |
| 4600 | |
| 4601 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4602 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ToBool) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4603 | NoHandleAllocation ha; |
| 4604 | ASSERT(args.length() == 1); |
| 4605 | |
| 4606 | return args[0]->ToBoolean(); |
| 4607 | } |
| 4608 | |
| 4609 | |
| 4610 | // Returns the type string of a value; see ECMA-262, 11.4.3 (p 47). |
| 4611 | // Possible optimizations: put the type string into the oddballs. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4612 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Typeof) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4613 | NoHandleAllocation ha; |
| 4614 | |
| 4615 | Object* obj = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4616 | if (obj->IsNumber()) return isolate->heap()->number_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4617 | HeapObject* heap_obj = HeapObject::cast(obj); |
| 4618 | |
| 4619 | // typeof an undetectable object is 'undefined' |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4620 | if (heap_obj->map()->is_undetectable()) { |
| 4621 | return isolate->heap()->undefined_symbol(); |
| 4622 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4623 | |
| 4624 | InstanceType instance_type = heap_obj->map()->instance_type(); |
| 4625 | if (instance_type < FIRST_NONSTRING_TYPE) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4626 | return isolate->heap()->string_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | switch (instance_type) { |
| 4630 | case ODDBALL_TYPE: |
| 4631 | if (heap_obj->IsTrue() || heap_obj->IsFalse()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4632 | return isolate->heap()->boolean_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4633 | } |
| 4634 | if (heap_obj->IsNull()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4635 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4636 | } |
| 4637 | ASSERT(heap_obj->IsUndefined()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4638 | return isolate->heap()->undefined_symbol(); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 4639 | case JS_FUNCTION_TYPE: case JS_REGEXP_TYPE: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4640 | return isolate->heap()->function_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4641 | default: |
| 4642 | // For any kind of object not handled above, the spec rule for |
| 4643 | // host objects gives that it is okay to return "object" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4644 | return isolate->heap()->object_symbol(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4645 | } |
| 4646 | } |
| 4647 | |
| 4648 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4649 | static bool AreDigits(const char*s, int from, int to) { |
| 4650 | for (int i = from; i < to; i++) { |
| 4651 | if (s[i] < '0' || s[i] > '9') return false; |
| 4652 | } |
| 4653 | |
| 4654 | return true; |
| 4655 | } |
| 4656 | |
| 4657 | |
| 4658 | static int ParseDecimalInteger(const char*s, int from, int to) { |
| 4659 | ASSERT(to - from < 10); // Overflow is not possible. |
| 4660 | ASSERT(from < to); |
| 4661 | int d = s[from] - '0'; |
| 4662 | |
| 4663 | for (int i = from + 1; i < to; i++) { |
| 4664 | d = 10 * d + (s[i] - '0'); |
| 4665 | } |
| 4666 | |
| 4667 | return d; |
| 4668 | } |
| 4669 | |
| 4670 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4671 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4672 | NoHandleAllocation ha; |
| 4673 | ASSERT(args.length() == 1); |
| 4674 | CONVERT_CHECKED(String, subject, args[0]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4675 | subject->TryFlatten(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4676 | |
| 4677 | // Fast case: short integer or some sorts of junk values. |
| 4678 | int len = subject->length(); |
| 4679 | if (subject->IsSeqAsciiString()) { |
| 4680 | if (len == 0) return Smi::FromInt(0); |
| 4681 | |
| 4682 | char const* data = SeqAsciiString::cast(subject)->GetChars(); |
| 4683 | bool minus = (data[0] == '-'); |
| 4684 | int start_pos = (minus ? 1 : 0); |
| 4685 | |
| 4686 | if (start_pos == len) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4687 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4688 | } else if (data[start_pos] > '9') { |
| 4689 | // Fast check for a junk value. A valid string may start from a |
| 4690 | // whitespace, a sign ('+' or '-'), the decimal point, a decimal digit or |
| 4691 | // the 'I' character ('Infinity'). All of that have codes not greater than |
| 4692 | // '9' except 'I'. |
| 4693 | if (data[start_pos] != 'I') { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4694 | return isolate->heap()->nan_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4695 | } |
| 4696 | } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) { |
| 4697 | // The maximal/minimal smi has 10 digits. If the string has less digits we |
| 4698 | // know it will fit into the smi-data type. |
| 4699 | int d = ParseDecimalInteger(data, start_pos, len); |
| 4700 | if (minus) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4701 | if (d == 0) return isolate->heap()->minus_zero_value(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4702 | d = -d; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4703 | } else if (!subject->HasHashCode() && |
| 4704 | len <= String::kMaxArrayIndexSize && |
| 4705 | (len == 1 || data[0] != '0')) { |
| 4706 | // String hash is not calculated yet but all the data are present. |
| 4707 | // Update the hash field to speed up sequential convertions. |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 4708 | uint32_t hash = StringHasher::MakeArrayIndexHash(d, len); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4709 | #ifdef DEBUG |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4710 | subject->Hash(); // Force hash calculation. |
| 4711 | ASSERT_EQ(static_cast<int>(subject->hash_field()), |
| 4712 | static_cast<int>(hash)); |
| 4713 | #endif |
| 4714 | subject->set_hash_field(hash); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 4715 | } |
| 4716 | return Smi::FromInt(d); |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | // Slower case. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 4721 | return isolate->heap()->NumberFromDouble( |
| 4722 | StringToDouble(isolate->unicode_cache(), subject, ALLOW_HEX)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4726 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringFromCharCodeArray) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4727 | NoHandleAllocation ha; |
| 4728 | ASSERT(args.length() == 1); |
| 4729 | |
| 4730 | CONVERT_CHECKED(JSArray, codes, args[0]); |
| 4731 | int length = Smi::cast(codes->length())->value(); |
| 4732 | |
| 4733 | // Check if the string can be ASCII. |
| 4734 | int i; |
| 4735 | for (i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4736 | Object* element; |
| 4737 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 4738 | // We probably can't get an exception here, but just in order to enforce |
| 4739 | // the checking of inputs in the runtime calls we check here. |
| 4740 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 4741 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4742 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
| 4743 | if ((chr & 0xffff) > String::kMaxAsciiCharCode) |
| 4744 | break; |
| 4745 | } |
| 4746 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4747 | MaybeObject* maybe_object = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4748 | if (i == length) { // The string is ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4749 | maybe_object = isolate->heap()->AllocateRawAsciiString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4750 | } else { // The string is not ASCII. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4751 | maybe_object = isolate->heap()->AllocateRawTwoByteString(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4754 | Object* object = NULL; |
| 4755 | if (!maybe_object->ToObject(&object)) return maybe_object; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4756 | String* result = String::cast(object); |
| 4757 | for (int i = 0; i < length; i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4758 | Object* element; |
| 4759 | { MaybeObject* maybe_element = codes->GetElement(i); |
| 4760 | if (!maybe_element->ToObject(&element)) return maybe_element; |
| 4761 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4762 | CONVERT_NUMBER_CHECKED(int, chr, Int32, element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4763 | result->Set(i, chr & 0xffff); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4764 | } |
| 4765 | return result; |
| 4766 | } |
| 4767 | |
| 4768 | |
| 4769 | // kNotEscaped is generated by the following: |
| 4770 | // |
| 4771 | // #!/bin/perl |
| 4772 | // for (my $i = 0; $i < 256; $i++) { |
| 4773 | // print "\n" if $i % 16 == 0; |
| 4774 | // my $c = chr($i); |
| 4775 | // my $escaped = 1; |
| 4776 | // $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#; |
| 4777 | // print $escaped ? "0, " : "1, "; |
| 4778 | // } |
| 4779 | |
| 4780 | |
| 4781 | static bool IsNotEscaped(uint16_t character) { |
| 4782 | // Only for 8 bit characters, the rest are always escaped (in a different way) |
| 4783 | ASSERT(character < 256); |
| 4784 | static const char kNotEscaped[256] = { |
| 4785 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4786 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4787 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, |
| 4788 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, |
| 4789 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4790 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 4791 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 4792 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, |
| 4793 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4794 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4795 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4796 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4797 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4798 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4799 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4800 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 4801 | }; |
| 4802 | return kNotEscaped[character] != 0; |
| 4803 | } |
| 4804 | |
| 4805 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4806 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIEscape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4807 | const char hex_chars[] = "0123456789ABCDEF"; |
| 4808 | NoHandleAllocation ha; |
| 4809 | ASSERT(args.length() == 1); |
| 4810 | CONVERT_CHECKED(String, source, args[0]); |
| 4811 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4812 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4813 | |
| 4814 | int escaped_length = 0; |
| 4815 | int length = source->length(); |
| 4816 | { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4817 | Access<StringInputBuffer> buffer( |
| 4818 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4819 | buffer->Reset(source); |
| 4820 | while (buffer->has_more()) { |
| 4821 | uint16_t character = buffer->GetNext(); |
| 4822 | if (character >= 256) { |
| 4823 | escaped_length += 6; |
| 4824 | } else if (IsNotEscaped(character)) { |
| 4825 | escaped_length++; |
| 4826 | } else { |
| 4827 | escaped_length += 3; |
| 4828 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4829 | // 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] | 4830 | ASSERT(String::kMaxLength < 0x7fffffff - 6); // Cannot overflow. |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4831 | if (escaped_length > String::kMaxLength) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4832 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4833 | return Failure::OutOfMemoryException(); |
| 4834 | } |
| 4835 | } |
| 4836 | } |
| 4837 | // No length change implies no change. Return original string if no change. |
| 4838 | if (escaped_length == length) { |
| 4839 | return source; |
| 4840 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4841 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4842 | { MaybeObject* maybe_o = |
| 4843 | isolate->heap()->AllocateRawAsciiString(escaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4844 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 4845 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4846 | String* destination = String::cast(o); |
| 4847 | int dest_position = 0; |
| 4848 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4849 | Access<StringInputBuffer> buffer( |
| 4850 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4851 | buffer->Rewind(); |
| 4852 | while (buffer->has_more()) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4853 | uint16_t chr = buffer->GetNext(); |
| 4854 | if (chr >= 256) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4855 | destination->Set(dest_position, '%'); |
| 4856 | destination->Set(dest_position+1, 'u'); |
| 4857 | destination->Set(dest_position+2, hex_chars[chr >> 12]); |
| 4858 | destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]); |
| 4859 | destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]); |
| 4860 | destination->Set(dest_position+5, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4861 | dest_position += 6; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4862 | } else if (IsNotEscaped(chr)) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4863 | destination->Set(dest_position, chr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4864 | dest_position++; |
| 4865 | } else { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4866 | destination->Set(dest_position, '%'); |
| 4867 | destination->Set(dest_position+1, hex_chars[chr >> 4]); |
| 4868 | destination->Set(dest_position+2, hex_chars[chr & 0xf]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4869 | dest_position += 3; |
| 4870 | } |
| 4871 | } |
| 4872 | return destination; |
| 4873 | } |
| 4874 | |
| 4875 | |
| 4876 | static inline int TwoDigitHex(uint16_t character1, uint16_t character2) { |
| 4877 | static const signed char kHexValue['g'] = { |
| 4878 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4879 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4880 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4881 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
| 4882 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4883 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4884 | -1, 10, 11, 12, 13, 14, 15 }; |
| 4885 | |
| 4886 | if (character1 > 'f') return -1; |
| 4887 | int hi = kHexValue[character1]; |
| 4888 | if (hi == -1) return -1; |
| 4889 | if (character2 > 'f') return -1; |
| 4890 | int lo = kHexValue[character2]; |
| 4891 | if (lo == -1) return -1; |
| 4892 | return (hi << 4) + lo; |
| 4893 | } |
| 4894 | |
| 4895 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4896 | static inline int Unescape(String* source, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4897 | int i, |
| 4898 | int length, |
| 4899 | int* step) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4900 | uint16_t character = source->Get(i); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 4901 | int32_t hi = 0; |
| 4902 | int32_t lo = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4903 | if (character == '%' && |
| 4904 | i <= length - 6 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4905 | source->Get(i + 1) == 'u' && |
| 4906 | (hi = TwoDigitHex(source->Get(i + 2), |
| 4907 | source->Get(i + 3))) != -1 && |
| 4908 | (lo = TwoDigitHex(source->Get(i + 4), |
| 4909 | source->Get(i + 5))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4910 | *step = 6; |
| 4911 | return (hi << 8) + lo; |
| 4912 | } else if (character == '%' && |
| 4913 | i <= length - 3 && |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4914 | (lo = TwoDigitHex(source->Get(i + 1), |
| 4915 | source->Get(i + 2))) != -1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4916 | *step = 3; |
| 4917 | return lo; |
| 4918 | } else { |
| 4919 | *step = 1; |
| 4920 | return character; |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 4925 | RUNTIME_FUNCTION(MaybeObject*, Runtime_URIUnescape) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4926 | NoHandleAllocation ha; |
| 4927 | ASSERT(args.length() == 1); |
| 4928 | CONVERT_CHECKED(String, source, args[0]); |
| 4929 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4930 | source->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4931 | |
| 4932 | bool ascii = true; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4933 | int length = source->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4934 | |
| 4935 | int unescaped_length = 0; |
| 4936 | for (int i = 0; i < length; unescaped_length++) { |
| 4937 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4938 | if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4939 | ascii = false; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4940 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4941 | i += step; |
| 4942 | } |
| 4943 | |
| 4944 | // No length change implies no change. Return original string if no change. |
| 4945 | if (unescaped_length == length) |
| 4946 | return source; |
| 4947 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4948 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4949 | { MaybeObject* maybe_o = |
| 4950 | ascii ? |
| 4951 | isolate->heap()->AllocateRawAsciiString(unescaped_length) : |
| 4952 | isolate->heap()->AllocateRawTwoByteString(unescaped_length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 4953 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 4954 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4955 | String* destination = String::cast(o); |
| 4956 | |
| 4957 | int dest_position = 0; |
| 4958 | for (int i = 0; i < length; dest_position++) { |
| 4959 | int step; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 4960 | destination->Set(dest_position, Unescape(source, i, length, &step)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4961 | i += step; |
| 4962 | } |
| 4963 | return destination; |
| 4964 | } |
| 4965 | |
| 4966 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 4967 | static const unsigned int kQuoteTableLength = 128u; |
| 4968 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 4969 | static const int kJsonQuotesCharactersPerEntry = 8; |
| 4970 | static const char* const JsonQuotes = |
| 4971 | "\\u0000 \\u0001 \\u0002 \\u0003 " |
| 4972 | "\\u0004 \\u0005 \\u0006 \\u0007 " |
| 4973 | "\\b \\t \\n \\u000b " |
| 4974 | "\\f \\r \\u000e \\u000f " |
| 4975 | "\\u0010 \\u0011 \\u0012 \\u0013 " |
| 4976 | "\\u0014 \\u0015 \\u0016 \\u0017 " |
| 4977 | "\\u0018 \\u0019 \\u001a \\u001b " |
| 4978 | "\\u001c \\u001d \\u001e \\u001f " |
| 4979 | " ! \\\" # " |
| 4980 | "$ % & ' " |
| 4981 | "( ) * + " |
| 4982 | ", - . / " |
| 4983 | "0 1 2 3 " |
| 4984 | "4 5 6 7 " |
| 4985 | "8 9 : ; " |
| 4986 | "< = > ? " |
| 4987 | "@ A B C " |
| 4988 | "D E F G " |
| 4989 | "H I J K " |
| 4990 | "L M N O " |
| 4991 | "P Q R S " |
| 4992 | "T U V W " |
| 4993 | "X Y Z [ " |
| 4994 | "\\\\ ] ^ _ " |
| 4995 | "` a b c " |
| 4996 | "d e f g " |
| 4997 | "h i j k " |
| 4998 | "l m n o " |
| 4999 | "p q r s " |
| 5000 | "t u v w " |
| 5001 | "x y z { " |
| 5002 | "| } ~ \177 "; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5003 | |
| 5004 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5005 | // For a string that is less than 32k characters it should always be |
| 5006 | // possible to allocate it in new space. |
| 5007 | static const int kMaxGuaranteedNewSpaceString = 32 * 1024; |
| 5008 | |
| 5009 | |
| 5010 | // Doing JSON quoting cannot make the string more than this many times larger. |
| 5011 | static const int kJsonQuoteWorstCaseBlowup = 6; |
| 5012 | |
| 5013 | |
| 5014 | // Covers the entire ASCII range (all other characters are unchanged by JSON |
| 5015 | // quoting). |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5016 | static const byte JsonQuoteLengths[kQuoteTableLength] = { |
| 5017 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5018 | 2, 2, 2, 6, 2, 2, 6, 6, |
| 5019 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5020 | 6, 6, 6, 6, 6, 6, 6, 6, |
| 5021 | 1, 1, 2, 1, 1, 1, 1, 1, |
| 5022 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5023 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5024 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5025 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5026 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5027 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5028 | 1, 1, 1, 1, 2, 1, 1, 1, |
| 5029 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5030 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5031 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5032 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 5033 | }; |
| 5034 | |
| 5035 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5036 | template <typename StringType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5037 | MaybeObject* AllocateRawString(Isolate* isolate, int length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5038 | |
| 5039 | |
| 5040 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5041 | MaybeObject* AllocateRawString<SeqTwoByteString>(Isolate* isolate, int length) { |
| 5042 | return isolate->heap()->AllocateRawTwoByteString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | |
| 5046 | template <> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5047 | MaybeObject* AllocateRawString<SeqAsciiString>(Isolate* isolate, int length) { |
| 5048 | return isolate->heap()->AllocateRawAsciiString(length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5052 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5053 | static MaybeObject* SlowQuoteJsonString(Isolate* isolate, |
| 5054 | Vector<const Char> characters) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5055 | int length = characters.length(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5056 | const Char* read_cursor = characters.start(); |
| 5057 | const Char* end = read_cursor + length; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5058 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5059 | int quoted_length = kSpaceForQuotes; |
| 5060 | while (read_cursor < end) { |
| 5061 | Char c = *(read_cursor++); |
| 5062 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5063 | quoted_length++; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5064 | } else { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5065 | quoted_length += JsonQuoteLengths[static_cast<unsigned>(c)]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5066 | } |
| 5067 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5068 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5069 | quoted_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5070 | Object* new_object; |
| 5071 | if (!new_alloc->ToObject(&new_object)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5072 | return new_alloc; |
| 5073 | } |
| 5074 | StringType* new_string = StringType::cast(new_object); |
| 5075 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5076 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5077 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5078 | if (comma) *(write_cursor++) = ','; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5079 | *(write_cursor++) = '"'; |
| 5080 | |
| 5081 | read_cursor = characters.start(); |
| 5082 | while (read_cursor < end) { |
| 5083 | Char c = *(read_cursor++); |
| 5084 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5085 | *(write_cursor++) = c; |
| 5086 | } else { |
| 5087 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5088 | const char* replacement = JsonQuotes + |
| 5089 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5090 | for (int i = 0; i < len; i++) { |
| 5091 | *write_cursor++ = *replacement++; |
| 5092 | } |
| 5093 | } |
| 5094 | } |
| 5095 | *(write_cursor++) = '"'; |
| 5096 | return new_string; |
| 5097 | } |
| 5098 | |
| 5099 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5100 | template <typename Char, typename StringType, bool comma> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5101 | static MaybeObject* QuoteJsonString(Isolate* isolate, |
| 5102 | Vector<const Char> characters) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5103 | int length = characters.length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5104 | isolate->counters()->quote_json_char_count()->Increment(length); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5105 | const int kSpaceForQuotes = 2 + (comma ? 1 :0); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5106 | int worst_case_length = length * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; |
| 5107 | if (worst_case_length > kMaxGuaranteedNewSpaceString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5108 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5111 | MaybeObject* new_alloc = AllocateRawString<StringType>(isolate, |
| 5112 | worst_case_length); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5113 | Object* new_object; |
| 5114 | if (!new_alloc->ToObject(&new_object)) { |
| 5115 | return new_alloc; |
| 5116 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5117 | if (!isolate->heap()->new_space()->Contains(new_object)) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5118 | // Even if our string is small enough to fit in new space we still have to |
| 5119 | // handle it being allocated in old space as may happen in the third |
| 5120 | // attempt. See CALL_AND_RETRY in heap-inl.h and similar code in |
| 5121 | // CEntryStub::GenerateCore. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5122 | return SlowQuoteJsonString<Char, StringType, comma>(isolate, characters); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5123 | } |
| 5124 | StringType* new_string = StringType::cast(new_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5125 | ASSERT(isolate->heap()->new_space()->Contains(new_string)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5126 | |
| 5127 | STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); |
| 5128 | Char* write_cursor = reinterpret_cast<Char*>( |
| 5129 | new_string->address() + SeqAsciiString::kHeaderSize); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5130 | if (comma) *(write_cursor++) = ','; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5131 | *(write_cursor++) = '"'; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5132 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5133 | const Char* read_cursor = characters.start(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5134 | const Char* end = read_cursor + length; |
| 5135 | while (read_cursor < end) { |
| 5136 | Char c = *(read_cursor++); |
| 5137 | if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { |
| 5138 | *(write_cursor++) = c; |
| 5139 | } else { |
| 5140 | int len = JsonQuoteLengths[static_cast<unsigned>(c)]; |
| 5141 | const char* replacement = JsonQuotes + |
| 5142 | static_cast<unsigned>(c) * kJsonQuotesCharactersPerEntry; |
| 5143 | write_cursor[0] = replacement[0]; |
| 5144 | if (len > 1) { |
| 5145 | write_cursor[1] = replacement[1]; |
| 5146 | if (len > 2) { |
| 5147 | ASSERT(len == 6); |
| 5148 | write_cursor[2] = replacement[2]; |
| 5149 | write_cursor[3] = replacement[3]; |
| 5150 | write_cursor[4] = replacement[4]; |
| 5151 | write_cursor[5] = replacement[5]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5152 | } |
| 5153 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5154 | write_cursor += len; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5155 | } |
| 5156 | } |
| 5157 | *(write_cursor++) = '"'; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 5158 | |
| 5159 | int final_length = static_cast<int>( |
| 5160 | write_cursor - reinterpret_cast<Char*>( |
| 5161 | new_string->address() + SeqAsciiString::kHeaderSize)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 5162 | isolate->heap()->new_space()-> |
| 5163 | template ShrinkStringAtAllocationBoundary<StringType>( |
| 5164 | new_string, final_length); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5165 | return new_string; |
| 5166 | } |
| 5167 | |
| 5168 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5169 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONString) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5170 | NoHandleAllocation ha; |
| 5171 | CONVERT_CHECKED(String, str, args[0]); |
| 5172 | if (!str->IsFlat()) { |
| 5173 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5174 | Object* flat; |
| 5175 | if (!try_flatten->ToObject(&flat)) { |
| 5176 | return try_flatten; |
| 5177 | } |
| 5178 | str = String::cast(flat); |
| 5179 | ASSERT(str->IsFlat()); |
| 5180 | } |
| 5181 | if (str->IsTwoByteRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5182 | return QuoteJsonString<uc16, SeqTwoByteString, false>(isolate, |
| 5183 | str->ToUC16Vector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5184 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5185 | return QuoteJsonString<char, SeqAsciiString, false>(isolate, |
| 5186 | str->ToAsciiVector()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5191 | RUNTIME_FUNCTION(MaybeObject*, Runtime_QuoteJSONStringComma) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5192 | NoHandleAllocation ha; |
| 5193 | CONVERT_CHECKED(String, str, args[0]); |
| 5194 | if (!str->IsFlat()) { |
| 5195 | MaybeObject* try_flatten = str->TryFlatten(); |
| 5196 | Object* flat; |
| 5197 | if (!try_flatten->ToObject(&flat)) { |
| 5198 | return try_flatten; |
| 5199 | } |
| 5200 | str = String::cast(flat); |
| 5201 | ASSERT(str->IsFlat()); |
| 5202 | } |
| 5203 | if (str->IsTwoByteRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5204 | return QuoteJsonString<uc16, SeqTwoByteString, true>(isolate, |
| 5205 | str->ToUC16Vector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5206 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5207 | return QuoteJsonString<char, SeqAsciiString, true>(isolate, |
| 5208 | str->ToAsciiVector()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 5209 | } |
| 5210 | } |
| 5211 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5212 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseInt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5213 | NoHandleAllocation ha; |
| 5214 | |
| 5215 | CONVERT_CHECKED(String, s, args[0]); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5216 | CONVERT_SMI_CHECKED(radix, args[1]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5217 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5218 | s->TryFlatten(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5219 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 5220 | RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5221 | double value = StringToInt(isolate->unicode_cache(), s, radix); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5222 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5223 | } |
| 5224 | |
| 5225 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5226 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringParseFloat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5227 | NoHandleAllocation ha; |
| 5228 | CONVERT_CHECKED(String, str, args[0]); |
| 5229 | |
| 5230 | // ECMA-262 section 15.1.2.3, empty string is NaN |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 5231 | double value = StringToDouble(isolate->unicode_cache(), |
| 5232 | str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5233 | |
| 5234 | // Create a number object from the value. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5235 | return isolate->heap()->NumberFromDouble(value); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5239 | template <class Converter> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5240 | MUST_USE_RESULT static MaybeObject* ConvertCaseHelper( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5241 | Isolate* isolate, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5242 | String* s, |
| 5243 | int length, |
| 5244 | int input_string_length, |
| 5245 | unibrow::Mapping<Converter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5246 | // We try this twice, once with the assumption that the result is no longer |
| 5247 | // than the input and, if that assumption breaks, again with the exact |
| 5248 | // length. This may not be pretty, but it is nicer than what was here before |
| 5249 | // and I hereby claim my vaffel-is. |
| 5250 | // |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5251 | // Allocate the resulting string. |
| 5252 | // |
| 5253 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5254 | // character is also ascii. This is currently the case, but it |
| 5255 | // might break in the future if we implement more context and locale |
| 5256 | // dependent upper/lower conversions. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5257 | Object* o; |
| 5258 | { MaybeObject* maybe_o = s->IsAsciiRepresentation() |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5259 | ? isolate->heap()->AllocateRawAsciiString(length) |
| 5260 | : isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5261 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5262 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5263 | String* result = String::cast(o); |
| 5264 | bool has_changed_character = false; |
| 5265 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5266 | // Convert all characters to upper case, assuming that they will fit |
| 5267 | // in the buffer |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5268 | Access<StringInputBuffer> buffer( |
| 5269 | isolate->runtime_state()->string_input_buffer()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5270 | buffer->Reset(s); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 5271 | unibrow::uchar chars[Converter::kMaxWidth]; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5272 | // We can assume that the string is not empty |
| 5273 | uc32 current = buffer->GetNext(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5274 | for (int i = 0; i < length;) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5275 | bool has_next = buffer->has_more(); |
| 5276 | uc32 next = has_next ? buffer->GetNext() : 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5277 | int char_length = mapping->get(current, next, chars); |
| 5278 | if (char_length == 0) { |
| 5279 | // The case conversion of this character is the character itself. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5280 | result->Set(i, current); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5281 | i++; |
| 5282 | } else if (char_length == 1) { |
| 5283 | // Common case: converting the letter resulted in one character. |
| 5284 | ASSERT(static_cast<uc32>(chars[0]) != current); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5285 | result->Set(i, chars[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5286 | has_changed_character = true; |
| 5287 | i++; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5288 | } else if (length == input_string_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5289 | // We've assumed that the result would be as long as the |
| 5290 | // input but here is a character that converts to several |
| 5291 | // characters. No matter, we calculate the exact length |
| 5292 | // of the result and try the whole thing again. |
| 5293 | // |
| 5294 | // Note that this leaves room for optimization. We could just |
| 5295 | // memcpy what we already have to the result string. Also, |
| 5296 | // the result string is the last object allocated we could |
| 5297 | // "realloc" it and probably, in the vast majority of cases, |
| 5298 | // extend the existing string to be able to hold the full |
| 5299 | // result. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5300 | int next_length = 0; |
| 5301 | if (has_next) { |
| 5302 | next_length = mapping->get(next, 0, chars); |
| 5303 | if (next_length == 0) next_length = 1; |
| 5304 | } |
| 5305 | int current_length = i + char_length + next_length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5306 | while (buffer->has_more()) { |
| 5307 | current = buffer->GetNext(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5308 | // NOTE: we use 0 as the next character here because, while |
| 5309 | // the next character may affect what a character converts to, |
| 5310 | // it does not in any case affect the length of what it convert |
| 5311 | // to. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5312 | int char_length = mapping->get(current, 0, chars); |
| 5313 | if (char_length == 0) char_length = 1; |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 5314 | current_length += char_length; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5315 | if (current_length > Smi::kMaxValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5316 | isolate->context()->mark_out_of_memory(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5317 | return Failure::OutOfMemoryException(); |
| 5318 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5319 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5320 | // Try again with the real length. |
| 5321 | return Smi::FromInt(current_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5322 | } else { |
| 5323 | for (int j = 0; j < char_length; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5324 | result->Set(i, chars[j]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5325 | i++; |
| 5326 | } |
| 5327 | has_changed_character = true; |
| 5328 | } |
| 5329 | current = next; |
| 5330 | } |
| 5331 | if (has_changed_character) { |
| 5332 | return result; |
| 5333 | } else { |
| 5334 | // If we didn't actually change anything in doing the conversion |
| 5335 | // we simple return the result and let the converted string |
| 5336 | // become garbage; there is no reason to keep two identical strings |
| 5337 | // alive. |
| 5338 | return s; |
| 5339 | } |
| 5340 | } |
| 5341 | |
| 5342 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5343 | namespace { |
| 5344 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5345 | static const uintptr_t kOneInEveryByte = kUintptrAllBitsSet / 0xFF; |
| 5346 | |
| 5347 | |
| 5348 | // Given a word and two range boundaries returns a word with high bit |
| 5349 | // set in every byte iff the corresponding input byte was strictly in |
| 5350 | // the range (m, n). All the other bits in the result are cleared. |
| 5351 | // This function is only useful when it can be inlined and the |
| 5352 | // boundaries are statically known. |
| 5353 | // Requires: all bytes in the input word and the boundaries must be |
| 5354 | // ascii (less than 0x7F). |
| 5355 | static inline uintptr_t AsciiRangeMask(uintptr_t w, char m, char n) { |
| 5356 | // Every byte in an ascii string is less than or equal to 0x7F. |
| 5357 | ASSERT((w & (kOneInEveryByte * 0x7F)) == w); |
| 5358 | // Use strict inequalities since in edge cases the function could be |
| 5359 | // further simplified. |
| 5360 | ASSERT(0 < m && m < n && n < 0x7F); |
| 5361 | // Has high bit set in every w byte less than n. |
| 5362 | uintptr_t tmp1 = kOneInEveryByte * (0x7F + n) - w; |
| 5363 | // Has high bit set in every w byte greater than m. |
| 5364 | uintptr_t tmp2 = w + kOneInEveryByte * (0x7F - m); |
| 5365 | return (tmp1 & tmp2 & (kOneInEveryByte * 0x80)); |
| 5366 | } |
| 5367 | |
| 5368 | |
| 5369 | enum AsciiCaseConversion { |
| 5370 | ASCII_TO_LOWER, |
| 5371 | ASCII_TO_UPPER |
| 5372 | }; |
| 5373 | |
| 5374 | |
| 5375 | template <AsciiCaseConversion dir> |
| 5376 | struct FastAsciiConverter { |
| 5377 | static bool Convert(char* dst, char* src, int length) { |
| 5378 | #ifdef DEBUG |
| 5379 | char* saved_dst = dst; |
| 5380 | char* saved_src = src; |
| 5381 | #endif |
| 5382 | // We rely on the distance between upper and lower case letters |
| 5383 | // being a known power of 2. |
| 5384 | ASSERT('a' - 'A' == (1 << 5)); |
| 5385 | // Boundaries for the range of input characters than require conversion. |
| 5386 | const char lo = (dir == ASCII_TO_LOWER) ? 'A' - 1 : 'a' - 1; |
| 5387 | const char hi = (dir == ASCII_TO_LOWER) ? 'Z' + 1 : 'z' + 1; |
| 5388 | bool changed = false; |
| 5389 | char* const limit = src + length; |
| 5390 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 5391 | // Process the prefix of the input that requires no conversion one |
| 5392 | // (machine) word at a time. |
| 5393 | while (src <= limit - sizeof(uintptr_t)) { |
| 5394 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 5395 | if (AsciiRangeMask(w, lo, hi) != 0) { |
| 5396 | changed = true; |
| 5397 | break; |
| 5398 | } |
| 5399 | *reinterpret_cast<uintptr_t*>(dst) = w; |
| 5400 | src += sizeof(uintptr_t); |
| 5401 | dst += sizeof(uintptr_t); |
| 5402 | } |
| 5403 | // Process the remainder of the input performing conversion when |
| 5404 | // required one word at a time. |
| 5405 | while (src <= limit - sizeof(uintptr_t)) { |
| 5406 | uintptr_t w = *reinterpret_cast<uintptr_t*>(src); |
| 5407 | uintptr_t m = AsciiRangeMask(w, lo, hi); |
| 5408 | // The mask has high (7th) bit set in every byte that needs |
| 5409 | // conversion and we know that the distance between cases is |
| 5410 | // 1 << 5. |
| 5411 | *reinterpret_cast<uintptr_t*>(dst) = w ^ (m >> 2); |
| 5412 | src += sizeof(uintptr_t); |
| 5413 | dst += sizeof(uintptr_t); |
| 5414 | } |
| 5415 | #endif |
| 5416 | // Process the last few bytes of the input (or the whole input if |
| 5417 | // unaligned access is not supported). |
| 5418 | while (src < limit) { |
| 5419 | char c = *src; |
| 5420 | if (lo < c && c < hi) { |
| 5421 | c ^= (1 << 5); |
| 5422 | changed = true; |
| 5423 | } |
| 5424 | *dst = c; |
| 5425 | ++src; |
| 5426 | ++dst; |
| 5427 | } |
| 5428 | #ifdef DEBUG |
| 5429 | CheckConvert(saved_dst, saved_src, length, changed); |
| 5430 | #endif |
| 5431 | return changed; |
| 5432 | } |
| 5433 | |
| 5434 | #ifdef DEBUG |
| 5435 | static void CheckConvert(char* dst, char* src, int length, bool changed) { |
| 5436 | bool expected_changed = false; |
| 5437 | for (int i = 0; i < length; i++) { |
| 5438 | if (dst[i] == src[i]) continue; |
| 5439 | expected_changed = true; |
| 5440 | if (dir == ASCII_TO_LOWER) { |
| 5441 | ASSERT('A' <= src[i] && src[i] <= 'Z'); |
| 5442 | ASSERT(dst[i] == src[i] + ('a' - 'A')); |
| 5443 | } else { |
| 5444 | ASSERT(dir == ASCII_TO_UPPER); |
| 5445 | ASSERT('a' <= src[i] && src[i] <= 'z'); |
| 5446 | ASSERT(dst[i] == src[i] - ('a' - 'A')); |
| 5447 | } |
| 5448 | } |
| 5449 | ASSERT(expected_changed == changed); |
| 5450 | } |
| 5451 | #endif |
| 5452 | }; |
| 5453 | |
| 5454 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5455 | struct ToLowerTraits { |
| 5456 | typedef unibrow::ToLowercase UnibrowConverter; |
| 5457 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5458 | typedef FastAsciiConverter<ASCII_TO_LOWER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5459 | }; |
| 5460 | |
| 5461 | |
| 5462 | struct ToUpperTraits { |
| 5463 | typedef unibrow::ToUppercase UnibrowConverter; |
| 5464 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5465 | typedef FastAsciiConverter<ASCII_TO_UPPER> AsciiConverter; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5466 | }; |
| 5467 | |
| 5468 | } // namespace |
| 5469 | |
| 5470 | |
| 5471 | template <typename ConvertTraits> |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5472 | MUST_USE_RESULT static MaybeObject* ConvertCase( |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5473 | Arguments args, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5474 | Isolate* isolate, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5475 | unibrow::Mapping<typename ConvertTraits::UnibrowConverter, 128>* mapping) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5476 | NoHandleAllocation ha; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5477 | CONVERT_CHECKED(String, s, args[0]); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5478 | s = s->TryFlattenGetString(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5479 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5480 | const int length = s->length(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5481 | // 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] | 5482 | if (length == 0) return s; |
| 5483 | |
| 5484 | // Simpler handling of ascii strings. |
| 5485 | // |
| 5486 | // NOTE: This assumes that the upper/lower case of an ascii |
| 5487 | // character is also ascii. This is currently the case, but it |
| 5488 | // might break in the future if we implement more context and locale |
| 5489 | // dependent upper/lower conversions. |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5490 | if (s->IsSeqAsciiString()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5491 | Object* o; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5492 | { MaybeObject* maybe_o = isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5493 | if (!maybe_o->ToObject(&o)) return maybe_o; |
| 5494 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5495 | SeqAsciiString* result = SeqAsciiString::cast(o); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5496 | bool has_changed_character = ConvertTraits::AsciiConverter::Convert( |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 5497 | result->GetChars(), SeqAsciiString::cast(s)->GetChars(), length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5498 | return has_changed_character ? result : s; |
| 5499 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5500 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5501 | Object* answer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5502 | { MaybeObject* maybe_answer = |
| 5503 | ConvertCaseHelper(isolate, s, length, length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5504 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 5505 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5506 | if (answer->IsSmi()) { |
| 5507 | // Retry with correct length. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5508 | { MaybeObject* maybe_answer = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5509 | ConvertCaseHelper(isolate, |
| 5510 | s, Smi::cast(answer)->value(), length, mapping); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5511 | if (!maybe_answer->ToObject(&answer)) return maybe_answer; |
| 5512 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5513 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5514 | return answer; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5518 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5519 | return ConvertCase<ToLowerTraits>( |
| 5520 | args, isolate, isolate->runtime_state()->to_lower_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5524 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToUpperCase) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5525 | return ConvertCase<ToUpperTraits>( |
| 5526 | args, isolate, isolate->runtime_state()->to_upper_mapping()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5529 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5530 | static inline bool IsTrimWhiteSpace(unibrow::uchar c) { |
| 5531 | return unibrow::WhiteSpace::Is(c) || c == 0x200b; |
| 5532 | } |
| 5533 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5534 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5535 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringTrim) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5536 | NoHandleAllocation ha; |
| 5537 | ASSERT(args.length() == 3); |
| 5538 | |
| 5539 | CONVERT_CHECKED(String, s, args[0]); |
| 5540 | CONVERT_BOOLEAN_CHECKED(trimLeft, args[1]); |
| 5541 | CONVERT_BOOLEAN_CHECKED(trimRight, args[2]); |
| 5542 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5543 | s->TryFlatten(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5544 | int length = s->length(); |
| 5545 | |
| 5546 | int left = 0; |
| 5547 | if (trimLeft) { |
| 5548 | while (left < length && IsTrimWhiteSpace(s->Get(left))) { |
| 5549 | left++; |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | int right = length; |
| 5554 | if (trimRight) { |
| 5555 | while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { |
| 5556 | right--; |
| 5557 | } |
| 5558 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5559 | return s->SubString(left, right); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 5560 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5561 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5562 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5563 | template <typename SubjectChar, typename PatternChar> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5564 | void FindStringIndices(Isolate* isolate, |
| 5565 | Vector<const SubjectChar> subject, |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5566 | Vector<const PatternChar> pattern, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5567 | ZoneList<int>* indices, |
| 5568 | unsigned int limit) { |
| 5569 | ASSERT(limit > 0); |
| 5570 | // Collect indices of pattern in subject, and the end-of-string index. |
| 5571 | // Stop after finding at most limit values. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5572 | StringSearch<PatternChar, SubjectChar> search(isolate, pattern); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5573 | int pattern_length = pattern.length(); |
| 5574 | int index = 0; |
| 5575 | while (limit > 0) { |
| 5576 | index = search.Search(subject, index); |
| 5577 | if (index < 0) return; |
| 5578 | indices->Add(index); |
| 5579 | index += pattern_length; |
| 5580 | limit--; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5584 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5585 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringSplit) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5586 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5587 | HandleScope handle_scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5588 | CONVERT_ARG_CHECKED(String, subject, 0); |
| 5589 | CONVERT_ARG_CHECKED(String, pattern, 1); |
| 5590 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[2]); |
| 5591 | |
| 5592 | int subject_length = subject->length(); |
| 5593 | int pattern_length = pattern->length(); |
| 5594 | RUNTIME_ASSERT(pattern_length > 0); |
| 5595 | |
| 5596 | // The limit can be very large (0xffffffffu), but since the pattern |
| 5597 | // isn't empty, we can never create more parts than ~half the length |
| 5598 | // of the subject. |
| 5599 | |
| 5600 | if (!subject->IsFlat()) FlattenString(subject); |
| 5601 | |
| 5602 | static const int kMaxInitialListCapacity = 16; |
| 5603 | |
| 5604 | ZoneScope scope(DELETE_ON_EXIT); |
| 5605 | |
| 5606 | // Find (up to limit) indices of separator and end-of-string in subject |
| 5607 | int initial_capacity = Min<uint32_t>(kMaxInitialListCapacity, limit); |
| 5608 | ZoneList<int> indices(initial_capacity); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5609 | if (!pattern->IsFlat()) FlattenString(pattern); |
| 5610 | |
| 5611 | // No allocation block. |
| 5612 | { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5613 | AssertNoAllocation nogc; |
| 5614 | if (subject->IsAsciiRepresentation()) { |
| 5615 | Vector<const char> subject_vector = subject->ToAsciiVector(); |
| 5616 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5617 | FindStringIndices(isolate, |
| 5618 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5619 | pattern->ToAsciiVector(), |
| 5620 | &indices, |
| 5621 | limit); |
| 5622 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5623 | FindStringIndices(isolate, |
| 5624 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5625 | pattern->ToUC16Vector(), |
| 5626 | &indices, |
| 5627 | limit); |
| 5628 | } |
| 5629 | } else { |
| 5630 | Vector<const uc16> subject_vector = subject->ToUC16Vector(); |
| 5631 | if (pattern->IsAsciiRepresentation()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5632 | FindStringIndices(isolate, |
| 5633 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5634 | pattern->ToAsciiVector(), |
| 5635 | &indices, |
| 5636 | limit); |
| 5637 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5638 | FindStringIndices(isolate, |
| 5639 | subject_vector, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5640 | pattern->ToUC16Vector(), |
| 5641 | &indices, |
| 5642 | limit); |
| 5643 | } |
| 5644 | } |
| 5645 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5646 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5647 | if (static_cast<uint32_t>(indices.length()) < limit) { |
| 5648 | indices.Add(subject_length); |
| 5649 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5650 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5651 | // 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] | 5652 | |
| 5653 | // Create JSArray of substrings separated by separator. |
| 5654 | int part_count = indices.length(); |
| 5655 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5656 | Handle<JSArray> result = isolate->factory()->NewJSArray(part_count); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5657 | result->set_length(Smi::FromInt(part_count)); |
| 5658 | |
| 5659 | ASSERT(result->HasFastElements()); |
| 5660 | |
| 5661 | if (part_count == 1 && indices.at(0) == subject_length) { |
| 5662 | FixedArray::cast(result->elements())->set(0, *subject); |
| 5663 | return *result; |
| 5664 | } |
| 5665 | |
| 5666 | Handle<FixedArray> elements(FixedArray::cast(result->elements())); |
| 5667 | int part_start = 0; |
| 5668 | for (int i = 0; i < part_count; i++) { |
| 5669 | HandleScope local_loop_handle; |
| 5670 | int part_end = indices.at(i); |
| 5671 | Handle<String> substring = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5672 | isolate->factory()->NewSubString(subject, part_start, part_end); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 5673 | elements->set(i, *substring); |
| 5674 | part_start = part_end + pattern_length; |
| 5675 | } |
| 5676 | |
| 5677 | return *result; |
| 5678 | } |
| 5679 | |
| 5680 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5681 | // Copies ascii characters to the given fixed array looking up |
| 5682 | // one-char strings in the cache. Gives up on the first char that is |
| 5683 | // not in the cache and fills the remainder with smi zeros. Returns |
| 5684 | // the length of the successfully copied prefix. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5685 | static int CopyCachedAsciiCharsToArray(Heap* heap, |
| 5686 | const char* chars, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5687 | FixedArray* elements, |
| 5688 | int length) { |
| 5689 | AssertNoAllocation nogc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5690 | FixedArray* ascii_cache = heap->single_character_string_cache(); |
| 5691 | Object* undefined = heap->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5692 | int i; |
| 5693 | for (i = 0; i < length; ++i) { |
| 5694 | Object* value = ascii_cache->get(chars[i]); |
| 5695 | if (value == undefined) break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5696 | ASSERT(!heap->InNewSpace(value)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5697 | elements->set(i, value, SKIP_WRITE_BARRIER); |
| 5698 | } |
| 5699 | if (i < length) { |
| 5700 | ASSERT(Smi::FromInt(0) == 0); |
| 5701 | memset(elements->data_start() + i, 0, kPointerSize * (length - i)); |
| 5702 | } |
| 5703 | #ifdef DEBUG |
| 5704 | for (int j = 0; j < length; ++j) { |
| 5705 | Object* element = elements->get(j); |
| 5706 | ASSERT(element == Smi::FromInt(0) || |
| 5707 | (element->IsString() && String::cast(element)->LooksValid())); |
| 5708 | } |
| 5709 | #endif |
| 5710 | return i; |
| 5711 | } |
| 5712 | |
| 5713 | |
| 5714 | // Converts a String to JSArray. |
| 5715 | // For example, "foo" => ["f", "o", "o"]. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5716 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToArray) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5717 | HandleScope scope(isolate); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5718 | ASSERT(args.length() == 2); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5719 | CONVERT_ARG_CHECKED(String, s, 0); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5720 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5721 | |
| 5722 | s->TryFlatten(); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 5723 | 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] | 5724 | |
| 5725 | Handle<FixedArray> elements; |
| 5726 | if (s->IsFlat() && s->IsAsciiRepresentation()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5727 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5728 | { MaybeObject* maybe_obj = |
| 5729 | isolate->heap()->AllocateUninitializedFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 5730 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 5731 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5732 | elements = Handle<FixedArray>(FixedArray::cast(obj), isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5733 | |
| 5734 | Vector<const char> chars = s->ToAsciiVector(); |
| 5735 | // Note, this will initialize all elements (not only the prefix) |
| 5736 | // to prevent GC from seeing partially initialized array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5737 | int num_copied_from_cache = CopyCachedAsciiCharsToArray(isolate->heap(), |
| 5738 | chars.start(), |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5739 | *elements, |
| 5740 | length); |
| 5741 | |
| 5742 | for (int i = num_copied_from_cache; i < length; ++i) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5743 | Handle<Object> str = LookupSingleCharacterStringFromCode(chars[i]); |
| 5744 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5745 | } |
| 5746 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5747 | elements = isolate->factory()->NewFixedArray(length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5748 | for (int i = 0; i < length; ++i) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5749 | Handle<Object> str = LookupSingleCharacterStringFromCode(s->Get(i)); |
| 5750 | elements->set(i, *str); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | #ifdef DEBUG |
| 5755 | for (int i = 0; i < length; ++i) { |
| 5756 | ASSERT(String::cast(elements->get(i))->length() == 1); |
| 5757 | } |
| 5758 | #endif |
| 5759 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5760 | return *isolate->factory()->NewJSArrayWithElements(elements); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
| 5763 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5764 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewStringWrapper) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 5765 | NoHandleAllocation ha; |
| 5766 | ASSERT(args.length() == 1); |
| 5767 | CONVERT_CHECKED(String, value, args[0]); |
| 5768 | return value->ToObject(); |
| 5769 | } |
| 5770 | |
| 5771 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5772 | bool Runtime::IsUpperCaseChar(RuntimeState* runtime_state, uint16_t ch) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 5773 | unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5774 | 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] | 5775 | return char_length == 0; |
| 5776 | } |
| 5777 | |
| 5778 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5779 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToString) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5780 | NoHandleAllocation ha; |
| 5781 | ASSERT(args.length() == 1); |
| 5782 | |
| 5783 | Object* number = args[0]; |
| 5784 | RUNTIME_ASSERT(number->IsNumber()); |
| 5785 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5786 | return isolate->heap()->NumberToString(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5790 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToStringSkipCache) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5791 | NoHandleAllocation ha; |
| 5792 | ASSERT(args.length() == 1); |
| 5793 | |
| 5794 | Object* number = args[0]; |
| 5795 | RUNTIME_ASSERT(number->IsNumber()); |
| 5796 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5797 | return isolate->heap()->NumberToString(number, false); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 5798 | } |
| 5799 | |
| 5800 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5801 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToInteger) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5802 | NoHandleAllocation ha; |
| 5803 | ASSERT(args.length() == 1); |
| 5804 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5805 | CONVERT_DOUBLE_CHECKED(number, args[0]); |
| 5806 | |
| 5807 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5808 | if (number > 0 && number <= Smi::kMaxValue) { |
| 5809 | return Smi::FromInt(static_cast<int>(number)); |
| 5810 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5811 | return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5815 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5816 | NoHandleAllocation ha; |
| 5817 | ASSERT(args.length() == 1); |
| 5818 | |
| 5819 | CONVERT_DOUBLE_CHECKED(number, args[0]); |
| 5820 | |
| 5821 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5822 | if (number > 0 && number <= Smi::kMaxValue) { |
| 5823 | return Smi::FromInt(static_cast<int>(number)); |
| 5824 | } |
| 5825 | |
| 5826 | double double_value = DoubleToInteger(number); |
| 5827 | // Map both -0 and +0 to +0. |
| 5828 | if (double_value == 0) double_value = 0; |
| 5829 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5830 | return isolate->heap()->NumberFromDouble(double_value); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5834 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSUint32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5835 | NoHandleAllocation ha; |
| 5836 | ASSERT(args.length() == 1); |
| 5837 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5838 | CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5839 | return isolate->heap()->NumberFromUint32(number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5843 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToJSInt32) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5844 | NoHandleAllocation ha; |
| 5845 | ASSERT(args.length() == 1); |
| 5846 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5847 | CONVERT_DOUBLE_CHECKED(number, args[0]); |
| 5848 | |
| 5849 | // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5850 | if (number > 0 && number <= Smi::kMaxValue) { |
| 5851 | return Smi::FromInt(static_cast<int>(number)); |
| 5852 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5853 | return isolate->heap()->NumberFromInt32(DoubleToInt32(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5857 | // Converts a Number to a Smi, if possible. Returns NaN if the number is not |
| 5858 | // a small integer. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5859 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToSmi) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5860 | NoHandleAllocation ha; |
| 5861 | ASSERT(args.length() == 1); |
| 5862 | |
| 5863 | Object* obj = args[0]; |
| 5864 | if (obj->IsSmi()) { |
| 5865 | return obj; |
| 5866 | } |
| 5867 | if (obj->IsHeapNumber()) { |
| 5868 | double value = HeapNumber::cast(obj)->value(); |
| 5869 | int int_value = FastD2I(value); |
| 5870 | if (value == FastI2D(int_value) && Smi::IsValid(int_value)) { |
| 5871 | return Smi::FromInt(int_value); |
| 5872 | } |
| 5873 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5874 | return isolate->heap()->nan_value(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 5877 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5878 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateHeapNumber) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5879 | NoHandleAllocation ha; |
| 5880 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5881 | return isolate->heap()->AllocateHeapNumber(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5885 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5886 | NoHandleAllocation ha; |
| 5887 | ASSERT(args.length() == 2); |
| 5888 | |
| 5889 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5890 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5891 | return isolate->heap()->NumberFromDouble(x + y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5895 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSub) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5896 | NoHandleAllocation ha; |
| 5897 | ASSERT(args.length() == 2); |
| 5898 | |
| 5899 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5900 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5901 | return isolate->heap()->NumberFromDouble(x - y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5905 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMul) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5906 | NoHandleAllocation ha; |
| 5907 | ASSERT(args.length() == 2); |
| 5908 | |
| 5909 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5910 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5911 | return isolate->heap()->NumberFromDouble(x * y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5915 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberUnaryMinus) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5916 | NoHandleAllocation ha; |
| 5917 | ASSERT(args.length() == 1); |
| 5918 | |
| 5919 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5920 | return isolate->heap()->NumberFromDouble(-x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5924 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAlloc) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 5925 | NoHandleAllocation ha; |
| 5926 | ASSERT(args.length() == 0); |
| 5927 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5928 | return isolate->heap()->NumberFromDouble(9876543210.0); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5932 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberDiv) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5933 | NoHandleAllocation ha; |
| 5934 | ASSERT(args.length() == 2); |
| 5935 | |
| 5936 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5937 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5938 | return isolate->heap()->NumberFromDouble(x / y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5942 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberMod) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5943 | NoHandleAllocation ha; |
| 5944 | ASSERT(args.length() == 2); |
| 5945 | |
| 5946 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 5947 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 5948 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 5949 | x = modulo(x, y); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 5950 | // NumberFromDouble may return a Smi instead of a Number object |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5951 | return isolate->heap()->NumberFromDouble(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 5955 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringAdd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5956 | NoHandleAllocation ha; |
| 5957 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5958 | CONVERT_CHECKED(String, str1, args[0]); |
| 5959 | CONVERT_CHECKED(String, str2, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 5960 | isolate->counters()->string_add_runtime()->Increment(); |
| 5961 | return isolate->heap()->AllocateConsString(str1, str2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 5965 | template <typename sinkchar> |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5966 | static inline void StringBuilderConcatHelper(String* special, |
| 5967 | sinkchar* sink, |
| 5968 | FixedArray* fixed_array, |
| 5969 | int array_length) { |
| 5970 | int position = 0; |
| 5971 | for (int i = 0; i < array_length; i++) { |
| 5972 | Object* element = fixed_array->get(i); |
| 5973 | if (element->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5974 | // Smi encoding of position and length. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5975 | int encoded_slice = Smi::cast(element)->value(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 5976 | int pos; |
| 5977 | int len; |
| 5978 | if (encoded_slice > 0) { |
| 5979 | // Position and length encoded in one smi. |
| 5980 | pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 5981 | len = StringBuilderSubstringLength::decode(encoded_slice); |
| 5982 | } else { |
| 5983 | // Position and length encoded in two smis. |
| 5984 | Object* obj = fixed_array->get(++i); |
| 5985 | ASSERT(obj->IsSmi()); |
| 5986 | pos = Smi::cast(obj)->value(); |
| 5987 | len = -encoded_slice; |
| 5988 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5989 | String::WriteToFlat(special, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 5990 | sink + position, |
| 5991 | pos, |
| 5992 | pos + len); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5993 | position += len; |
| 5994 | } else { |
| 5995 | String* string = String::cast(element); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 5996 | int element_length = string->length(); |
| 5997 | String::WriteToFlat(string, sink + position, 0, element_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5998 | position += element_length; |
| 5999 | } |
| 6000 | } |
| 6001 | } |
| 6002 | |
| 6003 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6004 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderConcat) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6005 | NoHandleAllocation ha; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6006 | ASSERT(args.length() == 3); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6007 | CONVERT_CHECKED(JSArray, array, args[0]); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6008 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6009 | isolate->context()->mark_out_of_memory(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6010 | return Failure::OutOfMemoryException(); |
| 6011 | } |
| 6012 | int array_length = Smi::cast(args[1])->value(); |
| 6013 | CONVERT_CHECKED(String, special, args[2]); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6014 | |
| 6015 | // This assumption is used by the slice encoding in one or two smis. |
| 6016 | ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 6017 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6018 | int special_length = special->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6019 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6020 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6021 | } |
| 6022 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6023 | if (fixed_array->length() < array_length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6024 | array_length = fixed_array->length(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6025 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6026 | |
| 6027 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6028 | return isolate->heap()->empty_string(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6029 | } else if (array_length == 1) { |
| 6030 | Object* first = fixed_array->get(0); |
| 6031 | if (first->IsString()) return first; |
| 6032 | } |
| 6033 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6034 | bool ascii = special->HasOnlyAsciiChars(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6035 | int position = 0; |
| 6036 | for (int i = 0; i < array_length; i++) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6037 | int increment = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6038 | Object* elt = fixed_array->get(i); |
| 6039 | if (elt->IsSmi()) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6040 | // Smi encoding of position and length. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6041 | int smi_value = Smi::cast(elt)->value(); |
| 6042 | int pos; |
| 6043 | int len; |
| 6044 | if (smi_value > 0) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6045 | // Position and length encoded in one smi. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6046 | pos = StringBuilderSubstringPosition::decode(smi_value); |
| 6047 | len = StringBuilderSubstringLength::decode(smi_value); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6048 | } else { |
| 6049 | // Position and length encoded in two smis. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6050 | len = -smi_value; |
| 6051 | // Get the position and check that it is a positive smi. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6052 | i++; |
| 6053 | if (i >= array_length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6054 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6055 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6056 | Object* next_smi = fixed_array->get(i); |
| 6057 | if (!next_smi->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6058 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6059 | } |
| 6060 | pos = Smi::cast(next_smi)->value(); |
| 6061 | if (pos < 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6062 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6063 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6064 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6065 | ASSERT(pos >= 0); |
| 6066 | ASSERT(len >= 0); |
| 6067 | if (pos > special_length || len > special_length - pos) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6068 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 6069 | } |
| 6070 | increment = len; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6071 | } else if (elt->IsString()) { |
| 6072 | String* element = String::cast(elt); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6073 | int element_length = element->length(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6074 | increment = element_length; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 6075 | if (ascii && !element->HasOnlyAsciiChars()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6076 | ascii = false; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6077 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6078 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6079 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6080 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6081 | if (increment > String::kMaxLength - position) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6082 | isolate->context()->mark_out_of_memory(); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 6083 | return Failure::OutOfMemoryException(); |
| 6084 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6085 | position += increment; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | int length = position; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6089 | Object* object; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6090 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6091 | if (ascii) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6092 | { MaybeObject* maybe_object = |
| 6093 | isolate->heap()->AllocateRawAsciiString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6094 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6095 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6096 | SeqAsciiString* answer = SeqAsciiString::cast(object); |
| 6097 | StringBuilderConcatHelper(special, |
| 6098 | answer->GetChars(), |
| 6099 | fixed_array, |
| 6100 | array_length); |
| 6101 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6102 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6103 | { MaybeObject* maybe_object = |
| 6104 | isolate->heap()->AllocateRawTwoByteString(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6105 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6106 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6107 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6108 | StringBuilderConcatHelper(special, |
| 6109 | answer->GetChars(), |
| 6110 | fixed_array, |
| 6111 | array_length); |
| 6112 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6113 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6114 | } |
| 6115 | |
| 6116 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6117 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringBuilderJoin) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6118 | NoHandleAllocation ha; |
| 6119 | ASSERT(args.length() == 3); |
| 6120 | CONVERT_CHECKED(JSArray, array, args[0]); |
| 6121 | if (!args[1]->IsSmi()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6122 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6123 | return Failure::OutOfMemoryException(); |
| 6124 | } |
| 6125 | int array_length = Smi::cast(args[1])->value(); |
| 6126 | CONVERT_CHECKED(String, separator, args[2]); |
| 6127 | |
| 6128 | if (!array->HasFastElements()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6129 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6130 | } |
| 6131 | FixedArray* fixed_array = FixedArray::cast(array->elements()); |
| 6132 | if (fixed_array->length() < array_length) { |
| 6133 | array_length = fixed_array->length(); |
| 6134 | } |
| 6135 | |
| 6136 | if (array_length == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6137 | return isolate->heap()->empty_string(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6138 | } else if (array_length == 1) { |
| 6139 | Object* first = fixed_array->get(0); |
| 6140 | if (first->IsString()) return first; |
| 6141 | } |
| 6142 | |
| 6143 | int separator_length = separator->length(); |
| 6144 | int max_nof_separators = |
| 6145 | (String::kMaxLength + separator_length - 1) / separator_length; |
| 6146 | if (max_nof_separators < (array_length - 1)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6147 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6148 | return Failure::OutOfMemoryException(); |
| 6149 | } |
| 6150 | int length = (array_length - 1) * separator_length; |
| 6151 | for (int i = 0; i < array_length; i++) { |
| 6152 | Object* element_obj = fixed_array->get(i); |
| 6153 | if (!element_obj->IsString()) { |
| 6154 | // TODO(1161): handle this case. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6155 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6156 | } |
| 6157 | String* element = String::cast(element_obj); |
| 6158 | int increment = element->length(); |
| 6159 | if (increment > String::kMaxLength - length) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6160 | isolate->context()->mark_out_of_memory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6161 | return Failure::OutOfMemoryException(); |
| 6162 | } |
| 6163 | length += increment; |
| 6164 | } |
| 6165 | |
| 6166 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6167 | { MaybeObject* maybe_object = |
| 6168 | isolate->heap()->AllocateRawTwoByteString(length); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6169 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 6170 | } |
| 6171 | SeqTwoByteString* answer = SeqTwoByteString::cast(object); |
| 6172 | |
| 6173 | uc16* sink = answer->GetChars(); |
| 6174 | #ifdef DEBUG |
| 6175 | uc16* end = sink + length; |
| 6176 | #endif |
| 6177 | |
| 6178 | String* first = String::cast(fixed_array->get(0)); |
| 6179 | int first_length = first->length(); |
| 6180 | String::WriteToFlat(first, sink, 0, first_length); |
| 6181 | sink += first_length; |
| 6182 | |
| 6183 | for (int i = 1; i < array_length; i++) { |
| 6184 | ASSERT(sink + separator_length <= end); |
| 6185 | String::WriteToFlat(separator, sink, 0, separator_length); |
| 6186 | sink += separator_length; |
| 6187 | |
| 6188 | String* element = String::cast(fixed_array->get(i)); |
| 6189 | int element_length = element->length(); |
| 6190 | ASSERT(sink + element_length <= end); |
| 6191 | String::WriteToFlat(element, sink, 0, element_length); |
| 6192 | sink += element_length; |
| 6193 | } |
| 6194 | ASSERT(sink == end); |
| 6195 | |
| 6196 | ASSERT(!answer->HasOnlyAsciiChars()); // Use %_FastAsciiArrayJoin instead. |
| 6197 | return answer; |
| 6198 | } |
| 6199 | |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 6200 | template <typename Char> |
| 6201 | static void JoinSparseArrayWithSeparator(FixedArray* elements, |
| 6202 | int elements_length, |
| 6203 | uint32_t array_length, |
| 6204 | String* separator, |
| 6205 | Vector<Char> buffer) { |
| 6206 | int previous_separator_position = 0; |
| 6207 | int separator_length = separator->length(); |
| 6208 | int cursor = 0; |
| 6209 | for (int i = 0; i < elements_length; i += 2) { |
| 6210 | int position = NumberToInt32(elements->get(i)); |
| 6211 | String* string = String::cast(elements->get(i + 1)); |
| 6212 | int string_length = string->length(); |
| 6213 | if (string->length() > 0) { |
| 6214 | while (previous_separator_position < position) { |
| 6215 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6216 | 0, separator_length); |
| 6217 | cursor += separator_length; |
| 6218 | previous_separator_position++; |
| 6219 | } |
| 6220 | String::WriteToFlat<Char>(string, &buffer[cursor], |
| 6221 | 0, string_length); |
| 6222 | cursor += string->length(); |
| 6223 | } |
| 6224 | } |
| 6225 | if (separator_length > 0) { |
| 6226 | // Array length must be representable as a signed 32-bit number, |
| 6227 | // otherwise the total string length would have been too large. |
| 6228 | ASSERT(array_length <= 0x7fffffff); // Is int32_t. |
| 6229 | int last_array_index = static_cast<int>(array_length - 1); |
| 6230 | while (previous_separator_position < last_array_index) { |
| 6231 | String::WriteToFlat<Char>(separator, &buffer[cursor], |
| 6232 | 0, separator_length); |
| 6233 | cursor += separator_length; |
| 6234 | previous_separator_position++; |
| 6235 | } |
| 6236 | } |
| 6237 | ASSERT(cursor <= buffer.length()); |
| 6238 | } |
| 6239 | |
| 6240 | |
| 6241 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SparseJoinWithSeparator) { |
| 6242 | NoHandleAllocation ha; |
| 6243 | ASSERT(args.length() == 3); |
| 6244 | CONVERT_CHECKED(JSArray, elements_array, args[0]); |
| 6245 | RUNTIME_ASSERT(elements_array->HasFastElements()); |
| 6246 | CONVERT_NUMBER_CHECKED(uint32_t, array_length, Uint32, args[1]); |
| 6247 | CONVERT_CHECKED(String, separator, args[2]); |
| 6248 | // elements_array is fast-mode JSarray of alternating positions |
| 6249 | // (increasing order) and strings. |
| 6250 | // array_length is length of original array (used to add separators); |
| 6251 | // separator is string to put between elements. Assumed to be non-empty. |
| 6252 | |
| 6253 | // Find total length of join result. |
| 6254 | int string_length = 0; |
| 6255 | bool is_ascii = true; |
| 6256 | int max_string_length = SeqAsciiString::kMaxLength; |
| 6257 | bool overflow = false; |
| 6258 | CONVERT_NUMBER_CHECKED(int, elements_length, |
| 6259 | Int32, elements_array->length()); |
| 6260 | RUNTIME_ASSERT((elements_length & 1) == 0); // Even length. |
| 6261 | FixedArray* elements = FixedArray::cast(elements_array->elements()); |
| 6262 | for (int i = 0; i < elements_length; i += 2) { |
| 6263 | RUNTIME_ASSERT(elements->get(i)->IsNumber()); |
| 6264 | CONVERT_CHECKED(String, string, elements->get(i + 1)); |
| 6265 | int length = string->length(); |
| 6266 | if (is_ascii && !string->IsAsciiRepresentation()) { |
| 6267 | is_ascii = false; |
| 6268 | max_string_length = SeqTwoByteString::kMaxLength; |
| 6269 | } |
| 6270 | if (length > max_string_length || |
| 6271 | max_string_length - length < string_length) { |
| 6272 | overflow = true; |
| 6273 | break; |
| 6274 | } |
| 6275 | string_length += length; |
| 6276 | } |
| 6277 | int separator_length = separator->length(); |
| 6278 | if (!overflow && separator_length > 0) { |
| 6279 | if (array_length <= 0x7fffffffu) { |
| 6280 | int separator_count = static_cast<int>(array_length) - 1; |
| 6281 | int remaining_length = max_string_length - string_length; |
| 6282 | if ((remaining_length / separator_length) >= separator_count) { |
| 6283 | string_length += separator_length * (array_length - 1); |
| 6284 | } else { |
| 6285 | // Not room for the separators within the maximal string length. |
| 6286 | overflow = true; |
| 6287 | } |
| 6288 | } else { |
| 6289 | // Nonempty separator and at least 2^31-1 separators necessary |
| 6290 | // means that the string is too large to create. |
| 6291 | STATIC_ASSERT(String::kMaxLength < 0x7fffffff); |
| 6292 | overflow = true; |
| 6293 | } |
| 6294 | } |
| 6295 | if (overflow) { |
| 6296 | // Throw OutOfMemory exception for creating too large a string. |
| 6297 | V8::FatalProcessOutOfMemory("Array join result too large."); |
| 6298 | } |
| 6299 | |
| 6300 | if (is_ascii) { |
| 6301 | MaybeObject* result_allocation = |
| 6302 | isolate->heap()->AllocateRawAsciiString(string_length); |
| 6303 | if (result_allocation->IsFailure()) return result_allocation; |
| 6304 | SeqAsciiString* result_string = |
| 6305 | SeqAsciiString::cast(result_allocation->ToObjectUnchecked()); |
| 6306 | JoinSparseArrayWithSeparator<char>(elements, |
| 6307 | elements_length, |
| 6308 | array_length, |
| 6309 | separator, |
| 6310 | Vector<char>(result_string->GetChars(), |
| 6311 | string_length)); |
| 6312 | return result_string; |
| 6313 | } else { |
| 6314 | MaybeObject* result_allocation = |
| 6315 | isolate->heap()->AllocateRawTwoByteString(string_length); |
| 6316 | if (result_allocation->IsFailure()) return result_allocation; |
| 6317 | SeqTwoByteString* result_string = |
| 6318 | SeqTwoByteString::cast(result_allocation->ToObjectUnchecked()); |
| 6319 | JoinSparseArrayWithSeparator<uc16>(elements, |
| 6320 | elements_length, |
| 6321 | array_length, |
| 6322 | separator, |
| 6323 | Vector<uc16>(result_string->GetChars(), |
| 6324 | string_length)); |
| 6325 | return result_string; |
| 6326 | } |
| 6327 | } |
| 6328 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 6329 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6330 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberOr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6331 | NoHandleAllocation ha; |
| 6332 | ASSERT(args.length() == 2); |
| 6333 | |
| 6334 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6335 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6336 | return isolate->heap()->NumberFromInt32(x | y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6337 | } |
| 6338 | |
| 6339 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6340 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberAnd) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6341 | NoHandleAllocation ha; |
| 6342 | ASSERT(args.length() == 2); |
| 6343 | |
| 6344 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6345 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6346 | return isolate->heap()->NumberFromInt32(x & y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6350 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberXor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6351 | NoHandleAllocation ha; |
| 6352 | ASSERT(args.length() == 2); |
| 6353 | |
| 6354 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6355 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6356 | return isolate->heap()->NumberFromInt32(x ^ y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6360 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberNot) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6361 | NoHandleAllocation ha; |
| 6362 | ASSERT(args.length() == 1); |
| 6363 | |
| 6364 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6365 | return isolate->heap()->NumberFromInt32(~x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6369 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShl) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6370 | NoHandleAllocation ha; |
| 6371 | ASSERT(args.length() == 2); |
| 6372 | |
| 6373 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6374 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6375 | return isolate->heap()->NumberFromInt32(x << (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6379 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberShr) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6380 | NoHandleAllocation ha; |
| 6381 | ASSERT(args.length() == 2); |
| 6382 | |
| 6383 | CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]); |
| 6384 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6385 | return isolate->heap()->NumberFromUint32(x >> (y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
| 6388 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6389 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberSar) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6390 | NoHandleAllocation ha; |
| 6391 | ASSERT(args.length() == 2); |
| 6392 | |
| 6393 | CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]); |
| 6394 | CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6395 | return isolate->heap()->NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6399 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6400 | NoHandleAllocation ha; |
| 6401 | ASSERT(args.length() == 2); |
| 6402 | |
| 6403 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6404 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6405 | if (isnan(x)) return Smi::FromInt(NOT_EQUAL); |
| 6406 | if (isnan(y)) return Smi::FromInt(NOT_EQUAL); |
| 6407 | if (x == y) return Smi::FromInt(EQUAL); |
| 6408 | Object* result; |
| 6409 | if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) { |
| 6410 | result = Smi::FromInt(EQUAL); |
| 6411 | } else { |
| 6412 | result = Smi::FromInt(NOT_EQUAL); |
| 6413 | } |
| 6414 | return result; |
| 6415 | } |
| 6416 | |
| 6417 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6418 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringEquals) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6419 | NoHandleAllocation ha; |
| 6420 | ASSERT(args.length() == 2); |
| 6421 | |
| 6422 | CONVERT_CHECKED(String, x, args[0]); |
| 6423 | CONVERT_CHECKED(String, y, args[1]); |
| 6424 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6425 | bool not_equal = !x->Equals(y); |
| 6426 | // This is slightly convoluted because the value that signifies |
| 6427 | // equality is 0 and inequality is 1 so we have to negate the result |
| 6428 | // from String::Equals. |
| 6429 | ASSERT(not_equal == 0 || not_equal == 1); |
| 6430 | STATIC_CHECK(EQUAL == 0); |
| 6431 | STATIC_CHECK(NOT_EQUAL == 1); |
| 6432 | return Smi::FromInt(not_equal); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
| 6435 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6436 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6437 | NoHandleAllocation ha; |
| 6438 | ASSERT(args.length() == 3); |
| 6439 | |
| 6440 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6441 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6442 | if (isnan(x) || isnan(y)) return args[2]; |
| 6443 | if (x == y) return Smi::FromInt(EQUAL); |
| 6444 | if (isless(x, y)) return Smi::FromInt(LESS); |
| 6445 | return Smi::FromInt(GREATER); |
| 6446 | } |
| 6447 | |
| 6448 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6449 | // Compare two Smis as if they were converted to strings and then |
| 6450 | // compared lexicographically. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6451 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SmiLexicographicCompare) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6452 | NoHandleAllocation ha; |
| 6453 | ASSERT(args.length() == 2); |
| 6454 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6455 | // Extract the integer values from the Smis. |
| 6456 | CONVERT_CHECKED(Smi, x, args[0]); |
| 6457 | CONVERT_CHECKED(Smi, y, args[1]); |
| 6458 | int x_value = x->value(); |
| 6459 | int y_value = y->value(); |
| 6460 | |
| 6461 | // If the integers are equal so are the string representations. |
| 6462 | if (x_value == y_value) return Smi::FromInt(EQUAL); |
| 6463 | |
| 6464 | // If one of the integers are zero the normal integer order is the |
| 6465 | // same as the lexicographic order of the string representations. |
| 6466 | if (x_value == 0 || y_value == 0) return Smi::FromInt(x_value - y_value); |
| 6467 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 6468 | // 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] | 6469 | // smallest because the char code of '-' is less than the char code |
| 6470 | // of any digit. Otherwise, we make both values positive. |
| 6471 | if (x_value < 0 || y_value < 0) { |
| 6472 | if (y_value >= 0) return Smi::FromInt(LESS); |
| 6473 | if (x_value >= 0) return Smi::FromInt(GREATER); |
| 6474 | x_value = -x_value; |
| 6475 | y_value = -y_value; |
| 6476 | } |
| 6477 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6478 | // Arrays for the individual characters of the two Smis. Smis are |
| 6479 | // 31 bit integers and 10 decimal digits are therefore enough. |
| 6480 | // TODO(isolates): maybe we should simply allocate 20 bytes on the stack. |
| 6481 | int* x_elms = isolate->runtime_state()->smi_lexicographic_compare_x_elms(); |
| 6482 | int* y_elms = isolate->runtime_state()->smi_lexicographic_compare_y_elms(); |
| 6483 | |
| 6484 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 6485 | // Convert the integers to arrays of their decimal digits. |
| 6486 | int x_index = 0; |
| 6487 | int y_index = 0; |
| 6488 | while (x_value > 0) { |
| 6489 | x_elms[x_index++] = x_value % 10; |
| 6490 | x_value /= 10; |
| 6491 | } |
| 6492 | while (y_value > 0) { |
| 6493 | y_elms[y_index++] = y_value % 10; |
| 6494 | y_value /= 10; |
| 6495 | } |
| 6496 | |
| 6497 | // Loop through the arrays of decimal digits finding the first place |
| 6498 | // where they differ. |
| 6499 | while (--x_index >= 0 && --y_index >= 0) { |
| 6500 | int diff = x_elms[x_index] - y_elms[y_index]; |
| 6501 | if (diff != 0) return Smi::FromInt(diff); |
| 6502 | } |
| 6503 | |
| 6504 | // If one array is a suffix of the other array, the longest array is |
| 6505 | // the representation of the largest of the Smis in the |
| 6506 | // lexicographic ordering. |
| 6507 | return Smi::FromInt(x_index - y_index); |
| 6508 | } |
| 6509 | |
| 6510 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6511 | static Object* StringInputBufferCompare(RuntimeState* state, |
| 6512 | String* x, |
| 6513 | String* y) { |
| 6514 | StringInputBuffer& bufx = *state->string_input_buffer_compare_bufx(); |
| 6515 | StringInputBuffer& bufy = *state->string_input_buffer_compare_bufy(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6516 | bufx.Reset(x); |
| 6517 | bufy.Reset(y); |
| 6518 | while (bufx.has_more() && bufy.has_more()) { |
| 6519 | int d = bufx.GetNext() - bufy.GetNext(); |
| 6520 | if (d < 0) return Smi::FromInt(LESS); |
| 6521 | else if (d > 0) return Smi::FromInt(GREATER); |
| 6522 | } |
| 6523 | |
| 6524 | // x is (non-trivial) prefix of y: |
| 6525 | if (bufy.has_more()) return Smi::FromInt(LESS); |
| 6526 | // y is prefix of x: |
| 6527 | return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL); |
| 6528 | } |
| 6529 | |
| 6530 | |
| 6531 | static Object* FlatStringCompare(String* x, String* y) { |
| 6532 | ASSERT(x->IsFlat()); |
| 6533 | ASSERT(y->IsFlat()); |
| 6534 | Object* equal_prefix_result = Smi::FromInt(EQUAL); |
| 6535 | int prefix_length = x->length(); |
| 6536 | if (y->length() < prefix_length) { |
| 6537 | prefix_length = y->length(); |
| 6538 | equal_prefix_result = Smi::FromInt(GREATER); |
| 6539 | } else if (y->length() > prefix_length) { |
| 6540 | equal_prefix_result = Smi::FromInt(LESS); |
| 6541 | } |
| 6542 | int r; |
| 6543 | if (x->IsAsciiRepresentation()) { |
| 6544 | Vector<const char> x_chars = x->ToAsciiVector(); |
| 6545 | if (y->IsAsciiRepresentation()) { |
| 6546 | Vector<const char> y_chars = y->ToAsciiVector(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 6547 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6548 | } else { |
| 6549 | Vector<const uc16> y_chars = y->ToUC16Vector(); |
| 6550 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6551 | } |
| 6552 | } else { |
| 6553 | Vector<const uc16> x_chars = x->ToUC16Vector(); |
| 6554 | if (y->IsAsciiRepresentation()) { |
| 6555 | Vector<const char> y_chars = y->ToAsciiVector(); |
| 6556 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6557 | } else { |
| 6558 | Vector<const uc16> y_chars = y->ToUC16Vector(); |
| 6559 | r = CompareChars(x_chars.start(), y_chars.start(), prefix_length); |
| 6560 | } |
| 6561 | } |
| 6562 | Object* result; |
| 6563 | if (r == 0) { |
| 6564 | result = equal_prefix_result; |
| 6565 | } else { |
| 6566 | result = (r < 0) ? Smi::FromInt(LESS) : Smi::FromInt(GREATER); |
| 6567 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6568 | ASSERT(result == |
| 6569 | StringInputBufferCompare(Isolate::Current()->runtime_state(), x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6570 | return result; |
| 6571 | } |
| 6572 | |
| 6573 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6574 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StringCompare) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6575 | NoHandleAllocation ha; |
| 6576 | ASSERT(args.length() == 2); |
| 6577 | |
| 6578 | CONVERT_CHECKED(String, x, args[0]); |
| 6579 | CONVERT_CHECKED(String, y, args[1]); |
| 6580 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6581 | isolate->counters()->string_compare_runtime()->Increment(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 6582 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6583 | // A few fast case tests before we flatten. |
| 6584 | if (x == y) return Smi::FromInt(EQUAL); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6585 | if (y->length() == 0) { |
| 6586 | if (x->length() == 0) return Smi::FromInt(EQUAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6587 | return Smi::FromInt(GREATER); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6588 | } else if (x->length() == 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6589 | return Smi::FromInt(LESS); |
| 6590 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6591 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 6592 | int d = x->Get(0) - y->Get(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6593 | if (d < 0) return Smi::FromInt(LESS); |
| 6594 | else if (d > 0) return Smi::FromInt(GREATER); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6595 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6596 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6597 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(x); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6598 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6599 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6600 | { MaybeObject* maybe_obj = isolate->heap()->PrepareForCompare(y); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 6601 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 6602 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6603 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6604 | return (x->IsFlat() && y->IsFlat()) ? FlatStringCompare(x, y) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6605 | : StringInputBufferCompare(isolate->runtime_state(), x, y); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6609 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_acos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6610 | NoHandleAllocation ha; |
| 6611 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6612 | isolate->counters()->math_acos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6613 | |
| 6614 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6615 | return isolate->transcendental_cache()->Get(TranscendentalCache::ACOS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
| 6618 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6619 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_asin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6620 | NoHandleAllocation ha; |
| 6621 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6622 | isolate->counters()->math_asin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6623 | |
| 6624 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6625 | return isolate->transcendental_cache()->Get(TranscendentalCache::ASIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6626 | } |
| 6627 | |
| 6628 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6629 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6630 | NoHandleAllocation ha; |
| 6631 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6632 | isolate->counters()->math_atan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6633 | |
| 6634 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6635 | return isolate->transcendental_cache()->Get(TranscendentalCache::ATAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6636 | } |
| 6637 | |
| 6638 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6639 | static const double kPiDividedBy4 = 0.78539816339744830962; |
| 6640 | |
| 6641 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6642 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_atan2) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6643 | NoHandleAllocation ha; |
| 6644 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6645 | isolate->counters()->math_atan2()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6646 | |
| 6647 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6648 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6649 | double result; |
| 6650 | if (isinf(x) && isinf(y)) { |
| 6651 | // Make sure that the result in case of two infinite arguments |
| 6652 | // is a multiple of Pi / 4. The sign of the result is determined |
| 6653 | // by the first argument (x) and the sign of the second argument |
| 6654 | // determines the multiplier: one or three. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6655 | int multiplier = (x < 0) ? -1 : 1; |
| 6656 | if (y < 0) multiplier *= 3; |
| 6657 | result = multiplier * kPiDividedBy4; |
| 6658 | } else { |
| 6659 | result = atan2(x, y); |
| 6660 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6661 | return isolate->heap()->AllocateHeapNumber(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
| 6664 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6665 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_ceil) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6666 | NoHandleAllocation ha; |
| 6667 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6668 | isolate->counters()->math_ceil()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6669 | |
| 6670 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6671 | return isolate->heap()->NumberFromDouble(ceiling(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6672 | } |
| 6673 | |
| 6674 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6675 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_cos) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6676 | NoHandleAllocation ha; |
| 6677 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6678 | isolate->counters()->math_cos()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6679 | |
| 6680 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6681 | return isolate->transcendental_cache()->Get(TranscendentalCache::COS, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6685 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_exp) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6686 | NoHandleAllocation ha; |
| 6687 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6688 | isolate->counters()->math_exp()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6689 | |
| 6690 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6691 | return isolate->transcendental_cache()->Get(TranscendentalCache::EXP, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6695 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_floor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6696 | NoHandleAllocation ha; |
| 6697 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6698 | isolate->counters()->math_floor()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6699 | |
| 6700 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6701 | return isolate->heap()->NumberFromDouble(floor(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6705 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_log) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6706 | NoHandleAllocation ha; |
| 6707 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6708 | isolate->counters()->math_log()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6709 | |
| 6710 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6711 | return isolate->transcendental_cache()->Get(TranscendentalCache::LOG, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6715 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6716 | NoHandleAllocation ha; |
| 6717 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6718 | isolate->counters()->math_pow()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6719 | |
| 6720 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6721 | |
| 6722 | // If the second argument is a smi, it is much faster to call the |
| 6723 | // custom powi() function than the generic pow(). |
| 6724 | if (args[1]->IsSmi()) { |
| 6725 | int y = Smi::cast(args[1])->value(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6726 | return isolate->heap()->NumberFromDouble(power_double_int(x, y)); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 6727 | } |
| 6728 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6729 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6730 | return isolate->heap()->AllocateHeapNumber(power_double_double(x, y)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6733 | // Fast version of Math.pow if we know that y is not an integer and |
| 6734 | // 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] | 6735 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_pow_cfunction) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6736 | NoHandleAllocation ha; |
| 6737 | ASSERT(args.length() == 2); |
| 6738 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
| 6739 | CONVERT_DOUBLE_CHECKED(y, args[1]); |
| 6740 | if (y == 0) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 6741 | return Smi::FromInt(1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6742 | } else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6743 | return isolate->heap()->nan_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6744 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6745 | return isolate->heap()->AllocateHeapNumber(pow(x, y)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6746 | } |
| 6747 | } |
| 6748 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6749 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6750 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RoundNumber) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6751 | NoHandleAllocation ha; |
| 6752 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6753 | isolate->counters()->math_round()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6754 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 6755 | if (!args[0]->IsHeapNumber()) { |
| 6756 | // Must be smi. Return the argument unchanged for all the other types |
| 6757 | // to make fuzz-natives test happy. |
| 6758 | return args[0]; |
| 6759 | } |
| 6760 | |
| 6761 | HeapNumber* number = reinterpret_cast<HeapNumber*>(args[0]); |
| 6762 | |
| 6763 | double value = number->value(); |
| 6764 | int exponent = number->get_exponent(); |
| 6765 | int sign = number->get_sign(); |
| 6766 | |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 6767 | if (exponent < -1) { |
| 6768 | // Number in range ]-0.5..0.5[. These always round to +/-zero. |
| 6769 | if (sign) return isolate->heap()->minus_zero_value(); |
| 6770 | return Smi::FromInt(0); |
| 6771 | } |
| 6772 | |
| 6773 | // We compare with kSmiValueSize - 2 because (2^30 - 0.1) has exponent 29 and |
| 6774 | // should be rounded to 2^30, which is not smi (for 31-bit smis, similar |
| 6775 | // agument holds for 32-bit smis). |
| 6776 | if (!sign && exponent < kSmiValueSize - 2) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 6777 | return Smi::FromInt(static_cast<int>(value + 0.5)); |
| 6778 | } |
| 6779 | |
| 6780 | // If the magnitude is big enough, there's no place for fraction part. If we |
| 6781 | // try to add 0.5 to this number, 1.0 will be added instead. |
| 6782 | if (exponent >= 52) { |
| 6783 | return number; |
| 6784 | } |
| 6785 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6786 | if (sign && value >= -0.5) return isolate->heap()->minus_zero_value(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 6787 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 6788 | // Do not call NumberFromDouble() to avoid extra checks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6789 | return isolate->heap()->AllocateHeapNumber(floor(value + 0.5)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6790 | } |
| 6791 | |
| 6792 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6793 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sin) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6794 | NoHandleAllocation ha; |
| 6795 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6796 | isolate->counters()->math_sin()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6797 | |
| 6798 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6799 | return isolate->transcendental_cache()->Get(TranscendentalCache::SIN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
| 6802 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6803 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_sqrt) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6804 | NoHandleAllocation ha; |
| 6805 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6806 | isolate->counters()->math_sqrt()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6807 | |
| 6808 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6809 | return isolate->heap()->AllocateHeapNumber(sqrt(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6813 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Math_tan) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6814 | NoHandleAllocation ha; |
| 6815 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6816 | isolate->counters()->math_tan()->Increment(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6817 | |
| 6818 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 6819 | return isolate->transcendental_cache()->Get(TranscendentalCache::TAN, x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 6820 | } |
| 6821 | |
| 6822 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6823 | static int MakeDay(int year, int month, int day) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6824 | static const int day_from_month[] = {0, 31, 59, 90, 120, 151, |
| 6825 | 181, 212, 243, 273, 304, 334}; |
| 6826 | static const int day_from_month_leap[] = {0, 31, 60, 91, 121, 152, |
| 6827 | 182, 213, 244, 274, 305, 335}; |
| 6828 | |
| 6829 | year += month / 12; |
| 6830 | month %= 12; |
| 6831 | if (month < 0) { |
| 6832 | year--; |
| 6833 | month += 12; |
| 6834 | } |
| 6835 | |
| 6836 | ASSERT(month >= 0); |
| 6837 | ASSERT(month < 12); |
| 6838 | |
| 6839 | // year_delta is an arbitrary number such that: |
| 6840 | // a) year_delta = -1 (mod 400) |
| 6841 | // b) year + year_delta > 0 for years in the range defined by |
| 6842 | // ECMA 262 - 15.9.1.1, i.e. upto 100,000,000 days on either side of |
| 6843 | // Jan 1 1970. This is required so that we don't run into integer |
| 6844 | // division of negative numbers. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6845 | // 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] | 6846 | // operations. |
| 6847 | static const int year_delta = 399999; |
| 6848 | static const int base_day = 365 * (1970 + year_delta) + |
| 6849 | (1970 + year_delta) / 4 - |
| 6850 | (1970 + year_delta) / 100 + |
| 6851 | (1970 + year_delta) / 400; |
| 6852 | |
| 6853 | int year1 = year + year_delta; |
| 6854 | int day_from_year = 365 * year1 + |
| 6855 | year1 / 4 - |
| 6856 | year1 / 100 + |
| 6857 | year1 / 400 - |
| 6858 | base_day; |
| 6859 | |
| 6860 | if (year % 4 || (year % 100 == 0 && year % 400 != 0)) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6861 | return day_from_year + day_from_month[month] + day - 1; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6862 | } |
| 6863 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6864 | return day_from_year + day_from_month_leap[month] + day - 1; |
| 6865 | } |
| 6866 | |
| 6867 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 6868 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateMakeDay) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 6869 | NoHandleAllocation ha; |
| 6870 | ASSERT(args.length() == 3); |
| 6871 | |
| 6872 | CONVERT_SMI_CHECKED(year, args[0]); |
| 6873 | CONVERT_SMI_CHECKED(month, args[1]); |
| 6874 | CONVERT_SMI_CHECKED(date, args[2]); |
| 6875 | |
| 6876 | return Smi::FromInt(MakeDay(year, month, date)); |
| 6877 | } |
| 6878 | |
| 6879 | |
| 6880 | static const int kDays4Years[] = {0, 365, 2 * 365, 3 * 365 + 1}; |
| 6881 | static const int kDaysIn4Years = 4 * 365 + 1; |
| 6882 | static const int kDaysIn100Years = 25 * kDaysIn4Years - 1; |
| 6883 | static const int kDaysIn400Years = 4 * kDaysIn100Years + 1; |
| 6884 | static const int kDays1970to2000 = 30 * 365 + 7; |
| 6885 | static const int kDaysOffset = 1000 * kDaysIn400Years + 5 * kDaysIn400Years - |
| 6886 | kDays1970to2000; |
| 6887 | static const int kYearsOffset = 400000; |
| 6888 | |
| 6889 | static const char kDayInYear[] = { |
| 6890 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6891 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6892 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6893 | 22, 23, 24, 25, 26, 27, 28, |
| 6894 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6895 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6896 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6897 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6898 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6899 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6900 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6901 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6902 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6903 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6904 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6905 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6906 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6907 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6908 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6909 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6910 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6911 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6912 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6913 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6914 | |
| 6915 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6916 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6917 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6918 | 22, 23, 24, 25, 26, 27, 28, |
| 6919 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6920 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6921 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6922 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6923 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6924 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6925 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6926 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6927 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6928 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6929 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6930 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6931 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6932 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6933 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6934 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6935 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6936 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6937 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6938 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6939 | |
| 6940 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6941 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6942 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6943 | 22, 23, 24, 25, 26, 27, 28, 29, |
| 6944 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6945 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6946 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6947 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6948 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6949 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6950 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6951 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6952 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6953 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6954 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6955 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6956 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6957 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6958 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6959 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6960 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6961 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6962 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6963 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6964 | |
| 6965 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6966 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6967 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6968 | 22, 23, 24, 25, 26, 27, 28, |
| 6969 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6970 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6971 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6972 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6973 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6974 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6975 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6976 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6977 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6978 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6979 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6980 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6981 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6982 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6983 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6984 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 6985 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6986 | 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 6987 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 6988 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}; |
| 6989 | |
| 6990 | static const char kMonthInYear[] = { |
| 6991 | 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, |
| 6992 | 0, 0, 0, 0, 0, 0, |
| 6993 | 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, |
| 6994 | 1, 1, 1, |
| 6995 | 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, |
| 6996 | 2, 2, 2, 2, 2, 2, |
| 6997 | 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, |
| 6998 | 3, 3, 3, 3, 3, |
| 6999 | 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, |
| 7000 | 4, 4, 4, 4, 4, 4, |
| 7001 | 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, |
| 7002 | 5, 5, 5, 5, 5, |
| 7003 | 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, |
| 7004 | 6, 6, 6, 6, 6, 6, |
| 7005 | 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, |
| 7006 | 7, 7, 7, 7, 7, 7, |
| 7007 | 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, |
| 7008 | 8, 8, 8, 8, 8, |
| 7009 | 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, |
| 7010 | 9, 9, 9, 9, 9, 9, |
| 7011 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7012 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7013 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7014 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7015 | |
| 7016 | 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, |
| 7017 | 0, 0, 0, 0, 0, 0, |
| 7018 | 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, |
| 7019 | 1, 1, 1, |
| 7020 | 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, |
| 7021 | 2, 2, 2, 2, 2, 2, |
| 7022 | 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, |
| 7023 | 3, 3, 3, 3, 3, |
| 7024 | 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, |
| 7025 | 4, 4, 4, 4, 4, 4, |
| 7026 | 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, |
| 7027 | 5, 5, 5, 5, 5, |
| 7028 | 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, |
| 7029 | 6, 6, 6, 6, 6, 6, |
| 7030 | 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, |
| 7031 | 7, 7, 7, 7, 7, 7, |
| 7032 | 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, |
| 7033 | 8, 8, 8, 8, 8, |
| 7034 | 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, |
| 7035 | 9, 9, 9, 9, 9, 9, |
| 7036 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7037 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7038 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7039 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7040 | |
| 7041 | 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, |
| 7042 | 0, 0, 0, 0, 0, 0, |
| 7043 | 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, |
| 7044 | 1, 1, 1, 1, |
| 7045 | 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, |
| 7046 | 2, 2, 2, 2, 2, 2, |
| 7047 | 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, |
| 7048 | 3, 3, 3, 3, 3, |
| 7049 | 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, |
| 7050 | 4, 4, 4, 4, 4, 4, |
| 7051 | 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, |
| 7052 | 5, 5, 5, 5, 5, |
| 7053 | 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, |
| 7054 | 6, 6, 6, 6, 6, 6, |
| 7055 | 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, |
| 7056 | 7, 7, 7, 7, 7, 7, |
| 7057 | 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, |
| 7058 | 8, 8, 8, 8, 8, |
| 7059 | 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, |
| 7060 | 9, 9, 9, 9, 9, 9, |
| 7061 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7062 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7063 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7064 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7065 | |
| 7066 | 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, |
| 7067 | 0, 0, 0, 0, 0, 0, |
| 7068 | 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, |
| 7069 | 1, 1, 1, |
| 7070 | 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, |
| 7071 | 2, 2, 2, 2, 2, 2, |
| 7072 | 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, |
| 7073 | 3, 3, 3, 3, 3, |
| 7074 | 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, |
| 7075 | 4, 4, 4, 4, 4, 4, |
| 7076 | 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, |
| 7077 | 5, 5, 5, 5, 5, |
| 7078 | 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, |
| 7079 | 6, 6, 6, 6, 6, 6, |
| 7080 | 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, |
| 7081 | 7, 7, 7, 7, 7, 7, |
| 7082 | 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, |
| 7083 | 8, 8, 8, 8, 8, |
| 7084 | 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, |
| 7085 | 9, 9, 9, 9, 9, 9, |
| 7086 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7087 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 7088 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 7089 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}; |
| 7090 | |
| 7091 | |
| 7092 | // This function works for dates from 1970 to 2099. |
| 7093 | static inline void DateYMDFromTimeAfter1970(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7094 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7095 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7096 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7097 | #endif |
| 7098 | |
| 7099 | year = 1970 + (4 * date + 2) / kDaysIn4Years; |
| 7100 | date %= kDaysIn4Years; |
| 7101 | |
| 7102 | month = kMonthInYear[date]; |
| 7103 | day = kDayInYear[date]; |
| 7104 | |
| 7105 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7106 | } |
| 7107 | |
| 7108 | |
| 7109 | static inline void DateYMDFromTimeSlow(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7110 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7111 | #ifdef DEBUG |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 7112 | int save_date = date; // Need this for ASSERT in the end. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7113 | #endif |
| 7114 | |
| 7115 | date += kDaysOffset; |
| 7116 | year = 400 * (date / kDaysIn400Years) - kYearsOffset; |
| 7117 | date %= kDaysIn400Years; |
| 7118 | |
| 7119 | ASSERT(MakeDay(year, 0, 1) + date == save_date); |
| 7120 | |
| 7121 | date--; |
| 7122 | int yd1 = date / kDaysIn100Years; |
| 7123 | date %= kDaysIn100Years; |
| 7124 | year += 100 * yd1; |
| 7125 | |
| 7126 | date++; |
| 7127 | int yd2 = date / kDaysIn4Years; |
| 7128 | date %= kDaysIn4Years; |
| 7129 | year += 4 * yd2; |
| 7130 | |
| 7131 | date--; |
| 7132 | int yd3 = date / 365; |
| 7133 | date %= 365; |
| 7134 | year += yd3; |
| 7135 | |
| 7136 | bool is_leap = (!yd1 || yd2) && !yd3; |
| 7137 | |
| 7138 | ASSERT(date >= -1); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7139 | ASSERT(is_leap || (date >= 0)); |
| 7140 | ASSERT((date < 365) || (is_leap && (date < 366))); |
| 7141 | ASSERT(is_leap == ((year % 4 == 0) && (year % 100 || (year % 400 == 0)))); |
| 7142 | ASSERT(is_leap || ((MakeDay(year, 0, 1) + date) == save_date)); |
| 7143 | ASSERT(!is_leap || ((MakeDay(year, 0, 1) + date + 1) == save_date)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7144 | |
| 7145 | if (is_leap) { |
| 7146 | day = kDayInYear[2*365 + 1 + date]; |
| 7147 | month = kMonthInYear[2*365 + 1 + date]; |
| 7148 | } else { |
| 7149 | day = kDayInYear[date]; |
| 7150 | month = kMonthInYear[date]; |
| 7151 | } |
| 7152 | |
| 7153 | ASSERT(MakeDay(year, month, day) == save_date); |
| 7154 | } |
| 7155 | |
| 7156 | |
| 7157 | static inline void DateYMDFromTime(int date, |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 7158 | int& year, int& month, int& day) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7159 | if (date >= 0 && date < 32 * kDaysIn4Years) { |
| 7160 | DateYMDFromTimeAfter1970(date, year, month, day); |
| 7161 | } else { |
| 7162 | DateYMDFromTimeSlow(date, year, month, day); |
| 7163 | } |
| 7164 | } |
| 7165 | |
| 7166 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7167 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateYMDFromTime) { |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7168 | NoHandleAllocation ha; |
| 7169 | ASSERT(args.length() == 2); |
| 7170 | |
| 7171 | CONVERT_DOUBLE_CHECKED(t, args[0]); |
| 7172 | CONVERT_CHECKED(JSArray, res_array, args[1]); |
| 7173 | |
| 7174 | int year, month, day; |
| 7175 | DateYMDFromTime(static_cast<int>(floor(t / 86400000)), year, month, day); |
| 7176 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7177 | RUNTIME_ASSERT(res_array->elements()->map() == |
| 7178 | isolate->heap()->fixed_array_map()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 7179 | FixedArray* elms = FixedArray::cast(res_array->elements()); |
| 7180 | RUNTIME_ASSERT(elms->length() == 3); |
| 7181 | |
| 7182 | elms->set(0, Smi::FromInt(year)); |
| 7183 | elms->set(1, Smi::FromInt(month)); |
| 7184 | elms->set(2, Smi::FromInt(day)); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 7185 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7186 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7187 | } |
| 7188 | |
| 7189 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7190 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewArgumentsFast) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7191 | NoHandleAllocation ha; |
| 7192 | ASSERT(args.length() == 3); |
| 7193 | |
| 7194 | JSFunction* callee = JSFunction::cast(args[0]); |
| 7195 | Object** parameters = reinterpret_cast<Object**>(args[1]); |
| 7196 | const int length = Smi::cast(args[2])->value(); |
| 7197 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7198 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7199 | { MaybeObject* maybe_result = |
| 7200 | isolate->heap()->AllocateArgumentsObject(callee, length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7201 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7202 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7203 | // Allocate the elements if needed. |
| 7204 | if (length > 0) { |
| 7205 | // Allocate the fixed array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7206 | Object* obj; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7207 | { MaybeObject* maybe_obj = isolate->heap()->AllocateRawFixedArray(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7208 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 7209 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7210 | |
| 7211 | AssertNoAllocation no_gc; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 7212 | FixedArray* array = reinterpret_cast<FixedArray*>(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7213 | array->set_map(isolate->heap()->fixed_array_map()); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7214 | array->set_length(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7215 | |
| 7216 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7217 | for (int i = 0; i < length; i++) { |
| 7218 | array->set(i, *--parameters, mode); |
| 7219 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 7220 | JSObject::cast(result)->set_elements(FixedArray::cast(obj)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 7221 | } |
| 7222 | return result; |
| 7223 | } |
| 7224 | |
| 7225 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7226 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewClosure) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7227 | HandleScope scope(isolate); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7228 | ASSERT(args.length() == 3); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 7229 | CONVERT_ARG_CHECKED(Context, context, 0); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 7230 | CONVERT_ARG_CHECKED(SharedFunctionInfo, shared, 1); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7231 | CONVERT_BOOLEAN_CHECKED(pretenure, args[2]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7232 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 7233 | // Allocate global closures in old space and allocate local closures |
| 7234 | // in new space. Additionally pretenure closures that are assigned |
| 7235 | // directly to properties. |
| 7236 | pretenure = pretenure || (context->global_context() == *context); |
| 7237 | PretenureFlag pretenure_flag = pretenure ? TENURED : NOT_TENURED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7238 | Handle<JSFunction> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7239 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 7240 | context, |
| 7241 | pretenure_flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7242 | return *result; |
| 7243 | } |
| 7244 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7245 | |
| 7246 | static SmartPointer<Object**> GetNonBoundArguments(int bound_argc, |
| 7247 | int* total_argc) { |
| 7248 | // Find frame containing arguments passed to the caller. |
| 7249 | JavaScriptFrameIterator it; |
| 7250 | JavaScriptFrame* frame = it.frame(); |
| 7251 | List<JSFunction*> functions(2); |
| 7252 | frame->GetFunctions(&functions); |
| 7253 | if (functions.length() > 1) { |
| 7254 | int inlined_frame_index = functions.length() - 1; |
| 7255 | JSFunction* inlined_function = functions[inlined_frame_index]; |
| 7256 | int args_count = inlined_function->shared()->formal_parameter_count(); |
| 7257 | ScopedVector<SlotRef> args_slots(args_count); |
| 7258 | SlotRef::ComputeSlotMappingForArguments(frame, |
| 7259 | inlined_frame_index, |
| 7260 | &args_slots); |
| 7261 | |
| 7262 | *total_argc = bound_argc + args_count; |
| 7263 | SmartPointer<Object**> param_data(NewArray<Object**>(*total_argc)); |
| 7264 | for (int i = 0; i < args_count; i++) { |
| 7265 | Handle<Object> val = args_slots[i].GetValue(); |
| 7266 | param_data[bound_argc + i] = val.location(); |
| 7267 | } |
| 7268 | return param_data; |
| 7269 | } else { |
| 7270 | it.AdvanceToArgumentsFrame(); |
| 7271 | frame = it.frame(); |
| 7272 | int args_count = frame->ComputeParametersCount(); |
| 7273 | |
| 7274 | *total_argc = bound_argc + args_count; |
| 7275 | SmartPointer<Object**> param_data(NewArray<Object**>(*total_argc)); |
| 7276 | for (int i = 0; i < args_count; i++) { |
| 7277 | Handle<Object> val = Handle<Object>(frame->GetParameter(i)); |
| 7278 | param_data[bound_argc + i] = val.location(); |
| 7279 | } |
| 7280 | return param_data; |
| 7281 | } |
| 7282 | } |
| 7283 | |
| 7284 | |
| 7285 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObjectFromBound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7286 | HandleScope scope(isolate); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7287 | ASSERT(args.length() == 2); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7288 | // First argument is a function to use as a constructor. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7289 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7290 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7291 | // Second argument is either null or an array of bound arguments. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7292 | Handle<FixedArray> bound_args; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7293 | int bound_argc = 0; |
| 7294 | if (!args[1]->IsNull()) { |
| 7295 | CONVERT_ARG_CHECKED(JSArray, params, 1); |
| 7296 | RUNTIME_ASSERT(params->HasFastElements()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7297 | bound_args = Handle<FixedArray>(FixedArray::cast(params->elements())); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7298 | bound_argc = Smi::cast(params->length())->value(); |
| 7299 | } |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7300 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7301 | int total_argc = 0; |
| 7302 | SmartPointer<Object**> param_data = |
| 7303 | GetNonBoundArguments(bound_argc, &total_argc); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7304 | for (int i = 0; i < bound_argc; i++) { |
| 7305 | Handle<Object> val = Handle<Object>(bound_args->get(i)); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7306 | param_data[i] = val.location(); |
| 7307 | } |
| 7308 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7309 | bool exception = false; |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7310 | Handle<Object> result = |
| 7311 | Execution::New(function, total_argc, *param_data, &exception); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7312 | if (exception) { |
| 7313 | return Failure::Exception(); |
| 7314 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 7315 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 7316 | ASSERT(!result.is_null()); |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame] | 7317 | return *result; |
| 7318 | } |
| 7319 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7320 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7321 | static void TrySettingInlineConstructStub(Isolate* isolate, |
| 7322 | Handle<JSFunction> function) { |
| 7323 | Handle<Object> prototype = isolate->factory()->null_value(); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7324 | if (function->has_instance_prototype()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7325 | prototype = Handle<Object>(function->instance_prototype(), isolate); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 7326 | } |
| 7327 | if (function->shared()->CanGenerateInlineConstructor(*prototype)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7328 | ConstructStubCompiler compiler; |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 7329 | MaybeObject* code = compiler.CompileConstructStub(*function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7330 | if (!code->IsFailure()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7331 | function->shared()->set_construct_stub( |
| 7332 | Code::cast(code->ToObjectUnchecked())); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7333 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7334 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7335 | } |
| 7336 | |
| 7337 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7338 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7339 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7340 | ASSERT(args.length() == 1); |
| 7341 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7342 | Handle<Object> constructor = args.at<Object>(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7343 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7344 | // If the constructor isn't a proper function we throw a type error. |
| 7345 | if (!constructor->IsJSFunction()) { |
| 7346 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7347 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7348 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7349 | return isolate->Throw(*type_error); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7350 | } |
| 7351 | |
| 7352 | Handle<JSFunction> function = Handle<JSFunction>::cast(constructor); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7353 | |
| 7354 | // If function should not have prototype, construction is not allowed. In this |
| 7355 | // case generated code bailouts here, since function has no initial_map. |
| 7356 | if (!function->should_have_prototype()) { |
| 7357 | Vector< Handle<Object> > arguments = HandleVector(&constructor, 1); |
| 7358 | Handle<Object> type_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7359 | isolate->factory()->NewTypeError("not_constructor", arguments); |
| 7360 | return isolate->Throw(*type_error); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 7361 | } |
| 7362 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7363 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7364 | Debug* debug = isolate->debug(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7365 | // Handle stepping into constructors if step into is active. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7366 | if (debug->StepInActive()) { |
| 7367 | debug->HandleStepIn(function, Handle<Object>::null(), 0, true); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7368 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7369 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7370 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7371 | if (function->has_initial_map()) { |
| 7372 | if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7373 | // The 'Function' function ignores the receiver object when |
| 7374 | // called using 'new' and creates a new JSFunction object that |
| 7375 | // is returned. The receiver object is only used for error |
| 7376 | // reporting if an error occurs when constructing the new |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7377 | // JSFunction. FACTORY->NewJSObject() should not be used to |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7378 | // allocate JSFunctions since it does not properly initialize |
| 7379 | // the shared part of the function. Since the receiver is |
| 7380 | // ignored anyway, we use the global object as the receiver |
| 7381 | // instead of a new JSFunction object. This way, errors are |
| 7382 | // reported the same way whether or not 'Function' is called |
| 7383 | // using 'new'. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7384 | return isolate->context()->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7385 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7386 | } |
| 7387 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7388 | // The function should be compiled for the optimization hints to be |
| 7389 | // available. We cannot use EnsureCompiled because that forces a |
| 7390 | // compilation through the shared function info which makes it |
| 7391 | // impossible for us to optimize. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7392 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7393 | if (!function->is_compiled()) CompileLazy(function, CLEAR_EXCEPTION); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7394 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7395 | if (!function->has_initial_map() && |
| 7396 | shared->IsInobjectSlackTrackingInProgress()) { |
| 7397 | // The tracking is already in progress for another function. We can only |
| 7398 | // track one initial_map at a time, so we force the completion before the |
| 7399 | // function is called as a constructor for the first time. |
| 7400 | shared->CompleteInobjectSlackTracking(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | bool first_allocation = !shared->live_objects_may_exist(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7404 | Handle<JSObject> result = isolate->factory()->NewJSObject(function); |
| 7405 | RETURN_IF_EMPTY_HANDLE(isolate, result); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7406 | // Delay setting the stub if inobject slack tracking is in progress. |
| 7407 | if (first_allocation && !shared->IsInobjectSlackTrackingInProgress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7408 | TrySettingInlineConstructStub(isolate, function); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7409 | } |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7410 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7411 | isolate->counters()->constructed_objects()->Increment(); |
| 7412 | isolate->counters()->constructed_objects_runtime()->Increment(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 7413 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 7414 | return *result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7415 | } |
| 7416 | |
| 7417 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7418 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FinalizeInstanceSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7419 | HandleScope scope(isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7420 | ASSERT(args.length() == 1); |
| 7421 | |
| 7422 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7423 | function->shared()->CompleteInobjectSlackTracking(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7424 | TrySettingInlineConstructStub(isolate, function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7425 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7426 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 7427 | } |
| 7428 | |
| 7429 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7430 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyCompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7431 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7432 | ASSERT(args.length() == 1); |
| 7433 | |
| 7434 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 7435 | #ifdef DEBUG |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 7436 | if (FLAG_trace_lazy && !function->shared()->is_compiled()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7437 | PrintF("[lazy: "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7438 | function->PrintName(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7439 | PrintF("]\n"); |
| 7440 | } |
| 7441 | #endif |
| 7442 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 7443 | // Compile the target function. Here we compile using CompileLazyInLoop in |
| 7444 | // order to get the optimized version. This helps code like delta-blue |
| 7445 | // that calls performance-critical routines through constructors. A |
| 7446 | // constructor call doesn't use a CallIC, it uses a LoadIC followed by a |
| 7447 | // direct call. Since the in-loop tracking takes place through CallICs |
| 7448 | // this means that things called through constructors are never known to |
| 7449 | // 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] | 7450 | ASSERT(!function->is_compiled()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 7451 | if (!CompileLazyInLoop(function, KEEP_EXCEPTION)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7452 | return Failure::Exception(); |
| 7453 | } |
| 7454 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7455 | // All done. Return the compiled code. |
| 7456 | ASSERT(function->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7457 | return function->code(); |
| 7458 | } |
| 7459 | |
| 7460 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7461 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LazyRecompile) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7462 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7463 | ASSERT(args.length() == 1); |
| 7464 | Handle<JSFunction> function = args.at<JSFunction>(0); |
| 7465 | // If the function is not optimizable or debugger is active continue using the |
| 7466 | // code from the full compiler. |
| 7467 | if (!function->shared()->code()->optimizable() || |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 7468 | isolate->DebuggerHasBreakPoints()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7469 | if (FLAG_trace_opt) { |
| 7470 | PrintF("[failed to optimize "); |
| 7471 | function->PrintName(); |
| 7472 | PrintF(": is code optimizable: %s, is debugger enabled: %s]\n", |
| 7473 | function->shared()->code()->optimizable() ? "T" : "F", |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 7474 | isolate->DebuggerHasBreakPoints() ? "T" : "F"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7475 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7476 | function->ReplaceCode(function->shared()->code()); |
| 7477 | return function->code(); |
| 7478 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7479 | if (CompileOptimized(function, AstNode::kNoNumber, CLEAR_EXCEPTION)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7480 | return function->code(); |
| 7481 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 7482 | if (FLAG_trace_opt) { |
| 7483 | PrintF("[failed to optimize "); |
| 7484 | function->PrintName(); |
| 7485 | PrintF(": optimized compilation failed]\n"); |
| 7486 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7487 | function->ReplaceCode(function->shared()->code()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7488 | return function->code(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7489 | } |
| 7490 | |
| 7491 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7492 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyDeoptimized) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7493 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7494 | ASSERT(args.length() == 1); |
| 7495 | RUNTIME_ASSERT(args[0]->IsSmi()); |
| 7496 | Deoptimizer::BailoutType type = |
| 7497 | static_cast<Deoptimizer::BailoutType>(Smi::cast(args[0])->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7498 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
| 7499 | ASSERT(isolate->heap()->IsAllocationAllowed()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7500 | int frames = deoptimizer->output_count(); |
| 7501 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 7502 | deoptimizer->MaterializeHeapNumbers(); |
| 7503 | delete deoptimizer; |
| 7504 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7505 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7506 | JavaScriptFrame* frame = NULL; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 7507 | for (int i = 0; i < frames - 1; i++) it.Advance(); |
| 7508 | frame = it.frame(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7509 | |
| 7510 | RUNTIME_ASSERT(frame->function()->IsJSFunction()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7511 | Handle<JSFunction> function(JSFunction::cast(frame->function()), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7512 | Handle<Object> arguments; |
| 7513 | for (int i = frame->ComputeExpressionsCount() - 1; i >= 0; --i) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7514 | if (frame->GetExpression(i) == isolate->heap()->arguments_marker()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7515 | if (arguments.is_null()) { |
| 7516 | // FunctionGetArguments can't throw an exception, so cast away the |
| 7517 | // doubt with an assert. |
| 7518 | arguments = Handle<Object>( |
| 7519 | Accessors::FunctionGetArguments(*function, |
| 7520 | NULL)->ToObjectUnchecked()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7521 | ASSERT(*arguments != isolate->heap()->null_value()); |
| 7522 | ASSERT(*arguments != isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7523 | } |
| 7524 | frame->SetExpression(i, *arguments); |
| 7525 | } |
| 7526 | } |
| 7527 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7528 | isolate->compilation_cache()->MarkForLazyOptimizing(function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7529 | if (type == Deoptimizer::EAGER) { |
| 7530 | RUNTIME_ASSERT(function->IsOptimized()); |
| 7531 | } else { |
| 7532 | RUNTIME_ASSERT(!function->IsOptimized()); |
| 7533 | } |
| 7534 | |
| 7535 | // Avoid doing too much work when running with --always-opt and keep |
| 7536 | // the optimized code around. |
| 7537 | if (FLAG_always_opt || type == Deoptimizer::LAZY) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7538 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7539 | } |
| 7540 | |
| 7541 | // Count the number of optimized activations of the function. |
| 7542 | int activations = 0; |
| 7543 | while (!it.done()) { |
| 7544 | JavaScriptFrame* frame = it.frame(); |
| 7545 | if (frame->is_optimized() && frame->function() == *function) { |
| 7546 | activations++; |
| 7547 | } |
| 7548 | it.Advance(); |
| 7549 | } |
| 7550 | |
| 7551 | // TODO(kasperl): For now, we cannot support removing the optimized |
| 7552 | // code when we have recursive invocations of the same function. |
| 7553 | if (activations == 0) { |
| 7554 | if (FLAG_trace_deopt) { |
| 7555 | PrintF("[removing optimized code for: "); |
| 7556 | function->PrintName(); |
| 7557 | PrintF("]\n"); |
| 7558 | } |
| 7559 | function->ReplaceCode(function->shared()->code()); |
| 7560 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7561 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
| 7564 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7565 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NotifyOSR) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7566 | Deoptimizer* deoptimizer = Deoptimizer::Grab(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7567 | delete deoptimizer; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7568 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7569 | } |
| 7570 | |
| 7571 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7572 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeoptimizeFunction) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7573 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7574 | ASSERT(args.length() == 1); |
| 7575 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7576 | if (!function->IsOptimized()) return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7577 | |
| 7578 | Deoptimizer::DeoptimizeFunction(*function); |
| 7579 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7580 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7581 | } |
| 7582 | |
| 7583 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 7584 | RUNTIME_FUNCTION(MaybeObject*, Runtime_OptimizeFunctionOnNextCall) { |
| 7585 | HandleScope scope(isolate); |
| 7586 | ASSERT(args.length() == 1); |
| 7587 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7588 | if (!function->IsOptimizable()) return isolate->heap()->undefined_value(); |
| 7589 | function->MarkForLazyRecompilation(); |
| 7590 | return isolate->heap()->undefined_value(); |
| 7591 | } |
| 7592 | |
| 7593 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 7594 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationStatus) { |
| 7595 | HandleScope scope(isolate); |
| 7596 | ASSERT(args.length() == 1); |
| 7597 | if (!V8::UseCrankshaft()) { |
| 7598 | return Smi::FromInt(4); // 4 == "never". |
| 7599 | } |
| 7600 | if (FLAG_always_opt) { |
| 7601 | return Smi::FromInt(3); // 3 == "always". |
| 7602 | } |
| 7603 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7604 | return function->IsOptimized() ? Smi::FromInt(1) // 1 == "yes". |
| 7605 | : Smi::FromInt(2); // 2 == "no". |
| 7606 | } |
| 7607 | |
| 7608 | |
| 7609 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetOptimizationCount) { |
| 7610 | HandleScope scope(isolate); |
| 7611 | ASSERT(args.length() == 1); |
| 7612 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7613 | return Smi::FromInt(function->shared()->opt_count()); |
| 7614 | } |
| 7615 | |
| 7616 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7617 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileForOnStackReplacement) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7618 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7619 | ASSERT(args.length() == 1); |
| 7620 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 7621 | |
| 7622 | // We're not prepared to handle a function with arguments object. |
| 7623 | ASSERT(!function->shared()->scope_info()->HasArgumentsShadow()); |
| 7624 | |
| 7625 | // We have hit a back edge in an unoptimized frame for a function that was |
| 7626 | // selected for on-stack replacement. Find the unoptimized code object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7627 | Handle<Code> unoptimized(function->shared()->code(), isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7628 | // Keep track of whether we've succeeded in optimizing. |
| 7629 | bool succeeded = unoptimized->optimizable(); |
| 7630 | if (succeeded) { |
| 7631 | // If we are trying to do OSR when there are already optimized |
| 7632 | // activations of the function, it means (a) the function is directly or |
| 7633 | // indirectly recursive and (b) an optimized invocation has been |
| 7634 | // deoptimized so that we are currently in an unoptimized activation. |
| 7635 | // Check for optimized activations of this function. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7636 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7637 | while (succeeded && !it.done()) { |
| 7638 | JavaScriptFrame* frame = it.frame(); |
| 7639 | succeeded = !frame->is_optimized() || frame->function() != *function; |
| 7640 | it.Advance(); |
| 7641 | } |
| 7642 | } |
| 7643 | |
| 7644 | int ast_id = AstNode::kNoNumber; |
| 7645 | if (succeeded) { |
| 7646 | // The top JS function is this one, the PC is somewhere in the |
| 7647 | // unoptimized code. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7648 | JavaScriptFrameIterator it(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7649 | JavaScriptFrame* frame = it.frame(); |
| 7650 | ASSERT(frame->function() == *function); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 7651 | ASSERT(frame->LookupCode() == *unoptimized); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7652 | ASSERT(unoptimized->contains(frame->pc())); |
| 7653 | |
| 7654 | // Use linear search of the unoptimized code's stack check table to find |
| 7655 | // the AST id matching the PC. |
| 7656 | Address start = unoptimized->instruction_start(); |
| 7657 | unsigned target_pc_offset = static_cast<unsigned>(frame->pc() - start); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 7658 | Address table_cursor = start + unoptimized->stack_check_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7659 | uint32_t table_length = Memory::uint32_at(table_cursor); |
| 7660 | table_cursor += kIntSize; |
| 7661 | for (unsigned i = 0; i < table_length; ++i) { |
| 7662 | // Table entries are (AST id, pc offset) pairs. |
| 7663 | uint32_t pc_offset = Memory::uint32_at(table_cursor + kIntSize); |
| 7664 | if (pc_offset == target_pc_offset) { |
| 7665 | ast_id = static_cast<int>(Memory::uint32_at(table_cursor)); |
| 7666 | break; |
| 7667 | } |
| 7668 | table_cursor += 2 * kIntSize; |
| 7669 | } |
| 7670 | ASSERT(ast_id != AstNode::kNoNumber); |
| 7671 | if (FLAG_trace_osr) { |
| 7672 | PrintF("[replacing on-stack at AST id %d in ", ast_id); |
| 7673 | function->PrintName(); |
| 7674 | PrintF("]\n"); |
| 7675 | } |
| 7676 | |
| 7677 | // Try to compile the optimized code. A true return value from |
| 7678 | // CompileOptimized means that compilation succeeded, not necessarily |
| 7679 | // that optimization succeeded. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7680 | if (CompileOptimized(function, ast_id, CLEAR_EXCEPTION) && |
| 7681 | function->IsOptimized()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7682 | DeoptimizationInputData* data = DeoptimizationInputData::cast( |
| 7683 | function->code()->deoptimization_data()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7684 | if (data->OsrPcOffset()->value() >= 0) { |
| 7685 | if (FLAG_trace_osr) { |
| 7686 | PrintF("[on-stack replacement offset %d in optimized code]\n", |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7687 | data->OsrPcOffset()->value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7688 | } |
| 7689 | ASSERT(data->OsrAstId()->value() == ast_id); |
| 7690 | } else { |
| 7691 | // We may never generate the desired OSR entry if we emit an |
| 7692 | // early deoptimize. |
| 7693 | succeeded = false; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7694 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7695 | } else { |
| 7696 | succeeded = false; |
| 7697 | } |
| 7698 | } |
| 7699 | |
| 7700 | // Revert to the original stack checks in the original unoptimized code. |
| 7701 | if (FLAG_trace_osr) { |
| 7702 | PrintF("[restoring original stack checks in "); |
| 7703 | function->PrintName(); |
| 7704 | PrintF("]\n"); |
| 7705 | } |
| 7706 | StackCheckStub check_stub; |
| 7707 | Handle<Code> check_code = check_stub.GetCode(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7708 | Handle<Code> replacement_code = isolate->builtins()->OnStackReplacement(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7709 | Deoptimizer::RevertStackCheckCode(*unoptimized, |
| 7710 | *check_code, |
| 7711 | *replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7712 | |
| 7713 | // Allow OSR only at nesting level zero again. |
| 7714 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 7715 | |
| 7716 | // If the optimization attempt succeeded, return the AST id tagged as a |
| 7717 | // smi. This tells the builtin that we need to translate the unoptimized |
| 7718 | // frame to an optimized one. |
| 7719 | if (succeeded) { |
| 7720 | ASSERT(function->code()->kind() == Code::OPTIMIZED_FUNCTION); |
| 7721 | return Smi::FromInt(ast_id); |
| 7722 | } else { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 7723 | if (function->IsMarkedForLazyRecompilation()) { |
| 7724 | function->ReplaceCode(function->shared()->code()); |
| 7725 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 7726 | return Smi::FromInt(-1); |
| 7727 | } |
| 7728 | } |
| 7729 | |
| 7730 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7731 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7732 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7733 | ASSERT(args.length() == 1); |
| 7734 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 7735 | return *Execution::GetFunctionDelegate(args.at<Object>(0)); |
| 7736 | } |
| 7737 | |
| 7738 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7739 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetConstructorDelegate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7740 | HandleScope scope(isolate); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 7741 | ASSERT(args.length() == 1); |
| 7742 | RUNTIME_ASSERT(!args[0]->IsJSFunction()); |
| 7743 | return *Execution::GetConstructorDelegate(args.at<Object>(0)); |
| 7744 | } |
| 7745 | |
| 7746 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7747 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewContext) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7748 | NoHandleAllocation ha; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7749 | ASSERT(args.length() == 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7750 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7751 | CONVERT_CHECKED(JSFunction, function, args[0]); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 7752 | int length = function->shared()->scope_info()->NumberOfContextSlots(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7753 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7754 | { MaybeObject* maybe_result = |
| 7755 | isolate->heap()->AllocateFunctionContext(length, function); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7756 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7757 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7758 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7759 | isolate->set_context(Context::cast(result)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7760 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7761 | return result; // non-failure |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7762 | } |
| 7763 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7764 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7765 | MUST_USE_RESULT static MaybeObject* PushContextHelper(Isolate* isolate, |
| 7766 | Object* object, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7767 | bool is_catch_context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7768 | // Convert the object to a proper JavaScript object. |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7769 | Object* js_object = object; |
| 7770 | if (!js_object->IsJSObject()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7771 | MaybeObject* maybe_js_object = js_object->ToObject(); |
| 7772 | if (!maybe_js_object->ToObject(&js_object)) { |
| 7773 | if (!Failure::cast(maybe_js_object)->IsInternalError()) { |
| 7774 | return maybe_js_object; |
| 7775 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7776 | HandleScope scope(isolate); |
| 7777 | Handle<Object> handle(object, isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7778 | Handle<Object> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7779 | isolate->factory()->NewTypeError("with_expression", |
| 7780 | HandleVector(&handle, 1)); |
| 7781 | return isolate->Throw(*result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7782 | } |
| 7783 | } |
| 7784 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7785 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7786 | { MaybeObject* maybe_result = isolate->heap()->AllocateWithContext( |
| 7787 | isolate->context(), JSObject::cast(js_object), is_catch_context); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7788 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 7789 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7790 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7791 | Context* context = Context::cast(result); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7792 | isolate->set_context(context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7793 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 7794 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7795 | } |
| 7796 | |
| 7797 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7798 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushContext) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7799 | NoHandleAllocation ha; |
| 7800 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7801 | return PushContextHelper(isolate, args[0], false); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7802 | } |
| 7803 | |
| 7804 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7805 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushCatchContext) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7806 | NoHandleAllocation ha; |
| 7807 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7808 | return PushContextHelper(isolate, args[0], true); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7809 | } |
| 7810 | |
| 7811 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7812 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7813 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7814 | ASSERT(args.length() == 2); |
| 7815 | |
| 7816 | CONVERT_ARG_CHECKED(Context, context, 0); |
| 7817 | CONVERT_ARG_CHECKED(String, name, 1); |
| 7818 | |
| 7819 | int index; |
| 7820 | PropertyAttributes attributes; |
| 7821 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7822 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7823 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7824 | // If the slot was not found the result is true. |
| 7825 | if (holder.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7826 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7829 | // If the slot was found in a context, it should be DONT_DELETE. |
| 7830 | if (holder->IsContext()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7831 | return isolate->heap()->false_value(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7832 | } |
| 7833 | |
| 7834 | // The slot was found in a JSObject, either a context extension object, |
| 7835 | // the global object, or an arguments object. Try to delete it |
| 7836 | // (respecting DONT_DELETE). For consistency with V8's usual behavior, |
| 7837 | // which allows deleting all parameters in functions that mention |
| 7838 | // 'arguments', we do this even for the case of slots found on an |
| 7839 | // arguments object. The slot was found on an arguments object if the |
| 7840 | // index is non-negative. |
| 7841 | Handle<JSObject> object = Handle<JSObject>::cast(holder); |
| 7842 | if (index >= 0) { |
| 7843 | return object->DeleteElement(index, JSObject::NORMAL_DELETION); |
| 7844 | } else { |
| 7845 | return object->DeleteProperty(*name, JSObject::NORMAL_DELETION); |
| 7846 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7847 | } |
| 7848 | |
| 7849 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7850 | // A mechanism to return a pair of Object pointers in registers (if possible). |
| 7851 | // How this is achieved is calling convention-dependent. |
| 7852 | // All currently supported x86 compiles uses calling conventions that are cdecl |
| 7853 | // variants where a 64-bit value is returned in two 32-bit registers |
| 7854 | // (edx:eax on ia32, r1:r0 on ARM). |
| 7855 | // In AMD-64 calling convention a struct of two pointers is returned in rdx:rax. |
| 7856 | // In Win64 calling convention, a struct of two pointers is returned in memory, |
| 7857 | // 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] | 7858 | #ifdef V8_HOST_ARCH_64_BIT |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7859 | struct ObjectPair { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7860 | MaybeObject* x; |
| 7861 | MaybeObject* y; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7862 | }; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7863 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7864 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7865 | ObjectPair result = {x, y}; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7866 | // Pointers x and y returned in rax and rdx, in AMD-x64-abi. |
| 7867 | // In Win64 they are assigned to a hidden first argument. |
| 7868 | return result; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7869 | } |
| 7870 | #else |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7871 | typedef uint64_t ObjectPair; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7872 | static inline ObjectPair MakePair(MaybeObject* x, MaybeObject* y) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7873 | return reinterpret_cast<uint32_t>(x) | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7874 | (reinterpret_cast<ObjectPair>(y) << 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7875 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 7876 | #endif |
| 7877 | |
| 7878 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7879 | static inline MaybeObject* Unhole(Heap* heap, |
| 7880 | MaybeObject* x, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7881 | PropertyAttributes attributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7882 | ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0); |
| 7883 | USE(attributes); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7884 | return x->IsTheHole() ? heap->undefined_value() : x; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7885 | } |
| 7886 | |
| 7887 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7888 | static JSObject* ComputeReceiverForNonGlobal(Isolate* isolate, |
| 7889 | JSObject* holder) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7890 | ASSERT(!holder->IsGlobalObject()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7891 | Context* top = isolate->context(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7892 | // Get the context extension function. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7893 | JSFunction* context_extension_function = |
| 7894 | top->global_context()->context_extension_function(); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7895 | // If the holder isn't a context extension object, we just return it |
| 7896 | // as the receiver. This allows arguments objects to be used as |
| 7897 | // receivers, but only if they are put in the context scope chain |
| 7898 | // explicitly via a with-statement. |
| 7899 | Object* constructor = holder->map()->constructor(); |
| 7900 | if (constructor != context_extension_function) return holder; |
| 7901 | // Fall back to using the global object as the receiver if the |
| 7902 | // property turns out to be a local variable allocated in a context |
| 7903 | // extension object - introduced via eval. |
| 7904 | return top->global()->global_receiver(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
| 7907 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7908 | static ObjectPair LoadContextSlotHelper(Arguments args, |
| 7909 | Isolate* isolate, |
| 7910 | bool throw_error) { |
| 7911 | HandleScope scope(isolate); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 7912 | ASSERT_EQ(2, args.length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7913 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7914 | if (!args[0]->IsContext() || !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7915 | return MakePair(isolate->ThrowIllegalOperation(), NULL); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7916 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7917 | Handle<Context> context = args.at<Context>(0); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7918 | Handle<String> name = args.at<String>(1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7919 | |
| 7920 | int index; |
| 7921 | PropertyAttributes attributes; |
| 7922 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 7923 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7924 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7925 | // If the index is non-negative, the slot has been found in a local |
| 7926 | // variable or a parameter. Read it from the context object or the |
| 7927 | // arguments object. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7928 | if (index >= 0) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7929 | // If the "property" we were looking for is a local variable or an |
| 7930 | // argument in a context, the receiver is the global object; see |
| 7931 | // ECMA-262, 3rd., 10.1.6 and 10.2.3. |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7932 | // GetElement below can cause GC. |
| 7933 | Handle<JSObject> receiver( |
| 7934 | isolate->context()->global()->global_receiver()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7935 | MaybeObject* value = (holder->IsContext()) |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7936 | ? Context::cast(*holder)->get(index) |
| 7937 | : JSObject::cast(*holder)->GetElement(index); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7938 | return MakePair(Unhole(isolate->heap(), value, attributes), *receiver); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7939 | } |
| 7940 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7941 | // If the holder is found, we read the property from it. |
| 7942 | if (!holder.is_null() && holder->IsJSObject()) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 7943 | ASSERT(Handle<JSObject>::cast(holder)->HasProperty(*name)); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7944 | JSObject* object = JSObject::cast(*holder); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7945 | JSObject* receiver; |
| 7946 | if (object->IsGlobalObject()) { |
| 7947 | receiver = GlobalObject::cast(object)->global_receiver(); |
| 7948 | } else if (context->is_exception_holder(*holder)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7949 | receiver = isolate->context()->global()->global_receiver(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7950 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7951 | receiver = ComputeReceiverForNonGlobal(isolate, object); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 7952 | } |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7953 | |
| 7954 | // GetProperty below can cause GC. |
| 7955 | Handle<JSObject> receiver_handle(receiver); |
| 7956 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 7957 | // No need to unhole the value here. This is taken care of by the |
| 7958 | // GetProperty function. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 7959 | MaybeObject* value = object->GetProperty(*name); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 7960 | return MakePair(value, *receiver_handle); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7961 | } |
| 7962 | |
| 7963 | if (throw_error) { |
| 7964 | // The property doesn't exist - throw exception. |
| 7965 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7966 | isolate->factory()->NewReferenceError("not_defined", |
| 7967 | HandleVector(&name, 1)); |
| 7968 | return MakePair(isolate->Throw(*reference_error), NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7969 | } else { |
| 7970 | // The property doesn't exist - return undefined |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7971 | return MakePair(isolate->heap()->undefined_value(), |
| 7972 | isolate->heap()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7973 | } |
| 7974 | } |
| 7975 | |
| 7976 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7977 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7978 | return LoadContextSlotHelper(args, isolate, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7979 | } |
| 7980 | |
| 7981 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7982 | RUNTIME_FUNCTION(ObjectPair, Runtime_LoadContextSlotNoReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7983 | return LoadContextSlotHelper(args, isolate, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 7987 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StoreContextSlot) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7988 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 7989 | ASSERT(args.length() == 4); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7990 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 7991 | Handle<Object> value(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7992 | CONVERT_ARG_CHECKED(Context, context, 1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7993 | CONVERT_ARG_CHECKED(String, name, 2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 7994 | CONVERT_SMI_CHECKED(strict_unchecked, args[3]); |
| 7995 | RUNTIME_ASSERT(strict_unchecked == kStrictMode || |
| 7996 | strict_unchecked == kNonStrictMode); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 7997 | StrictModeFlag strict_mode = static_cast<StrictModeFlag>(strict_unchecked); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 7998 | |
| 7999 | int index; |
| 8000 | PropertyAttributes attributes; |
| 8001 | ContextLookupFlags flags = FOLLOW_CHAINS; |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 8002 | Handle<Object> holder = context->Lookup(name, flags, &index, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8003 | |
| 8004 | if (index >= 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8005 | if (holder->IsContext()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8006 | // Ignore if read_only variable. |
| 8007 | if ((attributes & READ_ONLY) == 0) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8008 | // Context is a fixed array and set cannot fail. |
| 8009 | Context::cast(*holder)->set(index, *value); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8010 | } else if (strict_mode == kStrictMode) { |
| 8011 | // Setting read only property in strict mode. |
| 8012 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8013 | isolate->factory()->NewTypeError("strict_cannot_assign", |
| 8014 | HandleVector(&name, 1)); |
| 8015 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8016 | } |
| 8017 | } else { |
| 8018 | ASSERT((attributes & READ_ONLY) == 0); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8019 | Handle<Object> result = |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8020 | SetElement(Handle<JSObject>::cast(holder), index, value, strict_mode); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8021 | if (result.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8022 | ASSERT(isolate->has_pending_exception()); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 8023 | return Failure::Exception(); |
| 8024 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8025 | } |
| 8026 | return *value; |
| 8027 | } |
| 8028 | |
| 8029 | // Slow case: The property is not in a FixedArray context. |
| 8030 | // It is either in an JSObject extension context or it was not found. |
| 8031 | Handle<JSObject> context_ext; |
| 8032 | |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8033 | if (!holder.is_null()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8034 | // The property exists in the extension context. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8035 | context_ext = Handle<JSObject>::cast(holder); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8036 | } else { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8037 | // The property was not found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8038 | ASSERT(attributes == ABSENT); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 8039 | |
| 8040 | if (strict_mode == kStrictMode) { |
| 8041 | // Throw in strict mode (assignment to undefined variable). |
| 8042 | Handle<Object> error = |
| 8043 | isolate->factory()->NewReferenceError( |
| 8044 | "not_defined", HandleVector(&name, 1)); |
| 8045 | return isolate->Throw(*error); |
| 8046 | } |
| 8047 | // 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] | 8048 | attributes = NONE; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8049 | context_ext = Handle<JSObject>(isolate->context()->global()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8052 | // Set the property, but ignore if read_only variable on the context |
| 8053 | // extension object itself. |
| 8054 | if ((attributes & READ_ONLY) == 0 || |
| 8055 | (context_ext->GetLocalPropertyAttribute(*name) == ABSENT)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8056 | RETURN_IF_EMPTY_HANDLE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8057 | isolate, |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8058 | SetProperty(context_ext, name, value, NONE, strict_mode)); |
| 8059 | } else if (strict_mode == kStrictMode && (attributes & READ_ONLY) != 0) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8060 | // Setting read only property in strict mode. |
| 8061 | Handle<Object> error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8062 | isolate->factory()->NewTypeError( |
| 8063 | "strict_cannot_assign", HandleVector(&name, 1)); |
| 8064 | return isolate->Throw(*error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8065 | } |
| 8066 | return *value; |
| 8067 | } |
| 8068 | |
| 8069 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8070 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Throw) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8071 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8072 | ASSERT(args.length() == 1); |
| 8073 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8074 | return isolate->Throw(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8075 | } |
| 8076 | |
| 8077 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8078 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ReThrow) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8079 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8080 | ASSERT(args.length() == 1); |
| 8081 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8082 | return isolate->ReThrow(args[0]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8083 | } |
| 8084 | |
| 8085 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8086 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PromoteScheduledException) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8087 | ASSERT_EQ(0, args.length()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8088 | return isolate->PromoteScheduledException(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 8089 | } |
| 8090 | |
| 8091 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8092 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowReferenceError) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8093 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8094 | ASSERT(args.length() == 1); |
| 8095 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8096 | Handle<Object> name(args[0], isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8097 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8098 | isolate->factory()->NewReferenceError("not_defined", |
| 8099 | HandleVector(&name, 1)); |
| 8100 | return isolate->Throw(*reference_error); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8104 | RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 8105 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8106 | |
| 8107 | // First check if this is a real stack overflow. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8108 | if (isolate->stack_guard()->IsStackOverflow()) { |
| 8109 | NoHandleAllocation na; |
| 8110 | return isolate->StackOverflow(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 8111 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8112 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 8113 | return Execution::HandleStackGuardInterrupt(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8114 | } |
| 8115 | |
| 8116 | |
| 8117 | // NOTE: These PrintXXX functions are defined for all builds (not just |
| 8118 | // DEBUG builds) because we may want to be able to trace function |
| 8119 | // calls in all modes. |
| 8120 | static void PrintString(String* str) { |
| 8121 | // not uncommon to have empty strings |
| 8122 | if (str->length() > 0) { |
| 8123 | SmartPointer<char> s = |
| 8124 | str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 8125 | PrintF("%s", *s); |
| 8126 | } |
| 8127 | } |
| 8128 | |
| 8129 | |
| 8130 | static void PrintObject(Object* obj) { |
| 8131 | if (obj->IsSmi()) { |
| 8132 | PrintF("%d", Smi::cast(obj)->value()); |
| 8133 | } else if (obj->IsString() || obj->IsSymbol()) { |
| 8134 | PrintString(String::cast(obj)); |
| 8135 | } else if (obj->IsNumber()) { |
| 8136 | PrintF("%g", obj->Number()); |
| 8137 | } else if (obj->IsFailure()) { |
| 8138 | PrintF("<failure>"); |
| 8139 | } else if (obj->IsUndefined()) { |
| 8140 | PrintF("<undefined>"); |
| 8141 | } else if (obj->IsNull()) { |
| 8142 | PrintF("<null>"); |
| 8143 | } else if (obj->IsTrue()) { |
| 8144 | PrintF("<true>"); |
| 8145 | } else if (obj->IsFalse()) { |
| 8146 | PrintF("<false>"); |
| 8147 | } else { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 8148 | PrintF("%p", reinterpret_cast<void*>(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8149 | } |
| 8150 | } |
| 8151 | |
| 8152 | |
| 8153 | static int StackSize() { |
| 8154 | int n = 0; |
| 8155 | for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++; |
| 8156 | return n; |
| 8157 | } |
| 8158 | |
| 8159 | |
| 8160 | static void PrintTransition(Object* result) { |
| 8161 | // indentation |
| 8162 | { const int nmax = 80; |
| 8163 | int n = StackSize(); |
| 8164 | if (n <= nmax) |
| 8165 | PrintF("%4d:%*s", n, n, ""); |
| 8166 | else |
| 8167 | PrintF("%4d:%*s", n, nmax, "..."); |
| 8168 | } |
| 8169 | |
| 8170 | if (result == NULL) { |
| 8171 | // constructor calls |
| 8172 | JavaScriptFrameIterator it; |
| 8173 | JavaScriptFrame* frame = it.frame(); |
| 8174 | if (frame->IsConstructor()) PrintF("new "); |
| 8175 | // function name |
| 8176 | Object* fun = frame->function(); |
| 8177 | if (fun->IsJSFunction()) { |
| 8178 | PrintObject(JSFunction::cast(fun)->shared()->name()); |
| 8179 | } else { |
| 8180 | PrintObject(fun); |
| 8181 | } |
| 8182 | // function arguments |
| 8183 | // (we are intentionally only printing the actually |
| 8184 | // supplied parameters, not all parameters required) |
| 8185 | PrintF("(this="); |
| 8186 | PrintObject(frame->receiver()); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8187 | const int length = frame->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8188 | for (int i = 0; i < length; i++) { |
| 8189 | PrintF(", "); |
| 8190 | PrintObject(frame->GetParameter(i)); |
| 8191 | } |
| 8192 | PrintF(") {\n"); |
| 8193 | |
| 8194 | } else { |
| 8195 | // function result |
| 8196 | PrintF("} -> "); |
| 8197 | PrintObject(result); |
| 8198 | PrintF("\n"); |
| 8199 | } |
| 8200 | } |
| 8201 | |
| 8202 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8203 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceEnter) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8204 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8205 | NoHandleAllocation ha; |
| 8206 | PrintTransition(NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8207 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8208 | } |
| 8209 | |
| 8210 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8211 | RUNTIME_FUNCTION(MaybeObject*, Runtime_TraceExit) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8212 | NoHandleAllocation ha; |
| 8213 | PrintTransition(args[0]); |
| 8214 | return args[0]; // return TOS |
| 8215 | } |
| 8216 | |
| 8217 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8218 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8219 | NoHandleAllocation ha; |
| 8220 | ASSERT(args.length() == 1); |
| 8221 | |
| 8222 | #ifdef DEBUG |
| 8223 | if (args[0]->IsString()) { |
| 8224 | // If we have a string, assume it's a code "marker" |
| 8225 | // and print some interesting cpu debugging info. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 8226 | JavaScriptFrameIterator it(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8227 | JavaScriptFrame* frame = it.frame(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 8228 | PrintF("fp = %p, sp = %p, caller_sp = %p: ", |
| 8229 | frame->fp(), frame->sp(), frame->caller_sp()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8230 | } else { |
| 8231 | PrintF("DebugPrint: "); |
| 8232 | } |
| 8233 | args[0]->Print(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8234 | if (args[0]->IsHeapObject()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 8235 | PrintF("\n"); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 8236 | HeapObject::cast(args[0])->map()->Print(); |
| 8237 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8238 | #else |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8239 | // ShortPrint is available in release mode. Print is not. |
| 8240 | args[0]->ShortPrint(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8241 | #endif |
| 8242 | PrintF("\n"); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8243 | Flush(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8244 | |
| 8245 | return args[0]; // return TOS |
| 8246 | } |
| 8247 | |
| 8248 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8249 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugTrace) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 8250 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8251 | NoHandleAllocation ha; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8252 | isolate->PrintStack(); |
| 8253 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8254 | } |
| 8255 | |
| 8256 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8257 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateCurrentTime) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8258 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 8259 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8260 | |
| 8261 | // According to ECMA-262, section 15.9.1, page 117, the precision of |
| 8262 | // the number in a Date object representing a particular instant in |
| 8263 | // time is milliseconds. Therefore, we floor the result of getting |
| 8264 | // the OS time. |
| 8265 | double millis = floor(OS::TimeCurrentMillis()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8266 | return isolate->heap()->NumberFromDouble(millis); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8267 | } |
| 8268 | |
| 8269 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8270 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateParseString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8271 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8272 | ASSERT(args.length() == 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8273 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8274 | CONVERT_ARG_CHECKED(String, str, 0); |
| 8275 | FlattenString(str); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8276 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8277 | CONVERT_ARG_CHECKED(JSArray, output, 1); |
| 8278 | RUNTIME_ASSERT(output->HasFastElements()); |
| 8279 | |
| 8280 | AssertNoAllocation no_allocation; |
| 8281 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8282 | FixedArray* output_array = FixedArray::cast(output->elements()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8283 | RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 8284 | bool result; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 8285 | if (str->IsAsciiRepresentation()) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8286 | result = DateParser::Parse(str->ToAsciiVector(), |
| 8287 | output_array, |
| 8288 | isolate->unicode_cache()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8289 | } else { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 8290 | ASSERT(str->IsTwoByteRepresentation()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 8291 | result = DateParser::Parse(str->ToUC16Vector(), |
| 8292 | output_array, |
| 8293 | isolate->unicode_cache()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 8294 | } |
| 8295 | |
| 8296 | if (result) { |
| 8297 | return *output; |
| 8298 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8299 | return isolate->heap()->null_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8300 | } |
| 8301 | } |
| 8302 | |
| 8303 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8304 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimezone) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8305 | NoHandleAllocation ha; |
| 8306 | ASSERT(args.length() == 1); |
| 8307 | |
| 8308 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 8309 | const char* zone = OS::LocalTimezone(x); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8310 | return isolate->heap()->AllocateStringFromUtf8(CStrVector(zone)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8311 | } |
| 8312 | |
| 8313 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8314 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateLocalTimeOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8315 | NoHandleAllocation ha; |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 8316 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8317 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8318 | return isolate->heap()->NumberFromDouble(OS::LocalTimeOffset()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8319 | } |
| 8320 | |
| 8321 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8322 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DateDaylightSavingsOffset) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8323 | NoHandleAllocation ha; |
| 8324 | ASSERT(args.length() == 1); |
| 8325 | |
| 8326 | CONVERT_DOUBLE_CHECKED(x, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8327 | return isolate->heap()->NumberFromDouble(OS::DaylightSavingsOffset(x)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8328 | } |
| 8329 | |
| 8330 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8331 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalReceiver) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8332 | ASSERT(args.length() == 1); |
| 8333 | Object* global = args[0]; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8334 | if (!global->IsJSGlobalObject()) return isolate->heap()->null_value(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8335 | return JSGlobalObject::cast(global)->global_receiver(); |
| 8336 | } |
| 8337 | |
| 8338 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8339 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ParseJson) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8340 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8341 | ASSERT_EQ(1, args.length()); |
| 8342 | CONVERT_ARG_CHECKED(String, source, 0); |
| 8343 | |
| 8344 | Handle<Object> result = JsonParser::Parse(source); |
| 8345 | if (result.is_null()) { |
| 8346 | // Syntax error or stack overflow in scanner. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8347 | ASSERT(isolate->has_pending_exception()); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8348 | return Failure::Exception(); |
| 8349 | } |
| 8350 | return *result; |
| 8351 | } |
| 8352 | |
| 8353 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8354 | bool CodeGenerationFromStringsAllowed(Isolate* isolate, |
| 8355 | Handle<Context> context) { |
| 8356 | if (context->allow_code_gen_from_strings()->IsFalse()) { |
| 8357 | // Check with callback if set. |
| 8358 | AllowCodeGenerationFromStringsCallback callback = |
| 8359 | isolate->allow_code_gen_callback(); |
| 8360 | if (callback == NULL) { |
| 8361 | // No callback set and code generation disallowed. |
| 8362 | return false; |
| 8363 | } else { |
| 8364 | // Callback set. Let it decide if code generation is allowed. |
| 8365 | VMState state(isolate, EXTERNAL); |
| 8366 | return callback(v8::Utils::ToLocal(context)); |
| 8367 | } |
| 8368 | } |
| 8369 | return true; |
| 8370 | } |
| 8371 | |
| 8372 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8373 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CompileString) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8374 | HandleScope scope(isolate); |
fschneider@chromium.org | e03fb64 | 2010-11-01 12:34:09 +0000 | [diff] [blame] | 8375 | ASSERT_EQ(1, args.length()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 8376 | CONVERT_ARG_CHECKED(String, source, 0); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8377 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8378 | // Extract global context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8379 | Handle<Context> context(isolate->context()->global_context()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8380 | |
| 8381 | // Check if global context allows code generation from |
| 8382 | // strings. Throw an exception if it doesn't. |
| 8383 | if (!CodeGenerationFromStringsAllowed(isolate, context)) { |
| 8384 | return isolate->Throw(*isolate->factory()->NewError( |
| 8385 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 8386 | } |
| 8387 | |
| 8388 | // Compile source string in the global context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8389 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval(source, |
| 8390 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8391 | true, |
| 8392 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 8393 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8394 | Handle<JSFunction> fun = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8395 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 8396 | context, |
| 8397 | NOT_TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8398 | return *fun; |
| 8399 | } |
| 8400 | |
| 8401 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8402 | static ObjectPair CompileGlobalEval(Isolate* isolate, |
| 8403 | Handle<String> source, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8404 | Handle<Object> receiver, |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 8405 | StrictModeFlag strict_mode) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8406 | Handle<Context> context = Handle<Context>(isolate->context()); |
| 8407 | Handle<Context> global_context = Handle<Context>(context->global_context()); |
| 8408 | |
| 8409 | // Check if global context allows code generation from |
| 8410 | // strings. Throw an exception if it doesn't. |
| 8411 | if (!CodeGenerationFromStringsAllowed(isolate, global_context)) { |
| 8412 | isolate->Throw(*isolate->factory()->NewError( |
| 8413 | "code_gen_from_strings", HandleVector<Object>(NULL, 0))); |
| 8414 | return MakePair(Failure::Exception(), NULL); |
| 8415 | } |
| 8416 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8417 | // Deal with a normal eval call with a string argument. Compile it |
| 8418 | // and return the compiled function bound in the local context. |
| 8419 | Handle<SharedFunctionInfo> shared = Compiler::CompileEval( |
| 8420 | source, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8421 | Handle<Context>(isolate->context()), |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8422 | context->IsGlobalContext(), |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 8423 | strict_mode); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8424 | if (shared.is_null()) return MakePair(Failure::Exception(), NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8425 | Handle<JSFunction> compiled = |
| 8426 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 8427 | shared, context, NOT_TENURED); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8428 | return MakePair(*compiled, *receiver); |
| 8429 | } |
| 8430 | |
| 8431 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8432 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEval) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8433 | ASSERT(args.length() == 4); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8434 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8435 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8436 | Handle<Object> callee = args.at<Object>(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8437 | Handle<Object> receiver; // Will be overwritten. |
| 8438 | |
| 8439 | // Compute the calling context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8440 | Handle<Context> context = Handle<Context>(isolate->context(), isolate); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8441 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8442 | // Make sure Isolate::context() agrees with the old code that traversed |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8443 | // the stack frames to compute the context. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8444 | StackFrameLocator locator; |
| 8445 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8446 | ASSERT(Context::cast(frame->context()) == *context); |
| 8447 | #endif |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8448 | |
| 8449 | // Find where the 'eval' symbol is bound. It is unaliased only if |
| 8450 | // it is bound in the global context. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8451 | int index = -1; |
| 8452 | PropertyAttributes attributes = ABSENT; |
| 8453 | while (true) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8454 | receiver = context->Lookup(isolate->factory()->eval_symbol(), |
| 8455 | FOLLOW_PROTOTYPE_CHAIN, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8456 | &index, &attributes); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8457 | // Stop search when eval is found or when the global context is |
| 8458 | // reached. |
| 8459 | if (attributes != ABSENT || context->IsGlobalContext()) break; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8460 | if (context->is_function_context()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8461 | context = Handle<Context>(Context::cast(context->closure()->context()), |
| 8462 | isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8463 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8464 | context = Handle<Context>(context->previous(), isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8465 | } |
| 8466 | } |
| 8467 | |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8468 | // If eval could not be resolved, it has been deleted and we need to |
| 8469 | // throw a reference error. |
| 8470 | if (attributes == ABSENT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8471 | Handle<Object> name = isolate->factory()->eval_symbol(); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8472 | Handle<Object> reference_error = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8473 | isolate->factory()->NewReferenceError("not_defined", |
| 8474 | HandleVector(&name, 1)); |
| 8475 | return MakePair(isolate->Throw(*reference_error), NULL); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8478 | if (!context->IsGlobalContext()) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8479 | // 'eval' is not bound in the global context. Just call the function |
| 8480 | // with the given arguments. This is not necessarily the global eval. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 8481 | if (receiver->IsContext() || receiver->IsJSContextExtensionObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8482 | receiver = Handle<JSObject>( |
| 8483 | isolate->context()->global()->global_receiver(), isolate); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8484 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8485 | return MakePair(*callee, *receiver); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8486 | } |
| 8487 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8488 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 8489 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8490 | if (*callee != isolate->global_context()->global_eval_fun() || |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8491 | !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8492 | return MakePair(*callee, |
| 8493 | isolate->context()->global()->global_receiver()); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8494 | } |
| 8495 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8496 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8497 | return CompileGlobalEval(isolate, |
| 8498 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8499 | args.at<Object>(2), |
| 8500 | static_cast<StrictModeFlag>( |
| 8501 | Smi::cast(args[3])->value())); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
| 8504 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8505 | RUNTIME_FUNCTION(ObjectPair, Runtime_ResolvePossiblyDirectEvalNoLookup) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8506 | ASSERT(args.length() == 4); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8507 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8508 | HandleScope scope(isolate); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8509 | Handle<Object> callee = args.at<Object>(0); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8510 | |
| 8511 | // 'eval' is bound in the global context, but it may have been overwritten. |
| 8512 | // Compare it to the builtin 'GlobalEval' function to make sure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8513 | if (*callee != isolate->global_context()->global_eval_fun() || |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8514 | !args[1]->IsString()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8515 | return MakePair(*callee, |
| 8516 | isolate->context()->global()->global_receiver()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 8517 | } |
| 8518 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8519 | ASSERT(args[3]->IsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8520 | return CompileGlobalEval(isolate, |
| 8521 | args.at<String>(1), |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8522 | args.at<Object>(2), |
| 8523 | static_cast<StrictModeFlag>( |
| 8524 | Smi::cast(args[3])->value())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 8525 | } |
| 8526 | |
| 8527 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8528 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetNewFunctionAttributes) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8529 | // This utility adjusts the property attributes for newly created Function |
| 8530 | // object ("new Function(...)") by changing the map. |
| 8531 | // All it does is changing the prototype property to enumerable |
| 8532 | // as specified in ECMA262, 15.3.5.2. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8533 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8534 | ASSERT(args.length() == 1); |
| 8535 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8536 | |
| 8537 | Handle<Map> map = func->shared()->strict_mode() |
| 8538 | ? isolate->strict_mode_function_instance_map() |
| 8539 | : isolate->function_instance_map(); |
| 8540 | |
| 8541 | ASSERT(func->map()->instance_type() == map->instance_type()); |
| 8542 | ASSERT(func->map()->instance_size() == map->instance_size()); |
| 8543 | func->set_map(*map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 8544 | return *func; |
| 8545 | } |
| 8546 | |
| 8547 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8548 | RUNTIME_FUNCTION(MaybeObject*, Runtime_AllocateInNewSpace) { |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8549 | // Allocate a block of memory in NewSpace (filled with a filler). |
| 8550 | // Use as fallback for allocation in generated code when NewSpace |
| 8551 | // is full. |
| 8552 | ASSERT(args.length() == 1); |
| 8553 | CONVERT_ARG_CHECKED(Smi, size_smi, 0); |
| 8554 | int size = size_smi->value(); |
| 8555 | RUNTIME_ASSERT(IsAligned(size, kPointerSize)); |
| 8556 | RUNTIME_ASSERT(size > 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8557 | Heap* heap = isolate->heap(); |
| 8558 | const int kMinFreeNewSpaceAfterGC = heap->InitialSemiSpaceSize() * 3/4; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8559 | RUNTIME_ASSERT(size <= kMinFreeNewSpaceAfterGC); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8560 | Object* allocation; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8561 | { MaybeObject* maybe_allocation = heap->new_space()->AllocateRaw(size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8562 | if (maybe_allocation->ToObject(&allocation)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8563 | heap->CreateFillerObjectAt(HeapObject::cast(allocation)->address(), size); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8564 | } |
| 8565 | return maybe_allocation; |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8566 | } |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 8567 | } |
| 8568 | |
| 8569 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8570 | // 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] | 8571 | // array. Returns true if the element was pushed on the stack and |
| 8572 | // false otherwise. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 8573 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PushIfAbsent) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8574 | ASSERT(args.length() == 2); |
| 8575 | CONVERT_CHECKED(JSArray, array, args[0]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 8576 | CONVERT_CHECKED(JSObject, element, args[1]); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8577 | RUNTIME_ASSERT(array->HasFastElements()); |
| 8578 | int length = Smi::cast(array->length())->value(); |
| 8579 | FixedArray* elements = FixedArray::cast(array->elements()); |
| 8580 | for (int i = 0; i < length; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8581 | if (elements->get(i) == element) return isolate->heap()->false_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8582 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8583 | Object* obj; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8584 | // Strict not needed. Used for cycle detection in Array join implementation. |
| 8585 | { MaybeObject* maybe_obj = array->SetFastElement(length, element, |
| 8586 | kNonStrictMode); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 8587 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 8588 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8589 | return isolate->heap()->true_value(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 8590 | } |
| 8591 | |
| 8592 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8593 | /** |
| 8594 | * A simple visitor visits every element of Array's. |
| 8595 | * The backend storage can be a fixed array for fast elements case, |
| 8596 | * or a dictionary for sparse array. Since Dictionary is a subtype |
| 8597 | * of FixedArray, the class can be used by both fast and slow cases. |
| 8598 | * The second parameter of the constructor, fast_elements, specifies |
| 8599 | * whether the storage is a FixedArray or Dictionary. |
| 8600 | * |
| 8601 | * An index limit is used to deal with the situation that a result array |
| 8602 | * length overflows 32-bit non-negative integer. |
| 8603 | */ |
| 8604 | class ArrayConcatVisitor { |
| 8605 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8606 | ArrayConcatVisitor(Isolate* isolate, |
| 8607 | Handle<FixedArray> storage, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8608 | bool fast_elements) : |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8609 | isolate_(isolate), |
| 8610 | storage_(Handle<FixedArray>::cast( |
| 8611 | isolate->global_handles()->Create(*storage))), |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8612 | index_offset_(0u), |
| 8613 | fast_elements_(fast_elements) { } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8614 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8615 | ~ArrayConcatVisitor() { |
| 8616 | clear_storage(); |
| 8617 | } |
| 8618 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8619 | void visit(uint32_t i, Handle<Object> elm) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8620 | if (i >= JSObject::kMaxElementCount - index_offset_) return; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8621 | uint32_t index = index_offset_ + i; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8622 | |
| 8623 | if (fast_elements_) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8624 | if (index < static_cast<uint32_t>(storage_->length())) { |
| 8625 | storage_->set(index, *elm); |
| 8626 | return; |
| 8627 | } |
| 8628 | // Our initial estimate of length was foiled, possibly by |
| 8629 | // getters on the arrays increasing the length of later arrays |
| 8630 | // during iteration. |
| 8631 | // This shouldn't happen in anything but pathological cases. |
| 8632 | SetDictionaryMode(index); |
| 8633 | // Fall-through to dictionary mode. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8634 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8635 | ASSERT(!fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8636 | Handle<NumberDictionary> dict(NumberDictionary::cast(*storage_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8637 | Handle<NumberDictionary> result = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8638 | isolate_->factory()->DictionaryAtNumberPut(dict, index, elm); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8639 | if (!result.is_identical_to(dict)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8640 | // Dictionary needed to grow. |
| 8641 | clear_storage(); |
| 8642 | set_storage(*result); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8643 | } |
| 8644 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8645 | |
| 8646 | void increase_index_offset(uint32_t delta) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8647 | if (JSObject::kMaxElementCount - index_offset_ < delta) { |
| 8648 | index_offset_ = JSObject::kMaxElementCount; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 8649 | } else { |
| 8650 | index_offset_ += delta; |
| 8651 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8654 | Handle<JSArray> ToArray() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8655 | Handle<JSArray> array = isolate_->factory()->NewJSArray(0); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8656 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8657 | isolate_->factory()->NewNumber(static_cast<double>(index_offset_)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8658 | Handle<Map> map; |
| 8659 | if (fast_elements_) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8660 | map = isolate_->factory()->GetFastElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8661 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8662 | map = isolate_->factory()->GetSlowElementsMap(Handle<Map>(array->map())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8663 | } |
| 8664 | array->set_map(*map); |
| 8665 | array->set_length(*length); |
| 8666 | array->set_elements(*storage_); |
| 8667 | return array; |
| 8668 | } |
kasperl@chromium.org | edf0cd1 | 2010-01-05 13:29:12 +0000 | [diff] [blame] | 8669 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8670 | private: |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8671 | // Convert storage to dictionary mode. |
| 8672 | void SetDictionaryMode(uint32_t index) { |
| 8673 | ASSERT(fast_elements_); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8674 | Handle<FixedArray> current_storage(*storage_); |
| 8675 | Handle<NumberDictionary> slow_storage( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8676 | isolate_->factory()->NewNumberDictionary(current_storage->length())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8677 | uint32_t current_length = static_cast<uint32_t>(current_storage->length()); |
| 8678 | for (uint32_t i = 0; i < current_length; i++) { |
| 8679 | HandleScope loop_scope; |
| 8680 | Handle<Object> element(current_storage->get(i)); |
| 8681 | if (!element->IsTheHole()) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8682 | Handle<NumberDictionary> new_storage = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8683 | isolate_->factory()->DictionaryAtNumberPut(slow_storage, i, element); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8684 | if (!new_storage.is_identical_to(slow_storage)) { |
| 8685 | slow_storage = loop_scope.CloseAndEscape(new_storage); |
| 8686 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8687 | } |
| 8688 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8689 | clear_storage(); |
| 8690 | set_storage(*slow_storage); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8691 | fast_elements_ = false; |
| 8692 | } |
| 8693 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8694 | inline void clear_storage() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8695 | isolate_->global_handles()->Destroy( |
| 8696 | Handle<Object>::cast(storage_).location()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
| 8699 | inline void set_storage(FixedArray* storage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8700 | storage_ = Handle<FixedArray>::cast( |
| 8701 | isolate_->global_handles()->Create(storage)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8702 | } |
| 8703 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8704 | Isolate* isolate_; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 8705 | Handle<FixedArray> storage_; // Always a global handle. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8706 | // Index after last seen index. Always less than or equal to |
| 8707 | // JSObject::kMaxElementCount. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8708 | uint32_t index_offset_; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8709 | bool fast_elements_; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8710 | }; |
| 8711 | |
| 8712 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8713 | static uint32_t EstimateElementCount(Handle<JSArray> array) { |
| 8714 | uint32_t length = static_cast<uint32_t>(array->length()->Number()); |
| 8715 | int element_count = 0; |
| 8716 | switch (array->GetElementsKind()) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8717 | case JSObject::FAST_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8718 | // Fast elements can't have lengths that are not representable by |
| 8719 | // a 32-bit signed integer. |
| 8720 | ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0); |
| 8721 | int fast_length = static_cast<int>(length); |
| 8722 | Handle<FixedArray> elements(FixedArray::cast(array->elements())); |
| 8723 | for (int i = 0; i < fast_length; i++) { |
| 8724 | if (!elements->get(i)->IsTheHole()) element_count++; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 8725 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 8726 | break; |
| 8727 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8728 | case JSObject::DICTIONARY_ELEMENTS: { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8729 | Handle<NumberDictionary> dictionary( |
| 8730 | NumberDictionary::cast(array->elements())); |
| 8731 | int capacity = dictionary->Capacity(); |
| 8732 | for (int i = 0; i < capacity; i++) { |
| 8733 | Handle<Object> key(dictionary->KeyAt(i)); |
| 8734 | if (dictionary->IsKey(*key)) { |
| 8735 | element_count++; |
| 8736 | } |
| 8737 | } |
| 8738 | break; |
| 8739 | } |
| 8740 | default: |
| 8741 | // External arrays are always dense. |
| 8742 | return length; |
| 8743 | } |
| 8744 | // As an estimate, we assume that the prototype doesn't contain any |
| 8745 | // inherited elements. |
| 8746 | return element_count; |
| 8747 | } |
| 8748 | |
| 8749 | |
| 8750 | |
| 8751 | template<class ExternalArrayClass, class ElementType> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8752 | static void IterateExternalArrayElements(Isolate* isolate, |
| 8753 | Handle<JSObject> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8754 | bool elements_are_ints, |
| 8755 | bool elements_are_guaranteed_smis, |
| 8756 | ArrayConcatVisitor* visitor) { |
| 8757 | Handle<ExternalArrayClass> array( |
| 8758 | ExternalArrayClass::cast(receiver->elements())); |
| 8759 | uint32_t len = static_cast<uint32_t>(array->length()); |
| 8760 | |
| 8761 | ASSERT(visitor != NULL); |
| 8762 | if (elements_are_ints) { |
| 8763 | if (elements_are_guaranteed_smis) { |
| 8764 | for (uint32_t j = 0; j < len; j++) { |
| 8765 | HandleScope loop_scope; |
| 8766 | Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get(j)))); |
| 8767 | visitor->visit(j, e); |
| 8768 | } |
| 8769 | } else { |
| 8770 | for (uint32_t j = 0; j < len; j++) { |
| 8771 | HandleScope loop_scope; |
| 8772 | int64_t val = static_cast<int64_t>(array->get(j)); |
| 8773 | if (Smi::IsValid(static_cast<intptr_t>(val))) { |
| 8774 | Handle<Smi> e(Smi::FromInt(static_cast<int>(val))); |
| 8775 | visitor->visit(j, e); |
| 8776 | } else { |
| 8777 | Handle<Object> e = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8778 | isolate->factory()->NewNumber(static_cast<ElementType>(val)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8779 | visitor->visit(j, e); |
| 8780 | } |
| 8781 | } |
| 8782 | } |
| 8783 | } else { |
| 8784 | for (uint32_t j = 0; j < len; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8785 | HandleScope loop_scope(isolate); |
| 8786 | Handle<Object> e = isolate->factory()->NewNumber(array->get(j)); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8787 | visitor->visit(j, e); |
| 8788 | } |
| 8789 | } |
| 8790 | } |
| 8791 | |
| 8792 | |
| 8793 | // Used for sorting indices in a List<uint32_t>. |
| 8794 | static int compareUInt32(const uint32_t* ap, const uint32_t* bp) { |
| 8795 | uint32_t a = *ap; |
| 8796 | uint32_t b = *bp; |
| 8797 | return (a == b) ? 0 : (a < b) ? -1 : 1; |
| 8798 | } |
| 8799 | |
| 8800 | |
| 8801 | static void CollectElementIndices(Handle<JSObject> object, |
| 8802 | uint32_t range, |
| 8803 | List<uint32_t>* indices) { |
| 8804 | JSObject::ElementsKind kind = object->GetElementsKind(); |
| 8805 | switch (kind) { |
| 8806 | case JSObject::FAST_ELEMENTS: { |
| 8807 | Handle<FixedArray> elements(FixedArray::cast(object->elements())); |
| 8808 | uint32_t length = static_cast<uint32_t>(elements->length()); |
| 8809 | if (range < length) length = range; |
| 8810 | for (uint32_t i = 0; i < length; i++) { |
| 8811 | if (!elements->get(i)->IsTheHole()) { |
| 8812 | indices->Add(i); |
| 8813 | } |
| 8814 | } |
| 8815 | break; |
| 8816 | } |
| 8817 | case JSObject::DICTIONARY_ELEMENTS: { |
| 8818 | Handle<NumberDictionary> dict(NumberDictionary::cast(object->elements())); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8819 | uint32_t capacity = dict->Capacity(); |
| 8820 | for (uint32_t j = 0; j < capacity; j++) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8821 | HandleScope loop_scope; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8822 | Handle<Object> k(dict->KeyAt(j)); |
| 8823 | if (dict->IsKey(*k)) { |
| 8824 | ASSERT(k->IsNumber()); |
| 8825 | uint32_t index = static_cast<uint32_t>(k->Number()); |
| 8826 | if (index < range) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8827 | indices->Add(index); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 8828 | } |
| 8829 | } |
| 8830 | } |
| 8831 | break; |
| 8832 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8833 | default: { |
| 8834 | int dense_elements_length; |
| 8835 | switch (kind) { |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8836 | case JSObject::EXTERNAL_PIXEL_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8837 | dense_elements_length = |
| 8838 | ExternalPixelArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8839 | break; |
| 8840 | } |
| 8841 | case JSObject::EXTERNAL_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8842 | dense_elements_length = |
| 8843 | ExternalByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8844 | break; |
| 8845 | } |
| 8846 | case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8847 | dense_elements_length = |
| 8848 | ExternalUnsignedByteArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8849 | break; |
| 8850 | } |
| 8851 | case JSObject::EXTERNAL_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8852 | dense_elements_length = |
| 8853 | ExternalShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8854 | break; |
| 8855 | } |
| 8856 | case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8857 | dense_elements_length = |
| 8858 | ExternalUnsignedShortArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8859 | break; |
| 8860 | } |
| 8861 | case JSObject::EXTERNAL_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8862 | dense_elements_length = |
| 8863 | ExternalIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8864 | break; |
| 8865 | } |
| 8866 | case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8867 | dense_elements_length = |
| 8868 | ExternalUnsignedIntArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8869 | break; |
| 8870 | } |
| 8871 | case JSObject::EXTERNAL_FLOAT_ELEMENTS: { |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 8872 | dense_elements_length = |
| 8873 | ExternalFloatArray::cast(object->elements())->length(); |
| 8874 | break; |
| 8875 | } |
| 8876 | case JSObject::EXTERNAL_DOUBLE_ELEMENTS: { |
| 8877 | dense_elements_length = |
| 8878 | ExternalDoubleArray::cast(object->elements())->length(); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8879 | break; |
| 8880 | } |
| 8881 | default: |
| 8882 | UNREACHABLE(); |
| 8883 | dense_elements_length = 0; |
| 8884 | break; |
| 8885 | } |
| 8886 | uint32_t length = static_cast<uint32_t>(dense_elements_length); |
| 8887 | if (range <= length) { |
| 8888 | length = range; |
| 8889 | // We will add all indices, so we might as well clear it first |
| 8890 | // and avoid duplicates. |
| 8891 | indices->Clear(); |
| 8892 | } |
| 8893 | for (uint32_t i = 0; i < length; i++) { |
| 8894 | indices->Add(i); |
| 8895 | } |
| 8896 | if (length == range) return; // All indices accounted for already. |
| 8897 | break; |
| 8898 | } |
| 8899 | } |
| 8900 | |
| 8901 | Handle<Object> prototype(object->GetPrototype()); |
| 8902 | if (prototype->IsJSObject()) { |
| 8903 | // The prototype will usually have no inherited element indices, |
| 8904 | // but we have to check. |
| 8905 | CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices); |
| 8906 | } |
| 8907 | } |
| 8908 | |
| 8909 | |
| 8910 | /** |
| 8911 | * A helper function that visits elements of a JSArray in numerical |
| 8912 | * order. |
| 8913 | * |
| 8914 | * The visitor argument called for each existing element in the array |
| 8915 | * with the element index and the element's value. |
| 8916 | * Afterwards it increments the base-index of the visitor by the array |
| 8917 | * length. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8918 | * Returns false if any access threw an exception, otherwise true. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8919 | */ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8920 | static bool IterateElements(Isolate* isolate, |
| 8921 | Handle<JSArray> receiver, |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8922 | ArrayConcatVisitor* visitor) { |
| 8923 | uint32_t length = static_cast<uint32_t>(receiver->length()->Number()); |
| 8924 | switch (receiver->GetElementsKind()) { |
| 8925 | case JSObject::FAST_ELEMENTS: { |
| 8926 | // Run through the elements FixedArray and use HasElement and GetElement |
| 8927 | // to check the prototype for missing elements. |
| 8928 | Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); |
| 8929 | int fast_length = static_cast<int>(length); |
| 8930 | ASSERT(fast_length <= elements->length()); |
| 8931 | for (int j = 0; j < fast_length; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8932 | HandleScope loop_scope(isolate); |
| 8933 | Handle<Object> element_value(elements->get(j), isolate); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8934 | if (!element_value->IsTheHole()) { |
| 8935 | visitor->visit(j, element_value); |
| 8936 | } else if (receiver->HasElement(j)) { |
| 8937 | // Call GetElement on receiver, not its prototype, or getters won't |
| 8938 | // have the correct receiver. |
| 8939 | element_value = GetElement(receiver, j); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8940 | if (element_value.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8941 | visitor->visit(j, element_value); |
| 8942 | } |
| 8943 | } |
| 8944 | break; |
| 8945 | } |
| 8946 | case JSObject::DICTIONARY_ELEMENTS: { |
| 8947 | Handle<NumberDictionary> dict(receiver->element_dictionary()); |
| 8948 | List<uint32_t> indices(dict->Capacity() / 2); |
| 8949 | // Collect all indices in the object and the prototypes less |
| 8950 | // than length. This might introduce duplicates in the indices list. |
| 8951 | CollectElementIndices(receiver, length, &indices); |
| 8952 | indices.Sort(&compareUInt32); |
| 8953 | int j = 0; |
| 8954 | int n = indices.length(); |
| 8955 | while (j < n) { |
| 8956 | HandleScope loop_scope; |
| 8957 | uint32_t index = indices[j]; |
| 8958 | Handle<Object> element = GetElement(receiver, index); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 8959 | if (element.is_null()) return false; |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8960 | visitor->visit(index, element); |
| 8961 | // Skip to next different index (i.e., omit duplicates). |
| 8962 | do { |
| 8963 | j++; |
| 8964 | } while (j < n && indices[j] == index); |
| 8965 | } |
| 8966 | break; |
| 8967 | } |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 8968 | case JSObject::EXTERNAL_PIXEL_ELEMENTS: { |
| 8969 | Handle<ExternalPixelArray> pixels(ExternalPixelArray::cast( |
| 8970 | receiver->elements())); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8971 | for (uint32_t j = 0; j < length; j++) { |
| 8972 | Handle<Smi> e(Smi::FromInt(pixels->get(j))); |
| 8973 | visitor->visit(j, e); |
| 8974 | } |
| 8975 | break; |
| 8976 | } |
| 8977 | case JSObject::EXTERNAL_BYTE_ELEMENTS: { |
| 8978 | IterateExternalArrayElements<ExternalByteArray, int8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8979 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8980 | break; |
| 8981 | } |
| 8982 | case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: { |
| 8983 | IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8984 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8985 | break; |
| 8986 | } |
| 8987 | case JSObject::EXTERNAL_SHORT_ELEMENTS: { |
| 8988 | IterateExternalArrayElements<ExternalShortArray, int16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8989 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8990 | break; |
| 8991 | } |
| 8992 | case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: { |
| 8993 | IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8994 | isolate, receiver, true, true, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 8995 | break; |
| 8996 | } |
| 8997 | case JSObject::EXTERNAL_INT_ELEMENTS: { |
| 8998 | IterateExternalArrayElements<ExternalIntArray, int32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 8999 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9000 | break; |
| 9001 | } |
| 9002 | case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: { |
| 9003 | IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9004 | isolate, receiver, true, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9005 | break; |
| 9006 | } |
| 9007 | case JSObject::EXTERNAL_FLOAT_ELEMENTS: { |
| 9008 | IterateExternalArrayElements<ExternalFloatArray, float>( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9009 | isolate, receiver, false, false, visitor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9010 | break; |
| 9011 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 9012 | case JSObject::EXTERNAL_DOUBLE_ELEMENTS: { |
| 9013 | IterateExternalArrayElements<ExternalDoubleArray, double>( |
| 9014 | isolate, receiver, false, false, visitor); |
| 9015 | break; |
| 9016 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 9017 | default: |
| 9018 | UNREACHABLE(); |
| 9019 | break; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9020 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9021 | visitor->increase_index_offset(length); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9022 | return true; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
| 9025 | |
| 9026 | /** |
| 9027 | * Array::concat implementation. |
| 9028 | * See ECMAScript 262, 15.4.4.4. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9029 | * 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] | 9030 | * following the ECMAScript 5 specification. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9031 | */ |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9032 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayConcat) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9033 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9034 | HandleScope handle_scope(isolate); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9035 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9036 | CONVERT_ARG_CHECKED(JSArray, arguments, 0); |
| 9037 | int argument_count = static_cast<int>(arguments->length()->Number()); |
| 9038 | RUNTIME_ASSERT(arguments->HasFastElements()); |
| 9039 | Handle<FixedArray> elements(FixedArray::cast(arguments->elements())); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9040 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9041 | // Pass 1: estimate the length and number of elements of the result. |
| 9042 | // The actual length can be larger if any of the arguments have getters |
| 9043 | // that mutate other arguments (but will otherwise be precise). |
| 9044 | // 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] | 9045 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9046 | uint32_t estimate_result_length = 0; |
| 9047 | uint32_t estimate_nof_elements = 0; |
| 9048 | { |
| 9049 | for (int i = 0; i < argument_count; i++) { |
| 9050 | HandleScope loop_scope; |
| 9051 | Handle<Object> obj(elements->get(i)); |
| 9052 | uint32_t length_estimate; |
| 9053 | uint32_t element_estimate; |
| 9054 | if (obj->IsJSArray()) { |
| 9055 | Handle<JSArray> array(Handle<JSArray>::cast(obj)); |
| 9056 | length_estimate = |
| 9057 | static_cast<uint32_t>(array->length()->Number()); |
| 9058 | element_estimate = |
| 9059 | EstimateElementCount(array); |
| 9060 | } else { |
| 9061 | length_estimate = 1; |
| 9062 | element_estimate = 1; |
| 9063 | } |
| 9064 | // Avoid overflows by capping at kMaxElementCount. |
| 9065 | if (JSObject::kMaxElementCount - estimate_result_length < |
| 9066 | length_estimate) { |
| 9067 | estimate_result_length = JSObject::kMaxElementCount; |
| 9068 | } else { |
| 9069 | estimate_result_length += length_estimate; |
| 9070 | } |
| 9071 | if (JSObject::kMaxElementCount - estimate_nof_elements < |
| 9072 | element_estimate) { |
| 9073 | estimate_nof_elements = JSObject::kMaxElementCount; |
| 9074 | } else { |
| 9075 | estimate_nof_elements += element_estimate; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9076 | } |
| 9077 | } |
| 9078 | } |
| 9079 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9080 | // If estimated number of elements is more than half of length, a |
| 9081 | // fixed array (fast case) is more time and space-efficient than a |
| 9082 | // dictionary. |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9083 | bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9084 | |
| 9085 | Handle<FixedArray> storage; |
| 9086 | if (fast_case) { |
| 9087 | // The backing storage array must have non-existing elements to |
| 9088 | // preserve holes across concat operations. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9089 | storage = isolate->factory()->NewFixedArrayWithHoles( |
| 9090 | estimate_result_length); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9091 | } else { |
| 9092 | // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate |
| 9093 | uint32_t at_least_space_for = estimate_nof_elements + |
| 9094 | (estimate_nof_elements >> 2); |
| 9095 | storage = Handle<FixedArray>::cast( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9096 | isolate->factory()->NewNumberDictionary(at_least_space_for)); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9097 | } |
| 9098 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9099 | ArrayConcatVisitor visitor(isolate, storage, fast_case); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9100 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9101 | for (int i = 0; i < argument_count; i++) { |
| 9102 | Handle<Object> obj(elements->get(i)); |
| 9103 | if (obj->IsJSArray()) { |
| 9104 | Handle<JSArray> array = Handle<JSArray>::cast(obj); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9105 | if (!IterateElements(isolate, array, &visitor)) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 9106 | return Failure::Exception(); |
| 9107 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9108 | } else { |
| 9109 | visitor.visit(0, obj); |
| 9110 | visitor.increase_index_offset(1); |
| 9111 | } |
| 9112 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9113 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 9114 | return *visitor.ToArray(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 9115 | } |
| 9116 | |
| 9117 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9118 | // This will not allocate (flatten the string), but it may run |
| 9119 | // very slowly for very deeply nested ConsStrings. For debugging use only. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9120 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GlobalPrint) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9121 | NoHandleAllocation ha; |
| 9122 | ASSERT(args.length() == 1); |
| 9123 | |
| 9124 | CONVERT_CHECKED(String, string, args[0]); |
| 9125 | StringInputBuffer buffer(string); |
| 9126 | while (buffer.has_more()) { |
| 9127 | uint16_t character = buffer.GetNext(); |
| 9128 | PrintF("%c", character); |
| 9129 | } |
| 9130 | return string; |
| 9131 | } |
| 9132 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9133 | // Moves all own elements of an object, that are below a limit, to positions |
| 9134 | // starting at zero. All undefined values are placed after non-undefined values, |
| 9135 | // and are followed by non-existing element. Does not change the length |
| 9136 | // property. |
| 9137 | // Returns the number of non-undefined elements collected. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9138 | RUNTIME_FUNCTION(MaybeObject*, Runtime_RemoveArrayHoles) { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9139 | ASSERT(args.length() == 2); |
| 9140 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9141 | CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]); |
| 9142 | return object->PrepareElementsForSort(limit); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
| 9145 | |
| 9146 | // 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] | 9147 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MoveArrayContents) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9148 | ASSERT(args.length() == 2); |
| 9149 | CONVERT_CHECKED(JSArray, from, args[0]); |
| 9150 | CONVERT_CHECKED(JSArray, to, args[1]); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9151 | HeapObject* new_elements = from->elements(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9152 | MaybeObject* maybe_new_map; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9153 | if (new_elements->map() == isolate->heap()->fixed_array_map() || |
| 9154 | new_elements->map() == isolate->heap()->fixed_cow_array_map()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9155 | maybe_new_map = to->map()->GetFastElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9156 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9157 | maybe_new_map = to->map()->GetSlowElementsMap(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9158 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9159 | Object* new_map; |
| 9160 | if (!maybe_new_map->ToObject(&new_map)) return maybe_new_map; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 9161 | to->set_map(Map::cast(new_map)); |
| 9162 | to->set_elements(new_elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9163 | to->set_length(from->length()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9164 | Object* obj; |
| 9165 | { MaybeObject* maybe_obj = from->ResetElements(); |
| 9166 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 9167 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9168 | from->set_length(Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9169 | return to; |
| 9170 | } |
| 9171 | |
| 9172 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9173 | // How many elements does this object/array have? |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9174 | RUNTIME_FUNCTION(MaybeObject*, Runtime_EstimateNumberOfElements) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9175 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9176 | CONVERT_CHECKED(JSObject, object, args[0]); |
| 9177 | HeapObject* elements = object->elements(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9178 | if (elements->IsDictionary()) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 9179 | return Smi::FromInt(NumberDictionary::cast(elements)->NumberOfElements()); |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9180 | } else if (object->IsJSArray()) { |
| 9181 | return JSArray::cast(object)->length(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9182 | } else { |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 9183 | return Smi::FromInt(FixedArray::cast(elements)->length()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9184 | } |
| 9185 | } |
| 9186 | |
| 9187 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9188 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SwapElements) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9189 | HandleScope handle_scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9190 | |
| 9191 | ASSERT_EQ(3, args.length()); |
| 9192 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9193 | CONVERT_ARG_CHECKED(JSObject, object, 0); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9194 | Handle<Object> key1 = args.at<Object>(1); |
| 9195 | Handle<Object> key2 = args.at<Object>(2); |
| 9196 | |
| 9197 | uint32_t index1, index2; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9198 | if (!key1->ToArrayIndex(&index1) |
| 9199 | || !key2->ToArrayIndex(&index2)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9200 | return isolate->ThrowIllegalOperation(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9203 | Handle<JSObject> jsobject = Handle<JSObject>::cast(object); |
| 9204 | Handle<Object> tmp1 = GetElement(jsobject, index1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9205 | RETURN_IF_EMPTY_HANDLE(isolate, tmp1); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9206 | Handle<Object> tmp2 = GetElement(jsobject, index2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9207 | RETURN_IF_EMPTY_HANDLE(isolate, tmp2); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9208 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9209 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 9210 | SetElement(jsobject, index1, tmp2, kStrictMode)); |
| 9211 | RETURN_IF_EMPTY_HANDLE(isolate, |
| 9212 | SetElement(jsobject, index2, tmp1, kStrictMode)); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 9213 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9214 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9218 | // 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] | 9219 | // might have elements. Can either return keys (positive integers) or |
| 9220 | // intervals (pair of a negative integer (-start-1) followed by a |
| 9221 | // positive (length)) or undefined values. |
| 9222 | // 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] | 9223 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetArrayKeys) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9224 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9225 | HandleScope scope(isolate); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9226 | CONVERT_ARG_CHECKED(JSObject, array, 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9227 | CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9228 | if (array->elements()->IsDictionary()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9229 | // Create an array and get all the keys into it, then remove all the |
| 9230 | // 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] | 9231 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(array, INCLUDE_PROTOS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9232 | int keys_length = keys->length(); |
| 9233 | for (int i = 0; i < keys_length; i++) { |
| 9234 | Object* key = keys->get(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9235 | uint32_t index = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9236 | if (!key->ToArrayIndex(&index) || index >= length) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9237 | // Zap invalid keys. |
| 9238 | keys->set_undefined(i); |
| 9239 | } |
| 9240 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9241 | return *isolate->factory()->NewJSArrayWithElements(keys); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9242 | } else { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9243 | ASSERT(array->HasFastElements()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9244 | Handle<FixedArray> single_interval = isolate->factory()->NewFixedArray(2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9245 | // -1 means start of array. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 9246 | single_interval->set(0, Smi::FromInt(-1)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 9247 | uint32_t actual_length = |
| 9248 | static_cast<uint32_t>(FixedArray::cast(array->elements())->length()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 9249 | uint32_t min_length = actual_length < length ? actual_length : length; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9250 | Handle<Object> length_object = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9251 | isolate->factory()->NewNumber(static_cast<double>(min_length)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9252 | single_interval->set(1, *length_object); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9253 | return *isolate->factory()->NewJSArrayWithElements(single_interval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9254 | } |
| 9255 | } |
| 9256 | |
| 9257 | |
| 9258 | // DefineAccessor takes an optional final argument which is the |
| 9259 | // property attributes (eg, DONT_ENUM, DONT_DELETE). IMPORTANT: due |
| 9260 | // to the way accessors are implemented, it is set for both the getter |
| 9261 | // and setter on the first call to DefineAccessor and ignored on |
| 9262 | // subsequent calls. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9263 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DefineAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9264 | RUNTIME_ASSERT(args.length() == 4 || args.length() == 5); |
| 9265 | // Compute attributes. |
| 9266 | PropertyAttributes attributes = NONE; |
| 9267 | if (args.length() == 5) { |
| 9268 | CONVERT_CHECKED(Smi, attrs, args[4]); |
| 9269 | int value = attrs->value(); |
| 9270 | // Only attribute bits should be set. |
| 9271 | ASSERT((value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0); |
| 9272 | attributes = static_cast<PropertyAttributes>(value); |
| 9273 | } |
| 9274 | |
| 9275 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 9276 | CONVERT_CHECKED(String, name, args[1]); |
| 9277 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 9278 | CONVERT_CHECKED(JSFunction, fun, args[3]); |
| 9279 | return obj->DefineAccessor(name, flag->value() == 0, fun, attributes); |
| 9280 | } |
| 9281 | |
| 9282 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9283 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LookupAccessor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9284 | ASSERT(args.length() == 3); |
| 9285 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 9286 | CONVERT_CHECKED(String, name, args[1]); |
| 9287 | CONVERT_CHECKED(Smi, flag, args[2]); |
| 9288 | return obj->LookupAccessor(name, flag->value() == 0); |
| 9289 | } |
| 9290 | |
| 9291 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9292 | #ifdef ENABLE_DEBUGGER_SUPPORT |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9293 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugBreak) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 9294 | ASSERT(args.length() == 0); |
| 9295 | return Execution::DebugBreakHelper(); |
| 9296 | } |
| 9297 | |
| 9298 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9299 | // Helper functions for wrapping and unwrapping stack frame ids. |
| 9300 | static Smi* WrapFrameId(StackFrame::Id id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 9301 | ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9302 | return Smi::FromInt(id >> 2); |
| 9303 | } |
| 9304 | |
| 9305 | |
| 9306 | static StackFrame::Id UnwrapFrameId(Smi* wrapped) { |
| 9307 | return static_cast<StackFrame::Id>(wrapped->value() << 2); |
| 9308 | } |
| 9309 | |
| 9310 | |
| 9311 | // Adds a JavaScript function as a debug event listener. |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9312 | // args[0]: debug event listener function to set or null or undefined for |
| 9313 | // clearing the event listener function |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9314 | // args[1]: object supplied during callback |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9315 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDebugEventListener) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9316 | ASSERT(args.length() == 2); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 9317 | RUNTIME_ASSERT(args[0]->IsJSFunction() || |
| 9318 | args[0]->IsUndefined() || |
| 9319 | args[0]->IsNull()); |
| 9320 | Handle<Object> callback = args.at<Object>(0); |
| 9321 | Handle<Object> data = args.at<Object>(1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9322 | isolate->debugger()->SetEventListener(callback, data); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9323 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9324 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9325 | } |
| 9326 | |
| 9327 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9328 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Break) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 9329 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9330 | isolate->stack_guard()->DebugBreak(); |
| 9331 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9332 | } |
| 9333 | |
| 9334 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9335 | static MaybeObject* DebugLookupResultValue(Heap* heap, |
| 9336 | Object* receiver, |
| 9337 | String* name, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9338 | LookupResult* result, |
| 9339 | bool* caught_exception) { |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9340 | Object* value; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9341 | switch (result->type()) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 9342 | case NORMAL: |
| 9343 | value = result->holder()->GetNormalizedProperty(result); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9344 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9345 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9346 | } |
| 9347 | return value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9348 | case FIELD: |
| 9349 | value = |
| 9350 | JSObject::cast( |
| 9351 | result->holder())->FastPropertyAt(result->GetFieldIndex()); |
| 9352 | if (value->IsTheHole()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9353 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9354 | } |
| 9355 | return value; |
| 9356 | case CONSTANT_FUNCTION: |
| 9357 | return result->GetConstantFunction(); |
| 9358 | case CALLBACKS: { |
| 9359 | Object* structure = result->GetCallbackObject(); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame^] | 9360 | if (structure->IsForeign() || structure->IsAccessorInfo()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9361 | MaybeObject* maybe_value = receiver->GetPropertyWithCallback( |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9362 | receiver, structure, name, result->holder()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9363 | if (!maybe_value->ToObject(&value)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9364 | if (maybe_value->IsRetryAfterGC()) return maybe_value; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9365 | ASSERT(maybe_value->IsException()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9366 | maybe_value = heap->isolate()->pending_exception(); |
| 9367 | heap->isolate()->clear_pending_exception(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9368 | if (caught_exception != NULL) { |
| 9369 | *caught_exception = true; |
| 9370 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9371 | return maybe_value; |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9372 | } |
| 9373 | return value; |
| 9374 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9375 | return heap->undefined_value(); |
sgjesse@chromium.org | 715915b | 2009-01-19 16:08:47 +0000 | [diff] [blame] | 9376 | } |
| 9377 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9378 | case INTERCEPTOR: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9379 | case MAP_TRANSITION: |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 9380 | case EXTERNAL_ARRAY_TRANSITION: |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9381 | case CONSTANT_TRANSITION: |
| 9382 | case NULL_DESCRIPTOR: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9383 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9384 | default: |
| 9385 | UNREACHABLE(); |
| 9386 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9387 | UNREACHABLE(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9388 | return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9389 | } |
| 9390 | |
| 9391 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 9392 | // Get debugger related details for an object property. |
| 9393 | // args[0]: object holding property |
| 9394 | // args[1]: name of the property |
| 9395 | // |
| 9396 | // The array returned contains the following information: |
| 9397 | // 0: Property value |
| 9398 | // 1: Property details |
| 9399 | // 2: Property value is exception |
| 9400 | // 3: Getter function if defined |
| 9401 | // 4: Setter function if defined |
| 9402 | // Items 2-4 are only filled if the property has either a getter or a setter |
| 9403 | // defined through __defineGetter__ and/or __defineSetter__. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9404 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPropertyDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9405 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9406 | |
| 9407 | ASSERT(args.length() == 2); |
| 9408 | |
| 9409 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9410 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9411 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9412 | // Make sure to set the current context to the context before the debugger was |
| 9413 | // entered (if the debugger is entered). The reason for switching context here |
| 9414 | // is that for some property lookups (accessors and interceptors) callbacks |
| 9415 | // into the embedding application can occour, and the embedding application |
| 9416 | // could have the assumption that its own global context is the current |
| 9417 | // context and not some internal debugger context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9418 | SaveContext save(isolate); |
| 9419 | if (isolate->debug()->InDebugger()) { |
| 9420 | isolate->set_context(*isolate->debug()->debugger_entry()->GetContext()); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 9421 | } |
| 9422 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9423 | // Skip the global proxy as it has no properties and always delegates to the |
| 9424 | // real global object. |
| 9425 | if (obj->IsJSGlobalProxy()) { |
| 9426 | obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype())); |
| 9427 | } |
| 9428 | |
| 9429 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9430 | // Check if the name is trivially convertible to an index and get the element |
| 9431 | // if so. |
| 9432 | uint32_t index; |
| 9433 | if (name->AsArrayIndex(&index)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9434 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(2); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9435 | Object* element_or_char; |
| 9436 | { MaybeObject* maybe_element_or_char = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9437 | Runtime::GetElementOrCharAt(isolate, obj, index); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9438 | if (!maybe_element_or_char->ToObject(&element_or_char)) { |
| 9439 | return maybe_element_or_char; |
| 9440 | } |
| 9441 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 9442 | details->set(0, element_or_char); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9443 | details->set(1, PropertyDetails(NONE, NORMAL).AsSmi()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9444 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9445 | } |
| 9446 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9447 | // Find the number of objects making up this. |
| 9448 | int length = LocalPrototypeChainLength(*obj); |
| 9449 | |
| 9450 | // Try local lookup on each of the objects. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9451 | Handle<JSObject> jsproto = obj; |
| 9452 | for (int i = 0; i < length; i++) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9453 | LookupResult result; |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9454 | jsproto->LocalLookup(*name, &result); |
| 9455 | if (result.IsProperty()) { |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9456 | // LookupResult is not GC safe as it holds raw object pointers. |
| 9457 | // GC can happen later in this code so put the required fields into |
| 9458 | // local variables using handles when required for later use. |
| 9459 | PropertyType result_type = result.type(); |
| 9460 | Handle<Object> result_callback_obj; |
| 9461 | if (result_type == CALLBACKS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9462 | result_callback_obj = Handle<Object>(result.GetCallbackObject(), |
| 9463 | isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9464 | } |
| 9465 | Smi* property_details = result.GetPropertyDetails().AsSmi(); |
| 9466 | // DebugLookupResultValue can cause GC so details from LookupResult needs |
| 9467 | // to be copied to handles before this. |
| 9468 | bool caught_exception = false; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9469 | Object* raw_value; |
| 9470 | { MaybeObject* maybe_raw_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9471 | DebugLookupResultValue(isolate->heap(), *obj, *name, |
| 9472 | &result, &caught_exception); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9473 | if (!maybe_raw_value->ToObject(&raw_value)) return maybe_raw_value; |
| 9474 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9475 | Handle<Object> value(raw_value, isolate); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9476 | |
| 9477 | // If the callback object is a fixed array then it contains JavaScript |
| 9478 | // getter and/or setter. |
| 9479 | bool hasJavaScriptAccessors = result_type == CALLBACKS && |
| 9480 | result_callback_obj->IsFixedArray(); |
| 9481 | Handle<FixedArray> details = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9482 | isolate->factory()->NewFixedArray(hasJavaScriptAccessors ? 5 : 2); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9483 | details->set(0, *value); |
| 9484 | details->set(1, property_details); |
| 9485 | if (hasJavaScriptAccessors) { |
| 9486 | details->set(2, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9487 | caught_exception ? isolate->heap()->true_value() |
| 9488 | : isolate->heap()->false_value()); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9489 | details->set(3, FixedArray::cast(*result_callback_obj)->get(0)); |
| 9490 | details->set(4, FixedArray::cast(*result_callback_obj)->get(1)); |
| 9491 | } |
| 9492 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9493 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 9494 | } |
| 9495 | if (i < length - 1) { |
| 9496 | jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype())); |
| 9497 | } |
| 9498 | } |
| 9499 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9500 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9501 | } |
| 9502 | |
| 9503 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9504 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetProperty) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9505 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9506 | |
| 9507 | ASSERT(args.length() == 2); |
| 9508 | |
| 9509 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9510 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9511 | |
| 9512 | LookupResult result; |
| 9513 | obj->Lookup(*name, &result); |
| 9514 | if (result.IsProperty()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9515 | return DebugLookupResultValue(isolate->heap(), *obj, *name, &result, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9516 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9517 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9518 | } |
| 9519 | |
| 9520 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9521 | // Return the property type calculated from the property details. |
| 9522 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9523 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyTypeFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9524 | ASSERT(args.length() == 1); |
| 9525 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9526 | PropertyType type = PropertyDetails(details).type(); |
| 9527 | return Smi::FromInt(static_cast<int>(type)); |
| 9528 | } |
| 9529 | |
| 9530 | |
| 9531 | // Return the property attribute calculated from the property details. |
| 9532 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9533 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyAttributesFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9534 | ASSERT(args.length() == 1); |
| 9535 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9536 | PropertyAttributes attributes = PropertyDetails(details).attributes(); |
| 9537 | return Smi::FromInt(static_cast<int>(attributes)); |
| 9538 | } |
| 9539 | |
| 9540 | |
| 9541 | // Return the property insertion index calculated from the property details. |
| 9542 | // args[0]: smi with property details. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9543 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPropertyIndexFromDetails) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9544 | ASSERT(args.length() == 1); |
| 9545 | CONVERT_CHECKED(Smi, details, args[0]); |
| 9546 | int index = PropertyDetails(details).index(); |
| 9547 | return Smi::FromInt(index); |
| 9548 | } |
| 9549 | |
| 9550 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9551 | // Return property value from named interceptor. |
| 9552 | // args[0]: object |
| 9553 | // args[1]: property name |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9554 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugNamedInterceptorPropertyValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9555 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9556 | ASSERT(args.length() == 2); |
| 9557 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9558 | RUNTIME_ASSERT(obj->HasNamedInterceptor()); |
| 9559 | CONVERT_ARG_CHECKED(String, name, 1); |
| 9560 | |
| 9561 | PropertyAttributes attributes; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9562 | return obj->GetPropertyWithInterceptor(*obj, *name, &attributes); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9563 | } |
| 9564 | |
| 9565 | |
| 9566 | // Return element value from indexed interceptor. |
| 9567 | // args[0]: object |
| 9568 | // args[1]: index |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9569 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugIndexedInterceptorElementValue) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9570 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9571 | ASSERT(args.length() == 2); |
| 9572 | CONVERT_ARG_CHECKED(JSObject, obj, 0); |
| 9573 | RUNTIME_ASSERT(obj->HasIndexedInterceptor()); |
| 9574 | CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]); |
| 9575 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9576 | return obj->GetElementWithInterceptor(*obj, index); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9577 | } |
| 9578 | |
| 9579 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9580 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CheckExecutionState) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9581 | ASSERT(args.length() >= 1); |
| 9582 | CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9583 | // Check that the break id is valid. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9584 | if (isolate->debug()->break_id() == 0 || |
| 9585 | break_id != isolate->debug()->break_id()) { |
| 9586 | return isolate->Throw( |
| 9587 | isolate->heap()->illegal_execution_state_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9588 | } |
| 9589 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9590 | return isolate->heap()->true_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9591 | } |
| 9592 | |
| 9593 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9594 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9595 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9596 | ASSERT(args.length() == 1); |
| 9597 | |
| 9598 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9599 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9600 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 9601 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9602 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 9603 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9604 | |
| 9605 | // Count all frames which are relevant to debugging stack trace. |
| 9606 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9607 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9608 | if (id == StackFrame::NO_ID) { |
| 9609 | // If there is no JavaScript stack frame count is 0. |
| 9610 | return Smi::FromInt(0); |
| 9611 | } |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9612 | for (JavaScriptFrameIterator it(isolate, id); !it.done(); it.Advance()) n++; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9613 | return Smi::FromInt(n); |
| 9614 | } |
| 9615 | |
| 9616 | |
| 9617 | static const int kFrameDetailsFrameIdIndex = 0; |
| 9618 | static const int kFrameDetailsReceiverIndex = 1; |
| 9619 | static const int kFrameDetailsFunctionIndex = 2; |
| 9620 | static const int kFrameDetailsArgumentCountIndex = 3; |
| 9621 | static const int kFrameDetailsLocalCountIndex = 4; |
| 9622 | static const int kFrameDetailsSourcePositionIndex = 5; |
| 9623 | static const int kFrameDetailsConstructCallIndex = 6; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9624 | static const int kFrameDetailsAtReturnIndex = 7; |
| 9625 | static const int kFrameDetailsDebuggerFrameIndex = 8; |
| 9626 | static const int kFrameDetailsFirstDynamicIndex = 9; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9627 | |
| 9628 | // Return an array with frame details |
| 9629 | // args[0]: number: break id |
| 9630 | // args[1]: number: frame index |
| 9631 | // |
| 9632 | // The array returned contains the following information: |
| 9633 | // 0: Frame id |
| 9634 | // 1: Receiver |
| 9635 | // 2: Function |
| 9636 | // 3: Argument count |
| 9637 | // 4: Local count |
| 9638 | // 5: Source position |
| 9639 | // 6: Constructor call |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9640 | // 7: Is at return |
| 9641 | // 8: Debugger frame |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9642 | // Arguments name, value |
| 9643 | // Locals name, value |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9644 | // Return value if any |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9645 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFrameDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9646 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9647 | ASSERT(args.length() == 2); |
| 9648 | |
| 9649 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9650 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 9651 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 9652 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 9653 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 9654 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9655 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9656 | Heap* heap = isolate->heap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9657 | |
| 9658 | // Find the relevant frame with the requested index. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9659 | StackFrame::Id id = isolate->debug()->break_frame_id(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9660 | if (id == StackFrame::NO_ID) { |
| 9661 | // If there are no JavaScript stack frames return undefined. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9662 | return heap->undefined_value(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9663 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9664 | int count = 0; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9665 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9666 | for (; !it.done(); it.Advance()) { |
| 9667 | if (count == index) break; |
| 9668 | count++; |
| 9669 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9670 | if (it.done()) return heap->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9671 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9672 | bool is_optimized_frame = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9673 | it.frame()->LookupCode()->kind() == Code::OPTIMIZED_FUNCTION; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9674 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9675 | // Traverse the saved contexts chain to find the active context for the |
| 9676 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9677 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9678 | while (save != NULL && !save->below(it.frame())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9679 | save = save->prev(); |
| 9680 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 9681 | ASSERT(save != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9682 | |
| 9683 | // Get the frame id. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9684 | Handle<Object> frame_id(WrapFrameId(it.frame()->id()), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9685 | |
| 9686 | // Find source position. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9687 | int position = |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9688 | it.frame()->LookupCode()->SourcePosition(it.frame()->pc()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9689 | |
| 9690 | // Check for constructor frame. |
| 9691 | bool constructor = it.frame()->IsConstructor(); |
| 9692 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9693 | // Get scope info and read from it for local variable information. |
| 9694 | Handle<JSFunction> function(JSFunction::cast(it.frame()->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9695 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9696 | ScopeInfo<> info(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9697 | |
| 9698 | // Get the context. |
| 9699 | Handle<Context> context(Context::cast(it.frame()->context())); |
| 9700 | |
| 9701 | // Get the locals names and values into a temporary array. |
| 9702 | // |
| 9703 | // TODO(1240907): Hide compiler-introduced stack variables |
| 9704 | // (e.g. .result)? For users of the debugger, they will probably be |
| 9705 | // confusing. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9706 | Handle<FixedArray> locals = |
| 9707 | isolate->factory()->NewFixedArray(info.NumberOfLocals() * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9708 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9709 | // Fill in the names of the locals. |
| 9710 | for (int i = 0; i < info.NumberOfLocals(); i++) { |
| 9711 | locals->set(i * 2, *info.LocalName(i)); |
| 9712 | } |
| 9713 | |
| 9714 | // Fill in the values of the locals. |
| 9715 | for (int i = 0; i < info.NumberOfLocals(); i++) { |
| 9716 | if (is_optimized_frame) { |
| 9717 | // If we are inspecting an optimized frame use undefined as the |
| 9718 | // value for all locals. |
| 9719 | // |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9720 | // TODO(1140): We should be able to get the correct values |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9721 | // for locals in optimized frames. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9722 | locals->set(i * 2 + 1, isolate->heap()->undefined_value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9723 | } else if (i < info.number_of_stack_slots()) { |
| 9724 | // Get the value from the stack. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9725 | locals->set(i * 2 + 1, it.frame()->GetExpression(i)); |
| 9726 | } else { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9727 | // Traverse the context chain to the function context as all local |
| 9728 | // variables stored in the context will be on the function context. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9729 | Handle<String> name = info.LocalName(i); |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 9730 | while (!context->is_function_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9731 | context = Handle<Context>(context->previous()); |
| 9732 | } |
| 9733 | ASSERT(context->is_function_context()); |
| 9734 | locals->set(i * 2 + 1, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9735 | context->get(scope_info->ContextSlotIndex(*name, NULL))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9736 | } |
| 9737 | } |
| 9738 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9739 | // Check whether this frame is positioned at return. If not top |
| 9740 | // frame or if the frame is optimized it cannot be at a return. |
| 9741 | bool at_return = false; |
| 9742 | if (!is_optimized_frame && index == 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9743 | at_return = isolate->debug()->IsBreakAtReturn(it.frame()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9744 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9745 | |
| 9746 | // If positioned just before return find the value to be returned and add it |
| 9747 | // to the frame information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9748 | Handle<Object> return_value = isolate->factory()->undefined_value(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9749 | if (at_return) { |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 9750 | StackFrameIterator it2(isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9751 | Address internal_frame_sp = NULL; |
| 9752 | while (!it2.done()) { |
| 9753 | if (it2.frame()->is_internal()) { |
| 9754 | internal_frame_sp = it2.frame()->sp(); |
| 9755 | } else { |
| 9756 | if (it2.frame()->is_java_script()) { |
| 9757 | if (it2.frame()->id() == it.frame()->id()) { |
| 9758 | // The internal frame just before the JavaScript frame contains the |
| 9759 | // value to return on top. A debug break at return will create an |
| 9760 | // internal frame to store the return value (eax/rax/r0) before |
| 9761 | // entering the debug break exit frame. |
| 9762 | if (internal_frame_sp != NULL) { |
| 9763 | return_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9764 | Handle<Object>(Memory::Object_at(internal_frame_sp), |
| 9765 | isolate); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9766 | break; |
| 9767 | } |
| 9768 | } |
| 9769 | } |
| 9770 | |
| 9771 | // Indicate that the previous frame was not an internal frame. |
| 9772 | internal_frame_sp = NULL; |
| 9773 | } |
| 9774 | it2.Advance(); |
| 9775 | } |
| 9776 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9777 | |
| 9778 | // Now advance to the arguments adapter frame (if any). It contains all |
| 9779 | // the provided parameters whereas the function frame always have the number |
| 9780 | // of arguments matching the functions parameters. The rest of the |
| 9781 | // information (except for what is collected above) is the same. |
| 9782 | it.AdvanceToArgumentsFrame(); |
| 9783 | |
| 9784 | // Find the number of arguments to fill. At least fill the number of |
| 9785 | // parameters for the function and fill more if more parameters are provided. |
| 9786 | int argument_count = info.number_of_parameters(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9787 | if (argument_count < it.frame()->ComputeParametersCount()) { |
| 9788 | argument_count = it.frame()->ComputeParametersCount(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9789 | } |
| 9790 | |
| 9791 | // Calculate the size of the result. |
| 9792 | int details_size = kFrameDetailsFirstDynamicIndex + |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9793 | 2 * (argument_count + info.NumberOfLocals()) + |
| 9794 | (at_return ? 1 : 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9795 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9796 | |
| 9797 | // Add the frame id. |
| 9798 | details->set(kFrameDetailsFrameIdIndex, *frame_id); |
| 9799 | |
| 9800 | // Add the function (same as in function frame). |
| 9801 | details->set(kFrameDetailsFunctionIndex, it.frame()->function()); |
| 9802 | |
| 9803 | // Add the arguments count. |
| 9804 | details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count)); |
| 9805 | |
| 9806 | // Add the locals count |
| 9807 | details->set(kFrameDetailsLocalCountIndex, |
| 9808 | Smi::FromInt(info.NumberOfLocals())); |
| 9809 | |
| 9810 | // Add the source position. |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 9811 | if (position != RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9812 | details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position)); |
| 9813 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9814 | details->set(kFrameDetailsSourcePositionIndex, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9815 | } |
| 9816 | |
| 9817 | // Add the constructor information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9818 | details->set(kFrameDetailsConstructCallIndex, heap->ToBoolean(constructor)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9819 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9820 | // Add the at return information. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9821 | details->set(kFrameDetailsAtReturnIndex, heap->ToBoolean(at_return)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9822 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9823 | // Add information on whether this frame is invoked in the debugger context. |
| 9824 | details->set(kFrameDetailsDebuggerFrameIndex, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9825 | heap->ToBoolean(*save->context() == |
| 9826 | *isolate->debug()->debug_context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9827 | |
| 9828 | // Fill the dynamic part. |
| 9829 | int details_index = kFrameDetailsFirstDynamicIndex; |
| 9830 | |
| 9831 | // Add arguments name and value. |
| 9832 | for (int i = 0; i < argument_count; i++) { |
| 9833 | // Name of the argument. |
| 9834 | if (i < info.number_of_parameters()) { |
| 9835 | details->set(details_index++, *info.parameter_name(i)); |
| 9836 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9837 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9838 | } |
| 9839 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9840 | // Parameter value. If we are inspecting an optimized frame, use |
| 9841 | // undefined as the value. |
| 9842 | // |
| 9843 | // TODO(3141533): We should be able to get the actual parameter |
| 9844 | // value for optimized frames. |
| 9845 | if (!is_optimized_frame && |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 9846 | (i < it.frame()->ComputeParametersCount())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9847 | details->set(details_index++, it.frame()->GetParameter(i)); |
| 9848 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9849 | details->set(details_index++, heap->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9850 | } |
| 9851 | } |
| 9852 | |
| 9853 | // Add locals name and value from the temporary copy from the function frame. |
| 9854 | for (int i = 0; i < info.NumberOfLocals() * 2; i++) { |
| 9855 | details->set(details_index++, locals->get(i)); |
| 9856 | } |
| 9857 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 9858 | // Add the value being returned. |
| 9859 | if (at_return) { |
| 9860 | details->set(details_index++, *return_value); |
| 9861 | } |
| 9862 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9863 | // Add the receiver (same as in function frame). |
| 9864 | // THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE |
| 9865 | // THE FRAME ITERATOR TO WRAP THE RECEIVER. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9866 | Handle<Object> receiver(it.frame()->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9867 | if (!receiver->IsJSObject()) { |
| 9868 | // If the receiver is NOT a JSObject we have hit an optimization |
| 9869 | // where a value object is not converted into a wrapped JS objects. |
| 9870 | // To hide this optimization from the debugger, we wrap the receiver |
| 9871 | // by creating correct wrapper object based on the calling frame's |
| 9872 | // global context. |
| 9873 | it.Advance(); |
| 9874 | Handle<Context> calling_frames_global_context( |
| 9875 | Context::cast(Context::cast(it.frame()->context())->global_context())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9876 | receiver = |
| 9877 | isolate->factory()->ToObject(receiver, calling_frames_global_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9878 | } |
| 9879 | details->set(kFrameDetailsReceiverIndex, *receiver); |
| 9880 | |
| 9881 | ASSERT_EQ(details_size, details_index); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9882 | return *isolate->factory()->NewJSArrayWithElements(details); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
| 9885 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9886 | // 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] | 9887 | static bool CopyContextLocalsToScopeObject( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9888 | Isolate* isolate, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9889 | Handle<SerializedScopeInfo> serialized_scope_info, |
| 9890 | ScopeInfo<>& scope_info, |
| 9891 | Handle<Context> context, |
| 9892 | Handle<JSObject> scope_object) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9893 | // Fill all context locals to the context extension. |
| 9894 | for (int i = Context::MIN_CONTEXT_SLOTS; |
| 9895 | i < scope_info.number_of_context_slots(); |
| 9896 | i++) { |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9897 | int context_index = serialized_scope_info->ContextSlotIndex( |
| 9898 | *scope_info.context_slot_name(i), NULL); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9899 | |
| 9900 | // Don't include the arguments shadow (.arguments) context variable. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9901 | if (*scope_info.context_slot_name(i) != |
| 9902 | isolate->heap()->arguments_shadow_symbol()) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9903 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9904 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9905 | SetProperty(scope_object, |
| 9906 | scope_info.context_slot_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9907 | Handle<Object>(context->get(context_index), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9908 | NONE, |
| 9909 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9910 | false); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9911 | } |
| 9912 | } |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9913 | |
| 9914 | return true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9915 | } |
| 9916 | |
| 9917 | |
| 9918 | // Create a plain JSObject which materializes the local scope for the specified |
| 9919 | // frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9920 | static Handle<JSObject> MaterializeLocalScope(Isolate* isolate, |
| 9921 | JavaScriptFrame* frame) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9922 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9923 | Handle<SharedFunctionInfo> shared(function->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9924 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 9925 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9926 | |
| 9927 | // Allocate and initialize a JSObject with all the arguments, stack locals |
| 9928 | // heap locals and extension properties of the debugged function. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9929 | Handle<JSObject> local_scope = |
| 9930 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9931 | |
| 9932 | // First fill all parameters. |
| 9933 | for (int i = 0; i < scope_info.number_of_parameters(); ++i) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9934 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9935 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9936 | SetProperty(local_scope, |
| 9937 | scope_info.parameter_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9938 | Handle<Object>(frame->GetParameter(i), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9939 | NONE, |
| 9940 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9941 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9942 | } |
| 9943 | |
| 9944 | // Second fill all stack locals. |
| 9945 | 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] | 9946 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9947 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9948 | SetProperty(local_scope, |
| 9949 | scope_info.stack_slot_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9950 | Handle<Object>(frame->GetExpression(i), isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9951 | NONE, |
| 9952 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9953 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9954 | } |
| 9955 | |
| 9956 | // Third fill all context locals. |
| 9957 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 9958 | Handle<Context> function_context(frame_context->fcontext()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9959 | if (!CopyContextLocalsToScopeObject(isolate, |
| 9960 | serialized_scope_info, scope_info, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9961 | function_context, local_scope)) { |
| 9962 | return Handle<JSObject>(); |
| 9963 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9964 | |
| 9965 | // Finally copy any properties from the function context extension. This will |
| 9966 | // be variables introduced by eval. |
| 9967 | if (function_context->closure() == *function) { |
| 9968 | if (function_context->has_extension() && |
| 9969 | !function_context->IsGlobalContext()) { |
| 9970 | Handle<JSObject> ext(JSObject::cast(function_context->extension())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 9971 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9972 | for (int i = 0; i < keys->length(); i++) { |
| 9973 | // Names of variables introduced by eval are strings. |
| 9974 | ASSERT(keys->get(i)->IsString()); |
| 9975 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9976 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9977 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 9978 | SetProperty(local_scope, |
| 9979 | key, |
| 9980 | GetProperty(ext, key), |
| 9981 | NONE, |
| 9982 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 9983 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9984 | } |
| 9985 | } |
| 9986 | } |
| 9987 | return local_scope; |
| 9988 | } |
| 9989 | |
| 9990 | |
| 9991 | // Create a plain JSObject which materializes the closure content for the |
| 9992 | // context. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 9993 | static Handle<JSObject> MaterializeClosure(Isolate* isolate, |
| 9994 | Handle<Context> context) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 9995 | ASSERT(context->is_function_context()); |
| 9996 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 9997 | Handle<SharedFunctionInfo> shared(context->closure()->shared()); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 9998 | Handle<SerializedScopeInfo> serialized_scope_info(shared->scope_info()); |
| 9999 | ScopeInfo<> scope_info(*serialized_scope_info); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10000 | |
| 10001 | // Allocate and initialize a JSObject with all the content of theis function |
| 10002 | // closure. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10003 | Handle<JSObject> closure_scope = |
| 10004 | isolate->factory()->NewJSObject(isolate->object_function()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10005 | |
| 10006 | // Check whether the arguments shadow object exists. |
| 10007 | int arguments_shadow_index = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10008 | shared->scope_info()->ContextSlotIndex( |
| 10009 | isolate->heap()->arguments_shadow_symbol(), NULL); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10010 | if (arguments_shadow_index >= 0) { |
| 10011 | // In this case all the arguments are available in the arguments shadow |
| 10012 | // object. |
| 10013 | Handle<JSObject> arguments_shadow( |
| 10014 | JSObject::cast(context->get(arguments_shadow_index))); |
| 10015 | for (int i = 0; i < scope_info.number_of_parameters(); ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10016 | // We don't expect exception-throwing getters on the arguments shadow. |
| 10017 | Object* element = arguments_shadow->GetElement(i)->ToObjectUnchecked(); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10018 | RETURN_IF_EMPTY_HANDLE_VALUE( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10019 | isolate, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10020 | SetProperty(closure_scope, |
| 10021 | scope_info.parameter_name(i), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10022 | Handle<Object>(element, isolate), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10023 | NONE, |
| 10024 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10025 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10026 | } |
| 10027 | } |
| 10028 | |
| 10029 | // Fill all context locals to the context extension. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10030 | if (!CopyContextLocalsToScopeObject(isolate, |
| 10031 | serialized_scope_info, scope_info, |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10032 | context, closure_scope)) { |
| 10033 | return Handle<JSObject>(); |
| 10034 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10035 | |
| 10036 | // Finally copy any properties from the function context extension. This will |
| 10037 | // be variables introduced by eval. |
| 10038 | if (context->has_extension()) { |
| 10039 | Handle<JSObject> ext(JSObject::cast(context->extension())); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 10040 | Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10041 | for (int i = 0; i < keys->length(); i++) { |
| 10042 | // Names of variables introduced by eval are strings. |
| 10043 | ASSERT(keys->get(i)->IsString()); |
| 10044 | Handle<String> key(String::cast(keys->get(i))); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10045 | RETURN_IF_EMPTY_HANDLE_VALUE( |
| 10046 | isolate, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 10047 | SetProperty(closure_scope, |
| 10048 | key, |
| 10049 | GetProperty(ext, key), |
| 10050 | NONE, |
| 10051 | kNonStrictMode), |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 10052 | Handle<JSObject>()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10053 | } |
| 10054 | } |
| 10055 | |
| 10056 | return closure_scope; |
| 10057 | } |
| 10058 | |
| 10059 | |
| 10060 | // Iterate over the actual scopes visible from a stack frame. All scopes are |
| 10061 | // backed by an actual context except the local scope, which is inserted |
| 10062 | // "artifically" in the context chain. |
| 10063 | class ScopeIterator { |
| 10064 | public: |
| 10065 | enum ScopeType { |
| 10066 | ScopeTypeGlobal = 0, |
| 10067 | ScopeTypeLocal, |
| 10068 | ScopeTypeWith, |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10069 | ScopeTypeClosure, |
| 10070 | // Every catch block contains an implicit with block (its parameter is |
| 10071 | // a JSContextExtensionObject) that extends current scope with a variable |
| 10072 | // holding exception object. Such with blocks are treated as scopes of their |
| 10073 | // own type. |
| 10074 | ScopeTypeCatch |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10075 | }; |
| 10076 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10077 | ScopeIterator(Isolate* isolate, JavaScriptFrame* frame) |
| 10078 | : isolate_(isolate), |
| 10079 | frame_(frame), |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10080 | function_(JSFunction::cast(frame->function())), |
| 10081 | context_(Context::cast(frame->context())), |
| 10082 | local_done_(false), |
| 10083 | at_local_(false) { |
| 10084 | |
| 10085 | // Check whether the first scope is actually a local scope. |
| 10086 | if (context_->IsGlobalContext()) { |
| 10087 | // If there is a stack slot for .result then this local scope has been |
| 10088 | // created for evaluating top level code and it is not a real local scope. |
| 10089 | // Checking for the existence of .result seems fragile, but the scope info |
| 10090 | // saved with the code object does not otherwise have that information. |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10091 | int index = function_->shared()->scope_info()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10092 | StackSlotIndex(isolate_->heap()->result_symbol()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10093 | at_local_ = index < 0; |
| 10094 | } else if (context_->is_function_context()) { |
| 10095 | at_local_ = true; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 10096 | } else if (context_->closure() != *function_) { |
| 10097 | // The context_ is a with block from the outer function. |
| 10098 | ASSERT(context_->has_extension()); |
| 10099 | at_local_ = true; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10100 | } |
| 10101 | } |
| 10102 | |
| 10103 | // More scopes? |
| 10104 | bool Done() { return context_.is_null(); } |
| 10105 | |
| 10106 | // Move to the next scope. |
| 10107 | void Next() { |
| 10108 | // If at a local scope mark the local scope as passed. |
| 10109 | if (at_local_) { |
| 10110 | at_local_ = false; |
| 10111 | local_done_ = true; |
| 10112 | |
| 10113 | // If the current context is not associated with the local scope the |
| 10114 | // current context is the next real scope, so don't move to the next |
| 10115 | // context in this case. |
| 10116 | if (context_->closure() != *function_) { |
| 10117 | return; |
| 10118 | } |
| 10119 | } |
| 10120 | |
| 10121 | // The global scope is always the last in the chain. |
| 10122 | if (context_->IsGlobalContext()) { |
| 10123 | context_ = Handle<Context>(); |
| 10124 | return; |
| 10125 | } |
| 10126 | |
| 10127 | // Move to the next context. |
| 10128 | if (context_->is_function_context()) { |
| 10129 | context_ = Handle<Context>(Context::cast(context_->closure()->context())); |
| 10130 | } else { |
| 10131 | context_ = Handle<Context>(context_->previous()); |
| 10132 | } |
| 10133 | |
| 10134 | // If passing the local scope indicate that the current scope is now the |
| 10135 | // local scope. |
| 10136 | if (!local_done_ && |
| 10137 | (context_->IsGlobalContext() || (context_->is_function_context()))) { |
| 10138 | at_local_ = true; |
| 10139 | } |
| 10140 | } |
| 10141 | |
| 10142 | // Return the type of the current scope. |
| 10143 | int Type() { |
| 10144 | if (at_local_) { |
| 10145 | return ScopeTypeLocal; |
| 10146 | } |
| 10147 | if (context_->IsGlobalContext()) { |
| 10148 | ASSERT(context_->global()->IsGlobalObject()); |
| 10149 | return ScopeTypeGlobal; |
| 10150 | } |
| 10151 | if (context_->is_function_context()) { |
| 10152 | return ScopeTypeClosure; |
| 10153 | } |
| 10154 | ASSERT(context_->has_extension()); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10155 | // Current scope is either an explicit with statement or a with statement |
| 10156 | // implicitely generated for a catch block. |
| 10157 | // If the extension object here is a JSContextExtensionObject then |
| 10158 | // current with statement is one frome a catch block otherwise it's a |
| 10159 | // regular with statement. |
| 10160 | if (context_->extension()->IsJSContextExtensionObject()) { |
| 10161 | return ScopeTypeCatch; |
| 10162 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10163 | return ScopeTypeWith; |
| 10164 | } |
| 10165 | |
| 10166 | // Return the JavaScript object with the content of the current scope. |
| 10167 | Handle<JSObject> ScopeObject() { |
| 10168 | switch (Type()) { |
| 10169 | case ScopeIterator::ScopeTypeGlobal: |
| 10170 | return Handle<JSObject>(CurrentContext()->global()); |
| 10171 | break; |
| 10172 | case ScopeIterator::ScopeTypeLocal: |
| 10173 | // Materialize the content of the local scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10174 | return MaterializeLocalScope(isolate_, frame_); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10175 | break; |
| 10176 | case ScopeIterator::ScopeTypeWith: |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10177 | case ScopeIterator::ScopeTypeCatch: |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10178 | // Return the with object. |
| 10179 | return Handle<JSObject>(CurrentContext()->extension()); |
| 10180 | break; |
| 10181 | case ScopeIterator::ScopeTypeClosure: |
| 10182 | // Materialize the content of the closure scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10183 | return MaterializeClosure(isolate_, CurrentContext()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10184 | break; |
| 10185 | } |
| 10186 | UNREACHABLE(); |
| 10187 | return Handle<JSObject>(); |
| 10188 | } |
| 10189 | |
| 10190 | // Return the context for this scope. For the local context there might not |
| 10191 | // be an actual context. |
| 10192 | Handle<Context> CurrentContext() { |
| 10193 | if (at_local_ && context_->closure() != *function_) { |
| 10194 | return Handle<Context>(); |
| 10195 | } |
| 10196 | return context_; |
| 10197 | } |
| 10198 | |
| 10199 | #ifdef DEBUG |
| 10200 | // Debug print of the content of the current scope. |
| 10201 | void DebugPrint() { |
| 10202 | switch (Type()) { |
| 10203 | case ScopeIterator::ScopeTypeGlobal: |
| 10204 | PrintF("Global:\n"); |
| 10205 | CurrentContext()->Print(); |
| 10206 | break; |
| 10207 | |
| 10208 | case ScopeIterator::ScopeTypeLocal: { |
| 10209 | PrintF("Local:\n"); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10210 | ScopeInfo<> scope_info(function_->shared()->scope_info()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10211 | scope_info.Print(); |
| 10212 | if (!CurrentContext().is_null()) { |
| 10213 | CurrentContext()->Print(); |
| 10214 | if (CurrentContext()->has_extension()) { |
| 10215 | Handle<JSObject> extension = |
| 10216 | Handle<JSObject>(CurrentContext()->extension()); |
| 10217 | if (extension->IsJSContextExtensionObject()) { |
| 10218 | extension->Print(); |
| 10219 | } |
| 10220 | } |
| 10221 | } |
| 10222 | break; |
| 10223 | } |
| 10224 | |
| 10225 | case ScopeIterator::ScopeTypeWith: { |
| 10226 | PrintF("With:\n"); |
| 10227 | Handle<JSObject> extension = |
| 10228 | Handle<JSObject>(CurrentContext()->extension()); |
| 10229 | extension->Print(); |
| 10230 | break; |
| 10231 | } |
| 10232 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10233 | case ScopeIterator::ScopeTypeCatch: { |
| 10234 | PrintF("Catch:\n"); |
| 10235 | Handle<JSObject> extension = |
| 10236 | Handle<JSObject>(CurrentContext()->extension()); |
| 10237 | extension->Print(); |
| 10238 | break; |
| 10239 | } |
| 10240 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10241 | case ScopeIterator::ScopeTypeClosure: { |
| 10242 | PrintF("Closure:\n"); |
| 10243 | CurrentContext()->Print(); |
| 10244 | if (CurrentContext()->has_extension()) { |
| 10245 | Handle<JSObject> extension = |
| 10246 | Handle<JSObject>(CurrentContext()->extension()); |
| 10247 | if (extension->IsJSContextExtensionObject()) { |
| 10248 | extension->Print(); |
| 10249 | } |
| 10250 | } |
| 10251 | break; |
| 10252 | } |
| 10253 | |
| 10254 | default: |
| 10255 | UNREACHABLE(); |
| 10256 | } |
| 10257 | PrintF("\n"); |
| 10258 | } |
| 10259 | #endif |
| 10260 | |
| 10261 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10262 | Isolate* isolate_; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10263 | JavaScriptFrame* frame_; |
| 10264 | Handle<JSFunction> function_; |
| 10265 | Handle<Context> context_; |
| 10266 | bool local_done_; |
| 10267 | bool at_local_; |
| 10268 | |
| 10269 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator); |
| 10270 | }; |
| 10271 | |
| 10272 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10273 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10274 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10275 | ASSERT(args.length() == 2); |
| 10276 | |
| 10277 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10278 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10279 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10280 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10281 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10282 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10283 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10284 | |
| 10285 | // Get the frame where the debugging is performed. |
| 10286 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10287 | JavaScriptFrameIterator it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10288 | JavaScriptFrame* frame = it.frame(); |
| 10289 | |
| 10290 | // Count the visible scopes. |
| 10291 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10292 | for (ScopeIterator it(isolate, frame); !it.Done(); it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10293 | n++; |
| 10294 | } |
| 10295 | |
| 10296 | return Smi::FromInt(n); |
| 10297 | } |
| 10298 | |
| 10299 | |
| 10300 | static const int kScopeDetailsTypeIndex = 0; |
| 10301 | static const int kScopeDetailsObjectIndex = 1; |
| 10302 | static const int kScopeDetailsSize = 2; |
| 10303 | |
| 10304 | // Return an array with scope details |
| 10305 | // args[0]: number: break id |
| 10306 | // args[1]: number: frame index |
| 10307 | // args[2]: number: scope index |
| 10308 | // |
| 10309 | // The array returned contains the following information: |
| 10310 | // 0: Scope type |
| 10311 | // 1: Scope object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10312 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScopeDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10313 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10314 | ASSERT(args.length() == 3); |
| 10315 | |
| 10316 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10317 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10318 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10319 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10320 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10321 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10322 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10323 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[2]); |
| 10324 | |
| 10325 | // Get the frame where the debugging is performed. |
| 10326 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10327 | JavaScriptFrameIterator frame_it(isolate, id); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10328 | JavaScriptFrame* frame = frame_it.frame(); |
| 10329 | |
| 10330 | // Find the requested scope. |
| 10331 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10332 | ScopeIterator it(isolate, frame); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10333 | for (; !it.Done() && n < index; it.Next()) { |
| 10334 | n++; |
| 10335 | } |
| 10336 | if (it.Done()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10337 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10338 | } |
| 10339 | |
| 10340 | // Calculate the size of the result. |
| 10341 | int details_size = kScopeDetailsSize; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10342 | Handle<FixedArray> details = isolate->factory()->NewFixedArray(details_size); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10343 | |
| 10344 | // Fill in scope details. |
| 10345 | details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10346 | Handle<JSObject> scope_object = it.ScopeObject(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10347 | RETURN_IF_EMPTY_HANDLE(isolate, scope_object); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 10348 | details->set(kScopeDetailsObjectIndex, *scope_object); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10349 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10350 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10351 | } |
| 10352 | |
| 10353 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10354 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugPrintScopes) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10355 | HandleScope scope(isolate); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10356 | ASSERT(args.length() == 0); |
| 10357 | |
| 10358 | #ifdef DEBUG |
| 10359 | // Print the scopes for the top frame. |
| 10360 | StackFrameLocator locator; |
| 10361 | JavaScriptFrame* frame = locator.FindJavaScriptFrame(0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10362 | for (ScopeIterator it(isolate, frame); !it.Done(); it.Next()) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10363 | it.DebugPrint(); |
| 10364 | } |
| 10365 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10366 | return isolate->heap()->undefined_value(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10367 | } |
| 10368 | |
| 10369 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10370 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadCount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10371 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10372 | ASSERT(args.length() == 1); |
| 10373 | |
| 10374 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10375 | Object* result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10376 | { MaybeObject* maybe_result = Runtime_CheckExecutionState( |
| 10377 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10378 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 10379 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10380 | |
| 10381 | // Count all archived V8 threads. |
| 10382 | int n = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10383 | for (ThreadState* thread = |
| 10384 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10385 | thread != NULL; |
| 10386 | thread = thread->Next()) { |
| 10387 | n++; |
| 10388 | } |
| 10389 | |
| 10390 | // Total number of threads is current thread and archived threads. |
| 10391 | return Smi::FromInt(n + 1); |
| 10392 | } |
| 10393 | |
| 10394 | |
| 10395 | static const int kThreadDetailsCurrentThreadIndex = 0; |
| 10396 | static const int kThreadDetailsThreadIdIndex = 1; |
| 10397 | static const int kThreadDetailsSize = 2; |
| 10398 | |
| 10399 | // Return an array with thread details |
| 10400 | // args[0]: number: break id |
| 10401 | // args[1]: number: thread index |
| 10402 | // |
| 10403 | // The array returned contains the following information: |
| 10404 | // 0: Is current thread? |
| 10405 | // 1: Thread id |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10406 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetThreadDetails) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10407 | HandleScope scope(isolate); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10408 | ASSERT(args.length() == 2); |
| 10409 | |
| 10410 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10411 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10412 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10413 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10414 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10415 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10416 | CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]); |
| 10417 | |
| 10418 | // Allocate array for result. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10419 | Handle<FixedArray> details = |
| 10420 | isolate->factory()->NewFixedArray(kThreadDetailsSize); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10421 | |
| 10422 | // Thread index 0 is current thread. |
| 10423 | if (index == 0) { |
| 10424 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10425 | details->set(kThreadDetailsCurrentThreadIndex, |
| 10426 | isolate->heap()->true_value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10427 | details->set(kThreadDetailsThreadIdIndex, |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 10428 | Smi::FromInt(ThreadId::Current().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10429 | } else { |
| 10430 | // Find the thread with the requested index. |
| 10431 | int n = 1; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10432 | ThreadState* thread = |
| 10433 | isolate->thread_manager()->FirstThreadStateInUse(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10434 | while (index != n && thread != NULL) { |
| 10435 | thread = thread->Next(); |
| 10436 | n++; |
| 10437 | } |
| 10438 | if (thread == NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10439 | return isolate->heap()->undefined_value(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10440 | } |
| 10441 | |
| 10442 | // Fill the details. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10443 | details->set(kThreadDetailsCurrentThreadIndex, |
| 10444 | isolate->heap()->false_value()); |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 10445 | details->set(kThreadDetailsThreadIdIndex, |
| 10446 | Smi::FromInt(thread->id().ToInteger())); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10447 | } |
| 10448 | |
| 10449 | // Convert to JS array and return. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10450 | return *isolate->factory()->NewJSArrayWithElements(details); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 10451 | } |
| 10452 | |
| 10453 | |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10454 | // Sets the disable break state |
| 10455 | // args[0]: disable break state |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10456 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetDisableBreak) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10457 | HandleScope scope(isolate); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10458 | ASSERT(args.length() == 1); |
| 10459 | CONVERT_BOOLEAN_CHECKED(disable_break, args[0]); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10460 | isolate->debug()->set_disable_break(disable_break); |
| 10461 | return isolate->heap()->undefined_value(); |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 10462 | } |
| 10463 | |
| 10464 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10465 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetBreakLocations) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10466 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10467 | ASSERT(args.length() == 1); |
| 10468 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10469 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 10470 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10471 | // Find the number of break points |
| 10472 | Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10473 | if (break_locations->IsUndefined()) return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10474 | // Return array as JS array |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10475 | return *isolate->factory()->NewJSArrayWithElements( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10476 | Handle<FixedArray>::cast(break_locations)); |
| 10477 | } |
| 10478 | |
| 10479 | |
| 10480 | // Set a break point in a function |
| 10481 | // args[0]: function |
| 10482 | // args[1]: number: break source position (within the function source) |
| 10483 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10484 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFunctionBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10485 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10486 | ASSERT(args.length() == 3); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 10487 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 10488 | Handle<SharedFunctionInfo> shared(fun->shared()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10489 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 10490 | RUNTIME_ASSERT(source_position >= 0); |
| 10491 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 10492 | |
| 10493 | // Set break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10494 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, |
| 10495 | &source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10496 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 10497 | return Smi::FromInt(source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10498 | } |
| 10499 | |
| 10500 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10501 | Object* Runtime::FindSharedFunctionInfoInScript(Isolate* isolate, |
| 10502 | Handle<Script> script, |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 10503 | int position) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10504 | // Iterate the heap looking for SharedFunctionInfo generated from the |
| 10505 | // script. The inner most SharedFunctionInfo containing the source position |
| 10506 | // for the requested break point is found. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 10507 | // NOTE: This might require several heap iterations. If the SharedFunctionInfo |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10508 | // which is found is not compiled it is compiled and the heap is iterated |
| 10509 | // again as the compilation might create inner functions from the newly |
| 10510 | // compiled function and the actual requested break point might be in one of |
| 10511 | // these functions. |
| 10512 | bool done = false; |
| 10513 | // The current candidate for the source position: |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10514 | int target_start_position = RelocInfo::kNoPosition; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10515 | Handle<SharedFunctionInfo> target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10516 | while (!done) { |
| 10517 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10518 | for (HeapObject* obj = iterator.next(); |
| 10519 | obj != NULL; obj = iterator.next()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10520 | if (obj->IsSharedFunctionInfo()) { |
| 10521 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj)); |
| 10522 | if (shared->script() == *script) { |
| 10523 | // If the SharedFunctionInfo found has the requested script data and |
| 10524 | // contains the source position it is a candidate. |
| 10525 | int start_position = shared->function_token_position(); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 10526 | if (start_position == RelocInfo::kNoPosition) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10527 | start_position = shared->start_position(); |
| 10528 | } |
| 10529 | if (start_position <= position && |
| 10530 | position <= shared->end_position()) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 10531 | // 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] | 10532 | // candidate this is the new candidate. |
| 10533 | if (target.is_null()) { |
| 10534 | target_start_position = start_position; |
| 10535 | target = shared; |
| 10536 | } else { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10537 | if (target_start_position == start_position && |
| 10538 | shared->end_position() == target->end_position()) { |
| 10539 | // If a top-level function contain only one function |
| 10540 | // declartion the source for the top-level and the function is |
| 10541 | // the same. In that case prefer the non top-level function. |
| 10542 | if (!shared->is_toplevel()) { |
| 10543 | target_start_position = start_position; |
| 10544 | target = shared; |
| 10545 | } |
| 10546 | } else if (target_start_position <= start_position && |
| 10547 | shared->end_position() <= target->end_position()) { |
| 10548 | // This containment check includes equality as a function inside |
| 10549 | // a top-level function can share either start or end position |
| 10550 | // with the top-level function. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10551 | target_start_position = start_position; |
| 10552 | target = shared; |
| 10553 | } |
| 10554 | } |
| 10555 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10556 | } |
| 10557 | } |
| 10558 | } |
| 10559 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10560 | if (target.is_null()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10561 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10562 | } |
| 10563 | |
| 10564 | // If the candidate found is compiled we are done. NOTE: when lazy |
| 10565 | // compilation of inner functions is introduced some additional checking |
| 10566 | // needs to be done here to compile inner functions. |
| 10567 | done = target->is_compiled(); |
| 10568 | if (!done) { |
| 10569 | // If the candidate is not compiled compile it to reveal any inner |
| 10570 | // functions which might contain the requested source position. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10571 | CompileLazyShared(target, KEEP_EXCEPTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10572 | } |
| 10573 | } |
| 10574 | |
| 10575 | return *target; |
| 10576 | } |
| 10577 | |
| 10578 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10579 | // Changes the state of a break point in a script and returns source position |
| 10580 | // where break point was set. NOTE: Regarding performance see the NOTE for |
| 10581 | // GetScriptFromScriptData. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10582 | // args[0]: script to set break point in |
| 10583 | // args[1]: number: break source position (within the script source) |
| 10584 | // args[2]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10585 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetScriptBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10586 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10587 | ASSERT(args.length() == 3); |
| 10588 | CONVERT_ARG_CHECKED(JSValue, wrapper, 0); |
| 10589 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 10590 | RUNTIME_ASSERT(source_position >= 0); |
| 10591 | Handle<Object> break_point_object_arg = args.at<Object>(2); |
| 10592 | |
| 10593 | // Get the script from the script wrapper. |
| 10594 | RUNTIME_ASSERT(wrapper->value()->IsScript()); |
| 10595 | Handle<Script> script(Script::cast(wrapper->value())); |
| 10596 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 10597 | Object* result = Runtime::FindSharedFunctionInfoInScript( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10598 | isolate, script, source_position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10599 | if (!result->IsUndefined()) { |
| 10600 | Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result)); |
| 10601 | // Find position within function. The script position might be before the |
| 10602 | // source position of the first function. |
| 10603 | int position; |
| 10604 | if (shared->start_position() > source_position) { |
| 10605 | position = 0; |
| 10606 | } else { |
| 10607 | position = source_position - shared->start_position(); |
| 10608 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10609 | isolate->debug()->SetBreakPoint(shared, break_point_object_arg, &position); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 10610 | position += shared->start_position(); |
| 10611 | return Smi::FromInt(position); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10612 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10613 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10614 | } |
| 10615 | |
| 10616 | |
| 10617 | // Clear a break point |
| 10618 | // args[0]: number: break point object |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10619 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearBreakPoint) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10620 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10621 | ASSERT(args.length() == 1); |
| 10622 | Handle<Object> break_point_object_arg = args.at<Object>(0); |
| 10623 | |
| 10624 | // Clear break point. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10625 | isolate->debug()->ClearBreakPoint(break_point_object_arg); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10626 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10627 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10628 | } |
| 10629 | |
| 10630 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10631 | // Change the state of break on exceptions. |
| 10632 | // args[0]: Enum value indicating whether to affect caught/uncaught exceptions. |
| 10633 | // args[1]: Boolean indicating on/off. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10634 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ChangeBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10635 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10636 | ASSERT(args.length() == 2); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10637 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 10638 | CONVERT_BOOLEAN_CHECKED(enable, args[1]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10639 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10640 | // If the number doesn't match an enum value, the ChangeBreakOnException |
| 10641 | // function will default to affecting caught exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10642 | ExceptionBreakType type = |
| 10643 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10644 | // Update break point state. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10645 | isolate->debug()->ChangeBreakOnException(type, enable); |
| 10646 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10647 | } |
| 10648 | |
| 10649 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10650 | // Returns the state of break on exceptions |
| 10651 | // args[0]: boolean indicating uncaught exceptions |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10652 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IsBreakOnException) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10653 | HandleScope scope(isolate); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10654 | ASSERT(args.length() == 1); |
| 10655 | RUNTIME_ASSERT(args[0]->IsNumber()); |
| 10656 | |
| 10657 | ExceptionBreakType type = |
| 10658 | static_cast<ExceptionBreakType>(NumberToUint32(args[0])); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10659 | bool result = isolate->debug()->IsBreakOnException(type); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 10660 | return Smi::FromInt(result); |
| 10661 | } |
| 10662 | |
| 10663 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10664 | // Prepare for stepping |
| 10665 | // args[0]: break id for checking execution state |
| 10666 | // args[1]: step action from the enumeration StepAction |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10667 | // args[2]: number of times to perform the step, for step out it is the number |
| 10668 | // of frames to step down. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10669 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrepareStep) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10670 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10671 | ASSERT(args.length() == 3); |
| 10672 | // Check arguments. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10673 | Object* check; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10674 | { MaybeObject* maybe_check = Runtime_CheckExecutionState( |
| 10675 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10676 | if (!maybe_check->ToObject(&check)) return maybe_check; |
| 10677 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10678 | if (!args[1]->IsNumber() || !args[2]->IsNumber()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10679 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10680 | } |
| 10681 | |
| 10682 | // Get the step action and check validity. |
| 10683 | StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1])); |
| 10684 | if (step_action != StepIn && |
| 10685 | step_action != StepNext && |
| 10686 | step_action != StepOut && |
| 10687 | step_action != StepInMin && |
| 10688 | step_action != StepMin) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10689 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10690 | } |
| 10691 | |
| 10692 | // Get the number of steps. |
| 10693 | int step_count = NumberToInt32(args[2]); |
| 10694 | if (step_count < 1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10695 | return isolate->Throw(isolate->heap()->illegal_argument_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10696 | } |
| 10697 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10698 | // Clear all current stepping setup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10699 | isolate->debug()->ClearStepping(); |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 10700 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10701 | // Prepare step. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10702 | isolate->debug()->PrepareStep(static_cast<StepAction>(step_action), |
| 10703 | step_count); |
| 10704 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10705 | } |
| 10706 | |
| 10707 | |
| 10708 | // Clear all stepping set by PrepareStep. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10709 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ClearStepping) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10710 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10711 | ASSERT(args.length() == 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10712 | isolate->debug()->ClearStepping(); |
| 10713 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10714 | } |
| 10715 | |
| 10716 | |
| 10717 | // Creates a copy of the with context chain. The copy of the context chain is |
| 10718 | // is linked to the function context supplied. |
| 10719 | static Handle<Context> CopyWithContextChain(Handle<Context> context_chain, |
| 10720 | Handle<Context> function_context) { |
| 10721 | // At the bottom of the chain. Return the function context to link to. |
| 10722 | if (context_chain->is_function_context()) { |
| 10723 | return function_context; |
| 10724 | } |
| 10725 | |
| 10726 | // Recursively copy the with contexts. |
| 10727 | Handle<Context> previous(context_chain->previous()); |
| 10728 | Handle<JSObject> extension(JSObject::cast(context_chain->extension())); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 10729 | Handle<Context> context = CopyWithContextChain(previous, function_context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10730 | return context->GetIsolate()->factory()->NewWithContext( |
| 10731 | context, extension, context_chain->IsCatchContext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10732 | } |
| 10733 | |
| 10734 | |
| 10735 | // Helper function to find or create the arguments object for |
| 10736 | // Runtime_DebugEvaluate. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10737 | static Handle<Object> GetArgumentsObject(Isolate* isolate, |
| 10738 | JavaScriptFrame* frame, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10739 | Handle<JSFunction> function, |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10740 | Handle<SerializedScopeInfo> scope_info, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10741 | const ScopeInfo<>* sinfo, |
| 10742 | Handle<Context> function_context) { |
| 10743 | // Try to find the value of 'arguments' to pass as parameter. If it is not |
| 10744 | // found (that is the debugged function does not reference 'arguments' and |
| 10745 | // does not support eval) then create an 'arguments' object. |
| 10746 | int index; |
| 10747 | if (sinfo->number_of_stack_slots() > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10748 | index = scope_info->StackSlotIndex(isolate->heap()->arguments_symbol()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10749 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10750 | return Handle<Object>(frame->GetExpression(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10751 | } |
| 10752 | } |
| 10753 | |
| 10754 | if (sinfo->number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10755 | index = scope_info->ContextSlotIndex(isolate->heap()->arguments_symbol(), |
| 10756 | NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10757 | if (index != -1) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10758 | return Handle<Object>(function_context->get(index), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10759 | } |
| 10760 | } |
| 10761 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 10762 | const int length = frame->ComputeParametersCount(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10763 | Handle<JSObject> arguments = |
| 10764 | isolate->factory()->NewArgumentsObject(function, length); |
| 10765 | Handle<FixedArray> array = isolate->factory()->NewFixedArray(length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 10766 | |
| 10767 | AssertNoAllocation no_gc; |
| 10768 | WriteBarrierMode mode = array->GetWriteBarrierMode(no_gc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10769 | for (int i = 0; i < length; i++) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 10770 | array->set(i, frame->GetParameter(i), mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10771 | } |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 10772 | arguments->set_elements(*array); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10773 | return arguments; |
| 10774 | } |
| 10775 | |
| 10776 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10777 | static const char kSourceStr[] = |
| 10778 | "(function(arguments,__source__){return eval(__source__);})"; |
| 10779 | |
| 10780 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10781 | // 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] | 10782 | // 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] | 10783 | // extension part has all the parameters and locals of the function on the |
| 10784 | // stack frame. A function which calls eval with the code to evaluate is then |
| 10785 | // compiled in this context and called in this context. As this context |
| 10786 | // replaces the context of the function on the stack frame a new (empty) |
| 10787 | // function is created as well to be used as the closure for the context. |
| 10788 | // This function and the context acts as replacements for the function on the |
| 10789 | // stack frame presenting the same view of the values of parameters and |
| 10790 | // local variables as if the piece of JavaScript was evaluated at the point |
| 10791 | // where the function on the stack frame is currently stopped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10792 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluate) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10793 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10794 | |
| 10795 | // Check the execution state and decode arguments frame and source to be |
| 10796 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10797 | ASSERT(args.length() == 5); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10798 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10799 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 10800 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10801 | if (!maybe_check_result->ToObject(&check_result)) { |
| 10802 | return maybe_check_result; |
| 10803 | } |
| 10804 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10805 | CONVERT_CHECKED(Smi, wrapped_id, args[1]); |
| 10806 | CONVERT_ARG_CHECKED(String, source, 2); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10807 | CONVERT_BOOLEAN_CHECKED(disable_break, args[3]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10808 | Handle<Object> additional_context(args[4]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10809 | |
| 10810 | // Handle the processing of break. |
| 10811 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10812 | |
| 10813 | // Get the frame where the debugging is performed. |
| 10814 | StackFrame::Id id = UnwrapFrameId(wrapped_id); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 10815 | JavaScriptFrameIterator it(isolate, id); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10816 | JavaScriptFrame* frame = it.frame(); |
| 10817 | Handle<JSFunction> function(JSFunction::cast(frame->function())); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 10818 | Handle<SerializedScopeInfo> scope_info(function->shared()->scope_info()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10819 | ScopeInfo<> sinfo(*scope_info); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10820 | |
| 10821 | // Traverse the saved contexts chain to find the active context for the |
| 10822 | // selected frame. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10823 | SaveContext* save = isolate->save_context(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10824 | while (save != NULL && !save->below(frame)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10825 | save = save->prev(); |
| 10826 | } |
| 10827 | ASSERT(save != NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10828 | SaveContext savex(isolate); |
| 10829 | isolate->set_context(*(save->context())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10830 | |
| 10831 | // Create the (empty) function replacing the function on the stack frame for |
| 10832 | // the purpose of evaluating in the context created below. It is important |
| 10833 | // that this function does not describe any parameters and local variables |
| 10834 | // in the context. If it does then this will cause problems with the lookup |
| 10835 | // in Context::Lookup, where context slots for parameters and local variables |
| 10836 | // are looked at before the extension object. |
| 10837 | Handle<JSFunction> go_between = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10838 | isolate->factory()->NewFunction(isolate->factory()->empty_string(), |
| 10839 | isolate->factory()->undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10840 | go_between->set_context(function->context()); |
| 10841 | #ifdef DEBUG |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10842 | ScopeInfo<> go_between_sinfo(go_between->shared()->scope_info()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10843 | ASSERT(go_between_sinfo.number_of_parameters() == 0); |
| 10844 | ASSERT(go_between_sinfo.number_of_context_slots() == 0); |
| 10845 | #endif |
| 10846 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10847 | // Materialize the content of the local scope into a JSObject. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10848 | Handle<JSObject> local_scope = MaterializeLocalScope(isolate, frame); |
| 10849 | RETURN_IF_EMPTY_HANDLE(isolate, local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10850 | |
| 10851 | // Allocate a new context for the debug evaluation and set the extension |
| 10852 | // object build. |
| 10853 | Handle<Context> context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10854 | isolate->factory()->NewFunctionContext(Context::MIN_CONTEXT_SLOTS, |
| 10855 | go_between); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10856 | context->set_extension(*local_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10857 | // 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] | 10858 | Handle<Context> frame_context(Context::cast(frame->context())); |
| 10859 | Handle<Context> function_context(frame_context->fcontext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10860 | context = CopyWithContextChain(frame_context, context); |
| 10861 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10862 | if (additional_context->IsJSObject()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10863 | context = isolate->factory()->NewWithContext(context, |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10864 | Handle<JSObject>::cast(additional_context), false); |
| 10865 | } |
| 10866 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10867 | // Wrap the evaluation statement in a new function compiled in the newly |
| 10868 | // created context. The function has one parameter which has to be called |
| 10869 | // '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] | 10870 | // the function being debugged. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10871 | // function(arguments,__source__) {return eval(__source__);} |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10872 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10873 | Handle<String> function_source = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10874 | isolate->factory()->NewStringFromAscii( |
| 10875 | Vector<const char>(kSourceStr, sizeof(kSourceStr) - 1)); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10876 | |
| 10877 | // Currently, the eval code will be executed in non-strict mode, |
| 10878 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10879 | Handle<SharedFunctionInfo> shared = |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 10880 | Compiler::CompileEval(function_source, |
| 10881 | context, |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10882 | context->IsGlobalContext(), |
| 10883 | kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10884 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10885 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10886 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10887 | |
| 10888 | // Invoke the result of the compilation to get the evaluation function. |
| 10889 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10890 | Handle<Object> receiver(frame->receiver(), isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10891 | Handle<Object> evaluation_function = |
| 10892 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 10893 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10894 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10895 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10896 | Handle<Object> arguments = GetArgumentsObject(isolate, frame, |
| 10897 | function, scope_info, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 10898 | &sinfo, function_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10899 | |
| 10900 | // Invoke the evaluation function and return the result. |
| 10901 | const int argc = 2; |
| 10902 | Object** argv[argc] = { arguments.location(), |
| 10903 | Handle<Object>::cast(source).location() }; |
| 10904 | Handle<Object> result = |
| 10905 | Execution::Call(Handle<JSFunction>::cast(evaluation_function), receiver, |
| 10906 | argc, argv, &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10907 | if (has_pending_exception) return Failure::Exception(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10908 | |
| 10909 | // Skip the global proxy as it has no properties and always delegates to the |
| 10910 | // real global object. |
| 10911 | if (result->IsJSGlobalProxy()) { |
| 10912 | result = Handle<JSObject>(JSObject::cast(result->GetPrototype())); |
| 10913 | } |
| 10914 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10915 | return *result; |
| 10916 | } |
| 10917 | |
| 10918 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10919 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugEvaluateGlobal) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10920 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10921 | |
| 10922 | // Check the execution state and decode arguments frame and source to be |
| 10923 | // evaluated. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10924 | ASSERT(args.length() == 4); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10925 | Object* check_result; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10926 | { MaybeObject* maybe_check_result = Runtime_CheckExecutionState( |
| 10927 | RUNTIME_ARGUMENTS(isolate, args)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 10928 | if (!maybe_check_result->ToObject(&check_result)) { |
| 10929 | return maybe_check_result; |
| 10930 | } |
| 10931 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10932 | CONVERT_ARG_CHECKED(String, source, 1); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10933 | CONVERT_BOOLEAN_CHECKED(disable_break, args[2]); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10934 | Handle<Object> additional_context(args[3]); |
kasper.lund | bd3ec4e | 2008-07-09 11:06:54 +0000 | [diff] [blame] | 10935 | |
| 10936 | // Handle the processing of break. |
| 10937 | DisableBreak disable_break_save(disable_break); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10938 | |
| 10939 | // Enter the top context from before the debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10940 | SaveContext save(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10941 | SaveContext* top = &save; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10942 | while (top != NULL && *top->context() == *isolate->debug()->debug_context()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10943 | top = top->prev(); |
| 10944 | } |
| 10945 | if (top != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10946 | isolate->set_context(*top->context()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10947 | } |
| 10948 | |
| 10949 | // Get the global context now set to the top context from before the |
| 10950 | // debugger was invoked. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10951 | Handle<Context> context = isolate->global_context(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10952 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10953 | bool is_global = true; |
| 10954 | |
| 10955 | if (additional_context->IsJSObject()) { |
| 10956 | // 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] | 10957 | Handle<JSFunction> go_between = isolate->factory()->NewFunction( |
| 10958 | isolate->factory()->empty_string(), |
| 10959 | isolate->factory()->undefined_value()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10960 | go_between->set_context(*context); |
| 10961 | context = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10962 | isolate->factory()->NewFunctionContext( |
| 10963 | Context::MIN_CONTEXT_SLOTS, go_between); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 10964 | context->set_extension(JSObject::cast(*additional_context)); |
| 10965 | is_global = false; |
| 10966 | } |
| 10967 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10968 | // Compile the source to be evaluated. |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10969 | // Currently, the eval code will be executed in non-strict mode, |
| 10970 | // even in the strict code context. |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10971 | Handle<SharedFunctionInfo> shared = |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 10972 | Compiler::CompileEval(source, context, is_global, kNonStrictMode); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 10973 | if (shared.is_null()) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10974 | Handle<JSFunction> compiled_function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10975 | Handle<JSFunction>( |
| 10976 | isolate->factory()->NewFunctionFromSharedFunctionInfo(shared, |
| 10977 | context)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10978 | |
| 10979 | // Invoke the result of the compilation to get the evaluation function. |
| 10980 | bool has_pending_exception; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10981 | Handle<Object> receiver = isolate->global(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10982 | Handle<Object> result = |
| 10983 | Execution::Call(compiled_function, receiver, 0, NULL, |
| 10984 | &has_pending_exception); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10985 | if (has_pending_exception) return Failure::Exception(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10986 | return *result; |
| 10987 | } |
| 10988 | |
| 10989 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 10990 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetLoadedScripts) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10991 | HandleScope scope(isolate); |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 10992 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10993 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10994 | // Fill the script objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 10995 | Handle<FixedArray> instances = isolate->debug()->GetLoadedScripts(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 10996 | |
| 10997 | // Convert the script objects to proper JS objects. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 10998 | for (int i = 0; i < instances->length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 10999 | Handle<Script> script = Handle<Script>(Script::cast(instances->get(i))); |
| 11000 | // Get the script wrapper in a local handle before calling GetScriptWrapper, |
| 11001 | // because using |
| 11002 | // instances->set(i, *GetScriptWrapper(script)) |
| 11003 | // is unsafe as GetScriptWrapper might call GC and the C++ compiler might |
| 11004 | // already have deferenced the instances handle. |
| 11005 | Handle<JSValue> wrapper = GetScriptWrapper(script); |
| 11006 | instances->set(i, *wrapper); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11007 | } |
| 11008 | |
| 11009 | // Return result as a JS array. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11010 | Handle<JSObject> result = |
| 11011 | isolate->factory()->NewJSObject(isolate->array_function()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11012 | Handle<JSArray>::cast(result)->SetContent(*instances); |
| 11013 | return *result; |
| 11014 | } |
| 11015 | |
| 11016 | |
| 11017 | // Helper function used by Runtime_DebugReferencedBy below. |
| 11018 | static int DebugReferencedBy(JSObject* target, |
| 11019 | Object* instance_filter, int max_references, |
| 11020 | FixedArray* instances, int instances_size, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11021 | JSFunction* arguments_function) { |
| 11022 | NoHandleAllocation ha; |
| 11023 | AssertNoAllocation no_alloc; |
| 11024 | |
| 11025 | // Iterate the heap. |
| 11026 | int count = 0; |
| 11027 | JSObject* last = NULL; |
| 11028 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11029 | HeapObject* heap_obj = NULL; |
| 11030 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11031 | (max_references == 0 || count < max_references)) { |
| 11032 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11033 | if (heap_obj->IsJSObject()) { |
| 11034 | // Skip context extension objects and argument arrays as these are |
| 11035 | // checked in the context of functions using them. |
| 11036 | JSObject* obj = JSObject::cast(heap_obj); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11037 | if (obj->IsJSContextExtensionObject() || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11038 | obj->map()->constructor() == arguments_function) { |
| 11039 | continue; |
| 11040 | } |
| 11041 | |
| 11042 | // Check if the JS object has a reference to the object looked for. |
| 11043 | if (obj->ReferencesObject(target)) { |
| 11044 | // Check instance filter if supplied. This is normally used to avoid |
| 11045 | // references from mirror objects (see Runtime_IsInPrototypeChain). |
| 11046 | if (!instance_filter->IsUndefined()) { |
| 11047 | Object* V = obj; |
| 11048 | while (true) { |
| 11049 | Object* prototype = V->GetPrototype(); |
| 11050 | if (prototype->IsNull()) { |
| 11051 | break; |
| 11052 | } |
| 11053 | if (instance_filter == prototype) { |
| 11054 | obj = NULL; // Don't add this object. |
| 11055 | break; |
| 11056 | } |
| 11057 | V = prototype; |
| 11058 | } |
| 11059 | } |
| 11060 | |
| 11061 | if (obj != NULL) { |
| 11062 | // Valid reference found add to instance array if supplied an update |
| 11063 | // count. |
| 11064 | if (instances != NULL && count < instances_size) { |
| 11065 | instances->set(count, obj); |
| 11066 | } |
| 11067 | last = obj; |
| 11068 | count++; |
| 11069 | } |
| 11070 | } |
| 11071 | } |
| 11072 | } |
| 11073 | |
| 11074 | // Check for circular reference only. This can happen when the object is only |
| 11075 | // referenced from mirrors and has a circular reference in which case the |
| 11076 | // object is not really alive and would have been garbage collected if not |
| 11077 | // referenced from the mirror. |
| 11078 | if (count == 1 && last == target) { |
| 11079 | count = 0; |
| 11080 | } |
| 11081 | |
| 11082 | // Return the number of referencing objects found. |
| 11083 | return count; |
| 11084 | } |
| 11085 | |
| 11086 | |
| 11087 | // Scan the heap for objects with direct references to an object |
| 11088 | // args[0]: the object to find references to |
| 11089 | // args[1]: constructor function for instances to exclude (Mirror) |
| 11090 | // args[2]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11091 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugReferencedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11092 | ASSERT(args.length() == 3); |
| 11093 | |
| 11094 | // 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] | 11095 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11096 | |
| 11097 | // Check parameters. |
| 11098 | CONVERT_CHECKED(JSObject, target, args[0]); |
| 11099 | Object* instance_filter = args[1]; |
| 11100 | RUNTIME_ASSERT(instance_filter->IsUndefined() || |
| 11101 | instance_filter->IsJSObject()); |
| 11102 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]); |
| 11103 | RUNTIME_ASSERT(max_references >= 0); |
| 11104 | |
| 11105 | // Get the constructor function for context extension and arguments array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11106 | JSObject* arguments_boilerplate = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11107 | isolate->context()->global_context()->arguments_boilerplate(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11108 | JSFunction* arguments_function = |
| 11109 | JSFunction::cast(arguments_boilerplate->map()->constructor()); |
| 11110 | |
| 11111 | // Get the number of referencing objects. |
| 11112 | int count; |
| 11113 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11114 | NULL, 0, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11115 | |
| 11116 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11117 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11118 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11119 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 11120 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11121 | FixedArray* instances = FixedArray::cast(object); |
| 11122 | |
| 11123 | // Fill the referencing objects. |
| 11124 | count = DebugReferencedBy(target, instance_filter, max_references, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 11125 | instances, count, arguments_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11126 | |
| 11127 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11128 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11129 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 11130 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11131 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11132 | } |
| 11133 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11134 | return result; |
| 11135 | } |
| 11136 | |
| 11137 | |
| 11138 | // Helper function used by Runtime_DebugConstructedBy below. |
| 11139 | static int DebugConstructedBy(JSFunction* constructor, int max_references, |
| 11140 | FixedArray* instances, int instances_size) { |
| 11141 | AssertNoAllocation no_alloc; |
| 11142 | |
| 11143 | // Iterate the heap. |
| 11144 | int count = 0; |
| 11145 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11146 | HeapObject* heap_obj = NULL; |
| 11147 | while (((heap_obj = iterator.next()) != NULL) && |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11148 | (max_references == 0 || count < max_references)) { |
| 11149 | // Only look at all JSObjects. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11150 | if (heap_obj->IsJSObject()) { |
| 11151 | JSObject* obj = JSObject::cast(heap_obj); |
| 11152 | if (obj->map()->constructor() == constructor) { |
| 11153 | // Valid reference found add to instance array if supplied an update |
| 11154 | // count. |
| 11155 | if (instances != NULL && count < instances_size) { |
| 11156 | instances->set(count, obj); |
| 11157 | } |
| 11158 | count++; |
| 11159 | } |
| 11160 | } |
| 11161 | } |
| 11162 | |
| 11163 | // Return the number of referencing objects found. |
| 11164 | return count; |
| 11165 | } |
| 11166 | |
| 11167 | |
| 11168 | // Scan the heap for objects constructed by a specific function. |
| 11169 | // args[0]: the constructor to find instances of |
| 11170 | // args[1]: the the maximum number of objects to return |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11171 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugConstructedBy) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11172 | ASSERT(args.length() == 2); |
| 11173 | |
| 11174 | // First perform a full GC in order to avoid dead objects. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11175 | isolate->heap()->CollectAllGarbage(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11176 | |
| 11177 | // Check parameters. |
| 11178 | CONVERT_CHECKED(JSFunction, constructor, args[0]); |
| 11179 | CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]); |
| 11180 | RUNTIME_ASSERT(max_references >= 0); |
| 11181 | |
| 11182 | // Get the number of referencing objects. |
| 11183 | int count; |
| 11184 | count = DebugConstructedBy(constructor, max_references, NULL, 0); |
| 11185 | |
| 11186 | // Allocate an array to hold the result. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11187 | Object* object; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11188 | { MaybeObject* maybe_object = isolate->heap()->AllocateFixedArray(count); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11189 | if (!maybe_object->ToObject(&object)) return maybe_object; |
| 11190 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11191 | FixedArray* instances = FixedArray::cast(object); |
| 11192 | |
| 11193 | // Fill the referencing objects. |
| 11194 | count = DebugConstructedBy(constructor, max_references, instances, count); |
| 11195 | |
| 11196 | // Return result as JS array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11197 | Object* result; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11198 | { MaybeObject* maybe_result = isolate->heap()->AllocateJSObject( |
| 11199 | isolate->context()->global_context()->array_function()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 11200 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 11201 | } |
| 11202 | JSArray::cast(result)->SetContent(instances); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11203 | return result; |
| 11204 | } |
| 11205 | |
| 11206 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 11207 | // Find the effective prototype object as returned by __proto__. |
| 11208 | // args[0]: the object to find the prototype for. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11209 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugGetPrototype) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11210 | ASSERT(args.length() == 1); |
| 11211 | |
| 11212 | CONVERT_CHECKED(JSObject, obj, args[0]); |
| 11213 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 11214 | // Use the __proto__ accessor. |
| 11215 | return Accessors::ObjectPrototype.getter(obj, NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11216 | } |
| 11217 | |
| 11218 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11219 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SystemBreak) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 11220 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11221 | CPU::DebugBreak(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11222 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11223 | } |
| 11224 | |
| 11225 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11226 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleFunction) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11227 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11228 | HandleScope scope(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11229 | ASSERT(args.length() == 1); |
| 11230 | // Get the function and make sure it is compiled. |
| 11231 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11232 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 11233 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11234 | return Failure::Exception(); |
| 11235 | } |
| 11236 | func->code()->PrintLn(); |
| 11237 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11238 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11239 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11240 | |
| 11241 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11242 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DebugDisassembleConstructor) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11243 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11244 | HandleScope scope(isolate); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11245 | ASSERT(args.length() == 1); |
| 11246 | // Get the function and make sure it is compiled. |
| 11247 | CONVERT_ARG_CHECKED(JSFunction, func, 0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11248 | Handle<SharedFunctionInfo> shared(func->shared()); |
| 11249 | if (!EnsureCompiled(shared, KEEP_EXCEPTION)) { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11250 | return Failure::Exception(); |
| 11251 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11252 | shared->construct_stub()->PrintLn(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11253 | #endif // DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11254 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 11255 | } |
| 11256 | |
| 11257 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11258 | RUNTIME_FUNCTION(MaybeObject*, Runtime_FunctionGetInferredName) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 11259 | NoHandleAllocation ha; |
| 11260 | ASSERT(args.length() == 1); |
| 11261 | |
| 11262 | CONVERT_CHECKED(JSFunction, f, args[0]); |
| 11263 | return f->shared()->inferred_name(); |
| 11264 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11265 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11266 | |
| 11267 | static int FindSharedFunctionInfosForScript(Script* script, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11268 | FixedArray* buffer) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11269 | AssertNoAllocation no_allocations; |
| 11270 | |
| 11271 | int counter = 0; |
| 11272 | int buffer_size = buffer->length(); |
| 11273 | HeapIterator iterator; |
| 11274 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 11275 | ASSERT(obj != NULL); |
| 11276 | if (!obj->IsSharedFunctionInfo()) { |
| 11277 | continue; |
| 11278 | } |
| 11279 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj); |
| 11280 | if (shared->script() != script) { |
| 11281 | continue; |
| 11282 | } |
| 11283 | if (counter < buffer_size) { |
| 11284 | buffer->set(counter, shared); |
| 11285 | } |
| 11286 | counter++; |
| 11287 | } |
| 11288 | return counter; |
| 11289 | } |
| 11290 | |
| 11291 | // For a script finds all SharedFunctionInfo's in the heap that points |
| 11292 | // to this script. Returns JSArray of SharedFunctionInfo wrapped |
| 11293 | // in OpaqueReferences. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11294 | RUNTIME_FUNCTION(MaybeObject*, |
| 11295 | Runtime_LiveEditFindSharedFunctionInfosForScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11296 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11297 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11298 | CONVERT_CHECKED(JSValue, script_value, args[0]); |
| 11299 | |
| 11300 | Handle<Script> script = Handle<Script>(Script::cast(script_value->value())); |
| 11301 | |
| 11302 | const int kBufferSize = 32; |
| 11303 | |
| 11304 | Handle<FixedArray> array; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11305 | array = isolate->factory()->NewFixedArray(kBufferSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11306 | int number = FindSharedFunctionInfosForScript(*script, *array); |
| 11307 | if (number > kBufferSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11308 | array = isolate->factory()->NewFixedArray(number); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11309 | FindSharedFunctionInfosForScript(*script, *array); |
| 11310 | } |
| 11311 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11312 | Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11313 | result->set_length(Smi::FromInt(number)); |
| 11314 | |
| 11315 | LiveEdit::WrapSharedFunctionInfos(result); |
| 11316 | |
| 11317 | return *result; |
| 11318 | } |
| 11319 | |
| 11320 | // For a script calculates compilation information about all its functions. |
| 11321 | // The script source is explicitly specified by the second argument. |
| 11322 | // The source of the actual script is not used, however it is important that |
| 11323 | // all generated code keeps references to this particular instance of script. |
| 11324 | // Returns a JSArray of compilation infos. The array is ordered so that |
| 11325 | // each function with all its descendant is always stored in a continues range |
| 11326 | // 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] | 11327 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditGatherCompileInfo) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11328 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11329 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11330 | CONVERT_CHECKED(JSValue, script, args[0]); |
| 11331 | CONVERT_ARG_CHECKED(String, source, 1); |
| 11332 | Handle<Script> script_handle = Handle<Script>(Script::cast(script->value())); |
| 11333 | |
| 11334 | JSArray* result = LiveEdit::GatherCompileInfo(script_handle, source); |
| 11335 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11336 | if (isolate->has_pending_exception()) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11337 | return Failure::Exception(); |
| 11338 | } |
| 11339 | |
| 11340 | return result; |
| 11341 | } |
| 11342 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11343 | // Changes the source of the script to a new_source. |
| 11344 | // If old_script_name is provided (i.e. is a String), also creates a copy of |
| 11345 | // the script with its original source and sends notification to debugger. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11346 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11347 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11348 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11349 | CONVERT_CHECKED(JSValue, original_script_value, args[0]); |
| 11350 | CONVERT_ARG_CHECKED(String, new_source, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11351 | Handle<Object> old_script_name(args[2], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11352 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11353 | CONVERT_CHECKED(Script, original_script_pointer, |
| 11354 | original_script_value->value()); |
| 11355 | Handle<Script> original_script(original_script_pointer); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11356 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11357 | Object* old_script = LiveEdit::ChangeScriptSource(original_script, |
| 11358 | new_source, |
| 11359 | old_script_name); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11360 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11361 | if (old_script->IsScript()) { |
| 11362 | Handle<Script> script_handle(Script::cast(old_script)); |
| 11363 | return *(GetScriptWrapper(script_handle)); |
| 11364 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11365 | return isolate->heap()->null_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11366 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11367 | } |
| 11368 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11369 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11370 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSourceUpdated) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11371 | ASSERT(args.length() == 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11372 | HandleScope scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11373 | CONVERT_ARG_CHECKED(JSArray, shared_info, 0); |
| 11374 | return LiveEdit::FunctionSourceUpdated(shared_info); |
| 11375 | } |
| 11376 | |
| 11377 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11378 | // Replaces code of SharedFunctionInfo with a new one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11379 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceFunctionCode) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11380 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11381 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11382 | CONVERT_ARG_CHECKED(JSArray, new_compile_info, 0); |
| 11383 | CONVERT_ARG_CHECKED(JSArray, shared_info, 1); |
| 11384 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11385 | return LiveEdit::ReplaceFunctionCode(new_compile_info, shared_info); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11386 | } |
| 11387 | |
| 11388 | // Connects SharedFunctionInfo to another script. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11389 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditFunctionSetScript) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11390 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11391 | HandleScope scope(isolate); |
| 11392 | Handle<Object> function_object(args[0], isolate); |
| 11393 | Handle<Object> script_object(args[1], isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11394 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11395 | if (function_object->IsJSValue()) { |
| 11396 | Handle<JSValue> function_wrapper = Handle<JSValue>::cast(function_object); |
| 11397 | if (script_object->IsJSValue()) { |
| 11398 | CONVERT_CHECKED(Script, script, JSValue::cast(*script_object)->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11399 | script_object = Handle<Object>(script, isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11400 | } |
| 11401 | |
| 11402 | LiveEdit::SetFunctionScript(function_wrapper, script_object); |
| 11403 | } else { |
| 11404 | // Just ignore this. We may not have a SharedFunctionInfo for some functions |
| 11405 | // and we check it in this function. |
| 11406 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11407 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11408 | return isolate->heap()->undefined_value(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11409 | } |
| 11410 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11411 | |
| 11412 | // In a code of a parent function replaces original function as embedded object |
| 11413 | // with a substitution one. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11414 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditReplaceRefToNestedFunction) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11415 | ASSERT(args.length() == 3); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11416 | HandleScope scope(isolate); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11417 | |
| 11418 | CONVERT_ARG_CHECKED(JSValue, parent_wrapper, 0); |
| 11419 | CONVERT_ARG_CHECKED(JSValue, orig_wrapper, 1); |
| 11420 | CONVERT_ARG_CHECKED(JSValue, subst_wrapper, 2); |
| 11421 | |
| 11422 | LiveEdit::ReplaceRefToNestedFunction(parent_wrapper, orig_wrapper, |
| 11423 | subst_wrapper); |
| 11424 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11425 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 11426 | } |
| 11427 | |
| 11428 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11429 | // Updates positions of a shared function info (first parameter) according |
| 11430 | // to script source change. Text change is described in second parameter as |
| 11431 | // array of groups of 3 numbers: |
| 11432 | // (change_begin, change_end, change_end_new_position). |
| 11433 | // 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] | 11434 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditPatchFunctionPositions) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11435 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11436 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11437 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
| 11438 | CONVERT_ARG_CHECKED(JSArray, position_change_array, 1); |
| 11439 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 11440 | return LiveEdit::PatchFunctionPositions(shared_array, position_change_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11441 | } |
| 11442 | |
| 11443 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11444 | // For array of SharedFunctionInfo's (each wrapped in JSValue) |
| 11445 | // checks that none of them have activations on stacks (of any thread). |
| 11446 | // Returns array of the same length with corresponding results of |
| 11447 | // LiveEdit::FunctionPatchabilityStatus type. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11448 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCheckAndDropActivations) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11449 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11450 | HandleScope scope(isolate); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11451 | CONVERT_ARG_CHECKED(JSArray, shared_array, 0); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11452 | CONVERT_BOOLEAN_CHECKED(do_drop, args[1]); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11453 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11454 | return *LiveEdit::CheckAndDropActivations(shared_array, do_drop); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 11455 | } |
| 11456 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11457 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 11458 | // of JSArray of triplets (pos1, pos1_end, pos2_end) describing list |
| 11459 | // of diff chunks. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11460 | RUNTIME_FUNCTION(MaybeObject*, Runtime_LiveEditCompareStrings) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11461 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11462 | HandleScope scope(isolate); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11463 | CONVERT_ARG_CHECKED(String, s1, 0); |
| 11464 | CONVERT_ARG_CHECKED(String, s2, 1); |
| 11465 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 11466 | return *LiveEdit::CompareStrings(s1, s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11467 | } |
| 11468 | |
| 11469 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11470 | // A testing entry. Returns statement position which is the closest to |
| 11471 | // source_position. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11472 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFunctionCodePositionFromSource) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11473 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11474 | HandleScope scope(isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11475 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 11476 | CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]); |
| 11477 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11478 | Handle<Code> code(function->code(), isolate); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11479 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11480 | if (code->kind() != Code::FUNCTION && |
| 11481 | code->kind() != Code::OPTIMIZED_FUNCTION) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11482 | return isolate->heap()->undefined_value(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11483 | } |
| 11484 | |
| 11485 | RelocIterator it(*code, RelocInfo::ModeMask(RelocInfo::STATEMENT_POSITION)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11486 | int closest_pc = 0; |
| 11487 | int distance = kMaxInt; |
| 11488 | while (!it.done()) { |
| 11489 | int statement_position = static_cast<int>(it.rinfo()->data()); |
| 11490 | // Check if this break point is closer that what was previously found. |
| 11491 | if (source_position <= statement_position && |
| 11492 | statement_position - source_position < distance) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 11493 | closest_pc = |
| 11494 | static_cast<int>(it.rinfo()->pc() - code->instruction_start()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 11495 | distance = statement_position - source_position; |
| 11496 | // Check whether we can't get any closer. |
| 11497 | if (distance == 0) break; |
| 11498 | } |
| 11499 | it.next(); |
| 11500 | } |
| 11501 | |
| 11502 | return Smi::FromInt(closest_pc); |
| 11503 | } |
| 11504 | |
| 11505 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11506 | // Calls specified function with or without entering the debugger. |
| 11507 | // This is used in unit tests to run code as if debugger is entered or simply |
| 11508 | // to have a stack with C++ frame in the middle. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11509 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ExecuteInDebugContext) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11510 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11511 | HandleScope scope(isolate); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11512 | CONVERT_ARG_CHECKED(JSFunction, function, 0); |
| 11513 | CONVERT_BOOLEAN_CHECKED(without_debugger, args[1]); |
| 11514 | |
| 11515 | Handle<Object> result; |
| 11516 | bool pending_exception; |
| 11517 | { |
| 11518 | if (without_debugger) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11519 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11520 | &pending_exception); |
| 11521 | } else { |
| 11522 | EnterDebugger enter_debugger; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11523 | result = Execution::Call(function, isolate->global(), 0, NULL, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 11524 | &pending_exception); |
| 11525 | } |
| 11526 | } |
| 11527 | if (!pending_exception) { |
| 11528 | return *result; |
| 11529 | } else { |
| 11530 | return Failure::Exception(); |
| 11531 | } |
| 11532 | } |
| 11533 | |
| 11534 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11535 | // Sets a v8 flag. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11536 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SetFlags) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11537 | CONVERT_CHECKED(String, arg, args[0]); |
| 11538 | SmartPointer<char> flags = |
| 11539 | arg->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL); |
| 11540 | FlagList::SetFlagsFromString(*flags, StrLength(*flags)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11541 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11542 | } |
| 11543 | |
| 11544 | |
| 11545 | // Performs a GC. |
| 11546 | // Presently, it only does a full GC. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11547 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectGarbage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11548 | isolate->heap()->CollectAllGarbage(true); |
| 11549 | return isolate->heap()->undefined_value(); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11550 | } |
| 11551 | |
| 11552 | |
| 11553 | // Gets the current heap usage. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11554 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetHeapUsage) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11555 | int usage = static_cast<int>(isolate->heap()->SizeOfObjects()); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11556 | if (!Smi::IsValid(usage)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11557 | return *isolate->factory()->NewNumberFromInt(usage); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11558 | } |
| 11559 | return Smi::FromInt(usage); |
| 11560 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11561 | |
| 11562 | |
| 11563 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11564 | RUNTIME_FUNCTION(MaybeObject*, Runtime_HasLOLEnabled) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11565 | #ifdef LIVE_OBJECT_LIST |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11566 | return isolate->heap()->true_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11567 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11568 | return isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11569 | #endif |
| 11570 | } |
| 11571 | |
| 11572 | |
| 11573 | // Captures a live object list from the present heap. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11574 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CaptureLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11575 | #ifdef LIVE_OBJECT_LIST |
| 11576 | return LiveObjectList::Capture(); |
| 11577 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11578 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11579 | #endif |
| 11580 | } |
| 11581 | |
| 11582 | |
| 11583 | // Deletes the specified live object list. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11584 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DeleteLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11585 | #ifdef LIVE_OBJECT_LIST |
| 11586 | CONVERT_SMI_CHECKED(id, args[0]); |
| 11587 | bool success = LiveObjectList::Delete(id); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11588 | return success ? isolate->heap()->true_value() : |
| 11589 | isolate->heap()->false_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11590 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11591 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11592 | #endif |
| 11593 | } |
| 11594 | |
| 11595 | |
| 11596 | // Generates the response to a debugger request for a dump of the objects |
| 11597 | // contained in the difference between the captured live object lists |
| 11598 | // specified by id1 and id2. |
| 11599 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 11600 | // dumped. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11601 | RUNTIME_FUNCTION(MaybeObject*, Runtime_DumpLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11602 | #ifdef LIVE_OBJECT_LIST |
| 11603 | HandleScope scope; |
| 11604 | CONVERT_SMI_CHECKED(id1, args[0]); |
| 11605 | CONVERT_SMI_CHECKED(id2, args[1]); |
| 11606 | CONVERT_SMI_CHECKED(start, args[2]); |
| 11607 | CONVERT_SMI_CHECKED(count, args[3]); |
| 11608 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 4); |
| 11609 | EnterDebugger enter_debugger; |
| 11610 | return LiveObjectList::Dump(id1, id2, start, count, filter_obj); |
| 11611 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11612 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11613 | #endif |
| 11614 | } |
| 11615 | |
| 11616 | |
| 11617 | // Gets the specified object as requested by the debugger. |
| 11618 | // 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] | 11619 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11620 | #ifdef LIVE_OBJECT_LIST |
| 11621 | CONVERT_SMI_CHECKED(obj_id, args[0]); |
| 11622 | Object* result = LiveObjectList::GetObj(obj_id); |
| 11623 | return result; |
| 11624 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11625 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11626 | #endif |
| 11627 | } |
| 11628 | |
| 11629 | |
| 11630 | // Gets the obj id for the specified address if valid. |
| 11631 | // 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] | 11632 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjId) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11633 | #ifdef LIVE_OBJECT_LIST |
| 11634 | HandleScope scope; |
| 11635 | CONVERT_ARG_CHECKED(String, address, 0); |
| 11636 | Object* result = LiveObjectList::GetObjId(address); |
| 11637 | return result; |
| 11638 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11639 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11640 | #endif |
| 11641 | } |
| 11642 | |
| 11643 | |
| 11644 | // Gets the retainers that references the specified object alive. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11645 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLObjRetainers) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11646 | #ifdef LIVE_OBJECT_LIST |
| 11647 | HandleScope scope; |
| 11648 | CONVERT_SMI_CHECKED(obj_id, args[0]); |
| 11649 | RUNTIME_ASSERT(args[1]->IsUndefined() || args[1]->IsJSObject()); |
| 11650 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsBoolean()); |
| 11651 | RUNTIME_ASSERT(args[3]->IsUndefined() || args[3]->IsSmi()); |
| 11652 | RUNTIME_ASSERT(args[4]->IsUndefined() || args[4]->IsSmi()); |
| 11653 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 5); |
| 11654 | |
| 11655 | Handle<JSObject> instance_filter; |
| 11656 | if (args[1]->IsJSObject()) { |
| 11657 | instance_filter = args.at<JSObject>(1); |
| 11658 | } |
| 11659 | bool verbose = false; |
| 11660 | if (args[2]->IsBoolean()) { |
| 11661 | verbose = args[2]->IsTrue(); |
| 11662 | } |
| 11663 | int start = 0; |
| 11664 | if (args[3]->IsSmi()) { |
| 11665 | start = Smi::cast(args[3])->value(); |
| 11666 | } |
| 11667 | int limit = Smi::kMaxValue; |
| 11668 | if (args[4]->IsSmi()) { |
| 11669 | limit = Smi::cast(args[4])->value(); |
| 11670 | } |
| 11671 | |
| 11672 | return LiveObjectList::GetObjRetainers(obj_id, |
| 11673 | instance_filter, |
| 11674 | verbose, |
| 11675 | start, |
| 11676 | limit, |
| 11677 | filter_obj); |
| 11678 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11679 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11680 | #endif |
| 11681 | } |
| 11682 | |
| 11683 | |
| 11684 | // Gets the reference path between 2 objects. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11685 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetLOLPath) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11686 | #ifdef LIVE_OBJECT_LIST |
| 11687 | HandleScope scope; |
| 11688 | CONVERT_SMI_CHECKED(obj_id1, args[0]); |
| 11689 | CONVERT_SMI_CHECKED(obj_id2, args[1]); |
| 11690 | RUNTIME_ASSERT(args[2]->IsUndefined() || args[2]->IsJSObject()); |
| 11691 | |
| 11692 | Handle<JSObject> instance_filter; |
| 11693 | if (args[2]->IsJSObject()) { |
| 11694 | instance_filter = args.at<JSObject>(2); |
| 11695 | } |
| 11696 | |
| 11697 | Object* result = |
| 11698 | LiveObjectList::GetPath(obj_id1, obj_id2, instance_filter); |
| 11699 | return result; |
| 11700 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11701 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11702 | #endif |
| 11703 | } |
| 11704 | |
| 11705 | |
| 11706 | // Generates the response to a debugger request for a list of all |
| 11707 | // previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11708 | RUNTIME_FUNCTION(MaybeObject*, Runtime_InfoLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11709 | #ifdef LIVE_OBJECT_LIST |
| 11710 | CONVERT_SMI_CHECKED(start, args[0]); |
| 11711 | CONVERT_SMI_CHECKED(count, args[1]); |
| 11712 | return LiveObjectList::Info(start, count); |
| 11713 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11714 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11715 | #endif |
| 11716 | } |
| 11717 | |
| 11718 | |
| 11719 | // Gets a dump of the specified object as requested by the debugger. |
| 11720 | // 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] | 11721 | RUNTIME_FUNCTION(MaybeObject*, Runtime_PrintLOLObj) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11722 | #ifdef LIVE_OBJECT_LIST |
| 11723 | HandleScope scope; |
| 11724 | CONVERT_SMI_CHECKED(obj_id, args[0]); |
| 11725 | Object* result = LiveObjectList::PrintObj(obj_id); |
| 11726 | return result; |
| 11727 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11728 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11729 | #endif |
| 11730 | } |
| 11731 | |
| 11732 | |
| 11733 | // Resets and releases all previously captured live object lists. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11734 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ResetLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11735 | #ifdef LIVE_OBJECT_LIST |
| 11736 | LiveObjectList::Reset(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11737 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11738 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11739 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11740 | #endif |
| 11741 | } |
| 11742 | |
| 11743 | |
| 11744 | // Generates the response to a debugger request for a summary of the types |
| 11745 | // of objects in the difference between the captured live object lists |
| 11746 | // specified by id1 and id2. |
| 11747 | // If id1 is 0 (i.e. not a valid lol), then the whole of lol id2 will be |
| 11748 | // summarized. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11749 | RUNTIME_FUNCTION(MaybeObject*, Runtime_SummarizeLOL) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11750 | #ifdef LIVE_OBJECT_LIST |
| 11751 | HandleScope scope; |
| 11752 | CONVERT_SMI_CHECKED(id1, args[0]); |
| 11753 | CONVERT_SMI_CHECKED(id2, args[1]); |
| 11754 | CONVERT_ARG_CHECKED(JSObject, filter_obj, 2); |
| 11755 | |
| 11756 | EnterDebugger enter_debugger; |
| 11757 | return LiveObjectList::Summarize(id1, id2, filter_obj); |
| 11758 | #else |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11759 | return isolate->heap()->undefined_value(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 11760 | #endif |
| 11761 | } |
| 11762 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11763 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 11764 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11765 | |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 11766 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11767 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerResume) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11768 | NoHandleAllocation ha; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11769 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11770 | |
| 11771 | CONVERT_CHECKED(Smi, smi_modules, args[0]); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11772 | CONVERT_CHECKED(Smi, smi_tag, args[1]); |
| 11773 | v8::V8::ResumeProfilerEx(smi_modules->value(), smi_tag->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11774 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11775 | } |
| 11776 | |
| 11777 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11778 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ProfilerPause) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11779 | NoHandleAllocation ha; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11780 | ASSERT(args.length() == 2); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11781 | |
| 11782 | CONVERT_CHECKED(Smi, smi_modules, args[0]); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11783 | CONVERT_CHECKED(Smi, smi_tag, args[1]); |
| 11784 | v8::V8::PauseProfilerEx(smi_modules->value(), smi_tag->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11785 | return isolate->heap()->undefined_value(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 11786 | } |
| 11787 | |
| 11788 | #endif // ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 11789 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11790 | // Finds the script object from the script data. NOTE: This operation uses |
| 11791 | // heap traversal to find the function generated for the source position |
| 11792 | // for the requested break point. For lazily compiled functions several heap |
| 11793 | // traversals might be required rendering this operation as a rather slow |
| 11794 | // operation. However for setting break points which is normally done through |
| 11795 | // some kind of user interaction the performance is not crucial. |
| 11796 | static Handle<Object> Runtime_GetScriptFromScriptName( |
| 11797 | Handle<String> script_name) { |
| 11798 | // Scan the heap for Script objects to find the script with the requested |
| 11799 | // script data. |
| 11800 | Handle<Script> script; |
| 11801 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11802 | HeapObject* obj = NULL; |
| 11803 | while (script.is_null() && ((obj = iterator.next()) != NULL)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11804 | // If a script is found check if it has the script data requested. |
| 11805 | if (obj->IsScript()) { |
| 11806 | if (Script::cast(obj)->name()->IsString()) { |
| 11807 | if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) { |
| 11808 | script = Handle<Script>(Script::cast(obj)); |
| 11809 | } |
| 11810 | } |
| 11811 | } |
| 11812 | } |
| 11813 | |
| 11814 | // 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] | 11815 | if (script.is_null()) return FACTORY->undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11816 | |
| 11817 | // Return the script found. |
| 11818 | return GetScriptWrapper(script); |
| 11819 | } |
| 11820 | |
| 11821 | |
| 11822 | // Get the script object from script data. NOTE: Regarding performance |
| 11823 | // see the NOTE for GetScriptFromScriptData. |
| 11824 | // 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] | 11825 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetScript) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11826 | HandleScope scope(isolate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11827 | |
| 11828 | ASSERT(args.length() == 1); |
| 11829 | |
| 11830 | CONVERT_CHECKED(String, script_name, args[0]); |
| 11831 | |
| 11832 | // Find the requested script. |
| 11833 | Handle<Object> result = |
| 11834 | Runtime_GetScriptFromScriptName(Handle<String>(script_name)); |
| 11835 | return *result; |
| 11836 | } |
| 11837 | |
| 11838 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11839 | // Determines whether the given stack frame should be displayed in |
| 11840 | // a stack trace. The caller is the error constructor that asked |
| 11841 | // for the stack trace to be collected. The first time a construct |
| 11842 | // call to this function is encountered it is skipped. The seen_caller |
| 11843 | // in/out parameter is used to remember if the caller has been seen |
| 11844 | // yet. |
| 11845 | static bool ShowFrameInStackTrace(StackFrame* raw_frame, Object* caller, |
| 11846 | bool* seen_caller) { |
| 11847 | // Only display JS frames. |
| 11848 | if (!raw_frame->is_java_script()) |
| 11849 | return false; |
| 11850 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
| 11851 | Object* raw_fun = frame->function(); |
| 11852 | // Not sure when this can happen but skip it just in case. |
| 11853 | if (!raw_fun->IsJSFunction()) |
| 11854 | return false; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11855 | if ((raw_fun == caller) && !(*seen_caller)) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11856 | *seen_caller = true; |
| 11857 | return false; |
| 11858 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11859 | // Skip all frames until we've seen the caller. Also, skip the most |
| 11860 | // obvious builtin calls. Some builtin calls (such as Number.ADD |
| 11861 | // which is invoked using 'call') are very difficult to recognize |
| 11862 | // so we're leaving them in for now. |
| 11863 | return *seen_caller && !frame->receiver()->IsJSBuiltinsObject(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11864 | } |
| 11865 | |
| 11866 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11867 | // Collect the raw data for a stack trace. Returns an array of 4 |
| 11868 | // element segments each containing a receiver, function, code and |
| 11869 | // native code offset. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11870 | RUNTIME_FUNCTION(MaybeObject*, Runtime_CollectStackTrace) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11871 | ASSERT_EQ(args.length(), 2); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11872 | Handle<Object> caller = args.at<Object>(0); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11873 | CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[1]); |
| 11874 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11875 | HandleScope scope(isolate); |
| 11876 | Factory* factory = isolate->factory(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11877 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 11878 | limit = Max(limit, 0); // Ensure that limit is not negative. |
| 11879 | int initial_size = Min(limit, 10); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11880 | Handle<FixedArray> elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11881 | factory->NewFixedArrayWithHoles(initial_size * 4); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11882 | |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11883 | StackFrameIterator iter(isolate); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11884 | // If the caller parameter is a function we skip frames until we're |
| 11885 | // under it before starting to collect. |
| 11886 | bool seen_caller = !caller->IsJSFunction(); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11887 | int cursor = 0; |
| 11888 | int frames_seen = 0; |
| 11889 | while (!iter.done() && frames_seen < limit) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11890 | StackFrame* raw_frame = iter.frame(); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 11891 | if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) { |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11892 | frames_seen++; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11893 | JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame); |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 11894 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 11895 | // function. |
| 11896 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11897 | frame->Summarize(&frames); |
| 11898 | for (int i = frames.length() - 1; i >= 0; i--) { |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11899 | if (cursor + 4 > elements->length()) { |
| 11900 | int new_capacity = JSObject::NewElementsCapacity(elements->length()); |
| 11901 | Handle<FixedArray> new_elements = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11902 | factory->NewFixedArrayWithHoles(new_capacity); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11903 | for (int i = 0; i < cursor; i++) { |
| 11904 | new_elements->set(i, elements->get(i)); |
| 11905 | } |
| 11906 | elements = new_elements; |
| 11907 | } |
| 11908 | ASSERT(cursor + 4 <= elements->length()); |
| 11909 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 11910 | Handle<Object> recv = frames[i].receiver(); |
| 11911 | Handle<JSFunction> fun = frames[i].function(); |
| 11912 | Handle<Code> code = frames[i].code(); |
| 11913 | Handle<Smi> offset(Smi::FromInt(frames[i].offset())); |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 11914 | elements->set(cursor++, *recv); |
| 11915 | elements->set(cursor++, *fun); |
| 11916 | elements->set(cursor++, *code); |
| 11917 | elements->set(cursor++, *offset); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11918 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11919 | } |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 11920 | iter.Advance(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11921 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11922 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11923 | result->set_length(Smi::FromInt(cursor)); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 11924 | return *result; |
| 11925 | } |
| 11926 | |
| 11927 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11928 | // Returns V8 version as a string. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11929 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetV8Version) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11930 | ASSERT_EQ(args.length(), 0); |
| 11931 | |
| 11932 | NoHandleAllocation ha; |
| 11933 | |
| 11934 | const char* version_string = v8::V8::GetVersion(); |
| 11935 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11936 | return isolate->heap()->AllocateStringFromAscii(CStrVector(version_string), |
| 11937 | NOT_TENURED); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 11938 | } |
| 11939 | |
| 11940 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11941 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Abort) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11942 | ASSERT(args.length() == 2); |
| 11943 | OS::PrintError("abort: %s\n", reinterpret_cast<char*>(args[0]) + |
| 11944 | Smi::cast(args[1])->value()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11945 | isolate->PrintStack(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 11946 | OS::Abort(); |
| 11947 | UNREACHABLE(); |
| 11948 | return NULL; |
| 11949 | } |
| 11950 | |
| 11951 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 11952 | RUNTIME_FUNCTION(MaybeObject*, Runtime_GetFromCache) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11953 | // 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] | 11954 | CONVERT_CHECKED(JSFunctionResultCache, cache, args[0]); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11955 | Object* key = args[1]; |
| 11956 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11957 | int finger_index = cache->finger_index(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11958 | Object* o = cache->get(finger_index); |
| 11959 | if (o == key) { |
| 11960 | // The fastest case: hit the same place again. |
| 11961 | return cache->get(finger_index + 1); |
| 11962 | } |
| 11963 | |
| 11964 | for (int i = finger_index - 2; |
| 11965 | i >= JSFunctionResultCache::kEntriesIndex; |
| 11966 | i -= 2) { |
| 11967 | o = cache->get(i); |
| 11968 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11969 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11970 | return cache->get(i + 1); |
| 11971 | } |
| 11972 | } |
| 11973 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11974 | int size = cache->size(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11975 | ASSERT(size <= cache->length()); |
| 11976 | |
| 11977 | for (int i = size - 2; i > finger_index; i -= 2) { |
| 11978 | o = cache->get(i); |
| 11979 | if (o == key) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11980 | cache->set_finger_index(i); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 11981 | return cache->get(i + 1); |
| 11982 | } |
| 11983 | } |
| 11984 | |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11985 | // 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] | 11986 | HandleScope scope(isolate); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11987 | |
| 11988 | Handle<JSFunctionResultCache> cache_handle(cache); |
| 11989 | Handle<Object> key_handle(key); |
| 11990 | Handle<Object> value; |
| 11991 | { |
| 11992 | Handle<JSFunction> factory(JSFunction::cast( |
| 11993 | cache_handle->get(JSFunctionResultCache::kFactoryIndex))); |
| 11994 | // TODO(antonm): consider passing a receiver when constructing a cache. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 11995 | Handle<Object> receiver(isolate->global_context()->global()); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 11996 | // This handle is nor shared, nor used later, so it's safe. |
| 11997 | Object** argv[] = { key_handle.location() }; |
| 11998 | bool pending_exception = false; |
| 11999 | value = Execution::Call(factory, |
| 12000 | receiver, |
| 12001 | 1, |
| 12002 | argv, |
| 12003 | &pending_exception); |
| 12004 | if (pending_exception) return Failure::Exception(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12005 | } |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 12006 | |
| 12007 | #ifdef DEBUG |
| 12008 | cache_handle->JSFunctionResultCacheVerify(); |
| 12009 | #endif |
| 12010 | |
| 12011 | // Function invocation may have cleared the cache. Reread all the data. |
| 12012 | finger_index = cache_handle->finger_index(); |
| 12013 | size = cache_handle->size(); |
| 12014 | |
| 12015 | // If we have spare room, put new data into it, otherwise evict post finger |
| 12016 | // entry which is likely to be the least recently used. |
| 12017 | int index = -1; |
| 12018 | if (size < cache_handle->length()) { |
| 12019 | cache_handle->set_size(size + JSFunctionResultCache::kEntrySize); |
| 12020 | index = size; |
| 12021 | } else { |
| 12022 | index = finger_index + JSFunctionResultCache::kEntrySize; |
| 12023 | if (index == cache_handle->length()) { |
| 12024 | index = JSFunctionResultCache::kEntriesIndex; |
| 12025 | } |
| 12026 | } |
| 12027 | |
| 12028 | ASSERT(index % 2 == 0); |
| 12029 | ASSERT(index >= JSFunctionResultCache::kEntriesIndex); |
| 12030 | ASSERT(index < cache_handle->length()); |
| 12031 | |
| 12032 | cache_handle->set(index, *key_handle); |
| 12033 | cache_handle->set(index + 1, *value); |
| 12034 | cache_handle->set_finger_index(index); |
| 12035 | |
| 12036 | #ifdef DEBUG |
| 12037 | cache_handle->JSFunctionResultCacheVerify(); |
| 12038 | #endif |
| 12039 | |
| 12040 | return *value; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 12041 | } |
| 12042 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12043 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12044 | RUNTIME_FUNCTION(MaybeObject*, Runtime_NewMessageObject) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12045 | HandleScope scope(isolate); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12046 | CONVERT_ARG_CHECKED(String, type, 0); |
| 12047 | CONVERT_ARG_CHECKED(JSArray, arguments, 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12048 | return *isolate->factory()->NewJSMessageObject( |
| 12049 | type, |
| 12050 | arguments, |
| 12051 | 0, |
| 12052 | 0, |
| 12053 | isolate->factory()->undefined_value(), |
| 12054 | isolate->factory()->undefined_value(), |
| 12055 | isolate->factory()->undefined_value()); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12056 | } |
| 12057 | |
| 12058 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12059 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetType) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12060 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12061 | return message->type(); |
| 12062 | } |
| 12063 | |
| 12064 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12065 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetArguments) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12066 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12067 | return message->arguments(); |
| 12068 | } |
| 12069 | |
| 12070 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12071 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetStartPosition) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12072 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12073 | return Smi::FromInt(message->start_position()); |
| 12074 | } |
| 12075 | |
| 12076 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12077 | RUNTIME_FUNCTION(MaybeObject*, Runtime_MessageGetScript) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 12078 | CONVERT_CHECKED(JSMessageObject, message, args[0]); |
| 12079 | return message->script(); |
| 12080 | } |
| 12081 | |
| 12082 | |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 12083 | #ifdef DEBUG |
| 12084 | // ListNatives is ONLY used by the fuzz-natives.js in debug mode |
| 12085 | // Exclude the code in release mode. |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12086 | RUNTIME_FUNCTION(MaybeObject*, Runtime_ListNatives) { |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 12087 | ASSERT(args.length() == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12088 | HandleScope scope; |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12089 | #define COUNT_ENTRY(Name, argc, ressize) + 1 |
| 12090 | int entry_count = 0 |
| 12091 | RUNTIME_FUNCTION_LIST(COUNT_ENTRY) |
| 12092 | INLINE_FUNCTION_LIST(COUNT_ENTRY) |
| 12093 | INLINE_RUNTIME_FUNCTION_LIST(COUNT_ENTRY); |
| 12094 | #undef COUNT_ENTRY |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12095 | Factory* factory = isolate->factory(); |
| 12096 | Handle<FixedArray> elements = factory->NewFixedArray(entry_count); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12097 | int index = 0; |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12098 | bool inline_runtime_functions = false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 12099 | #define ADD_ENTRY(Name, argc, ressize) \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12100 | { \ |
| 12101 | HandleScope inner; \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12102 | Handle<String> name; \ |
| 12103 | /* Inline runtime functions have an underscore in front of the name. */ \ |
| 12104 | if (inline_runtime_functions) { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12105 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12106 | Vector<const char>("_" #Name, StrLength("_" #Name))); \ |
| 12107 | } else { \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12108 | name = factory->NewStringFromAscii( \ |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12109 | Vector<const char>(#Name, StrLength(#Name))); \ |
| 12110 | } \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12111 | Handle<FixedArray> pair_elements = factory->NewFixedArray(2); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12112 | pair_elements->set(0, *name); \ |
| 12113 | pair_elements->set(1, Smi::FromInt(argc)); \ |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12114 | Handle<JSArray> pair = factory->NewJSArrayWithElements(pair_elements); \ |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12115 | elements->set(index++, *pair); \ |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12116 | } |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12117 | inline_runtime_functions = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12118 | RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12119 | inline_runtime_functions = true; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12120 | INLINE_FUNCTION_LIST(ADD_ENTRY) |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 12121 | INLINE_RUNTIME_FUNCTION_LIST(ADD_ENTRY) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12122 | #undef ADD_ENTRY |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 12123 | ASSERT_EQ(index, entry_count); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12124 | Handle<JSArray> result = factory->NewJSArrayWithElements(elements); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12125 | return *result; |
| 12126 | } |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 12127 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12128 | |
| 12129 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12130 | RUNTIME_FUNCTION(MaybeObject*, Runtime_Log) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12131 | ASSERT(args.length() == 2); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 12132 | CONVERT_CHECKED(String, format, args[0]); |
| 12133 | CONVERT_CHECKED(JSArray, elms, args[1]); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12134 | Vector<const char> chars = format->ToAsciiVector(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12135 | LOGGER->LogRuntime(chars, elms); |
| 12136 | return isolate->heap()->undefined_value(); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12137 | } |
| 12138 | |
| 12139 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 12140 | RUNTIME_FUNCTION(MaybeObject*, Runtime_IS_VAR) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12141 | UNREACHABLE(); // implemented as macro in the parser |
| 12142 | return NULL; |
| 12143 | } |
| 12144 | |
| 12145 | |
| 12146 | // ---------------------------------------------------------------------------- |
| 12147 | // Implementation of Runtime |
| 12148 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12149 | #define F(name, number_of_args, result_size) \ |
| 12150 | { Runtime::k##name, Runtime::RUNTIME, #name, \ |
| 12151 | FUNCTION_ADDR(Runtime_##name), number_of_args, result_size }, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12152 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12153 | |
| 12154 | #define I(name, number_of_args, result_size) \ |
| 12155 | { Runtime::kInline##name, Runtime::INLINE, \ |
| 12156 | "_" #name, NULL, number_of_args, result_size }, |
| 12157 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12158 | static const Runtime::Function kIntrinsicFunctions[] = { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12159 | RUNTIME_FUNCTION_LIST(F) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12160 | INLINE_FUNCTION_LIST(I) |
| 12161 | INLINE_RUNTIME_FUNCTION_LIST(I) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12162 | }; |
| 12163 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12164 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12165 | MaybeObject* Runtime::InitializeIntrinsicFunctionNames(Heap* heap, |
| 12166 | Object* dictionary) { |
| 12167 | ASSERT(Isolate::Current()->heap() == heap); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12168 | ASSERT(dictionary != NULL); |
| 12169 | ASSERT(StringDictionary::cast(dictionary)->NumberOfElements() == 0); |
| 12170 | for (int i = 0; i < kNumFunctions; ++i) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12171 | Object* name_symbol; |
| 12172 | { MaybeObject* maybe_name_symbol = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12173 | heap->LookupAsciiSymbol(kIntrinsicFunctions[i].name); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12174 | if (!maybe_name_symbol->ToObject(&name_symbol)) return maybe_name_symbol; |
| 12175 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12176 | StringDictionary* string_dictionary = StringDictionary::cast(dictionary); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 12177 | { MaybeObject* maybe_dictionary = string_dictionary->Add( |
| 12178 | String::cast(name_symbol), |
| 12179 | Smi::FromInt(i), |
| 12180 | PropertyDetails(NONE, NORMAL)); |
| 12181 | if (!maybe_dictionary->ToObject(&dictionary)) { |
| 12182 | // Non-recoverable failure. Calling code must restart heap |
| 12183 | // initialization. |
| 12184 | return maybe_dictionary; |
| 12185 | } |
| 12186 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12187 | } |
| 12188 | return dictionary; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12189 | } |
| 12190 | |
| 12191 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12192 | const Runtime::Function* Runtime::FunctionForSymbol(Handle<String> name) { |
| 12193 | Heap* heap = name->GetHeap(); |
| 12194 | int entry = heap->intrinsic_function_names()->FindEntry(*name); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12195 | if (entry != kNotFound) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12196 | Object* smi_index = heap->intrinsic_function_names()->ValueAt(entry); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12197 | int function_index = Smi::cast(smi_index)->value(); |
| 12198 | return &(kIntrinsicFunctions[function_index]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12199 | } |
| 12200 | return NULL; |
| 12201 | } |
| 12202 | |
| 12203 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 12204 | const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 12205 | return &(kIntrinsicFunctions[static_cast<int>(id)]); |
| 12206 | } |
| 12207 | |
| 12208 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12209 | void Runtime::PerformGC(Object* result) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 12210 | Isolate* isolate = Isolate::Current(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12211 | Failure* failure = Failure::cast(result); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12212 | if (failure->IsRetryAfterGC()) { |
| 12213 | // Try to do a garbage collection; ignore it if it fails. The C |
| 12214 | // 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] | 12215 | isolate->heap()->CollectGarbage(failure->allocation_space()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12216 | } else { |
| 12217 | // Handle last resort GC and make sure to allow future allocations |
| 12218 | // to grow the heap without causing GCs (if possible). |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 12219 | isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 12220 | isolate->heap()->CollectAllGarbage(false); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 12221 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 12222 | } |
| 12223 | |
| 12224 | |
| 12225 | } } // namespace v8::internal |