Changeset 15437 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Jul 14, 2006, 3:39:58 PM (19 years ago)
Author:
ggaren
Message:

JavaScriptCore:

Reviewed by Maciej.


  • Implemented ref-counting of JSContexts by splitting into two datatypes: JSGlobalContext, which you can create/retain/release, and JSContext, which you can't.


Internally, you retain a JSGlobalContext/ExecState by retaining its
interpreter, which, in the case of a global ExecState, owns it.


  • Also made ~Interpreter() protected to catch places where Interpreter is manually deleted. (Can't make it private because some crazy fool decided it would be a good idea to subclass Interpreter in other frameworks. I pity da fool.)
  • API/APICast.h: (toJS): Added cast for new JSGlobalContext
  • API/JSStringRef.h: Changed vague "you must" language to more specific (but, ultimately, equally vague) "behavior is undefined if you don't" language. (KJS::Interpreter::Interpreter): Factored more common initialization into init()
  • kjs/interpreter.h: (KJS::Interpreter::ref): new (KJS::Interpreter::deref): new (KJS::Interpreter::refCount): new
  • kjs/testkjs.cpp: (doIt): Ref-count the interpreter.

JavaScriptGlue:

Reviewed by Maciej.


  • Updated JSInterpreter to work with Interpreter ref-counting in JavaScriptCore.

(JSInterpreter::JSInterpreter::~JSInterpreter): Now protected to catch
manual delete.

WebCore:

Reviewed by Maciej.

  • Updated ScriptInterpreter to work with Interpreter ref-counting in JavaScriptCore.

(KJS::ScriptInterpreter::~ScriptInterpreter): Now protected to catch
manual delete.

Location:
trunk/JavaScriptCore
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/APICast.h

    r15376 r15437  
    4141
    4242inline KJS::ExecState* toJS(JSContextRef c)
     43{
     44    return reinterpret_cast<KJS::ExecState*>(const_cast<__JSContext*>(c));
     45}
     46
     47inline KJS::ExecState* toJS(JSGlobalContextRef c)
    4348{
    4449    return reinterpret_cast<KJS::ExecState*>(c);
  • trunk/JavaScriptCore/API/JSBase.h

    r15428 r15437  
    3333
    3434/*! @typedef JSContextRef A JavaScript execution context. Holds the global object and other execution state. */
    35 typedef struct __JSContext* JSContextRef;
     35typedef const struct __JSContext* JSContextRef;
     36
     37/*! @typedef JSGlobalContextRef A global JavaScript execution context. A JSGlobalContext is a JSContext. */
     38typedef struct __JSContext* JSGlobalContextRef;
     39
    3640/*! @typedef JSString A UTF16 character buffer. The fundamental string representation in JavaScript. */
    3741typedef struct __JSString* JSStringRef;
     42
    3843/*! @typedef JSClassRef A JavaScript class. Used with JSObjectMake to construct objects with custom behavior. */
    3944typedef struct __JSClass* JSClassRef;
     45
    4046/*! @typedef JSPropertyListRef A JavaScript property list. Used for listing the properties in an object so they can be enumerated. */
    4147typedef struct __JSPropertyList* JSPropertyListRef;
     48
    4249/*! @typedef JSPropertyEnumeratorRef A JavaScript property enumerator. Used for enumerating the properties in an object. */
    4350typedef struct __JSPropertyEnumerator* JSPropertyEnumeratorRef;
     
    4855/*! @typedef JSValueRef A JavaScript value. The base type for all JavaScript values, and polymorphic functions on them. */
    4956typedef const struct __JSValue* JSValueRef;
     57
    5058/*! @typedef JSObjectRef A JavaScript object. A JSObject is a JSValue. */
    5159typedef struct __JSValue* JSObjectRef;
  • trunk/JavaScriptCore/API/JSContextRef.cpp

    r15428 r15437  
    3535using namespace KJS;
    3636
    37 JSContextRef JSContextCreate(JSClassRef globalObjectClass)
     37JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
    3838{
    3939    JSLock lock;
     
    4747
    4848    Interpreter* interpreter = new Interpreter(globalObject); // adds the built-in object prototype to the global object
    49     return toRef(interpreter->globalExec());
     49    JSGlobalContextRef context = reinterpret_cast<JSGlobalContextRef>(interpreter->globalExec());
     50    return JSGlobalContextRetain(context);
    5051}
    5152
    52 void JSContextDestroy(JSContextRef context)
     53JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef context)
    5354{
    5455    JSLock lock;
    5556    ExecState* exec = toJS(context);
    56     delete exec->dynamicInterpreter();
     57    exec->dynamicInterpreter()->ref();
     58    return context;
     59}
     60
     61void JSGlobalContextRelease(JSGlobalContextRef context)
     62{
     63    JSLock lock;
     64    ExecState* exec = toJS(context);
     65    exec->dynamicInterpreter()->deref();
    5766}
    5867
  • trunk/JavaScriptCore/API/JSContextRef.h

    r15428 r15437  
    3939/*!
    4040@function
    41 @abstract Creates a JavaScript execution context.
    42 @discussion JSContextCreate allocates a global object and populates it with all the
     41@abstract Creates a global JavaScript execution context.
     42@discussion JSGlobalContextCreate allocates a global object and populates it with all the
    4343 built-in JavaScript objects, such as Object, Function, String, and Array.
    44 @param globalObjectClass The class to use when creating the JSContext's global object.
    45  Pass NULL to use the default object class.
    46 @result A JSContext with a global object of class globalObjectClass.
     44@param globalObjectClass The class to use when creating the global object. Pass
     45 NULL to use the default object class.
     46@result A JSGlobalContext with a global object of class globalObjectClass.
    4747*/
    48 JSContextRef JSContextCreate(JSClassRef globalObjectClass);
     48JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass);
    4949
    5050/*!
    5151@function
    52 @abstract       Destroys a JavaScript execution context, freeing its resources.
    53 @param context  The JSContext to destroy.
     52@abstract Retains a global JavaScript execution context.
     53@param context The JSGlobalContext to retain.
     54@result A JSGlobalContext that is the same as context.
    5455*/
    55 void JSContextDestroy(JSContextRef context);
     56JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef context);
    5657
    5758/*!
    5859@function
    59 @abstract       Gets the global object of a JavaScript execution context.
    60 @param context  The JSContext whose global object you want to get.
    61 @result         context's global object.
     60@abstract Releases a global JavaScript execution context.
     61@param context The JSGlobalContext to release.
     62*/
     63void JSGlobalContextRelease(JSGlobalContextRef context);
     64
     65/*!
     66@function
     67@abstract Gets the global object of a JavaScript execution context.
     68@param context The JSContext whose global object you want to get.
     69@result context's global object.
    6270*/
    6371JSObjectRef JSContextGetGlobalObject(JSContextRef context);
  • trunk/JavaScriptCore/API/JSStringRef.h

    r15400 r15437  
    111111@param string The source JSString.
    112112@param buffer The destination byte buffer into which to copy a null-terminated
    113  UTF8 string representation of string. The buffer must be at least bufferSize
    114  bytes in size. On return, buffer contains a UTF8 string representation of string.
    115  If bufferSize is too small, buffer will contain only partial results.
     113 UTF8 representation of string. On return, buffer contains a UTF8 string
     114 representation of string. If bufferSize is too small, buffer will contain only
     115 partial results. If buffer is not at least bufferSize bytes in size,
     116 behavior is undefined.
    116117@param bufferSize The size of the external buffer in bytes.
    117118@result The number of bytes written into buffer (including the null-terminator byte).
  • trunk/JavaScriptCore/API/minidom.c

    r15404 r15437  
    3737    UNUSED_PARAM(argv);
    3838   
    39     JSContextRef context = JSContextCreate(NULL);
     39    JSGlobalContextRef context = JSGlobalContextCreate(NULL);
    4040    JSObjectRef globalObject = JSContextGetGlobalObject(context);
    4141   
     
    7474#endif
    7575   
    76     JSContextDestroy(context);
     76    JSGlobalContextRelease(context);
    7777    printf("PASS: Program exited normally.\n");
    7878    return 0;
  • trunk/JavaScriptCore/API/testapi.c

    r15404 r15437  
    3535#include <math.h>
    3636
    37 static JSContextRef context = 0;
     37static JSGlobalContextRef context = 0;
    3838
    3939static void assertEqualsAsBoolean(JSValueRef value, bool expectedValue)
     
    302302    UNUSED_PARAM(argv);
    303303   
    304     context = JSContextCreate(NULL);
     304    context = JSGlobalContextCreate(NULL);
    305305   
    306306    JSObjectRef globalObject = JSContextGetGlobalObject(context);
     
    605605    JSStringRelease(badSyntax);
    606606   
    607     JSContextDestroy(context);
     607    JSGlobalContextRelease(context);
    608608    printf("PASS: Program exited normally.\n");
    609609    return 0;
  • trunk/JavaScriptCore/ChangeLog

    r15434 r15437  
     12006-07-14  Geoffrey Garen  <[email protected]>
     2
     3        Reviewed by Maciej.
     4       
     5        - Implemented ref-counting of JSContexts by splitting into two datatypes:
     6        JSGlobalContext, which you can create/retain/release, and JSContext, which
     7        you can't.
     8       
     9        Internally, you retain a JSGlobalContext/ExecState by retaining its
     10        interpreter, which, in the case of a global ExecState, owns it.
     11       
     12        - Also made ~Interpreter() protected to catch places where Interpreter
     13        is manually deleted. (Can't make it private because some crazy fool
     14        decided it would be a good idea to subclass Interpreter in other frameworks.
     15        I pity da fool.)
     16
     17        * API/APICast.h:
     18        (toJS): Added cast for new JSGlobalContext
     19        * API/JSStringRef.h: Changed vague "you must" language to more specific
     20        (but, ultimately, equally vague) "behavior is undefined if you don't"
     21        language.
     22        (KJS::Interpreter::Interpreter): Factored more common initialization into
     23        init()
     24        * kjs/interpreter.h:
     25        (KJS::Interpreter::ref): new
     26        (KJS::Interpreter::deref): new
     27        (KJS::Interpreter::refCount): new
     28        * kjs/testkjs.cpp:
     29        (doIt): Ref-count the interpreter.
     30
    1312006-07-14  Maciej Stachowiak  <[email protected]>
    232
  • trunk/JavaScriptCore/JavaScriptCore.exp

    r15404 r15437  
    66_JSClassRelease
    77_JSClassRetain
    8 _JSContextCreate
    9 _JSContextDestroy
    108_JSContextGetGlobalObject
    119_JSEvaluateScript
    1210_JSGarbageCollect
     11_JSGlobalContextCreate
     12_JSGlobalContextRelease
     13_JSGlobalContextRetain
    1314_JSObjectCallAsConstructor
    1415_JSObjectCallAsFunction
  • trunk/JavaScriptCore/kjs/interpreter.cpp

    r15224 r15437  
    208208   
    209209Interpreter::Interpreter(JSObject* globalObject)
    210     : m_timeoutTime(0)
    211     , m_globalExec(this, 0)
     210    : m_globalExec(this, 0)
    212211    , m_globalObject(globalObject)
    213     , m_argumentsPropertyName(&argumentsPropertyName)
    214     , m_specialPrototypePropertyName(&specialPrototypePropertyName)
    215     , m_timeoutChecker(0)
    216     , m_timedOut(false)
    217     , m_startTimeoutCheckCount(0)
    218     , m_pauseTimeoutCheckCount(0)
    219212{
    220213    init();
     
    222215
    223216Interpreter::Interpreter()
    224     : m_timeoutTime(0)
    225     , m_globalExec(this, 0)
     217    : m_globalExec(this, 0)
    226218    , m_globalObject(new JSObject())
    227     , m_argumentsPropertyName(&argumentsPropertyName)
    228     , m_specialPrototypePropertyName(&specialPrototypePropertyName)
    229     , m_timeoutChecker(0)
    230     , m_timedOut(false)
    231     , m_startTimeoutCheckCount(0)
    232     , m_pauseTimeoutCheckCount(0)
    233219{
    234220    init();
     
    239225    JSLock lock;
    240226
     227    m_refCount = 0;
     228    m_timeoutTime = 0;
    241229    m_recursion = 0;
    242230    m_debugger= 0;
    243231    m_context = 0;
     232    m_timedOut = false;
     233    m_timeoutChecker = 0;
     234    m_startTimeoutCheckCount = 0;
     235    m_pauseTimeoutCheckCount = 0;
    244236    m_compatMode = NativeMode;
     237    m_argumentsPropertyName = &argumentsPropertyName;
     238    m_specialPrototypePropertyName = &specialPrototypePropertyName;
    245239
    246240    interpreterMap().set(m_globalObject, this);
  • trunk/JavaScriptCore/kjs/interpreter.h

    r15163 r15437  
    7575     */
    7676    Interpreter();
    77     virtual ~Interpreter();
    7877
    7978    /**
     
    338337    bool checkTimeout();
    339338   
     339    void ref() { ++m_refCount; }
     340    void deref() { if (--m_refCount <= 0) delete this; }
     341    int refCount() const { return m_refCount; }
     342   
    340343protected:
     344    virtual ~Interpreter(); // only deref should delete us
    341345    virtual bool shouldInterruptScript() const { return true; }
     346
    342347    long m_timeoutTime;
    343348
     
    359364     */
    360365    Interpreter operator=(const Interpreter&);
     366   
     367    int m_refCount;
    361368   
    362369    ExecState m_globalExec;
  • trunk/JavaScriptCore/kjs/testkjs.cpp

    r14457 r15437  
    213213
    214214  // create interpreter
    215   Interpreter interp(global);
     215  RefPtr<Interpreter> interp = new Interpreter(global);
    216216  // add debug() function
    217   global->put(interp.globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
     217  global->put(interp->globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
    218218  // add "print" for compatibility with the mozilla js shell
    219   global->put(interp.globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
     219  global->put(interp->globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
    220220  // add "quit" for compatibility with the mozilla js shell
    221   global->put(interp.globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
     221  global->put(interp->globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
    222222  // add "gc" for compatibility with the mozilla js shell
    223   global->put(interp.globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
     223  global->put(interp->globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
    224224  // add "version" for compatibility with the mozilla js shell
    225   global->put(interp.globalExec(), "version", new TestFunctionImp(TestFunctionImp::Version, 1));
    226   global->put(interp.globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
     225  global->put(interp->globalExec(), "version", new TestFunctionImp(TestFunctionImp::Version, 1));
     226  global->put(interp->globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
    227227 
    228228  Interpreter::setShouldPrintExceptions(true);
     
    239239    }
    240240   
    241     Completion completion = interp.evaluate(fileName, 0, script);
     241    Completion completion = interp->evaluate(fileName, 0, script);
    242242    success = success && completion.complType() != Throw;
    243243    free(script);
Note: See TracChangeset for help on using the changeset viewer.