Commit ff057a67 authored by mhahnenberg@apple.com's avatar mhahnenberg@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:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@96627 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 6cabcb62
......@@ -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 @@ CallType JSCallbackFunction::getCallData(JSCell*, CallData& callData)
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
......@@ -55,6 +55,9 @@ public:
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&);
......
......@@ -243,7 +243,18 @@ JSObject* OpaqueJSClass::prototype(ExecState* exec)
* | | |
* 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;
......
......@@ -154,9 +154,11 @@ shouldBe("MyObject()", undefined);
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);
......
2011-10-04 Mark Hahnenberg <mhahnenberg@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 <jonlee@apple.com>
Extend DOM WheelEvent to differentiate between physical and logical scroll directions
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment