- Added names to functions.
- Removed GetPrivate/SetPrivate from callbackFunctions and callbackConstructors.
The private data idiom is that a JS object stores its native implementation
as private data. For functions and constructors, the native implementation is nothing
more than the callback they already store, so supporting private data, too,
confuses the idiom. If you *really* want, you can still create a custom
function with private data.
* API/JSCallbackConstructor.cpp:
* API/JSCallbackConstructor.h:
* API/JSCallbackFunction.cpp:
(KJS::JSCallbackFunction::JSCallbackFunction):
* API/JSCallbackFunction.h:
* API/JSCallbackObject.cpp:
(KJS::JSCallbackObject::staticFunctionGetter):
* API/JSObjectRef.cpp:
(JSObjectMakeFunction):
(JSObjectMakeFunctionWithBody):
(JSObjectGetPrivate):
(JSObjectSetPrivate):
* API/JSObjectRef.h:
* API/minidom.c:
(main):
* API/testapi.c:
(main):
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@15469
268f45cc-cd09-0410-ab3c-
d52691b4dbfc
return toJS(m_callback(execRef, thisRef, argumentCount, arguments, toRef(exec->exceptionSlot())));
}
-void JSCallbackConstructor::setPrivate(void* data)
-{
- m_privateData = data;
-}
-
-void* JSCallbackConstructor::getPrivate()
-{
- return m_privateData;
-}
-
} // namespace KJS
virtual bool implementsConstruct() const;
virtual JSObject* construct(ExecState*, const List &args);
- void setPrivate(void* data);
- void* getPrivate();
-
virtual const ClassInfo *classInfo() const { return &info; }
static const ClassInfo info;
JSCallbackConstructor(); // prevent default construction
JSCallbackConstructor(const JSCallbackConstructor&);
- void* m_privateData;
JSObjectCallAsConstructorCallback m_callback;
};
const ClassInfo JSCallbackFunction::info = { "CallbackFunction", &InternalFunctionImp::info, 0, 0 };
-JSCallbackFunction::JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback)
- : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()))
+JSCallbackFunction::JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback, const Identifier& name)
+ : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name)
, m_callback(callback)
{
}
-bool JSCallbackFunction::implementsCall() const
-{
- return true;
-}
-
JSValue* JSCallbackFunction::callAsFunction(ExecState* exec, JSObject* thisObj, const List &args)
{
JSContextRef execRef = toRef(exec);
return toJS(m_callback(execRef, thisRef, thisObjRef, argumentCount, arguments, toRef(exec->exceptionSlot())));
}
-void JSCallbackFunction::setPrivate(void* data)
-{
- m_privateData = data;
-}
-
-void* JSCallbackFunction::getPrivate()
-{
- return m_privateData;
-}
-
} // namespace KJS
class JSCallbackFunction : public InternalFunctionImp
{
public:
- JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback);
+ JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback, const Identifier& name);
- virtual bool implementsCall() const;
virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List &args);
- void setPrivate(void* data);
- void* getPrivate();
-
virtual const ClassInfo *classInfo() const { return &info; }
static const ClassInfo info;
JSCallbackFunction(); // prevent default construction
JSCallbackFunction(const JSCallbackFunction&);
- void* m_privateData;
JSObjectCallAsFunctionCallback m_callback;
};
*/
#include "APICast.h"
+#include "JSCallbackFunction.h"
#include "JSCallbackObject.h"
#include "JSStringRef.h"
#include "JSClassRef.h"
for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass) {
if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
- JSValue* v = toJS(JSObjectMakeFunction(toRef(exec), entry->callAsFunction));
- thisObj->putDirect(propertyName, v, entry->attributes);
- return v;
+ JSObject* o = new JSCallbackFunction(exec, entry->callAsFunction, propertyName);
+ thisObj->putDirect(propertyName, o, entry->attributes);
+ return o;
}
}
}
return toRef(new JSCallbackObject(context, jsClass, jsPrototype));
}
-JSObjectRef JSObjectMakeFunction(JSContextRef context, JSObjectCallAsFunctionCallback callAsFunction)
+JSObjectRef JSObjectMakeFunction(JSContextRef context, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
{
JSLock lock;
ExecState* exec = toJS(context);
- return toRef(new JSCallbackFunction(exec, callAsFunction));
+ Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
+
+ return toRef(new JSCallbackFunction(exec, callAsFunction, nameID));
}
JSObjectRef JSObjectMakeConstructor(JSContextRef context, JSObjectCallAsConstructorCallback callAsConstructor)
JSLock lock;
ExecState* exec = toJS(context);
- UString::Rep* nameRep = name ? toJS(name) : &UString::Rep::null;
UString::Rep* bodyRep = toJS(body);
UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null;
- Identifier nameIdentifier = nameRep ? Identifier(nameRep) : Identifier("anonymous");
+ Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
List args;
for (unsigned i = 0; i < parameterCount; i++)
args.append(jsString(UString(toJS(parameterNames[i]))));
args.append(jsString(UString(bodyRep)));
- JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameIdentifier, UString(sourceURLRep), startingLineNumber);
+ JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameID, UString(sourceURLRep), startingLineNumber);
if (exec->hadException()) {
if (exception)
*exception = toRef(exec->exception());
if (jsObject->inherits(&JSCallbackObject::info))
return static_cast<JSCallbackObject*>(jsObject)->getPrivate();
- if (jsObject->inherits(&JSCallbackFunction::info))
- return static_cast<JSCallbackFunction*>(jsObject)->getPrivate();
-
- if (jsObject->inherits(&JSCallbackConstructor::info))
- return static_cast<JSCallbackConstructor*>(jsObject)->getPrivate();
-
return 0;
}
return true;
}
- if (jsObject->inherits(&JSCallbackFunction::info)) {
- static_cast<JSCallbackFunction*>(jsObject)->setPrivate(data);
- return true;
- }
-
- if (jsObject->inherits(&JSCallbackConstructor::info)) {
- static_cast<JSCallbackConstructor*>(jsObject)->setPrivate(data);
- return true;
- }
-
return false;
}
@function
@abstract Convenience method for creating a JavaScript function with a given callback as its implementation.
@param context The execution context to use.
+@param name A JSString containing the function's name. This will be used when converting the function to string. Pass NULL to create an anonymous function.
@param callAsFunction The JSObjectCallAsFunctionCallback to invoke when the function is called.
@result A JSObject that is an anonymous function. The object's prototype will be the default function prototype.
*/
-JSObjectRef JSObjectMakeFunction(JSContextRef context, JSObjectCallAsFunctionCallback callAsFunction);
+JSObjectRef JSObjectMakeFunction(JSContextRef context, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction);
/*!
@function
@abstract Convenience method for creating a JavaScript constructor with a given callback as its implementation.
@function
@abstract Creates a function with a given script as its body.
@param context The execution context to use.
-@param name A JSString containing the function's name. Pass NULL to create an anonymous function.
+@param name A JSString containing the function's name. This will be used when converting the function to string. Pass NULL to create an anonymous function.
@param parameterCount An integer count of the number of parameter names in parameterNames.
@param parameterNames A JSString array containing the names of the function's parameters. Pass NULL if parameterCount is 0.
@param body A JSString containing the script to use as the function's body.
@abstract Gets a pointer to private data from an object.
@param object A JSObject whose private data you want to get.
@result A void* that points to the object's private data, if the object has private data, otherwise NULL.
-@discussion JSObjectGetPrivate and JSObjectSetPrivate only work on objects created by JSObjectMake, JSObjectMakeFunction, and JSObjectMakeConstructor.
*/
void* JSObjectGetPrivate(JSObjectRef object);
@abstract Sets a pointer to private data on an object.
@param object A JSObject whose private data you want to set.
@param data A void* that points to the object's private data.
-@result true if the set operation succeeds, otherwise false.
-@discussion JSObjectGetPrivate and JSObjectSetPrivate only work on objects created by JSObjectMake, JSObjectMakeFunction, and JSObjectMakeConstructor.
+@result true if the object can store private data, otherwise false.
+@discussion Only custom objects created with a JSClass can store private data.
*/
bool JSObjectSetPrivate(JSObjectRef object, void* data);
JSObjectRef globalObject = JSContextGetGlobalObject(context);
JSStringRef printIString = JSStringCreateWithUTF8CString("print");
- JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunction(context, print), kJSPropertyAttributeNone, NULL);
+ JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunction(context, printIString, print), kJSPropertyAttributeNone, NULL);
JSStringRelease(printIString);
JSStringRef node = JSStringCreateWithUTF8CString("Node");
JSStringRelease(string);
JSStringRef print = JSStringCreateWithUTF8CString("print");
- JSObjectRef printFunction = JSObjectMakeFunction(context, print_callAsFunction);
+ JSObjectRef printFunction = JSObjectMakeFunction(context, print, print_callAsFunction);
JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL);
JSStringRelease(print);
- assert(JSObjectSetPrivate(printFunction, (void*)1));
- assert(JSObjectGetPrivate(printFunction) == (void*)1);
+ assert(!JSObjectSetPrivate(printFunction, (void*)1));
+ assert(!JSObjectGetPrivate(printFunction));
JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor");
JSObjectRef myConstructor = JSObjectMakeConstructor(context, myConstructor_callAsConstructor);
JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL);
JSStringRelease(myConstructorIString);
- assert(JSObjectSetPrivate(myConstructor, (void*)1));
- assert(JSObjectGetPrivate(myConstructor) == (void*)1);
+ assert(!JSObjectSetPrivate(myConstructor, (void*)1));
+ assert(!JSObjectGetPrivate(myConstructor));
o = JSObjectMake(context, NULL, NULL);
JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(1), kJSPropertyAttributeNone, NULL);
+2006-07-16 Geoffrey Garen <ggaren@apple.com>
+
+ Reviewed by Maciej.
+
+ - Added names to functions.
+
+ - Removed GetPrivate/SetPrivate from callbackFunctions and callbackConstructors.
+ The private data idiom is that a JS object stores its native implementation
+ as private data. For functions and constructors, the native implementation is nothing
+ more than the callback they already store, so supporting private data, too,
+ confuses the idiom. If you *really* want, you can still create a custom
+ function with private data.
+
+ * API/JSCallbackConstructor.cpp:
+ * API/JSCallbackConstructor.h:
+ * API/JSCallbackFunction.cpp:
+ (KJS::JSCallbackFunction::JSCallbackFunction):
+ * API/JSCallbackFunction.h:
+ * API/JSCallbackObject.cpp:
+ (KJS::JSCallbackObject::staticFunctionGetter):
+ * API/JSObjectRef.cpp:
+ (JSObjectMakeFunction):
+ (JSObjectMakeFunctionWithBody):
+ (JSObjectGetPrivate):
+ (JSObjectSetPrivate):
+ * API/JSObjectRef.h:
+ * API/minidom.c:
+ (main):
+ * API/testapi.c:
+ (main):
+
2006-07-15 Maciej Stachowiak <mjs@apple.com>
Reviewed by Darin.