-
Notifications
You must be signed in to change notification settings - Fork 5
/
point_js.h
59 lines (43 loc) · 1.55 KB
/
point_js.h
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
// 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.
#pragma once
#include <iostream>
#include "node_binding/type_convertor.h"
#include "point.h"
class PointJs : public Napi::ObjectWrap<PointJs> {
public:
static void Init(Napi::Env env, Napi::Object exports);
static Napi::Object New(Napi::Env env, const Point& p);
PointJs(const Napi::CallbackInfo& info);
void SetX(const Napi::CallbackInfo& info, const Napi::Value& v);
void SetY(const Napi::CallbackInfo& info, const Napi::Value& v);
Napi::Value GetX(const Napi::CallbackInfo& info);
Napi::Value GetY(const Napi::CallbackInfo& info);
private:
static Napi::FunctionReference constructor_;
Point point_;
};
namespace node_binding {
template <>
class TypeConvertor<Point> {
public:
static Point ToNativeValue(const Napi::Value& value) {
Napi::Object obj = value.As<Napi::Object>();
return {
TypeConvertor<int>::ToNativeValue(obj["x"]),
TypeConvertor<int>::ToNativeValue(obj["y"]),
};
}
static bool IsConvertible(const Napi::Value& value) {
if (!value.IsObject()) return false;
Napi::Object obj = value.As<Napi::Object>();
return TypeConvertor<int>::IsConvertible(obj["x"]) &&
TypeConvertor<int>::IsConvertible(obj["y"]);
}
static Napi::Value ToJSValue(const Napi::CallbackInfo& info,
const Point& value) {
return PointJs::New(info.Env(), value);
}
};
} // namespace node_binding