X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..14957cd040308e3eeec43d26bae5d76da13fcd85:/runtime/ObjectConstructor.cpp?ds=inline diff --git a/runtime/ObjectConstructor.cpp b/runtime/ObjectConstructor.cpp index 0838eb4..c325fa0 100644 --- a/runtime/ObjectConstructor.cpp +++ b/runtime/ObjectConstructor.cpp @@ -22,56 +22,91 @@ #include "ObjectConstructor.h" #include "Error.h" +#include "ExceptionHelpers.h" #include "JSFunction.h" #include "JSArray.h" #include "JSGlobalObject.h" +#include "Lookup.h" #include "ObjectPrototype.h" #include "PropertyDescriptor.h" #include "PropertyNameArray.h" -#include "PrototypeFunction.h" namespace JSC { ASSERT_CLASS_FITS_IN_CELL(ObjectConstructor); -static JSValue JSC_HOST_CALL objectConstructorGetPrototypeOf(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptor(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL objectConstructorGetOwnPropertyNames(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL objectConstructorKeys(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL objectConstructorDefineProperty(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState*, JSObject*, JSValue, const ArgList&); -static JSValue JSC_HOST_CALL objectConstructorCreate(ExecState*, JSObject*, JSValue, const ArgList&); +static EncodedJSValue JSC_HOST_CALL objectConstructorGetPrototypeOf(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptor(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyNames(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorKeys(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorDefineProperty(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorCreate(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorSeal(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorFreeze(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorPreventExtensions(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorIsSealed(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorIsFrozen(ExecState*); +static EncodedJSValue JSC_HOST_CALL objectConstructorIsExtensible(ExecState*); -ObjectConstructor::ObjectConstructor(ExecState* exec, NonNullPassRefPtr structure, ObjectPrototype* objectPrototype, Structure* prototypeFunctionStructure) -: InternalFunction(&exec->globalData(), structure, Identifier(exec, "Object")) +} + +#include "ObjectConstructor.lut.h" + +namespace JSC { + +const ClassInfo ObjectConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::objectConstructorTable }; + +/* Source for ObjectConstructor.lut.h +@begin objectConstructorTable + getPrototypeOf objectConstructorGetPrototypeOf DontEnum|Function 1 + getOwnPropertyDescriptor objectConstructorGetOwnPropertyDescriptor DontEnum|Function 2 + getOwnPropertyNames objectConstructorGetOwnPropertyNames DontEnum|Function 1 + keys objectConstructorKeys DontEnum|Function 1 + defineProperty objectConstructorDefineProperty DontEnum|Function 3 + defineProperties objectConstructorDefineProperties DontEnum|Function 2 + create objectConstructorCreate DontEnum|Function 2 + seal objectConstructorSeal DontEnum|Function 1 + freeze objectConstructorFreeze DontEnum|Function 1 + preventExtensions objectConstructorPreventExtensions DontEnum|Function 1 + isSealed objectConstructorIsSealed DontEnum|Function 1 + isFrozen objectConstructorIsFrozen DontEnum|Function 1 + isExtensible objectConstructorIsExtensible DontEnum|Function 1 +@end +*/ + +ObjectConstructor::ObjectConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, ObjectPrototype* objectPrototype) + : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, "Object")) { // ECMA 15.2.3.1 - putDirectWithoutTransition(exec->propertyNames().prototype, objectPrototype, DontEnum | DontDelete | ReadOnly); - + putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, objectPrototype, DontEnum | DontDelete | ReadOnly); // no. of arguments for constructor - putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete); - - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().getPrototypeOf, objectConstructorGetPrototypeOf), DontEnum); - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().getOwnPropertyDescriptor, objectConstructorGetOwnPropertyDescriptor), DontEnum); - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().getOwnPropertyNames, objectConstructorGetOwnPropertyNames), DontEnum); - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().keys, objectConstructorKeys), DontEnum); - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 3, exec->propertyNames().defineProperty, objectConstructorDefineProperty), DontEnum); - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().defineProperties, objectConstructorDefineProperties), DontEnum); - putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().create, objectConstructorCreate), DontEnum); + putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete); +} + +bool ObjectConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot) +{ + return getStaticFunctionSlot(exec, ExecState::objectConstructorTable(exec), this, propertyName, slot); +} + +bool ObjectConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) +{ + return getStaticFunctionDescriptor(exec, ExecState::objectConstructorTable(exec), this, propertyName, descriptor); } // ECMA 15.2.2 -static ALWAYS_INLINE JSObject* constructObject(ExecState* exec, const ArgList& args) +static ALWAYS_INLINE JSObject* constructObject(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args) { JSValue arg = args.at(0); if (arg.isUndefinedOrNull()) - return new (exec) JSObject(exec->lexicalGlobalObject()->emptyObjectStructure()); - return arg.toObject(exec); + return constructEmptyObject(exec, globalObject); + return arg.toObject(exec, globalObject); } -static JSObject* constructWithObjectConstructor(ExecState* exec, JSObject*, const ArgList& args) +static EncodedJSValue JSC_HOST_CALL constructWithObjectConstructor(ExecState* exec) { - return constructObject(exec, args); + ArgList args(exec); + return JSValue::encode(constructObject(exec, asInternalFunction(exec->callee())->globalObject(), args)); } ConstructType ObjectConstructor::getConstructData(ConstructData& constructData) @@ -80,9 +115,10 @@ ConstructType ObjectConstructor::getConstructData(ConstructData& constructData) return ConstructTypeHost; } -static JSValue JSC_HOST_CALL callObjectConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args) +static EncodedJSValue JSC_HOST_CALL callObjectConstructor(ExecState* exec) { - return constructObject(exec, args); + ArgList args(exec); + return JSValue::encode(constructObject(exec, asInternalFunction(exec->callee())->globalObject(), args)); } CallType ObjectConstructor::getCallData(CallData& callData) @@ -91,75 +127,75 @@ CallType ObjectConstructor::getCallData(CallData& callData) return CallTypeHost; } -JSValue JSC_HOST_CALL objectConstructorGetPrototypeOf(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorGetPrototypeOf(ExecState* exec) { - if (!args.at(0).isObject()) - return throwError(exec, TypeError, "Requested prototype of a value that is not an object."); - return asObject(args.at(0))->prototype(); + if (!exec->argument(0).isObject()) + return throwVMError(exec, createTypeError(exec, "Requested prototype of a value that is not an object.")); + return JSValue::encode(asObject(exec->argument(0))->prototype()); } -JSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptor(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyDescriptor(ExecState* exec) { - if (!args.at(0).isObject()) - return throwError(exec, TypeError, "Requested property descriptor of a value that is not an object."); - UString propertyName = args.at(1).toString(exec); + if (!exec->argument(0).isObject()) + return throwVMError(exec, createTypeError(exec, "Requested property descriptor of a value that is not an object.")); + UString propertyName = exec->argument(1).toString(exec); if (exec->hadException()) - return jsNull(); - JSObject* object = asObject(args.at(0)); + return JSValue::encode(jsNull()); + JSObject* object = asObject(exec->argument(0)); PropertyDescriptor descriptor; if (!object->getOwnPropertyDescriptor(exec, Identifier(exec, propertyName), descriptor)) - return jsUndefined(); + return JSValue::encode(jsUndefined()); if (exec->hadException()) - return jsUndefined(); + return JSValue::encode(jsUndefined()); JSObject* description = constructEmptyObject(exec); if (!descriptor.isAccessorDescriptor()) { - description->putDirect(exec->propertyNames().value, descriptor.value() ? descriptor.value() : jsUndefined(), 0); - description->putDirect(exec->propertyNames().writable, jsBoolean(descriptor.writable()), 0); + description->putDirect(exec->globalData(), exec->propertyNames().value, descriptor.value() ? descriptor.value() : jsUndefined(), 0); + description->putDirect(exec->globalData(), exec->propertyNames().writable, jsBoolean(descriptor.writable()), 0); } else { - description->putDirect(exec->propertyNames().get, descriptor.getter() ? descriptor.getter() : jsUndefined(), 0); - description->putDirect(exec->propertyNames().set, descriptor.setter() ? descriptor.setter() : jsUndefined(), 0); + description->putDirect(exec->globalData(), exec->propertyNames().get, descriptor.getter() ? descriptor.getter() : jsUndefined(), 0); + description->putDirect(exec->globalData(), exec->propertyNames().set, descriptor.setter() ? descriptor.setter() : jsUndefined(), 0); } - description->putDirect(exec->propertyNames().enumerable, jsBoolean(descriptor.enumerable()), 0); - description->putDirect(exec->propertyNames().configurable, jsBoolean(descriptor.configurable()), 0); + description->putDirect(exec->globalData(), exec->propertyNames().enumerable, jsBoolean(descriptor.enumerable()), 0); + description->putDirect(exec->globalData(), exec->propertyNames().configurable, jsBoolean(descriptor.configurable()), 0); - return description; + return JSValue::encode(description); } // FIXME: Use the enumeration cache. -JSValue JSC_HOST_CALL objectConstructorGetOwnPropertyNames(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorGetOwnPropertyNames(ExecState* exec) { - if (!args.at(0).isObject()) - return throwError(exec, TypeError, "Requested property names of a value that is not an object."); + if (!exec->argument(0).isObject()) + return throwVMError(exec, createTypeError(exec, "Requested property names of a value that is not an object.")); PropertyNameArray properties(exec); - asObject(args.at(0))->getOwnPropertyNames(exec, properties, IncludeDontEnumProperties); + asObject(exec->argument(0))->getOwnPropertyNames(exec, properties, IncludeDontEnumProperties); JSArray* names = constructEmptyArray(exec); size_t numProperties = properties.size(); for (size_t i = 0; i < numProperties; i++) names->push(exec, jsOwnedString(exec, properties[i].ustring())); - return names; + return JSValue::encode(names); } // FIXME: Use the enumeration cache. -JSValue JSC_HOST_CALL objectConstructorKeys(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorKeys(ExecState* exec) { - if (!args.at(0).isObject()) - return throwError(exec, TypeError, "Requested keys of a value that is not an object."); + if (!exec->argument(0).isObject()) + return throwVMError(exec, createTypeError(exec, "Requested keys of a value that is not an object.")); PropertyNameArray properties(exec); - asObject(args.at(0))->getOwnPropertyNames(exec, properties); + asObject(exec->argument(0))->getOwnPropertyNames(exec, properties); JSArray* keys = constructEmptyArray(exec); size_t numProperties = properties.size(); for (size_t i = 0; i < numProperties; i++) keys->push(exec, jsOwnedString(exec, properties[i].ustring())); - return keys; + return JSValue::encode(keys); } // ES5 8.10.5 ToPropertyDescriptor static bool toPropertyDescriptor(ExecState* exec, JSValue in, PropertyDescriptor& desc) { if (!in.isObject()) { - throwError(exec, TypeError, "Property description must be an object."); + throwError(exec, createTypeError(exec, "Property description must be an object.")); return false; } JSObject* description = asObject(in); @@ -200,8 +236,8 @@ static bool toPropertyDescriptor(ExecState* exec, JSValue in, PropertyDescriptor return false; if (!get.isUndefined()) { CallData callData; - if (get.getCallData(callData) == CallTypeNone) { - throwError(exec, TypeError, "Getter must be a function."); + if (getCallData(get, callData) == CallTypeNone) { + throwError(exec, createTypeError(exec, "Getter must be a function.")); return false; } } else @@ -216,8 +252,8 @@ static bool toPropertyDescriptor(ExecState* exec, JSValue in, PropertyDescriptor return false; if (!set.isUndefined()) { CallData callData; - if (set.getCallData(callData) == CallTypeNone) { - throwError(exec, TypeError, "Setter must be a function."); + if (getCallData(set, callData) == CallTypeNone) { + throwError(exec, createTypeError(exec, "Setter must be a function.")); return false; } } else @@ -230,32 +266,32 @@ static bool toPropertyDescriptor(ExecState* exec, JSValue in, PropertyDescriptor return true; if (desc.value()) { - throwError(exec, TypeError, "Invalid property. 'value' present on property with getter or setter."); + throwError(exec, createTypeError(exec, "Invalid property. 'value' present on property with getter or setter.")); return false; } if (desc.writablePresent()) { - throwError(exec, TypeError, "Invalid property. 'writable' present on property with getter or setter."); + throwError(exec, createTypeError(exec, "Invalid property. 'writable' present on property with getter or setter.")); return false; } return true; } -JSValue JSC_HOST_CALL objectConstructorDefineProperty(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorDefineProperty(ExecState* exec) { - if (!args.at(0).isObject()) - return throwError(exec, TypeError, "Properties can only be defined on Objects."); - JSObject* O = asObject(args.at(0)); - UString propertyName = args.at(1).toString(exec); + if (!exec->argument(0).isObject()) + return throwVMError(exec, createTypeError(exec, "Properties can only be defined on Objects.")); + JSObject* O = asObject(exec->argument(0)); + UString propertyName = exec->argument(1).toString(exec); if (exec->hadException()) - return jsNull(); + return JSValue::encode(jsNull()); PropertyDescriptor descriptor; - if (!toPropertyDescriptor(exec, args.at(2), descriptor)) - return jsNull(); + if (!toPropertyDescriptor(exec, exec->argument(2), descriptor)) + return JSValue::encode(jsNull()); ASSERT((descriptor.attributes() & (Getter | Setter)) || (!descriptor.isAccessorDescriptor())); ASSERT(!exec->hadException()); O->defineOwnProperty(exec, Identifier(exec, propertyName), descriptor, true); - return O; + return JSValue::encode(O); } static JSValue defineProperties(ExecState* exec, JSObject* object, JSObject* properties) @@ -292,26 +328,77 @@ static JSValue defineProperties(ExecState* exec, JSObject* object, JSObject* pro return object; } -JSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState* exec) +{ + if (!exec->argument(0).isObject()) + return throwVMError(exec, createTypeError(exec, "Properties can only be defined on Objects.")); + if (!exec->argument(1).isObject()) + return throwVMError(exec, createTypeError(exec, "Property descriptor list must be an Object.")); + return JSValue::encode(defineProperties(exec, asObject(exec->argument(0)), asObject(exec->argument(1)))); +} + +EncodedJSValue JSC_HOST_CALL objectConstructorCreate(ExecState* exec) +{ + if (!exec->argument(0).isObject() && !exec->argument(0).isNull()) + return throwVMError(exec, createTypeError(exec, "Object prototype may only be an Object or null.")); + JSValue proto = exec->argument(0); + JSObject* newObject = proto.isObject() ? constructEmptyObject(exec, asObject(proto)->inheritorID(exec->globalData())) : constructEmptyObject(exec, exec->lexicalGlobalObject()->nullPrototypeObjectStructure()); + if (exec->argument(1).isUndefined()) + return JSValue::encode(newObject); + if (!exec->argument(1).isObject()) + return throwVMError(exec, createTypeError(exec, "Property descriptor list must be an Object.")); + return JSValue::encode(defineProperties(exec, newObject, asObject(exec->argument(1)))); +} + +EncodedJSValue JSC_HOST_CALL objectConstructorSeal(ExecState* exec) +{ + JSValue obj = exec->argument(0); + if (!obj.isObject()) + return throwVMError(exec, createTypeError(exec, "Object.seal can only be called on Objects.")); + asObject(obj)->seal(exec->globalData()); + return JSValue::encode(obj); +} + +EncodedJSValue JSC_HOST_CALL objectConstructorFreeze(ExecState* exec) +{ + JSValue obj = exec->argument(0); + if (!obj.isObject()) + return throwVMError(exec, createTypeError(exec, "Object.freeze can only be called on Objects.")); + asObject(obj)->freeze(exec->globalData()); + return JSValue::encode(obj); +} + +EncodedJSValue JSC_HOST_CALL objectConstructorPreventExtensions(ExecState* exec) +{ + JSValue obj = exec->argument(0); + if (!obj.isObject()) + return throwVMError(exec, createTypeError(exec, "Object.preventExtensions can only be called on Objects.")); + asObject(obj)->preventExtensions(exec->globalData()); + return JSValue::encode(obj); +} + +EncodedJSValue JSC_HOST_CALL objectConstructorIsSealed(ExecState* exec) +{ + JSValue obj = exec->argument(0); + if (!obj.isObject()) + return throwVMError(exec, createTypeError(exec, "Object.isSealed can only be called on Objects.")); + return JSValue::encode(jsBoolean(asObject(obj)->isSealed(exec->globalData()))); +} + +EncodedJSValue JSC_HOST_CALL objectConstructorIsFrozen(ExecState* exec) { - if (!args.at(0).isObject()) - return throwError(exec, TypeError, "Properties can only be defined on Objects."); - if (!args.at(1).isObject()) - return throwError(exec, TypeError, "Property descriptor list must be an Object."); - return defineProperties(exec, asObject(args.at(0)), asObject(args.at(1))); + JSValue obj = exec->argument(0); + if (!obj.isObject()) + return throwVMError(exec, createTypeError(exec, "Object.isFrozen can only be called on Objects.")); + return JSValue::encode(jsBoolean(asObject(obj)->isFrozen(exec->globalData()))); } -JSValue JSC_HOST_CALL objectConstructorCreate(ExecState* exec, JSObject*, JSValue, const ArgList& args) +EncodedJSValue JSC_HOST_CALL objectConstructorIsExtensible(ExecState* exec) { - if (!args.at(0).isObject() && !args.at(0).isNull()) - return throwError(exec, TypeError, "Object prototype may only be an Object or null."); - JSObject* newObject = constructEmptyObject(exec); - newObject->setPrototype(args.at(0)); - if (args.at(1).isUndefined()) - return newObject; - if (!args.at(1).isObject()) - return throwError(exec, TypeError, "Property descriptor list must be an Object."); - return defineProperties(exec, newObject, asObject(args.at(1))); + JSValue obj = exec->argument(0); + if (!obj.isObject()) + return throwVMError(exec, createTypeError(exec, "Object.isExtensible can only be called on Objects.")); + return JSValue::encode(jsBoolean(asObject(obj)->isExtensible())); } } // namespace JSC