// Copyright 2011 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "v8.h" #include "accessors.h" #include "ast.h" #include "deoptimizer.h" #include "execution.h" #include "factory.h" #include "list-inl.h" #include "safepoint-table.h" #include "scopeinfo.h" namespace v8 { namespace internal { template static C* FindInPrototypeChain(Object* obj, bool* found_it) { ASSERT(!*found_it); Heap* heap = HEAP; while (!Is(obj)) { if (obj == heap->null_value()) return NULL; obj = obj->GetPrototype(); } *found_it = true; return C::cast(obj); } // Entry point that never should be called. MaybeObject* Accessors::IllegalSetter(JSObject*, Object*, void*) { UNREACHABLE(); return NULL; } Object* Accessors::IllegalGetAccessor(Object* object, void*) { UNREACHABLE(); return object; } MaybeObject* Accessors::ReadOnlySetAccessor(JSObject*, Object* value, void*) { // According to ECMA-262, section 8.6.2.2, page 28, setting // read-only properties must be silently ignored. return value; } // // Accessors::ArrayLength // MaybeObject* Accessors::ArrayGetLength(Object* object, void*) { // Traverse the prototype chain until we reach an array. bool found_it = false; JSArray* holder = FindInPrototypeChain(object, &found_it); if (!found_it) return Smi::FromInt(0); return holder->length(); } // The helper function will 'flatten' Number objects. Object* Accessors::FlattenNumber(Object* value) { if (value->IsNumber() || !value->IsJSValue()) return value; JSValue* wrapper = JSValue::cast(value); ASSERT(Isolate::Current()->context()->global_context()->number_function()-> has_initial_map()); Map* number_map = Isolate::Current()->context()->global_context()-> number_function()->initial_map(); if (wrapper->map() == number_map) return wrapper->value(); return value; } MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { Isolate* isolate = object->GetIsolate(); // This means one of the object's prototypes is a JSArray and the // object does not have a 'length' property. Calling SetProperty // causes an infinite loop. if (!object->IsJSArray()) { return object->SetLocalPropertyIgnoreAttributes( isolate->heap()->length_symbol(), value, NONE); } value = FlattenNumber(value); // Need to call methods that may trigger GC. HandleScope scope(isolate); // Protect raw pointers. Handle object_handle(object, isolate); Handle value_handle(value, isolate); bool has_exception; Handle uint32_v = Execution::ToUint32(value_handle, &has_exception); if (has_exception) return Failure::Exception(); Handle number_v = Execution::ToNumber(value_handle, &has_exception); if (has_exception) return Failure::Exception(); if (uint32_v->Number() == number_v->Number()) { return Handle::cast(object_handle)->SetElementsLength(*uint32_v); } return isolate->Throw( *isolate->factory()->NewRangeError("invalid_array_length", HandleVector(NULL, 0))); } const AccessorDescriptor Accessors::ArrayLength = { ArrayGetLength, ArraySetLength, 0 }; // // Accessors::StringLength // MaybeObject* Accessors::StringGetLength(Object* object, void*) { Object* value = object; if (object->IsJSValue()) value = JSValue::cast(object)->value(); if (value->IsString()) return Smi::FromInt(String::cast(value)->length()); // If object is not a string we return 0 to be compatible with WebKit. // Note: Firefox returns the length of ToString(object). return Smi::FromInt(0); } const AccessorDescriptor Accessors::StringLength = { StringGetLength, IllegalSetter, 0 }; // // Accessors::ScriptSource // MaybeObject* Accessors::ScriptGetSource(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->source(); } const AccessorDescriptor Accessors::ScriptSource = { ScriptGetSource, IllegalSetter, 0 }; // // Accessors::ScriptName // MaybeObject* Accessors::ScriptGetName(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->name(); } const AccessorDescriptor Accessors::ScriptName = { ScriptGetName, IllegalSetter, 0 }; // // Accessors::ScriptId // MaybeObject* Accessors::ScriptGetId(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->id(); } const AccessorDescriptor Accessors::ScriptId = { ScriptGetId, IllegalSetter, 0 }; // // Accessors::ScriptLineOffset // MaybeObject* Accessors::ScriptGetLineOffset(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->line_offset(); } const AccessorDescriptor Accessors::ScriptLineOffset = { ScriptGetLineOffset, IllegalSetter, 0 }; // // Accessors::ScriptColumnOffset // MaybeObject* Accessors::ScriptGetColumnOffset(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->column_offset(); } const AccessorDescriptor Accessors::ScriptColumnOffset = { ScriptGetColumnOffset, IllegalSetter, 0 }; // // Accessors::ScriptData // MaybeObject* Accessors::ScriptGetData(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->data(); } const AccessorDescriptor Accessors::ScriptData = { ScriptGetData, IllegalSetter, 0 }; // // Accessors::ScriptType // MaybeObject* Accessors::ScriptGetType(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->type(); } const AccessorDescriptor Accessors::ScriptType = { ScriptGetType, IllegalSetter, 0 }; // // Accessors::ScriptCompilationType // MaybeObject* Accessors::ScriptGetCompilationType(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->compilation_type(); } const AccessorDescriptor Accessors::ScriptCompilationType = { ScriptGetCompilationType, IllegalSetter, 0 }; // // Accessors::ScriptGetLineEnds // MaybeObject* Accessors::ScriptGetLineEnds(Object* object, void*) { JSValue* wrapper = JSValue::cast(object); Isolate* isolate = wrapper->GetIsolate(); HandleScope scope(isolate); Handle