| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_RUNNER_H_ | 5 #ifndef GIN_RUNNER_H_ |
| 6 #define GIN_RUNNER_H_ | 6 #define GIN_RUNNER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include <string> |
| 9 #include "v8/include/v8.h" | 9 #include "gin/context_holder.h" |
| 10 | 10 |
| 11 namespace gin { | 11 namespace gin { |
| 12 | 12 |
| 13 class Runner; | 13 class Runner; |
| 14 | 14 |
| 15 class RunnerDelegate { | 15 class RunnerDelegate { |
| 16 public: | 16 public: |
| 17 // Returns the object that is passed to the script's |main| function. | 17 RunnerDelegate(); |
| 18 virtual v8::Handle<v8::Object> CreateRootObject(Runner* runner) = 0; | 18 virtual ~RunnerDelegate(); |
| 19 | 19 |
| 20 protected: | 20 // Returns the template for the global object. |
| 21 virtual ~RunnerDelegate(); | 21 virtual v8::Handle<v8::ObjectTemplate> GetGlobalTemplate(Runner* runner); |
| 22 |
| 23 virtual void DidCreateContext(Runner* runner); |
| 22 }; | 24 }; |
| 23 | 25 |
| 24 class Runner { | 26 class Runner : public ContextHolder { |
| 25 public: | 27 public: |
| 26 Runner(RunnerDelegate* delegate, v8::Isolate* isolate); | 28 Runner(RunnerDelegate* delegate, v8::Isolate* isolate); |
| 27 ~Runner(); | 29 ~Runner(); |
| 28 | 30 |
| 31 void Run(const std::string& script); |
| 29 void Run(v8::Handle<v8::Script> script); | 32 void Run(v8::Handle<v8::Script> script); |
| 30 | 33 |
| 31 v8::Isolate* isolate() const { return isolate_; } | |
| 32 | |
| 33 v8::Handle<v8::Context> context() const { | |
| 34 return v8::Local<v8::Context>::New(isolate_, context_); | |
| 35 } | |
| 36 | |
| 37 v8::Handle<v8::Object> global() const { | 34 v8::Handle<v8::Object> global() const { |
| 38 return context()->Global(); | 35 return context()->Global(); |
| 39 } | 36 } |
| 40 | 37 |
| 41 class Scope { | 38 class Scope { |
| 42 public: | 39 public: |
| 43 explicit Scope(Runner* runner); | 40 explicit Scope(Runner* runner); |
| 44 ~Scope(); | 41 ~Scope(); |
| 45 | 42 |
| 46 private: | 43 private: |
| 47 v8::HandleScope handle_scope_; | 44 v8::HandleScope handle_scope_; |
| 48 v8::Context::Scope scope_; | 45 v8::Context::Scope scope_; |
| 49 | 46 |
| 50 DISALLOW_COPY_AND_ASSIGN(Scope); | 47 DISALLOW_COPY_AND_ASSIGN(Scope); |
| 51 }; | 48 }; |
| 52 | 49 |
| 53 private: | 50 private: |
| 54 friend class Scope; | 51 friend class Scope; |
| 55 | 52 |
| 56 v8::Handle<v8::Function> GetMain(); | |
| 57 | |
| 58 RunnerDelegate* delegate_; | 53 RunnerDelegate* delegate_; |
| 59 v8::Isolate* isolate_; | |
| 60 v8::Persistent<v8::Context> context_; | |
| 61 | 54 |
| 62 DISALLOW_COPY_AND_ASSIGN(Runner); | 55 DISALLOW_COPY_AND_ASSIGN(Runner); |
| 63 }; | 56 }; |
| 64 | 57 |
| 65 } // namespace gin | 58 } // namespace gin |
| 66 | 59 |
| 67 #endif // GIN_RUNNER_H_ | 60 #endif // GIN_RUNNER_H_ |
| OLD | NEW |