javascript_environment.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/javascript_environment.h"
  5. #include <string>
  6. #include "base/command_line.h"
  7. #include "base/message_loop/message_loop.h"
  8. #include "base/task_scheduler/initialization_util.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "content/public/common/content_switches.h"
  11. #include "gin/array_buffer.h"
  12. #include "gin/v8_initializer.h"
  13. #include "atom/common/node_includes.h"
  14. namespace atom {
  15. JavascriptEnvironment::JavascriptEnvironment()
  16. : initialized_(Initialize()),
  17. isolate_holder_(base::ThreadTaskRunnerHandle::Get()),
  18. isolate_(isolate_holder_.isolate()),
  19. isolate_scope_(isolate_),
  20. locker_(isolate_),
  21. handle_scope_(isolate_),
  22. context_(isolate_, v8::Context::New(isolate_)),
  23. context_scope_(v8::Local<v8::Context>::New(isolate_, context_)) {
  24. }
  25. void JavascriptEnvironment::OnMessageLoopCreated() {
  26. isolate_holder_.AddRunMicrotasksObserver();
  27. }
  28. void JavascriptEnvironment::OnMessageLoopDestroying() {
  29. isolate_holder_.RemoveRunMicrotasksObserver();
  30. }
  31. bool JavascriptEnvironment::Initialize() {
  32. auto cmd = base::CommandLine::ForCurrentProcess();
  33. // --js-flags.
  34. std::string js_flags = cmd->GetSwitchValueASCII(switches::kJavaScriptFlags);
  35. if (!js_flags.empty())
  36. v8::V8::SetFlagsFromString(js_flags.c_str(), js_flags.size());
  37. // The V8Platform of gin relies on Chromium's task schedule, which has not
  38. // been started at this point, so we have to rely on Node's V8Platform.
  39. platform_ = node::CreatePlatform(
  40. base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.1, 0),
  41. uv_default_loop(), nullptr);
  42. v8::V8::InitializePlatform(platform_);
  43. gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode,
  44. gin::IsolateHolder::kStableV8Extras,
  45. gin::ArrayBufferAllocator::SharedInstance(),
  46. false);
  47. return true;
  48. }
  49. NodeEnvironment::NodeEnvironment(node::Environment* env) : env_(env) {
  50. }
  51. NodeEnvironment::~NodeEnvironment() {
  52. node::FreeEnvironment(env_);
  53. }
  54. } // namespace atom