2000-06-22 03:57:09 +04: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 09:09:37 +04:00
|
|
|
using JSTypes::JSValue;
|
2000-06-24 03:25:55 +04:00
|
|
|
using JSTypes::JSObject;
|
2000-06-23 09:09:37 +04:00
|
|
|
using JSTypes::JSType;
|
2000-06-22 03:57:09 +04:00
|
|
|
using JSTypes::JSScope;
|
2000-07-19 00:53:03 +04:00
|
|
|
using JSTypes::JSFunction;
|
2000-06-24 06:51:03 +04:00
|
|
|
using ICG::ICodeModule;
|
2000-06-23 09:09:37 +04:00
|
|
|
|
2000-07-08 05:08:29 +04:00
|
|
|
|
2000-06-23 09:09:37 +04:00
|
|
|
struct JSSlot {
|
2000-07-08 05:08:29 +04:00
|
|
|
typedef enum { kNoFlag = 0, kIsConstructor = 0x01 } SlotFlags; // <-- readonly, enumerable etc
|
|
|
|
|
2000-06-24 06:51:03 +04:00
|
|
|
JSType* mType;
|
2000-06-23 09:09:37 +04:00
|
|
|
uint32 mIndex;
|
2000-07-08 05:08:29 +04:00
|
|
|
SlotFlags mFlags;
|
2000-06-23 09:09:37 +04:00
|
|
|
|
2000-07-08 05:08:29 +04:00
|
|
|
JSSlot() : mType(0), mFlags(kNoFlag)
|
2000-06-23 09:09:37 +04:00
|
|
|
{
|
|
|
|
}
|
2000-07-08 05:08:29 +04:00
|
|
|
|
|
|
|
bool isConstructor() const { return (mFlags & kIsConstructor) != 0; }
|
2000-06-23 09:09:37 +04:00
|
|
|
};
|
|
|
|
|
2000-09-12 02:10:44 +04:00
|
|
|
typedef gc_map_allocator(JSSlot) gc_slot_allocator;
|
2000-07-19 00:53:03 +04:00
|
|
|
typedef std::map<String, JSSlot, std::less<const String>, gc_slot_allocator> JSSlots;
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::pair<String, JSFunction*> MethodEntry;
|
|
|
|
typedef std::vector<MethodEntry> JSMethods;
|
2000-06-24 03:25:55 +04:00
|
|
|
|
2000-06-22 03:57:09 +04:00
|
|
|
/**
|
|
|
|
* Represents a class in the JavaScript 2 (ECMA 4) language.
|
2000-06-23 09:09:37 +04: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-22 03:57:09 +04:00
|
|
|
*/
|
2000-06-23 09:09:37 +04:00
|
|
|
class JSClass : public JSType {
|
2000-06-22 03:57:09 +04:00
|
|
|
protected:
|
2000-06-23 09:09:37 +04:00
|
|
|
JSScope* mScope;
|
|
|
|
uint32 mSlotCount;
|
2000-06-24 03:25:55 +04:00
|
|
|
JSSlots mSlots;
|
2000-06-28 20:11:54 +04:00
|
|
|
uint32 mStaticCount;
|
|
|
|
JSSlots mStaticSlots;
|
|
|
|
JSValue* mStaticData;
|
2000-07-19 00:53:03 +04:00
|
|
|
JSMethods mMethods;
|
2000-06-22 03:57:09 +04:00
|
|
|
public:
|
2000-06-23 09:09:37 +04:00
|
|
|
JSClass(JSScope* scope, const String& name, JSClass* superClass = 0)
|
2000-06-25 02:07:00 +04:00
|
|
|
: JSType(name, superClass),
|
|
|
|
mScope(new JSScope(scope)),
|
2000-06-28 20:11:54 +04:00
|
|
|
mSlotCount(superClass ? superClass->mSlotCount : 0),
|
|
|
|
mStaticCount(0),
|
2000-07-08 05:08:29 +04:00
|
|
|
mStaticData(0)
|
2000-06-22 03:57:09 +04:00
|
|
|
{
|
2000-07-19 00:53:03 +04:00
|
|
|
if (superClass) {
|
|
|
|
// inherit superclass methods
|
|
|
|
JSMethods::iterator end = superClass->mMethods.end();
|
|
|
|
for (JSMethods::iterator i = superClass->mMethods.begin(); i != end; i++)
|
|
|
|
mMethods.push_back(*i);
|
|
|
|
}
|
2000-06-22 03:57:09 +04:00
|
|
|
}
|
|
|
|
|
2000-06-24 06:51:03 +04:00
|
|
|
JSClass* getSuperClass()
|
|
|
|
{
|
2000-06-28 23:46:13 +04:00
|
|
|
return static_cast<JSClass*>(mBaseType);
|
2000-06-24 06:51:03 +04:00
|
|
|
}
|
2000-06-23 09:09:37 +04:00
|
|
|
|
2000-06-24 06:51:03 +04:00
|
|
|
JSScope* getScope()
|
|
|
|
{
|
|
|
|
return mScope;
|
|
|
|
}
|
|
|
|
|
2000-06-28 20:11:54 +04:00
|
|
|
const JSSlot& defineSlot(const String& name, JSType* type)
|
2000-06-23 09:09:37 +04:00
|
|
|
{
|
|
|
|
JSSlot& slot = mSlots[name];
|
|
|
|
ASSERT(slot.mType == 0);
|
|
|
|
slot.mType = type;
|
2000-06-28 20:11:54 +04:00
|
|
|
slot.mIndex = mSlotCount++; // starts at 0.
|
2000-06-23 09:09:37 +04:00
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2000-09-12 02:10:44 +04:00
|
|
|
|
|
|
|
|
2000-06-28 20:11:54 +04:00
|
|
|
const JSSlot& getSlot(const String& name)
|
2000-06-23 09:09:37 +04:00
|
|
|
{
|
|
|
|
return mSlots[name];
|
|
|
|
}
|
2000-06-24 03:25:55 +04:00
|
|
|
|
2000-06-25 02:07:00 +04:00
|
|
|
bool hasSlot(const String& name)
|
2000-06-24 04:50:59 +04:00
|
|
|
{
|
2000-06-25 02:07:00 +04:00
|
|
|
return (mSlots.find(name) != mSlots.end());
|
2000-06-24 04:50:59 +04:00
|
|
|
}
|
|
|
|
|
2000-06-25 02:07:00 +04:00
|
|
|
JSSlots& getSlots()
|
2000-06-24 03:25:55 +04:00
|
|
|
{
|
2000-06-25 02:07:00 +04:00
|
|
|
return mSlots;
|
2000-06-24 03:25:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 getSlotCount()
|
|
|
|
{
|
|
|
|
return mSlotCount;
|
|
|
|
}
|
2000-06-27 07:42:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Define a static/class variable.
|
|
|
|
*/
|
2000-06-28 20:11:54 +04:00
|
|
|
const JSSlot& defineStatic(const String& name, JSType* type)
|
2000-06-27 07:42:42 +04:00
|
|
|
{
|
2000-06-28 20:11:54 +04:00
|
|
|
JSSlot& slot = mStaticSlots[name];
|
|
|
|
ASSERT(slot.mType == 0);
|
|
|
|
slot.mType = type;
|
|
|
|
slot.mIndex = mStaticCount++;
|
|
|
|
return slot;
|
|
|
|
}
|
2000-07-08 05:08:29 +04:00
|
|
|
|
|
|
|
const JSSlot& defineConstructor(const String& name)
|
|
|
|
{
|
|
|
|
JSSlot& slot = mStaticSlots[name];
|
|
|
|
ASSERT(slot.mType == 0);
|
|
|
|
slot.mType = &JSTypes::Function_Type;
|
|
|
|
slot.mIndex = mStaticCount++;
|
|
|
|
slot.mFlags = JSSlot::kIsConstructor;
|
|
|
|
return slot;
|
|
|
|
}
|
2000-06-28 20:11:54 +04:00
|
|
|
|
|
|
|
const JSSlot& getStatic(const String& name)
|
|
|
|
{
|
|
|
|
return mStaticSlots[name];
|
|
|
|
}
|
|
|
|
|
2000-07-08 05:08:29 +04:00
|
|
|
bool hasStatic(const String& name, JSType*& type, bool &isConstructor)
|
2000-06-29 05:21:41 +04:00
|
|
|
{
|
|
|
|
JSSlots::const_iterator i = mStaticSlots.find(name);
|
|
|
|
if (i != mStaticSlots.end()) {
|
|
|
|
type = i->second.mType;
|
2000-07-08 05:08:29 +04:00
|
|
|
isConstructor = i->second.isConstructor();
|
2000-06-29 05:21:41 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-06-28 20:11:54 +04:00
|
|
|
bool hasStatic(const String& name)
|
|
|
|
{
|
|
|
|
return (mStaticSlots.find(name) != mStaticSlots.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool complete()
|
|
|
|
{
|
|
|
|
mStaticData = new JSValue[mStaticCount];
|
|
|
|
return (mStaticData != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue& operator[] (uint32 index)
|
|
|
|
{
|
|
|
|
return mStaticData[index];
|
2000-06-27 07:42:42 +04:00
|
|
|
}
|
2000-06-28 23:46:13 +04:00
|
|
|
|
|
|
|
virtual void printProperties(Formatter& f)
|
|
|
|
{
|
|
|
|
f << "Properties:\n";
|
|
|
|
JSObject::printProperties(f);
|
|
|
|
f << "Statics:\n";
|
|
|
|
printStatics(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printStatics(Formatter& f)
|
|
|
|
{
|
|
|
|
JSClass* superClass = getSuperClass();
|
|
|
|
if (superClass) superClass->printStatics(f);
|
|
|
|
for (JSSlots::iterator i = mStaticSlots.begin(), end = mStaticSlots.end(); i != end; ++i) {
|
|
|
|
f << i->first << " : " << mStaticData[i->second.mIndex] << "\n";
|
|
|
|
}
|
|
|
|
}
|
2000-07-19 00:53:03 +04:00
|
|
|
|
|
|
|
void defineMethod(const String& name, JSFunction *f)
|
|
|
|
{
|
|
|
|
uint32 slot;
|
|
|
|
if (hasMethod(name, slot))
|
|
|
|
mMethods[slot] = MethodEntry(name, f);
|
|
|
|
else
|
|
|
|
mMethods.push_back(MethodEntry(name, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasMethod(const String& name, uint32& index)
|
|
|
|
{
|
|
|
|
JSMethods::iterator end = mMethods.end();
|
|
|
|
for (JSMethods::iterator i = mMethods.begin(); i != end; i++) {
|
|
|
|
if (i->first == name) {
|
2000-08-03 03:09:29 +04:00
|
|
|
index = static_cast<uint32>(i - mMethods.begin());
|
2000-07-19 00:53:03 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction* getMethod(uint32 index)
|
|
|
|
{
|
|
|
|
return mMethods[index].second;
|
|
|
|
}
|
2000-06-24 03:25:55 +04:00
|
|
|
};
|
2000-06-27 06:39:32 +04:00
|
|
|
|
2000-06-24 03:25:55 +04:00
|
|
|
/**
|
|
|
|
* Represents an instance of a JSClass.
|
|
|
|
*/
|
|
|
|
class JSInstance : public JSObject {
|
|
|
|
protected:
|
|
|
|
JSValue mSlots[1];
|
|
|
|
public:
|
|
|
|
void* operator new(size_t n, JSClass* thisClass)
|
|
|
|
{
|
2000-06-28 20:32:52 +04:00
|
|
|
uint32 slotCount = thisClass->getSlotCount();
|
|
|
|
if (slotCount > 0) n += sizeof(JSValue) * (slotCount - 1);
|
|
|
|
return gc_base::operator new(n);
|
2000-06-24 03:25:55 +04:00
|
|
|
}
|
|
|
|
|
2000-06-29 09:46:36 +04:00
|
|
|
#if !defined(XP_MAC)
|
2000-06-29 09:48:09 +04:00
|
|
|
void operator delete(void* /*ptr*/, JSClass* /*thisClass*/) {}
|
2000-06-29 09:46:36 +04:00
|
|
|
#endif
|
|
|
|
|
2000-06-24 03:25:55 +04:00
|
|
|
JSInstance(JSClass* thisClass)
|
|
|
|
{
|
2000-06-28 20:11:54 +04:00
|
|
|
mType = thisClass;
|
2000-06-28 20:32:52 +04:00
|
|
|
// initialize extra slots with undefined.
|
|
|
|
uint32 slotCount = thisClass->getSlotCount();
|
|
|
|
if (slotCount > 0) {
|
|
|
|
std::uninitialized_fill(&mSlots[1], &mSlots[1] + (slotCount - 1),
|
|
|
|
JSTypes::kUndefinedValue);
|
|
|
|
}
|
2000-06-24 09:22:23 +04:00
|
|
|
// for grins, use the prototype link to access methods.
|
2000-09-12 02:10:44 +04:00
|
|
|
// setPrototype(thisClass->getScope());
|
2000-06-24 03:25:55 +04:00
|
|
|
}
|
|
|
|
|
2000-07-19 00:53:03 +04:00
|
|
|
JSFunction* getMethod(uint32 index)
|
|
|
|
{
|
|
|
|
return getClass()->getMethod(index);
|
|
|
|
}
|
|
|
|
|
2000-06-24 03:25:55 +04:00
|
|
|
JSClass* getClass()
|
|
|
|
{
|
2000-06-28 20:11:54 +04:00
|
|
|
return static_cast<JSClass*>(mType);
|
2000-06-24 03:25:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValue& operator[] (uint32 index)
|
|
|
|
{
|
|
|
|
return mSlots[index];
|
|
|
|
}
|
2000-06-24 04:50:59 +04:00
|
|
|
|
|
|
|
virtual void printProperties(Formatter& f)
|
|
|
|
{
|
2000-06-25 02:07:00 +04:00
|
|
|
f << "Properties:\n";
|
2000-06-24 04:50:59 +04:00
|
|
|
JSObject::printProperties(f);
|
|
|
|
f << "Slots:\n";
|
2000-06-25 02:07:00 +04:00
|
|
|
printSlots(f, getClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void printSlots(Formatter& f, JSClass* thisClass)
|
|
|
|
{
|
|
|
|
JSClass* superClass = thisClass->getSuperClass();
|
|
|
|
if (superClass) printSlots(f, superClass);
|
|
|
|
JSSlots& slots = thisClass->getSlots();
|
2000-06-24 04:50:59 +04:00
|
|
|
for (JSSlots::iterator i = slots.begin(), end = slots.end(); i != end; ++i) {
|
|
|
|
f << i->first << " : " << mSlots[i->second.mIndex] << "\n";
|
|
|
|
}
|
|
|
|
}
|
2000-06-22 03:57:09 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace JSClasses */
|
|
|
|
} /* namespace JavaScript */
|
|
|
|
|
|
|
|
#endif /* jsclasses_h */
|