nodes.cpp

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 3,000 行 · 第 1/5 页

CPP
3,000
字号
// -*- c-basic-offset: 2 -*-/* *  This file is part of the KDE libraries *  Copyright (C) 1999-2002, 2003 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., 51 Franklin Street, Fifth Floor, *  Boston, MA 02110-1301, USA. * */#include "nodes.h"#include <math.h>#include <assert.h>#ifdef KJS_DEBUG_MEM#include <stdio.h>#include <typeinfo>#endif#ifdef KJS_VERBOSE#include <iostream>using namespace std;#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()) { \    setExceptionDetailsIfNeeded(exec); \    return Completion(Throw, exec->exception()); \  } \  if (Collector::outOfMemory()) \    return Completion(Throw, Error::create(exec,GeneralError,"Out of memory"));#define KJS_CHECKEXCEPTIONVALUE \  if (exec->hadException()) { \    setExceptionDetailsIfNeeded(exec); \    return exec->exception(); \  } \  if (Collector::outOfMemory()) \    return Undefined(); // will be picked up by KJS_CHECKEXCEPTION#define KJS_CHECKEXCEPTIONREFERENCE \  if (exec->hadException()) { \    setExceptionDetailsIfNeeded(exec); \    return Reference::makeValueReference(Undefined()); \  } \  if (Collector::outOfMemory()) \    return Reference::makeValueReference(Undefined()); // will be picked up by KJS_CHECKEXCEPTION#define KJS_CHECKEXCEPTIONLIST \  if (exec->hadException()) { \    setExceptionDetailsIfNeeded(exec); \    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();  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) const{  Value v = evaluate(exec);  KJS_CHECKEXCEPTIONREFERENCE  return Reference::makeValueReference(v);}// fallback for those nodes without a evaluate() reimplementation// TODO: reimplemint in each sub class, make Node::evaluate() pure virtualValue Node::evaluate(ExecState *exec) const{  //  fprintf(stderr, "%s::evaluate()\n", typeid(*this).name());  return evaluateReference(exec).getValue(exec);}bool Node::toBoolean(ExecState *exec) const{//   fprintf(stderr, "Node(%s)::toBoolean()\n", typeid(*this).name());  return evaluate(exec).toBoolean(exec);}double Node::toNumber(ExecState *exec) const{//   fprintf(stderr, "Node(%s)::toNumber()\n", typeid(*this).name());  return evaluate(exec).toNumber(exec);}UString Node::toString(ExecState *exec) const{  return evaluate(exec).toString(exec);}#ifdef KJS_DEBUG_MEMvoid Node::finalCheck(){  if (!s_nodes) {	  fprintf(stderr, "Node::finalCheck(): list 0\n");	  return;  }  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) const{  Object err = Error::create(exec, e, msg, lineNo(), sourceId());  exec->setException(err);  return err;}Value Node::throwError(ExecState *exec, ErrorType e, const char *msg,                       const Value &v, const Node *expr) const{  char *vStr = strdup(v.toString(exec).ascii());  char *exprStr = strdup(expr->toCode().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{  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;}void Node::setExceptionDetailsIfNeeded(ExecState *exec) const{    if (exec->hadException()) {        Object exception = exec->exception().toObject(exec);        if (!exception.hasProperty(exec, "line") /* &&            !exception.hasProperty(exec, "sourceURL")*/ ) {            exception.put(exec, "line", Number(line));//             exception.put(exec, "sourceURL", String(sourceURL));        }    }}// ----------------------------- StatementNode --------------------------------StatementNode::StatementNode() : l0(-1), l1(-1), sourceCode(0), breakPoint(false){}StatementNode::~StatementNode(){  if (sourceCode)    sourceCode->deref();}void StatementNode::setLoc(int line0, int line1, SourceCode *src){  // ### require these to be passed to the constructor  l0 = line0;  l1 = line1;  if (sourceCode != src) {    if (sourceCode)      sourceCode->deref();    sourceCode = src;    sourceCode->ref();  }}// return true if the debugger wants us to stop at this pointbool StatementNode::hitStatement(ExecState *exec){  assert(sourceCode);  assert(exec->context().imp()->sourceId == sourceCode->sid);  exec->context().imp()->setLines(l0,l1);  Debugger *dbg = exec->dynamicInterpreter()->imp()->debugger();  if (dbg)    return dbg->atStatement(exec);  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 *){}// ----------------------------- NullNode -------------------------------------Value NullNode::evaluate(ExecState *) const{  return Null();}bool NullNode::toBoolean(ExecState *) const{  return false;}double NullNode::toNumber(ExecState *) const{  return 0.0;}UString NullNode::toString(ExecState *) const{  return "null";}// ----------------------------- BooleanNode ----------------------------------Value BooleanNode::evaluate(ExecState *) const{  return Boolean(val);}bool BooleanNode::toBoolean(ExecState *) const{  return val;}double BooleanNode::toNumber(ExecState *) const{  return val ? 1.0 : 0.0;}UString BooleanNode::toString(ExecState *) const{  return val ? "true" : "false";}// ----------------------------- NumberNode -----------------------------------Value NumberNode::evaluate(ExecState *) const{  return Number(val);}bool NumberNode::toBoolean(ExecState *) const{  return !((val == 0) /* || (iVal() == N0) */ || isNaN(val));}double NumberNode::toNumber(ExecState *) const{  return val;}UString NumberNode::toString(ExecState *) const{  return UString::from(val);}// ----------------------------- StringNode -----------------------------------Value StringNode::evaluate(ExecState *) const{  return String(val);}bool StringNode::toBoolean(ExecState *) const{  return !val.isEmpty();}double StringNode::toNumber(ExecState *) const{  return val.toDouble();}UString StringNode::toString(ExecState *) const{  return val;}// ----------------------------- RegExpNode -----------------------------------Value RegExpNode::evaluate(ExecState *exec) const{  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);}bool RegExpNode::toBoolean(ExecState *) const{  return true;}// ----------------------------- ThisNode -------------------------------------// ECMA 11.1.1Value ThisNode::evaluate(ExecState *exec) const{  return exec->context().imp()->thisValue();}// ----------------------------- ResolveNode ----------------------------------// ECMA 11.1.2 & 10.1.4Value ResolveNode::evaluate(ExecState *exec) const{  return evaluateReference(exec).getValue(exec);}Reference ResolveNode::evaluateReference(ExecState *exec) const{  ScopeChain chain = exec->context().imp()->scopeChain();  while (!chain.isEmpty()) {    ObjectImp *o = chain.top();    //cerr << "Resolve: looking at '" << ident.ascii() << "'"    //     << " in " << (void*)o << " " << o->classInfo()->className << endl;    if (o->hasProperty(exec,ident)) {      //cerr << "Resolve: FOUND '" << ident.ascii() << "'"      //     << " in " << (void*)o << " " << o->classInfo()->className << endl;      return Reference(o, ident);    }    chain.pop();  }  // identifier not found#ifdef KJS_VERBOSE  cerr << "Resolve::evaluateReference: didn't find '" << ident.ustring().ascii() << "'" << endl;#endif  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) const{  return group->evaluate(exec);}Reference GroupNode::evaluateReference(ExecState *exec) const{  return group->evaluateReference(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) const{  Object array = exec->lexicalInterpreter()->builtinArray().construct(exec, List::empty());  int length = 0;  for (const 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) const{  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) const{  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) const{  Object obj = exec->lexicalInterpreter()->builtinObject().construct(exec, List::empty());  for (const 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*/) const{  Value s;  if (str.isNull()) {    s = String(UString::from(numeric));  } else {    s = String(str.ustring());  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?