Title: [96627] trunk/Source/_javascript_Core
Revision
96627
Author
mhahnenb...@apple.com
Date
2011-10-04 12:02:03 -0700 (Tue, 04 Oct 2011)

Log 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:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/JSCallbackFunction.cpp (96626 => 96627)


--- trunk/Source/_javascript_Core/API/JSCallbackFunction.cpp	2011-10-04 18:58:47 UTC (rev 96626)
+++ trunk/Source/_javascript_Core/API/JSCallbackFunction.cpp	2011-10-04 19:02:03 UTC (rev 96627)
@@ -30,6 +30,7 @@
 #include "APICast.h"
 #include "CodeBlock.h"
 #include "ExceptionHelpers.h"
+#include "JSCallbackObject.h"
 #include "JSFunction.h"
 #include "FunctionPrototype.h"
 #include <runtime/JSGlobalObject.h>
@@ -88,4 +89,25 @@
     return CallTypeHost;
 }
 
+JSValueRef JSCallbackFunction::toStringCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef* exception)
+{
+    JSObject* object = toJS(thisObject);
+    if (object->inherits(&JSCallbackObject<JSNonFinalObject>::s_info))
+        return static_cast<JSCallbackObject<JSNonFinalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeString, exception);
+    if (object->inherits(&JSCallbackObject<JSGlobalObject>::s_info))
+        return static_cast<JSCallbackObject<JSGlobalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeString, exception);
+    return 0;
+}
+
+JSValueRef JSCallbackFunction::valueOfCallback(JSContextRef ctx, JSObjectRef, JSObjectRef thisObject, size_t, const JSValueRef[], JSValueRef* exception)
+{
+    JSObject* object = toJS(thisObject);
+    if (object->inherits(&JSCallbackObject<JSNonFinalObject>::s_info))
+        return static_cast<JSCallbackObject<JSNonFinalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeNumber, exception);
+    if (object->inherits(&JSCallbackObject<JSGlobalObject>::s_info))
+        return static_cast<JSCallbackObject<JSGlobalObject>*>(object)->classRef()->convertToType(ctx, thisObject, kJSTypeNumber, exception);
+    return 0;
+}
+
+
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/API/JSCallbackFunction.h (96626 => 96627)


--- trunk/Source/_javascript_Core/API/JSCallbackFunction.h	2011-10-04 18:58:47 UTC (rev 96626)
+++ trunk/Source/_javascript_Core/API/JSCallbackFunction.h	2011-10-04 19:02:03 UTC (rev 96627)
@@ -55,6 +55,9 @@
         return Structure::create(globalData, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info); 
     }
 
+    static JSValueRef toStringCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
+    static JSValueRef valueOfCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
+
 private:
     virtual CallType getCallDataVirtual(CallData&);
     static CallType getCallData(JSCell*, CallData&);

Modified: trunk/Source/_javascript_Core/API/JSClassRef.cpp (96626 => 96627)


--- trunk/Source/_javascript_Core/API/JSClassRef.cpp	2011-10-04 18:58:47 UTC (rev 96626)
+++ trunk/Source/_javascript_Core/API/JSClassRef.cpp	2011-10-04 19:02:03 UTC (rev 96627)
@@ -243,7 +243,18 @@
      *       |        |          |
      *  DerivedClass  |  DerivedClassPrototype
      */
-    
+
+    if (convertToType) {
+        if (!prototypeClass)
+            prototypeClass = OpaqueJSClass::create(&kJSClassDefinitionEmpty).leakRef();
+        if (!prototypeClass->m_staticFunctions)
+            prototypeClass->m_staticFunctions = new OpaqueJSClassStaticFunctionsTable;
+        const Identifier& toString = exec->propertyNames().toString;
+        const Identifier& valueOf = exec->propertyNames().valueOf;
+        prototypeClass->m_staticFunctions->add(StringImpl::create(toString.characters(), toString.length()), new StaticFunctionEntry(&JSCallbackFunction::toStringCallback, 0));
+        prototypeClass->m_staticFunctions->add(StringImpl::create(valueOf.characters(), valueOf.length()), new StaticFunctionEntry(&JSCallbackFunction::valueOfCallback, 0));
+    }
+
     if (!prototypeClass)
         return 0;
 

Modified: trunk/Source/_javascript_Core/API/tests/testapi.js (96626 => 96627)


--- trunk/Source/_javascript_Core/API/tests/testapi.js	2011-10-04 18:58:47 UTC (rev 96626)
+++ trunk/Source/_javascript_Core/API/tests/testapi.js	2011-10-04 19:02:03 UTC (rev 96627)
@@ -154,9 +154,11 @@
 shouldBe("typeof myObject", "object");
 shouldBe("MyObject ? 1 : 0", true); // toBoolean
 shouldBe("+MyObject", 1); // toNumber
-shouldBe("(MyObject.toString())", "[object MyObject]"); // toString
-shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string
+shouldBe("(Object.prototype.toString.call(MyObject))", "[object MyObject]"); // Object.prototype.toString
+shouldBe("(MyObject.toString())", "MyObjectAsString"); // toString
+shouldBe("String(MyObject)", "MyObjectAsString"); // toString
 shouldBe("MyObject - 0", 1); // toNumber
+shouldBe("MyObject.valueOf()", 1); // valueOf
 
 shouldBe("typeof MyConstructor", "object");
 constructedObject = new MyConstructor(1);

Modified: trunk/Source/_javascript_Core/ChangeLog (96626 => 96627)


--- trunk/Source/_javascript_Core/ChangeLog	2011-10-04 18:58:47 UTC (rev 96626)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-10-04 19:02:03 UTC (rev 96627)
@@ -1,3 +1,22 @@
+2011-10-04  Mark Hahnenberg  <mhahnenb...@apple.com>
+
+        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:
+
 2011-10-03  Jon Lee  <jon...@apple.com>
 
         Extend DOM WheelEvent to differentiate between physical and logical scroll directions
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to