Changeset 15469 in webkit for trunk/JavaScriptCore/API


Ignore:
Timestamp:
Jul 16, 2006, 3:17:04 PM (19 years ago)
Author:
ggaren
Message:

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):
Location:
trunk/JavaScriptCore/API
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/JSCallbackConstructor.cpp

    r15444 r15469  
    5555}
    5656
    57 void JSCallbackConstructor::setPrivate(void* data)
    58 {
    59     m_privateData = data;
    60 }
    61 
    62 void* JSCallbackConstructor::getPrivate()
    63 {
    64     return m_privateData;
    65 }
    66 
    6757} // namespace KJS
  • trunk/JavaScriptCore/API/JSCallbackConstructor.h

    r15376 r15469  
    4141    virtual JSObject* construct(ExecState*, const List &args);
    4242
    43     void setPrivate(void* data);
    44     void* getPrivate();
    45 
    4643    virtual const ClassInfo *classInfo() const { return &info; }
    4744    static const ClassInfo info;
     
    5148    JSCallbackConstructor(const JSCallbackConstructor&);
    5249   
    53     void* m_privateData;
    5450    JSObjectCallAsConstructorCallback m_callback;
    5551};
  • trunk/JavaScriptCore/API/JSCallbackFunction.cpp

    r15444 r15469  
    3434const ClassInfo JSCallbackFunction::info = { "CallbackFunction", &InternalFunctionImp::info, 0, 0 };
    3535
    36 JSCallbackFunction::JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback)
    37     : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()))
     36JSCallbackFunction::JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback, const Identifier& name)
     37    : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name)
    3838    , m_callback(callback)
    3939{
    40 }
    41 
    42 bool JSCallbackFunction::implementsCall() const
    43 {
    44     return true;
    4540}
    4641
     
    5853}
    5954
    60 void JSCallbackFunction::setPrivate(void* data)
    61 {
    62     m_privateData = data;
    63 }
    64 
    65 void* JSCallbackFunction::getPrivate()
    66 {
    67     return m_privateData;
    68 }
    69 
    7055} // namespace KJS
  • trunk/JavaScriptCore/API/JSCallbackFunction.h

    r15376 r15469  
    3737{
    3838public:
    39     JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback);
     39    JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback, const Identifier& name);
    4040
    41     virtual bool implementsCall() const;
    4241    virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List &args);
    43 
    44     void setPrivate(void* data);
    45     void* getPrivate();
    4642
    4743    virtual const ClassInfo *classInfo() const { return &info; }
     
    5248    JSCallbackFunction(const JSCallbackFunction&);
    5349   
    54     void* m_privateData;
    5550    JSObjectCallAsFunctionCallback m_callback;
    5651};
  • trunk/JavaScriptCore/API/JSCallbackObject.cpp

    r15468 r15469  
    2626
    2727#include "APICast.h"
     28#include "JSCallbackFunction.h"
    2829#include "JSCallbackObject.h"
    2930#include "JSStringRef.h"
     
    406407        if (__JSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) {
    407408            if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.ustring().rep())) {
    408                 JSValue* v = toJS(JSObjectMakeFunction(toRef(exec), entry->callAsFunction));
    409                 thisObj->putDirect(propertyName, v, entry->attributes);
    410                 return v;
     409                JSObject* o = new JSCallbackFunction(exec, entry->callAsFunction, propertyName);
     410                thisObj->putDirect(propertyName, o, entry->attributes);
     411                return o;
    411412            }
    412413        }
  • trunk/JavaScriptCore/API/JSObjectRef.cpp

    r15468 r15469  
    7676}
    7777
    78 JSObjectRef JSObjectMakeFunction(JSContextRef context, JSObjectCallAsFunctionCallback callAsFunction)
    79 {
    80     JSLock lock;
    81     ExecState* exec = toJS(context);
    82     return toRef(new JSCallbackFunction(exec, callAsFunction));
     78JSObjectRef JSObjectMakeFunction(JSContextRef context, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction)
     79{
     80    JSLock lock;
     81    ExecState* exec = toJS(context);
     82    Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
     83   
     84    return toRef(new JSCallbackFunction(exec, callAsFunction, nameID));
    8385}
    8486
     
    9597   
    9698    ExecState* exec = toJS(context);
    97     UString::Rep* nameRep = name ? toJS(name) : &UString::Rep::null;
    9899    UString::Rep* bodyRep = toJS(body);
    99100    UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null;
    100101   
    101     Identifier nameIdentifier = nameRep ? Identifier(nameRep) : Identifier("anonymous");
     102    Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous");
    102103   
    103104    List args;
     
    106107    args.append(jsString(UString(bodyRep)));
    107108
    108     JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameIdentifier, UString(sourceURLRep), startingLineNumber);
     109    JSObject* result = exec->dynamicInterpreter()->builtinFunction()->construct(exec, args, nameID, UString(sourceURLRep), startingLineNumber);
    109110    if (exec->hadException()) {
    110111        if (exception)
     
    220221        return static_cast<JSCallbackObject*>(jsObject)->getPrivate();
    221222   
    222     if (jsObject->inherits(&JSCallbackFunction::info))
    223         return static_cast<JSCallbackFunction*>(jsObject)->getPrivate();
    224    
    225     if (jsObject->inherits(&JSCallbackConstructor::info))
    226         return static_cast<JSCallbackConstructor*>(jsObject)->getPrivate();
    227    
    228223    return 0;
    229224}
     
    238233    }
    239234       
    240     if (jsObject->inherits(&JSCallbackFunction::info)) {
    241         static_cast<JSCallbackFunction*>(jsObject)->setPrivate(data);
    242         return true;
    243     }
    244        
    245     if (jsObject->inherits(&JSCallbackConstructor::info)) {
    246         static_cast<JSCallbackConstructor*>(jsObject)->setPrivate(data);
    247         return true;
    248     }
    249    
    250235    return false;
    251236}
  • trunk/JavaScriptCore/API/JSObjectRef.h

    r15468 r15469  
    387387@abstract Convenience method for creating a JavaScript function with a given callback as its implementation.
    388388@param context The execution context to use.
     389@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.
    389390@param callAsFunction The JSObjectCallAsFunctionCallback to invoke when the function is called.
    390391@result A JSObject that is an anonymous function. The object's prototype will be the default function prototype.
    391392*/
    392 JSObjectRef JSObjectMakeFunction(JSContextRef context, JSObjectCallAsFunctionCallback callAsFunction);
     393JSObjectRef JSObjectMakeFunction(JSContextRef context, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction);
    393394/*!
    394395@function
     
    404405@abstract Creates a function with a given script as its body.
    405406@param context The execution context to use.
    406 @param name A JSString containing the function's name. Pass NULL to create an anonymous function.
     407@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.
    407408@param parameterCount An integer count of the number of parameter names in parameterNames.
    408409@param parameterNames A JSString array containing the names of the function's parameters. Pass NULL if parameterCount is 0.
     
    501502@param object A JSObject whose private data you want to get.
    502503@result A void* that points to the object's private data, if the object has private data, otherwise NULL.
    503 @discussion JSObjectGetPrivate and JSObjectSetPrivate only work on objects created by JSObjectMake, JSObjectMakeFunction, and JSObjectMakeConstructor.
    504504*/
    505505void* JSObjectGetPrivate(JSObjectRef object);
     
    510510@param object A JSObject whose private data you want to set.
    511511@param data A void* that points to the object's private data.
    512 @result true if the set operation succeeds, otherwise false.
    513 @discussion JSObjectGetPrivate and JSObjectSetPrivate only work on objects created by JSObjectMake, JSObjectMakeFunction, and JSObjectMakeConstructor.
     512@result true if the object can store private data, otherwise false.
     513@discussion Only custom objects created with a JSClass can store private data.
    514514*/
    515515bool JSObjectSetPrivate(JSObjectRef object, void* data);
  • trunk/JavaScriptCore/API/minidom.c

    r15465 r15469  
    4141   
    4242    JSStringRef printIString = JSStringCreateWithUTF8CString("print");
    43     JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunction(context, print), kJSPropertyAttributeNone, NULL);
     43    JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunction(context, printIString, print), kJSPropertyAttributeNone, NULL);
    4444    JSStringRelease(printIString);
    4545   
  • trunk/JavaScriptCore/API/testapi.c

    r15468 r15469  
    571571
    572572    JSStringRef print = JSStringCreateWithUTF8CString("print");
    573     JSObjectRef printFunction = JSObjectMakeFunction(context, print_callAsFunction);
     573    JSObjectRef printFunction = JSObjectMakeFunction(context, print, print_callAsFunction);
    574574    JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL);
    575575    JSStringRelease(print);
    576576   
    577     assert(JSObjectSetPrivate(printFunction, (void*)1));
    578     assert(JSObjectGetPrivate(printFunction) == (void*)1);
     577    assert(!JSObjectSetPrivate(printFunction, (void*)1));
     578    assert(!JSObjectGetPrivate(printFunction));
    579579
    580580    JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor");
     
    583583    JSStringRelease(myConstructorIString);
    584584   
    585     assert(JSObjectSetPrivate(myConstructor, (void*)1));
    586     assert(JSObjectGetPrivate(myConstructor) == (void*)1);
     585    assert(!JSObjectSetPrivate(myConstructor, (void*)1));
     586    assert(!JSObjectGetPrivate(myConstructor));
    587587   
    588588    o = JSObjectMake(context, NULL, NULL);
Note: See TracChangeset for help on using the changeset viewer.