Oolite
Loading...
Searching...
No Matches
OOJSShipGroup.m File Reference
import "OOJavaScriptEngine.h"
import "OOShipGroup.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 56 of file OOJSShipGroup.m.

56 {
57 // Property IDs
58 kShipGroup_ships, // array of ships, double, read-only
59 kShipGroup_leader, // leader, Ship, read/write
60 kShipGroup_name, // name, string, read/write
61 kShipGroup_count, // number of ships, integer, read-only
62};
@ 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 84 of file OOJSShipGroup.m.

85{
86 sShipGroupPrototype = JS_InitClass(context, global, NULL, &sShipGroupClass, ShipGroupConstruct, 0, sShipGroupProperties, sShipGroupMethods, NULL, NULL);
88}
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.

Referenced by OOJavaScriptEngine(Private)::createMainThreadContext, and InitOOJSShipGroup().

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

◆ ShipGroupAddShip()

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

Definition at line 237 of file OOJSShipGroup.m.

238{
239 OOJS_NATIVE_ENTER(context)
240
241 OOShipGroup* thisGroup = nil;
242 ShipEntity* ship = nil;
243 BOOL OK = YES;
244
245 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
246 return NO;
247
248 if (argc > 0)
249 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
250 if (ship == nil) {
251 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0]))
252 OOJS_RETURN_VOID; // OK, do nothing for null ship.
253
254 OOJSReportBadArguments(context, @"ShipGroup", @"addShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
255 return NO;
256 }
257
258 if ([thisGroup containsShip:ship]) {
259 // nothing to do...
261 } else {
262 ShipEntity* thisGroupLeader = [thisGroup leader];
263
264 if ([thisGroupLeader escortGroup] == thisGroup) // escort group!
265 {
266 if ([thisGroup count] > 1) // already with some escorts
267 {
268 OOShipGroup* thatGroup = [ship group];
269 if ([thatGroup count] > 1 && [[thatGroup leader] escortGroup] == thatGroup) // new escort already escorting!
270 {
271 OOJSReportWarningForCaller(context, @"ShipGroup", @"addShip", @"Ship %@ cannot be assigned to two escort groups, ignoring.", ship);
272 OK = NO;
273 } else {
274 OK = [thisGroupLeader acceptAsEscort:ship];
275 }
276 } else // [thisGroup count] == 1, default unescorted ship?
277 {
278 if ([thisGroupLeader escortGroup] == [thisGroupLeader group]) {
279 // Default unescorted, unescortable, ship. Create new group and use that instead.
280 [thisGroupLeader setGroup:[[OOShipGroup alloc] initWithName:@"ship group"]];
281 thisGroup = [thisGroupLeader group];
282 } else {
283 // Unescorted ship with custom group. See if it accepts escorts.
284 OK = [thisGroupLeader acceptAsEscort:ship];
285 }
286 }
287 }
288 if (OK) {
289 OOJS_RETURN_BOOL([thisGroup addShip:ship]); // if ship is there already, noop & YES
290 } else
292 }
293
295}
#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
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()

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

Definition at line 173 of file OOJSShipGroup.m.

174{
175 OOJS_NATIVE_ENTER(context)
176
177 if (EXPECT_NOT(!JS_IsConstructing(context, vp))) {
178 OOJSReportError(context, @"ShipGroup() cannot be called as a function, it must be used as a constructor (as in new ShipGroup(...)).");
179 return NO;
180 }
181
182 NSString* name = nil;
183 ShipEntity* leader = nil;
184
185 if (argc >= 1) {
186 if (!JSVAL_IS_STRING(OOJS_ARGV[0])) {
187 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV, @"Could not create ShipGroup", @"group name");
188 return NO;
189 }
190 name = OOStringFromJSValue(context, OOJS_ARGV[0]);
191 }
192
193 if (argc >= 2) {
194 leader = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[1], [ShipEntity class]);
195 if (leader == nil && !JSVAL_IS_NULL(OOJS_ARGV[1])) {
196 OOJSReportBadArguments(context, nil, @"ShipGroup()", 1, OOJS_ARGV + 1, @"Could not create ShipGroup", @"ship");
197 return NO;
198 }
199 }
200
201 OOJS_RETURN_OBJECT([OOShipGroup groupWithName:name leader:leader]);
202
204}
#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(), and InitOOJSShipGroup().

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

◆ ShipGroupContainsShip()

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

Definition at line 324 of file OOJSShipGroup.m.

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

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

Here is the call graph for this function:

◆ ShipGroupGetProperty()

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

Definition at line 90 of file OOJSShipGroup.m.

91{
92 if (!JSID_IS_INT(propID))
93 return YES;
94
95 OOJS_NATIVE_ENTER(context)
96
97 OOShipGroup* group = nil;
98 id result = nil;
99
100 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group)))
101 return NO;
102
103 switch (JSID_TO_INT(propID)) {
104 case kShipGroup_ships:
105 result = [group memberArray];
106 if (result == nil)
107 result = [NSArray array];
108 break;
109
111 result = [group leader];
112 break;
113
114 case kShipGroup_name:
115 result = [group name];
116 if (result == nil)
117 result = [NSNull null];
118 break;
119
120 case kShipGroup_count:
121 return JS_NewNumberValue(context, [group count], value);
122
123 default:
125 return NO;
126 }
127
128 *value = OOJSValueFromNativeObject(context, result);
129 return YES;
130
132}
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()

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

Definition at line 298 of file OOJSShipGroup.m.

299{
300 OOJS_NATIVE_ENTER(context)
301
302 OOShipGroup* thisGroup = nil;
303 ShipEntity* ship = nil;
304
305 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, OOJS_THIS, &thisGroup)))
306 return NO;
307
308 if (argc > 0)
309 ship = OOJSNativeObjectOfClassFromJSValue(context, OOJS_ARGV[0], [ShipEntity class]);
310 if (ship == nil) {
311 if (argc > 0 && JSVAL_IS_NULL(OOJS_ARGV[0]))
312 OOJS_RETURN_VOID; // OK, do nothing for null ship.
313
314 OOJSReportBadArguments(context, @"ShipGroup", @"removeShip", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
315 return NO;
316 }
317
318 OOJS_RETURN_BOOL([thisGroup removeShip:ship]);
319
321}

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()

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

Definition at line 134 of file OOJSShipGroup.m.

135{
136 if (!JSID_IS_INT(propID))
137 return YES;
138
139 OOJS_NATIVE_ENTER(context)
140
141 OOShipGroup* group = nil;
142 ShipEntity* shipValue = nil;
143
144 if (EXPECT_NOT(!JSShipGroupGetShipGroup(context, this, &group)))
145 return NO;
146
147 switch (JSID_TO_INT(propID)) {
149 shipValue = OOJSNativeObjectOfClassFromJSValue(context, *value, [ShipEntity class]);
150 if (shipValue != nil || JSVAL_IS_NULL(*value)) {
151 [group setLeader:shipValue];
152 return YES;
153 }
154 break;
155
156 case kShipGroup_name:
157 [group setName:OOStringFromJSValueEvenIfNull(context, *value)];
158 return YES;
159 break;
160
161 default:
163 return NO;
164 }
165
166 OOJSReportBadPropertyValue(context, this, propID, sShipGroupProperties, *value);
167 return NO;
168
170}
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 41 of file OOJSShipGroup.m.

41 {
42 "ShipGroup",
43 JSCLASS_HAS_PRIVATE,
44
45 JS_PropertyStub, // addProperty
46 JS_PropertyStub, // delProperty
47 ShipGroupGetProperty, // getProperty
48 ShipGroupSetProperty, // setProperty
49 JS_EnumerateStub, // enumerate
50 JS_ResolveStub, // resolve
51 JS_ConvertStub, // convert
52 OOJSObjectWrapperFinalize, // finalize
53 JSCLASS_NO_OPTIONAL_MEMBERS
54};

Referenced by DEFINE_JS_OBJECT_GETTER(), InitOOJSShipGroup(), InitOOJSShipGroup(), and OOShipGroup(OOJavaScriptExtensions)::oo_jsValueInContext:.

◆ 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 73 of file OOJSShipGroup.m.

73 {
74 // JS name Function min args
75 { "toString", OOJSObjectWrapperToString, 0 },
76 { "addShip", ShipGroupAddShip, 1 },
77 { "containsShip", ShipGroupContainsShip, 1 },
78 { "removeShip", ShipGroupRemoveShip, 1 },
79 { 0 }
80};

Referenced by InitOOJSShipGroup(), and InitOOJSShipGroup().

◆ sShipGroupProperties

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

Definition at line 64 of file OOJSShipGroup.m.

64 {
65 // JS name ID flags
70 { 0 }
71};

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

◆ sShipGroupPrototype

JSObject* sShipGroupPrototype
static