OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 public: | 57 public: |
58 static void Init() | 58 static void Init() |
59 { | 59 { |
60 // it's threadsafe since C++11 and it will be instantiated only once and | 60 // it's threadsafe since C++11 and it will be instantiated only once and |
61 // destroyed at the application exit | 61 // destroyed at the application exit |
62 static V8Initializer initializer; | 62 static V8Initializer initializer; |
63 } | 63 } |
64 }; | 64 }; |
65 } | 65 } |
66 | 66 |
| 67 using namespace AdblockPlus; |
| 68 |
67 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | 69 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() |
68 { | 70 { |
69 V8Initializer::Init(); | 71 V8Initializer::Init(); |
70 isolate = v8::Isolate::New(); | 72 isolate = v8::Isolate::New(); |
71 } | 73 } |
72 | 74 |
73 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | 75 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() |
74 { | 76 { |
75 isolate->Dispose(); | 77 isolate->Dispose(); |
76 isolate = nullptr; | 78 isolate = nullptr; |
77 } | 79 } |
78 | 80 |
| 81 JsEngine::TimerTaskInfo::~TimerTaskInfo() |
| 82 { |
| 83 for (auto& arg : arguments) |
| 84 arg->Dispose(); |
| 85 } |
| 86 |
| 87 JsEngine::TimerTask JsEngine::CreateTimerTask(const v8::Arguments& arguments) |
| 88 { |
| 89 if (arguments.Length() < 2) |
| 90 throw std::runtime_error("setTimeout requires at least 2 parameters"); |
| 91 |
| 92 if (!arguments[0]->IsFunction()) |
| 93 throw std::runtime_error("First argument to setTimeout must be a function"); |
| 94 |
| 95 auto ii_timerTaskInfo = timerTaskInfos.emplace(timerTaskInfos.end()); |
| 96 ii_timerTaskInfo->delay = arguments[1]->IntegerValue(); |
| 97 |
| 98 for (int i = 0; i < arguments.Length(); i++) |
| 99 ii_timerTaskInfo->arguments.emplace_back(new v8::Persistent<v8::Value>(GetIs
olate(), arguments[i])); |
| 100 TimerTask retValue = { shared_from_this(), ii_timerTaskInfo }; |
| 101 return retValue; |
| 102 } |
| 103 |
| 104 void JsEngine::CallTimerTask(TimerTaskInfos::const_iterator ii_taskInfo) |
| 105 { |
| 106 const JsContext context(shared_from_this()); |
| 107 JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
ii_taskInfo->arguments[0])); |
| 108 JsValueList callbackArgs; |
| 109 for (int i = 2; i < ii_taskInfo->arguments.size(); i++) |
| 110 callbackArgs.emplace_back(new JsValue(shared_from_this(), |
| 111 v8::Local<v8::Value>::New(GetIsolate(), *ii_taskInfo->arguments[i]))); |
| 112 callback.Call(callbackArgs); |
| 113 timerTaskInfos.erase(ii_taskInfo); |
| 114 } |
| 115 |
79 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) | 116 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) |
80 : isolate(isolate) | 117 : isolate(isolate) |
81 { | 118 { |
82 } | 119 } |
83 | 120 |
84 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons
t ScopedV8IsolatePtr& isolate) | 121 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons
t ScopedV8IsolatePtr& isolate) |
85 { | 122 { |
86 JsEnginePtr result(new JsEngine(isolate)); | 123 JsEnginePtr result(new JsEngine(isolate)); |
87 | 124 |
88 const v8::Locker locker(result->GetIsolate()); | 125 const v8::Locker locker(result->GetIsolate()); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 310 |
274 | 311 |
275 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 312 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
276 AdblockPlus::JsValuePtr value) | 313 AdblockPlus::JsValuePtr value) |
277 { | 314 { |
278 auto global = GetGlobalObject(); | 315 auto global = GetGlobalObject(); |
279 if (!global) | 316 if (!global) |
280 throw std::runtime_error("Global object cannot be null"); | 317 throw std::runtime_error("Global object cannot be null"); |
281 global->SetProperty(name, value); | 318 global->SetProperty(name, value); |
282 } | 319 } |
OLD | NEW |