Oolite
Loading...
Searching...
No Matches
OOJSShipGroup.m File Reference
Include dependency graph for OOJSShipGroup.m:

Go to the source code of this file.

Enumerations

enum  { kShipGroup_ships , kShipGroup_leader , kShipGroup_name , kShipGroup_count }

Functions

static JSValue ShipGroupGetProperty (OOJS_GETTER_ARGS)
static JSValue ShipGroupSetProperty (OOJS_SETTER_ARGS)
static JSValue ShipGroupConstruct (OOJS_NATIVE_ARGS)
static JSValue ShipGroupAddShip (OOJS_NATIVE_ARGS)
static JSValue ShipGroupRemoveShip (OOJS_NATIVE_ARGS)
static JSValue ShipGroupContainsShip (OOJS_NATIVE_ARGS)
void InitOOJSShipGroup (JSContext *context, JSValueConst global)

Variables

static JSClassID sShipGroupClassID
static JSClassDef sShipGroupClass
static const JSCFunctionListEntry sShipGroupProperties []
static const JSCFunctionListEntry sShipGroupMethods []

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kShipGroup_ships 
kShipGroup_leader 
kShipGroup_name 
kShipGroup_count 

Definition at line 48 of file OOJSShipGroup.m.

48 {
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};
@ kShipGroup_count
@ kShipGroup_leader
@ kShipGroup_ships
@ kShipGroup_name

Function Documentation

◆ InitOOJSShipGroup()

void InitOOJSShipGroup ( JSContext * context,
JSValueConst global )

Definition at line 74 of file OOJSShipGroup.m.

75{
77 OOJSInitClass(context, global, sShipGroupClassID, JS_UNDEFINED, ShipGroupConstruct, 0,
80}
static JSPropertySpec sShipGroupProperties[]
static JSBool ShipGroupConstruct(JSContext *context, uintN argc, jsval *vp)
static JSFunctionSpec sShipGroupMethods[]
static JSClass sShipGroupClass
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
static JSClassID sShipGroupClassID
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
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_COUNT(array)

References InitOOJSShipGroup(), OOJS_COUNT, OOJSBasicPrivateObjectConverter(), OOJSInitClass(), OOJSRegisterClass(), OOJSRegisterObjectConverter(), ShipGroupConstruct(), sShipGroupClass, sShipGroupClassID, sShipGroupMethods, and sShipGroupProperties.

Here is the call graph for this function:

◆ ShipGroupAddShip()

JSValue ShipGroupAddShip ( OOJS_NATIVE_ARGS )
static

Definition at line 230 of file OOJSShipGroup.m.

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}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_THIS
void OOJSReportWarningForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
#define OOJS_RETURN_BOOL(v)
id OOJSNativeObjectOfClassFromJSValue(JSContext *context, jsval value, Class requiredClass)
#define OOJS_ARGV
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
JSValue OOJSNativeFailure(JSContext *context)
ShipEntity * leader()
BOOL acceptAsEscort:(ShipEntity *other_ship)
void setGroup:(OOShipGroup *group)
OOShipGroup * group()

References ShipEntity::acceptAsEscort:, count, EXPECT_NOT, ShipEntity::group, OOShipGroup::leader, MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJS_RETURN_VOID, OOJS_THIS, OOJSNativeFailure(), OOJSNativeObjectOfClassFromJSValue(), OOJSReportBadArguments(), OOJSReportWarningForCaller(), and ShipEntity::setGroup:.

Here is the call graph for this function:

◆ ShipGroupConstruct()

JSValue ShipGroupConstruct ( OOJS_NATIVE_ARGS )
static

Definition at line 158 of file OOJSShipGroup.m.

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}
#define OOJS_RETURN_OBJECT(o)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_IS_CONSTRUCTING

References EXPECT_NOT, nil, OOJS_ARGV, OOJS_IS_CONSTRUCTING, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_OBJECT, OOJSNativeFailure(), OOJSNativeObjectOfClassFromJSValue(), OOJSReportBadArguments(), OOJSReportError(), and OOStringFromJSValue().

Here is the call graph for this function:

◆ ShipGroupContainsShip()

JSValue ShipGroupContainsShip ( OOJS_NATIVE_ARGS )
static

Definition at line 317 of file OOJSShipGroup.m.

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}

References EXPECT_NOT, MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJS_THIS, OOJSNativeFailure(), OOJSNativeObjectOfClassFromJSValue(), and OOJSReportBadArguments().

Here is the call graph for this function:

◆ ShipGroupGetProperty()

JSValue ShipGroupGetProperty ( OOJS_GETTER_ARGS )
static

Definition at line 82 of file OOJSShipGroup.m.

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}
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSString * name()
NSArray * memberArray()

References count, EXPECT_NOT, kShipGroup_count, kShipGroup_leader, kShipGroup_name, kShipGroup_ships, OOShipGroup::leader, OOShipGroup::memberArray, OOShipGroup::name, nil, OOJS_COUNT, OOJS_GETTER_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSNativeFailure(), OOJSReportBadPropertySelector(), OOJSValueFromNativeObject(), and sShipGroupProperties.

Here is the call graph for this function:

◆ ShipGroupRemoveShip()

JSValue ShipGroupRemoveShip ( OOJS_NATIVE_ARGS )
static

Definition at line 291 of file OOJSShipGroup.m.

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}

References EXPECT_NOT, MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJS_RETURN_VOID, OOJS_THIS, OOJSNativeFailure(), OOJSNativeObjectOfClassFromJSValue(), and OOJSReportBadArguments().

Here is the call graph for this function:

◆ ShipGroupSetProperty()

JSValue ShipGroupSetProperty ( OOJS_SETTER_ARGS )
static

Definition at line 122 of file OOJSShipGroup.m.

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}
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
void setName:(NSString *name)
void setLeader:(ShipEntity *leader)

References EXPECT_NOT, kShipGroup_leader, kShipGroup_name, nil, OOJS_COUNT, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_SETTER_ARGS, OOJSNativeFailure(), OOJSNativeObjectOfClassFromJSValue(), OOJSReportBadPropertySelector(), OOJSReportBadPropertyValue(), OOShipGroup::setLeader:, OOShipGroup::setName:, and sShipGroupProperties.

Here is the call graph for this function:

Variable Documentation

◆ sShipGroupClass

JSClassDef sShipGroupClass
static
Initial value:
= {
.class_name = "ShipGroup",
}
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)

Definition at line 43 of file OOJSShipGroup.m.

43 {
44 .class_name = "ShipGroup",
45 .finalizer = OOJSObjectWrapperFinalize,
46};

◆ sShipGroupClassID

JSClassID sShipGroupClassID
static

Definition at line 32 of file OOJSShipGroup.m.

Referenced by InitOOJSShipGroup().

◆ sShipGroupMethods

const JSCFunctionListEntry sShipGroupMethods[]
static
Initial value:
= {
JS_CFUNC_DEF("toString", 0, OOJSObjectWrapperToString),
JS_CFUNC_DEF("addShip", 1, ShipGroupAddShip),
JS_CFUNC_DEF("containsShip", 1, ShipGroupContainsShip),
JS_CFUNC_DEF("removeShip", 1, ShipGroupRemoveShip),
}
static JSBool ShipGroupAddShip(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupContainsShip(JSContext *context, uintN argc, jsval *vp)
static JSBool ShipGroupRemoveShip(JSContext *context, uintN argc, jsval *vp)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)

Definition at line 64 of file OOJSShipGroup.m.

64 {
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};

◆ sShipGroupProperties

const JSCFunctionListEntry sShipGroupProperties[]
static
Initial value:
= {
}
static JSBool ShipGroupGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool ShipGroupSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
#define OOJS_PROPERTY_RO(name, getter, magic)

Definition at line 56 of file OOJSShipGroup.m.