Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: experimental/SkV8Example/JsContext.cpp

Issue 673223002: Get SkV8Example running again. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/SkV8Example/JsContext.h ('k') | experimental/SkV8Example/Path2D.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2013 Google Inc. 3 * Copyright 2013 Google Inc.
4 * 4 *
5 * 5 *
6 * Use of this source code is governed by a BSD-style license that can be 6 * Use of this source code is governed by a BSD-style license that can be
7 * found in the LICENSE file. 7 * found in the LICENSE file.
8 * 8 *
9 */ 9 */
10 #include <v8.h> 10 #include <v8.h>
11 11
12 using namespace v8;
13
14 #include "Global.h" 12 #include "Global.h"
15 #include "JsContext.h" 13 #include "JsContext.h"
16 #include "Path2D.h" 14 #include "Path2D.h"
17 #include "SkCanvas.h" 15 #include "SkCanvas.h"
18 16
19 17
20 // Extracts a C string from a V8 Utf8Value. 18 // Extracts a C string from a V8 Utf8Value.
21 // TODO(jcgregrio) Currently dup'd in two files, fix. 19 // TODO(jcgregrio) Currently dup'd in two files, fix.
22 static const char* to_cstring(const v8::String::Utf8Value& value) { 20 static const char* to_cstring(const v8::String::Utf8Value& value) {
23 return *value ? *value : "<string conversion failed>"; 21 return *value ? *value : "<string conversion failed>";
24 } 22 }
25 23
26 Persistent<ObjectTemplate> JsContext::gContextTemplate; 24 v8::Persistent<v8::ObjectTemplate> JsContext::gContextTemplate;
27 25
28 // Wraps 'this' in a Javascript object. 26 // Wraps 'this' in a Javascript object.
29 Handle<Object> JsContext::wrap() { 27 v8::Handle<v8::Object> JsContext::wrap() {
30 // Handle scope for temporary handles. 28 // Handle scope for temporary handles.
31 EscapableHandleScope handleScope(fGlobal->getIsolate()); 29 v8::EscapableHandleScope handleScope(fGlobal->getIsolate());
32 30
33 // Fetch the template for creating JavaScript JsContext wrappers. 31 // Fetch the template for creating JavaScript JsContext wrappers.
34 // It only has to be created once, which we do on demand. 32 // It only has to be created once, which we do on demand.
35 if (gContextTemplate.IsEmpty()) { 33 if (gContextTemplate.IsEmpty()) {
36 Local<ObjectTemplate> localTemplate = ObjectTemplate::New(); 34 v8::Local<v8::ObjectTemplate> localTemplate = v8::ObjectTemplate::New();
37 35
38 // Add a field to store the pointer to a JsContext instance. 36 // Add a field to store the pointer to a JsContext instance.
39 localTemplate->SetInternalFieldCount(1); 37 localTemplate->SetInternalFieldCount(1);
40 38
41 this->addAttributesAndMethods(localTemplate); 39 this->addAttributesAndMethods(localTemplate);
42 40
43 gContextTemplate.Reset(fGlobal->getIsolate(), localTemplate); 41 gContextTemplate.Reset(fGlobal->getIsolate(), localTemplate);
44 } 42 }
45 Handle<ObjectTemplate> templ = 43 v8::Handle<v8::ObjectTemplate> templ =
46 Local<ObjectTemplate>::New(fGlobal->getIsolate(), gContextTemplate); 44 v8::Local<v8::ObjectTemplate>::New(fGlobal->getIsolate(), gContextTe mplate);
47 45
48 // Create an empty JsContext wrapper. 46 // Create an empty JsContext wrapper.
49 Local<Object> result = templ->NewInstance(); 47 v8::Local<v8::Object> result = templ->NewInstance();
50 48
51 // Wrap the raw C++ pointer in an External so it can be referenced 49 // Wrap the raw C++ pointer in an External so it can be referenced
52 // from within JavaScript. 50 // from within JavaScript.
53 Handle<External> contextPtr = External::New(fGlobal->getIsolate(), this); 51 v8::Handle<v8::External> contextPtr = v8::External::New(fGlobal->getIsolate( ), this);
54 52
55 // Store the context pointer in the JavaScript wrapper. 53 // Store the context pointer in the JavaScript wrapper.
56 result->SetInternalField(0, contextPtr); 54 result->SetInternalField(0, contextPtr);
57 55
58 // Return the result through the current handle scope. Since each 56 // Return the result through the current handle scope. Since each
59 // of these handles will go away when the handle scope is deleted 57 // of these handles will go away when the handle scope is deleted
60 // we need to call Close to let one, the result, escape into the 58 // we need to call Close to let one, the result, escape into the
61 // outer handle scope. 59 // outer handle scope.
62 return handleScope.Escape(result); 60 return handleScope.Escape(result);
63 } 61 }
64 62
65 void JsContext::onDraw(SkCanvas* canvas) { 63 void JsContext::onDraw(SkCanvas* canvas) {
66 // Record canvas and window in this. 64 // Record canvas and window in this.
67 fCanvas = canvas; 65 fCanvas = canvas;
68 66
69 // Create a handle scope to keep the temporary object references. 67 // Create a handle scope to keep the temporary object references.
70 HandleScope handleScope(fGlobal->getIsolate()); 68 v8::HandleScope handleScope(fGlobal->getIsolate());
71 69
72 // Create a local context from our global context. 70 // Create a local context from our global context.
73 Local<Context> context = fGlobal->getContext(); 71 v8::Local<v8::Context> context = fGlobal->getContext();
74 72
75 // Enter the context so all the remaining operations take place there. 73 // Enter the context so all the remaining operations take place there.
76 Context::Scope contextScope(context); 74 v8::Context::Scope contextScope(context);
77 75
78 // Wrap the C++ this pointer in a JavaScript wrapper. 76 // Wrap the C++ this pointer in a JavaScript wrapper.
79 Handle<Object> contextObj = this->wrap(); 77 v8::Handle<v8::Object> contextObj = this->wrap();
80 78
81 // Set up an exception handler before calling the Process function. 79 // Set up an exception handler before calling the Process function.
82 TryCatch tryCatch; 80 v8::TryCatch tryCatch;
83 81
84 // Invoke the process function, giving the global object as 'this' 82 // Invoke the process function, giving the global object as 'this'
85 // and one argument, this JsContext. 83 // and one argument, this JsContext.
86 const int argc = 1; 84 const int argc = 1;
87 Handle<Value> argv[argc] = { contextObj }; 85 v8::Handle<v8::Value> argv[argc] = { contextObj };
88 Local<Function> onDraw = 86 v8::Local<v8::Function> onDraw =
89 Local<Function>::New(fGlobal->getIsolate(), fOnDraw); 87 v8::Local<v8::Function>::New(fGlobal->getIsolate(), fOnDraw);
90 Handle<Value> result = onDraw->Call(context->Global(), argc, argv); 88 v8::Handle<v8::Value> result = onDraw->Call(context->Global(), argc, argv);
91 89
92 // Handle any exceptions or output. 90 // Handle any exceptions or output.
93 if (result.IsEmpty()) { 91 if (result.IsEmpty()) {
94 SkASSERT(tryCatch.HasCaught()); 92 SkASSERT(tryCatch.HasCaught());
95 // Print errors that happened during execution. 93 // Print errors that happened during execution.
96 fGlobal->reportException(&tryCatch); 94 fGlobal->reportException(&tryCatch);
97 } else { 95 } else {
98 SkASSERT(!tryCatch.HasCaught()); 96 SkASSERT(!tryCatch.HasCaught());
99 if (!result->IsUndefined()) { 97 if (!result->IsUndefined()) {
100 // If all went well and the result wasn't undefined then print 98 // If all went well and the result wasn't undefined then print
101 // the returned value. 99 // the returned value.
102 String::Utf8Value str(result); 100 v8::String::Utf8Value str(result);
103 const char* cstr = to_cstring(str); 101 const char* cstr = to_cstring(str);
104 printf("%s\n", cstr); 102 printf("%s\n", cstr);
105 } 103 }
106 } 104 }
107 } 105 }
108 106
109 // Fetch the onDraw function from the global context. 107 // Fetch the onDraw function from the global context.
110 bool JsContext::initialize() { 108 bool JsContext::initialize() {
111 109
112 // Create a stack-allocated handle scope. 110 // Create a stack-allocated handle scope.
113 HandleScope handleScope(fGlobal->getIsolate()); 111 v8::HandleScope handleScope(fGlobal->getIsolate());
114 112
115 // Create a local context from our global context. 113 // Create a local context from our global context.
116 Local<Context> context = fGlobal->getContext(); 114 v8::Local<v8::Context> context = fGlobal->getContext();
117 115
118 // Enter the scope so all operations take place in the scope. 116 // Enter the scope so all operations take place in the scope.
119 Context::Scope contextScope(context); 117 v8::Context::Scope contextScope(context);
120 118
121 v8::TryCatch try_catch; 119 v8::TryCatch try_catch;
122 120
123 Handle<String> fn_name = String::NewFromUtf8( 121 v8::Handle<v8::String> fn_name = v8::String::NewFromUtf8(
124 fGlobal->getIsolate(), "onDraw"); 122 fGlobal->getIsolate(), "onDraw");
125 Handle<Value> fn_val = context->Global()->Get(fn_name); 123 v8::Handle<v8::Value> fn_val = context->Global()->Get(fn_name);
126 124
127 if (!fn_val->IsFunction()) { 125 if (!fn_val->IsFunction()) {
128 printf("Not a function.\n"); 126 printf("Not a function.\n");
129 return false; 127 return false;
130 } 128 }
131 129
132 // It is a function; cast it to a Function. 130 // It is a function; cast it to a Function.
133 Handle<Function> fn_fun = Handle<Function>::Cast(fn_val); 131 v8::Handle<v8::Function> fn_fun = v8::Handle<v8::Function>::Cast(fn_val);
134 132
135 // Store the function in a Persistent handle, since we also want that to 133 // Store the function in a Persistent handle, since we also want that to
136 // remain after this call returns. 134 // remain after this call returns.
137 fOnDraw.Reset(fGlobal->getIsolate(), fn_fun); 135 fOnDraw.Reset(fGlobal->getIsolate(), fn_fun);
138 136
139 return true; 137 return true;
140 } 138 }
OLDNEW
« no previous file with comments | « experimental/SkV8Example/JsContext.h ('k') | experimental/SkV8Example/Path2D.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698