/*
 * The contents of this file are subject to the Netscape Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/NPL/

 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.

 * The Original Code is Rhino code, released
 * May 6, 1999.

 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 * Rights Reserved.

 * Contributor(s): 

 * Alternatively, the contents of this file may be used under the
 * terms of the GNU Public License (the "GPL"), in which case the
 * provisions of the GPL are applicable instead of those above.
 * If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use your
 * version of this file under the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this
 * file under either the NPL or the GPL.
 */

#ifndef _JS_SCRIPTABLE_OBJECT_H_
#define _JS_SCRIPTABLE_OBJECT_H_

#include "js-scriptable.h"
#include "js-function.h"
#include "js-flattenedobject.h"

/* Classes */
class JSFunctionObject;
class JSScriptableObject: public JSScriptable { /* GC collectable */
    protected:
        class Slot { /* GC collectable */
            public:
                static const int HAS_GETTER  = 0x01;
                static const int HAS_SETTER  = 0x02;

                int             intKey;
                jsString_t      stringKey;
                jsVal_t         value;
                short           attributes;
                short           flags;

            public:
                void *operator new(size_t size, void *placement) {
                    JSContext *cx = (JSContext *) placement;

                    return JS_NewGCObj(cx, size);
                }
        } ;
        class GetterSlot: public Slot {
            public:
                jsGetterProc_t  getter;
                jsSetterProc_t  setter;
        } ;
#ifdef COMPACT_MEM
        class SlotsSet {
            public:
                Slot            **slots;
                int             slotsLength;
                int             count;

                /* getter's cache */
                jsString_t      lastName;
                int             lastHash;
                jsVal_t         lastValue;
        } ;
#endif

    public:
        typedef void    (*jsFinishInit_t)(JSContext *cx, JSScriptable *scope,
                                JSFunctionObject *ctor, JSScriptable *proto);
        typedef jsVal_t (*jsConstructor_t)(JSContext *cx,
                                int numArgs, jsVal_t *args,
                                JSFunction *ctorObj, jsBool_t inNewExpr);
        typedef jsVal_t (*jsCFunction_t)(JSContext *cx, JSScriptable *thisObj,
                                int numArgs, jsVal_t *args,
                                JSFunction *funObj);

        typedef struct _jsFunctionRep {
            jsString_t    name;
            jsCFunction_t       cfunc;
            short               length;
        } jsFunction_t;

    protected:
        static  const int       SLOT_NOT_FOUND = -1;
        static  const Slot      *REMOVED;

        JSScriptable    *parent;
        JSScriptable    *prototype;
#ifdef COMPACT_MEM
        SlotsSet        *slotsSet;
#else
        Slot            **slots;
        int             slotsLength;
        int             count;

        /* getter's cache */
        jsString_t      lastName;
        int             lastHash;
        jsVal_t         lastValue;
#endif

    public:
        static  const jsString_t        className;

    protected:
        static  int     getHashCode(jsString_t string);

        int     getSlot(jsString_t name, int index);
        void    grow(JSContext *cx);
        int     addSlot(JSContext *cx, jsString_t name, int index,
                        jsBool_t getterSlot);
        void    removeSlot(JSContext *cx, jsString_t name, int index);
        int     getSlotToSet(JSContext *cx, jsString_t name, int index,
                        jsBool_t getterSlot);
        jsVal_t *getIds(JSContext *cx, int *numIds, jsBool_t getAll);
        jsVal_t getProperty(jsString_t name);
        JSFunction *
                getFunctionProperty(JSContext *cx, JSFlattenedObject *f,
                        jsString_t name);

    public:
        static  void    classInitialize();
        static  void    defineClass(JSContext *cx, JSScriptable *scope,
                                JSScriptable *proto,
                                jsConstructor_t constructor,
                                jsFinishInit_t finishInit);
        static  void    defineClass(JSContext *cx, JSScriptable *scope,
                                JSScriptable *proto,
                                ::jsConstructor_t constructor);

        static  JSScriptable *
                        getClassPrototype(JSContext *cx, JSScriptable *scope,
                                jsString_t className);
        static  JSScriptable *
                        getFunctionPrototype(JSContext *cx,
                                JSScriptable *scope);
        static  JSScriptable *
                        getObjectPrototype(JSContext *cx,
                                JSScriptable *scope);
        static  JSScriptable *
                        getTopLevelScope(JSContext *cx, JSScriptable *obj);

        JSScriptableObject();

        void    defineFunctionProperties(JSContext *cx,
                        const jsString_t clazz, int numFunctions,
                        jsFunction_t *functions, int attribute);
        void    defineFunctionProperties(JSContext *cx, jsString_t clazz,
                        int numFunctions, ::jsFunction_t *functions);
        void    defineProperty(JSContext *cx, jsString_t name, jsVal_t value,
                        int attributes);
        void    defineProperty(JSContext *cx, jsString_t name,
                        jsGetterProc_t  getter, jsSetterProc_t  setter,
                        int attributes);
        void    defineProperties(JSContext *cx, int numProperties,
                        jsProperty_t *properties);
        int     getAttributes(JSContext *cx, jsString_t name,
                        JSScriptable *start);
        int     getAttributes(JSContext *cx, int index, JSScriptable *start);
        void    setAttributes(JSContext *cx, jsString_t name,
                        JSScriptable *start, int attributes);
        void    setAttributes(JSContext *cx, int index, JSScriptable *start,
                        int attributes);

        /* instanceof method */
        jsBool_t
                isInstanceOf(const jsString_t className);

        /* scriptable methods */
        jsBool_t
                get(JSContext *cx, jsString_t name, JSScriptable *start,
                        jsVal_t *value);
        jsBool_t
                get(JSContext *cx, int index, JSScriptable *start,
                        jsVal_t *value);
        jsBool_t
                has(JSContext *cx, jsString_t name, JSScriptable *start);
        jsBool_t
                has(JSContext *cx, int index, JSScriptable *start);
        void    put(JSContext *cx, jsString_t name, JSScriptable *start,
                        jsVal_t value);
        void    put(JSContext *cx, int index, JSScriptable *start,
                        jsVal_t value);
        void    del(JSContext *cx, jsString_t name);
        void    del(JSContext *cx, int index);
        JSScriptable *
                getPrototype(JSContext *cx);
        void    setPrototype(JSContext *cx, JSScriptable *prototype);
        JSScriptable *
                getParentScope(JSContext *cx);
        void    setParentScope(JSContext *cx, JSScriptable *parent);
        jsVal_t *getIds(JSContext *cx, int *numIds);
        jsVal_t getDefaultValue(JSContext *cx, jsType_t hint);
        jsBool_t
                hasInstance(JSContext *cx, JSScriptable *instance);

        /* gc methods */
        void    mark(JSContext *cx);
        void    removeAllProperties();
};

#endif /* _JS_SCRIPTABLE_OBJECT_H_ */

