AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, JsValuePtr thisPtr) const
{
  if (!IsFunction())
    throw new std::runtime_error("Attempting to call a non-function");

  const JsContext context(jsEngine);
  if (!thisPtr)
  {
	  v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New(jsEngine->GetIsolate(), *jsEngine->context);
    thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global()));
  }
  if (!thisPtr->IsObject())
    throw new std::runtime_error("`this` pointer has to be an object");
  v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue());

  std::vector<v8::Handle<v8::Value>> argv;
  for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it)
    argv.push_back((*it)->UnwrapValue());

  const v8::TryCatch tryCatch;
  v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
  v8::Local<v8::Value> result = func->Call(thisObj, argv.size(),
      argv.size() ? &argv.front() : 0);

  if (tryCatch.HasCaught())
    throw JsError(tryCatch.Exception(), tryCatch.Message());

  return JsValuePtr(new JsValue(jsEngine, result));
}
AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Arguments& arguments)
{
  const JsContext context(shared_from_this());
  JsValueList list;
  for (int i = 0; i < arguments.Length(); i++)
    list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i])));
  return list;
}
AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& name) const
{
  if (!IsObject())
    throw new std::runtime_error("Attempting to get property of a non-object");

  const JsContext context(jsEngine);
  v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), name);
  v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
  return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& source,
    const std::string& filename)
{
  const JsContext context(shared_from_this());
  const v8::TryCatch tryCatch;
  const v8::Handle<v8::Script> script = CompileScript(isolate, source,
    filename);
  CheckTryCatch(tryCatch);
  v8::Local<v8::Value> result = script->Run();
  CheckTryCatch(tryCatch);
  return JsValuePtr(new JsValue(shared_from_this(), result));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback(
    v8::InvocationCallback callback)
{
  const JsContext context(shared_from_this());

  // Note: we are leaking this weak pointer, no obvious way to destroy it when
  // it's no longer used
  std::weak_ptr<JsEngine>* data =
      new std::weak_ptr<JsEngine>(shared_from_this());
  v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
      v8::External::New(data));
  return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction()));
}
std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
{
  if (!IsObject())
    throw new std::runtime_error("Attempting to get propert list for a non-object");

  const JsContext context(jsEngine);
  v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue());
  JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnPropertyNames()))->AsList();
  std::vector<std::string> result;
  for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++it)
    result.push_back((*it)->AsString());
  return result;
}
Exemple #7
0
AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo)
{
  V8Initializer::Init();
  JsEnginePtr result(new JsEngine());

  const v8::Locker locker(result->isolate);
  const v8::HandleScope handleScope;

  result->context.reset(result->isolate, v8::Context::New(result->isolate));
  v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New(
    result->isolate, result->context)->Global();
  AdblockPlus::GlobalJsObject::Setup(result, appInfo,
    JsValuePtr(new JsValue(result, globalContext)));
  return result;
}
AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const
{
  if (!IsArray())
    throw std::runtime_error("Cannot convert a non-array to list");

  const JsContext context(jsEngine);
  JsValueList result;
  v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue());
  uint32_t length = array->Length();
  for (uint32_t i = 0; i < length; i++)
  {
    v8::Local<v8::Value> item = array->Get(i);
    result.push_back(JsValuePtr(new JsValue(jsEngine, item)));
  }
  return result;
}
AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, const ScopedV8IsolatePtr& isolate)
{
  V8Initializer::Init();
  JsEnginePtr result(new JsEngine(isolate));

  const v8::Locker locker(result->GetIsolate());
  const v8::Isolate::Scope isolateScope(result->GetIsolate());
  const v8::HandleScope handleScope(result->GetIsolate());

  result->context.reset(new v8::UniquePersistent<v8::Context>(result->GetIsolate(), v8::Context::New(result->GetIsolate())));

  v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New(result->GetIsolate(), *result->context)->Global();
  result->globalJsObject = JsValuePtr(new JsValue(result, globalContext));

  AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject);
  return result;
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
{
  const JsContext context(shared_from_this());
  return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New()));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
{
  const JsContext context(shared_from_this());
  return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val)));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
{
  const JsContext context(shared_from_this());
  return JsValuePtr(new JsValue(shared_from_this(),
    v8::Number::New(isolate, val)));
}
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
{
  const JsContext context(shared_from_this());
  return JsValuePtr(new JsValue(shared_from_this(),
    Utils::ToV8String(isolate, val)));
}