2000-04-21 04:04:14 +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.
|
|
|
|
*/
|
2000-04-05 10:05:57 +04:00
|
|
|
|
|
|
|
#include "interpreter.h"
|
2000-04-06 00:33:41 +04:00
|
|
|
#include "world.h"
|
2000-04-05 10:05:57 +04:00
|
|
|
|
2000-04-29 04:29:56 +04:00
|
|
|
#include <assert.h>
|
|
|
|
|
2000-04-05 10:10:53 +04:00
|
|
|
namespace JavaScript {
|
2000-04-26 09:35:07 +04:00
|
|
|
namespace Interpreter {
|
|
|
|
|
|
|
|
// operand access macros.
|
2000-04-18 04:17:34 +04:00
|
|
|
#define op1(i) (i->o1())
|
|
|
|
#define op2(i) (i->o2())
|
|
|
|
#define op3(i) (i->o3())
|
2000-05-11 04:35:06 +04:00
|
|
|
#define op4(i) (i->o4())
|
2000-04-05 10:05:57 +04:00
|
|
|
|
2000-04-26 09:35:07 +04:00
|
|
|
// mnemonic names for operands.
|
2000-04-18 04:17:34 +04:00
|
|
|
#define dst(i) op1(i)
|
2000-04-11 07:11:00 +04:00
|
|
|
#define src1(i) op2(i)
|
|
|
|
#define src2(i) op3(i)
|
2000-04-18 04:17:34 +04:00
|
|
|
#define ofs(i) (i->getOffset())
|
2000-05-11 04:35:06 +04:00
|
|
|
#define val3(i) op3(i)
|
|
|
|
#define val4(i) op4(i)
|
2000-04-11 07:11:00 +04:00
|
|
|
|
2000-04-26 09:35:07 +04:00
|
|
|
using namespace ICG;
|
|
|
|
using namespace JSTypes;
|
2000-04-21 04:37:51 +04:00
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
// These classes are private to the JS interpreter.
|
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct Handler: public gc_base {
|
|
|
|
Handler(Label *catchLabel, Label *finallyLabel)
|
|
|
|
: catchTarget(catchLabel), finallyTarget(finallyLabel) {}
|
|
|
|
Label *catchTarget;
|
|
|
|
Label *finallyTarget;
|
|
|
|
};
|
|
|
|
typedef std::vector<Handler *> CatchStack;
|
|
|
|
|
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
/**
|
|
|
|
* Represents the current function's invocation state.
|
|
|
|
*/
|
2000-04-26 09:35:07 +04:00
|
|
|
struct Activation : public gc_base {
|
2000-04-26 05:42:00 +04:00
|
|
|
JSValues mRegisters;
|
2000-04-26 09:35:07 +04:00
|
|
|
ICodeModule* mICode;
|
2000-04-27 05:27:09 +04:00
|
|
|
CatchStack catchStack;
|
2000-04-26 05:42:00 +04:00
|
|
|
|
2000-04-26 09:35:07 +04:00
|
|
|
Activation(ICodeModule* iCode, const JSValues& args)
|
|
|
|
: mRegisters(iCode->itsMaxRegister + 1), mICode(iCode)
|
2000-04-19 01:51:45 +04:00
|
|
|
{
|
2000-04-26 05:42:00 +04:00
|
|
|
// copy arg list to initial registers.
|
|
|
|
JSValues::iterator dest = mRegisters.begin();
|
|
|
|
for (JSValues::const_iterator src = args.begin(),
|
|
|
|
end = args.end(); src != end; ++src, ++dest) {
|
|
|
|
*dest = *src;
|
|
|
|
}
|
|
|
|
}
|
2000-04-08 07:23:44 +04:00
|
|
|
|
2000-04-26 09:35:07 +04:00
|
|
|
Activation(ICodeModule* iCode, Activation* caller,
|
2000-04-26 05:42:00 +04:00
|
|
|
const RegisterList& list)
|
2000-04-26 09:35:07 +04:00
|
|
|
: mRegisters(iCode->itsMaxRegister + 1), mICode(iCode)
|
2000-04-26 05:42:00 +04:00
|
|
|
{
|
|
|
|
// copy caller's parameter list to initial registers.
|
|
|
|
JSValues::iterator dest = mRegisters.begin();
|
|
|
|
const JSValues& params = caller->mRegisters;
|
|
|
|
for (RegisterList::const_iterator src = list.begin(),
|
|
|
|
end = list.end(); src != end; ++src, ++dest) {
|
|
|
|
*dest = params[*src];
|
|
|
|
}
|
|
|
|
}
|
2000-05-19 21:41:10 +04:00
|
|
|
|
|
|
|
Activation(ICodeModule* iCode, const JSValue arg)
|
|
|
|
: mRegisters(iCode->itsMaxRegister + 1), mICode(iCode)
|
|
|
|
{
|
|
|
|
mRegisters[0] = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Activation(ICodeModule* iCode, const JSValue arg1, const JSValue arg2)
|
|
|
|
: mRegisters(iCode->itsMaxRegister + 1), mICode(iCode)
|
|
|
|
{
|
|
|
|
mRegisters[0] = arg1;
|
|
|
|
mRegisters[1] = arg2;
|
|
|
|
}
|
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
};
|
2000-04-11 04:32:17 +04:00
|
|
|
|
2000-04-29 18:43:36 +04:00
|
|
|
JSValues& Context::getRegisters() { return mActivation->mRegisters; }
|
|
|
|
ICodeModule* Context::getICode() { return mActivation->mICode; }
|
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
/**
|
|
|
|
* Stores saved state from the *previous* activation, the current
|
|
|
|
* activation is alive and well in locals of the interpreter loop.
|
|
|
|
*/
|
2000-04-28 06:23:08 +04:00
|
|
|
struct Linkage : public Context::Frame, public gc_base {
|
2000-04-26 09:35:07 +04:00
|
|
|
Linkage* mNext; // next linkage in linkage stack.
|
2000-04-26 05:42:00 +04:00
|
|
|
InstructionIterator mReturnPC;
|
2000-04-26 09:35:07 +04:00
|
|
|
Activation* mActivation; // caller's activation.
|
2000-04-26 05:42:00 +04:00
|
|
|
Register mResult; // the desired target register for the return value
|
2000-04-11 04:32:17 +04:00
|
|
|
|
2000-05-19 21:41:10 +04:00
|
|
|
Linkage(Linkage* linkage, InstructionIterator returnPC,
|
2000-04-26 09:35:07 +04:00
|
|
|
Activation* activation, Register result)
|
2000-05-19 21:41:10 +04:00
|
|
|
: mNext(linkage), mReturnPC(returnPC),
|
2000-04-26 05:42:00 +04:00
|
|
|
mActivation(activation), mResult(result)
|
|
|
|
{
|
|
|
|
}
|
2000-04-26 09:35:07 +04:00
|
|
|
|
2000-04-29 04:23:06 +04:00
|
|
|
Context::Frame* getNext() { return mNext; }
|
2000-04-26 09:35:07 +04:00
|
|
|
|
2000-04-29 04:23:06 +04:00
|
|
|
void getState(InstructionIterator& pc, JSValues*& registers, ICodeModule*& iCode)
|
2000-04-26 09:35:07 +04:00
|
|
|
{
|
|
|
|
pc = mReturnPC;
|
|
|
|
registers = &mActivation->mRegisters;
|
|
|
|
iCode = mActivation->mICode;
|
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
};
|
2000-05-19 21:41:10 +04:00
|
|
|
/*
|
|
|
|
void Context::doCall(JSFunction *target, Instruction *pc)
|
|
|
|
{
|
|
|
|
if (target->isNative()) {
|
|
|
|
RegisterList ¶ms = op3(call);
|
|
|
|
JSValues argv(params.size());
|
|
|
|
JSValues::size_type i = 0;
|
|
|
|
for (RegisterList::const_iterator src = params.begin(), end = params.end();
|
|
|
|
src != end; ++src, ++i) {
|
|
|
|
argv[i] = (*registers)[*src];
|
|
|
|
}
|
|
|
|
if (op2(call) != NotARegister)
|
|
|
|
(*registers)[op2(call)] = static_cast<JSNativeFunction*>(target)->mCode(argv);
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC,
|
|
|
|
mActivation, op1(call));
|
|
|
|
iCode = target->getICode();
|
|
|
|
mActivation = new Activation(iCode, mActivation, op3(call));
|
|
|
|
registers = &mActivation->mRegisters;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2000-05-23 04:08:29 +04:00
|
|
|
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue shiftLeft_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-19 21:41:10 +04:00
|
|
|
{
|
2000-05-23 04:08:29 +04:00
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
return JSValue(num1.i32 << (num2.u32 & 0x1F));
|
2000-05-19 21:41:10 +04:00
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue shiftRight_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
return JSValue(num1.i32 >> (num2.u32 & 0x1F));
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue UshiftRight_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toUInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
return JSValue(num1.u32 >> (num2.u32 & 0x1F));
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue and_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
return JSValue(num1.i32 & num2.i32);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue or_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
return JSValue(num1.i32 | num2.i32);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue xor_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
return JSValue(num1.i32 ^ num2.i32);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue add_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// could also handle these as separate entries in the override table for add
|
|
|
|
// by specifying add(String, Any), add(Any, String), add(String, String)
|
|
|
|
//
|
|
|
|
if (r1.isString() || r2.isString()) {
|
|
|
|
JSValue r = r1.toString();
|
|
|
|
JSString& str1 = *r.string;
|
|
|
|
JSString& str2 = *r2.toString().string;
|
|
|
|
str1 += str2;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 + num2.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue add_String1(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 + num2.f64);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue subtract_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 - num2.f64);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue multiply_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-19 21:41:10 +04:00
|
|
|
{
|
2000-05-23 04:08:29 +04:00
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
2000-05-19 21:41:10 +04:00
|
|
|
return JSValue(num1.f64 * num2.f64);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue divide_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 / num2.f64);
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue remainder_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(fmod(num1.f64, num2.f64));
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue less_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue lv = r1.toPrimitive(JSValue::Number);
|
|
|
|
JSValue rv = r2.toPrimitive(JSValue::Number);
|
|
|
|
if (lv.isString() && rv.isString()) {
|
|
|
|
// XXX FIXME urgh, call w_strcmp ??? on a JSString ???
|
|
|
|
return JSValue();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv = lv.toNumber();
|
|
|
|
rv = rv.toNumber();
|
|
|
|
if (lv.isNaN() || rv.isNaN())
|
|
|
|
return JSValue();
|
|
|
|
else
|
|
|
|
return JSValue(lv.f64 < rv.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue lessEqual_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue lv = r1.toPrimitive(JSValue::Number);
|
|
|
|
JSValue rv = r2.toPrimitive(JSValue::Number);
|
|
|
|
if (lv.isString() && rv.isString()) {
|
|
|
|
// XXX FIXME urgh, call w_strcmp ??? on a JSString ???
|
|
|
|
return JSValue();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv = lv.toNumber();
|
|
|
|
rv = rv.toNumber();
|
|
|
|
if (lv.isNaN() || rv.isNaN())
|
|
|
|
return JSValue();
|
|
|
|
else
|
|
|
|
return JSValue(lv.f64 <= rv.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue equal_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
JSValue lv = r1.toPrimitive(JSValue::Number);
|
|
|
|
JSValue rv = r2.toPrimitive(JSValue::Number);
|
|
|
|
if (lv.isString() && rv.isString()) {
|
|
|
|
// XXX FIXME urgh, call w_strcmp ??? on a JSString ???
|
|
|
|
return JSValue();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv = lv.toNumber();
|
|
|
|
rv = rv.toNumber();
|
|
|
|
if (lv.isNaN() || rv.isNaN())
|
|
|
|
return JSValue();
|
|
|
|
else
|
|
|
|
return JSValue(lv.f64 == rv.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-05-24 04:54:40 +04:00
|
|
|
static JSValue identical_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 04:08:29 +04:00
|
|
|
{
|
|
|
|
if (r1.getType() != r2.getType())
|
|
|
|
return kFalse;
|
|
|
|
if (r1.isUndefined() )
|
|
|
|
return kTrue;
|
|
|
|
if (r1.isNull())
|
|
|
|
return kTrue;
|
|
|
|
if (r1.isNumber()) {
|
|
|
|
if (r1.isNaN())
|
|
|
|
return kFalse;
|
|
|
|
if (r2.isNaN())
|
|
|
|
return kFalse;
|
|
|
|
return JSValue(r1.f64 == r2.f64);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (r1.isString())
|
|
|
|
return kFalse; // XXX implement me!! w_strcmp??
|
|
|
|
if (r1.isBoolean())
|
|
|
|
return JSValue(r1.boolean == r2.boolean);
|
|
|
|
if (r1.isObject())
|
|
|
|
return JSValue(r1.object == r2.object);
|
|
|
|
return kFalse;
|
|
|
|
}
|
|
|
|
}
|
2000-05-19 21:41:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
class BinaryOperator {
|
|
|
|
public:
|
2000-05-23 04:08:29 +04:00
|
|
|
|
|
|
|
// Wah, here's a third enumeration of opcodes - ExprNode, ICodeOp and now here, this can't be right??
|
2000-05-19 21:41:10 +04:00
|
|
|
typedef enum {
|
|
|
|
Add, Subtract, Multiply, Divide,
|
|
|
|
Remainder, LeftShift, RightShift, LogicalRightShift,
|
|
|
|
BitwiseOr, BitwiseXor, BitwiseAnd, Less, LessEqual,
|
|
|
|
Equal, Identical
|
|
|
|
} BinaryOp;
|
|
|
|
|
2000-05-23 04:08:29 +04:00
|
|
|
BinaryOperator(const JSType *t1, const JSType *t2, JSBinaryOperator *function) :
|
2000-05-19 21:41:10 +04:00
|
|
|
t1(t1), t2(t2), function(function) { }
|
|
|
|
|
2000-05-23 04:08:29 +04:00
|
|
|
static BinaryOp mapICodeOp(ICodeOp op);
|
|
|
|
|
2000-05-19 21:41:10 +04:00
|
|
|
const JSType *t1;
|
|
|
|
const JSType *t2;
|
2000-05-23 04:08:29 +04:00
|
|
|
JSBinaryOperator *function;
|
2000-05-19 21:41:10 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2000-05-23 04:08:29 +04:00
|
|
|
BinaryOperator::BinaryOp BinaryOperator::mapICodeOp(ICodeOp op) {
|
|
|
|
// a table later... or maybe we need a grand opcode re-unification
|
|
|
|
switch (op) {
|
|
|
|
case ADD : return Add;
|
|
|
|
case SUBTRACT : return Subtract;
|
|
|
|
case MULTIPLY : return Multiply;
|
|
|
|
case DIVIDE : return Divide;
|
|
|
|
case REMAINDER : return Remainder;
|
|
|
|
case SHIFTLEFT : return LeftShift;
|
|
|
|
case SHIFTRIGHT : return RightShift;
|
|
|
|
case USHIFTRIGHT: return LogicalRightShift;
|
|
|
|
|
|
|
|
case AND : return BitwiseAnd;
|
|
|
|
case OR : return BitwiseOr;
|
|
|
|
case XOR : return BitwiseXor;
|
|
|
|
|
|
|
|
case COMPARE_LT : return Less;
|
|
|
|
case COMPARE_LE : return LessEqual;
|
|
|
|
case COMPARE_EQ : return Equal;
|
|
|
|
case STRICT_EQ : return Identical;
|
|
|
|
default :
|
|
|
|
NOT_REACHED("Unsupported binary op");
|
|
|
|
return (BinaryOp)-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-19 21:41:10 +04:00
|
|
|
typedef std::vector<BinaryOperator *> BinaryOperatorList;
|
|
|
|
BinaryOperatorList binaryOperators[15];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InitBinaryOperators {
|
|
|
|
public:
|
|
|
|
InitBinaryOperators() {
|
2000-05-23 04:08:29 +04:00
|
|
|
binaryOperators[BinaryOperator::Add].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(add_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Subtract].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(subtract_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Multiply].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(multiply_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Divide].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(divide_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Remainder].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(remainder_Default)));
|
|
|
|
binaryOperators[BinaryOperator::LeftShift].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(shiftLeft_Default)));
|
|
|
|
binaryOperators[BinaryOperator::RightShift].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(shiftRight_Default)));
|
|
|
|
binaryOperators[BinaryOperator::LogicalRightShift].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(UshiftRight_Default)));
|
|
|
|
binaryOperators[BinaryOperator::BitwiseOr].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(or_Default)));
|
|
|
|
binaryOperators[BinaryOperator::BitwiseXor].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(xor_Default)));
|
|
|
|
binaryOperators[BinaryOperator::BitwiseAnd].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(and_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Less].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(less_Default)));
|
|
|
|
binaryOperators[BinaryOperator::LessEqual].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(lessEqual_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Equal].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(equal_Default)));
|
|
|
|
binaryOperators[BinaryOperator::Identical].push_back(new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(identical_Default)));
|
2000-05-19 21:41:10 +04:00
|
|
|
}
|
|
|
|
} initializer = InitBinaryOperators();
|
|
|
|
|
|
|
|
|
2000-05-24 04:54:40 +04:00
|
|
|
static const JSValue findBinaryOverride(JSValue &operand1, JSValue &operand2, BinaryOperator::BinaryOp op)
|
2000-05-19 21:41:10 +04:00
|
|
|
{
|
|
|
|
int32 bestDist1 = JSType::NoRelation;
|
|
|
|
int32 bestDist2 = JSType::NoRelation;
|
|
|
|
BinaryOperatorList::iterator candidate = NULL;
|
|
|
|
|
|
|
|
for (BinaryOperatorList::iterator i = binaryOperators[op].begin();
|
|
|
|
i != binaryOperators[op].end(); i++)
|
|
|
|
{
|
|
|
|
int32 dist1 = operand1.getType()->distance((*i)->t1);
|
|
|
|
int32 dist2 = operand2.getType()->distance((*i)->t2);
|
|
|
|
|
|
|
|
if ((dist1 < bestDist1) && (dist2 < bestDist2)) {
|
|
|
|
bestDist1 = dist1;
|
|
|
|
bestDist2 = dist2;
|
|
|
|
candidate = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(candidate);
|
|
|
|
return JSValue((*candidate)->function);
|
|
|
|
}
|
2000-04-21 04:04:14 +04:00
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
JSValue Context::interpret(ICodeModule* iCode, const JSValues& args)
|
|
|
|
{
|
2000-04-29 04:23:06 +04:00
|
|
|
assert(mActivation == 0); /* recursion == bad */
|
|
|
|
|
|
|
|
JSValue rv;
|
2000-05-19 21:41:10 +04:00
|
|
|
|
2000-04-29 04:23:06 +04:00
|
|
|
mActivation = new Activation(iCode, args);
|
|
|
|
JSValues* registers = &mActivation->mRegisters;
|
2000-04-21 04:04:14 +04:00
|
|
|
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin();
|
2000-04-26 05:42:00 +04:00
|
|
|
|
2000-04-27 05:27:09 +04:00
|
|
|
// stack of all catch/finally handlers available for the current activation
|
2000-04-29 04:23:06 +04:00
|
|
|
// to implement jsr/rts for finally code
|
|
|
|
std::stack<InstructionIterator> subroutineStack;
|
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
while (true) {
|
|
|
|
try {
|
2000-04-26 09:35:07 +04:00
|
|
|
// tell any listeners about the current execution state.
|
|
|
|
// XXX should only do this if we're single stepping/tracing.
|
2000-04-29 04:23:06 +04:00
|
|
|
if (mListeners.size())
|
2000-05-12 05:19:39 +04:00
|
|
|
broadcast(EV_STEP);
|
2000-04-29 04:23:06 +04:00
|
|
|
|
2000-04-29 18:43:36 +04:00
|
|
|
Instruction* instruction = *mPC;
|
2000-04-26 05:42:00 +04:00
|
|
|
switch (instruction->op()) {
|
|
|
|
case CALL:
|
|
|
|
{
|
|
|
|
Call* call = static_cast<Call*>(instruction);
|
2000-05-09 02:59:42 +04:00
|
|
|
JSFunction *target = (*registers)[op2(call)].function;
|
|
|
|
if (target->isNative()) {
|
|
|
|
RegisterList ¶ms = op3(call);
|
|
|
|
JSValues argv(params.size());
|
2000-05-12 05:19:39 +04:00
|
|
|
JSValues::size_type i = 0;
|
2000-05-09 02:59:42 +04:00
|
|
|
for (RegisterList::const_iterator src = params.begin(), end = params.end();
|
|
|
|
src != end; ++src, ++i) {
|
|
|
|
argv[i] = (*registers)[*src];
|
|
|
|
}
|
|
|
|
if (op2(call) != NotARegister)
|
2000-05-09 07:57:28 +04:00
|
|
|
(*registers)[op2(call)] = static_cast<JSNativeFunction*>(target)->mCode(argv);
|
2000-05-09 02:59:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2000-05-19 21:41:10 +04:00
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC,
|
2000-05-09 02:59:42 +04:00
|
|
|
mActivation, op1(call));
|
2000-05-19 21:41:10 +04:00
|
|
|
mActivation = new Activation(target->getICode(), mActivation, op3(call));
|
2000-05-09 02:59:42 +04:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin();
|
|
|
|
continue;
|
2000-05-09 02:59:42 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case RETURN_VOID:
|
|
|
|
{
|
2000-04-28 09:43:33 +04:00
|
|
|
JSValue result;
|
2000-04-26 09:35:07 +04:00
|
|
|
Linkage *linkage = mLinkage;
|
2000-04-26 05:42:00 +04:00
|
|
|
if (!linkage)
|
2000-04-29 04:23:06 +04:00
|
|
|
{
|
2000-04-29 18:43:36 +04:00
|
|
|
// let the garbage collector free activations.
|
|
|
|
mActivation = 0;
|
|
|
|
return result;
|
2000-04-29 04:23:06 +04:00
|
|
|
}
|
2000-04-26 09:35:07 +04:00
|
|
|
mLinkage = linkage->mNext;
|
2000-04-29 04:23:06 +04:00
|
|
|
mActivation = linkage->mActivation;
|
|
|
|
registers = &mActivation->mRegisters;
|
2000-04-26 05:42:00 +04:00
|
|
|
(*registers)[linkage->mResult] = result;
|
2000-04-29 18:43:36 +04:00
|
|
|
mPC = linkage->mReturnPC;
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case RETURN:
|
|
|
|
{
|
|
|
|
Return* ret = static_cast<Return*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
JSValue result;
|
2000-04-26 05:42:00 +04:00
|
|
|
if (op1(ret) != NotARegister)
|
|
|
|
result = (*registers)[op1(ret)];
|
2000-04-26 09:35:07 +04:00
|
|
|
Linkage* linkage = mLinkage;
|
2000-04-26 05:42:00 +04:00
|
|
|
if (!linkage)
|
2000-04-29 04:23:06 +04:00
|
|
|
{
|
2000-04-29 18:43:36 +04:00
|
|
|
// let the garbage collector free activations.
|
|
|
|
mActivation = 0;
|
|
|
|
return result;
|
2000-04-29 04:23:06 +04:00
|
|
|
}
|
2000-04-26 09:35:07 +04:00
|
|
|
mLinkage = linkage->mNext;
|
2000-04-29 04:23:06 +04:00
|
|
|
mActivation = linkage->mActivation;
|
|
|
|
registers = &mActivation->mRegisters;
|
2000-04-26 05:42:00 +04:00
|
|
|
(*registers)[linkage->mResult] = result;
|
2000-04-29 18:43:36 +04:00
|
|
|
mPC = linkage->mReturnPC;
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case MOVE:
|
|
|
|
{
|
|
|
|
Move* mov = static_cast<Move*>(instruction);
|
|
|
|
(*registers)[dst(mov)] = (*registers)[src1(mov)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOAD_NAME:
|
|
|
|
{
|
|
|
|
LoadName* ln = static_cast<LoadName*>(instruction);
|
2000-04-29 03:04:43 +04:00
|
|
|
(*registers)[dst(ln)] = mGlobal->getVariable(*src1(ln));
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SAVE_NAME:
|
|
|
|
{
|
|
|
|
SaveName* sn = static_cast<SaveName*>(instruction);
|
2000-04-29 03:04:43 +04:00
|
|
|
mGlobal->setVariable(*dst(sn), (*registers)[src1(sn)]);
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEW_OBJECT:
|
|
|
|
{
|
|
|
|
NewObject* no = static_cast<NewObject*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
(*registers)[dst(no)] = JSValue(new JSObject());
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEW_ARRAY:
|
|
|
|
{
|
|
|
|
NewArray* na = static_cast<NewArray*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
(*registers)[dst(na)] = JSValue(new JSArray());
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GET_PROP:
|
|
|
|
{
|
|
|
|
GetProp* gp = static_cast<GetProp*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
JSValue& value = (*registers)[src1(gp)];
|
|
|
|
if (value.tag == JSValue::object_tag) {
|
|
|
|
JSObject* object = value.object;
|
|
|
|
(*registers)[dst(gp)] = object->getProperty(*src2(gp));
|
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_PROP:
|
|
|
|
{
|
|
|
|
SetProp* sp = static_cast<SetProp*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
JSValue& value = (*registers)[dst(sp)];
|
|
|
|
if (value.tag == JSValue::object_tag) {
|
|
|
|
JSObject* object = value.object;
|
|
|
|
object->setProperty(*src1(sp), (*registers)[src2(sp)]);
|
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 04:03:23 +04:00
|
|
|
/*
|
2000-05-19 21:41:10 +04:00
|
|
|
In 1.5, there is no 'array' type really, the index operation
|
|
|
|
turns into a get_property call like this, only we need to be
|
|
|
|
using JSString throughout.
|
2000-05-18 04:03:23 +04:00
|
|
|
case GET_ELEMENT:
|
|
|
|
{
|
|
|
|
GetElement* ge = static_cast<GetElement*>(instruction);
|
|
|
|
JSValue& base = (*registers)[src1(ge)];
|
|
|
|
JSValue index = (*registers)[src2(ge)].toString();
|
|
|
|
if (base.tag == JSValue::object_tag) {
|
|
|
|
JSObject* object = base.object;
|
|
|
|
(*registers)[dst(ge)] = object->getProperty(*index.string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
*/
|
2000-04-26 05:42:00 +04:00
|
|
|
case GET_ELEMENT:
|
|
|
|
{
|
|
|
|
GetElement* ge = static_cast<GetElement*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
JSValue& value = (*registers)[src1(ge)];
|
|
|
|
if (value.tag == JSValue::array_tag) {
|
|
|
|
JSArray* array = value.array;
|
|
|
|
(*registers)[dst(ge)] = (*array)[(*registers)[src2(ge)]];
|
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_ELEMENT:
|
|
|
|
{
|
|
|
|
SetElement* se = static_cast<SetElement*>(instruction);
|
2000-04-28 09:43:33 +04:00
|
|
|
JSValue& value = (*registers)[dst(se)];
|
|
|
|
if (value.tag == JSValue::array_tag) {
|
|
|
|
JSArray* array = value.array;
|
|
|
|
(*array)[(*registers)[src1(se)]] = (*registers)[src2(se)];
|
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 04:03:23 +04:00
|
|
|
|
2000-04-26 05:42:00 +04:00
|
|
|
case LOAD_IMMEDIATE:
|
|
|
|
{
|
|
|
|
LoadImmediate* li = static_cast<LoadImmediate*>(instruction);
|
|
|
|
(*registers)[dst(li)] = JSValue(src1(li));
|
|
|
|
}
|
|
|
|
break;
|
2000-05-09 02:59:42 +04:00
|
|
|
case LOAD_STRING:
|
|
|
|
{
|
|
|
|
LoadString* ls = static_cast<LoadString*>(instruction);
|
2000-05-09 09:38:48 +04:00
|
|
|
(*registers)[dst(ls)] = JSValue(src1(ls));
|
2000-05-09 02:59:42 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 04:03:23 +04:00
|
|
|
case LOAD_VALUE:
|
|
|
|
{
|
|
|
|
LoadValue* lv = static_cast<LoadValue*>(instruction);
|
|
|
|
(*registers)[dst(lv)] = src1(lv);
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 05:42:00 +04:00
|
|
|
case BRANCH:
|
|
|
|
{
|
|
|
|
GenericBranch* bra =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + ofs(bra);
|
2000-04-08 07:23:44 +04:00
|
|
|
continue;
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-19 21:41:10 +04:00
|
|
|
case BRANCH_TRUE:
|
2000-04-26 05:42:00 +04:00
|
|
|
{
|
|
|
|
GenericBranch* bc =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
2000-05-19 21:41:10 +04:00
|
|
|
ASSERT((*registers)[src1(bc)].isBoolean());
|
|
|
|
if ((*registers)[src1(bc)].boolean) {
|
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + ofs(bc);
|
2000-04-26 05:42:00 +04:00
|
|
|
continue;
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-19 21:41:10 +04:00
|
|
|
case BRANCH_FALSE:
|
2000-04-26 05:42:00 +04:00
|
|
|
{
|
|
|
|
GenericBranch* bc =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
2000-05-19 21:41:10 +04:00
|
|
|
ASSERT((*registers)[src1(bc)].isBoolean());
|
|
|
|
if (!(*registers)[src1(bc)].boolean) {
|
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + ofs(bc);
|
2000-04-26 05:42:00 +04:00
|
|
|
continue;
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 04:03:23 +04:00
|
|
|
case SHIFTLEFT:
|
|
|
|
case SHIFTRIGHT:
|
|
|
|
case USHIFTRIGHT:
|
|
|
|
case AND:
|
|
|
|
case OR:
|
|
|
|
case XOR:
|
2000-04-26 05:42:00 +04:00
|
|
|
case ADD:
|
|
|
|
case SUBTRACT:
|
|
|
|
case MULTIPLY:
|
2000-05-23 04:08:29 +04:00
|
|
|
case DIVIDE:
|
|
|
|
case REMAINDER:
|
|
|
|
case COMPARE_LT:
|
|
|
|
case COMPARE_LE:
|
|
|
|
case COMPARE_EQ:
|
|
|
|
case STRICT_EQ:
|
2000-04-26 05:42:00 +04:00
|
|
|
{
|
|
|
|
Arithmetic* mul = static_cast<Arithmetic*>(instruction);
|
2000-05-11 04:35:06 +04:00
|
|
|
JSValue& dest = (*registers)[dst(mul)];
|
|
|
|
JSValue& r1 = (*registers)[src1(mul)];
|
|
|
|
JSValue& r2 = (*registers)[src2(mul)];
|
2000-05-23 04:08:29 +04:00
|
|
|
const JSValue ovr = findBinaryOverride(r1, r2, BinaryOperator::mapICodeOp(instruction->op()));
|
|
|
|
JSFunction *target = ovr.function;
|
|
|
|
if (target->isNative()) {
|
|
|
|
JSValues argv(2);
|
|
|
|
argv[0] = r1;
|
|
|
|
argv[1] = r2;
|
|
|
|
dest = static_cast<JSBinaryOperator*>(target)->mCode(r1, r2);
|
|
|
|
break;
|
2000-05-19 21:41:10 +04:00
|
|
|
}
|
|
|
|
else {
|
2000-05-23 04:08:29 +04:00
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC,
|
|
|
|
mActivation, dst(mul));
|
|
|
|
mActivation = new Activation(target->getICode(), r1, r2);
|
|
|
|
registers = &mActivation->mRegisters;
|
|
|
|
mPC = mActivation->mICode->its_iCode->begin();
|
|
|
|
continue;
|
2000-05-19 21:41:10 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-11 04:35:06 +04:00
|
|
|
|
|
|
|
case PROP_XCR:
|
|
|
|
{
|
|
|
|
PropXcr *px = static_cast<PropXcr*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(px)];
|
|
|
|
JSValue& base = (*registers)[src1(px)];
|
|
|
|
JSObject *object = base.object;
|
|
|
|
JSValue r = object->getProperty(*src2(px)).toNumber();
|
|
|
|
dest = r;
|
|
|
|
r.f64 += val4(px);
|
|
|
|
object->setProperty(*src2(px), r);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAME_XCR:
|
|
|
|
{
|
|
|
|
NameXcr *nx = static_cast<NameXcr*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(nx)];
|
|
|
|
JSValue r = mGlobal->getVariable(*src1(nx)).toNumber();
|
|
|
|
dest = r;
|
|
|
|
r.f64 += val3(nx);
|
2000-05-18 04:03:23 +04:00
|
|
|
mGlobal->setVariable(*src1(nx), r);
|
2000-05-11 04:35:06 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-05-18 04:03:23 +04:00
|
|
|
case TEST:
|
|
|
|
{
|
|
|
|
Test* tst = static_cast<Test*>(instruction);
|
|
|
|
(*registers)[dst(tst)] = (*registers)[src1(tst)].toBoolean();
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-09 02:59:42 +04:00
|
|
|
case NEGATE:
|
|
|
|
{
|
|
|
|
Negate* neg = static_cast<Negate*>(instruction);
|
2000-05-09 07:11:54 +04:00
|
|
|
(*registers)[dst(neg)] = JSValue(-(*registers)[src1(neg)].toNumber().f64);
|
2000-05-09 02:59:42 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case POSATE:
|
|
|
|
{
|
|
|
|
Posate* pos = static_cast<Posate*>(instruction);
|
2000-05-09 07:11:54 +04:00
|
|
|
(*registers)[dst(pos)] = (*registers)[src1(pos)].toNumber();
|
2000-05-09 02:59:42 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 04:03:23 +04:00
|
|
|
case BITNOT:
|
|
|
|
{
|
|
|
|
Bitnot* bn = static_cast<Bitnot*>(instruction);
|
|
|
|
(*registers)[dst(bn)] = JSValue(~(*registers)[src1(bn)].toInt32().i32);
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 05:42:00 +04:00
|
|
|
case NOT:
|
|
|
|
{
|
|
|
|
Not* nt = static_cast<Not*>(instruction);
|
2000-05-23 04:08:29 +04:00
|
|
|
ASSERT((*registers)[src1(nt)].isBoolean());
|
|
|
|
(*registers)[dst(nt)] = JSValue(!(*registers)[src1(nt)].boolean);
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-04-29 18:43:36 +04:00
|
|
|
case THROW:
|
2000-04-26 05:42:00 +04:00
|
|
|
{
|
2000-04-27 05:27:09 +04:00
|
|
|
Throw* thrw = static_cast<Throw*>(instruction);
|
|
|
|
throw new JSException((*registers)[op1(thrw)]);
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
2000-04-22 02:52:52 +04:00
|
|
|
|
2000-05-06 01:38:16 +04:00
|
|
|
case TRYIN:
|
2000-04-26 05:42:00 +04:00
|
|
|
{ // push the catch handler address onto the try stack
|
2000-05-06 01:38:16 +04:00
|
|
|
Tryin* tri = static_cast<Tryin*>(instruction);
|
2000-04-29 04:23:06 +04:00
|
|
|
mActivation->catchStack.push_back(new Handler(op1(tri),
|
|
|
|
op2(tri)));
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-05-06 01:38:16 +04:00
|
|
|
case TRYOUT:
|
2000-04-26 05:42:00 +04:00
|
|
|
{
|
2000-04-29 04:23:06 +04:00
|
|
|
Handler *h = mActivation->catchStack.back();
|
|
|
|
mActivation->catchStack.pop_back();
|
2000-04-27 05:27:09 +04:00
|
|
|
delete h;
|
2000-04-08 07:23:44 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
break;
|
2000-04-29 18:43:36 +04:00
|
|
|
case JSR:
|
2000-04-27 05:27:09 +04:00
|
|
|
{
|
2000-04-29 18:43:36 +04:00
|
|
|
subroutineStack.push(++mPC);
|
2000-04-27 05:27:09 +04:00
|
|
|
Jsr* jsr = static_cast<Jsr*>(instruction);
|
|
|
|
uint32 offset = ofs(jsr);
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + offset;
|
2000-04-27 05:27:09 +04:00
|
|
|
continue;
|
|
|
|
}
|
2000-04-29 18:43:36 +04:00
|
|
|
case RTS:
|
2000-04-27 05:27:09 +04:00
|
|
|
{
|
|
|
|
ASSERT(!subroutineStack.empty());
|
2000-04-29 18:43:36 +04:00
|
|
|
mPC = subroutineStack.top();
|
2000-04-27 05:27:09 +04:00
|
|
|
subroutineStack.pop();
|
|
|
|
continue;
|
|
|
|
}
|
2000-04-29 18:43:36 +04:00
|
|
|
case WITHIN:
|
|
|
|
{
|
|
|
|
Within* within = static_cast<Within*>(instruction);
|
|
|
|
JSValue& value = (*registers)[op1(within)];
|
|
|
|
assert(value.tag == JSValue::object_tag);
|
|
|
|
mGlobal = new JSScope(mGlobal, value.object);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WITHOUT:
|
|
|
|
{
|
2000-05-01 21:18:49 +04:00
|
|
|
// Without* without = static_cast<Without*>(instruction);
|
2000-04-29 18:43:36 +04:00
|
|
|
mGlobal = mGlobal->getParent();
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 05:42:00 +04:00
|
|
|
default:
|
|
|
|
NOT_REACHED("bad opcode");
|
|
|
|
break;
|
2000-04-22 02:52:52 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
|
|
|
|
// increment the program counter.
|
2000-04-29 18:43:36 +04:00
|
|
|
++mPC;
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
2000-04-27 05:27:09 +04:00
|
|
|
catch (JSException x) {
|
|
|
|
if (mLinkage) {
|
2000-04-29 04:23:06 +04:00
|
|
|
if (mActivation->catchStack.empty()) {
|
2000-04-27 05:27:09 +04:00
|
|
|
Linkage *pLinkage = mLinkage;
|
|
|
|
for (; pLinkage != NULL; pLinkage = pLinkage->mNext) {
|
|
|
|
if (!pLinkage->mActivation->catchStack.empty()) {
|
2000-04-29 04:23:06 +04:00
|
|
|
mActivation = pLinkage->mActivation;
|
|
|
|
Handler *h = mActivation->catchStack.back();
|
|
|
|
registers = &mActivation->mRegisters;
|
2000-04-27 05:27:09 +04:00
|
|
|
if (h->catchTarget) {
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + h->catchTarget->mOffset;
|
2000-04-27 05:27:09 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ASSERT(h->finallyTarget);
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + h->finallyTarget->mOffset;
|
2000-04-27 05:27:09 +04:00
|
|
|
}
|
|
|
|
mLinkage = pLinkage;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pLinkage)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2000-04-29 04:23:06 +04:00
|
|
|
Handler *h = mActivation->catchStack.back();
|
2000-04-27 05:27:09 +04:00
|
|
|
if (h->catchTarget) {
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + h->catchTarget->mOffset;
|
2000-04-27 05:27:09 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ASSERT(h->finallyTarget);
|
2000-05-19 21:41:10 +04:00
|
|
|
mPC = mActivation->mICode->its_iCode->begin() + h->finallyTarget->mOffset;
|
2000-04-27 05:27:09 +04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2000-04-29 04:23:06 +04:00
|
|
|
rv = x.value;
|
2000-04-19 01:51:45 +04:00
|
|
|
}
|
2000-04-26 05:42:00 +04:00
|
|
|
}
|
2000-04-29 04:23:06 +04:00
|
|
|
|
|
|
|
return rv;
|
2000-04-26 05:42:00 +04:00
|
|
|
} /* interpret */
|
2000-04-05 10:05:57 +04:00
|
|
|
|
2000-04-26 09:35:07 +04:00
|
|
|
void Context::addListener(Listener* listener)
|
|
|
|
{
|
|
|
|
mListeners.push_back(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Context::removeListener(Listener* listener)
|
2000-05-12 05:19:39 +04:00
|
|
|
{
|
|
|
|
ListenerIterator e = mListeners.end();
|
2000-05-18 04:03:23 +04:00
|
|
|
ListenerIterator l = std::find(mListeners.begin(), e, listener);
|
2000-05-12 05:19:39 +04:00
|
|
|
if (l != e) mListeners.erase(l);
|
2000-04-26 09:35:07 +04:00
|
|
|
}
|
|
|
|
|
2000-05-12 05:19:39 +04:00
|
|
|
void Context::broadcast(Event event)
|
2000-04-26 09:35:07 +04:00
|
|
|
{
|
2000-04-29 04:23:06 +04:00
|
|
|
for (ListenerIterator i = mListeners.begin(), e = mListeners.end();
|
|
|
|
i != e; ++i) {
|
2000-04-26 09:35:07 +04:00
|
|
|
Listener* listener = *i;
|
2000-05-12 05:19:39 +04:00
|
|
|
listener->listen(this, event);
|
2000-04-26 09:35:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context::Frame* Context::getFrames()
|
|
|
|
{
|
|
|
|
return mLinkage;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace Interpreter */
|
2000-04-18 11:14:49 +04:00
|
|
|
} /* namespace JavaScript */
|