blob: 959068dc7d8dd8e851e7ff038412f2688447b2ae [file] [log] [blame]
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 *
6 * Use of this source code is governed by a BSD-style license that can be
7 * found in the LICENSE file.
8 *
9 */
10#include <v8.h>
11
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000012#include "Global.h"
13#include "JsContext.h"
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +000014#include "Path2D.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000015#include "SkCanvas.h"
16
17
18// Extracts a C string from a V8 Utf8Value.
19// TODO(jcgregrio) Currently dup'd in two files, fix.
20static const char* to_cstring(const v8::String::Utf8Value& value) {
21 return *value ? *value : "<string conversion failed>";
22}
23
jcgregorioe22f45f2014-10-24 12:49:17 -070024v8::Persistent<v8::ObjectTemplate> JsContext::gContextTemplate;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000025
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000026// Wraps 'this' in a Javascript object.
jcgregorioe22f45f2014-10-24 12:49:17 -070027v8::Handle<v8::Object> JsContext::wrap() {
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000028 // Handle scope for temporary handles.
jcgregorioe22f45f2014-10-24 12:49:17 -070029 v8::EscapableHandleScope handleScope(fGlobal->getIsolate());
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000030
31 // Fetch the template for creating JavaScript JsContext wrappers.
32 // It only has to be created once, which we do on demand.
33 if (gContextTemplate.IsEmpty()) {
jcgregorioe22f45f2014-10-24 12:49:17 -070034 v8::Local<v8::ObjectTemplate> localTemplate = v8::ObjectTemplate::New();
commit-bot@chromium.org24e04962014-03-04 20:44:32 +000035
36 // Add a field to store the pointer to a JsContext instance.
37 localTemplate->SetInternalFieldCount(1);
38
39 this->addAttributesAndMethods(localTemplate);
40
41 gContextTemplate.Reset(fGlobal->getIsolate(), localTemplate);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000042 }
jcgregorioe22f45f2014-10-24 12:49:17 -070043 v8::Handle<v8::ObjectTemplate> templ =
44 v8::Local<v8::ObjectTemplate>::New(fGlobal->getIsolate(), gContextTemplate);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000045
46 // Create an empty JsContext wrapper.
jcgregorioe22f45f2014-10-24 12:49:17 -070047 v8::Local<v8::Object> result = templ->NewInstance();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000048
49 // Wrap the raw C++ pointer in an External so it can be referenced
50 // from within JavaScript.
jcgregorioe22f45f2014-10-24 12:49:17 -070051 v8::Handle<v8::External> contextPtr = v8::External::New(fGlobal->getIsolate(), this);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000052
53 // Store the context pointer in the JavaScript wrapper.
54 result->SetInternalField(0, contextPtr);
55
56 // Return the result through the current handle scope. Since each
57 // of these handles will go away when the handle scope is deleted
58 // we need to call Close to let one, the result, escape into the
59 // outer handle scope.
60 return handleScope.Escape(result);
61}
62
63void JsContext::onDraw(SkCanvas* canvas) {
64 // Record canvas and window in this.
65 fCanvas = canvas;
66
67 // Create a handle scope to keep the temporary object references.
jcgregorioe22f45f2014-10-24 12:49:17 -070068 v8::HandleScope handleScope(fGlobal->getIsolate());
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000069
70 // Create a local context from our global context.
jcgregorioe22f45f2014-10-24 12:49:17 -070071 v8::Local<v8::Context> context = fGlobal->getContext();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000072
73 // Enter the context so all the remaining operations take place there.
jcgregorioe22f45f2014-10-24 12:49:17 -070074 v8::Context::Scope contextScope(context);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000075
76 // Wrap the C++ this pointer in a JavaScript wrapper.
jcgregorioe22f45f2014-10-24 12:49:17 -070077 v8::Handle<v8::Object> contextObj = this->wrap();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000078
79 // Set up an exception handler before calling the Process function.
jcgregorioe22f45f2014-10-24 12:49:17 -070080 v8::TryCatch tryCatch;
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000081
82 // Invoke the process function, giving the global object as 'this'
83 // and one argument, this JsContext.
84 const int argc = 1;
jcgregorioe22f45f2014-10-24 12:49:17 -070085 v8::Handle<v8::Value> argv[argc] = { contextObj };
86 v8::Local<v8::Function> onDraw =
87 v8::Local<v8::Function>::New(fGlobal->getIsolate(), fOnDraw);
88 v8::Handle<v8::Value> result = onDraw->Call(context->Global(), argc, argv);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000089
90 // Handle any exceptions or output.
91 if (result.IsEmpty()) {
92 SkASSERT(tryCatch.HasCaught());
93 // Print errors that happened during execution.
94 fGlobal->reportException(&tryCatch);
95 } else {
96 SkASSERT(!tryCatch.HasCaught());
97 if (!result->IsUndefined()) {
98 // If all went well and the result wasn't undefined then print
99 // the returned value.
jcgregorioe22f45f2014-10-24 12:49:17 -0700100 v8::String::Utf8Value str(result);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000101 const char* cstr = to_cstring(str);
102 printf("%s\n", cstr);
103 }
104 }
105}
106
107// Fetch the onDraw function from the global context.
108bool JsContext::initialize() {
109
110 // Create a stack-allocated handle scope.
jcgregorioe22f45f2014-10-24 12:49:17 -0700111 v8::HandleScope handleScope(fGlobal->getIsolate());
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000112
113 // Create a local context from our global context.
jcgregorioe22f45f2014-10-24 12:49:17 -0700114 v8::Local<v8::Context> context = fGlobal->getContext();
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000115
116 // Enter the scope so all operations take place in the scope.
jcgregorioe22f45f2014-10-24 12:49:17 -0700117 v8::Context::Scope contextScope(context);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000118
119 v8::TryCatch try_catch;
120
jcgregorioe22f45f2014-10-24 12:49:17 -0700121 v8::Handle<v8::String> fn_name = v8::String::NewFromUtf8(
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000122 fGlobal->getIsolate(), "onDraw");
jcgregorioe22f45f2014-10-24 12:49:17 -0700123 v8::Handle<v8::Value> fn_val = context->Global()->Get(fn_name);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000124
125 if (!fn_val->IsFunction()) {
126 printf("Not a function.\n");
127 return false;
128 }
129
130 // It is a function; cast it to a Function.
jcgregorioe22f45f2014-10-24 12:49:17 -0700131 v8::Handle<v8::Function> fn_fun = v8::Handle<v8::Function>::Cast(fn_val);
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000132
133 // Store the function in a Persistent handle, since we also want that to
134 // remain after this call returns.
135 fOnDraw.Reset(fGlobal->getIsolate(), fn_fun);
136
137 return true;
138}