-
Notifications
You must be signed in to change notification settings - Fork 5
/
rect_js.cc
67 lines (54 loc) · 1.92 KB
/
rect_js.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2019 The NodeBinding Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "examples/rect_js.h"
#include "node_binding/constructor.h"
#include "node_binding/type_convertor.h"
using namespace node_binding;
Napi::FunctionReference RectJs::constructor_;
// static
void RectJs::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(
env, "Rect",
{
InstanceAccessor("topLeft", &RectJs::GetTopLeft, &RectJs::SetTopLeft),
InstanceAccessor("bottomRight", &RectJs::GetBottomRight,
&RectJs::SetBottomRight),
InstanceMethod("area", &RectJs::Area),
});
constructor_ = Napi::Persistent(func);
constructor_.SuppressDestruct();
exports.Set("Rect", func);
}
RectJs::RectJs(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<RectJs>(info) {
if (info.Length() == 0) {
rect_ = TypedConstruct(info, &Constructor<Rect>::Call<>);
} else if (info.Length() == 2) {
rect_ = TypedConstruct(
info, &Constructor<Rect>::Call<const Point&, const Point&>);
} else {
Napi::Env env = info.Env();
THROW_JS_WRONG_NUMBER_OF_ARGUMENTS(env);
}
}
void RectJs::SetTopLeft(const Napi::CallbackInfo& info, const Napi::Value& v) {
if (IsConvertible<Point>(v)) {
rect_.top_left = ToNativeValue<Point>(v);
}
}
void RectJs::SetBottomRight(const Napi::CallbackInfo& info,
const Napi::Value& v) {
if (IsConvertible<Point>(v)) {
rect_.bottom_right = ToNativeValue<Point>(v);
}
}
Napi::Value RectJs::GetTopLeft(const Napi::CallbackInfo& info) {
return ToJSValue(info, rect_.top_left);
}
Napi::Value RectJs::GetBottomRight(const Napi::CallbackInfo& info) {
return ToJSValue(info, rect_.bottom_right);
}
Napi::Value RectJs::Area(const Napi::CallbackInfo& info) {
return TypedCall(info, &Rect::Area, &rect_);
}