10fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org/* 20fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org * Copyright 2013 Google Inc. 30fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org * 40fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org * 50fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be 60fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org * found in the LICENSE file. 70fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org * 80fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org */ 90fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 100fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org#ifndef SkV8Example_Global_DEFINED 110fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org#define SkV8Example_Global_DEFINED 120fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 13872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org#include <map> 14872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org 150fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org#include <v8.h> 160fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 170fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.orgusing namespace v8; 180fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 190fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org#include "SkTypes.h" 200fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org#include "SkEvent.h" 210fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 220fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.orgclass SkOSWindow; 230fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 24872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.orgtypedef Persistent<Function, CopyablePersistentTraits<Function> > CopyablePersistentFn; 25872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org 260fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org// Provides the global isolate and context for our V8 instance. 270fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org// Also implements all the global level functions. 280fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.orgclass Global : SkNoncopyable { 290fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.orgpublic: 300fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org Global(Isolate* isolate) 310fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org : fIsolate(isolate) 320fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org , fWindow(NULL) 33872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org , fLastTimerID(0) 340fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org { 350fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org gGlobal = this; 36c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org this->initialize(); 370fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org } 380fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org virtual ~Global() {} 390fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 400fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org // The script will be parsed into the context this Global contains. 410fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org bool parseScript(const char script[]); 420fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 430fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org Local<Context> getContext() { 440fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org return Local<Context>::New(fIsolate, fContext); 450fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org } 460fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 470fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org Isolate* getIsolate() { 480fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org return fIsolate; 490fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org } 500fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 510fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org void setWindow(SkOSWindow* win) { 520fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org fWindow = win; 530fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org } 540fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org SkOSWindow* getWindow() { 550fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org return fWindow; 560fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org } 570fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 580fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org void reportException(TryCatch* tryCatch); 590fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 600fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.orgprivate: 61c8d732800e1a660fca6d2478fc24d6e6935ef8adcommit-bot@chromium.org void initialize(); 620fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org Handle<Context> createRootContext(); 63872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org int32_t getNextTimerID(); 640fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 650fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org static bool TimeOutProc(const SkEvent& evt); 660fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 670fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org // Static functions that implement the global JS functions we add to 680fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org // the context. 690fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args); 700fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org static void Print(const v8::FunctionCallbackInfo<v8::Value>& args); 710fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org static void Inval(const v8::FunctionCallbackInfo<Value>& args); 720fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 730fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org Persistent<Context> fContext; 740fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org Isolate* fIsolate; 750fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org SkOSWindow* fWindow; 760fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org static Global* gGlobal; 770fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 78872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org // Handle to the functions to call when a timeout triggers as indexed by id. 79872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org std::map<int32_t, CopyablePersistentFn > fTimeouts; 80872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org 81872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org // Last timer ID generated. 82872796bb8eaabbd9ab76dc8821310438aa690d85commit-bot@chromium.org int32_t fLastTimerID; 830fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org}; 840fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org 850fc0dea333f313a579558beb7ba498db0711780ecommit-bot@chromium.org#endif 86