Ignore:
Timestamp:
Sep 19, 2009, 3:14:56 PM (16 years ago)
Author:
[email protected]
Message:

Implement ES5 Object.defineProperties function
https://bugs.webkit.org/show_bug.cgi?id=29522

Reviewed by Sam Weinig

Implement Object.defineProperties. Fairly simple patch, simply makes use of
existing functionality used for defineProperty.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/ObjectConstructor.cpp

    r48542 r48565  
    3939static JSValue JSC_HOST_CALL objectConstructorKeys(ExecState*, JSObject*, JSValue, const ArgList&);
    4040static JSValue JSC_HOST_CALL objectConstructorDefineProperty(ExecState*, JSObject*, JSValue, const ArgList&);
     41static JSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState*, JSObject*, JSValue, const ArgList&);
    4142
    4243ObjectConstructor::ObjectConstructor(ExecState* exec, PassRefPtr<Structure> structure, ObjectPrototype* objectPrototype, Structure* prototypeFunctionStructure)
     
    5253    putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().getOwnPropertyDescriptor, objectConstructorGetOwnPropertyDescriptor), DontEnum);
    5354    putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().keys, objectConstructorKeys), DontEnum);
    54         putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 3, exec->propertyNames().defineProperty, objectConstructorDefineProperty), DontEnum);
     55    putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 3, exec->propertyNames().defineProperty, objectConstructorDefineProperty), DontEnum);
     56    putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 2, exec->propertyNames().defineProperties, objectConstructorDefineProperties), DontEnum);
    5557}
    5658
     
    238240}
    239241
     242static JSValue defineProperties(ExecState* exec, JSObject* object, JSObject* properties)
     243{
     244    PropertyNameArray propertyNames(exec);
     245    asObject(properties)->getOwnPropertyNames(exec, propertyNames);
     246    size_t numProperties = propertyNames.size();
     247    Vector<PropertyDescriptor> descriptors;
     248    MarkedArgumentBuffer markBuffer;
     249    for (size_t i = 0; i < numProperties; i++) {
     250        PropertySlot slot;
     251        JSValue prop = properties->get(exec, propertyNames[i]);
     252        if (exec->hadException())
     253            return jsNull();
     254        PropertyDescriptor descriptor;
     255        if (!toPropertyDescriptor(exec, prop, descriptor))
     256            return jsNull();
     257        descriptors.append(descriptor);
     258        // Ensure we mark all the values that we're accumulating
     259        if (descriptor.isDataDescriptor() && descriptor.value())
     260            markBuffer.append(descriptor.value());
     261        if (descriptor.isAccessorDescriptor()) {
     262            if (descriptor.getter())
     263                markBuffer.append(descriptor.getter());
     264            if (descriptor.setter())
     265                markBuffer.append(descriptor.setter());
     266        }
     267    }
     268    for (size_t i = 0; i < numProperties; i++) {
     269        object->defineOwnProperty(exec, propertyNames[i], descriptors[i], true);
     270        if (exec->hadException())
     271            return jsNull();
     272    }
     273    return object;
     274}
     275
     276JSValue JSC_HOST_CALL objectConstructorDefineProperties(ExecState* exec, JSObject*, JSValue, const ArgList& args)
     277{
     278    if (!args.at(0).isObject())
     279        return throwError(exec, TypeError, "Properties can only be defined on Objects.");
     280    if (!args.at(1).isObject())
     281        return throwError(exec, TypeError, "Property descriptor list must be an Object.");
     282    return defineProperties(exec, asObject(args.at(0)), asObject(args.at(1)));
     283}
     284
    240285} // namespace JSC
Note: See TracChangeset for help on using the changeset viewer.