Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
OOJSShipGroup.m File Reference
import "OOShipGroup.h"
import "OOJavaScriptEngine.h"
import "Universe.h"
+ 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 JSBool ShipGroupGetProperty (JSContext *context, JSObject *this, jsid propID, jsval *value)
 
static JSBool ShipGroupSetProperty (JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
 
static JSBool ShipGroupConstruct (JSContext *context, uintN argc, jsval *vp)
 
static JSBool ShipGroupAddShip (JSContext *context, uintN argc, jsval *vp)
 
static JSBool ShipGroupRemoveShip (JSContext *context, uintN argc, jsval *vp)
 
static JSBool ShipGroupContainsShip (JSContext *context, uintN argc, jsval *vp)
 
 DEFINE_JS_OBJECT_GETTER (JSShipGroupGetShipGroup, &sShipGroupClass, sShipGroupPrototype, OOShipGroup)
 
void InitOOJSShipGroup (JSContext *context, JSObject *global)
 

Variables

static JSObject * sShipGroupPrototype
 
static JSClass sShipGroupClass
 
static JSPropertySpec sShipGroupProperties []
 
static JSFunctionSpec sShipGroupMethods []
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kShipGroup_ships 
kShipGroup_leader 
kShipGroup_name 
kShipGroup_count 

Definition at line 62 of file OOJSShipGroup.m.

63{
64 // Property IDs
65 kShipGroup_ships, // array of ships, double, read-only
66 kShipGroup_leader, // leader, Ship, read/write
67 kShipGroup_name, // name, string, read/write
68 kShipGroup_count, // number of ships, integer, read-only
69};
@ kShipGroup_count
@ kShipGroup_leader
@ kShipGroup_ships
@ kShipGroup_name

Function Documentation

◆ DEFINE_JS_OBJECT_GETTER()

DEFINE_JS_OBJECT_GETTER ( JSShipGroupGetShipGroup ,
& sShipGroupClass,
sShipGroupPrototype ,
OOShipGroup  )

◆ InitOOJSShipGroup()

void InitOOJSShipGroup ( JSContext * context,
JSObject * global )

Definition at line 97 of file OOJSShipGroup.m.

98{
99 sShipGroupPrototype = JS_InitClass(context, global, NULL, &sShipGroupClass, ShipGroupConstruct, 0, sShipGroupProperties, sShipGroupMethods, NULL, NULL);
101}
static JSPropertySpec sShipGroupProperties[]
static JSObject * sShipGroupPrototype
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)

References OOJSBasicPrivateObjectConverter(), OOJSRegisterObjectConverter(), ShipGroupConstruct(), sShipGroupClass, sShipGroupMethods, sShipGroupProperties, and sShipGroupPrototype.

+ Here is the call graph for this function:

◆ ShipGroupAddShip()

static JSBool ShipGroupAddShip ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 258 of file OOJSShipGroup.m.

259{
260 OOJS_NATIVE_ENTER(context)
261
262 OOShipGroup *thisGroup = nil;
263 ShipEntity *ship = nil;
264 BOOL OK = YES;
265
266 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup))) return NO;
267
268 if (argc > 0) ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
269 if (ship == nil)
270 {
271 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0])) OOJS_RETURN_VOID; // OK, do nothing for null ship.
272
273 OOJSReportBadArguments(context, @"ShipGroup", @"addShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
274 return NO;
275 }
276
277 if ([thisGroup containsShip:ship])
278 {
279 // nothing to do...
281 }
282 else
283 {
284 ShipEntity *thisGroupLeader = [thisGroup leader];
285
286 if ([thisGroupLeader escortGroup] == thisGroup) // escort group!
287 {
288 if ([thisGroup count] > 1) // already with some escorts
289 {
290 OOShipGroup *thatGroup = [ship group];
291 if ([thatGroup count] > 1 && [[thatGroup leader] escortGroup] == thatGroup) // new escort already escorting!
292 {
293 OOJSReportWarningForCaller(context, @"ShipGroup", @"addShip", @"Ship %@ cannot be assigned to two escort groups, ignoring.", ship);
294 OK = NO;
295 }
296 else
297 {
298 OK = [thisGroupLeader acceptAsEscort:ship];
299 }
300 }
301 else // [thisGroup count] == 1, default unescorted ship?
302 {
303 if ([thisGroupLeader escortGroup] == [thisGroupLeader group])
304 {
305 // Default unescorted, unescortable, ship. Create new group and use that instead.
306 [thisGroupLeader setGroup:[[OOShipGroup alloc] initWithName:@"ship group"]];
307 thisGroup = [thisGroupLeader group];
308 }
309 else
310 {
311 // Unescorted ship with custom group. See if it accepts escorts.
312 OK = [thisGroupLeader acceptAsEscort:ship];
313 }
314 }
315 }
316 if (OK)
317 {
318 OOJS_RETURN_BOOL([thisGroup addShip:ship]); // if ship is there already, noop & YES
319 }
320 else OOJS_RETURN_BOOL(NO);
321 }
322
324}
#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:111
unsigned count
return nil
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_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJS_RETURN_VOID, OOJS_THIS, OOJSNativeObjectOfClassFromJSValue(), OOJSReportBadArguments(), OOJSReportWarningForCaller(), and ShipEntity::setGroup:.

+ Here is the call graph for this function:

◆ ShipGroupConstruct()

static JSBool ShipGroupConstruct ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 186 of file OOJSShipGroup.m.

187{
188 OOJS_NATIVE_ENTER(context)
189
190 if (EXPECT_NOT(!JS_IsConstructing(context, vp)))
191 {
192 OOJSReportError(context, @"ShipGroup() cannot be called as a function, it must be used as a constructor (as in new ShipGroup(...)).");
193 return NO;
194 }
195
196 NSString *name = nil;
197 ShipEntity *leader = nil;
198
199 if (argc >= 1)
200 {
201 if (!JSVAL_IS_STRING(OOJS_ARGV[0]))
202 {
203 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV, @"Could not create ShipGroup", @"group name");
204 return NO;
205 }
206 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
207 }
208
209 if (argc >= 2)
210 {
211 leader = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[1], [ShipEntity class]);
212 if (leader == nil && !JSVAL_IS_NULL(OOJS_ARGV[1]))
213 {
214 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV + 1, @"Could not create ShipGroup", @"ship");
215 return NO;
216 }
217 }
218
219 OOJS_RETURN_OBJECT([OOShipGroup groupWithName:name leader:leader]);
220
222}
#define OOJS_RETURN_OBJECT(o)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
void OOJSReportError(JSContext *context, NSString *format,...)

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

Referenced by InitOOJSShipGroup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ShipGroupContainsShip()

static JSBool ShipGroupContainsShip ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 353 of file OOJSShipGroup.m.

354{
355 OOJS_NATIVE_ENTER(context)
356
357 OOShipGroup *thisGroup = nil;
358 ShipEntity *ship = nil;
359
360 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup))) return NO;
361
362 if (argc > 0) ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
363 if (ship == nil)
364 {
365 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0])) OOJS_RETURN_BOOL(NO); // OK, return false for null ship.
366
367 OOJSReportBadArguments(context, @"ShipGroup", @"containsShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
368 return NO;
369 }
370
371 OOJS_RETURN_BOOL([thisGroup containsShip:ship]);
372
374}

◆ ShipGroupGetProperty()

static JSBool ShipGroupGetProperty ( JSContext * context,
JSObject * this,
jsid propID,
jsval * value )
static

Definition at line 104 of file OOJSShipGroup.m.

105{
106 if (!JSID_IS_INT(propID)) return YES;
107
108 OOJS_NATIVE_ENTER(context)
109
110 OOShipGroup *group = nil;
111 id result = nil;
112
113 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group))) return NO;
114
115 switch (JSID_TO_INT(propID))
116 {
117 case kShipGroup_ships:
118 result = [group memberArray];
119 if (result == nil) result = [NSArray array];
120 break;
121
123 result = [group leader];
124 break;
125
126 case kShipGroup_name:
127 result = [group name];
128 if (result == nil) result = [NSNull null];
129 break;
130
131 case kShipGroup_count:
132 return JS_NewNumberValue(context, [group count], value);
133
134 default:
136 return NO;
137 }
138
139 *value = OOJSValueFromNativeObject(context, result);
140 return YES;
141
143}
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_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportBadPropertySelector(), OOJSValueFromNativeObject(), and sShipGroupProperties.

+ Here is the call graph for this function:

◆ ShipGroupRemoveShip()

static JSBool ShipGroupRemoveShip ( JSContext * context,
uintN argc,
jsval * vp )
static

Definition at line 328 of file OOJSShipGroup.m.

329{
330 OOJS_NATIVE_ENTER(context)
331
332 OOShipGroup *thisGroup = nil;
333 ShipEntity *ship = nil;
334
335 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup))) return NO;
336
337 if (argc > 0) ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
338 if (ship == nil)
339 {
340 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0])) OOJS_RETURN_VOID; // OK, do nothing for null ship.
341
342 OOJSReportBadArguments(context, @"ShipGroup", @"removeShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
343 return NO;
344 }
345
346 OOJS_RETURN_BOOL([thisGroup removeShip:ship]);
347
349}

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

+ Here is the call graph for this function:

◆ ShipGroupSetProperty()

static JSBool ShipGroupSetProperty ( JSContext * context,
JSObject * this,
jsid propID,
JSBool strict,
jsval * value )
static

Definition at line 146 of file OOJSShipGroup.m.

147{
148 if (!JSID_IS_INT(propID)) return YES;
149
150 OOJS_NATIVE_ENTER(context)
151
152 OOShipGroup *group = nil;
153 ShipEntity *shipValue = nil;
154
155 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group))) return NO;
156
157 switch (JSID_TO_INT(propID))
158 {
160 shipValue = OOJSNativeObjectOfClassFromJSValue(context, *value, [ShipEntity class]);
161 if (shipValue != nil || JSVAL_IS_NULL(*value))
162 {
163 [group setLeader:shipValue];
164 return YES;
165 }
166 break;
167
168 case kShipGroup_name:
169 [group setName:OOStringFromJSValueEvenIfNull(context, *value)];
170 return YES;
171 break;
172
173 default:
175 return NO;
176 }
177
178 OOJSReportBadPropertyValue(context, this, propID, sShipGroupProperties, *value);
179 return NO;
180
182}
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_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSNativeObjectOfClassFromJSValue(), OOJSReportBadPropertySelector(), OOJSReportBadPropertyValue(), OOShipGroup::setLeader:, OOShipGroup::setName:, and sShipGroupProperties.

+ Here is the call graph for this function:

Variable Documentation

◆ sShipGroupClass

JSClass sShipGroupClass
static
Initial value:
=
{
"ShipGroup",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
JSCLASS_NO_OPTIONAL_MEMBERS
}
static JSBool ShipGroupGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool ShipGroupSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)

Definition at line 45 of file OOJSShipGroup.m.

46{
47 "ShipGroup",
48 JSCLASS_HAS_PRIVATE,
49
50 JS_PropertyStub, // addProperty
51 JS_PropertyStub, // delProperty
52 ShipGroupGetProperty, // getProperty
53 ShipGroupSetProperty, // setProperty
54 JS_EnumerateStub, // enumerate
55 JS_ResolveStub, // resolve
56 JS_ConvertStub, // convert
58 JSCLASS_NO_OPTIONAL_MEMBERS
59};

Referenced by InitOOJSShipGroup().

◆ sShipGroupMethods

JSFunctionSpec sShipGroupMethods[]
static
Initial value:
=
{
{ "toString", OOJSObjectWrapperToString, 0 },
{ "addShip", ShipGroupAddShip, 1 },
{ "containsShip", ShipGroupContainsShip, 1 },
{ "removeShip", ShipGroupRemoveShip, 1 },
{ 0 }
}
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 83 of file OOJSShipGroup.m.

84{
85 // JS name Function min args
86 { "toString", OOJSObjectWrapperToString, 0 },
87 { "addShip", ShipGroupAddShip, 1 },
88 { "containsShip", ShipGroupContainsShip, 1 },
89 { "removeShip", ShipGroupRemoveShip, 1 },
90 { 0 }
91};

Referenced by InitOOJSShipGroup().

◆ sShipGroupProperties

JSPropertySpec sShipGroupProperties[]
static
Initial value:
=
{
{ 0 }
}
#define OOJS_PROP_READWRITE_CB
#define OOJS_PROP_READONLY_CB

Definition at line 72 of file OOJSShipGroup.m.

73{
74 // JS name ID flags
79 { 0 }
80};

Referenced by InitOOJSShipGroup(), ShipGroupGetProperty(), and ShipGroupSetProperty().

◆ sShipGroupPrototype

JSObject* sShipGroupPrototype
static

Definition at line 32 of file OOJSShipGroup.m.

Referenced by InitOOJSShipGroup().