bool invokeCallback(v8::Persistent<v8::Object> callback, int argc, v8::Handle<v8::Value> argv[], bool& callbackReturnValue)
{
    // FIXME: If an exception was thrown by the callback, we should report it
    v8::TryCatch exceptionCatcher;

    v8::Local<v8::Function> callbackFunction;
    if (callback->IsFunction()) {
        callbackFunction = v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(callback));
    } else if (callback->IsObject()) {
        v8::Local<v8::Value> handleEventFunction = callback->Get(v8::String::NewSymbol("handleEvent"));
        if (handleEventFunction->IsFunction()) {
            callbackFunction = v8::Local<v8::Function>::Cast(handleEventFunction);
        }
    } else
        return false;

    if (callbackFunction.IsEmpty())
        return false;

    v8::Handle<v8::Object> thisObject = v8::Context::GetCurrent()->Global();

    V8Proxy* proxy = V8Proxy::retrieve();
    ASSERT(proxy);

    v8::Handle<v8::Value> result = proxy->CallFunction(callbackFunction, thisObject, argc, argv);

    callbackReturnValue = result.IsEmpty() && result->IsBoolean() && result->BooleanValue();

    return exceptionCatcher.HasCaught();
}
Esempio n. 2
0
String JSNSResolver::lookupNamespaceURI(ExceptionContext* exceptionContext,
                                        const String& prefix)
{
    v8::Handle<v8::Function> lookupNamespaceURIFunc;
    v8::Handle<v8::String> lookupNamespaceURIName = 
        v8::String::New("lookupNamespaceURI");

    // Check if the resolver has a function property named lookupNamespaceURI.
    if (m_resolver->Has(lookupNamespaceURIName)) {
        // In case the property is a getter that throws an error,
        // see LayoutTests/fast/dom/SelectorAPI/NSResolver-exceptions.xhtml
        ExceptionCatcher exceptionCatcher(exceptionContext);
        v8::Handle<v8::Value> lookupNamespaceURI = m_resolver->Get(
            lookupNamespaceURIName);
        if (exceptionContext->hadException())
            return String();
        if (lookupNamespaceURI->IsFunction()) {
            lookupNamespaceURIFunc = v8::Handle<v8::Function>::Cast(
                lookupNamespaceURI);
        }
    }

    if (lookupNamespaceURIFunc.IsEmpty() && !m_resolver->IsFunction()) {
        Frame* frame = ScriptController::retrieveActiveFrame();
        log_info(frame, "NSResolver does not have a lookupNamespaceURI method.",
                 String());
        return String();
    }

    // Catch exceptions from calling the namespace resolver.
    ExceptionCatcher exceptionCatcher(exceptionContext);

    const int argc = 1;
    v8::Handle<v8::Value> argv[argc] = { v8String(prefix) };
    v8::Handle<v8::Function> function = lookupNamespaceURIFunc.IsEmpty()
        ? v8::Handle<v8::Function>::Cast(m_resolver)
        : lookupNamespaceURIFunc;

    V8Proxy* proxy = V8Proxy::retrieve();
    v8::Handle<v8::Value> retval = proxy->CallFunction(function, m_resolver,
                                                       argc, argv);

    // Eat exceptions from namespace resolver and return an empty string. This
    // will cause NAMESPACE_ERR.
    if (exceptionContext->hadException())
        return String();

    return valueToStringWithNullOrUndefinedCheck(retval);
}
String JSXPathNSResolver::lookupNamespaceURI(const String& prefix) {
  v8::Handle<v8::Function> lookupNamespaceURIFunc;
  v8::Handle<v8::String> lookupNamespaceURIName = v8::String::New("lookupNamespaceURI");

  // Check if the resolver has a function property named lookupNamespaceURI.
  if (m_resolver->Has(lookupNamespaceURIName)) {
    v8::Handle<v8::Value> lookupNamespaceURI = m_resolver->Get(lookupNamespaceURIName);
    if (lookupNamespaceURI->IsFunction()) {
      lookupNamespaceURIFunc = v8::Handle<v8::Function>::Cast(lookupNamespaceURI);
    }
  }

  if (lookupNamespaceURIFunc.IsEmpty() && !m_resolver->IsFunction()) {
    Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
    log_info(frame, "XPathNSResolver does not have a lookupNamespaceURI method.", String());
    return String();
  }

  // Catch exceptions from calling the namespace resolver.
  v8::TryCatch try_catch;
  try_catch.SetVerbose(true);  // Print exceptions to console.

  const int argc = 1;
  v8::Handle<v8::Value> argv[argc] = { v8String(prefix) };
  v8::Handle<v8::Function> function = lookupNamespaceURIFunc.IsEmpty()
      ? v8::Handle<v8::Function>::Cast(m_resolver)
      : lookupNamespaceURIFunc;

  V8Proxy* proxy = V8Proxy::retrieve();
  v8::Handle<v8::Value> retval = proxy->CallFunction(function, m_resolver, argc, argv);

  // Eat exceptions from namespace resolver and return an empty string. This
  // will most likely cause NAMESPACE_ERR.
  if (try_catch.HasCaught()) {
    return String();
  }

  return valueToStringWithNullCheck(retval);
}