зеркало из https://github.com/mozilla/pjs.git
Got newClass working, added printProperties for instances, some slot
stuff is happening.
This commit is contained in:
Родитель
1af2ca85a9
Коммит
ac7b311416
|
@ -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<InvokeExprNode *>(p);
|
||||
if (i->op->getKind() == ExprNode::identifier) {
|
||||
ret = newClass(static_cast<IdentifierExprNode *>(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<BinaryExprNode *>(b->op1);
|
||||
TypedRegister base = genExpr(lb->op1);
|
||||
const StringAtom &name = static_cast<IdentifierExprNode *>(b->op2)->name;
|
||||
const StringAtom &name = static_cast<IdentifierExprNode *>(lb->op2)->name;
|
||||
uint32 slotIndex;
|
||||
if (isSlotName(base.second, name, slotIndex))
|
||||
setSlot(base, slotIndex, ret);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<GetSlot*>(instruction);
|
||||
JSValue& value = (*registers)[src1(gs).first];
|
||||
if (value.isObject()) {
|
||||
JSInstance* inst = static_cast<JSInstance *>(value.object);
|
||||
(*registers)[dst(gs).first] = (*inst)[src2(gs)];
|
||||
}
|
||||
// XXX runtime error
|
||||
}
|
||||
break;
|
||||
case SET_SLOT:
|
||||
{
|
||||
SetSlot* ss = static_cast<SetSlot*>(instruction);
|
||||
JSValue& value = (*registers)[dst(ss).first];
|
||||
if (value.isObject()) {
|
||||
JSInstance* inst = static_cast<JSInstance *>(value.object);
|
||||
(*inst)[src1(ss)] = (*registers)[src2(ss).first];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LOAD_IMMEDIATE:
|
||||
{
|
||||
LoadImmediate* li = static_cast<LoadImmediate*>(instruction);
|
||||
|
@ -822,6 +845,19 @@ using JSString throughout.
|
|||
}
|
||||
break;
|
||||
|
||||
case SLOT_XCR:
|
||||
{
|
||||
SlotXcr *sx = static_cast<SlotXcr*>(instruction);
|
||||
JSValue& dest = (*registers)[dst(sx).first];
|
||||
JSValue& base = (*registers)[src1(sx).first];
|
||||
JSInstance *inst = static_cast<JSInstance*>(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<NameXcr*>(instruction);
|
||||
|
|
|
@ -22,8 +22,8 @@
|
|||
// JS2 shell.
|
||||
//
|
||||
|
||||
#if 0
|
||||
#define DEBUGGER_FOO
|
||||
#if 1
|
||||
//#define DEBUGGER_FOO
|
||||
#define INTERPRET_INPUT
|
||||
#else
|
||||
#undef DEBUGGER_FOO
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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<InvokeExprNode *>(p);
|
||||
if (i->op->getKind() == ExprNode::identifier) {
|
||||
ret = newClass(static_cast<IdentifierExprNode *>(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<BinaryExprNode *>(b->op1);
|
||||
TypedRegister base = genExpr(lb->op1);
|
||||
const StringAtom &name = static_cast<IdentifierExprNode *>(b->op2)->name;
|
||||
const StringAtom &name = static_cast<IdentifierExprNode *>(lb->op2)->name;
|
||||
uint32 slotIndex;
|
||||
if (isSlotName(base.second, name, slotIndex))
|
||||
setSlot(base, slotIndex, ret);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<GetSlot*>(instruction);
|
||||
JSValue& value = (*registers)[src1(gs).first];
|
||||
if (value.isObject()) {
|
||||
JSInstance* inst = static_cast<JSInstance *>(value.object);
|
||||
(*registers)[dst(gs).first] = (*inst)[src2(gs)];
|
||||
}
|
||||
// XXX runtime error
|
||||
}
|
||||
break;
|
||||
case SET_SLOT:
|
||||
{
|
||||
SetSlot* ss = static_cast<SetSlot*>(instruction);
|
||||
JSValue& value = (*registers)[dst(ss).first];
|
||||
if (value.isObject()) {
|
||||
JSInstance* inst = static_cast<JSInstance *>(value.object);
|
||||
(*inst)[src1(ss)] = (*registers)[src2(ss).first];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case LOAD_IMMEDIATE:
|
||||
{
|
||||
LoadImmediate* li = static_cast<LoadImmediate*>(instruction);
|
||||
|
@ -822,6 +845,19 @@ using JSString throughout.
|
|||
}
|
||||
break;
|
||||
|
||||
case SLOT_XCR:
|
||||
{
|
||||
SlotXcr *sx = static_cast<SlotXcr*>(instruction);
|
||||
JSValue& dest = (*registers)[dst(sx).first];
|
||||
JSValue& base = (*registers)[src1(sx).first];
|
||||
JSInstance *inst = static_cast<JSInstance*>(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<NameXcr*>(instruction);
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -22,8 +22,8 @@
|
|||
// JS2 shell.
|
||||
//
|
||||
|
||||
#if 0
|
||||
#define DEBUGGER_FOO
|
||||
#if 1
|
||||
//#define DEBUGGER_FOO
|
||||
#define INTERPRET_INPUT
|
||||
#else
|
||||
#undef DEBUGGER_FOO
|
||||
|
|
Загрузка…
Ссылка в новой задаче