Oolite
Loading...
Searching...
No Matches
OOJSShipGroup.m
Go to the documentation of this file.
1/*
2
3OOShipGroup.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSShipGroup.h"
29#import "OOShipGroup.h"
30#import "ShipEntity.h"
31
32static JSClassID sShipGroupClassID;
33
37
38// Methods
42
43static JSClassDef sShipGroupClass = {
44 .class_name = "ShipGroup",
45 .finalizer = OOJSObjectWrapperFinalize,
46};
47
48enum {
49 // Property IDs
50 kShipGroup_ships, // array of ships, double, read-only
51 kShipGroup_leader, // leader, Ship, read/write
52 kShipGroup_name, // name, string, read/write
53 kShipGroup_count, // number of ships, integer, read-only
54};
55
63
64static const JSCFunctionListEntry sShipGroupMethods[] = {
65 // JS name, min args, function
66 JS_CFUNC_DEF("toString", 0, OOJSObjectWrapperToString),
67 JS_CFUNC_DEF("addShip", 1, ShipGroupAddShip),
68 JS_CFUNC_DEF("containsShip", 1, ShipGroupContainsShip),
69 JS_CFUNC_DEF("removeShip", 1, ShipGroupRemoveShip),
70};
71
73
81
83{
84 OOJS_NATIVE_ENTER(context)
85
86 OOShipGroup* group = nil;
87 id result = nil;
88
89 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group)))
90 return OOJSNativeFailure(context);
91
92 switch (magic) {
94 result = [group memberArray];
95 if (result == nil)
96 result = [NSArray array];
97 break;
98
100 result = [group leader];
101 break;
102
103 case kShipGroup_name:
104 result = [group name];
105 if (result == nil)
106 result = [NSNull null];
107 break;
108
109 case kShipGroup_count:
110 return JS_NewNumber(context, [group count]);
111
112 default:
114 return OOJSNativeFailure(context);
115 }
116
117 return OOJSValueFromNativeObject(context, result);
118
120}
121
123{
124 OOJS_NATIVE_ENTER(context)
125
126 OOShipGroup* group = nil;
127 ShipEntity* shipValue = nil;
128
129 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group)))
130 return OOJSNativeFailure(context);
131
132 switch (magic) {
134 shipValue = OOJSNativeObjectOfClassFromJSValue(context, value, [ShipEntity class]);
135 if (shipValue != nil || JS_IsNull(value)) {
136 [group setLeader:shipValue];
137 return JS_UNDEFINED;
138 }
139 break;
140
141 case kShipGroup_name:
142 [group setName:OOStringFromJSValueEvenIfNull(context, value)];
143 return JS_UNDEFINED;
144 break;
145
146 default:
148 return OOJSNativeFailure(context);
149 }
150
152 return OOJSNativeFailure(context);
153
155}
156
157// new ShipGroup([name : String [, leader : Ship]]) : ShipGroup
159{
160 OOJS_NATIVE_ENTER(context)
161
162 /* QuickJS has no JS_IsConstructing(). When a constructor_or_func native is
163 called with new, this is new.target (a constructor); when it is
164 called as a function, this is the caller's this.
165 */
167 OOJSReportError(context, @"ShipGroup() cannot be called as a function, it must be used as a constructor (as in new ShipGroup(...)).");
168 return JS_EXCEPTION;
169 }
170
171 NSString* name = nil;
172 ShipEntity* leader = nil;
173
174 if (argc >= 1) {
175 if (!JS_IsString(OOJS_ARGV[0])) {
176 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV, @"Could not create ShipGroup", @"group name");
177 return OOJSNativeFailure(context);
178 }
179 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
180 }
181
182 if (argc >= 2) {
183 leader = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[1], [ShipEntity class]);
184 if (leader == nil && !JS_IsNull(OOJS_ARGV[1])) {
185 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV + 1, @"Could not create ShipGroup", @"ship");
186 return OOJSNativeFailure(context);
187 }
188 }
189
190 OOJS_RETURN_OBJECT([OOShipGroup groupWithName:name leader:leader]);
191
193}
194
195@implementation OOShipGroup (OOJavaScriptExtensions)
196
197/* As with SpiderMonkey, the JS object retains the group, and _jsSelf is a
198 weak (uncounted) reference to the JS object, cleared by -oo_clearJSSelf:
199 when the JS object is finalized.
200*/
201- (JSValue)oo_jsValueInContext:(JSContext*)context
202{
203 if (!JS_IsObject(_jsSelf)) {
204 JSValue jsSelf = JS_NewObjectClass(context, (int)sShipGroupClassID);
205 if (JS_IsException(jsSelf)) {
206 JS_FreeValue(context, JS_GetException(context));
207 return JS_NULL;
208 }
209 JS_SetOpaque(jsSelf, [self retain]);
210
211 // Weak: the caller gets the only reference.
212 _jsSelf = jsSelf;
213 return jsSelf;
214 }
215
216 return JS_DupValue(context, _jsSelf);
217}
218
219- (void)oo_clearJSSelf:(JSValueConst)selfVal
220{
221 if (JS_IsObject(_jsSelf) && JS_VALUE_GET_PTR(_jsSelf) == JS_VALUE_GET_PTR(selfVal))
222 _jsSelf = JS_UNDEFINED;
223}
224
225@end
226
227// *** Methods ***
228
229// addShip(ship : Ship)
231{
232 OOJS_NATIVE_ENTER(context)
233
234 OOShipGroup* thisGroup = nil;
235 ShipEntity* ship = nil;
236 BOOL OK = YES;
237
238 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
239 return OOJSNativeFailure(context);
240
241 if (argc > 0)
242 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
243 if (ship == nil) {
244 if (argc > 0 && JS_IsNull(OOJS_ARGV[0]))
245 OOJS_RETURN_VOID; // OK, do nothing for null ship.
246
247 OOJSReportBadArguments(context, @"ShipGroup", @"addShip", MIN(argc, 1), OOJS_ARGV, nil, @"ship");
248 return OOJSNativeFailure(context);
249 }
250
251 if ([thisGroup containsShip:ship]) {
252 // nothing to do...
254 } else {
255 ShipEntity* thisGroupLeader = [thisGroup leader];
256
257 if ([thisGroupLeader escortGroup] == thisGroup) // escort group!
258 {
259 if ([thisGroup count] > 1) // already with some escorts
260 {
261 OOShipGroup* thatGroup = [ship group];
262 if ([thatGroup count] > 1 && [[thatGroup leader] escortGroup] == thatGroup) // new escort already escorting!
263 {
264 OOJSReportWarningForCaller(context, @"ShipGroup", @"addShip", @"Ship %@ cannot be assigned to two escort groups, ignoring.", ship);
265 OK = NO;
266 } else {
267 OK = [thisGroupLeader acceptAsEscort:ship];
268 }
269 } else // [thisGroup count] == 1, default unescorted ship?
270 {
271 if ([thisGroupLeader escortGroup] == [thisGroupLeader group]) {
272 // Default unescorted, unescortable, ship. Create new group and use that instead.
273 [thisGroupLeader setGroup:[[OOShipGroup alloc] initWithName:@"ship group"]];
274 thisGroup = [thisGroupLeader group];
275 } else {
276 // Unescorted ship with custom group. See if it accepts escorts.
277 OK = [thisGroupLeader acceptAsEscort:ship];
278 }
279 }
280 }
281 if (OK) {
282 OOJS_RETURN_BOOL([thisGroup addShip:ship]); // if ship is there already, noop & YES
283 } else
285 }
286
288}
289
290// removeShip(ship : Ship)
292{
293 OOJS_NATIVE_ENTER(context)
294
295 OOShipGroup* thisGroup = nil;
296 ShipEntity* ship = nil;
297
298 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
299 return OOJSNativeFailure(context);
300
301 if (argc > 0)
302 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
303 if (ship == nil) {
304 if (argc > 0 && JS_IsNull(OOJS_ARGV[0]))
305 OOJS_RETURN_VOID; // OK, do nothing for null ship.
306
307 OOJSReportBadArguments(context, @"ShipGroup", @"removeShip", MIN(argc, 1), OOJS_ARGV, nil, @"ship");
308 return OOJSNativeFailure(context);
309 }
310
311 OOJS_RETURN_BOOL([thisGroup removeShip:ship]);
312
314}
315
316// containsShip(ship : Ship) : Boolean
318{
319 OOJS_NATIVE_ENTER(context)
320
321 OOShipGroup* thisGroup = nil;
322 ShipEntity* ship = nil;
323
324 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
325 return OOJSNativeFailure(context);
326
327 if (argc > 0)
328 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
329 if (ship == nil) {
330 if (argc > 0 && JS_IsNull(OOJS_ARGV[0]))
331 OOJS_RETURN_BOOL(NO); // OK, return false for null ship.
332
333 OOJSReportBadArguments(context, @"ShipGroup", @"containsShip", MIN(argc, 1), OOJS_ARGV, nil, @"ship");
334 return OOJSNativeFailure(context);
335 }
336
337 OOJS_RETURN_BOOL([thisGroup containsShip:ship]);
338
340}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
void InitOOJSShipGroup(JSContext *context, JSObject *global)
static JSPropertySpec sShipGroupProperties[]
static JSBool ShipGroupAddShip(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool ShipGroupContainsShip(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupConstruct(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSBool ShipGroupRemoveShip(JSContext *context, uintN argc, jsval *vp)
static JSFunctionSpec sShipGroupMethods[]
static JSClass sShipGroupClass
@ kShipGroup_count
@ kShipGroup_leader
@ kShipGroup_ships
@ kShipGroup_name
#define OOJS_THIS
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
void OOJSReportWarningForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
#define OOJS_RETURN_BOOL(v)
id OOJSNativeObjectOfClassFromJSValue(JSContext *context, jsval value, Class requiredClass)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define OOJS_RETURN_VOID
#define MIN(A, B)
Definition OOMaths.h:91
unsigned count
return nil
static JSClassID sShipGroupClassID
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
#define OOJS_IS_CONSTRUCTING
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_SETTER_ARGS
#define OOJS_PROPERTY_RO(name, getter, magic)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_GETTER_ARGS
#define OOJS_NATIVE_ARGS
#define OOJS_COUNT(array)
void setName:(NSString *name)
void setLeader:(ShipEntity *leader)
NSString * name()
ShipEntity * leader()
NSArray * memberArray()
BOOL acceptAsEscort:(ShipEntity *other_ship)
void setGroup:(OOShipGroup *group)
OOShipGroup * group()