commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | * |
| 8 | */ |
| 9 | |
commit-bot@chromium.org | f679d1b | 2014-02-27 20:20:21 +0000 | [diff] [blame] | 10 | #include "Path2D.h" |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 11 | #include "Global.h" |
| 12 | |
commit-bot@chromium.org | f679d1b | 2014-02-27 20:20:21 +0000 | [diff] [blame] | 13 | Global* Path2D::gGlobal = NULL; |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 14 | v8::Persistent<v8::ObjectTemplate> Path2D::gPath2DTemplate; |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 15 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 16 | void weakPath2DCallback(const v8::WeakCallbackData<v8::Object, Path2D>& args) { |
| 17 | delete args.GetParameter(); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 18 | } |
| 19 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 20 | // Wraps an SkPath* in a Path2D object. |
| 21 | Path2D::Path2D(SkPath* path) : path_(path) { |
| 22 | // Handle scope for temporary handles. |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 23 | v8::HandleScope handleScope(gGlobal->getIsolate()); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 24 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 25 | // Just once create the ObjectTemplate for what Path2D looks like in JS. |
| 26 | if (gPath2DTemplate.IsEmpty()) { |
| 27 | v8::Local<v8::ObjectTemplate> localTemplate = v8::ObjectTemplate::New(); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 28 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 29 | // Add a field to store the pointer to a SkPath pointer. |
| 30 | localTemplate->SetInternalFieldCount(1); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 31 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 32 | gPath2DTemplate.Reset(gGlobal->getIsolate(), localTemplate); |
| 33 | } |
| 34 | v8::Handle<v8::ObjectTemplate> templ = |
| 35 | v8::Local<v8::ObjectTemplate>::New(gGlobal->getIsolate(), gPath2DTemplate); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 36 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 37 | // Create an empty Path2D wrapper. |
| 38 | v8::Local<v8::Object> result = templ->NewInstance(); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 39 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 40 | // Store the SkPath pointer in the JavaScript wrapper. |
| 41 | result->SetInternalField(0, v8::External::New(gGlobal->getIsolate(), this)); |
| 42 | gGlobal->getIsolate()->AdjustAmountOfExternalAllocatedMemory(sizeof(SkPath)); |
| 43 | |
| 44 | // Make a weak persistent and set up the callback so we can delete the path pointer. |
| 45 | // TODO(jcgregorio) Figure out why weakPath2DCallback never gets called and we leak. |
| 46 | v8::Persistent<v8::Object> weak(gGlobal->getIsolate(), result); |
| 47 | weak.SetWeak(this, weakPath2DCallback); |
| 48 | this->handle_.Reset(gGlobal->getIsolate(), weak); |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 51 | Path2D::~Path2D() { |
| 52 | delete path_; |
| 53 | handle_.Reset(); |
| 54 | gGlobal->getIsolate()->AdjustAmountOfExternalAllocatedMemory(-sizeof(SkPath)); |
commit-bot@chromium.org | e1df565 | 2014-02-13 16:00:58 +0000 | [diff] [blame] | 55 | } |