Changeset 48565 in webkit


Ignore:
Timestamp:
Sep 19, 2009 3:14:56 PM (15 years ago)
Author:
oliver@apple.com
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.

Location:
trunk
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r48563 r48565  
    2222        (JSC::Heap::freeBlock):
    2323        * runtime/Collector.h:
     24
     252009-09-19  Oliver Hunt  <oliver@apple.com>
     26
     27        Reviewed by Sam Weinig.
     28
     29        Implement ES5 Object.defineProperties function
     30        https://bugs.webkit.org/show_bug.cgi?id=29522
     31
     32        Implement Object.defineProperties.  Fairly simple patch, simply makes use of
     33        existing functionality used for defineProperty.
     34
     35        * runtime/CommonIdentifiers.h:
     36        * runtime/ObjectConstructor.cpp:
     37        (JSC::ObjectConstructor::ObjectConstructor):
     38        (JSC::defineProperties):
     39        (JSC::objectConstructorDefineProperties):
    2440
    25412009-09-19  Oliver Hunt  <oliver@apple.com>
  • trunk/JavaScriptCore/runtime/CommonIdentifiers.h

    r48542 r48565  
    4141    macro(constructor) \
    4242    macro(defineProperty) \
     43    macro(defineProperties) \
    4344    macro(enumerable) \
    4445    macro(eval) \
  • 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
  • trunk/LayoutTests/ChangeLog

    r48564 r48565  
     12009-09-19  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Sam Weinig.
     4
     5        Implement ES5 Object.defineProperties function
     6        https://bugs.webkit.org/show_bug.cgi?id=29522
     7
     8        Add tests for Object.defineProperties API.
     9
     10        * fast/js/Object-defineProperties-expected.txt: Added.
     11        * fast/js/Object-defineProperties.html: Added.
     12        * fast/js/resources/Object-defineProperties.js: Added.
     13
    1142009-09-19  Daniel Bates  <dbates@webkit.org>
    215
Note: See TracChangeset for help on using the changeset viewer.