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