Ejemplo n.º 1
0
bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMap) const
{
    if (!isObject())
        return false;

    v8::Handle<v8::Object> options = m_options->ToObject();
    if (options.IsEmpty())
        return false;

    v8::Local<v8::Array> properties = options->GetOwnPropertyNames();
    if (properties.IsEmpty())
        return true;
    for (uint32_t i = 0; i < properties->Length(); ++i) {
        v8::Local<v8::String> key = properties->Get(i)->ToString();
        if (!options->Has(key))
            continue;

        v8::Local<v8::Value> value = options->Get(key);
        TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
        TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false);
        if (!static_cast<const String&>(stringKey).isEmpty())
            hashMap.set(stringKey, stringValue);
    }

    return true;
}
Ejemplo n.º 2
0
static bool populateContextMenuItems(v8::Isolate* isolate,
                                     const v8::Local<v8::Array>& itemArray,
                                     ContextMenu& menu) {
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
  for (size_t i = 0; i < itemArray->Length(); ++i) {
    v8::Local<v8::Object> item =
        itemArray->Get(context, i).ToLocalChecked().As<v8::Object>();
    v8::Local<v8::Value> type;
    v8::Local<v8::Value> id;
    v8::Local<v8::Value> label;
    v8::Local<v8::Value> enabled;
    v8::Local<v8::Value> checked;
    v8::Local<v8::Value> subItems;
    if (!item->Get(context, v8AtomicString(isolate, "type")).ToLocal(&type) ||
        !item->Get(context, v8AtomicString(isolate, "id")).ToLocal(&id) ||
        !item->Get(context, v8AtomicString(isolate, "label")).ToLocal(&label) ||
        !item->Get(context, v8AtomicString(isolate, "enabled"))
             .ToLocal(&enabled) ||
        !item->Get(context, v8AtomicString(isolate, "checked"))
             .ToLocal(&checked) ||
        !item->Get(context, v8AtomicString(isolate, "subItems"))
             .ToLocal(&subItems))
      return false;
    if (!type->IsString())
      continue;
    String typeString = toCoreStringWithNullCheck(type.As<v8::String>());
    if (typeString == "separator") {
      ContextMenuItem item(ContextMenuItem(
          SeparatorType, ContextMenuItemCustomTagNoAction, String(), String()));
      menu.appendItem(item);
    } else if (typeString == "subMenu" && subItems->IsArray()) {
      ContextMenu subMenu;
      v8::Local<v8::Array> subItemsArray = v8::Local<v8::Array>::Cast(subItems);
      if (!populateContextMenuItems(isolate, subItemsArray, subMenu))
        return false;
      TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString,
                       label, false);
      ContextMenuItem item(SubmenuType, ContextMenuItemCustomTagNoAction,
                           labelString, String(), &subMenu);
      menu.appendItem(item);
    } else {
      int32_t int32Id;
      if (!v8Call(id->Int32Value(context), int32Id))
        return false;
      ContextMenuAction typedId = static_cast<ContextMenuAction>(
          ContextMenuItemBaseCustomTag + int32Id);
      TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, labelString,
                       label, false);
      ContextMenuItem menuItem(
          (typeString == "checkbox" ? CheckableActionType : ActionType),
          typedId, labelString, String());
      if (checked->IsBoolean())
        menuItem.setChecked(checked.As<v8::Boolean>()->Value());
      if (enabled->IsBoolean())
        menuItem.setEnabled(enabled.As<v8::Boolean>()->Value());
      menu.appendItem(menuItem);
    }
  }
  return true;
}
Ejemplo n.º 3
0
bool Dictionary::getOwnPropertiesAsStringHashMap(
    HashMap<String, String>& hashMap) const {
  v8::Local<v8::Object> object;
  if (!toObject(object))
    return false;

  v8::Local<v8::Array> properties;
  if (!object->GetOwnPropertyNames(v8Context()).ToLocal(&properties))
    return false;
  for (uint32_t i = 0; i < properties->Length(); ++i) {
    v8::Local<v8::String> key;
    if (!propertyKey(v8Context(), properties, i, key))
      continue;
    if (!v8CallBoolean(object->Has(v8Context(), key)))
      continue;

    v8::Local<v8::Value> value;
    if (!object->Get(v8Context(), key).ToLocal(&value))
      continue;
    TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
    TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false);
    if (!static_cast<const String&>(stringKey).isEmpty())
      hashMap.set(stringKey, stringValue);
  }

  return true;
}
Ejemplo n.º 4
0
inline bool Dictionary::getStringType(const String& key, StringType& value) const
{
    v8::Local<v8::Value> v8Value;
    if (!getKey(key, v8Value))
        return false;

    TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
    value = stringValue;
    return true;
}
bool getStringType(const Dictionary& dictionary, const String& key, StringType& value)
{
    v8::Local<v8::Value> v8Value;
    if (!dictionary.get(key, v8Value))
        return false;

    TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
    value = stringValue;
    return true;
}
Ejemplo n.º 6
0
bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) const
{
    v8::Local<v8::Value> v8Value;
    if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value))
        return false;

    TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
    value = stringValue;
    return true;
}
Ejemplo n.º 7
0
bool Dictionary::convert(ConversionContext& context, const String& key, String& value) const
{
    ConversionContextScope scope(context);

    v8::Local<v8::Value> v8Value;
    if (!getKey(key, v8Value))
        return true;

    TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
    value = stringValue;
    return true;
}
bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::ConversionContext& context, const String& key, String& value)
{
    Dictionary::ConversionContextScope scope(context);

    v8::Local<v8::Value> v8Value;
    if (!dictionary.get(key, v8Value))
        return true;

    TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
    value = stringValue;
    return true;
}
ScriptValue WorkerScriptController::evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition, CachedMetadataHandler* cacheHandler, V8CacheOptions v8CacheOptions)
{
    if (!initializeContextIfNeeded())
        return ScriptValue();

    ScriptState::Scope scope(m_scriptState.get());

    if (!m_disableEvalPending.isEmpty()) {
        m_scriptState->context()->AllowCodeGenerationFromStrings(false);
        m_scriptState->context()->SetErrorMessageForCodeGenerationFromStrings(v8String(isolate(), m_disableEvalPending));
        m_disableEvalPending = String();
    }

    v8::TryCatch block(isolate());

    v8::Local<v8::Script> compiledScript;
    v8::MaybeLocal<v8::Value> maybeResult;
    if (v8Call(V8ScriptRunner::compileScript(script, fileName, String(), scriptStartPosition, isolate(), cacheHandler, SharableCrossOrigin, v8CacheOptions), compiledScript, block))
        maybeResult = V8ScriptRunner::runCompiledScript(isolate(), compiledScript, m_workerGlobalScope);

    if (!block.CanContinue()) {
        forbidExecution();
        return ScriptValue();
    }

    if (block.HasCaught()) {
        v8::Local<v8::Message> message = block.Message();
        m_executionState->hadException = true;
        m_executionState->errorMessage = toCoreString(message->Get());
        if (v8Call(message->GetLineNumber(m_scriptState->context()), m_executionState->lineNumber)
            && v8Call(message->GetStartColumn(m_scriptState->context()), m_executionState->columnNumber)) {
            ++m_executionState->columnNumber;
        } else {
            m_executionState->lineNumber = 0;
            m_executionState->columnNumber = 0;
        }

        TOSTRING_DEFAULT(V8StringResource<>, sourceURL, message->GetScriptOrigin().ResourceName(), ScriptValue());
        m_executionState->sourceURL = sourceURL;
        m_executionState->exception = ScriptValue(m_scriptState.get(), block.Exception());
        block.Reset();
    } else {
        m_executionState->hadException = false;
    }

    v8::Local<v8::Value> result;
    if (!maybeResult.ToLocal(&result) || result->IsUndefined())
        return ScriptValue();

    return ScriptValue(m_scriptState.get(), result);
}
static v8::Local<v8::Value> getItem(HTMLAllCollection* collection, v8::Local<v8::Value> argument, const CallbackInfo& info)
{
    v8::Local<v8::Uint32> index;
    if (!argument->ToArrayIndex(info.GetIsolate()->GetCurrentContext()).ToLocal(&index)) {
        TOSTRING_DEFAULT(V8StringResource<>, name, argument, v8::Undefined(info.GetIsolate()));
        v8::Local<v8::Value> result = getNamedItems(collection, name, info);

        if (result.IsEmpty())
            return v8::Undefined(info.GetIsolate());

        return result;
    }

    RefPtrWillBeRawPtr<Element> result = collection->item(index->Value());
    return toV8(result.release(), info.Holder(), info.GetIsolate());
}
Ejemplo n.º 11
0
AtomicString V8CustomXPathNSResolver::lookupNamespaceURI(const String& prefix) {
  v8::Isolate* isolate = m_scriptState->isolate();
  v8::Local<v8::Function> lookupNamespaceURIFunc;
  v8::Local<v8::String> lookupNamespaceURIName =
      v8AtomicString(isolate, "lookupNamespaceURI");

  // Check if the resolver has a function property named lookupNamespaceURI.
  v8::Local<v8::Value> lookupNamespaceURI;
  if (m_resolver->Get(m_scriptState->context(), lookupNamespaceURIName)
          .ToLocal(&lookupNamespaceURI) &&
      lookupNamespaceURI->IsFunction())
    lookupNamespaceURIFunc = v8::Local<v8::Function>::Cast(lookupNamespaceURI);

  if (lookupNamespaceURIFunc.IsEmpty() && !m_resolver->IsFunction()) {
    LocalFrame* frame =
        toLocalDOMWindow(toDOMWindow(m_scriptState->context()))->frame();
    if (frame && frame->host())
      frame->console().addMessage(ConsoleMessage::create(
          JSMessageSource, ErrorMessageLevel,
          "XPathNSResolver does not have a lookupNamespaceURI method."));
    return nullAtom;
  }

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

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

  v8::Local<v8::Value> retval;
  // Eat exceptions from namespace resolver and return an empty string. This
  // will most likely cause NamespaceError.
  if (!V8ScriptRunner::callFunction(
           function, toExecutionContext(m_scriptState->context()), m_resolver,
           argc, argv, isolate)
           .ToLocal(&retval))
    return nullAtom;

  TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, returnString,
                   retval, nullAtom);
  return returnString;
}
Ejemplo n.º 12
0
bool Dictionary::get(const String& key, Vector<String>& value) const
{
    v8::Local<v8::Value> v8Value;
    if (!getKey(key, v8Value))
        return false;

    if (!v8Value->IsArray())
        return false;

    v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
    for (size_t i = 0; i < v8Array->Length(); ++i) {
        v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(m_isolate, i));
        TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false);
        value.append(stringValue);
    }

    return true;
}
Ejemplo n.º 13
0
String eventListenerHandlerBody(Document* document, EventListener* listener)
{
    if (listener->type() != EventListener::JSEventListenerType)
        return "";

    v8::HandleScope scope(toIsolate(document));
    V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
    v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
    v8::Context::Scope contextScope(context);
    v8::Handle<v8::Object> object = v8Listener->getListenerObject(document);
    if (object.IsEmpty())
        return "";
    v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
    if (function.IsEmpty())
        return "";

    TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, functionString, function, "");
    return functionString;
}
Ejemplo n.º 14
0
ScriptValue WorkerScriptController::evaluate(const String& script, const String& fileName, const TextPosition& scriptStartPosition)
{
    if (!initializeContextIfNeeded())
        return ScriptValue();

    ScriptState::Scope scope(m_scriptState.get());

    if (!m_disableEvalPending.isEmpty()) {
        m_scriptState->context()->AllowCodeGenerationFromStrings(false);
        m_scriptState->context()->SetErrorMessageForCodeGenerationFromStrings(v8String(m_isolate, m_disableEvalPending));
        m_disableEvalPending = String();
    }

    v8::TryCatch block;

    v8::Handle<v8::String> scriptString = v8String(m_isolate, script);
    v8::Handle<v8::Script> compiledScript = V8ScriptRunner::compileScript(scriptString, fileName, scriptStartPosition, 0, 0, m_isolate);
    v8::Local<v8::Value> result = V8ScriptRunner::runCompiledScript(m_isolate, compiledScript, &m_workerGlobalScope);

    if (!block.CanContinue()) {
        m_workerGlobalScope.script()->forbidExecution();
        return ScriptValue();
    }

    if (block.HasCaught()) {
        v8::Local<v8::Message> message = block.Message();
        m_globalScopeExecutionState->hadException = true;
        m_globalScopeExecutionState->errorMessage = toCoreString(message->Get());
        m_globalScopeExecutionState->lineNumber = message->GetLineNumber();
        m_globalScopeExecutionState->columnNumber = message->GetStartColumn() + 1;
        TOSTRING_DEFAULT(V8StringResource<>, sourceURL, message->GetScriptOrigin().ResourceName(), ScriptValue());
        m_globalScopeExecutionState->sourceURL = sourceURL;
        m_globalScopeExecutionState->exception = ScriptValue(m_scriptState.get(), block.Exception());
        block.Reset();
    } else {
        m_globalScopeExecutionState->hadException = false;
    }

    if (result.IsEmpty() || result->IsUndefined())
        return ScriptValue();

    return ScriptValue(m_scriptState.get(), result);
}
Ejemplo n.º 15
0
bool Dictionary::getPropertyNames(Vector<String>& names) const {
  v8::Local<v8::Object> object;
  if (!toObject(object))
    return false;

  v8::Local<v8::Array> properties;
  if (!object->GetPropertyNames(v8Context()).ToLocal(&properties))
    return false;
  for (uint32_t i = 0; i < properties->Length(); ++i) {
    v8::Local<v8::String> key;
    if (!propertyKey(v8Context(), properties, i, key))
      continue;
    if (!v8CallBoolean(object->Has(v8Context(), key)))
      continue;
    TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
    names.append(stringKey);
  }

  return true;
}
CORE_EXPORT bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Vector<String>& value)
{
    v8::Local<v8::Value> v8Value;
    if (!dictionary.get(key, v8Value))
        return false;

    if (!v8Value->IsArray())
        return false;

    v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
    for (size_t i = 0; i < v8Array->Length(); ++i) {
        v8::Local<v8::Value> indexedValue;
        if (!v8Array->Get(dictionary.v8Context(), v8::Uint32::New(dictionary.isolate(), i)).ToLocal(&indexedValue))
            return false;
        TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false);
        value.append(stringValue);
    }

    return true;
}
Ejemplo n.º 17
0
bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const
{
    v8::Local<v8::Value> v8Value;
    if (!getKey(key, v8Value))
        return false;

    // FIXME: Support array-like objects
    if (!v8Value->IsArray())
        return false;

    ASSERT(m_isolate);
    ASSERT(m_isolate == v8::Isolate::GetCurrent());
    v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
    for (size_t i = 0; i < v8Array->Length(); ++i) {
        v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(m_isolate, i));
        TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false);
        value.add(stringValue);
    }

    return true;
}
Ejemplo n.º 18
0
bool Dictionary::getOwnPropertyNames(Vector<String>& names) const
{
    if (!isObject())
        return false;

    v8::Handle<v8::Object> options = m_options->ToObject();
    if (options.IsEmpty())
        return false;

    v8::Local<v8::Array> properties = options->GetOwnPropertyNames();
    if (properties.IsEmpty())
        return true;
    for (uint32_t i = 0; i < properties->Length(); ++i) {
        v8::Local<v8::String> key = properties->Get(i)->ToString();
        if (!options->Has(key))
            continue;
        TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false);
        names.append(stringKey);
    }

    return true;
}