Changeset 96627 in webkit


Ignore:
Timestamp:
Oct 4, 2011 12:02:03 PM (13 years ago)
Author:
mhahnenberg@apple.com
Message:

Implicitly add toString and valueOf to prototype when convertToType callback is provided
https://bugs.webkit.org/show_bug.cgi?id=69156

Reviewed by Geoffrey Garen.

Added callbacks for toString and valueOf which are implicitly added to a client object's
prototype if they provide a convertToType callback when declaring their class through
the JSC API.

  • API/JSCallbackFunction.cpp:

(JSC::JSCallbackFunction::toStringCallback):
(JSC::JSCallbackFunction::valueOfCallback):

  • API/JSCallbackFunction.h:
  • API/JSClassRef.cpp:

(OpaqueJSClass::prototype):

  • API/tests/testapi.js:
Location:
trunk/Source/JavaScriptCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/JSCallbackFunction.cpp

    r96164 r96627  
    3131#include "CodeBlock.h"
    3232#include "ExceptionHelpers.h"
     33#include "JSCallbackObject.h"
    3334#include "JSFunction.h"
    3435#include "FunctionPrototype.h"
     
    8990}
    9091
     92JSValueRef JSCallbackFunction::toStringCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef* exception)
     93{
     94    JSObject* object = toJS(thisObject);
     95    if (object->inherits(&JSCallbackObject<JSNonFinalObject>::s_info))
     96        return static_cast<JSCallbackObject<JSNonFinalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeString, exception);
     97    if (object->inherits(&JSCallbackObject<JSGlobalObject>::s_info))
     98        return static_cast<JSCallbackObject<JSGlobalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeString, exception);
     99    return 0;
     100}
     101
     102JSValueRef JSCallbackFunction::valueOfCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef* exception)
     103{
     104    JSObject* object = toJS(thisObject);
     105    if (object->inherits(&JSCallbackObject<JSNonFinalObject>::s_info))
     106        return static_cast<JSCallbackObject<JSNonFinalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeNumber, exception);
     107    if (object->inherits(&JSCallbackObject<JSGlobalObject>::s_info))
     108        return static_cast<JSCallbackObject<JSGlobalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeNumber, exception);
     109    return 0;
     110}
     111
     112
    91113} // namespace JSC
  • trunk/Source/JavaScriptCore/API/JSCallbackFunction.h

    r96164 r96627  
    5656    }
    5757
     58    static JSValueRef toStringCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
     59    static JSValueRef valueOfCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
     60
    5861private:
    5962    virtual CallType getCallDataVirtual(CallData&);
  • trunk/Source/JavaScriptCore/API/JSClassRef.cpp

    r94701 r96627  
    244244     *  DerivedClass  |  DerivedClassPrototype
    245245     */
    246    
     246
     247    if (convertToType) {
     248        if (!prototypeClass)
     249            prototypeClass = OpaqueJSClass::create(&kJSClassDefinitionEmpty).leakRef();
     250        if (!prototypeClass->m_staticFunctions)
     251            prototypeClass->m_staticFunctions = new OpaqueJSClassStaticFunctionsTable;
     252        const Identifier& toString = exec->propertyNames().toString;
     253        const Identifier& valueOf = exec->propertyNames().valueOf;
     254        prototypeClass->m_staticFunctions->add(StringImpl::create(toString.characters(), toString.length()), new StaticFunctionEntry(&JSCallbackFunction::toStringCallback, 0));
     255        prototypeClass->m_staticFunctions->add(StringImpl::create(valueOf.characters(), valueOf.length()), new StaticFunctionEntry(&JSCallbackFunction::valueOfCallback, 0));
     256    }
     257
    247258    if (!prototypeClass)
    248259        return 0;
  • trunk/Source/JavaScriptCore/API/tests/testapi.js

    r87588 r96627  
    155155shouldBe("MyObject ? 1 : 0", true); // toBoolean
    156156shouldBe("+MyObject", 1); // toNumber
    157 shouldBe("(MyObject.toString())", "[object MyObject]"); // toString
    158 shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string
     157shouldBe("(Object.prototype.toString.call(MyObject))", "[object MyObject]"); // Object.prototype.toString
     158shouldBe("(MyObject.toString())", "MyObjectAsString"); // toString
     159shouldBe("String(MyObject)", "MyObjectAsString"); // toString
    159160shouldBe("MyObject - 0", 1); // toNumber
     161shouldBe("MyObject.valueOf()", 1); // valueOf
    160162
    161163shouldBe("typeof MyConstructor", "object");
  • trunk/Source/JavaScriptCore/ChangeLog

    r96613 r96627  
     12011-10-04  Mark Hahnenberg  <mhahnenberg@apple.com>
     2
     3        Implicitly add toString and valueOf to prototype when convertToType callback is provided
     4        https://bugs.webkit.org/show_bug.cgi?id=69156
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        Added callbacks for toString and valueOf which are implicitly added to a client object's
     9        prototype if they provide a convertToType callback when declaring their class through
     10        the JSC API.
     11
     12        * API/JSCallbackFunction.cpp:
     13        (JSC::JSCallbackFunction::toStringCallback):
     14        (JSC::JSCallbackFunction::valueOfCallback):
     15        * API/JSCallbackFunction.h:
     16        * API/JSClassRef.cpp:
     17        (OpaqueJSClass::prototype):
     18        * API/tests/testapi.js:
     19
    1202011-10-03  Jon Lee  <jonlee@apple.com>
    221
Note: See TracChangeset for help on using the changeset viewer.