|
4 | 4 |
|
5 | 5 | Napi::FunctionReference CairoSurface::constructor;
|
6 | 6 |
|
7 |
| -void CairoSurface::Initialize(Napi::Object target) { |
8 |
| - Napi::HandleScope scope(env); |
9 |
| - |
10 |
| - Napi::FunctionReference lcons = Napi::Function::New(env, CairoSurface::New); |
11 |
| - |
12 |
| - lcons->SetClassName(Napi::String::New(env, "CairoSurface")); |
13 |
| - InstanceMethod("width", &width), |
14 |
| - InstanceMethod("height", &height), |
15 |
| - InstanceMethod("getData", &getData), |
16 |
| - (target).Set(Napi::String::New(env, "CairoSurface"), Napi::GetFunction(lcons)); |
17 |
| - constructor.Reset(lcons); |
18 |
| -} |
19 |
| - |
20 |
| -CairoSurface::CairoSurface(std::string const& format, unsigned int width, unsigned int height) : Napi::ObjectWrap<CairoSurface>(), |
21 |
| - ss_(), |
22 |
| - width_(width), |
23 |
| - height_(height), |
24 |
| - format_(format) |
25 |
| -{ |
26 |
| -} |
27 |
| - |
28 |
| -CairoSurface::~CairoSurface() |
| 7 | +Napi::Object CairoSurface::Initialize(Napi::Env env, Napi::Object exports) |
29 | 8 | {
|
| 9 | + Napi::Function func = DefineClass(env, "CairoSurface", { |
| 10 | + InstanceMethod<&CairoSurface::width>("width"), |
| 11 | + InstanceMethod<&CairoSurface::height>("height"), |
| 12 | + InstanceMethod<&CairoSurface::getData>("getData") |
| 13 | + }); |
| 14 | + constructor = Napi::Persistent(func); |
| 15 | + constructor.SuppressDestruct(); |
| 16 | + exports.Set("CairoSurface", func); |
| 17 | + return exports; |
30 | 18 | }
|
31 | 19 |
|
32 |
| -Napi::Value CairoSurface::New(Napi::CallbackInfo const& info) |
| 20 | +// ctor |
| 21 | +CairoSurface::CairoSurface(Napi::CallbackInfo const& info) |
| 22 | + : Napi::ObjectWrap<CairoSurface>(info) |
33 | 23 | {
|
34 |
| - if (!info.IsConstructCall()) |
35 |
| - { |
36 |
| - Napi::Error::New(env, "Cannot call constructor as function, you need to use 'new' keyword").ThrowAsJavaScriptException(); |
37 |
| - return env.Null(); |
38 |
| - } |
39 |
| - |
40 |
| - if (info[0].IsExternal()) |
41 |
| - { |
42 |
| - // Currently there is no C++ that executes this call |
43 |
| - /* LCOV_EXCL_START */ |
44 |
| - Napi::External ext = info[0].As<Napi::External>(); |
45 |
| - void* ptr = ext->Value(); |
46 |
| - CairoSurface* im = static_cast<CairoSurface*>(ptr); |
47 |
| - im->Wrap(info.This()); |
48 |
| - return info.This(); |
49 |
| - return; |
50 |
| - /* LCOV_EXCL_STOP */ |
51 |
| - } |
52 |
| - |
| 24 | + Napi::Env env = info.Env(); |
53 | 25 | if (info.Length() == 3)
|
54 | 26 | {
|
55 | 27 | if (!info[0].IsString())
|
56 | 28 | {
|
57 | 29 | Napi::TypeError::New(env, "CairoSurface 'format' must be a string").ThrowAsJavaScriptException();
|
58 |
| - return env.Null(); |
| 30 | + return; |
59 | 31 | }
|
60 |
| - std::string format = TOSTR(info[0]); |
| 32 | + format_ = info[0].As<Napi::String>(); |
61 | 33 | if (!info[1].IsNumber() || !info[2].IsNumber())
|
62 | 34 | {
|
63 | 35 | Napi::TypeError::New(env, "CairoSurface 'width' and 'height' must be a integers").ThrowAsJavaScriptException();
|
64 |
| - return env.Null(); |
| 36 | + return; |
65 | 37 | }
|
66 |
| - CairoSurface* im = new CairoSurface(format, info[1].As<Napi::Number>().Int32Value(), info[2].As<Napi::Number>().Int32Value()); |
67 |
| - im->Wrap(info.This()); |
68 |
| - return info.This(); |
69 |
| - return; |
| 38 | + width_ = info[1].As<Napi::Number>().Int32Value(); |
| 39 | + height_ = info[2].As<Napi::Number>().Int32Value(); |
70 | 40 | }
|
71 | 41 | else
|
72 | 42 | {
|
73 | 43 | Napi::Error::New(env, "CairoSurface requires three arguments: format, width, and height").ThrowAsJavaScriptException();
|
74 |
| - return env.Null(); |
75 | 44 | }
|
76 |
| - return; |
77 | 45 | }
|
78 | 46 |
|
79 | 47 | Napi::Value CairoSurface::width(Napi::CallbackInfo const& info)
|
80 | 48 | {
|
81 |
| - CairoSurface* im = info.Holder().Unwrap<CairoSurface>(); |
82 |
| - return Napi::New(env, im->width()); |
| 49 | + Napi::Env env = info.Env(); |
| 50 | + return Napi::Number::New(env, width_); |
83 | 51 | }
|
84 | 52 |
|
85 | 53 | Napi::Value CairoSurface::height(Napi::CallbackInfo const& info)
|
86 | 54 | {
|
87 |
| - CairoSurface* im = info.Holder().Unwrap<CairoSurface>(); |
88 |
| - return Napi::New(env, im->height()); |
| 55 | + Napi::Env env = info.Env(); |
| 56 | + return Napi::Number::New(env, height_); |
89 | 57 | }
|
90 | 58 |
|
91 | 59 | Napi::Value CairoSurface::getData(Napi::CallbackInfo const& info)
|
92 | 60 | {
|
93 |
| - CairoSurface* surface = info.Holder().Unwrap<CairoSurface>(); |
94 |
| - std::string s = surface->ss_.str(); |
95 |
| - return Napi::Buffer::Copy(env, (char*)s.data(), s.size()); |
| 61 | + Napi::Env env = info.Env(); |
| 62 | + Napi::EscapableHandleScope scope(env); |
| 63 | + std::string str = stream_.str(); |
| 64 | + //return scope.Escape(Napi::Buffer<char>::Copy(env, const_cast<char*>(str.data()), str.size())); |
| 65 | + return scope.Escape(Napi::String::New(env, str)); |
96 | 66 | }
|
0 commit comments