// Copyright 2006-2008 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 "execution.h" #include "factory.h" #include "scopeinfo.h" #include "top.h" #include "zone-inl.h" namespace v8 { namespace internal { template static C* FindInPrototypeChain(Object* obj, bool* found_it) { ASSERT(!*found_it); 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. Object* Accessors::IllegalSetter(JSObject*, Object*, void*) { UNREACHABLE(); return NULL; } Object* Accessors::IllegalGetAccessor(Object* object, void*) { UNREACHABLE(); return object; } Object* 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 // Object* 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( Top::context()->global_context()->number_function()->has_initial_map()); Map* number_map = Top::context()->global_context()->number_function()->initial_map(); if (wrapper->map() == number_map) return wrapper->value(); return value; } Object* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { value = FlattenNumber(value); // Need to call methods that may trigger GC. HandleScope scope; // Protect raw pointers. Handle object_handle(object); Handle value_handle(value); 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(); // Restore raw pointers, object = *object_handle; value = *value_handle; if (uint32_v->Number() == number_v->Number()) { if (object->IsJSArray()) { return JSArray::cast(object)->SetElementsLength(*uint32_v); } else { // 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. return object->IgnoreAttributesAndSetLocalProperty(Heap::length_symbol(), value, NONE); } } return Top::Throw(*Factory::NewRangeError("invalid_array_length", HandleVector(NULL, 0))); } const AccessorDescriptor Accessors::ArrayLength = { ArrayGetLength, ArraySetLength, 0 }; // // Accessors::StringLength // Object* 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 // Object* 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 // Object* 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 // Object* 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 // Object* 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 // Object* 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 // Object* 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 // Object* Accessors::ScriptGetType(Object* object, void*) { Object* script = JSValue::cast(object)->value(); return Script::cast(script)->type(); } const AccessorDescriptor Accessors::ScriptType = { ScriptGetType, IllegalSetter, 0 }; // // Accessors::ScriptGetLineEnds // Object* Accessors::ScriptGetLineEnds(Object* object, void*) { HandleScope scope; Handle