Line data Source code
1 0 : /*
2 : OOJSDock.m
3 :
4 : Oolite
5 : Copyright (C) 2004-2013 Giles C Williams and contributors
6 :
7 : This program is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License
9 : as published by the Free Software Foundation; either version 2
10 : of the License, or (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 : MA 02110-1301, USA.
21 :
22 : */
23 :
24 : #import "OOJSDock.h"
25 : #import "OOJSEntity.h"
26 : #import "OOJSShip.h"
27 : #import "OOJSPlayer.h"
28 : #import "OOJavaScriptEngine.h"
29 :
30 : #import "DockEntity.h"
31 : #import "GameController.h"
32 :
33 :
34 0 : static JSObject *sDockPrototype;
35 :
36 : static BOOL JSDockGetDockEntity(JSContext *context, JSObject *stationObj, DockEntity **outEntity);
37 : static BOOL JSDockGetShipEntity(JSContext *context, JSObject *shipObj, ShipEntity **outEntity);
38 :
39 : static JSBool DockIsQueued(JSContext *context, uintN argc, jsval *vp);
40 :
41 :
42 : static JSBool DockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
43 : static JSBool DockSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
44 :
45 :
46 0 : static JSClass sDockClass =
47 : {
48 : "Dock",
49 : JSCLASS_HAS_PRIVATE,
50 :
51 : JS_PropertyStub, // addProperty
52 : JS_PropertyStub, // delProperty
53 : DockGetProperty, // getProperty
54 : DockSetProperty, // setProperty
55 : JS_EnumerateStub, // enumerate
56 : JS_ResolveStub, // resolve
57 : JS_ConvertStub, // convert
58 : OOJSObjectWrapperFinalize,// finalize
59 : JSCLASS_NO_OPTIONAL_MEMBERS
60 : };
61 :
62 :
63 0 : enum
64 : {
65 : // Property IDs
66 : kDock_allowsDocking,
67 : kDock_disallowedDockingCollides,
68 : kDock_allowsLaunching,
69 : kDock_dockingQueueLength,
70 : kDock_launchingQueueLength
71 : };
72 :
73 :
74 0 : static JSPropertySpec sDockProperties[] =
75 : {
76 : // JS name ID flags
77 : { "allowsDocking", kDock_allowsDocking, OOJS_PROP_READWRITE_CB },
78 : { "disallowedDockingCollides", kDock_disallowedDockingCollides, OOJS_PROP_READWRITE_CB },
79 : { "allowsLaunching", kDock_allowsLaunching, OOJS_PROP_READWRITE_CB },
80 : { "dockingQueueLength", kDock_dockingQueueLength, OOJS_PROP_READONLY_CB },
81 : { "launchingQueueLength", kDock_launchingQueueLength, OOJS_PROP_READONLY_CB },
82 : { 0 }
83 : };
84 :
85 :
86 0 : static JSFunctionSpec sDockMethods[] =
87 : {
88 : // JS name Function min args
89 : { "isQueued", DockIsQueued, 1 },
90 : { 0 }
91 : };
92 :
93 :
94 0 : void InitOOJSDock(JSContext *context, JSObject *global)
95 : {
96 : sDockPrototype = JS_InitClass(context, global, JSShipPrototype(), &sDockClass, OOJSUnconstructableConstruct, 0, sDockProperties, sDockMethods, NULL, NULL);
97 : OOJSRegisterObjectConverter(&sDockClass, OOJSBasicPrivateObjectConverter);
98 : OOJSRegisterSubclass(&sDockClass, JSShipClass());
99 : }
100 :
101 :
102 0 : static BOOL JSDockGetDockEntity(JSContext *context, JSObject *dockObj, DockEntity **outEntity)
103 : {
104 : OOJS_PROFILE_ENTER
105 :
106 : BOOL result;
107 : Entity *entity = nil;
108 :
109 : if (outEntity == NULL) return NO;
110 : *outEntity = nil;
111 :
112 : result = OOJSEntityGetEntity(context, dockObj, &entity);
113 : if (!result) return NO;
114 :
115 : if (![entity isKindOfClass:[DockEntity class]]) return NO;
116 :
117 : *outEntity = (DockEntity *)entity;
118 : return YES;
119 :
120 : OOJS_PROFILE_EXIT
121 : }
122 :
123 :
124 0 : static BOOL JSDockGetShipEntity(JSContext *context, JSObject *shipObj, ShipEntity **outEntity)
125 : {
126 : OOJS_PROFILE_ENTER
127 :
128 : BOOL result;
129 : Entity *entity = nil;
130 :
131 : if (outEntity == NULL) return NO;
132 : *outEntity = nil;
133 :
134 : result = OOJSEntityGetEntity(context, shipObj, &entity);
135 : if (!result) return NO;
136 :
137 : if (![entity isKindOfClass:[ShipEntity class]]) return NO;
138 :
139 : *outEntity = (ShipEntity *)entity;
140 : return YES;
141 :
142 : OOJS_PROFILE_EXIT
143 : }
144 :
145 :
146 : @implementation DockEntity (OOJavaScriptExtensions)
147 :
148 0 : - (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
149 : {
150 : *outClass = &sDockClass;
151 : *outPrototype = sDockPrototype;
152 : }
153 :
154 :
155 0 : - (NSString *) oo_jsClassName
156 : {
157 : return @"Dock";
158 : }
159 :
160 : @end
161 :
162 :
163 0 : static JSBool DockGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
164 : {
165 : if (!JSID_IS_INT(propID)) return YES;
166 :
167 : OOJS_NATIVE_ENTER(context)
168 :
169 : DockEntity *entity = nil;
170 :
171 : if (!JSDockGetDockEntity(context, this, &entity)) return NO;
172 : if (entity == nil) { *value = JSVAL_VOID; return YES; }
173 :
174 : switch (JSID_TO_INT(propID))
175 : {
176 : case kDock_allowsDocking:
177 : *value = OOJSValueFromBOOL([entity allowsDocking]);
178 : return YES;
179 :
180 : case kDock_disallowedDockingCollides:
181 : *value = OOJSValueFromBOOL([entity disallowedDockingCollides]);
182 : return YES;
183 :
184 : case kDock_allowsLaunching:
185 : *value = OOJSValueFromBOOL([entity allowsLaunching]);
186 : return YES;
187 :
188 : case kDock_dockingQueueLength:
189 : return JS_NewNumberValue(context, [entity countOfShipsInDockingQueue], value);
190 :
191 : case kDock_launchingQueueLength:
192 : return JS_NewNumberValue(context, [entity countOfShipsInLaunchQueue], value);
193 :
194 : default:
195 : OOJSReportBadPropertySelector(context, this, propID, sDockProperties);
196 : return NO;
197 : }
198 :
199 : OOJS_NATIVE_EXIT
200 : }
201 :
202 :
203 0 : static JSBool DockSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
204 : {
205 : if (!JSID_IS_INT(propID)) return YES;
206 :
207 : OOJS_NATIVE_ENTER(context)
208 :
209 : DockEntity *entity = nil;
210 : JSBool bValue;
211 : // int32 iValue;
212 :
213 : if (!JSDockGetDockEntity(context, this, &entity)) return NO;
214 : if (entity == nil) return YES;
215 :
216 : switch (JSID_TO_INT(propID))
217 : {
218 : case kDock_allowsDocking:
219 : if (JS_ValueToBoolean(context, *value, &bValue))
220 : {
221 : [entity setAllowsDocking:bValue];
222 : return YES;
223 : }
224 : break;
225 :
226 : case kDock_allowsLaunching:
227 : if (JS_ValueToBoolean(context, *value, &bValue))
228 : {
229 : [entity setAllowsLaunching:bValue];
230 : return YES;
231 : }
232 : break;
233 :
234 : case kDock_disallowedDockingCollides:
235 : if (JS_ValueToBoolean(context, *value, &bValue))
236 : {
237 : [entity setDisallowedDockingCollides:bValue];
238 : return YES;
239 : }
240 : break;
241 :
242 : default:
243 : OOJSReportBadPropertySelector(context, this, propID, sDockProperties);
244 : return NO;
245 : }
246 :
247 : OOJSReportBadPropertyValue(context, this, propID, sDockProperties, *value);
248 : return NO;
249 :
250 : OOJS_NATIVE_EXIT
251 : }
252 :
253 :
254 : // *** Methods ***
255 :
256 0 : static JSBool DockIsQueued(JSContext *context, uintN argc, jsval *vp)
257 : {
258 : OOJS_NATIVE_ENTER(context)
259 :
260 : BOOL result = NO;
261 : DockEntity *dock = nil;
262 :
263 : JSDockGetDockEntity(context, OOJS_THIS, &dock);
264 : if (argc == 0)
265 : {
266 : OOJSReportBadArguments(context, @"Dock", @"isQueued", MIN(argc, 1U), OOJS_ARGV, nil, @"ship");
267 : return NO;
268 : }
269 : ShipEntity *ship = nil;
270 : JSDockGetShipEntity(context, JSVAL_TO_OBJECT(OOJS_ARGV[0]), &ship);
271 : if (ship != nil)
272 : {
273 : result = [dock shipIsInDockingQueue:ship];
274 : }
275 :
276 : OOJS_RETURN_BOOL(result);
277 :
278 : OOJS_NATIVE_EXIT
279 : }
280 :
|