Changeset 15482 in webkit for trunk/JavaScriptCore/API


Ignore:
Timestamp:
Jul 17, 2006, 1:20:28 AM (19 years ago)
Author:
ggaren
Message:

Reviewed by Maciej.


  • Changed JSObjectMakeConstructor to JSObjectMakeConstructorWithCallback, to match JSObjectMakeFunctionWithCallback.


  • Added prototype parameter, so the generated constructor automatically works with hasInstance / instanceof


  • Moved hasInstance implementation from InternalFunctionImp to JSObject so that subclasses can inherit it without inheriting function-related baggage. More refactoring here would be good, but this seems like a good short-term solution.

(KJS::JSCallbackFunction::implementsHasInstance): override and return false,
because callback functions aren't constructors.

Location:
trunk/JavaScriptCore/API
Files:
11 edited

Legend:

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

    r15469 r15482  
    3838}
    3939
     40bool JSCallbackConstructor::implementsHasInstance() const
     41{
     42    return true;
     43}
     44
    4045bool JSCallbackConstructor::implementsConstruct() const
    4146{
  • trunk/JavaScriptCore/API/JSCallbackConstructor.h

    r15469 r15482  
    2929
    3030#include "JSObjectRef.h"
    31 #include "object.h"
     31#include <kjs/object.h>
    3232
    3333namespace KJS {
    34    
     34
    3535class JSCallbackConstructor : public JSObject
    3636{
     
    3838    JSCallbackConstructor(ExecState* exec, JSObjectCallAsConstructorCallback callback);
    3939   
     40    virtual bool implementsHasInstance() const;
     41   
    4042    virtual bool implementsConstruct() const;
    4143    virtual JSObject* construct(ExecState*, const List &args);
    42 
     44   
    4345    virtual const ClassInfo *classInfo() const { return &info; }
    4446    static const ClassInfo info;
  • trunk/JavaScriptCore/API/JSCallbackFunction.cpp

    r15469 r15482  
    4040}
    4141
     42// InternalFunctionImp mish-mashes constructor and function behavior -- we should
     43// refactor the code so this override isn't necessary
     44bool JSCallbackFunction::implementsHasInstance() const {
     45    return false;
     46}
     47
    4248JSValue* JSCallbackFunction::callAsFunction(ExecState* exec, JSObject* thisObj, const List &args)
    4349{
  • trunk/JavaScriptCore/API/JSCallbackFunction.h

    r15469 r15482  
    3939    JSCallbackFunction(ExecState* exec, JSObjectCallAsFunctionCallback callback, const Identifier& name);
    4040
     41    virtual bool implementsHasInstance() const;
    4142    virtual JSValue* callAsFunction(ExecState*, JSObject* thisObj, const List &args);
    4243
  • trunk/JavaScriptCore/API/JSNode.c

    r15481 r15482  
    178178}
    179179
    180 static JSObjectRef JSNode_prototype(JSContextRef context)
     180JSObjectRef JSNode_prototype(JSContextRef context)
    181181{
    182182    static JSObjectRef prototype;
  • trunk/JavaScriptCore/API/JSNode.h

    r15464 r15482  
    3232
    3333extern JSObjectRef JSNode_new(JSContextRef context, Node* node);
     34extern JSObjectRef JSNode_prototype(JSContextRef context);
    3435extern JSObjectRef JSNode_construct(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception);
    3536
  • trunk/JavaScriptCore/API/JSObjectRef.cpp

    r15481 r15482  
    8585}
    8686
    87 JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSObjectCallAsConstructorCallback callAsConstructor)
    88 {
    89     JSLock lock;
    90     ExecState* exec = toJS(ctx);
    91     return toRef(new JSCallbackConstructor(exec, callAsConstructor));
     87JSObjectRef JSObjectMakeConstructorWithCallback(JSContextRef ctx, JSValueRef prototype, JSObjectCallAsConstructorCallback callAsConstructor)
     88{
     89    JSLock lock;
     90    ExecState* exec = toJS(ctx);
     91    JSValue* jsPrototype = toJS(prototype);
     92   
     93    if (!jsPrototype)
     94        jsPrototype = exec->dynamicInterpreter()->builtinObjectPrototype();
     95   
     96    JSObject* constructor = new JSCallbackConstructor(exec, callAsConstructor);
     97    constructor->put(exec, prototypePropertyName, jsPrototype, DontEnum|DontDelete|ReadOnly);
     98    return toRef(constructor);
    9299}
    93100
  • trunk/JavaScriptCore/API/JSObjectRef.h

    r15481 r15482  
    389389@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.
    390390@param callAsFunction The JSObjectCallAsFunctionCallback to invoke when the function is called.
    391 @result A JSObject that is an anonymous function. The object's prototype will be the default function prototype.
     391@result A JSObject that is a function. The object's prototype will be the default function prototype.
    392392*/
    393393JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction);
     394
    394395/*!
    395396@function
    396397@abstract Convenience method for creating a JavaScript constructor with a given callback as its implementation.
    397398@param ctx The execution context to use.
     399@param prototype A JSValue to use as the constructor's .prototype property. This should be the same value your constructor will set as the prototype of the objects it constructs.
    398400@param callAsConstructor The JSObjectCallAsConstructorCallback to invoke when the constructor is used in a 'new' expression.
    399401@result A JSObject that is a constructor. The object's prototype will be the default object prototype.
    400402*/
    401 JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSObjectCallAsConstructorCallback callAsConstructor);
     403JSObjectRef JSObjectMakeConstructorWithCallback(JSContextRef ctx, JSValueRef prototype, JSObjectCallAsConstructorCallback callAsConstructor);
    402404
    403405/*!
  • trunk/JavaScriptCore/API/minidom.c

    r15481 r15482  
    4545   
    4646    JSStringRef node = JSStringCreateWithUTF8CString("Node");
    47     JSObjectSetProperty(context, globalObject, node, JSObjectMakeConstructor(context, JSNode_construct), kJSPropertyAttributeNone, NULL);
     47    JSObjectSetProperty(context, globalObject, node, JSObjectMakeConstructorWithCallback(context, JSNode_prototype(context), JSNode_construct), kJSPropertyAttributeNone, NULL);
    4848    JSStringRelease(node);
    4949   
  • trunk/JavaScriptCore/API/minidom.js

    r15473 r15482  
    241241    node.nodeType = 1;
    242242    shouldBe("node.nodeType", oldNodeType);
     243   
     244    shouldBe("node instanceof Node", true);
     245    shouldBe("new Object() instanceof Node", false);
     246   
     247    print(Node);
    243248
    244249    /*
  • trunk/JavaScriptCore/API/testapi.c

    r15481 r15482  
    610610
    611611    JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor");
    612     JSObjectRef myConstructor = JSObjectMakeConstructor(context, myConstructor_callAsConstructor);
     612    JSObjectRef myConstructor = JSObjectMakeConstructorWithCallback(context, NULL, myConstructor_callAsConstructor);
    613613    JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL);
    614614    JSStringRelease(myConstructorIString);
Note: See TracChangeset for help on using the changeset viewer.