| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 #ifndef SkV8Example_Global_DEFINED | 10 #ifndef SkV8Example_Global_DEFINED |
| 11 #define SkV8Example_Global_DEFINED | 11 #define SkV8Example_Global_DEFINED |
| 12 | 12 |
| 13 #include <map> | 13 #include <map> |
| 14 | 14 |
| 15 #include <v8.h> | 15 #include <v8.h> |
| 16 | 16 |
| 17 using namespace v8; | |
| 18 | 17 |
| 19 #include "SkTypes.h" | 18 #include "SkTypes.h" |
| 20 #include "SkEvent.h" | 19 #include "SkEvent.h" |
| 21 | 20 |
| 22 class SkOSWindow; | 21 class SkOSWindow; |
| 23 | 22 |
| 24 typedef Persistent<Function, CopyablePersistentTraits<Function> > CopyablePersis
tentFn; | 23 typedef v8::Persistent<v8::Function, v8::CopyablePersistentTraits<v8::Function>
> CopyablePersistentFn; |
| 25 | 24 |
| 26 // Provides the global isolate and context for our V8 instance. | 25 // Provides the global isolate and context for our V8 instance. |
| 27 // Also implements all the global level functions. | 26 // Also implements all the global level functions. |
| 28 class Global : SkNoncopyable { | 27 class Global : SkNoncopyable { |
| 29 public: | 28 public: |
| 30 Global(Isolate* isolate) | 29 Global(v8::Isolate* isolate) |
| 31 : fIsolate(isolate) | 30 : fIsolate(isolate) |
| 32 , fWindow(NULL) | 31 , fWindow(NULL) |
| 33 , fLastTimerID(0) | 32 , fLastTimerID(0) |
| 34 { | 33 { |
| 35 gGlobal = this; | 34 gGlobal = this; |
| 36 this->initialize(); | 35 this->initialize(); |
| 37 } | 36 } |
| 38 virtual ~Global() {} | 37 virtual ~Global() {} |
| 39 | 38 |
| 40 // The script will be parsed into the context this Global contains. | 39 // The script will be parsed into the context this Global contains. |
| 41 bool parseScript(const char script[]); | 40 bool parseScript(const char script[]); |
| 42 | 41 |
| 43 Local<Context> getContext() { | 42 v8::Local<v8::Context> getContext() { |
| 44 return Local<Context>::New(fIsolate, fContext); | 43 return v8::Local<v8::Context>::New(fIsolate, fContext); |
| 45 } | 44 } |
| 46 | 45 |
| 47 Isolate* getIsolate() { | 46 v8::Isolate* getIsolate() { |
| 48 return fIsolate; | 47 return fIsolate; |
| 49 } | 48 } |
| 50 | 49 |
| 51 void setWindow(SkOSWindow* win) { | 50 void setWindow(SkOSWindow* win) { |
| 52 fWindow = win; | 51 fWindow = win; |
| 53 } | 52 } |
| 54 SkOSWindow* getWindow() { | 53 SkOSWindow* getWindow() { |
| 55 return fWindow; | 54 return fWindow; |
| 56 } | 55 } |
| 57 | 56 |
| 58 void reportException(TryCatch* tryCatch); | 57 void reportException(v8::TryCatch* tryCatch); |
| 59 | 58 |
| 60 private: | 59 private: |
| 61 void initialize(); | 60 void initialize(); |
| 62 Handle<Context> createRootContext(); | 61 v8::Handle<v8::Context> createRootContext(); |
| 63 int32_t getNextTimerID(); | 62 int32_t getNextTimerID(); |
| 64 | 63 |
| 65 static bool TimeOutProc(const SkEvent& evt); | 64 static bool TimeOutProc(const SkEvent& evt); |
| 66 | 65 |
| 67 // Static functions that implement the global JS functions we add to | 66 // Static functions that implement the global JS functions we add to |
| 68 // the context. | 67 // the context. |
| 69 static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args); | 68 static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 70 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 69 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 71 static void Inval(const v8::FunctionCallbackInfo<Value>& args); | 70 static void Inval(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 72 | 71 |
| 73 Persistent<Context> fContext; | 72 v8::Persistent<v8::Context> fContext; |
| 74 Isolate* fIsolate; | 73 v8::Isolate* fIsolate; |
| 75 SkOSWindow* fWindow; | 74 SkOSWindow* fWindow; |
| 76 static Global* gGlobal; | 75 static Global* gGlobal; |
| 77 | 76 |
| 78 // Handle to the functions to call when a timeout triggers as indexed by id. | 77 // Handle to the functions to call when a timeout triggers as indexed by id. |
| 79 std::map<int32_t, CopyablePersistentFn > fTimeouts; | 78 std::map<int32_t, CopyablePersistentFn > fTimeouts; |
| 80 | 79 |
| 81 // Last timer ID generated. | 80 // Last timer ID generated. |
| 82 int32_t fLastTimerID; | 81 int32_t fLastTimerID; |
| 83 }; | 82 }; |
| 84 | 83 |
| 85 #endif | 84 #endif |
| OLD | NEW |