Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsEngine.cpp

Issue 29365532: Issue #3594 - Remove `globalJsObject` and add `GetGlobalObject()` (Closed)
Patch Set: Created Nov. 28, 2016, 2:05 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate) 84 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate)
85 { 85 {
86 JsEnginePtr result(new JsEngine(isolate)); 86 JsEnginePtr result(new JsEngine(isolate));
87 87
88 const v8::Locker locker(result->GetIsolate()); 88 const v8::Locker locker(result->GetIsolate());
89 const v8::Isolate::Scope isolateScope(result->GetIsolate()); 89 const v8::Isolate::Scope isolateScope(result->GetIsolate());
90 const v8::HandleScope handleScope(result->GetIsolate()); 90 const v8::HandleScope handleScope(result->GetIsolate());
91 91
92 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), 92 result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(),
93 v8::Context::New(result->GetIsolate()))); 93 v8::Context::New(result->GetIsolate())));
94 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( 94 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->GetGlobalObject()) ;
95 result->GetIsolate(), *result->context)->Global();
96 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext));
97 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject);
98 return result; 95 return result;
99 } 96 }
100 97
98 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::GetGlobalObject()
99 {
100 const auto i = GetIsolate();
sergei 2016/11/28 15:04:33 Why not to use JsContext context(shared_from_this(
Eric 2016/11/28 15:33:24 That should work. The code here is a minimal extra
101 const v8::Locker locker(i);
102 const v8::Isolate::Scope isolateScope(i);
103 const v8::HandleScope handleScope(i);
104 return JsValuePtr(new JsValue(shared_from_this(), v8::Local<v8::Context>::New( i, *context)->Global()));
105 };
106
101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 107 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
102 const std::string& filename) 108 const std::string& filename)
103 { 109 {
104 const JsContext context(shared_from_this()); 110 const JsContext context(shared_from_this());
105 const v8::TryCatch tryCatch; 111 const v8::TryCatch tryCatch;
106 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, 112 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source,
107 filename); 113 filename);
108 CheckTryCatch(tryCatch); 114 CheckTryCatch(tryCatch);
109 v8::Local<v8::Value> result = script->Run(); 115 v8::Local<v8::Value> result = script->Run();
110 CheckTryCatch(tryCatch); 116 CheckTryCatch(tryCatch);
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 if (!val) 243 if (!val)
238 throw std::runtime_error("LogSystem cannot be null"); 244 throw std::runtime_error("LogSystem cannot be null");
239 245
240 logSystem = val; 246 logSystem = val;
241 } 247 }
242 248
243 249
244 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 250 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
245 AdblockPlus::JsValuePtr value) 251 AdblockPlus::JsValuePtr value)
246 { 252 {
247 if (!globalJsObject) 253 auto global = GetGlobalObject();
254 if (!global)
248 throw std::runtime_error("Global object cannot be null"); 255 throw std::runtime_error("Global object cannot be null");
249 256 global->SetProperty(name, value);
250 globalJsObject->SetProperty(name, value);
251 } 257 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld