2000-04-21 00:04:14 +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.
|
|
|
|
*/
|
2000-04-05 06:05:57 +00:00
|
|
|
|
|
|
|
#include "interpreter.h"
|
2000-06-23 23:49:48 +00:00
|
|
|
#include "jsclasses.h"
|
2000-04-05 20:33:41 +00:00
|
|
|
#include "world.h"
|
2000-08-04 20:28:35 +00:00
|
|
|
#include "parser.h"
|
2000-07-21 01:16:49 +00:00
|
|
|
#include "jsmath.h"
|
2000-04-05 06:05:57 +00:00
|
|
|
|
2000-04-29 00:29:56 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2000-04-05 06:10:53 +00:00
|
|
|
namespace JavaScript {
|
2000-04-26 05:35:07 +00:00
|
|
|
namespace Interpreter {
|
|
|
|
|
|
|
|
// operand access macros.
|
2000-04-18 00:17:34 +00:00
|
|
|
#define op1(i) (i->o1())
|
|
|
|
#define op2(i) (i->o2())
|
|
|
|
#define op3(i) (i->o3())
|
2000-05-11 00:35:06 +00:00
|
|
|
#define op4(i) (i->o4())
|
2000-04-05 06:05:57 +00:00
|
|
|
|
2000-04-26 05:35:07 +00:00
|
|
|
// mnemonic names for operands.
|
2000-04-18 00:17:34 +00:00
|
|
|
#define dst(i) op1(i)
|
2000-04-11 03:11:00 +00:00
|
|
|
#define src1(i) op2(i)
|
|
|
|
#define src2(i) op3(i)
|
2000-04-18 00:17:34 +00:00
|
|
|
#define ofs(i) (i->getOffset())
|
2000-07-13 00:14:54 +00:00
|
|
|
#define val2(i) op2(i)
|
2000-05-11 00:35:06 +00:00
|
|
|
#define val3(i) op3(i)
|
|
|
|
#define val4(i) op4(i)
|
2000-04-11 03:11:00 +00:00
|
|
|
|
2000-04-26 05:35:07 +00:00
|
|
|
using namespace ICG;
|
|
|
|
using namespace JSTypes;
|
2000-06-24 00:50:59 +00:00
|
|
|
using namespace JSClasses;
|
2000-07-21 01:16:49 +00:00
|
|
|
using namespace JSMathClass;
|
2000-04-21 00:37:51 +00:00
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
// These classes are private to the JS interpreter.
|
|
|
|
|
2000-07-13 01:35:57 +00:00
|
|
|
/**
|
|
|
|
* exception-safe class to save off values.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class autosaver {
|
|
|
|
T& mRef;
|
|
|
|
T mOld;
|
|
|
|
public:
|
|
|
|
autosaver(T& ref, T val = T()) : mRef(ref), mOld(ref) { ref = val; }
|
|
|
|
~autosaver() { mRef = mOld; }
|
|
|
|
};
|
2000-07-11 23:49:20 +00:00
|
|
|
|
2000-08-04 20:28:35 +00:00
|
|
|
ICodeModule* Context::compileFunction(const String &source)
|
2000-07-26 01:56:47 +00:00
|
|
|
{
|
|
|
|
Arena a;
|
|
|
|
String filename = widenCString("Some source source");
|
|
|
|
Parser p(getWorld(), a, source, filename);
|
2000-08-04 20:28:35 +00:00
|
|
|
ExprNode* e = p.parseExpression(false);
|
2000-10-21 00:30:23 +00:00
|
|
|
ICodeGenerator icg(this);
|
2000-08-04 20:28:35 +00:00
|
|
|
ASSERT(e->getKind() == ExprNode::functionLiteral);
|
|
|
|
FunctionExprNode* f = static_cast<FunctionExprNode*>(e);
|
2000-10-27 23:55:31 +00:00
|
|
|
icg.allocateParameter(getWorld().identifiers["this"], false); // always parameter #0
|
2000-08-04 20:28:35 +00:00
|
|
|
VariableBinding* v = f->function.parameters;
|
|
|
|
while (v) {
|
|
|
|
if (v->name && (v->name->getKind() == ExprNode::identifier))
|
2000-10-27 23:55:31 +00:00
|
|
|
icg.allocateParameter((static_cast<IdentifierExprNode*>(v->name))->name, false);
|
2000-08-04 20:28:35 +00:00
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
icg.genStmt(f->function.body);
|
2000-10-09 22:21:26 +00:00
|
|
|
ICodeModule* result = icg.complete(icg.extractType(f->function.resultType));
|
2000-08-04 20:28:35 +00:00
|
|
|
result->setFileName(filename);
|
|
|
|
return result;
|
2000-07-26 01:56:47 +00:00
|
|
|
}
|
|
|
|
|
2000-07-11 23:49:20 +00:00
|
|
|
JSValue Context::readEvalFile(FILE* in, const String& fileName)
|
|
|
|
{
|
|
|
|
String buffer;
|
2000-11-01 01:39:43 +00:00
|
|
|
int ch;
|
|
|
|
while ((ch = getc(in)) != EOF)
|
|
|
|
buffer += static_cast<char>(ch);
|
|
|
|
|
2000-07-11 23:49:20 +00:00
|
|
|
JSValues emptyArgs;
|
|
|
|
JSValue result;
|
|
|
|
|
2000-07-13 01:35:57 +00:00
|
|
|
// save off important member variables, to enable recursive call to interpret.
|
|
|
|
// this is a little stinky, but should be exception-safe.
|
|
|
|
autosaver<Activation*> activation(mActivation, 0);
|
|
|
|
autosaver<Linkage*> linkage(mLinkage, 0);
|
|
|
|
autosaver<InstructionIterator> pc(mPC);
|
|
|
|
|
2000-11-01 01:39:43 +00:00
|
|
|
try {
|
|
|
|
Arena a;
|
|
|
|
Parser p(getWorld(), a, buffer, fileName);
|
|
|
|
StmtNode *parsedStatements = p.parseProgram();
|
|
|
|
/*******/
|
|
|
|
ASSERT(p.lexer.peek(true).hasKind(Token::end));
|
|
|
|
{
|
|
|
|
PrettyPrinter f(stdOut, 30);
|
2000-07-11 23:49:20 +00:00
|
|
|
{
|
2000-11-01 01:39:43 +00:00
|
|
|
PrettyPrinter::Block b(f, 2);
|
|
|
|
f << "Program =";
|
|
|
|
f.linearBreak(1);
|
|
|
|
StmtNode::printStatements(f, parsedStatements);
|
2000-07-11 23:49:20 +00:00
|
|
|
}
|
2000-11-01 01:39:43 +00:00
|
|
|
f.end();
|
|
|
|
}
|
|
|
|
stdOut << '\n';
|
|
|
|
/*******/
|
2000-07-11 23:49:20 +00:00
|
|
|
|
2000-07-26 01:56:47 +00:00
|
|
|
// Generate code for parsedStatements, which is a linked
|
2000-11-01 01:39:43 +00:00
|
|
|
// list of zero or more statements
|
|
|
|
ICodeModule* icm = genCode(parsedStatements, fileName);
|
|
|
|
if (icm) {
|
|
|
|
result = interpret(icm, emptyArgs);
|
|
|
|
delete icm;
|
2000-07-11 23:49:20 +00:00
|
|
|
}
|
2000-11-01 01:39:43 +00:00
|
|
|
} catch (Exception &e) {
|
2000-11-01 02:04:04 +00:00
|
|
|
throw new JSException(e.fullMessage());
|
2000-07-11 23:49:20 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ICodeModule* Context::genCode(StmtNode *p, const String &fileName)
|
|
|
|
{
|
2000-10-21 00:30:23 +00:00
|
|
|
ICodeGenerator icg(this);
|
2000-07-11 23:49:20 +00:00
|
|
|
|
|
|
|
TypedRegister ret(NotARegister, &None_Type);
|
|
|
|
while (p) {
|
|
|
|
ret = icg.genStmt(p);
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
icg.returnStmt(ret);
|
|
|
|
|
2000-10-09 22:21:26 +00:00
|
|
|
ICodeModule *icm = icg.complete(&Void_Type);
|
2000-10-18 23:37:44 +00:00
|
|
|
icm->setFileName(fileName);
|
2000-07-11 23:49:20 +00:00
|
|
|
return icm;
|
|
|
|
}
|
|
|
|
|
2000-10-18 23:37:44 +00:00
|
|
|
void Context::loadClass(const char *fileName)
|
|
|
|
{
|
2000-10-21 00:30:23 +00:00
|
|
|
ICodeGenerator icg(this);
|
2000-10-18 23:37:44 +00:00
|
|
|
icg.readICode(fileName); // loads it into the global object
|
|
|
|
}
|
2000-07-11 23:49:20 +00:00
|
|
|
|
2000-04-29 14:43:36 +00:00
|
|
|
JSValues& Context::getRegisters() { return mActivation->mRegisters; }
|
2000-12-08 23:55:39 +00:00
|
|
|
ICodeModule* Context::getICode() { return mICode; }
|
2000-04-29 14:43:36 +00:00
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
/**
|
|
|
|
* Stores saved state from the *previous* activation, the current
|
|
|
|
* activation is alive and well in locals of the interpreter loop.
|
|
|
|
*/
|
2000-04-28 02:23:08 +00:00
|
|
|
struct Linkage : public Context::Frame, public gc_base {
|
2000-04-26 05:35:07 +00:00
|
|
|
Linkage* mNext; // next linkage in linkage stack.
|
2000-04-26 01:42:00 +00:00
|
|
|
InstructionIterator mReturnPC;
|
2000-04-26 05:35:07 +00:00
|
|
|
Activation* mActivation; // caller's activation.
|
2000-06-27 02:39:32 +00:00
|
|
|
JSScope* mScope;
|
2000-06-20 22:45:45 +00:00
|
|
|
TypedRegister mResult; // the desired target register for the return value
|
2000-12-08 23:55:39 +00:00
|
|
|
ICodeModule* mICode; // the caller function
|
|
|
|
JSClosure* mClosure;
|
2000-04-11 00:32:17 +00:00
|
|
|
|
2000-05-19 17:41:10 +00:00
|
|
|
Linkage(Linkage* linkage, InstructionIterator returnPC,
|
2000-12-08 23:55:39 +00:00
|
|
|
Activation* activation, JSScope* scope, TypedRegister result, ICodeModule* iCode, JSClosure *closure)
|
2000-05-19 17:41:10 +00:00
|
|
|
: mNext(linkage), mReturnPC(returnPC),
|
2000-12-08 23:55:39 +00:00
|
|
|
mActivation(activation), mScope(scope), mResult(result), mICode(iCode), mClosure(closure)
|
2000-04-26 01:42:00 +00:00
|
|
|
{
|
|
|
|
}
|
2000-04-26 05:35:07 +00:00
|
|
|
|
2000-06-27 02:39:32 +00:00
|
|
|
Context::Frame* getNext() { return mNext; }
|
2000-04-26 05:35:07 +00:00
|
|
|
|
2000-06-27 02:39:32 +00:00
|
|
|
void getState(InstructionIterator& pc, JSValues*& registers, ICodeModule*& iCode)
|
2000-04-26 05:35:07 +00:00
|
|
|
{
|
|
|
|
pc = mReturnPC;
|
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-08 23:55:39 +00:00
|
|
|
iCode = mICode;
|
2000-04-26 05:35:07 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
};
|
2000-05-23 00:08:29 +00:00
|
|
|
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue shiftLeft_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-19 17:41:10 +00:00
|
|
|
{
|
2000-05-23 00:08:29 +00:00
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
return JSValue(num1.i32 << (num2.u32 & 0x1F));
|
2000-05-19 17:41:10 +00:00
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue shiftRight_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
return JSValue(num1.i32 >> (num2.u32 & 0x1F));
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue UshiftRight_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toUInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
return JSValue(num1.u32 >> (num2.u32 & 0x1F));
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue and_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
return JSValue(num1.i32 & num2.i32);
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue or_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
return JSValue(num1.i32 | num2.i32);
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue xor_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
return JSValue(num1.i32 ^ num2.i32);
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue add_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00: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()) {
|
2000-06-26 17:09:27 +00:00
|
|
|
JSString& str1 = *r1.toString().string;
|
2000-05-23 00:08:29 +00:00
|
|
|
JSString& str2 = *r2.toString().string;
|
2000-06-26 17:25:25 +00:00
|
|
|
return JSValue(new JSString(str1 + str2));
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 + num2.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-07-11 23:08:03 +00:00
|
|
|
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue subtract_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 - num2.f64);
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue multiply_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-19 17:41:10 +00:00
|
|
|
{
|
2000-05-23 00:08:29 +00:00
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
2000-05-19 17:41:10 +00:00
|
|
|
return JSValue(num1.f64 * num2.f64);
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue divide_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(num1.f64 / num2.f64);
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue remainder_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue num1(r1.toNumber());
|
|
|
|
JSValue num2(r2.toNumber());
|
|
|
|
return JSValue(fmod(num1.f64, num2.f64));
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue less_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue lv = r1.toPrimitive(JSValue::Number);
|
|
|
|
JSValue rv = r2.toPrimitive(JSValue::Number);
|
|
|
|
if (lv.isString() && rv.isString()) {
|
2000-06-28 18:41:30 +00:00
|
|
|
return JSValue(bool(lv.string->compare(*rv.string) < 0));
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv = lv.toNumber();
|
|
|
|
rv = rv.toNumber();
|
|
|
|
if (lv.isNaN() || rv.isNaN())
|
2000-11-06 22:57:10 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
else
|
|
|
|
return JSValue(lv.f64 < rv.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-07-11 23:49:20 +00:00
|
|
|
static JSValue lessOrEqual_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
JSValue lv = r1.toPrimitive(JSValue::Number);
|
|
|
|
JSValue rv = r2.toPrimitive(JSValue::Number);
|
|
|
|
if (lv.isString() && rv.isString()) {
|
2000-06-28 18:41:30 +00:00
|
|
|
return JSValue(bool(lv.string->compare(*rv.string) <= 0));
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lv = lv.toNumber();
|
|
|
|
rv = rv.toNumber();
|
|
|
|
if (lv.isNaN() || rv.isNaN())
|
2000-11-06 22:57:10 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
else
|
|
|
|
return JSValue(lv.f64 <= rv.f64);
|
|
|
|
}
|
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue equal_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
2000-11-06 22:57:10 +00:00
|
|
|
if (r1.isSameType(r2)) {
|
|
|
|
if (r1.isUndefined()) return kTrueValue;
|
|
|
|
if (r1.isNull()) return kTrueValue;
|
|
|
|
if (r1.isNumber()) {
|
|
|
|
JSValue lv = r1.toNumber(); // make sure we have f64's
|
|
|
|
JSValue rv = r2.toNumber();
|
|
|
|
if (lv.isNaN() || rv.isNaN())
|
|
|
|
return kFalseValue;
|
|
|
|
else
|
|
|
|
return JSValue(lv.f64 == rv.f64); // does the right thing for +/- 0
|
|
|
|
}
|
|
|
|
if (r1.isString())
|
|
|
|
return JSValue(bool(r1.string->compare(*r2.string) == 0));
|
|
|
|
if (r1.isBoolean())
|
|
|
|
return JSValue(bool(r1.boolean == r2.boolean));
|
|
|
|
if (r1.isObject())
|
|
|
|
return JSValue(bool(r1.object == r2.object));
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
2000-11-06 22:57:10 +00:00
|
|
|
else { // different types
|
|
|
|
if (r1.isNull() && r2.isUndefined()) return kTrueValue;
|
|
|
|
if (r2.isNull() && r1.isUndefined()) return kTrueValue;
|
|
|
|
if (r1.isNumber() && r2.isString())
|
|
|
|
return equal_Default(r1, r2.toNumber());
|
|
|
|
if (r2.isNumber() && r1.isString())
|
|
|
|
return equal_Default(r1.toNumber(), r2);
|
|
|
|
if (r1.isBoolean())
|
|
|
|
return equal_Default(r1.toNumber(), r2);
|
|
|
|
if (r2.isBoolean())
|
|
|
|
return equal_Default(r1, r2.toNumber());
|
|
|
|
if ((r1.isString() || r1.isNumber()) && r2.isObject())
|
|
|
|
return equal_Default(r1, r2.toPrimitive());
|
|
|
|
if ((r2.isString() || r2.isNumber()) && r1.isObject())
|
|
|
|
return equal_Default(r1.toPrimitive(), r2);
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
2000-11-06 22:57:10 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
2000-05-24 00:54:40 +00:00
|
|
|
static JSValue identical_Default(const JSValue& r1, const JSValue& r2)
|
2000-05-23 00:08:29 +00:00
|
|
|
{
|
|
|
|
if (r1.getType() != r2.getType())
|
2000-06-27 02:39:32 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
if (r1.isUndefined() )
|
2000-06-27 02:39:32 +00:00
|
|
|
return kTrueValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
if (r1.isNull())
|
2000-06-27 02:39:32 +00:00
|
|
|
return kTrueValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
if (r1.isNumber()) {
|
|
|
|
if (r1.isNaN())
|
2000-06-27 02:39:32 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
if (r2.isNaN())
|
2000-06-27 02:39:32 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
return JSValue(r1.f64 == r2.f64);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (r1.isString())
|
2000-07-13 01:35:57 +00:00
|
|
|
return JSValue(bool(r1.string->compare(*r2.string) == 0));
|
2000-05-23 00:08:29 +00:00
|
|
|
if (r1.isBoolean())
|
2000-07-13 01:35:57 +00:00
|
|
|
return JSValue(bool(r1.boolean == r2.boolean));
|
2000-05-23 00:08:29 +00:00
|
|
|
if (r1.isObject())
|
2000-07-13 01:35:57 +00:00
|
|
|
return JSValue(bool(r1.object == r2.object));
|
2000-06-27 02:39:32 +00:00
|
|
|
return kFalseValue;
|
2000-05-23 00:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-19 17:41:10 +00:00
|
|
|
|
|
|
|
|
2000-07-13 00:14:54 +00:00
|
|
|
static JSValue defineAdd(Context *cx, const JSValues& argv)
|
2000-06-16 01:36:59 +00:00
|
|
|
{
|
|
|
|
// should be three args, first two are types, third is a function.
|
|
|
|
ASSERT(argv[0].isType());
|
|
|
|
ASSERT(argv[1].isType());
|
|
|
|
ASSERT(argv[2].isFunction());
|
|
|
|
|
|
|
|
// XXX need to prove that argv[2].function takes T1 and T2 as args and returns Boolean for the relational operators ?
|
|
|
|
|
2000-07-13 00:14:54 +00:00
|
|
|
cx->addBinaryOperator(BinaryOperator::Add, new BinaryOperator(argv[0].type, argv[1].type, argv[2].function));
|
2000-06-16 01:36:59 +00:00
|
|
|
|
|
|
|
return kUndefinedValue;
|
|
|
|
}
|
|
|
|
|
2000-07-11 23:49:20 +00:00
|
|
|
#define DEFINE_OBO(NAME) \
|
2000-07-13 00:14:54 +00:00
|
|
|
static JSValue define##NAME(Context *cx, const JSValues& argv) \
|
2000-07-11 23:49:20 +00:00
|
|
|
{ \
|
|
|
|
ASSERT(argv[0].isType()); \
|
|
|
|
ASSERT(argv[1].isType()); \
|
|
|
|
ASSERT(argv[2].isFunction()); \
|
2000-07-13 00:14:54 +00:00
|
|
|
cx->addBinaryOperator(BinaryOperator::##NAME, \
|
2000-07-11 23:49:20 +00:00
|
|
|
new BinaryOperator(argv[0].type, argv[1].type, argv[2].function)); \
|
|
|
|
return kUndefinedValue; \
|
|
|
|
} \
|
|
|
|
|
|
|
|
DEFINE_OBO(Subtract)
|
|
|
|
DEFINE_OBO(Multiply)
|
|
|
|
DEFINE_OBO(Divide)
|
|
|
|
DEFINE_OBO(Remainder)
|
|
|
|
DEFINE_OBO(LeftShift)
|
|
|
|
DEFINE_OBO(RightShift)
|
|
|
|
DEFINE_OBO(LogicalRightShift)
|
|
|
|
DEFINE_OBO(BitwiseOr)
|
|
|
|
DEFINE_OBO(BitwiseXor)
|
|
|
|
DEFINE_OBO(BitwiseAnd)
|
|
|
|
DEFINE_OBO(Less)
|
|
|
|
DEFINE_OBO(LessOrEqual)
|
|
|
|
DEFINE_OBO(Equal)
|
|
|
|
DEFINE_OBO(Identical)
|
|
|
|
|
2000-07-13 00:14:54 +00:00
|
|
|
void Context::initOperatorsPackage()
|
|
|
|
{
|
|
|
|
// hack - the following should be available only after importing the 'Operators' package
|
|
|
|
// (hmm, how will that work - the import needs to connect the functions into this mechanism
|
|
|
|
// do we watch for the specific package name???)
|
|
|
|
|
|
|
|
struct OBO {
|
|
|
|
char *name;
|
|
|
|
JSNativeFunction::JSCode fun;
|
|
|
|
} OBOs[] = {
|
|
|
|
{ "defineAdd", defineAdd },
|
|
|
|
{ "defineSubtract", defineSubtract },
|
|
|
|
{ "defineMultiply", defineMultiply },
|
|
|
|
{ "defineDivide", defineDivide },
|
|
|
|
{ "defineRemainder", defineRemainder },
|
|
|
|
{ "defineLeftShift", defineLeftShift },
|
|
|
|
{ "defineRightShift", defineRightShift },
|
|
|
|
{ "defineLogicalRightShift",defineLogicalRightShift },
|
|
|
|
{ "defineBitwiseOr", defineBitwiseOr },
|
|
|
|
{ "defineBitwiseXor", defineBitwiseXor },
|
|
|
|
{ "defineBitwiseAnd", defineBitwiseAnd },
|
|
|
|
{ "defineLess", defineLess },
|
|
|
|
{ "defineLessOrEqual", defineLessOrEqual },
|
|
|
|
{ "defineEqual", defineEqual },
|
|
|
|
{ "defineIdentical", defineIdentical },
|
|
|
|
};
|
|
|
|
|
2000-09-19 20:35:05 +00:00
|
|
|
for (uint i = 0; i < sizeof(OBOs) / sizeof(struct OBO); i++)
|
2000-07-13 00:14:54 +00:00
|
|
|
mGlobal->defineNativeFunction(mWorld.identifiers[widenCString(OBOs[i].name)], OBOs[i].fun);
|
|
|
|
|
|
|
|
mHasOperatorsPackageLoaded = true;
|
|
|
|
}
|
|
|
|
|
2000-06-16 01:36:59 +00:00
|
|
|
void Context::initContext()
|
|
|
|
{
|
2000-06-23 05:10:46 +00:00
|
|
|
// if global has a parent, assume it's been initialized already.
|
|
|
|
if (mGlobal->getParent())
|
|
|
|
return;
|
|
|
|
|
2000-07-11 23:49:20 +00:00
|
|
|
// predefine the umm, predefined types;
|
|
|
|
struct PDT {
|
|
|
|
char *name;
|
|
|
|
JSType *type;
|
|
|
|
} PDTs[] = {
|
|
|
|
{ "any", &Any_Type },
|
|
|
|
{ "Integer", &Integer_Type },
|
|
|
|
{ "Number", &Number_Type },
|
|
|
|
{ "Character", &Character_Type },
|
|
|
|
{ "String", &String_Type },
|
|
|
|
{ "Function", &Function_Type },
|
|
|
|
{ "Array", &Array_Type },
|
|
|
|
{ "Type", &Type_Type },
|
|
|
|
{ "Boolean", &Boolean_Type },
|
|
|
|
{ "Null", &Null_Type },
|
|
|
|
{ "Void", &Void_Type },
|
|
|
|
{ "none", &None_Type }
|
|
|
|
};
|
2000-06-16 01:36:59 +00:00
|
|
|
|
2000-09-19 20:35:05 +00:00
|
|
|
for (uint i = 0; i < sizeof(PDTs) / sizeof(struct PDT); i++)
|
2000-07-11 23:49:20 +00:00
|
|
|
mGlobal->defineVariable(widenCString(PDTs[i].name), &Type_Type, JSValue(PDTs[i].type));
|
2000-06-16 01:36:59 +00:00
|
|
|
|
2000-07-21 01:16:49 +00:00
|
|
|
// set up the correct [[Class]] for the global object (matching SpiderMonkey)
|
|
|
|
mGlobal->setClass(new JSString("global"));
|
|
|
|
|
2000-07-25 22:58:04 +00:00
|
|
|
|
2000-07-21 01:16:49 +00:00
|
|
|
// add (XXX some) of the global object properties
|
|
|
|
mGlobal->setProperty(widenCString("NaN"), kNaNValue);
|
|
|
|
mGlobal->setProperty(widenCString("undefined"), kUndefinedValue);
|
|
|
|
|
2000-07-25 22:58:04 +00:00
|
|
|
|
2000-07-21 01:16:49 +00:00
|
|
|
// 'Object', 'Date', 'RegExp', 'Array' etc are all (constructor) properties of the global object
|
2000-08-04 20:28:35 +00:00
|
|
|
// Some of these overlap with the predefined types above. The way we handle this is to set a
|
|
|
|
// 'constructor' function for types. When new <typename> is encountered, the type is queried for
|
|
|
|
// a 'constructor' function to be invoked.
|
2000-07-25 22:58:04 +00:00
|
|
|
JSObject::initObjectObject(mGlobal);
|
2000-08-04 20:28:35 +00:00
|
|
|
|
2000-07-26 01:56:47 +00:00
|
|
|
JSFunction::initFunctionObject(mGlobal);
|
2000-08-04 20:28:35 +00:00
|
|
|
JSBoolean::initBooleanObject(mGlobal);
|
2000-07-25 22:58:04 +00:00
|
|
|
|
|
|
|
// the 'Math' object just has some useful properties
|
|
|
|
JSMath::initMathObject(mGlobal);
|
2000-11-01 01:39:43 +00:00
|
|
|
JSArray::initArrayObject(mGlobal);
|
2000-07-21 01:16:49 +00:00
|
|
|
|
|
|
|
// This initializes the state of the binary operator overload mechanism.
|
|
|
|
// One could argue that it is unneccessary to do this until the 'Operators'
|
|
|
|
// package is loaded and that all (un-typed) binary operators should use a
|
|
|
|
// form of icode that performed the inline operation instead.
|
2000-07-13 00:14:54 +00:00
|
|
|
JSBinaryOperator::JSBinaryCode defaultFunction[] = {
|
|
|
|
add_Default,
|
|
|
|
subtract_Default,
|
|
|
|
multiply_Default,
|
|
|
|
divide_Default,
|
|
|
|
remainder_Default,
|
|
|
|
shiftLeft_Default,
|
|
|
|
shiftRight_Default,
|
|
|
|
UshiftRight_Default,
|
|
|
|
or_Default,
|
|
|
|
xor_Default,
|
|
|
|
and_Default,
|
|
|
|
less_Default,
|
|
|
|
lessOrEqual_Default,
|
|
|
|
equal_Default,
|
|
|
|
identical_Default
|
2000-07-11 23:49:20 +00:00
|
|
|
};
|
|
|
|
|
2000-07-13 00:14:54 +00:00
|
|
|
for (BinaryOperator::BinaryOp b = BinaryOperator::BinaryOperatorFirst;
|
|
|
|
b < BinaryOperator::BinaryOperatorCount;
|
|
|
|
b = (BinaryOperator::BinaryOp)(b + 1) )
|
|
|
|
addBinaryOperator(b, new BinaryOperator(&Any_Type, &Any_Type, new JSBinaryOperator(defaultFunction[b])));
|
|
|
|
|
2000-06-16 01:36:59 +00:00
|
|
|
}
|
2000-05-19 17:41:10 +00:00
|
|
|
|
2000-07-13 00:14:54 +00:00
|
|
|
const JSValue Context::findBinaryOverride(JSValue &operand1, JSValue &operand2, BinaryOperator::BinaryOp op)
|
2000-05-19 17:41:10 +00:00
|
|
|
{
|
|
|
|
int32 bestDist1 = JSType::NoRelation;
|
|
|
|
int32 bestDist2 = JSType::NoRelation;
|
|
|
|
BinaryOperatorList::iterator candidate = NULL;
|
|
|
|
|
2000-07-13 00:14:54 +00:00
|
|
|
for (BinaryOperatorList::iterator i = mBinaryOperators[op].begin();
|
|
|
|
i != mBinaryOperators[op].end(); i++)
|
2000-05-19 17:41:10 +00:00
|
|
|
{
|
|
|
|
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 00:04:14 +00:00
|
|
|
|
2000-09-02 01:01:04 +00:00
|
|
|
|
|
|
|
bool Context::hasNamedArguments(ArgumentList &args)
|
|
|
|
{
|
|
|
|
Argument* e = args.end();
|
|
|
|
for (ArgumentList::iterator r = args.begin(); r != e; r++) {
|
|
|
|
if ((*r).second) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
JSValue Context::interpret(ICodeModule* iCode, const JSValues& args)
|
|
|
|
{
|
2000-04-29 00:23:06 +00:00
|
|
|
assert(mActivation == 0); /* recursion == bad */
|
|
|
|
|
|
|
|
JSValue rv;
|
2000-05-19 17:41:10 +00:00
|
|
|
|
2000-07-21 01:16:49 +00:00
|
|
|
// when invoked with empty args, make sure that 'this' is
|
|
|
|
// going to be the global object.
|
|
|
|
|
2000-12-08 23:55:39 +00:00
|
|
|
mICode = iCode;
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, args);
|
2000-04-29 00:23:06 +00:00
|
|
|
JSValues* registers = &mActivation->mRegisters;
|
2000-07-21 01:16:49 +00:00
|
|
|
if (args.size() == 0) (*registers)[0] = mGlobal;
|
2000-04-21 00:04:14 +00:00
|
|
|
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
InstructionIterator endPC = mICode->its_iCode->end();
|
2000-04-26 01:42:00 +00:00
|
|
|
|
2000-04-27 01:27:09 +00:00
|
|
|
// stack of all catch/finally handlers available for the current activation
|
2000-04-29 00:23:06 +00:00
|
|
|
// to implement jsr/rts for finally code
|
|
|
|
std::stack<InstructionIterator> subroutineStack;
|
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
while (true) {
|
|
|
|
try {
|
2000-04-26 05:35:07 +00:00
|
|
|
// tell any listeners about the current execution state.
|
|
|
|
// XXX should only do this if we're single stepping/tracing.
|
2000-04-29 00:23:06 +00:00
|
|
|
if (mListeners.size())
|
2000-05-12 01:19:39 +00:00
|
|
|
broadcast(EV_STEP);
|
2000-04-29 00:23:06 +00:00
|
|
|
|
2000-06-23 06:10:14 +00:00
|
|
|
assert(mPC != endPC);
|
2000-04-29 14:43:36 +00:00
|
|
|
Instruction* instruction = *mPC;
|
2000-04-26 01:42:00 +00:00
|
|
|
switch (instruction->op()) {
|
2000-07-10 23:18:07 +00:00
|
|
|
case CAST:
|
|
|
|
{
|
|
|
|
Cast* c = static_cast<Cast*>(instruction);
|
|
|
|
JSType *toType = op3(c);
|
2000-07-11 20:54:06 +00:00
|
|
|
(*registers)[dst(c).first] = (*registers)[src1(c).first].convert(toType);
|
2000-07-10 23:18:07 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-07-08 01:08:29 +00:00
|
|
|
case SUPER:
|
|
|
|
{
|
|
|
|
Super* su = static_cast<Super*>(instruction);
|
|
|
|
ASSERT((*registers)[0].isObject()); // should be scope of current class
|
|
|
|
JSScope *s = static_cast<JSScope *>((*registers)[0].object->getPrototype());
|
|
|
|
(*registers)[dst(su).first] = s;
|
|
|
|
}
|
|
|
|
break;
|
2000-06-21 22:32:21 +00:00
|
|
|
|
2000-07-18 20:53:03 +00:00
|
|
|
case GET_METHOD:
|
2000-06-28 18:41:30 +00:00
|
|
|
{
|
2000-07-18 20:53:03 +00:00
|
|
|
GetMethod* gm = static_cast<GetMethod*>(instruction);
|
|
|
|
JSValue base = (*registers)[src1(gm).first];
|
|
|
|
ASSERT(base.isObject()); // XXX runtime error
|
|
|
|
JSClass *theClass = dynamic_cast<JSClass*>(base.object->getType());
|
|
|
|
ASSERT(theClass);
|
2000-11-16 23:48:42 +00:00
|
|
|
(*registers)[dst(gm).first] = new JSBoundThis(base, theClass->getMethod(src2(gm)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIND_THIS:
|
|
|
|
{
|
|
|
|
BindThis* bt = static_cast<BindThis*>(instruction);
|
|
|
|
JSValue base = (*registers)[src1(bt).first];
|
|
|
|
JSValue target = (*registers)[src2(bt).first];
|
|
|
|
ASSERT(target.isFunction());
|
|
|
|
(*registers)[dst(bt).first] = new JSBoundThis(base, target.function);
|
2000-07-08 01:08:29 +00:00
|
|
|
}
|
2000-07-18 20:53:03 +00:00
|
|
|
break;
|
2000-07-08 01:08:29 +00:00
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
case CALL:
|
|
|
|
{
|
|
|
|
Call* call = static_cast<Call*>(instruction);
|
2000-08-04 20:28:35 +00:00
|
|
|
JSValue v = (*registers)[op2(call).first];
|
|
|
|
JSFunction *target = NULL;
|
|
|
|
if (v.isFunction())
|
|
|
|
target = v.function;
|
|
|
|
else
|
|
|
|
if (v.isObject()) {
|
|
|
|
JSType *t = dynamic_cast<JSType*>(v.object);
|
|
|
|
if (t)
|
|
|
|
target = t->getInvokor();
|
|
|
|
}
|
|
|
|
if (!target)
|
|
|
|
throw new JSException("Call to non callable object");
|
2000-05-08 22:59:42 +00:00
|
|
|
if (target->isNative()) {
|
2000-11-16 23:48:42 +00:00
|
|
|
ArgumentList *params = op3(call);
|
2000-10-21 00:30:23 +00:00
|
|
|
JSValues argv(params->size() + 1);
|
2000-12-08 23:55:39 +00:00
|
|
|
argv[0] = target->getThis();
|
2000-06-21 22:32:21 +00:00
|
|
|
JSValues::size_type i = 1;
|
2000-10-21 00:30:23 +00:00
|
|
|
for (ArgumentList::const_iterator src = params->begin(), end = params->end();
|
2000-05-08 22:59:42 +00:00
|
|
|
src != end; ++src, ++i) {
|
2000-09-02 01:01:04 +00:00
|
|
|
argv[i] = (*registers)[src->first.first];
|
2000-05-08 22:59:42 +00:00
|
|
|
}
|
2000-07-13 00:14:54 +00:00
|
|
|
JSValue result = static_cast<JSNativeFunction*>(target)->mCode(this, argv);
|
2000-06-21 22:32:21 +00:00
|
|
|
if (op1(call).first != NotARegister)
|
|
|
|
(*registers)[op1(call).first] = result;
|
2000-05-08 22:59:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2000-09-02 01:01:04 +00:00
|
|
|
ICodeModule *icm = target->getICode();
|
2000-11-16 23:48:42 +00:00
|
|
|
ArgumentList *args = op3(call);
|
2000-09-02 01:01:04 +00:00
|
|
|
|
2000-11-03 02:42:01 +00:00
|
|
|
// if all the parameters are positional
|
|
|
|
// if (icm->itsParameters->mPositionalCount == icm->itsParameters->size())
|
|
|
|
|
2000-10-27 23:55:31 +00:00
|
|
|
// the parameter count includes 'this' and any named rest parameter
|
2000-09-07 19:22:13 +00:00
|
|
|
//
|
2000-10-27 23:55:31 +00:00
|
|
|
uint32 pCount = icm->itsParameters->size() - 1; // we won't be passing 'this' via the arg list array
|
|
|
|
// callArgs will be the actual args passed to the target, put into correct register order.
|
|
|
|
// It has room for the rest parameter.
|
2000-10-21 00:30:23 +00:00
|
|
|
ArgumentList *callArgs = new ArgumentList(pCount, Argument(TypedRegister(NotARegister, &Null_Type), NULL));
|
2000-09-07 19:22:13 +00:00
|
|
|
|
2000-10-27 23:55:31 +00:00
|
|
|
// don't want to count the rest parameter while processing the others
|
|
|
|
if (icm->itsParameters->mRestParameter != ParameterList::NoRestParameter) pCount--;
|
|
|
|
|
|
|
|
|
2000-09-07 19:22:13 +00:00
|
|
|
uint32 i;
|
2000-10-27 23:55:31 +00:00
|
|
|
JSArray *restArg = NULL;
|
|
|
|
|
|
|
|
// first match all named arguments with their intended target locations
|
2000-10-21 00:30:23 +00:00
|
|
|
for (i = 0; i < args->size(); i++) {
|
2000-10-27 23:55:31 +00:00
|
|
|
|
|
|
|
const StringAtom *argName = (*args)[i].second;
|
|
|
|
|
|
|
|
TypedRegister parameter = icm->itsParameters->findVariable(*argName);
|
|
|
|
if (parameter.first == NotARegister) {
|
|
|
|
// arg name doesn't match any parameter name, it's a candidate
|
|
|
|
// for the rest parameter (if there is one)
|
|
|
|
if (icm->itsParameters->mRestParameter == ParameterList::NoRestParameter)
|
|
|
|
throw new JSException("Named argument doesn't match parameter name in call with no rest parameter");
|
|
|
|
else {
|
|
|
|
if (icm->itsParameters->mRestParameter != ParameterList::HasUnnamedRestParameter) {
|
|
|
|
// if the name is a numeric literal >= 0, use it as an array index
|
|
|
|
// otherwise just set the named property.
|
2000-10-28 00:07:36 +00:00
|
|
|
const char16 *c = argName->data();
|
2000-10-27 23:55:31 +00:00
|
|
|
const char16 *end;
|
2000-10-28 00:07:36 +00:00
|
|
|
double d = stringToDouble(c, c + argName->size(), end);
|
2000-11-01 02:04:04 +00:00
|
|
|
int index = -1;
|
2000-10-27 23:55:31 +00:00
|
|
|
|
|
|
|
if ((d != d) || (d < 0) || (d != (int)d)) { // a non-numeric or negative value
|
|
|
|
if (icm->itsParameters->mRestParameter == ParameterList::HasRestParameterBeforeBar)
|
|
|
|
throw new JSException("Non-numeric or negative argument name for positional rest parameter");
|
|
|
|
}
|
|
|
|
else { // shift the index value down by the number of positional parameters
|
2000-11-21 00:14:55 +00:00
|
|
|
index = (int)d;
|
|
|
|
index -= icm->itsParameters->mPositionalCount;
|
2000-09-07 19:22:13 +00:00
|
|
|
}
|
2000-10-27 23:55:31 +00:00
|
|
|
|
|
|
|
TypedRegister argument = (*args)[i].first; // this is the argument whose name didn't match
|
|
|
|
|
|
|
|
if (restArg == NULL) {
|
|
|
|
// allocate the rest argument and then subvert the register being used for the
|
|
|
|
// argument under consideration to hold the newly created rest argument.
|
|
|
|
restArg = new JSArray();
|
2000-11-01 01:55:13 +00:00
|
|
|
if (index == -1)
|
|
|
|
restArg->setProperty(*argName, (*registers)[argument.first]);
|
|
|
|
else
|
2000-11-21 00:14:55 +00:00
|
|
|
(*restArg)[uint32(index)] = (*registers)[argument.first];
|
2000-10-27 23:55:31 +00:00
|
|
|
(*registers)[argument.first] = restArg;
|
|
|
|
// The callArgs for the rest parameter position gets loaded from that slot
|
|
|
|
(*callArgs)[pCount] = Argument(TypedRegister(argument.first, &Array_Type), NULL);
|
|
|
|
}
|
2000-11-01 01:55:13 +00:00
|
|
|
else {
|
|
|
|
if (index == -1)
|
|
|
|
restArg->setProperty(*argName, (*registers)[argument.first]);
|
|
|
|
else
|
2000-11-21 00:14:55 +00:00
|
|
|
(*restArg)[uint32(index)] = (*registers)[argument.first];
|
2000-11-01 01:55:13 +00:00
|
|
|
}
|
2000-09-07 19:22:13 +00:00
|
|
|
}
|
2000-10-27 23:55:31 +00:00
|
|
|
// else just throw it away
|
2000-09-02 01:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-10-27 23:55:31 +00:00
|
|
|
uint32 targetIndex = parameter.first - 1; // this is the register number we're targetting
|
|
|
|
TypedRegister targetParameter = (*callArgs)[targetIndex].first;
|
|
|
|
if (targetParameter.first != NotARegister) // uh oh, some other argument wants this parameter
|
|
|
|
throw new JSException("Two (or more) arguments have the same name");
|
|
|
|
(*callArgs)[targetIndex] = (*args)[i];
|
2000-09-02 01:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
2000-10-27 23:55:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
// make sure that all non-optional parameters have values
|
|
|
|
for (i = 0; i < pCount; i++) {
|
|
|
|
TypedRegister parameter = (*callArgs)[i].first;
|
|
|
|
if (parameter.first == NotARegister) { // doesn't have an assigned argument
|
|
|
|
if (!icm->itsParameters->isOptional(i + 1)) // and parameter (allowing for 'this') doesn't have an optional value
|
|
|
|
throw new JSException("No argument supplied for non-optional parameter");
|
|
|
|
}
|
2000-09-02 01:01:04 +00:00
|
|
|
}
|
2000-10-27 23:55:31 +00:00
|
|
|
|
2000-12-08 23:55:39 +00:00
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC, mActivation, mGlobal, op1(call), mICode, mCurrentClosure);
|
|
|
|
mICode = icm;
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, mActivation, target->getThis(), callArgs);
|
2000-05-08 22:59:42 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
|
|
|
JSClosure *cl = dynamic_cast<JSClosure *>(target);
|
|
|
|
if (cl)
|
|
|
|
mCurrentClosure = cl;
|
2000-05-19 17:41:10 +00:00
|
|
|
continue;
|
2000-05-08 22:59:42 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
|
2000-12-08 23:55:39 +00:00
|
|
|
|
|
|
|
case NEW_CLOSURE:
|
|
|
|
{
|
|
|
|
NewClosure* nc = static_cast<NewClosure*>(instruction);
|
|
|
|
JSClosure* cl = new JSClosure(src1(nc), mActivation, mCurrentClosure);
|
|
|
|
(*registers)[dst(nc).first] = cl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GET_CLOSURE:
|
|
|
|
{
|
|
|
|
GetClosure* gc = static_cast<GetClosure*>(instruction);
|
|
|
|
uint32 count = src1(gc);
|
|
|
|
JSClosure* cl = mCurrentClosure;
|
|
|
|
while (count-- > 0) {
|
|
|
|
ASSERT(cl);
|
|
|
|
cl = cl->getPrevious();
|
|
|
|
}
|
|
|
|
(*registers)[dst(gc).first] = cl->getActivation();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-08-04 20:28:35 +00:00
|
|
|
case DIRECT_CALL:
|
|
|
|
{
|
|
|
|
DirectCall* call = static_cast<DirectCall*>(instruction);
|
|
|
|
JSFunction *target = op2(call);
|
|
|
|
if (target->isNative()) {
|
2000-10-21 00:30:23 +00:00
|
|
|
ArgumentList *params = op3(call);
|
|
|
|
JSValues argv(params->size() + 1);
|
2000-08-04 20:28:35 +00:00
|
|
|
JSValues::size_type i = 1;
|
2000-10-21 00:30:23 +00:00
|
|
|
for (ArgumentList::const_iterator src = params->begin(), end = params->end();
|
2000-08-04 20:28:35 +00:00
|
|
|
src != end; ++src, ++i) {
|
2000-09-02 01:01:04 +00:00
|
|
|
argv[i] = (*registers)[src->first.first];
|
2000-08-04 20:28:35 +00:00
|
|
|
}
|
|
|
|
JSValue result = static_cast<JSNativeFunction*>(target)->mCode(this, argv);
|
|
|
|
if (op1(call).first != NotARegister)
|
|
|
|
(*registers)[op1(call).first] = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC,
|
2000-12-08 23:55:39 +00:00
|
|
|
mActivation, mGlobal, op1(call), mICode, mCurrentClosure);
|
|
|
|
mICode = target->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, mActivation, kNullValue, op3(call));
|
2000-08-04 20:28:35 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
2000-08-04 20:28:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
case RETURN_VOID:
|
|
|
|
{
|
2000-04-26 05:35:07 +00:00
|
|
|
Linkage *linkage = mLinkage;
|
2000-04-26 01:42:00 +00:00
|
|
|
if (!linkage)
|
2000-04-29 00:23:06 +00:00
|
|
|
{
|
2000-04-29 14:43:36 +00:00
|
|
|
// let the garbage collector free activations.
|
|
|
|
mActivation = 0;
|
2000-06-26 15:06:36 +00:00
|
|
|
return kUndefinedValue;
|
2000-04-29 00:23:06 +00:00
|
|
|
}
|
2000-04-26 05:35:07 +00:00
|
|
|
mLinkage = linkage->mNext;
|
2000-04-29 00:23:06 +00:00
|
|
|
mActivation = linkage->mActivation;
|
2000-06-27 02:39:32 +00:00
|
|
|
mGlobal = linkage->mScope;
|
2000-04-29 00:23:06 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-06-24 02:50:23 +00:00
|
|
|
if (linkage->mResult.first != NotARegister)
|
2000-06-26 15:06:36 +00:00
|
|
|
(*registers)[linkage->mResult.first] = kUndefinedValue;
|
2000-04-29 14:43:36 +00:00
|
|
|
mPC = linkage->mReturnPC;
|
2000-12-08 23:55:39 +00:00
|
|
|
mICode = linkage->mICode;
|
|
|
|
mCurrentClosure = linkage->mClosure;
|
|
|
|
endPC = mICode->its_iCode->end();
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case RETURN:
|
|
|
|
{
|
|
|
|
Return* ret = static_cast<Return*>(instruction);
|
2000-04-28 05:43:33 +00:00
|
|
|
JSValue result;
|
2000-06-20 22:45:45 +00:00
|
|
|
if (op1(ret).first != NotARegister)
|
|
|
|
result = (*registers)[op1(ret).first];
|
2000-04-26 05:35:07 +00:00
|
|
|
Linkage* linkage = mLinkage;
|
2000-04-26 01:42:00 +00:00
|
|
|
if (!linkage)
|
2000-04-29 00:23:06 +00:00
|
|
|
{
|
2000-04-29 14:43:36 +00:00
|
|
|
// let the garbage collector free activations.
|
|
|
|
mActivation = 0;
|
|
|
|
return result;
|
2000-04-29 00:23:06 +00:00
|
|
|
}
|
2000-04-26 05:35:07 +00:00
|
|
|
mLinkage = linkage->mNext;
|
2000-04-29 00:23:06 +00:00
|
|
|
mActivation = linkage->mActivation;
|
2000-06-27 02:39:32 +00:00
|
|
|
mGlobal = linkage->mScope;
|
2000-04-29 00:23:06 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-06-24 02:50:23 +00:00
|
|
|
if (linkage->mResult.first != NotARegister)
|
|
|
|
(*registers)[linkage->mResult.first] = result;
|
2000-04-29 14:43:36 +00:00
|
|
|
mPC = linkage->mReturnPC;
|
2000-12-08 23:55:39 +00:00
|
|
|
mICode = linkage->mICode;
|
|
|
|
mCurrentClosure = linkage->mClosure;
|
|
|
|
endPC = mICode->its_iCode->end();
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case MOVE:
|
|
|
|
{
|
|
|
|
Move* mov = static_cast<Move*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
(*registers)[dst(mov).first] = (*registers)[src1(mov).first];
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LOAD_NAME:
|
|
|
|
{
|
2000-09-11 22:10:44 +00:00
|
|
|
LoadName* ln = static_cast<LoadName*>(instruction);
|
|
|
|
JSFunction *getter = mGlobal->getter(*src1(ln));
|
|
|
|
if (getter) {
|
|
|
|
ASSERT(!getter->isNative());
|
2000-12-08 23:55:39 +00:00
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC, mActivation, mGlobal, dst(ln), mICode, mCurrentClosure);
|
|
|
|
mICode = getter->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, kNullValue);
|
2000-09-11 22:10:44 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
2000-09-11 22:10:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
(*registers)[dst(ln).first] = mGlobal->getVariable(*src1(ln));
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SAVE_NAME:
|
|
|
|
{
|
|
|
|
SaveName* sn = static_cast<SaveName*>(instruction);
|
2000-09-11 22:10:44 +00:00
|
|
|
JSFunction *setter = mGlobal->setter(*dst(sn));
|
|
|
|
if (setter) {
|
|
|
|
ASSERT(!setter->isNative());
|
2000-12-08 23:55:39 +00:00
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC, mActivation, mGlobal, TypedRegister(NotARegister, &Null_Type), mICode, mCurrentClosure);
|
|
|
|
mICode = setter->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, (*registers)[src1(sn).first], kNullValue);
|
2000-09-11 22:10:44 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
2000-09-11 22:10:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mGlobal->setVariable(*dst(sn), (*registers)[src1(sn).first]);
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEW_OBJECT:
|
|
|
|
{
|
|
|
|
NewObject* no = static_cast<NewObject*>(instruction);
|
2000-07-25 22:58:04 +00:00
|
|
|
if (src1(no).first != NotARegister)
|
|
|
|
(*registers)[dst(no).first] = new JSObject((*registers)[src1(no).first]);
|
|
|
|
else
|
|
|
|
(*registers)[dst(no).first] = new JSObject();
|
2000-06-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NEW_CLASS:
|
|
|
|
{
|
|
|
|
NewClass* nc = static_cast<NewClass*>(instruction);
|
2000-07-08 01:08:29 +00:00
|
|
|
JSClass* thisClass = src1(nc);
|
|
|
|
JSInstance* thisInstance = new(thisClass) JSInstance(thisClass);
|
|
|
|
(*registers)[dst(nc).first] = thisInstance;
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-21 22:32:21 +00:00
|
|
|
case NEW_FUNCTION:
|
|
|
|
{
|
|
|
|
NewFunction* nf = static_cast<NewFunction*>(instruction);
|
2000-09-11 22:10:44 +00:00
|
|
|
(*registers)[dst(nf).first] = new JSFunction(src1(nf));
|
2000-06-21 22:32:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-04-26 01:42:00 +00:00
|
|
|
case NEW_ARRAY:
|
|
|
|
{
|
|
|
|
NewArray* na = static_cast<NewArray*>(instruction);
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(na).first] = new JSArray();
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-29 18:13:46 +00:00
|
|
|
case DELETE_PROP:
|
|
|
|
{
|
|
|
|
DeleteProp* dp = static_cast<DeleteProp*>(instruction);
|
|
|
|
JSValue& value = (*registers)[src1(dp).first];
|
|
|
|
if (value.isObject() && !value.isType()) {
|
|
|
|
(*registers)[dst(dp).first] = value.object->deleteProperty(*src2(dp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 01:42:00 +00:00
|
|
|
case GET_PROP:
|
|
|
|
{
|
|
|
|
GetProp* gp = static_cast<GetProp*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& value = (*registers)[src1(gp).first];
|
2000-06-23 22:53:09 +00:00
|
|
|
if (value.isObject()) {
|
2000-06-27 02:39:32 +00:00
|
|
|
if (value.isType()) {
|
2000-06-28 16:15:06 +00:00
|
|
|
// REVISIT: should signal error if slot doesn't exist.
|
2000-09-11 22:10:44 +00:00
|
|
|
NOT_REACHED("tell me I'm wrong");
|
2000-06-28 16:15:06 +00:00
|
|
|
JSClass* thisClass = dynamic_cast<JSClass*>(value.type);
|
|
|
|
if (thisClass && thisClass->hasStatic(*src2(gp))) {
|
2000-06-28 16:32:52 +00:00
|
|
|
const JSSlot& slot = thisClass->getStatic(*src2(gp));
|
2000-06-28 16:15:06 +00:00
|
|
|
(*registers)[dst(gp).first] = (*thisClass)[slot.mIndex];
|
|
|
|
}
|
2000-08-04 20:28:35 +00:00
|
|
|
else
|
|
|
|
(*registers)[dst(gp).first] = value.object->getProperty(*src2(gp));
|
2000-06-28 16:15:06 +00:00
|
|
|
} else {
|
2000-12-15 01:38:40 +00:00
|
|
|
JSFunction *getter = value.object->getter(*src2(gp));
|
|
|
|
if (getter) {
|
|
|
|
if (getter->isNative()) {
|
|
|
|
JSValues argv(1);
|
|
|
|
argv[0] = value;
|
|
|
|
JSValue result = static_cast<JSNativeFunction*>(getter)->mCode(this, argv);
|
|
|
|
if (dst(gp).first != NotARegister)
|
|
|
|
(*registers)[dst(gp).first] = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC, mActivation, mGlobal, dst(gp), mICode, mCurrentClosure);
|
|
|
|
mICode = getter->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, value);
|
|
|
|
registers = &mActivation->mRegisters;
|
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
(*registers)[dst(gp).first] = value.object->getProperty(*src2(gp));
|
2000-06-27 02:39:32 +00:00
|
|
|
}
|
2000-04-28 05:43:33 +00:00
|
|
|
}
|
2000-06-21 22:32:21 +00:00
|
|
|
// XXX runtime error
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_PROP:
|
|
|
|
{
|
|
|
|
SetProp* sp = static_cast<SetProp*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& value = (*registers)[dst(sp).first];
|
2000-06-23 22:53:09 +00:00
|
|
|
if (value.isObject()) {
|
2000-06-27 02:39:32 +00:00
|
|
|
if (value.isType()) {
|
2000-06-28 16:15:06 +00:00
|
|
|
// REVISIT: should signal error if slot doesn't exist.
|
2000-09-11 22:10:44 +00:00
|
|
|
NOT_REACHED("tell me I'm wrong");
|
2000-06-28 16:15:06 +00:00
|
|
|
JSClass* thisClass = dynamic_cast<JSClass*>(value.object);
|
|
|
|
if (thisClass && thisClass->hasStatic(*src1(sp))) {
|
2000-06-28 16:32:52 +00:00
|
|
|
const JSSlot& slot = thisClass->getStatic(*src1(sp));
|
2000-06-28 16:15:06 +00:00
|
|
|
(*thisClass)[slot.mIndex] = (*registers)[src2(sp).first];
|
|
|
|
}
|
2000-08-04 20:28:35 +00:00
|
|
|
else
|
|
|
|
value.object->setProperty(*src1(sp), (*registers)[src2(sp).first]);
|
2000-06-28 16:15:06 +00:00
|
|
|
} else {
|
|
|
|
value.object->setProperty(*src1(sp), (*registers)[src2(sp).first]);
|
2000-06-27 02:39:32 +00:00
|
|
|
}
|
2000-04-28 05:43:33 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-27 02:39:32 +00:00
|
|
|
case GET_STATIC:
|
|
|
|
{
|
|
|
|
GetStatic* gs = static_cast<GetStatic*>(instruction);
|
|
|
|
JSClass* c = src1(gs);
|
2000-06-28 16:15:06 +00:00
|
|
|
(*registers)[dst(gs).first] = (*c)[src2(gs)];
|
2000-06-27 02:39:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_STATIC:
|
|
|
|
{
|
|
|
|
SetStatic* ss = static_cast<SetStatic*>(instruction);
|
|
|
|
JSClass* c = dst(ss);
|
2000-06-28 16:15:06 +00:00
|
|
|
(*c)[src1(ss)] = (*registers)[src2(ss).first];
|
2000-06-27 02:39:32 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
/*
|
2000-05-19 17:41:10 +00: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 00:03:23 +00: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 01:42:00 +00:00
|
|
|
case GET_ELEMENT:
|
|
|
|
{
|
|
|
|
GetElement* ge = static_cast<GetElement*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& value = (*registers)[src1(ge).first];
|
2000-04-28 05:43:33 +00:00
|
|
|
if (value.tag == JSValue::array_tag) {
|
|
|
|
JSArray* array = value.array;
|
2000-06-20 22:45:45 +00:00
|
|
|
(*registers)[dst(ge).first] = (*array)[(*registers)[src2(ge).first]];
|
2000-04-28 05:43:33 +00:00
|
|
|
}
|
2000-11-01 01:39:43 +00:00
|
|
|
// FIXME - else case does what/? GET_PROPERTY of toString(index) ?
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_ELEMENT:
|
|
|
|
{
|
|
|
|
SetElement* se = static_cast<SetElement*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& value = (*registers)[dst(se).first];
|
2000-04-28 05:43:33 +00:00
|
|
|
if (value.tag == JSValue::array_tag) {
|
|
|
|
JSArray* array = value.array;
|
2000-06-20 22:45:45 +00:00
|
|
|
(*array)[(*registers)[src1(se).first]] = (*registers)[src2(se).first];
|
2000-04-28 05:43:33 +00:00
|
|
|
}
|
2000-11-01 01:39:43 +00:00
|
|
|
// FIXME - else case does what/? SET_PROPERTY of toString(index) ?
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
|
2000-06-24 00:50:59 +00:00
|
|
|
case GET_SLOT:
|
|
|
|
{
|
|
|
|
GetSlot* gs = static_cast<GetSlot*>(instruction);
|
|
|
|
JSValue& value = (*registers)[src1(gs).first];
|
|
|
|
if (value.isObject()) {
|
2000-12-08 23:55:39 +00:00
|
|
|
JSInstance* inst = dynamic_cast<JSInstance *>(value.object);
|
|
|
|
if (inst) {
|
|
|
|
if (inst->hasGetter(src2(gs))) {
|
|
|
|
JSFunction* getter = inst->getter(src2(gs));
|
2000-12-15 01:38:40 +00:00
|
|
|
if (getter->isNative()) {
|
|
|
|
JSValues argv(1);
|
|
|
|
argv[0] = value;
|
|
|
|
JSValue result = static_cast<JSNativeFunction*>(getter)->mCode(this, argv);
|
|
|
|
if (dst(gs).first != NotARegister)
|
|
|
|
(*registers)[dst(gs).first] = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC, mActivation, mGlobal, dst(gs), mICode, mCurrentClosure);
|
|
|
|
mICode = getter->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, value);
|
|
|
|
registers = &mActivation->mRegisters;
|
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
|
|
|
continue;
|
|
|
|
}
|
2000-12-08 23:55:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(*registers)[dst(gs).first] = (*inst)[src2(gs)];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Activation* act = dynamic_cast<Activation *>(value.object);
|
|
|
|
if (act) {
|
|
|
|
(*registers)[dst(gs).first] = act->mRegisters[src2(gs)];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NOT_REACHED("runtime error");
|
2000-10-09 22:21:26 +00:00
|
|
|
}
|
2000-06-24 00:50:59 +00:00
|
|
|
}
|
2000-12-08 23:55:39 +00:00
|
|
|
else
|
|
|
|
NOT_REACHED("runtime error");
|
2000-06-24 00:50:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SET_SLOT:
|
|
|
|
{
|
|
|
|
SetSlot* ss = static_cast<SetSlot*>(instruction);
|
|
|
|
JSValue& value = (*registers)[dst(ss).first];
|
|
|
|
if (value.isObject()) {
|
2000-12-08 23:55:39 +00:00
|
|
|
JSInstance* inst = dynamic_cast<JSInstance *>(value.object);
|
|
|
|
if (inst) {
|
|
|
|
if (inst->hasSetter(src1(ss))) {
|
|
|
|
JSFunction* setter = inst->setter(src1(ss));
|
2000-12-15 01:38:40 +00:00
|
|
|
if (setter->isNative()) {
|
|
|
|
JSValues argv(2);
|
|
|
|
argv[0] = value;
|
|
|
|
argv[1] = (*registers)[src2(ss).first];
|
|
|
|
JSValue result = static_cast<JSNativeFunction*>(setter)->mCode(this, argv);
|
|
|
|
if (dst(ss).first != NotARegister)
|
|
|
|
(*registers)[dst(ss).first] = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC, mActivation, mGlobal, TypedRegister(NotARegister, &Null_Type), mICode, mCurrentClosure);
|
|
|
|
mICode = setter->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, value, (*registers)[src2(ss).first]);
|
|
|
|
registers = &mActivation->mRegisters;
|
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
|
|
|
continue;
|
|
|
|
}
|
2000-12-08 23:55:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(*inst)[src1(ss)] = (*registers)[src2(ss).first];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Activation* act = dynamic_cast<Activation *>(value.object);
|
|
|
|
if (act) {
|
|
|
|
act->mRegisters[src1(ss)] = (*registers)[src2(ss).first];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NOT_REACHED("runtime error");
|
2000-10-09 22:21:26 +00:00
|
|
|
}
|
2000-06-24 00:50:59 +00:00
|
|
|
}
|
2000-12-08 23:55:39 +00:00
|
|
|
else
|
|
|
|
NOT_REACHED("runtime error");
|
2000-06-24 00:50:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
case LOAD_IMMEDIATE:
|
|
|
|
{
|
|
|
|
LoadImmediate* li = static_cast<LoadImmediate*>(instruction);
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(li).first] = src1(li);
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-08 22:59:42 +00:00
|
|
|
case LOAD_STRING:
|
|
|
|
{
|
|
|
|
LoadString* ls = static_cast<LoadString*>(instruction);
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(ls).first] = src1(ls);
|
2000-05-08 22:59:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-20 22:45:45 +00:00
|
|
|
case LOAD_BOOLEAN:
|
2000-05-18 00:03:23 +00:00
|
|
|
{
|
2000-06-20 22:45:45 +00:00
|
|
|
LoadBoolean* lb = static_cast<LoadBoolean*>(instruction);
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(lb).first] = src1(lb);
|
2000-05-18 00:03:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-04-26 01:42:00 +00:00
|
|
|
case BRANCH:
|
|
|
|
{
|
|
|
|
GenericBranch* bra =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + ofs(bra);
|
2000-04-08 03:23:44 +00:00
|
|
|
continue;
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-19 17:41:10 +00:00
|
|
|
case BRANCH_TRUE:
|
2000-04-26 01:42:00 +00:00
|
|
|
{
|
|
|
|
GenericBranch* bc =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
ASSERT((*registers)[src1(bc).first].isBoolean());
|
|
|
|
if ((*registers)[src1(bc).first].boolean) {
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + ofs(bc);
|
2000-04-26 01:42:00 +00:00
|
|
|
continue;
|
2000-04-21 22:52:52 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-19 17:41:10 +00:00
|
|
|
case BRANCH_FALSE:
|
2000-04-26 01:42:00 +00:00
|
|
|
{
|
|
|
|
GenericBranch* bc =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
ASSERT((*registers)[src1(bc).first].isBoolean());
|
|
|
|
if (!(*registers)[src1(bc).first].boolean) {
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + ofs(bc);
|
2000-04-26 01:42:00 +00:00
|
|
|
continue;
|
2000-04-21 22:52:52 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-09-07 19:22:13 +00:00
|
|
|
case BRANCH_INITIALIZED:
|
|
|
|
{
|
|
|
|
GenericBranch* bc =
|
|
|
|
static_cast<GenericBranch*>(instruction);
|
|
|
|
if ((*registers)[src1(bc).first].isInitialized()) {
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + ofs(bc);
|
2000-09-07 19:22:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-07-13 00:14:54 +00:00
|
|
|
case GENERIC_BINARY_OP:
|
|
|
|
{
|
|
|
|
GenericBinaryOP* gbo = static_cast<GenericBinaryOP*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(gbo).first];
|
|
|
|
JSValue& r1 = (*registers)[val3(gbo).first];
|
|
|
|
JSValue& r2 = (*registers)[val4(gbo).first];
|
|
|
|
const JSValue ovr = findBinaryOverride(r1, r2, val2(gbo));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mLinkage = new Linkage(mLinkage, ++mPC,
|
2000-12-08 23:55:39 +00:00
|
|
|
mActivation, mGlobal, dst(gbo), mICode, mCurrentClosure);
|
|
|
|
mICode = target->getICode();
|
|
|
|
mActivation = new Activation(mICode->itsMaxRegister, kNullValue, r1, r2);
|
2000-07-13 00:14:54 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin();
|
|
|
|
endPC = mICode->its_iCode->end();
|
2000-07-13 00:14:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2000-07-14 01:00:53 +00:00
|
|
|
|
2000-05-18 00:03:23 +00:00
|
|
|
case SHIFTLEFT:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
dest = JSValue(num1.i32 << (num2.u32 & 0x1F));
|
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
case SHIFTRIGHT:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
dest = JSValue(num1.i32 >> (num2.u32 & 0x1F));
|
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
case USHIFTRIGHT:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
JSValue num1(r1.toUInt32());
|
|
|
|
JSValue num2(r2.toUInt32());
|
|
|
|
dest = JSValue(num1.u32 >> (num2.u32 & 0x1F));
|
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
case AND:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
dest = JSValue(num1.i32 & num2.i32);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
case OR:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
dest = JSValue(num1.i32 | num2.i32);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
case XOR:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
JSValue num1(r1.toInt32());
|
|
|
|
JSValue num2(r2.toInt32());
|
|
|
|
dest = JSValue(num1.i32 ^ num2.i32);
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 01:42:00 +00:00
|
|
|
case ADD:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
dest = JSValue(r1.f64 + r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 01:42:00 +00:00
|
|
|
case SUBTRACT:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
dest = JSValue(r1.f64 - r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-04-26 01:42:00 +00:00
|
|
|
case MULTIPLY:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
dest = JSValue(r1.f64 * r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-23 00:08:29 +00:00
|
|
|
case DIVIDE:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
dest = JSValue(r1.f64 / r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-23 00:08:29 +00:00
|
|
|
case REMAINDER:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
dest = JSValue(fmod(r1.f64, r2.f64));
|
|
|
|
}
|
|
|
|
break;
|
2000-05-23 00:08:29 +00:00
|
|
|
case COMPARE_LT:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
if (r1.isNaN() || r2.isNaN())
|
|
|
|
dest = JSValue();
|
|
|
|
else
|
|
|
|
dest = JSValue(r1.f64 < r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-23 00:08:29 +00:00
|
|
|
case COMPARE_LE:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
if (r1.isNaN() || r2.isNaN())
|
|
|
|
dest = JSValue();
|
|
|
|
else
|
|
|
|
dest = JSValue(r1.f64 <= r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-23 00:08:29 +00:00
|
|
|
case COMPARE_EQ:
|
2000-07-14 01:00:53 +00:00
|
|
|
{
|
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
if (r1.isNaN() || r2.isNaN())
|
|
|
|
dest = JSValue();
|
|
|
|
else
|
|
|
|
dest = JSValue(r1.f64 == r2.f64);
|
|
|
|
}
|
|
|
|
break;
|
2000-05-23 00:08:29 +00:00
|
|
|
case STRICT_EQ:
|
2000-04-26 01:42:00 +00:00
|
|
|
{
|
2000-07-13 00:14:54 +00:00
|
|
|
Arithmetic* a = static_cast<Arithmetic*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(a).first];
|
|
|
|
JSValue& r1 = (*registers)[src1(a).first];
|
|
|
|
JSValue& r2 = (*registers)[src2(a).first];
|
2000-07-14 01:00:53 +00:00
|
|
|
ASSERT(r1.isNumber());
|
|
|
|
ASSERT(r2.isNumber());
|
|
|
|
if (r1.isNaN() || r2.isNaN())
|
|
|
|
dest = kFalseValue;
|
|
|
|
else
|
|
|
|
dest = JSValue(r1.f64 == r2.f64);
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-11 00:35:06 +00:00
|
|
|
|
2000-06-14 23:26:15 +00:00
|
|
|
case VAR_XCR:
|
|
|
|
{
|
|
|
|
VarXcr *vx = static_cast<VarXcr*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& dest = (*registers)[dst(vx).first];
|
|
|
|
JSValue r = (*registers)[src1(vx).first].toNumber();
|
2000-06-14 23:26:15 +00:00
|
|
|
dest = r;
|
|
|
|
r.f64 += val3(vx);
|
2000-06-20 22:45:45 +00:00
|
|
|
(*registers)[src1(vx).first] = r;
|
2000-06-14 23:26:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-05-11 00:35:06 +00:00
|
|
|
case PROP_XCR:
|
|
|
|
{
|
|
|
|
PropXcr *px = static_cast<PropXcr*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& dest = (*registers)[dst(px).first];
|
|
|
|
JSValue& base = (*registers)[src1(px).first];
|
2000-05-11 00:35:06 +00:00
|
|
|
JSObject *object = base.object;
|
|
|
|
JSValue r = object->getProperty(*src2(px)).toNumber();
|
|
|
|
dest = r;
|
|
|
|
r.f64 += val4(px);
|
|
|
|
object->setProperty(*src2(px), r);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-24 00:50:59 +00:00
|
|
|
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;
|
2000-06-28 16:15:06 +00:00
|
|
|
|
|
|
|
case STATIC_XCR:
|
|
|
|
{
|
|
|
|
StaticXcr *sx = static_cast<StaticXcr*>(instruction);
|
|
|
|
JSValue& dest = (*registers)[dst(sx).first];
|
|
|
|
JSClass* thisClass = src1(sx);
|
|
|
|
JSValue r = (*thisClass)[src2(sx)].toNumber();
|
|
|
|
dest = r;
|
|
|
|
r.f64 += val4(sx);
|
|
|
|
(*thisClass)[src2(sx)] = r;
|
|
|
|
}
|
|
|
|
break;
|
2000-06-24 00:50:59 +00:00
|
|
|
|
2000-05-11 00:35:06 +00:00
|
|
|
case NAME_XCR:
|
|
|
|
{
|
|
|
|
NameXcr *nx = static_cast<NameXcr*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& dest = (*registers)[dst(nx).first];
|
2000-05-11 00:35:06 +00:00
|
|
|
JSValue r = mGlobal->getVariable(*src1(nx)).toNumber();
|
|
|
|
dest = r;
|
|
|
|
r.f64 += val3(nx);
|
2000-05-18 00:03:23 +00:00
|
|
|
mGlobal->setVariable(*src1(nx), r);
|
2000-05-11 00:35:06 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-05-18 00:03:23 +00:00
|
|
|
case TEST:
|
|
|
|
{
|
|
|
|
Test* tst = static_cast<Test*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
(*registers)[dst(tst).first] = (*registers)[src1(tst).first].toBoolean();
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-08 22:59:42 +00:00
|
|
|
case NEGATE:
|
|
|
|
{
|
|
|
|
Negate* neg = static_cast<Negate*>(instruction);
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(neg).first] = -(*registers)[src1(neg).first].toNumber().f64;
|
2000-05-08 22:59:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case POSATE:
|
|
|
|
{
|
|
|
|
Posate* pos = static_cast<Posate*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
(*registers)[dst(pos).first] = (*registers)[src1(pos).first].toNumber();
|
2000-05-08 22:59:42 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-18 00:03:23 +00:00
|
|
|
case BITNOT:
|
|
|
|
{
|
|
|
|
Bitnot* bn = static_cast<Bitnot*>(instruction);
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(bn).first] = ~(*registers)[src1(bn).first].toInt32().i32;
|
2000-05-18 00:03:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-21 22:32:21 +00:00
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
case NOT:
|
|
|
|
{
|
|
|
|
Not* nt = static_cast<Not*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
ASSERT((*registers)[src1(nt).first].isBoolean());
|
2000-06-27 02:39:32 +00:00
|
|
|
(*registers)[dst(nt).first] = !(*registers)[src1(nt).first].boolean;
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-21 22:32:21 +00:00
|
|
|
|
2000-04-29 14:43:36 +00:00
|
|
|
case THROW:
|
2000-04-26 01:42:00 +00:00
|
|
|
{
|
2000-04-27 01:27:09 +00:00
|
|
|
Throw* thrw = static_cast<Throw*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
throw new JSException((*registers)[op1(thrw).first]);
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
2000-04-21 22:52:52 +00:00
|
|
|
|
2000-05-05 21:38:16 +00:00
|
|
|
case TRYIN:
|
2000-04-26 01:42:00 +00:00
|
|
|
{ // push the catch handler address onto the try stack
|
2000-05-05 21:38:16 +00:00
|
|
|
Tryin* tri = static_cast<Tryin*>(instruction);
|
2000-04-29 00:23:06 +00:00
|
|
|
mActivation->catchStack.push_back(new Handler(op1(tri),
|
|
|
|
op2(tri)));
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-05 21:38:16 +00:00
|
|
|
case TRYOUT:
|
2000-04-26 01:42:00 +00:00
|
|
|
{
|
2000-04-29 00:23:06 +00:00
|
|
|
Handler *h = mActivation->catchStack.back();
|
|
|
|
mActivation->catchStack.pop_back();
|
2000-04-27 01:27:09 +00:00
|
|
|
delete h;
|
2000-04-08 03:23:44 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
break;
|
2000-04-29 14:43:36 +00:00
|
|
|
case JSR:
|
2000-04-27 01:27:09 +00:00
|
|
|
{
|
2000-04-29 14:43:36 +00:00
|
|
|
subroutineStack.push(++mPC);
|
2000-04-27 01:27:09 +00:00
|
|
|
Jsr* jsr = static_cast<Jsr*>(instruction);
|
|
|
|
uint32 offset = ofs(jsr);
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + offset;
|
2000-04-27 01:27:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-04-29 14:43:36 +00:00
|
|
|
case RTS:
|
2000-04-27 01:27:09 +00:00
|
|
|
{
|
|
|
|
ASSERT(!subroutineStack.empty());
|
2000-04-29 14:43:36 +00:00
|
|
|
mPC = subroutineStack.top();
|
2000-04-27 01:27:09 +00:00
|
|
|
subroutineStack.pop();
|
|
|
|
continue;
|
|
|
|
}
|
2000-04-29 14:43:36 +00:00
|
|
|
case WITHIN:
|
|
|
|
{
|
|
|
|
Within* within = static_cast<Within*>(instruction);
|
2000-06-20 22:45:45 +00:00
|
|
|
JSValue& value = (*registers)[op1(within).first];
|
2000-04-29 14:43:36 +00:00
|
|
|
assert(value.tag == JSValue::object_tag);
|
|
|
|
mGlobal = new JSScope(mGlobal, value.object);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WITHOUT:
|
|
|
|
{
|
2000-05-01 17:18:49 +00:00
|
|
|
// Without* without = static_cast<Without*>(instruction);
|
2000-04-29 14:43:36 +00:00
|
|
|
mGlobal = mGlobal->getParent();
|
|
|
|
}
|
|
|
|
break;
|
2000-06-23 22:27:17 +00:00
|
|
|
case DEBUGGER:
|
|
|
|
{
|
|
|
|
if (mListeners.size())
|
|
|
|
broadcast(EV_DEBUG);
|
|
|
|
break;
|
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
default:
|
|
|
|
NOT_REACHED("bad opcode");
|
|
|
|
break;
|
2000-04-21 22:52:52 +00:00
|
|
|
}
|
2000-04-26 01:42:00 +00:00
|
|
|
|
|
|
|
// increment the program counter.
|
2000-04-29 14:43:36 +00:00
|
|
|
++mPC;
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
2000-07-11 20:54:06 +00:00
|
|
|
catch (JSException *x) {
|
2000-04-27 01:27:09 +00:00
|
|
|
if (mLinkage) {
|
2000-04-29 00:23:06 +00:00
|
|
|
if (mActivation->catchStack.empty()) {
|
2000-04-27 01:27:09 +00:00
|
|
|
Linkage *pLinkage = mLinkage;
|
|
|
|
for (; pLinkage != NULL; pLinkage = pLinkage->mNext) {
|
|
|
|
if (!pLinkage->mActivation->catchStack.empty()) {
|
2000-12-30 01:13:06 +00:00
|
|
|
mLinkage = pLinkage;
|
2000-04-29 00:23:06 +00:00
|
|
|
mActivation = pLinkage->mActivation;
|
2000-12-30 01:13:06 +00:00
|
|
|
mGlobal = pLinkage->mScope;
|
|
|
|
mICode = pLinkage->mICode;
|
2000-04-29 00:23:06 +00:00
|
|
|
registers = &mActivation->mRegisters;
|
2000-12-30 01:13:06 +00:00
|
|
|
(*registers)[mICode->mExceptionRegister] = x->value;
|
|
|
|
Handler *h = mActivation->catchStack.back();
|
2000-04-27 01:27:09 +00:00
|
|
|
if (h->catchTarget) {
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + h->catchTarget->mOffset;
|
2000-04-27 01:27:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ASSERT(h->finallyTarget);
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + h->finallyTarget->mOffset;
|
2000-04-27 01:27:09 +00:00
|
|
|
}
|
2000-12-30 01:13:06 +00:00
|
|
|
endPC = mICode->its_iCode->end();
|
2000-04-27 01:27:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pLinkage)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2000-04-29 00:23:06 +00:00
|
|
|
Handler *h = mActivation->catchStack.back();
|
2000-04-27 01:27:09 +00:00
|
|
|
if (h->catchTarget) {
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + h->catchTarget->mOffset;
|
2000-04-27 01:27:09 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ASSERT(h->finallyTarget);
|
2000-12-08 23:55:39 +00:00
|
|
|
mPC = mICode->its_iCode->begin() + h->finallyTarget->mOffset;
|
2000-04-27 01:27:09 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2000-07-11 20:54:06 +00:00
|
|
|
rv = x->value;
|
|
|
|
break;
|
2000-04-18 21:51:45 +00:00
|
|
|
}
|
2000-06-23 22:27:17 +00:00
|
|
|
|
2000-04-26 01:42:00 +00:00
|
|
|
}
|
2000-09-02 01:01:04 +00:00
|
|
|
mActivation = 0;
|
2000-04-29 00:23:06 +00:00
|
|
|
return rv;
|
2000-04-26 01:42:00 +00:00
|
|
|
} /* interpret */
|
2000-04-05 06:05:57 +00:00
|
|
|
|
2000-04-26 05:35:07 +00:00
|
|
|
void Context::addListener(Listener* listener)
|
|
|
|
{
|
|
|
|
mListeners.push_back(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Context::removeListener(Listener* listener)
|
2000-05-12 01:19:39 +00:00
|
|
|
{
|
|
|
|
ListenerIterator e = mListeners.end();
|
2000-05-18 00:03:23 +00:00
|
|
|
ListenerIterator l = std::find(mListeners.begin(), e, listener);
|
2000-05-12 01:19:39 +00:00
|
|
|
if (l != e) mListeners.erase(l);
|
2000-04-26 05:35:07 +00:00
|
|
|
}
|
|
|
|
|
2000-05-12 01:19:39 +00:00
|
|
|
void Context::broadcast(Event event)
|
2000-04-26 05:35:07 +00:00
|
|
|
{
|
2000-04-29 00:23:06 +00:00
|
|
|
for (ListenerIterator i = mListeners.begin(), e = mListeners.end();
|
|
|
|
i != e; ++i) {
|
2000-04-26 05:35:07 +00:00
|
|
|
Listener* listener = *i;
|
2000-05-12 01:19:39 +00:00
|
|
|
listener->listen(this, event);
|
2000-04-26 05:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context::Frame* Context::getFrames()
|
|
|
|
{
|
|
|
|
return mLinkage;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace Interpreter */
|
2000-04-18 07:14:49 +00:00
|
|
|
} /* namespace JavaScript */
|