📄 nodes.cpp
字号:
// -*- c-basic-offset: 2 -*-/* * This file is part of the KDE libraries * Copyright (C) 1999-2002 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) * Copyright (C) 2003 Apple Computer, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * */#include "nodes.h"//#include <iostream>#include <math.h>#include <assert.h>#ifdef KJS_DEBUG_MEM#include <stdio.h>#include <typeinfo>#endif#include "collector.h"#include "context.h"#include "debugger.h"#include "function_object.h"#include "internal.h"#include "value.h"#include "object.h"#include "types.h"#include "interpreter.h"#include "lexer.h"#include "operations.h"#include "ustring.h"using namespace KJS;#define KJS_BREAKPOINT \ if (!hitStatement(exec)) \ return Completion(Normal);#define KJS_ABORTPOINT \ if (exec->dynamicInterpreter()->imp()->debugger() && \ exec->dynamicInterpreter()->imp()->debugger()->imp()->aborted()) \ return Completion(Normal);#define KJS_CHECKEXCEPTION \ if (exec->hadException()) \ return Completion(Throw, exec->exception()); \ if (Collector::outOfMemory()) \ return Completion(Throw, Error::create(exec,GeneralError,"Out of memory"));#define KJS_CHECKEXCEPTIONVALUE \ if (exec->hadException()) \ return exec->exception(); \ if (Collector::outOfMemory()) \ return Undefined(); // will be picked up by KJS_CHECKEXCEPTION#define KJS_CHECKEXCEPTIONREFERENCE \ if (exec->hadException()) \ return Reference::makeValueReference(Undefined());; \ if (Collector::outOfMemory()) \ return Reference::makeValueReference(Undefined()); // will be picked up by KJS_CHECKEXCEPTION#define KJS_CHECKEXCEPTIONLIST \ if (exec->hadException()) \ return List(); \ if (Collector::outOfMemory()) \ return List(); // will be picked up by KJS_CHECKEXCEPTION#ifdef KJS_DEBUG_MEMstd::list<Node *> * Node::s_nodes = 0L;#endif// ------------------------------ Node -----------------------------------------Node::Node(){ line = Lexer::curr()->lineNo(); sourceURL = Lexer::curr()->sourceURL(); refcount = 0;#ifdef KJS_DEBUG_MEM if (!s_nodes) s_nodes = new std::list<Node *>; s_nodes->push_back(this);#endif}Node::~Node(){#ifdef KJS_DEBUG_MEM s_nodes->remove( this );#endif}Reference Node::evaluateReference(ExecState *exec){ Value v = evaluate(exec); KJS_CHECKEXCEPTIONREFERENCE return Reference::makeValueReference(v);}#ifdef KJS_DEBUG_MEMvoid Node::finalCheck(){ fprintf( stderr, "Node::finalCheck(): list count : %d\n", (int)s_nodes.size() ); std::list<Node *>::iterator it = s_nodes->begin(); for ( uint i = 0; it != s_nodes->end() ; ++it, ++i ) fprintf( stderr, "[%d] Still having node %p (%s) (refcount %d)\n", i, (void*)*it, typeid( **it ).name(), (*it)->refcount ); delete s_nodes; s_nodes = 0L;}#endifValue Node::throwError(ExecState *exec, ErrorType e, const char *msg){ Object err = Error::create(exec, e, msg, lineNo(), sourceId(), &sourceURL); exec->setException(err); return err;}Value Node::throwError(ExecState *exec, ErrorType e, const char *msg, Value v, Node *expr){ char *vStr = strdup(v.toString(exec).ascii()); char *exprStr = strdup(expr->toString().ascii()); int length = strlen(msg) - 4 /* two %s */ + strlen(vStr) + strlen(exprStr) + 1 /* null terminator */; char *str = new char[length]; sprintf(str, msg, vStr, exprStr); free(vStr); free(exprStr); Value result = throwError(exec, e, str); delete [] str; return result;}Value Node::throwError(ExecState *exec, ErrorType e, const char *msg, Identifier label){ const char *l = label.ascii(); int length = strlen(msg) - 2 /* %s */ + strlen(l) + 1 /* null terminator */; char *message = new char[length]; sprintf(message, msg, l); Value result = throwError(exec, e, message); delete [] message; return result;}// ------------------------------ StatementNode --------------------------------StatementNode::StatementNode() : l0(-1), l1(-1), sid(-1), breakPoint(false){}void StatementNode::setLoc(int line0, int line1, int sourceId){ l0 = line0; l1 = line1; sid = sourceId;}// return true if the debugger wants us to stop at this pointbool StatementNode::hitStatement(ExecState *exec){ Debugger *dbg = exec->dynamicInterpreter()->imp()->debugger(); if (dbg) return dbg->atStatement(exec,sid,l0,l1); else return true; // continue}// return true if the debugger wants us to stop at this pointbool StatementNode::abortStatement(ExecState *exec){ Debugger *dbg = exec->dynamicInterpreter()->imp()->debugger(); if (dbg) return dbg->imp()->aborted(); else return false;}void StatementNode::processFuncDecl(ExecState *exec){}// ------------------------------ NullNode -------------------------------------Value NullNode::evaluate(ExecState */*exec*/){ return Null();}// ------------------------------ BooleanNode ----------------------------------Value BooleanNode::evaluate(ExecState */*exec*/){ return Boolean(value);}// ------------------------------ NumberNode -----------------------------------Value NumberNode::evaluate(ExecState */*exec*/){ return Number(value);}// ------------------------------ StringNode -----------------------------------Value StringNode::evaluate(ExecState */*exec*/){ return String(value);}// ------------------------------ RegExpNode -----------------------------------Value RegExpNode::evaluate(ExecState *exec){ List list; String p(pattern); String f(flags); list.append(p); list.append(f); Object reg = exec->lexicalInterpreter()->imp()->builtinRegExp(); return reg.construct(exec,list);}// ------------------------------ ThisNode -------------------------------------// ECMA 11.1.1Value ThisNode::evaluate(ExecState *exec){ return exec->context().imp()->thisValue();}// ------------------------------ ResolveNode ----------------------------------// ECMA 11.1.2 & 10.1.4Value ResolveNode::evaluate(ExecState *exec){ return evaluateReference(exec).getValue(exec);}Reference ResolveNode::evaluateReference(ExecState *exec){ ScopeChain chain = exec->context().imp()->scopeChain(); while (!chain.isEmpty()) { ObjectImp *o = chain.top(); //cout << "Resolve: looking at '" << ident.ascii() << "'" // << " in " << (void*)o << " " << o->classInfo()->className << endl; if (o->hasProperty(exec,ident)) { //cout << "Resolve: FOUND '" << ident.ascii() << "'" // << " in " << (void*)o << " " << o->classInfo()->className << endl; return Reference(o, ident); } chain.pop(); } // identifier not found //cout << "Resolve: didn't find '" << ident.ascii() << "'" << endl; return Reference(Null(), ident);}// ------------------------------ GroupNode ------------------------------------void GroupNode::ref(){ Node::ref(); if ( group ) group->ref();}bool GroupNode::deref(){ if ( group && group->deref() ) delete group; return Node::deref();}// ECMA 11.1.6Value GroupNode::evaluate(ExecState *exec){ return group->evaluate(exec);}// ------------------------------ ElementNode ----------------------------------void ElementNode::ref(){ for (ElementNode *n = this; n; n = n->list) { n->Node::ref(); if (n->node) n->node->ref(); }}bool ElementNode::deref(){ ElementNode *next; for (ElementNode *n = this; n; n = next) { next = n->list; if (n->node && n->node->deref()) delete n->node; if (n != this && n->Node::deref()) delete n; } return Node::deref();}// ECMA 11.1.4Value ElementNode::evaluate(ExecState *exec){ Object array = exec->lexicalInterpreter()->builtinArray().construct(exec, List::empty()); int length = 0; for (ElementNode *n = this; n; n = n->list) { Value val = n->node->evaluate(exec); KJS_CHECKEXCEPTIONVALUE length += n->elision; array.put(exec, length++, val); } return array;}// ------------------------------ ArrayNode ------------------------------------void ArrayNode::ref(){ Node::ref(); if ( element ) element->ref();}bool ArrayNode::deref(){ if ( element && element->deref() ) delete element; return Node::deref();}// ECMA 11.1.4Value ArrayNode::evaluate(ExecState *exec){ Object array; int length; if (element) { array = Object(static_cast<ObjectImp*>(element->evaluate(exec).imp())); KJS_CHECKEXCEPTIONVALUE length = opt ? array.get(exec,lengthPropertyName).toInt32(exec) : 0; } else { Value newArr = exec->lexicalInterpreter()->builtinArray().construct(exec,List::empty()); array = Object(static_cast<ObjectImp*>(newArr.imp())); length = 0; } if (opt) array.put(exec,lengthPropertyName, Number(elision + length), DontEnum | DontDelete); return array;}// ------------------------------ ObjectLiteralNode ----------------------------void ObjectLiteralNode::ref(){ Node::ref(); if ( list ) list->ref();}bool ObjectLiteralNode::deref(){ if ( list && list->deref() ) delete list; return Node::deref();}// ECMA 11.1.5Value ObjectLiteralNode::evaluate(ExecState *exec){ if (list) return list->evaluate(exec); return exec->lexicalInterpreter()->builtinObject().construct(exec,List::empty());}// ------------------------------ PropertyValueNode ----------------------------void PropertyValueNode::ref(){ for (PropertyValueNode *n = this; n; n = n->list) { n->Node::ref(); if (n->name) n->name->ref(); if (n->assign) n->assign->ref(); }}bool PropertyValueNode::deref(){ PropertyValueNode *next; for (PropertyValueNode *n = this; n; n = next) { next = n->list; if ( n->name && n->name->deref() ) delete n->name; if ( n->assign && n->assign->deref() ) delete n->assign; if (n != this && n->Node::deref() ) delete n; } return Node::deref();}// ECMA 11.1.5Value PropertyValueNode::evaluate(ExecState *exec){ Object obj = exec->lexicalInterpreter()->builtinObject().construct(exec, List::empty()); for (PropertyValueNode *p = this; p; p = p->list) { Value n = p->name->evaluate(exec); KJS_CHECKEXCEPTIONVALUE Value v = p->assign->evaluate(exec); KJS_CHECKEXCEPTIONVALUE obj.put(exec, Identifier(n.toString(exec)), v); } return obj;}// ------------------------------ PropertyNode ---------------------------------// ECMA 11.1.5Value PropertyNode::evaluate(ExecState */*exec*/){ Value s; if (str.isNull()) { s = String(UString::from(numeric)); } else { s = String(str.ustring()); } return s;}// ------------------------------ AccessorNode1 --------------------------------void AccessorNode1::ref(){ Node::ref(); if ( expr1 ) expr1->ref(); if ( expr2 ) expr2->ref();}bool AccessorNode1::deref(){ if ( expr1 && expr1->deref() ) delete expr1; if ( expr2 && expr2->deref() ) delete expr2; return Node::deref();}// ECMA 11.2.1aValue AccessorNode1::evaluate(ExecState *exec){ return evaluateReference(exec).getValue(exec);}Reference AccessorNode1::evaluateReference(ExecState *exec){ Value v1 = expr1->evaluate(exec); KJS_CHECKEXCEPTIONREFERENCE Value v2 = expr2->evaluate(exec); KJS_CHECKEXCEPTIONREFERENCE Object o = v1.toObject(exec); unsigned i; if (v2.toUInt32(i)) return Reference(o, i); String s = v2.toString(exec); return Reference(o, Identifier(s.value()));}// ------------------------------ AccessorNode2 --------------------------------void AccessorNode2::ref(){ Node::ref(); if ( expr ) expr->ref();}bool AccessorNode2::deref(){ if ( expr && expr->deref() ) delete expr; return Node::deref();}// ECMA 11.2.1bValue AccessorNode2::evaluate(ExecState *exec){ return evaluateReference(exec).getValue(exec);}Reference AccessorNode2::evaluateReference(ExecState *exec){ Value v = expr->evaluate(exec); KJS_CHECKEXCEPTIONREFERENCE Object o = v.toObject(exec); return Reference(o, ident);}// ------------------------------ ArgumentListNode -----------------------------void ArgumentListNode::ref(){ for (ArgumentListNode *n = this; n; n = n->list) { n->Node::ref(); if (n->expr) n->expr->ref(); }}bool ArgumentListNode::deref(){ ArgumentListNode *next; for (ArgumentListNode *n = this; n; n = next) { next = n->list; if (n->expr && n->expr->deref()) delete n->expr; if (n != this && n->Node::deref()) delete n; } return Node::deref();}Value ArgumentListNode::evaluate(ExecState */*exec*/){ assert(0); return Value(); // dummy, see evaluateList()}// ECMA 11.2.4List ArgumentListNode::evaluateList(ExecState *exec){ List l; for (ArgumentListNode *n = this; n; n = n->list) { Value v = n->expr->evaluate(exec); KJS_CHECKEXCEPTIONLIST l.append(v); } return l;}// ------------------------------ ArgumentsNode --------------------------------void ArgumentsNode::ref(){ Node::ref(); if ( list ) list->ref();}bool ArgumentsNode::deref(){ if ( list && list->deref() ) delete list; return Node::deref();}Value ArgumentsNode::evaluate(ExecState */*exec*/){ assert(0); return Value(); // dummy, see evaluateList()}// ECMA 11.2.4List ArgumentsNode::evaluateList(ExecState *exec){ if (!list) return List(); return list->evaluateList(exec);}// ------------------------------ NewExprNode ----------------------------------// ECMA 11.2.2void NewExprNode::ref(){ Node::ref(); if ( expr ) expr->ref(); if ( args ) args->ref();}bool NewExprNode::deref(){ if ( expr && expr->deref() ) delete expr; if ( args && args->deref() ) delete args; return Node::deref();}Value NewExprNode::evaluate(ExecState *exec){ Value v = expr->evaluate(exec); KJS_CHECKEXCEPTIONVALUE List argList; if (args) { argList = args->evaluateList(exec); KJS_CHECKEXCEPTIONVALUE } if (v.type() != ObjectType) { return throwError(exec, TypeError, "Value %s (result of expression %s) is not an object. Cannot be used with new.", v, expr); } Object constr = Object(static_cast<ObjectImp*>(v.imp())); if (!constr.implementsConstruct()) { return throwError(exec, TypeError, "Value %s (result of expression %s) is not a constructor. Cannot be used with new.", v, expr); } Value res = constr.construct(exec,argList); return res;}// ------------------------------ FunctionCallNode -----------------------------void FunctionCallNode::ref(){ Node::ref(); if ( expr ) expr->ref(); if ( args ) args->ref();}bool FunctionCallNode::deref(){ if ( expr && expr->deref() ) delete expr; if ( args && args->deref() ) delete args; return Node::deref();}// ECMA 11.2.3Value FunctionCallNode::evaluate(ExecState *exec){ Reference ref = expr->evaluateReference(exec); KJS_CHECKEXCEPTIONVALUE List argList = args->evaluateList(exec); KJS_CHECKEXCEPTIONVALUE Value v = ref.getValue(exec); if (v.type() != ObjectType) { return throwError(exec, TypeError, "Value %s (result of expression %s) is not object.", v, expr); } Object func = Object(static_cast<ObjectImp*>(v.imp())); if (!func.implementsCall()) { return throwError(exec, TypeError, "Object %s (result of expression %s) does not allow calls.", v, expr); } Value thisVal; if (ref.isMutable()) thisVal = ref.getBase(exec); else thisVal = Null(); if (thisVal.type() == ObjectType && Object::dynamicCast(thisVal).inherits(&ActivationImp::info)) thisVal = Null(); if (thisVal.type() != ObjectType) { // ECMA 11.2.3 says that in this situation the this value should be null. // However, section 10.2.3 says that in the case where the value provided // by the caller is null, the global object should be used. It also says // that the section does not apply to interal functions, but for simplicity // of implementation we use the global object anyway here. This guarantees // that in host objects you always get a valid object for this. // thisVal = Null(); thisVal = exec->dynamicInterpreter()->globalObject();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -