shell_runner.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "gin/shell_runner.h"
  5. #include <memory>
  6. #include "gin/converter.h"
  7. #include "gin/per_context_data.h"
  8. #include "gin/public/context_holder.h"
  9. #include "gin/try_catch.h"
  10. #include "v8/include/v8-script.h"
  11. using v8::Context;
  12. using v8::HandleScope;
  13. using v8::Isolate;
  14. using v8::Object;
  15. using v8::ObjectTemplate;
  16. using v8::Script;
  17. namespace gin {
  18. ShellRunnerDelegate::ShellRunnerDelegate() = default;
  19. ShellRunnerDelegate::~ShellRunnerDelegate() = default;
  20. v8::Local<ObjectTemplate> ShellRunnerDelegate::GetGlobalTemplate(
  21. ShellRunner* runner,
  22. v8::Isolate* isolate) {
  23. return v8::Local<ObjectTemplate>();
  24. }
  25. void ShellRunnerDelegate::DidCreateContext(ShellRunner* runner) {
  26. }
  27. void ShellRunnerDelegate::WillRunScript(ShellRunner* runner) {
  28. }
  29. void ShellRunnerDelegate::DidRunScript(ShellRunner* runner) {
  30. }
  31. void ShellRunnerDelegate::UnhandledException(ShellRunner* runner,
  32. TryCatch& try_catch) {
  33. CHECK(false) << try_catch.GetStackTrace();
  34. }
  35. ShellRunner::ShellRunner(ShellRunnerDelegate* delegate, Isolate* isolate)
  36. : delegate_(delegate) {
  37. v8::Isolate::Scope isolate_scope(isolate);
  38. HandleScope handle_scope(isolate);
  39. v8::Local<v8::Context> context =
  40. Context::New(isolate, NULL, delegate_->GetGlobalTemplate(this, isolate));
  41. context_holder_ = std::make_unique<ContextHolder>(isolate);
  42. context_holder_->SetContext(context);
  43. PerContextData::From(context)->set_runner(this);
  44. v8::Context::Scope scope(context);
  45. delegate_->DidCreateContext(this);
  46. }
  47. ShellRunner::~ShellRunner() = default;
  48. v8::MaybeLocal<v8::Value> ShellRunner::Run(const std::string& source,
  49. const std::string& resource_name) {
  50. v8::Isolate* isolate = GetContextHolder()->isolate();
  51. TryCatch try_catch(isolate);
  52. v8::ScriptOrigin origin(isolate, StringToV8(isolate, resource_name));
  53. auto maybe_script = Script::Compile(GetContextHolder()->context(),
  54. StringToV8(isolate, source), &origin);
  55. v8::Local<Script> script;
  56. if (!maybe_script.ToLocal(&script)) {
  57. delegate_->UnhandledException(this, try_catch);
  58. return v8::MaybeLocal<v8::Value>();
  59. }
  60. return Run(script);
  61. }
  62. ContextHolder* ShellRunner::GetContextHolder() {
  63. return context_holder_.get();
  64. }
  65. v8::MaybeLocal<v8::Value> ShellRunner::Run(v8::Local<Script> script) {
  66. TryCatch try_catch(GetContextHolder()->isolate());
  67. delegate_->WillRunScript(this);
  68. auto maybe = script->Run(GetContextHolder()->context());
  69. delegate_->DidRunScript(this);
  70. if (maybe.IsEmpty()) {
  71. delegate_->UnhandledException(this, try_catch);
  72. }
  73. return maybe;
  74. }
  75. } // namespace gin