Index: src/JsEngine.cpp |
=================================================================== |
--- a/src/JsEngine.cpp |
+++ b/src/JsEngine.cpp |
@@ -1,27 +1,16 @@ |
#include <AdblockPlus.h> |
#include <sstream> |
#include "GlobalJsObject.h" |
#include "Utils.h" |
namespace |
{ |
- v8::Handle<v8::Context> CreateContext( |
- v8::Isolate* isolate, |
- AdblockPlus::JsEngine& jsEngine) |
- { |
- const v8::Locker locker(isolate); |
- const v8::HandleScope handleScope; |
- const v8::Handle<v8::ObjectTemplate> global = |
- AdblockPlus::GlobalJsObject::Create(jsEngine); |
- return v8::Context::New(0, global); |
- } |
- |
v8::Handle<v8::Script> CompileScript(const std::string& source, const std::string& filename) |
{ |
const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); |
if (filename.length()) |
{ |
const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()); |
return v8::Script::Compile(v8Source, v8Filename); |
} |
@@ -56,19 +45,24 @@ AdblockPlus::JsError::JsError(const v8:: |
: std::runtime_error(ExceptionToString(exception, message)) |
{ |
} |
AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem, |
WebRequest* const webRequest, |
ErrorCallback* const errorCallback) |
: fileSystem(*fileSystem), webRequest(*webRequest), |
- errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()), |
- context(CreateContext(isolate, *this)) |
+ errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()) |
{ |
+ const v8::Locker locker(isolate); |
+ const v8::HandleScope handleScope; |
+ |
+ context = v8::Context::New(); |
+ AdblockPlus::GlobalJsObject::Setup(*this, |
+ JsValuePtr(new JsValue(*this, context->Global()))); |
} |
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& source, |
const std::string& filename) |
{ |
const Context context(*this); |
const v8::TryCatch tryCatch; |
const v8::Handle<v8::Script> script = CompileScript(source, filename); |
@@ -110,16 +104,26 @@ AdblockPlus::JsValuePtr AdblockPlus::JsE |
} |
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
{ |
const Context context(*this); |
return JsValuePtr(new JsValue(*this, v8::Object::New())); |
} |
+AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |
+ v8::InvocationCallback callback) |
+{ |
+ const Context context(*this); |
+ |
+ v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, |
+ v8::External::New(this)); |
+ return JsValuePtr(new JsValue(*this, templ->GetFunction())); |
+} |
+ |
AdblockPlus::JsEngine& AdblockPlus::JsEngine::FromArguments(const v8::Arguments& arguments) |
{ |
const v8::Local<const v8::External> external = |
v8::Local<const v8::External>::Cast(arguments.Data()); |
return *static_cast<JsEngine* const>(external->Value()); |
} |
AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Arguments& arguments) |