2000-06-21 23:57:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* 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 oqr
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the JavaScript 2 Prototype.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 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 jsclasses_h
|
|
|
|
#define jsclasses_h
|
|
|
|
|
|
|
|
#include "jstypes.h"
|
|
|
|
|
|
|
|
namespace JavaScript {
|
|
|
|
namespace JSClasses {
|
|
|
|
|
2000-06-23 05:09:37 +00:00
|
|
|
using JSTypes::JSValue;
|
2000-06-23 23:25:55 +00:00
|
|
|
using JSTypes::JSObject;
|
2000-06-23 05:09:37 +00:00
|
|
|
using JSTypes::JSType;
|
2000-06-21 23:57:09 +00:00
|
|
|
using JSTypes::JSScope;
|
2000-06-24 02:51:03 +00:00
|
|
|
using ICG::ICodeModule;
|
2000-06-23 05:09:37 +00:00
|
|
|
|
|
|
|
struct JSSlot {
|
2000-06-24 02:51:03 +00:00
|
|
|
JSType* mType;
|
2000-06-23 05:09:37 +00:00
|
|
|
uint32 mIndex;
|
|
|
|
|
|
|
|
JSSlot() : mType(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(XP_MAC)
|
|
|
|
// copied from default template parameters in map.
|
|
|
|
typedef gc_allocator<std::pair<const String, JSSlot> > gc_slot_allocator;
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
typedef JSTypes::gc_map_allocator gc_slot_allocator;
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
typedef gc_allocator<JSSlot> gc_slot_allocator;
|
|
|
|
#endif
|
|
|
|
|
2000-06-23 23:25:55 +00:00
|
|
|
typedef std::map<String, JSSlot, std::less<const String>, gc_slot_allocator> JSSlots;
|
|
|
|
|
2000-06-21 23:57:09 +00:00
|
|
|
/**
|
|
|
|
* Represents a class in the JavaScript 2 (ECMA 4) language.
|
2000-06-23 05:09:37 +00:00
|
|
|
* Since a class defines a scope, and is defined in a scope,
|
|
|
|
* a new scope is created whose parent scope is the scope of
|
|
|
|
* class definition.
|
2000-06-21 23:57:09 +00:00
|
|
|
*/
|
2000-06-23 05:09:37 +00:00
|
|
|
class JSClass : public JSType {
|
2000-06-21 23:57:09 +00:00
|
|
|
protected:
|
|
|
|
JSClass* mSuperClass;
|
2000-06-23 05:09:37 +00:00
|
|
|
JSScope* mScope;
|
|
|
|
uint32 mSlotCount;
|
2000-06-23 23:25:55 +00:00
|
|
|
JSSlots mSlots;
|
2000-06-24 02:51:03 +00:00
|
|
|
ICodeModule* mConstructor;
|
|
|
|
// typedef std::vector<ICodeModule*, gc_allocator<ICodeModule*> > JSMethods;
|
|
|
|
// JSMethods mMethods;
|
2000-06-21 23:57:09 +00:00
|
|
|
public:
|
2000-06-23 05:09:37 +00:00
|
|
|
JSClass(JSScope* scope, const String& name, JSClass* superClass = 0)
|
2000-06-23 22:53:09 +00:00
|
|
|
: JSType(name, superClass), mSuperClass(superClass), mSlotCount(0)
|
2000-06-21 23:57:09 +00:00
|
|
|
{
|
2000-06-23 05:09:37 +00:00
|
|
|
mScope = new JSScope(scope);
|
|
|
|
setProperty(widenCString("methods"), JSValue(mScope));
|
2000-06-21 23:57:09 +00:00
|
|
|
}
|
|
|
|
|
2000-06-24 02:51:03 +00:00
|
|
|
JSClass* getSuperClass()
|
|
|
|
{
|
|
|
|
return mSuperClass;
|
|
|
|
}
|
2000-06-23 05:09:37 +00:00
|
|
|
|
2000-06-24 02:51:03 +00:00
|
|
|
JSScope* getScope()
|
|
|
|
{
|
|
|
|
return mScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setConstructor(ICodeModule* ctor)
|
|
|
|
{
|
|
|
|
mConstructor = ctor;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICodeModule* getConstructor()
|
|
|
|
{
|
|
|
|
return mConstructor;
|
|
|
|
}
|
2000-06-23 23:43:24 +00:00
|
|
|
|
2000-06-24 02:51:03 +00:00
|
|
|
JSSlot& addSlot(const String& name, JSType* type)
|
2000-06-23 05:09:37 +00:00
|
|
|
{
|
|
|
|
JSSlot& slot = mSlots[name];
|
|
|
|
ASSERT(slot.mType == 0);
|
|
|
|
slot.mType = type;
|
2000-06-23 23:25:55 +00:00
|
|
|
slot.mIndex = ++mSlotCount;
|
2000-06-23 05:09:37 +00:00
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSSlot& getSlot(const String& name)
|
|
|
|
{
|
|
|
|
return mSlots[name];
|
|
|
|
}
|
2000-06-23 23:25:55 +00:00
|
|
|
|
2000-06-24 00:50:59 +00:00
|
|
|
JSSlots& getSlots()
|
|
|
|
{
|
|
|
|
return mSlots;
|
|
|
|
}
|
|
|
|
|
2000-06-23 23:25:55 +00:00
|
|
|
bool hasSlot(const String& name)
|
|
|
|
{
|
|
|
|
return (mSlots.count(name) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 getSlotCount()
|
|
|
|
{
|
|
|
|
return mSlotCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents an instance of a JSClass.
|
|
|
|
*/
|
|
|
|
class JSInstance : public JSObject {
|
|
|
|
protected:
|
|
|
|
JSValue mSlots[1];
|
|
|
|
public:
|
|
|
|
void* operator new(size_t n, JSClass* thisClass)
|
|
|
|
{
|
|
|
|
return gc_base::operator new(thisClass->getSlotCount() * sizeof(JSValue) + n);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSInstance(JSClass* thisClass)
|
|
|
|
{
|
|
|
|
mSlots[0] = thisClass;
|
|
|
|
std::uninitialized_fill(&mSlots[1], &mSlots[1] + thisClass->getSlotCount(), JSValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass* getClass()
|
|
|
|
{
|
|
|
|
return static_cast<JSClass*>(mSlots[0].object);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue& operator[] (uint32 index)
|
|
|
|
{
|
|
|
|
return mSlots[index];
|
|
|
|
}
|
2000-06-24 00:50:59 +00:00
|
|
|
|
|
|
|
virtual void printProperties(Formatter& f)
|
|
|
|
{
|
|
|
|
JSObject::printProperties(f);
|
|
|
|
JSClass *thisClass = getClass();
|
|
|
|
JSSlots &slots = thisClass->getSlots();
|
|
|
|
f << "Slots:\n";
|
|
|
|
for (JSSlots::iterator i = slots.begin(), end = slots.end(); i != end; ++i) {
|
|
|
|
f << i->first << " : " << mSlots[i->second.mIndex] << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-21 23:57:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace JSClasses */
|
|
|
|
} /* namespace JavaScript */
|
|
|
|
|
|
|
|
#endif /* jsclasses_h */
|