TEST_F(JsEngineTest, Evaluate) { jsEngine->Evaluate("function hello() { return 'Hello'; }"); AdblockPlus::JsValuePtr result = jsEngine->Evaluate("hello()"); ASSERT_TRUE(result->IsString()); ASSERT_EQ("Hello", result->AsString()); }
AdblockPlus::JsValuePtr AdblockPlus::ConsoleJsObject::Setup( AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) { obj->SetProperty("log", jsEngine->NewCallback(::LogCallback)); obj->SetProperty("debug", jsEngine->NewCallback(::DebugCallback)); obj->SetProperty("info", jsEngine->NewCallback(::InfoCallback)); obj->SetProperty("warn", jsEngine->NewCallback(::WarnCallback)); obj->SetProperty("error", jsEngine->NewCallback(::ErrorCallback)); obj->SetProperty("trace", jsEngine->NewCallback(::TraceCallback)); return obj; }
AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call( const JsValueList& params, AdblockPlus::JsValuePtr thisPtr) const { if (!IsFunction()) throw new std::runtime_error("Attempting to call a non-function"); const JsContext context(jsEngine); if (!thisPtr) thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine->context->Global())); if (!thisPtr->IsObject()) throw new std::runtime_error("`this` pointer has to be an object"); v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast<v8::Value>(thisPtr->value); std::vector<v8::Handle<v8::Value> > argv; for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it) argv.push_back((*it)->value); const v8::TryCatch tryCatch; v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast<v8::Value>(value); v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), argv.size() ? &argv.front() : 0); if (tryCatch.HasCaught()) throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); return JsValuePtr(new JsValue(jsEngine, result)); }
jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclass jsValueClass) { if (!jsValue.get()) { return 0; } jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); return env->NewObject(globalJsValueClass, jsValueClassCtor, ptr); }
void PrefsCommand::operator()(const std::string& arguments) { std::istringstream argumentStream(arguments); std::string action; argumentStream >> action; if (!action.size()) { ShowUsage(); return; } if (action == "show") { std::string pref; argumentStream >> pref; AdblockPlus::JsValuePtr value = filterEngine.GetPref(pref); if (value->IsUndefined()) std::cout << "No such preference" << std::endl; else { if (value->IsString()) std::cout << "(string) "; else if (value->IsNumber()) std::cout << "(number) "; else if (value->IsBool()) std::cout << "(bool) "; else std::cout << "(unknown type) "; std::cout << value->AsString() << std::endl; } }
TEST_F(JsEngineTest, ValueCreation) { AdblockPlus::JsValuePtr value; value = jsEngine->NewValue("foo"); ASSERT_TRUE(value->IsString()); ASSERT_EQ("foo", value->AsString()); value = jsEngine->NewValue(12345678901234); ASSERT_TRUE(value->IsNumber()); ASSERT_EQ(12345678901234, value->AsInt()); value = jsEngine->NewValue(true); ASSERT_TRUE(value->IsBool()); ASSERT_TRUE(value->AsBool()); value = jsEngine->NewObject(); ASSERT_TRUE(value->IsObject()); ASSERT_EQ(0u, value->GetOwnPropertyNames().size()); }
TEST_F(JsValueTest, IntValue) { AdblockPlus::JsValuePtr value = jsEngine->Evaluate("12345678901234"); ASSERT_FALSE(value->IsUndefined()); ASSERT_FALSE(value->IsNull()); ASSERT_FALSE(value->IsString()); ASSERT_FALSE(value->IsBool()); ASSERT_TRUE(value->IsNumber()); ASSERT_FALSE(value->IsObject()); ASSERT_FALSE(value->IsArray()); ASSERT_FALSE(value->IsFunction()); ASSERT_EQ("12345678901234", value->AsString()); ASSERT_EQ(12345678901234, value->AsInt()); ASSERT_TRUE(value->AsBool()); ASSERT_ANY_THROW(value->AsList()); ASSERT_ANY_THROW(value->GetProperty("foo")); ASSERT_ANY_THROW(value->SetProperty("foo", false)); ASSERT_ANY_THROW(value->GetClass()); ASSERT_ANY_THROW(value->GetOwnPropertyNames()); ASSERT_ANY_THROW(value->Call()); }
AdblockPlus::JsValuePtr AdblockPlus::WebRequestJsObject::Setup( AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValuePtr obj) { obj->SetProperty("GET", jsEngine->NewCallback(::GETCallback)); return obj; }