/
AJPoint.cpp
210 lines (177 loc) · 7.02 KB
/
AJPoint.cpp
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
207
208
209
210
/*
Copyright 2012 Aphid Mobile
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <config.h>
#include "AJPoint.h"
#include <runtime/AJFunction.h>
#include <runtime/NativeFunctionWrapper.h>
#include <runtime/PrototypeFunction.h>
#include <runtime/Lookup.h>
#include "AJOABinding.h"
#include "OAUtil.h"
namespace Aphid {
using namespace AJ;
#include "AJPoint_table.in.h"
///-------------------------------------------------------------------------------------------------------------------
AJPoint::AJPoint(NonNullPassRefPtr<Structure> structure, AJOAGlobalObject* globalObject, Point impl)
: Base(structure, globalObject)
, m_impl(impl)
{
AJ_LEAK_DETECT_INC("AJPoint");
}
AJPoint::~AJPoint()
{
//oa_debug("AJPoint destructed");
AJ_LEAK_DETECT_DEC("AJPoint");
}
///-------------------------------------------------------------------------------------------------------------------
AJObject* AJPointConstructor::constructPoint(ExecState* exec, AJObject* constructor, const ArgList& args)
{
//Point(float x, float y)
if (args.size() == 2) {
if (
ensureFloatArg(exec, args, 0, "x")
&& ensureFloatArg(exec, args, 1, "y")
) {
float x = args.at(0).toFloat(exec);
float y = args.at(1).toFloat(exec);
Point data = PointMake(x, y);
AJOAGlobalObject* globalObject = ajoa_cast<AJPointConstructor*>(constructor)->globalObject();
return createAJDataWrapper<AJPoint>(exec, globalObject, data);
}
}
exec->clearException();
//Point(Point p)
if (args.size() == 1) {
if (
ENSURE_OBJ_NULL_ARG(exec, args, 0, "p", Point)
) {
Point p = ajoa_cast<AJPoint*>(asObject(args.at(0)))->data();
AJOAGlobalObject* globalObject = ajoa_cast<AJPointConstructor*>(constructor)->globalObject();
return createAJDataWrapper<AJPoint>(exec, globalObject, p);
}
}
exec->clearException();
//Point()
if (args.size() == 0) {
AJOAGlobalObject* globalObject = ajoa_cast<AJPointConstructor*>(constructor)->globalObject();
return createAJDataWrapper<AJPoint>(exec, globalObject, PointZero);
}
if (!exec->hadException())
OA_ERROR_INVALID_ARGS(exec, "Point");
return 0;
}
ConstructType AJPointConstructor::getConstructData(ConstructData& constructData)
{
constructData.native.function = AJPointConstructor::constructPoint;
return ConstructTypeHost;
}
///-------------------------------------------------------------------------------------------------------------------
Point toPoint(AJValue value)
{
return value.inherits(&AJPoint::s_info) ? ajoa_cast<AJPoint*>(asObject(value))->data() : Point();
}
//TODO: set inline?
AJValue toAJ(ExecState* exec, AJOAGlobalObject* globalObject, Point impl)
{
return new (exec) AJPoint(getOAStructure(AJPointBindingType, exec, globalObject), globalObject, impl);
}
///-------------------------------------------------------------------------------------------------------------------
AJ::AJValue ajPointX(AJ::ExecState* exec, AJ::AJValue thisValue, const AJ::Identifier&)
{
AJPoint* thisObject = ajoa_cast<AJPoint*>(asObject(thisValue));
return jsNumber(exec, thisObject->data().x);
}
void setAJPointX(AJ::ExecState* exec, AJ::AJObject* thisObject, AJ::AJValue value)
{
if (value.isNumber()) {
AJPoint* ajPoint = ajoa_cast<AJPoint*>(thisObject);
Point point = ajPoint->data();
point.x = value.toFloat(exec);
ajPoint->setData(point);
} else
OA_ERROR_INVALID_PROPERTY(exec, "x", "float");
}
AJ::AJValue ajPointY(AJ::ExecState* exec, AJ::AJValue thisValue, const AJ::Identifier&)
{
AJPoint* thisObject = ajoa_cast<AJPoint*>(asObject(thisValue));
return jsNumber(exec, thisObject->data().y);
}
void setAJPointY(AJ::ExecState* exec, AJ::AJObject* thisObject, AJ::AJValue value)
{
if (value.isNumber()) {
AJPoint* ajPoint = ajoa_cast<AJPoint*>(thisObject);
Point point = ajPoint->data();
point.y = value.toFloat(exec);
ajPoint->setData(point);
} else {
OA_ERROR_INVALID_PROPERTY(exec, "y", "float");
}
}
AJ::AJValue JSC_HOST_CALL ajPointFunctionSetXY(AJ::ExecState* exec, AJ::AJObject*, AJ::AJValue thisValue, const AJ::ArgList& args)
{
CHECK_INHERITS(exec, thisValue, AJPoint);
if (args.size() >= 2) {
if (!args.at(0).isNumber())
OA_ERROR_INVALID_ARGUMENT(exec, "x", "float");
else if (!args.at(1).isNumber())
OA_ERROR_INVALID_ARGUMENT(exec, "y", "float");
else {
Point p = {args.at(0).toFloat(exec), args.at(1).toFloat(exec)};
ajoa_cast<AJPoint*>(asObject(thisValue))->setData(p);
}
} else
OA_ERROR_NOT_ENOUGH_ARGUMENTS(exec, 2, args.size());
return jsUndefined();
}
AJ::AJValue ajPointConstructor(AJ::ExecState* exec, AJ::AJValue thisValue, const AJ::Identifier&)
{
return getOAConstructor(AJPointBindingType, exec, ajoa_cast<AJPoint*>(asObject(thisValue))->globalObject());
}
///-------------------------------------------------------------------------------------------------------------------
AJ::AJValue JSC_HOST_CALL ajPointPrototypeFunctionEquals(AJ::ExecState* exec, AJ::AJObject*, AJ::AJValue thisValue, const AJ::ArgList& args)
{
CHECK_INHERITS(exec, thisValue, AJPoint);
if (args.size() >= 1) {
if (!args.at(0).inherits(&AJPoint::s_info))
OA_ERROR_INVALID_ARGUMENT(exec, "p", "Point");
else {
AJPoint* thisObject = ajoa_cast<AJPoint*>(asObject(thisValue));
AJPoint* other = ajoa_cast<AJPoint*>(asObject(args.at(0)));
return jsBoolean(PointEqual(thisObject->data(), other->data()));
}
} else
OA_ERROR_NOT_ENOUGH_ARGUMENTS(exec, 1, args.size());
OA_TODO();
return jsUndefined();
}
AJ::AJValue JSC_HOST_CALL ajPointPrototypeFunctionToString(AJ::ExecState* exec, AJ::AJObject*, AJ::AJValue thisValue, const AJ::ArgList& args)
{
UNUSED_PARAM(args);
CHECK_INHERITS(exec, thisValue, AJPoint);
AJPoint* thisObject = ajoa_cast<AJPoint*>(asObject(thisValue));
return jsString(exec, toUString(thisObject->data()));
}
AJ::AJValue JSC_HOST_CALL ajPointPrototypeFunctionCopy(AJ::ExecState* exec, AJ::AJObject*, AJ::AJValue thisValue, const AJ::ArgList& args)
{
UNUSED_PARAM(args);
CHECK_INHERITS(exec, thisValue, AJPoint);
AJPoint* thisObject = ajoa_cast<AJPoint*>(asObject(thisValue));
return toAJ(exec, thisObject->globalObject(), thisObject->data());
}
///-------------------------------------------------------------------------------------------------------------------
AJ::AJValue ajPointConstructorZero(AJ::ExecState* exec, AJ::AJValue thisValue, const AJ::Identifier&)
{
AJPointConstructor* jsPoint = ajoa_cast<AJPointConstructor*>(asObject(thisValue));
return toAJ(exec, jsPoint->globalObject(), PointZero);
}
}