⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kjs_binding.cpp

📁 手机浏览器源码程序,功能强大
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// -*- c-basic-offset: 2 -*-
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2004 Apple Computer, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "kjs_binding.h"
#include "kjs_dom.h"
#include "kjs_window.h"
#include <kjs/internal.h> // for InterpreterImp

#include "dom/dom_exception.h"
#include "dom/dom2_range.h"
#include "xml/dom2_eventsimpl.h"

#include <kdebug.h>

using DOM::DOMString;

using namespace KJS;

/* TODO:
 * The catch all (...) clauses below shouldn't be necessary.
 * But they helped to view for example www.faz.net in an stable manner.
 * Those unknown exceptions should be treated as severe bugs and be fixed.
 *
 * these may be CSS exceptions - need to check - pmk
 */

Value DOMObject::get(ExecState *exec, const Identifier &p) const
{
  Value result;
#ifdef KHTML_NO_EXCEPTIONS
  DOM::_exceptioncode = 0;
  result = tryGet(exec,p);

  if (DOM::_exceptioncode) {
    if (DOM::_exceptioncode <= DOM::DOMException::_EXCEPTION_MAX) {
      Object err = Error::create(exec, GeneralError, QString("DOM exception %1").arg(DOM::_exceptioncode).local8Bit());
      err.put(exec, "code", Number(DOM::_exceptioncode));
      exec->setException( err );
      result = Undefined();
    } else {
      kdError(6070) << "Unknown exception in DOMObject::get()" << endl;
      result = String("Unknown exception");
    }
  DOM::_exceptioncode = 0;
  }
#else
  try {
    result = tryGet(exec,p);
  }
  catch (DOM::DOMException e) {
    // ### translate code into readable string ?
    // ### oh, and s/QString/i18n or I18N_NOOP (the code in kjs uses I18N_NOOP... but where is it translated ?)
    //     and where does it appear to the user ?
    Object err = Error::create(exec, GeneralError, QString("DOM exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException( err );
    result = Undefined();
  }
  catch (...) {
    kdError(6070) << "Unknown exception in DOMObject::get()" << endl;
    result = String("Unknown exception");
  }
#endif
  return result;
}

void DOMObject::put(ExecState *exec, const Identifier &propertyName,
                    const Value &value, int attr)
{
#ifdef KHTML_NO_EXCEPTIONS
  DOM::_exceptioncode = 0;
  tryPut(exec, propertyName, value, attr);

  if (DOM::_exceptioncode) {
    if (DOM::_exceptioncode <= DOM::DOMException::_EXCEPTION_MAX) {
      Object err = Error::create(exec, GeneralError, QString("DOM exception %1").arg(DOM::_exceptioncode).local8Bit());
      err.put(exec, "code", Number(DOM::_exceptioncode));
      exec->setException(err);
    } else {
      kdError(6070) << "Unknown exception in DOMObject::put()" << endl;
    }
    DOM::_exceptioncode = 0;
  }
#else
  try {
    tryPut(exec, propertyName, value, attr);
  }
  catch (DOM::DOMException e) {
    Object err = Error::create(exec, GeneralError, QString("DOM exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException(err);
  }
  catch (...) {
    kdError(6070) << "Unknown exception in DOMObject::put()" << endl;
  }
#endif
}

UString DOMObject::toString(ExecState *) const
{
  return "[object " + className() + "]";
}

Value DOMFunction::get(ExecState *exec, const Identifier &propertyName) const
{
  Value result;
#ifdef KHTML_NO_EXCEPTIONS
  DOM::_exceptioncode = 0;

  result = tryGet(exec, propertyName);

  if (DOM::_exceptioncode)
    {
    if (DOM::_exceptioncode <= DOM::DOMException::_EXCEPTION_MAX) {
      result = Undefined();
      Object err = Error::create(exec, GeneralError, QString("DOM exception %1").arg(DOM::_exceptioncode).local8Bit());
      exec->setException(err);
    } else {
      kdError(6070) << "Unknown exception in DOMFunction::get()" << endl;
      result = String("Unknown exception");
    }
    DOM::_exceptioncode = 0;
  }
#else
  try {
    result = tryGet(exec, propertyName);
  }
  catch (DOM::DOMException e) {
    result = Undefined();
    Object err = Error::create(exec, GeneralError, QString("DOM exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException(err);
  }
  catch (...) {
    kdError(6070) << "Unknown exception in DOMFunction::get()" << endl;
    result = String("Unknown exception");
  }
#endif
  return result;
}

Value DOMFunction::call(ExecState *exec, Object &thisObj, const List &args)
{
  Value val;
#ifdef KHTML_NO_EXCEPTIONS
  DOM::_exceptioncode = 0;
  val = tryCall(exec, thisObj, args);

  if (DOM::_exceptioncode)
  {
    Object err;
    int code = 0;
    // exceptioncodes are 0<dom<css<range
    // event exceptions are not actually ever raised, no need to catch
    if (DOM::_exceptioncode <= DOM::DOMException::_EXCEPTION_MAX) {
      code = DOM::_exceptioncode;
      err = Error::create(exec, GeneralError, QString("DOM Exception %1").arg(code).local8Bit());
    } else if (DOM::_exceptioncode <= DOM::CSSException::_EXCEPTION_MAX) {
      code = DOM::_exceptioncode - DOM::CSSException::_EXCEPTION_OFFSET;
      err = Error::create(exec, GeneralError, QString("CSS Exception %1").arg(code).local8Bit());
    } else if (DOM::_exceptioncode <= DOM::RangeException::_EXCEPTION_MAX) {
      code = DOM::_exceptioncode - DOM::RangeException::_EXCEPTION_OFFSET;
      err = Error::create(exec, GeneralError, QString("DOM Range Exception %1").arg(code).local8Bit());
    } else {
      kdError(6070) << "Unknown exception in DOMFunction::call()" << endl;
      err = Error::create(exec, GeneralError, "Unknown exception");
    }
    err.put(exec, "code", Number(code));
    exec->setException(err);
    DOM::_exceptioncode = 0;
  }
#else
  try {
    val = tryCall(exec, thisObj, args);
  }
  // pity there's no way to distinguish between these in JS code
  catch (DOM::DOMException e) {
    Object err = Error::create(exec, GeneralError, QString("DOM Exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException(err);
  }
  catch (DOM::RangeException e) {
    Object err = Error::create(exec, GeneralError, QString("DOM Range Exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException(err);
  }
  catch (DOM::CSSException e) {
    Object err = Error::create(exec, GeneralError, QString("CSS Exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException(err);
  }
  catch (DOM::EventException e) {
    Object err = Error::create(exec, GeneralError, QString("DOM Event Exception %1").arg(e.code).local8Bit());
    err.put(exec, "code", Number(e.code));
    exec->setException(err);
  }
  catch (...) {
    kdError(6070) << "Unknown exception in DOMFunction::call()" << endl;
    Object err = Error::create(exec, GeneralError, "Unknown exception");
    exec->setException(err);
  }
#endif
  return val;
}

static QPtrDict<DOMObject> * staticDomObjects = 0;
QPtrDict< QPtrDict<DOMObject> > * staticDomObjectsPerDocument = 0;

QPtrDict<DOMObject> & ScriptInterpreter::domObjects()
{
  if (!staticDomObjects) {
    staticDomObjects = new QPtrDict<DOMObject>(1021);
  }
  return *staticDomObjects;
}

QPtrDict< QPtrDict<DOMObject> > & ScriptInterpreter::domObjectsPerDocument()
{
  if (!staticDomObjectsPerDocument) {
    staticDomObjectsPerDocument = new QPtrDict<QPtrDict<DOMObject> >();
    staticDomObjectsPerDocument->setAutoDelete(true);
  }
  return *staticDomObjectsPerDocument;
}


ScriptInterpreter::ScriptInterpreter( const Object &global, KHTMLPart* part )
  : Interpreter( global ), m_part( part ),
    m_evt( 0L ), m_inlineCode(false), m_timerCallback(false)
{

⌨️ 快捷键说明

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