aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-03-04 20:44:32 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2014-03-04 20:44:32 +0000
commit24e0496e8d58e779487307fbe0ae20d5fea4cfc0 (patch)
treec1d1ed7fe41243ed7b248c5a67b989cb64b35112
parenta100bb25a5bdad593b088c706ba767c779de9f58 (diff)
Factor out a BaseContext from JsContext.
BUG=skia: R=robertphillips@google.com Author: jcgregorio@google.com Review URL: https://codereview.chromium.org/186783004 git-svn-id: http://skia.googlecode.com/svn/trunk@13660 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--experimental/SkV8Example/BaseContext.cpp286
-rw-r--r--experimental/SkV8Example/BaseContext.h84
-rw-r--r--experimental/SkV8Example/JsContext.cpp259
-rw-r--r--experimental/SkV8Example/JsContext.h64
-rw-r--r--gyp/v8.gyp2
5 files changed, 391 insertions, 304 deletions
diff --git a/experimental/SkV8Example/BaseContext.cpp b/experimental/SkV8Example/BaseContext.cpp
new file mode 100644
index 0000000000..486474e397
--- /dev/null
+++ b/experimental/SkV8Example/BaseContext.cpp
@@ -0,0 +1,286 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ */
+#include <v8.h>
+
+using namespace v8;
+
+#include "Global.h"
+#include "BaseContext.h"
+#include "Path2D.h"
+#include "SkCanvas.h"
+
+
+BaseContext* BaseContext::Unwrap(Handle<Object> obj) {
+ Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
+ void* ptr = field->Value();
+ return static_cast<BaseContext*>(ptr);
+}
+
+void BaseContext::FillRect(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 4) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 4 arguments required."));
+ return;
+ }
+ double x = args[0]->NumberValue();
+ double y = args[1]->NumberValue();
+ double w = args[2]->NumberValue();
+ double h = args[3]->NumberValue();
+
+ SkRect rect = {
+ SkDoubleToScalar(x),
+ SkDoubleToScalar(y),
+ SkDoubleToScalar(x) + SkDoubleToScalar(w),
+ SkDoubleToScalar(y) + SkDoubleToScalar(h)
+ };
+ canvas->drawRect(rect, BaseContext->fFillStyle);
+}
+
+void BaseContext::Save(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ canvas->save();
+}
+
+void BaseContext::Restore(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ canvas->restore();
+}
+
+void BaseContext::Rotate(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 1) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 1 arguments required."));
+ return;
+ }
+ double angle = args[0]->NumberValue();
+ canvas->rotate(SkRadiansToDegrees(angle));
+}
+
+void BaseContext::Translate(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 2) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 2 arguments required."));
+ return;
+ }
+ double dx = args[0]->NumberValue();
+ double dy = args[1]->NumberValue();
+ canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
+}
+
+void BaseContext::ResetTransform(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ canvas->resetMatrix();
+}
+
+void BaseContext::Stroke(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 1) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 1 arguments required."));
+ return;
+ }
+
+ Handle<External> field = Handle<External>::Cast(
+ args[0]->ToObject()->GetInternalField(0));
+ void* ptr = field->Value();
+ Path2D* path = static_cast<Path2D*>(ptr);
+
+ canvas->drawPath(path->getSkPath(), BaseContext->fStrokeStyle);
+}
+
+void BaseContext::Fill(const v8::FunctionCallbackInfo<Value>& args) {
+ BaseContext* BaseContext = Unwrap(args.This());
+ SkCanvas* canvas = BaseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+
+ if (args.Length() != 1) {
+ args.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ args.GetIsolate(), "Error: 1 arguments required."));
+ return;
+ }
+
+ Handle<External> field = Handle<External>::Cast(
+ args[0]->ToObject()->GetInternalField(0));
+ void* ptr = field->Value();
+ Path2D* path = static_cast<Path2D*>(ptr);
+
+ canvas->drawPath(path->getSkPath(), BaseContext->fFillStyle);
+}
+
+void BaseContext::GetStyle(Local<String> name,
+ const PropertyCallbackInfo<Value>& info,
+ const SkPaint& style) {
+ char buf[8];
+ SkColor color = style.getColor();
+ sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
+ SkColorGetB(color));
+
+ info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
+}
+
+void BaseContext::SetStyle(Local<String> name, Local<Value> value,
+ const PropertyCallbackInfo<void>& info,
+ SkPaint& style) {
+ Local<String> s = value->ToString();
+ if (s->Length() != 7 && s->Length() != 9) {
+ info.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ info.GetIsolate(),
+ "Invalid fill style format length."));
+ return;
+ }
+ char buf[10];
+ s->WriteUtf8(buf, sizeof(buf));
+
+ if (buf[0] != '#') {
+ info.GetIsolate()->ThrowException(
+ v8::String::NewFromUtf8(
+ info.GetIsolate(), "Invalid fill style format."));
+ return;
+ }
+
+ // Colors can be RRGGBBAA, but SkColor uses ARGB.
+ long color = strtol(buf+1, NULL, 16);
+ uint32_t alpha = SK_AlphaOPAQUE;
+ if (s->Length() == 9) {
+ alpha = color & 0xFF;
+ color >>= 8;
+ }
+ style.setColor(SkColorSetA(SkColor(color), alpha));
+}
+
+void BaseContext::GetFillStyle(Local<String> name,
+ const PropertyCallbackInfo<Value>& info) {
+ BaseContext* baseContext = Unwrap(info.This());
+ GetStyle(name, info, baseContext->fFillStyle);
+}
+
+void BaseContext::GetStrokeStyle(Local<String> name,
+ const PropertyCallbackInfo<Value>& info) {
+ BaseContext* baseContext = Unwrap(info.This());
+ GetStyle(name, info, baseContext->fStrokeStyle);
+}
+
+void BaseContext::SetFillStyle(Local<String> name, Local<Value> value,
+ const PropertyCallbackInfo<void>& info) {
+ BaseContext* baseContext = Unwrap(info.This());
+ SetStyle(name, value, info, baseContext->fFillStyle);
+}
+
+void BaseContext::SetStrokeStyle(Local<String> name, Local<Value> value,
+ const PropertyCallbackInfo<void>& info) {
+ BaseContext* baseContext = Unwrap(info.This());
+ SetStyle(name, value, info, baseContext->fStrokeStyle);
+}
+
+
+void BaseContext::GetWidth(Local<String> name,
+ const PropertyCallbackInfo<Value>& info) {
+ BaseContext* baseContext = Unwrap(info.This());
+ SkCanvas* canvas = baseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+ SkISize size = canvas->getDeviceSize();
+
+ info.GetReturnValue().Set(
+ Int32::New(baseContext->fGlobal->getIsolate(), size.fWidth));
+}
+
+void BaseContext::GetHeight(Local<String> name,
+ const PropertyCallbackInfo<Value>& info) {
+ BaseContext* baseContext = Unwrap(info.This());
+ SkCanvas* canvas = baseContext->getCanvas();
+ if (NULL == canvas) {
+ return;
+ }
+ SkISize size = canvas->getDeviceSize();
+
+ info.GetReturnValue().Set(
+ Int32::New(baseContext->fGlobal->getIsolate(), size.fHeight));
+}
+
+#define ADD_METHOD(name, fn) \
+ tmpl->Set(String::NewFromUtf8( \
+ fGlobal->getIsolate(), name, \
+ String::kInternalizedString), \
+ FunctionTemplate::New(fGlobal->getIsolate(), fn))
+
+void BaseContext::addAttributesAndMethods(Handle<ObjectTemplate> tmpl) {
+ HandleScope scope(fGlobal->getIsolate());
+
+ // Add accessors for each of the fields of the context object.
+ tmpl->SetAccessor(String::NewFromUtf8(
+ fGlobal->getIsolate(), "fillStyle", String::kInternalizedString),
+ GetFillStyle, SetFillStyle);
+ tmpl->SetAccessor(String::NewFromUtf8(
+ fGlobal->getIsolate(), "strokeStyle", String::kInternalizedString),
+ GetStrokeStyle, SetStrokeStyle);
+ tmpl->SetAccessor(String::NewFromUtf8(
+ fGlobal->getIsolate(), "width", String::kInternalizedString),
+ GetWidth);
+ tmpl->SetAccessor(String::NewFromUtf8(
+ fGlobal->getIsolate(), "height", String::kInternalizedString),
+ GetHeight);
+
+ // Add methods.
+ ADD_METHOD("fillRect", FillRect);
+ ADD_METHOD("stroke", Stroke);
+ ADD_METHOD("fill", Fill);
+ ADD_METHOD("rotate", Rotate);
+ ADD_METHOD("save", Save);
+ ADD_METHOD("restore", Restore);
+ ADD_METHOD("translate", Translate);
+ ADD_METHOD("resetTransform", ResetTransform);
+}
diff --git a/experimental/SkV8Example/BaseContext.h b/experimental/SkV8Example/BaseContext.h
new file mode 100644
index 0000000000..b7fe3815a6
--- /dev/null
+++ b/experimental/SkV8Example/BaseContext.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ */
+
+#ifndef SkV8Example_BaseContext_DEFINED
+#define SkV8Example_BaseContext_DEFINED
+
+#include <v8.h>
+
+#include "SkPaint.h"
+
+using namespace v8;
+
+class SkCanvas;
+class Global;
+
+// BaseContext contains common functionality for both JsContext
+// and DisplayList.
+class BaseContext {
+public:
+ BaseContext(Global* global)
+ : fGlobal(global)
+ {
+ fFillStyle.setColor(SK_ColorBLACK);
+ fFillStyle.setAntiAlias(true);
+ fFillStyle.setStyle(SkPaint::kFill_Style);
+ fStrokeStyle.setColor(SK_ColorBLACK);
+ fStrokeStyle.setAntiAlias(true);
+ fStrokeStyle.setStyle(SkPaint::kStroke_Style);
+ }
+ virtual ~BaseContext() {}
+
+ // Retrieve the SkCanvas to draw on. May return NULL.
+ virtual SkCanvas* getCanvas() = 0;
+
+ // Add the Javascript attributes and methods that BaseContext implements to the ObjectTemplate.
+ void addAttributesAndMethods(Handle<ObjectTemplate> tmpl);
+
+protected:
+ // Get the pointer out of obj.
+ static BaseContext* Unwrap(Handle<Object> obj);
+
+ Global* fGlobal;
+ SkPaint fFillStyle;
+ SkPaint fStrokeStyle;
+
+private:
+ static void GetStyle(Local<String> name,
+ const PropertyCallbackInfo<Value>& info,
+ const SkPaint& style);
+ static void SetStyle(Local<String> name, Local<Value> value,
+ const PropertyCallbackInfo<void>& info,
+ SkPaint& style);
+ // JS Attributes
+ static void GetFillStyle(Local<String> name,
+ const PropertyCallbackInfo<Value>& info);
+ static void SetFillStyle(Local<String> name, Local<Value> value,
+ const PropertyCallbackInfo<void>& info);
+ static void GetStrokeStyle(Local<String> name,
+ const PropertyCallbackInfo<Value>& info);
+ static void SetStrokeStyle(Local<String> name, Local<Value> value,
+ const PropertyCallbackInfo<void>& info);
+ static void GetWidth(Local<String> name,
+ const PropertyCallbackInfo<Value>& info);
+ static void GetHeight(Local<String> name,
+ const PropertyCallbackInfo<Value>& info);
+
+ // JS Methods
+ static void FillRect(const v8::FunctionCallbackInfo<Value>& args);
+ static void Stroke(const v8::FunctionCallbackInfo<Value>& args);
+ static void Fill(const v8::FunctionCallbackInfo<Value>& args);
+ static void Rotate(const v8::FunctionCallbackInfo<Value>& args);
+ static void Save(const v8::FunctionCallbackInfo<Value>& args);
+ static void Restore(const v8::FunctionCallbackInfo<Value>& args);
+ static void Translate(const v8::FunctionCallbackInfo<Value>& args);
+ static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args);
+};
+
+#endif
diff --git a/experimental/SkV8Example/JsContext.cpp b/experimental/SkV8Example/JsContext.cpp
index c849621905..8b0b072979 100644
--- a/experimental/SkV8Example/JsContext.cpp
+++ b/experimental/SkV8Example/JsContext.cpp
@@ -23,257 +23,8 @@ static const char* to_cstring(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
-JsContext* JsContext::Unwrap(Handle<Object> obj) {
- Handle<External> field = Handle<External>::Cast(obj->GetInternalField(0));
- void* ptr = field->Value();
- return static_cast<JsContext*>(ptr);
-}
-
-void JsContext::FillRect(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- if (args.Length() != 4) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- args.GetIsolate(), "Error: 4 arguments required."));
- return;
- }
- // TODO(jcgregorio) Really figure out the conversion from JS numbers to
- // SkScalars. Maybe test if int first? Not sure of the performance impact.
- double x = args[0]->NumberValue();
- double y = args[1]->NumberValue();
- double w = args[2]->NumberValue();
- double h = args[3]->NumberValue();
-
- SkRect rect = {
- SkDoubleToScalar(x),
- SkDoubleToScalar(y),
- SkDoubleToScalar(x) + SkDoubleToScalar(w),
- SkDoubleToScalar(y) + SkDoubleToScalar(h)
- };
- canvas->drawRect(rect, jsContext->fFillStyle);
-}
-
-void JsContext::Save(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- canvas->save();
-}
-
-void JsContext::Restore(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- canvas->restore();
-}
-
-void JsContext::Rotate(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- if (args.Length() != 1) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- args.GetIsolate(), "Error: 1 arguments required."));
- return;
- }
- double angle = args[0]->NumberValue();
- canvas->rotate(SkRadiansToDegrees(angle));
-}
-
-void JsContext::Translate(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- if (args.Length() != 2) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- args.GetIsolate(), "Error: 2 arguments required."));
- return;
- }
- double dx = args[0]->NumberValue();
- double dy = args[1]->NumberValue();
- canvas->translate(SkDoubleToScalar(dx), SkDoubleToScalar(dy));
-}
-
-void JsContext::ResetTransform(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- canvas->resetMatrix();
-}
-
-void JsContext::Stroke(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- if (args.Length() != 1) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- args.GetIsolate(), "Error: 1 arguments required."));
- return;
- }
-
- Handle<External> field = Handle<External>::Cast(
- args[0]->ToObject()->GetInternalField(0));
- void* ptr = field->Value();
- Path2D* path = static_cast<Path2D*>(ptr);
-
- canvas->drawPath(path->getSkPath(), jsContext->fStrokeStyle);
-}
-
-void JsContext::Fill(const v8::FunctionCallbackInfo<Value>& args) {
- JsContext* jsContext = Unwrap(args.This());
- SkCanvas* canvas = jsContext->fCanvas;
-
- if (args.Length() != 1) {
- args.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- args.GetIsolate(), "Error: 1 arguments required."));
- return;
- }
-
- Handle<External> field = Handle<External>::Cast(
- args[0]->ToObject()->GetInternalField(0));
- void* ptr = field->Value();
- Path2D* path = static_cast<Path2D*>(ptr);
-
- canvas->drawPath(path->getSkPath(), jsContext->fFillStyle);
-}
-
-void JsContext::GetStyle(Local<String> name,
- const PropertyCallbackInfo<Value>& info,
- const SkPaint& style) {
- char buf[8];
- SkColor color = style.getColor();
- sprintf(buf, "#%02X%02X%02X", SkColorGetR(color), SkColorGetG(color),
- SkColorGetB(color));
-
- info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), buf));
-}
-
-void JsContext::SetStyle(Local<String> name, Local<Value> value,
- const PropertyCallbackInfo<void>& info,
- SkPaint& style) {
- Local<String> s = value->ToString();
- if (s->Length() != 7 && s->Length() != 9) {
- info.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- info.GetIsolate(),
- "Invalid fill style format length."));
- return;
- }
- char buf[10];
- s->WriteUtf8(buf, sizeof(buf));
-
- if (buf[0] != '#') {
- info.GetIsolate()->ThrowException(
- v8::String::NewFromUtf8(
- info.GetIsolate(), "Invalid fill style format."));
- return;
- }
-
- // Colors can be RRGGBBAA, but SkColor uses ARGB.
- long color = strtol(buf+1, NULL, 16);
- uint32_t alpha = SK_AlphaOPAQUE;
- if (s->Length() == 9) {
- alpha = color & 0xFF;
- color >>= 8;
- }
- style.setColor(SkColorSetA(SkColor(color), alpha));
-}
-
-void JsContext::GetFillStyle(Local<String> name,
- const PropertyCallbackInfo<Value>& info) {
- JsContext* jsContext = Unwrap(info.This());
- GetStyle(name, info, jsContext->fFillStyle);
-}
-
-void JsContext::GetStrokeStyle(Local<String> name,
- const PropertyCallbackInfo<Value>& info) {
- JsContext* jsContext = Unwrap(info.This());
- GetStyle(name, info, jsContext->fStrokeStyle);
-}
-
-void JsContext::SetFillStyle(Local<String> name, Local<Value> value,
- const PropertyCallbackInfo<void>& info) {
- JsContext* jsContext = Unwrap(info.This());
- SetStyle(name, value, info, jsContext->fFillStyle);
-}
-
-void JsContext::SetStrokeStyle(Local<String> name, Local<Value> value,
- const PropertyCallbackInfo<void>& info) {
- JsContext* jsContext = Unwrap(info.This());
- SetStyle(name, value, info, jsContext->fStrokeStyle);
-}
-
-
-void JsContext::GetWidth(Local<String> name,
- const PropertyCallbackInfo<Value>& info) {
- JsContext* jsContext = Unwrap(info.This());
- SkISize size = jsContext->fCanvas->getDeviceSize();
-
- info.GetReturnValue().Set(
- Int32::New(jsContext->fGlobal->getIsolate(), size.fWidth));
-}
-
-void JsContext::GetHeight(Local<String> name,
- const PropertyCallbackInfo<Value>& info) {
- JsContext* jsContext = Unwrap(info.This());
- SkISize size = jsContext->fCanvas->getDeviceSize();
-
- info.GetReturnValue().Set(
- Int32::New(jsContext->fGlobal->getIsolate(), size.fHeight));
-}
-
-
Persistent<ObjectTemplate> JsContext::gContextTemplate;
-#define ADD_METHOD(name, fn) \
- result->Set(String::NewFromUtf8( \
- fGlobal->getIsolate(), name, \
- String::kInternalizedString), \
- FunctionTemplate::New(fGlobal->getIsolate(), fn))
-
-Handle<ObjectTemplate> JsContext::makeContextTemplate() {
- EscapableHandleScope handleScope(fGlobal->getIsolate());
-
- Local<ObjectTemplate> result = ObjectTemplate::New();
-
- // Add a field to store the pointer to a JsContext instance.
- result->SetInternalFieldCount(1);
-
- // Add accessors for each of the fields of the context object.
- result->SetAccessor(String::NewFromUtf8(
- fGlobal->getIsolate(), "fillStyle", String::kInternalizedString),
- GetFillStyle, SetFillStyle);
- result->SetAccessor(String::NewFromUtf8(
- fGlobal->getIsolate(), "strokeStyle", String::kInternalizedString),
- GetStrokeStyle, SetStrokeStyle);
- result->SetAccessor(String::NewFromUtf8(
- fGlobal->getIsolate(), "width", String::kInternalizedString),
- GetWidth);
- result->SetAccessor(String::NewFromUtf8(
- fGlobal->getIsolate(), "height", String::kInternalizedString),
- GetHeight);
-
- // Add methods.
- ADD_METHOD("fillRect", FillRect);
- ADD_METHOD("stroke", Stroke);
- ADD_METHOD("fill", Fill);
- ADD_METHOD("rotate", Rotate);
- ADD_METHOD("save", Save);
- ADD_METHOD("restore", Restore);
- ADD_METHOD("translate", Translate);
- ADD_METHOD("resetTransform", ResetTransform);
-
- // Return the result through the current handle scope.
- return handleScope.Escape(result);
-}
-
-
// Wraps 'this' in a Javascript object.
Handle<Object> JsContext::wrap() {
// Handle scope for temporary handles.
@@ -282,8 +33,14 @@ Handle<Object> JsContext::wrap() {
// Fetch the template for creating JavaScript JsContext wrappers.
// It only has to be created once, which we do on demand.
if (gContextTemplate.IsEmpty()) {
- Handle<ObjectTemplate> raw_template = this->makeContextTemplate();
- gContextTemplate.Reset(fGlobal->getIsolate(), raw_template);
+ Local<ObjectTemplate> localTemplate = ObjectTemplate::New();
+
+ // Add a field to store the pointer to a JsContext instance.
+ localTemplate->SetInternalFieldCount(1);
+
+ this->addAttributesAndMethods(localTemplate);
+
+ gContextTemplate.Reset(fGlobal->getIsolate(), localTemplate);
}
Handle<ObjectTemplate> templ =
Local<ObjectTemplate>::New(fGlobal->getIsolate(), gContextTemplate);
diff --git a/experimental/SkV8Example/JsContext.h b/experimental/SkV8Example/JsContext.h
index c1466a08dd..d3993ee6c7 100644
--- a/experimental/SkV8Example/JsContext.h
+++ b/experimental/SkV8Example/JsContext.h
@@ -13,6 +13,7 @@
#include <v8.h>
#include "SkPaint.h"
+#include "BaseContext.h"
using namespace v8;
@@ -26,20 +27,14 @@ class Global;
// context.fillStyle="#FF0000";
// context.fillRect(x, y, w, h);
// }
-class JsContext {
+class JsContext : public BaseContext {
public:
JsContext(Global* global)
- : fGlobal(global)
+ : INHERITED(global)
, fCanvas(NULL)
{
- fFillStyle.setColor(SK_ColorBLACK);
- fFillStyle.setAntiAlias(true);
- fFillStyle.setStyle(SkPaint::kFill_Style);
- fStrokeStyle.setColor(SK_ColorBLACK);
- fStrokeStyle.setAntiAlias(true);
- fStrokeStyle.setStyle(SkPaint::kStroke_Style);
}
- ~JsContext();
+ virtual ~JsContext() {}
// Parse the script.
bool initialize();
@@ -47,61 +42,24 @@ public:
// Call this with the SkCanvas you want onDraw to draw on.
void onDraw(SkCanvas* canvas);
+ virtual SkCanvas* getCanvas() { return fCanvas; };
+
private:
- static void GetStyle(Local<String> name,
- const PropertyCallbackInfo<Value>& info,
- const SkPaint& style);
- static void SetStyle(Local<String> name, Local<Value> value,
- const PropertyCallbackInfo<void>& info,
- SkPaint& style);
- // JS Attributes
- static void GetFillStyle(Local<String> name,
- const PropertyCallbackInfo<Value>& info);
- static void SetFillStyle(Local<String> name, Local<Value> value,
- const PropertyCallbackInfo<void>& info);
- static void GetStrokeStyle(Local<String> name,
- const PropertyCallbackInfo<Value>& info);
- static void SetStrokeStyle(Local<String> name, Local<Value> value,
- const PropertyCallbackInfo<void>& info);
- static void GetWidth(Local<String> name,
- const PropertyCallbackInfo<Value>& info);
- static void GetHeight(Local<String> name,
- const PropertyCallbackInfo<Value>& info);
-
- // JS Methods
- static void FillRect(const v8::FunctionCallbackInfo<Value>& args);
- static void Stroke(const v8::FunctionCallbackInfo<Value>& args);
- static void Fill(const v8::FunctionCallbackInfo<Value>& args);
- static void Rotate(const v8::FunctionCallbackInfo<Value>& args);
- static void Save(const v8::FunctionCallbackInfo<Value>& args);
- static void Restore(const v8::FunctionCallbackInfo<Value>& args);
- static void Translate(const v8::FunctionCallbackInfo<Value>& args);
- static void ResetTransform(const v8::FunctionCallbackInfo<Value>& args);
-
- // Get the pointer out of obj.
- static JsContext* Unwrap(Handle<Object> obj);
-
- // Create a template for JS object associated with JsContext, called lazily
- // by Wrap() and the results are stored in gContextTemplate;
- Handle<ObjectTemplate> makeContextTemplate();
// Wrap the 'this' pointer into an Object. Can be retrieved via Unwrap.
Handle<Object> wrap();
- Global* fGlobal;
-
- // Only valid when inside OnDraw().
- SkCanvas* fCanvas;
-
- SkPaint fFillStyle;
- SkPaint fStrokeStyle;
-
// A handle to the onDraw function defined in the script.
Persistent<Function> fOnDraw;
// The template for what a canvas context object looks like. The canvas
// context object is what's passed into the JS onDraw() function.
static Persistent<ObjectTemplate> gContextTemplate;
+
+ // Only valid when inside OnDraw().
+ SkCanvas* fCanvas;
+
+ typedef BaseContext INHERITED;
};
#endif
diff --git a/gyp/v8.gyp b/gyp/v8.gyp
index 856e7f903c..b2eb54c2ef 100644
--- a/gyp/v8.gyp
+++ b/gyp/v8.gyp
@@ -16,6 +16,8 @@
'../experimental/SkV8Example/Global.h',
'../experimental/SkV8Example/Path2D.cpp',
'../experimental/SkV8Example/Path2D.h',
+ '../experimental/SkV8Example/BaseContext.cpp',
+ '../experimental/SkV8Example/BaseContext.h',
'../experimental/SkV8Example/JsContext.cpp',
'../experimental/SkV8Example/JsContext.h',
],