From ac7b31141658a00ab58811e235667f1a0ba61f06 Mon Sep 17 00:00:00 2001 From: "rogerl%netscape.com" Date: Sat, 24 Jun 2000 00:50:59 +0000 Subject: [PATCH] Got newClass working, added printProperties for instances, some slot stuff is happening. --- js/js2/icodegenerator.cpp | 18 +++++++++++++++--- js/js2/icodegenerator.h | 1 + js/js2/interpreter.cpp | 36 ++++++++++++++++++++++++++++++++++++ js/js2/js2.cpp | 4 ++-- js/js2/jsclasses.h | 17 +++++++++++++++++ js/js2/jstypes.cpp | 6 ++++-- js/js2/jstypes.h | 4 ++-- js2/src/icodegenerator.cpp | 18 +++++++++++++++--- js2/src/icodegenerator.h | 1 + js2/src/interpreter.cpp | 36 ++++++++++++++++++++++++++++++++++++ js2/src/jsclasses.h | 17 +++++++++++++++++ js2/src/jstypes.cpp | 6 ++++-- js2/src/jstypes.h | 4 ++-- js2/tests/cpp/js2_shell.cpp | 4 ++-- 14 files changed, 154 insertions(+), 18 deletions(-) diff --git a/js/js2/icodegenerator.cpp b/js/js2/icodegenerator.cpp index d3ddf110ca7..d3680565689 100644 --- a/js/js2/icodegenerator.cpp +++ b/js/js2/icodegenerator.cpp @@ -197,6 +197,14 @@ TypedRegister ICodeGenerator::newObject() return dest; } +TypedRegister ICodeGenerator::newClass(const StringAtom &name) +{ + TypedRegister dest(getRegister(), &Any_Type); + NewClass *instr = new NewClass(dest, &name); + iCode->push_back(instr); + return dest; +} + TypedRegister ICodeGenerator::newFunction(ICodeModule *icm) { TypedRegister dest(getRegister(), &Function_Type); @@ -632,8 +640,12 @@ TypedRegister ICodeGenerator::genExpr(ExprNode *p, break; case ExprNode::New: { - // FIXME - handle args, etc... - ret = newObject(); + InvokeExprNode *i = static_cast(p); + if (i->op->getKind() == ExprNode::identifier) { + ret = newClass(static_cast(i->op)->name); + } + else + ret = newObject(); // XXX more } break; case ExprNode::call : @@ -934,7 +946,7 @@ TypedRegister ICodeGenerator::genExpr(ExprNode *p, if (b->op1->getKind() == ExprNode::dot) { BinaryExprNode *lb = static_cast(b->op1); TypedRegister base = genExpr(lb->op1); - const StringAtom &name = static_cast(b->op2)->name; + const StringAtom &name = static_cast(lb->op2)->name; uint32 slotIndex; if (isSlotName(base.second, name, slotIndex)) setSlot(base, slotIndex, ret); diff --git a/js/js2/icodegenerator.h b/js/js2/icodegenerator.h index e2c32da382e..eedcfb37ed9 100644 --- a/js/js2/icodegenerator.h +++ b/js/js2/icodegenerator.h @@ -238,6 +238,7 @@ namespace ICG { TypedRegister newObject(); TypedRegister newArray(); TypedRegister newFunction(ICodeModule *icm); + TypedRegister newClass(const StringAtom &name); TypedRegister loadName(const StringAtom &name); void saveName(const StringAtom &name, TypedRegister value); diff --git a/js/js2/interpreter.cpp b/js/js2/interpreter.cpp index a4b8cc68717..548a7547af0 100644 --- a/js/js2/interpreter.cpp +++ b/js/js2/interpreter.cpp @@ -56,6 +56,7 @@ namespace Interpreter { using namespace ICG; using namespace JSTypes; +using namespace JSClasses; // These classes are private to the JS interpreter. @@ -709,6 +710,28 @@ using JSString throughout. } break; + case GET_SLOT: + { + GetSlot* gs = static_cast(instruction); + JSValue& value = (*registers)[src1(gs).first]; + if (value.isObject()) { + JSInstance* inst = static_cast(value.object); + (*registers)[dst(gs).first] = (*inst)[src2(gs)]; + } + // XXX runtime error + } + break; + case SET_SLOT: + { + SetSlot* ss = static_cast(instruction); + JSValue& value = (*registers)[dst(ss).first]; + if (value.isObject()) { + JSInstance* inst = static_cast(value.object); + (*inst)[src1(ss)] = (*registers)[src2(ss).first]; + } + } + break; + case LOAD_IMMEDIATE: { LoadImmediate* li = static_cast(instruction); @@ -822,6 +845,19 @@ using JSString throughout. } break; + case SLOT_XCR: + { + SlotXcr *sx = static_cast(instruction); + JSValue& dest = (*registers)[dst(sx).first]; + JSValue& base = (*registers)[src1(sx).first]; + JSInstance *inst = static_cast(base.object); + JSValue r = (*inst)[src2(sx)].toNumber(); + dest = r; + r.f64 += val4(sx); + (*inst)[src2(sx)] = r; + } + break; + case NAME_XCR: { NameXcr *nx = static_cast(instruction); diff --git a/js/js2/js2.cpp b/js/js2/js2.cpp index 2eb6ae9b0b0..bdc6753a03c 100644 --- a/js/js2/js2.cpp +++ b/js/js2/js2.cpp @@ -22,8 +22,8 @@ // JS2 shell. // -#if 0 -#define DEBUGGER_FOO +#if 1 +//#define DEBUGGER_FOO #define INTERPRET_INPUT #else #undef DEBUGGER_FOO diff --git a/js/js2/jsclasses.h b/js/js2/jsclasses.h index bd0253146cb..2039497944d 100644 --- a/js/js2/jsclasses.h +++ b/js/js2/jsclasses.h @@ -104,6 +104,11 @@ namespace JSClasses { return mSlots[name]; } + JSSlots& getSlots() + { + return mSlots; + } + bool hasSlot(const String& name) { return (mSlots.count(name) != 0); @@ -142,6 +147,18 @@ namespace JSClasses { { return mSlots[index]; } + + 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"; + } + } + }; } /* namespace JSClasses */ diff --git a/js/js2/jstypes.cpp b/js/js2/jstypes.cpp index e8ff789a597..64918d8bcd0 100644 --- a/js/js2/jstypes.cpp +++ b/js/js2/jstypes.cpp @@ -32,6 +32,7 @@ */ #include "jstypes.h" +#include "jsclasses.h" #include "numerics.h" #include "icodegenerator.h" @@ -155,13 +156,13 @@ int JSValue::operator==(const JSValue& value) const return 0; } -Formatter& operator<<(Formatter& f, const JSObject& obj) +Formatter& operator<<(Formatter& f, JSObject& obj) { obj.printProperties(f); return f; } -void JSObject::printProperties(Formatter& f) const +void JSObject::printProperties(Formatter& f) { for (JSProperties::const_iterator i = mProperties.begin(); i != mProperties.end(); i++) { f << (*i).first << " : " << (*i).second; @@ -184,6 +185,7 @@ Formatter& operator<<(Formatter& f, const JSValue& value) break; case JSValue::object_tag: { + printFormat(f, "Object @ 0x%08X\n", value.object); f << *value.object; } diff --git a/js/js2/jstypes.h b/js/js2/jstypes.h index a08e4d81860..cae03ef5f28 100644 --- a/js/js2/jstypes.h +++ b/js/js2/jstypes.h @@ -253,10 +253,10 @@ namespace JSTypes { return mPrototype; } - void printProperties(Formatter& f) const; + virtual void printProperties(Formatter& f); }; - Formatter& operator<<(Formatter& f, const JSObject& obj); + Formatter& operator<<(Formatter& f, JSObject& obj); /** * Private representation of a JavaScript array. diff --git a/js2/src/icodegenerator.cpp b/js2/src/icodegenerator.cpp index d3ddf110ca7..d3680565689 100644 --- a/js2/src/icodegenerator.cpp +++ b/js2/src/icodegenerator.cpp @@ -197,6 +197,14 @@ TypedRegister ICodeGenerator::newObject() return dest; } +TypedRegister ICodeGenerator::newClass(const StringAtom &name) +{ + TypedRegister dest(getRegister(), &Any_Type); + NewClass *instr = new NewClass(dest, &name); + iCode->push_back(instr); + return dest; +} + TypedRegister ICodeGenerator::newFunction(ICodeModule *icm) { TypedRegister dest(getRegister(), &Function_Type); @@ -632,8 +640,12 @@ TypedRegister ICodeGenerator::genExpr(ExprNode *p, break; case ExprNode::New: { - // FIXME - handle args, etc... - ret = newObject(); + InvokeExprNode *i = static_cast(p); + if (i->op->getKind() == ExprNode::identifier) { + ret = newClass(static_cast(i->op)->name); + } + else + ret = newObject(); // XXX more } break; case ExprNode::call : @@ -934,7 +946,7 @@ TypedRegister ICodeGenerator::genExpr(ExprNode *p, if (b->op1->getKind() == ExprNode::dot) { BinaryExprNode *lb = static_cast(b->op1); TypedRegister base = genExpr(lb->op1); - const StringAtom &name = static_cast(b->op2)->name; + const StringAtom &name = static_cast(lb->op2)->name; uint32 slotIndex; if (isSlotName(base.second, name, slotIndex)) setSlot(base, slotIndex, ret); diff --git a/js2/src/icodegenerator.h b/js2/src/icodegenerator.h index e2c32da382e..eedcfb37ed9 100644 --- a/js2/src/icodegenerator.h +++ b/js2/src/icodegenerator.h @@ -238,6 +238,7 @@ namespace ICG { TypedRegister newObject(); TypedRegister newArray(); TypedRegister newFunction(ICodeModule *icm); + TypedRegister newClass(const StringAtom &name); TypedRegister loadName(const StringAtom &name); void saveName(const StringAtom &name, TypedRegister value); diff --git a/js2/src/interpreter.cpp b/js2/src/interpreter.cpp index a4b8cc68717..548a7547af0 100644 --- a/js2/src/interpreter.cpp +++ b/js2/src/interpreter.cpp @@ -56,6 +56,7 @@ namespace Interpreter { using namespace ICG; using namespace JSTypes; +using namespace JSClasses; // These classes are private to the JS interpreter. @@ -709,6 +710,28 @@ using JSString throughout. } break; + case GET_SLOT: + { + GetSlot* gs = static_cast(instruction); + JSValue& value = (*registers)[src1(gs).first]; + if (value.isObject()) { + JSInstance* inst = static_cast(value.object); + (*registers)[dst(gs).first] = (*inst)[src2(gs)]; + } + // XXX runtime error + } + break; + case SET_SLOT: + { + SetSlot* ss = static_cast(instruction); + JSValue& value = (*registers)[dst(ss).first]; + if (value.isObject()) { + JSInstance* inst = static_cast(value.object); + (*inst)[src1(ss)] = (*registers)[src2(ss).first]; + } + } + break; + case LOAD_IMMEDIATE: { LoadImmediate* li = static_cast(instruction); @@ -822,6 +845,19 @@ using JSString throughout. } break; + case SLOT_XCR: + { + SlotXcr *sx = static_cast(instruction); + JSValue& dest = (*registers)[dst(sx).first]; + JSValue& base = (*registers)[src1(sx).first]; + JSInstance *inst = static_cast(base.object); + JSValue r = (*inst)[src2(sx)].toNumber(); + dest = r; + r.f64 += val4(sx); + (*inst)[src2(sx)] = r; + } + break; + case NAME_XCR: { NameXcr *nx = static_cast(instruction); diff --git a/js2/src/jsclasses.h b/js2/src/jsclasses.h index bd0253146cb..2039497944d 100644 --- a/js2/src/jsclasses.h +++ b/js2/src/jsclasses.h @@ -104,6 +104,11 @@ namespace JSClasses { return mSlots[name]; } + JSSlots& getSlots() + { + return mSlots; + } + bool hasSlot(const String& name) { return (mSlots.count(name) != 0); @@ -142,6 +147,18 @@ namespace JSClasses { { return mSlots[index]; } + + 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"; + } + } + }; } /* namespace JSClasses */ diff --git a/js2/src/jstypes.cpp b/js2/src/jstypes.cpp index e8ff789a597..64918d8bcd0 100644 --- a/js2/src/jstypes.cpp +++ b/js2/src/jstypes.cpp @@ -32,6 +32,7 @@ */ #include "jstypes.h" +#include "jsclasses.h" #include "numerics.h" #include "icodegenerator.h" @@ -155,13 +156,13 @@ int JSValue::operator==(const JSValue& value) const return 0; } -Formatter& operator<<(Formatter& f, const JSObject& obj) +Formatter& operator<<(Formatter& f, JSObject& obj) { obj.printProperties(f); return f; } -void JSObject::printProperties(Formatter& f) const +void JSObject::printProperties(Formatter& f) { for (JSProperties::const_iterator i = mProperties.begin(); i != mProperties.end(); i++) { f << (*i).first << " : " << (*i).second; @@ -184,6 +185,7 @@ Formatter& operator<<(Formatter& f, const JSValue& value) break; case JSValue::object_tag: { + printFormat(f, "Object @ 0x%08X\n", value.object); f << *value.object; } diff --git a/js2/src/jstypes.h b/js2/src/jstypes.h index a08e4d81860..cae03ef5f28 100644 --- a/js2/src/jstypes.h +++ b/js2/src/jstypes.h @@ -253,10 +253,10 @@ namespace JSTypes { return mPrototype; } - void printProperties(Formatter& f) const; + virtual void printProperties(Formatter& f); }; - Formatter& operator<<(Formatter& f, const JSObject& obj); + Formatter& operator<<(Formatter& f, JSObject& obj); /** * Private representation of a JavaScript array. diff --git a/js2/tests/cpp/js2_shell.cpp b/js2/tests/cpp/js2_shell.cpp index 2eb6ae9b0b0..bdc6753a03c 100644 --- a/js2/tests/cpp/js2_shell.cpp +++ b/js2/tests/cpp/js2_shell.cpp @@ -22,8 +22,8 @@ // JS2 shell. // -#if 0 -#define DEBUGGER_FOO +#if 1 +//#define DEBUGGER_FOO #define INTERPRET_INPUT #else #undef DEBUGGER_FOO