nodes.cpp

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

CPP
3,000 行
字号
  printInfo(exec,(UString("new variable ")+ident.ustring()).cstring().c_str(),val);#endif  // We use Internal to bypass all checks in derived objects, e.g. so that  // "var location" creates a dynamic property instead of activating window.location.  int flags = Internal;  if (exec->context().imp()->codeType() != EvalCode)    flags |= DontDelete;  if (varType == VarDeclNode::Constant)    flags |= ReadOnly;  variable.put(exec, ident, val, flags);  // the spec wants us to return the name of the identifier here  // but we'll save the construction and copying as the return  // value isn't used by the caller  return Value();}void VarDeclNode::processVarDecls(ExecState *exec){  Object variable = exec->context().variableObject();  // ### use getDirect()? Check attributes?  // ### avoid duplication with actions performed in evaluate()?  if ( !variable.hasProperty( exec, ident ) ) { // already declared ?    int flags = None;    if (exec->_context->codeType() != EvalCode)      flags |= DontDelete;    if (varType == VarDeclNode::Constant)      flags |= ReadOnly;    // TODO: check for forbidden redeclaration of consts    variable.put(exec, ident, Undefined(), flags);  }}// ----------------------------- VarDeclListNode ------------------------------void VarDeclListNode::ref(){  for (VarDeclListNode *n = this; n; n = n->list) {    n->Node::ref();    if (n->var)      n->var->ref();  }}bool VarDeclListNode::deref(){  VarDeclListNode *next;  for (VarDeclListNode *n = this; n; n = next) {    next = n->list;    if (n->var && n->var->deref())      delete n->var;    if (n != this && n->Node::deref())      delete n;  }  return Node::deref();}// ECMA 12.2Value VarDeclListNode::evaluate(ExecState *exec) const{  for (const VarDeclListNode *n = this; n; n = n->list) {    (void)n->var->evaluate(exec);    KJS_CHECKEXCEPTIONVALUE  }  return Undefined();}void VarDeclListNode::processVarDecls(ExecState *exec){  for (VarDeclListNode *n = this; n; n = n->list)    n->var->processVarDecls(exec);}// ----------------------------- VarStatementNode -----------------------------void VarStatementNode::ref(){  StatementNode::ref();  if ( list )    list->ref();}bool VarStatementNode::deref(){  if ( list && list->deref() )    delete list;  return StatementNode::deref();}// ECMA 12.2Completion VarStatementNode::execute(ExecState *exec){  KJS_BREAKPOINT;  (void) list->evaluate(exec);  KJS_CHECKEXCEPTION  return Completion(Normal);}void VarStatementNode::processVarDecls(ExecState *exec){  list->processVarDecls(exec);}// ----------------------------- BlockNode ------------------------------------BlockNode::BlockNode(SourceElementsNode *s){  if (s) {    source = s->elements;    s->elements = 0;    setLoc(s->firstLine(), s->lastLine(), s->code());  } else {    source = 0;  }}void BlockNode::ref(){  StatementNode::ref();  if ( source )    source->ref();}bool BlockNode::deref(){  if ( source && source->deref() )    delete source;  return StatementNode::deref();}// ECMA 12.1Completion BlockNode::execute(ExecState *exec){  if (!source)    return Completion(Normal);  source->processFuncDecl(exec);  return source->execute(exec);}void BlockNode::processVarDecls(ExecState *exec){  if (source)    source->processVarDecls(exec);}// ----------------------------- EmptyStatementNode ---------------------------// ECMA 12.3Completion EmptyStatementNode::execute(ExecState * /*exec*/){  return Completion(Normal);}// ----------------------------- ExprStatementNode ----------------------------void ExprStatementNode::ref(){  StatementNode::ref();  if ( expr )    expr->ref();}bool ExprStatementNode::deref(){  if ( expr && expr->deref() )    delete expr;  return StatementNode::deref();}// ECMA 12.4Completion ExprStatementNode::execute(ExecState *exec){  KJS_BREAKPOINT;  Value v = expr->evaluate(exec);  KJS_CHECKEXCEPTION  return Completion(Normal, v);}// ----------------------------- IfNode ---------------------------------------void IfNode::ref(){  StatementNode::ref();  if ( statement1 )    statement1->ref();  if ( statement2 )    statement2->ref();  if ( expr )    expr->ref();}bool IfNode::deref(){  if ( statement1 && statement1->deref() )    delete statement1;  if ( statement2 && statement2->deref() )    delete statement2;  if ( expr && expr->deref() )    delete expr;  return StatementNode::deref();}// ECMA 12.5Completion IfNode::execute(ExecState *exec){  KJS_BREAKPOINT;  assert(expr);  bool b = expr->toBoolean(exec);  KJS_CHECKEXCEPTION  // if ... then  if (b)    return statement1->execute(exec);  // no else  if (!statement2)    return Completion(Normal);  // else  return statement2->execute(exec);}void IfNode::processVarDecls(ExecState *exec){  statement1->processVarDecls(exec);  if (statement2)    statement2->processVarDecls(exec);}// ----------------------------- DoWhileNode ----------------------------------void DoWhileNode::ref(){  StatementNode::ref();  if ( statement )    statement->ref();  if ( expr )    expr->ref();}bool DoWhileNode::deref(){  if ( statement && statement->deref() )    delete statement;  if ( expr && expr->deref() )    delete expr;  return StatementNode::deref();}// ECMA 12.6.1Completion DoWhileNode::execute(ExecState *exec){  KJS_BREAKPOINT;  Completion c;  Value value;  bool b;  do {    // bail out on error    KJS_CHECKEXCEPTION    exec->context().imp()->seenLabels()->pushIteration();    c = statement->execute(exec);    exec->context().imp()->seenLabels()->popIteration();    if (!((c.complType() == Continue) && ls.contains(c.target()))) {      if ((c.complType() == Break) && ls.contains(c.target()))        return Completion(Normal, value);      if (c.complType() != Normal)        return c;    }    b = expr->toBoolean(exec);    KJS_CHECKEXCEPTION  } while (b);  return Completion(Normal, value);}void DoWhileNode::processVarDecls(ExecState *exec){  statement->processVarDecls(exec);}// ----------------------------- WhileNode ------------------------------------void WhileNode::ref(){  StatementNode::ref();  if ( statement )    statement->ref();  if ( expr )    expr->ref();}bool WhileNode::deref(){  if ( statement && statement->deref() )    delete statement;  if ( expr && expr->deref() )    delete expr;  return StatementNode::deref();}// ECMA 12.6.2Completion WhileNode::execute(ExecState *exec){  KJS_BREAKPOINT;  Completion c;  Value value;  while (1) {    bool b = expr->toBoolean(exec);    KJS_CHECKEXCEPTION    // bail out on error    KJS_CHECKEXCEPTION    if (!b)      return Completion(Normal, value);    exec->context().imp()->seenLabels()->pushIteration();    c = statement->execute(exec);    exec->context().imp()->seenLabels()->popIteration();    if (c.isValueCompletion())      value = c.value();    if ((c.complType() == Continue) && ls.contains(c.target()))      continue;    if ((c.complType() == Break) && ls.contains(c.target()))      return Completion(Normal, value);    if (c.complType() != Normal)      return c;  }}void WhileNode::processVarDecls(ExecState *exec){  statement->processVarDecls(exec);}// ----------------------------- ForNode --------------------------------------void ForNode::ref(){  StatementNode::ref();  if ( statement )    statement->ref();  if ( expr1 )    expr1->ref();  if ( expr2 )    expr2->ref();  if ( expr3 )    expr3->ref();}bool ForNode::deref(){  if ( statement && statement->deref() )    delete statement;  if ( expr1 && expr1->deref() )    delete expr1;  if ( expr2 && expr2->deref() )    delete expr2;  if ( expr3 && expr3->deref() )    delete expr3;  return StatementNode::deref();}// ECMA 12.6.3Completion ForNode::execute(ExecState *exec){  Value v, cval;  if (expr1) {    v = expr1->evaluate(exec);    KJS_CHECKEXCEPTION  }  for (;;) {    if (expr2) {      bool b = expr2->toBoolean(exec);      KJS_CHECKEXCEPTION      if (!b)	return Completion(Normal, cval);    }    // bail out on error    KJS_CHECKEXCEPTION    exec->context().imp()->seenLabels()->pushIteration();    Completion c = statement->execute(exec);    exec->context().imp()->seenLabels()->popIteration();    if (c.isValueCompletion())      cval = c.value();    if (!((c.complType() == Continue) && ls.contains(c.target()))) {      if ((c.complType() == Break) && ls.contains(c.target()))        return Completion(Normal, cval);      if (c.complType() != Normal)        return c;    }    if (expr3) {      v = expr3->evaluate(exec);      KJS_CHECKEXCEPTION    }  }}void ForNode::processVarDecls(ExecState *exec){  if (expr1)    expr1->processVarDecls(exec);  statement->processVarDecls(exec);}// ----------------------------- ForInNode ------------------------------------ForInNode::ForInNode(Node *l, Node *e, StatementNode *s)  : init(0L), lexpr(l), expr(e), varDecl(0L), statement(s){}ForInNode::ForInNode(const Identifier &i, AssignExprNode *in, Node *e, StatementNode *s)  : ident(i), init(in), expr(e), statement(s){  // for( var foo = bar in baz )  varDecl = new VarDeclNode(ident, init, VarDeclNode::Variable);  lexpr = new ResolveNode(ident);}void ForInNode::ref(){  StatementNode::ref();  if ( statement )    statement->ref();  if ( expr )    expr->ref();  if ( lexpr )    lexpr->ref();  if ( init )    init->ref();  if ( varDecl )    varDecl->ref();}bool ForInNode::deref(){  if ( statement && statement->deref() )    delete statement;  if ( expr && expr->deref() )    delete expr;  if ( lexpr && lexpr->deref() )    delete lexpr;  if ( init && init->deref() )    delete init;  if ( varDecl && varDecl->deref() )    delete varDecl;  return StatementNode::deref();}// ECMA 12.6.4Completion ForInNode::execute(ExecState *exec){  Value retval;  Completion c;  if ( varDecl ) {    (void)varDecl->evaluate(exec);    KJS_CHECKEXCEPTION  }  Value v = expr->evaluate(exec);  // for Null and Undefined, we want to make sure not to go through  // the loop at all, because their object wrappers will have a  // property list but will throw an exception if you attempt to  // access any property.  if (v.isA(NullType) || v.isA(UndefinedType))    return Completion(Normal, retval);  Object o = v.toObject(exec);  KJS_CHECKEXCEPTION  ReferenceList propList = o.propList(exec);  ReferenceListIterator propIt = propList.begin();  while (propIt != propList.end()) {    Identifier name = propIt->getPropertyName(exec);    if (!o.hasProperty(exec,name)) {      propIt++;      continue;    }    Reference ref = lexpr->evaluateReference(exec);    KJS_CHECKEXCEPTION    ref.putValue(exec, String(name.ustring()));    exec->context().imp()->seenLabels()->pushIteration();    c = statement->execute(exec);    exec->context().imp()->seenLabels()->popIteration();    if (c.isValueCompletion())      retval = c.value();    if (!((c.complType() == Continue) && ls.contains(c.target()))) {      if ((c.complType() == Break) && ls.contains(c.target()))        break;      if (c.complType() != Normal) {        return c;      }    }    propIt++;  }  // bail out on error  KJS_CHECKEXCEPTION  return Completion(Normal, retval);}void ForInNode::processVarDecls(ExecState *exec){  statement->processVarDecls(exec);}// ----------------------------- ContinueNode ---------------------------------// ECMA 12.7Completion ContinueNode::execute(ExecState *exec){  KJS_BREAKPOINT;  Value dummy;  if (ident.isEmpty() && !exec->context().imp()->seenLabels()->inIteration())    return Completion(Throw,		      throwError(exec, SyntaxError, "continue used outside of iteration statement"));  else if (!ident.isEmpty() && !exec->context().imp()->seenLabels()->contains(ident))    return Completion(Throw,                      throwError(exec, SyntaxError, "Label %s not found in containing block. Can't continue.", ident));  else    return Completion(Continue, dummy, ident);}// ----------------------------- BreakNode ------------------------------------// ECMA 12.8Completion BreakNode::execute(ExecState *exec){  KJS_BREAKPOINT;  Value dummy;  if (ident.isEmpty() && !exec->context().imp()->seenLabels()->inIteration() &&      !exec->context().imp()->seenLabels()->inSwitch())    return Completion(Throw,		      throwError(exec, SyntaxError, "break used outside of iteration or switch statement"));  else if (!ident.isEmpty() && !exec->context().imp()->seenLabels()->contains(ident))    return Completion(Throw,                      throwError(exec, SyntaxError, "Label %s not found in containing block. Can't break.", ident));  else    return Completion(Break, dummy, ident);}// ----------------------------- ReturnNode -----------------------------------void ReturnNode::ref(){  StatementNode::ref();  if ( value )    value->ref();}bool ReturnNode::deref(){  if ( value && value->deref() )    delete value;  return StatementNode::deref();}// ECMA 12.9Completion ReturnNode::execute(ExecState *exec){  KJS_BREAKPOINT;  CodeType codeType = exec->context().imp()->codeType();  if (codeType != FunctionCode) {    return Completion(Throw, throwError(exec, SyntaxError, "Invalid return statement."));      }  if (!value)    return Completion(ReturnValue, Undefined());

⌨️ 快捷键说明

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