2013-12-18 04:45:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkV8Example_Global_DEFINED
|
|
|
|
#define SkV8Example_Global_DEFINED
|
|
|
|
|
2013-12-20 15:56:52 +00:00
|
|
|
#include <map>
|
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
#include <v8.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
#include "SkEvent.h"
|
|
|
|
|
|
|
|
class SkOSWindow;
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
typedef v8::Persistent<v8::Function, v8::CopyablePersistentTraits<v8::Function> > CopyablePersistentFn;
|
2013-12-20 15:56:52 +00:00
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
// Provides the global isolate and context for our V8 instance.
|
|
|
|
// Also implements all the global level functions.
|
|
|
|
class Global : SkNoncopyable {
|
|
|
|
public:
|
2014-10-24 19:49:17 +00:00
|
|
|
Global(v8::Isolate* isolate)
|
2013-12-18 04:45:37 +00:00
|
|
|
: fIsolate(isolate)
|
|
|
|
, fWindow(NULL)
|
2013-12-20 15:56:52 +00:00
|
|
|
, fLastTimerID(0)
|
2013-12-18 04:45:37 +00:00
|
|
|
{
|
|
|
|
gGlobal = this;
|
2014-01-06 18:17:24 +00:00
|
|
|
this->initialize();
|
2013-12-18 04:45:37 +00:00
|
|
|
}
|
|
|
|
virtual ~Global() {}
|
|
|
|
|
|
|
|
// The script will be parsed into the context this Global contains.
|
|
|
|
bool parseScript(const char script[]);
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Local<v8::Context> getContext() {
|
|
|
|
return v8::Local<v8::Context>::New(fIsolate, fContext);
|
2013-12-18 04:45:37 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Isolate* getIsolate() {
|
2013-12-18 04:45:37 +00:00
|
|
|
return fIsolate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWindow(SkOSWindow* win) {
|
|
|
|
fWindow = win;
|
|
|
|
}
|
|
|
|
SkOSWindow* getWindow() {
|
|
|
|
return fWindow;
|
|
|
|
}
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
void reportException(v8::TryCatch* tryCatch);
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
private:
|
2014-01-06 18:17:24 +00:00
|
|
|
void initialize();
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Handle<v8::Context> createRootContext();
|
2013-12-20 15:56:52 +00:00
|
|
|
int32_t getNextTimerID();
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
static bool TimeOutProc(const SkEvent& evt);
|
|
|
|
|
|
|
|
// Static functions that implement the global JS functions we add to
|
|
|
|
// the context.
|
|
|
|
static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2014-10-24 19:49:17 +00:00
|
|
|
static void Inval(const v8::FunctionCallbackInfo<v8::Value>& args);
|
2013-12-18 04:45:37 +00:00
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::Persistent<v8::Context> fContext;
|
|
|
|
v8::Isolate* fIsolate;
|
|
|
|
SkOSWindow* fWindow;
|
|
|
|
static Global* gGlobal;
|
2013-12-18 04:45:37 +00:00
|
|
|
|
2013-12-20 15:56:52 +00:00
|
|
|
// Handle to the functions to call when a timeout triggers as indexed by id.
|
|
|
|
std::map<int32_t, CopyablePersistentFn > fTimeouts;
|
|
|
|
|
|
|
|
// Last timer ID generated.
|
|
|
|
int32_t fLastTimerID;
|
2013-12-18 04:45:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|