forked from dmorissette/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoint.cpp
More file actions
206 lines (171 loc) · 5.54 KB
/
Copy pathpoint.cpp
File metadata and controls
206 lines (171 loc) · 5.54 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**********************************************************************
*
* Project: MapServer
* Purpose: V8 JavaScript Engine Support
* Author: Alan Boudreault (aboudreault@mapgears.com)
*
**********************************************************************
* Copyright (c) 2013, Alan Boudreault, MapGears
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**********************************************************************/
#include "mapserver-config.h"
#ifdef USE_V8_MAPSCRIPT
#include "v8_mapscript.h"
using namespace v8;
Persistent<FunctionTemplate> Point::constructor;
void Point::Initialize(Handle<Object> target)
{
HandleScope scope;
Handle<FunctionTemplate> c = FunctionTemplate::New(Point::New);
c->InstanceTemplate()->SetInternalFieldCount(1);
c->SetClassName(String::NewSymbol("pointObj"));
SET_ATTRIBUTE(c, "x", getProp, setProp);
SET_ATTRIBUTE(c, "y", getProp, setProp);
#ifdef USE_POINT_Z_M
SET_ATTRIBUTE(c, "z", getProp, setProp);
SET_ATTRIBUTE(c, "m", getProp, setProp);
#endif
NODE_SET_PROTOTYPE_METHOD(c, "setXY", setXY);
NODE_SET_PROTOTYPE_METHOD(c, "setXYZ", setXYZ);
target->Set(String::NewSymbol("pointObj"), c->GetFunction());
constructor.Reset(Isolate::GetCurrent(), c);
}
void Point::Dispose()
{
Point::constructor.Dispose();
Point::constructor.Clear();
}
Point::~Point()
{
if (this->freeInternal) {
msFree(this->get());
}
}
Handle<Function> Point::Constructor()
{
return (*Point::constructor)->GetFunction();
}
void Point::New(const v8::FunctionCallbackInfo<Value>& args)
{
HandleScope scope;
if (args[0]->IsExternal())
{
Local<External> ext = Local<External>::Cast(args[0]);
void *ptr = ext->Value();
Point *point = static_cast<Point*>(ptr);
Handle<Object> self = args.Holder();
point->Wrap(self);
if (point->parent_) {
self->SetHiddenValue(String::New("__parent__"), point->parent_->handle());
point->disableMemoryHandler();
}
}
else
{
pointObj *p = (pointObj *)msSmallMalloc(sizeof(pointObj));
p->x = 0;
p->y = 0;
#ifdef USE_POINT_Z_M
p->z = 0;
p->m = 0;
#endif
Point *point = new Point(p);
point->Wrap(args.Holder());
}
}
Point::Point(pointObj *p, ObjectWrap *pa):
ObjectWrap()
{
this->this_ = p;
this->parent_ = pa;
this->freeInternal = true;
}
void Point::getProp(Local<String> property,
const PropertyCallbackInfo<Value>& info)
{
HandleScope scope;
Point* p = ObjectWrap::Unwrap<Point>(info.Holder());
std::string name = TOSTR(property);
Handle<Value> value = Undefined();
if (name == "x")
value = Number::New(p->get()->x);
else if (name == "y")
value = Number::New(p->get()->y);
#ifdef USE_POINT_Z_M
else if (name == "z")
value = Number::New(p->get()->z);
else if (name == "m")
value = Number::New(p->get()->m);
#endif
info.GetReturnValue().Set(value);
}
void Point::setProp(Local<String> property,
Local<Value> value,
const PropertyCallbackInfo<void>& info)
{
HandleScope scope;
Point* p = ObjectWrap::Unwrap<Point>(info.Holder());
std::string name = TOSTR(property);
if (!value->IsNumber())
ThrowException(Exception::TypeError(
String::New("point value must be a number")));
if (name == "x") {
p->get()->x = value->NumberValue();
} else if (name == "y") {
p->get()->y = value->NumberValue();
}
#ifdef USE_POINT_Z_M
else if (name == "z") {
p->get()->z = value->NumberValue();
} else if (name == "m") {
p->get()->m = value->NumberValue();
}
#endif
}
void Point::setXY(const v8::FunctionCallbackInfo<v8::Value>& args)
{
HandleScope scope;
if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(String::New("Invalid argument"));
return;
}
Point* p = ObjectWrap::Unwrap<Point>(args.Holder());
p->get()->x = args[0]->NumberValue();
p->get()->y = args[1]->NumberValue();
}
void Point::setXYZ(const v8::FunctionCallbackInfo<v8::Value>& args)
{
HandleScope scope;
if (args.Length() < 3 ||
!args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber()) {
ThrowException(String::New("Invalid argument"));
return;
}
Point* p = ObjectWrap::Unwrap<Point>(args.Holder());
p->get()->x = args[0]->NumberValue();
p->get()->y = args[1]->NumberValue();
#ifdef USE_POINT_Z_M
p->get()->z = args[2]->NumberValue();
if (args.Length() > 3 && args[3]->IsNumber()) {
p->get()->m = args[3]->NumberValue();
}
#endif
}
#endif