Oolite
Loading...
Searching...
No Matches
OOJSFlasher.m
Go to the documentation of this file.
1/*
2OOJSFlasher.m (QuickJS-ng)
3
4Oolite
5Copyright (C) 2004-2013 Giles C Williams and contributors
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; either version 2
10of the License, or (at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20MA 02110-1301, USA.
21
22 */
23
24#import "OOJSFlasher.h"
26#import "OOFlasherEntity.h"
27#import "OOJSEntity.h"
28#import "OOJSVector.h"
31#import "ShipEntity.h"
32
33static JSClassID sFlasherClassID;
34
35static BOOL JSFlasherGetFlasherEntity(JSContext* context, JSValueConst jsobj, OOFlasherEntity** outEntity);
36
39
40static JSValue FlasherRemove(OOJS_NATIVE_ARGS);
41
42static JSClassDef sFlasherClass = {
43 .class_name = "Flasher",
44 .finalizer = OOJSObjectWrapperFinalize,
45};
46
47enum {
48 // Property IDs
55};
56
66
67static const JSCFunctionListEntry sFlasherMethods[] = {
68 // JS name, min args, function
69 JS_CFUNC_DEF("remove", 0, FlasherRemove),
70};
71
72void InitOOJSFlasher(JSContext* context, JSValueConst global)
73{
75
76 JSValue parentPrototype = JS_GetClassProto(context, JSEntityClassID());
77 NSCAssert(JS_IsObject(parentPrototype), @"Entity must be initialized before Flasher.");
78 OOJSInitClass(context, global, sFlasherClassID, parentPrototype, OOJSUnconstructableConstruct, 0,
80 JS_FreeValue(context, parentPrototype);
81
84}
85
86JSClassID JSFlasherClassID(void)
87{
88 return sFlasherClassID;
89}
90
91static BOOL JSFlasherGetFlasherEntity(JSContext* context, JSValueConst jsobj, OOFlasherEntity** outEntity)
92{
94
95 BOOL result;
96 Entity* entity = nil;
97
98 if (outEntity == NULL)
99 return NO;
100 *outEntity = nil;
101
102 result = OOJSEntityGetEntity(context, jsobj, &entity);
103 if (!result)
104 return NO;
105
106 if (![entity isKindOfClass:[OOFlasherEntity class]])
107 return NO;
108
109 *outEntity = (OOFlasherEntity*)entity;
110 return YES;
111
113}
114
115@implementation OOFlasherEntity (OOJavaScriptExtensions)
116
117- (JSClassID)oo_jsClassID
118{
119 return sFlasherClassID;
120}
121
122- (NSString*)oo_jsClassName
123{
124 return @"Flasher";
125}
126
128{
129 return YES;
130}
131
132@end
133
135{
136 OOJS_NATIVE_ENTER(context)
137
138 OOFlasherEntity* entity = nil;
139 id result = nil;
140
141 if (!JSFlasherGetFlasherEntity(context, this, &entity))
142 return OOJSNativeFailure(context);
143 if (entity == nil) {
144 return JS_UNDEFINED;
145 }
146
147 switch (magic) {
148 case kFlasher_active:
149 return OOJSValueFromBOOL([entity isActive]);
150
151 case kFlasher_color:
152 result = [[entity color] normalizedArray];
153 break;
154
156 return JS_NewNumber(context, [entity frequency]);
157
159 return JS_NewNumber(context, [entity fraction]);
160
161 case kFlasher_phase:
162 return JS_NewNumber(context, [entity phase]);
163
164 case kFlasher_size:
165 return JS_NewNumber(context, [entity diameter]);
166
167 default:
169 return OOJSNativeFailure(context);
170 }
171
172 return OOJSValueFromNativeObject(context, result);
173
175}
176
178{
179 OOJS_NATIVE_ENTER(context)
180
181 OOFlasherEntity* entity = nil;
182 double fValue;
183 int bValue;
184 OOColor* colorForScript = nil;
185
186 if (!JSFlasherGetFlasherEntity(context, this, &entity))
187 return OOJSNativeFailure(context);
188 if (entity == nil)
189 return JS_UNDEFINED;
190
191 switch (magic) {
192 case kFlasher_active:
193 if ((bValue = JS_ToBool(context, value)) >= 0) {
194 [entity setActive:bValue];
195 return JS_UNDEFINED;
196 }
197 break;
198
199 case kFlasher_color:
200 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, value)];
201 if (colorForScript != nil || JS_IsNull(value)) {
202 [entity setColor:colorForScript];
203 return JS_UNDEFINED;
204 }
205 break;
206
208 if (JS_ToFloat64(context, &fValue, value) == 0) {
209 if (fValue >= 0.0) {
210 [entity setFrequency:fValue];
211 return JS_UNDEFINED;
212 }
213 }
214 break;
215
217 if (JS_ToFloat64(context, &fValue, value) == 0) {
218 if (fValue > 0.0 && fValue <= 1.0) {
219 [entity setFraction:fValue];
220 return JS_UNDEFINED;
221 }
222 }
223 break;
224
225 case kFlasher_phase:
226 if (JS_ToFloat64(context, &fValue, value) == 0) {
227 [entity setPhase:fValue];
228 return JS_UNDEFINED;
229 }
230 break;
231
232 case kFlasher_size:
233 if (JS_ToFloat64(context, &fValue, value) == 0) {
234 if (fValue > 0.0) {
235 [entity setDiameter:fValue];
236 return JS_UNDEFINED;
237 }
238 }
239 break;
240
241 default:
243 return OOJSNativeFailure(context);
244 }
245
247 return OOJSNativeFailure(context);
248
250}
251
252// *** Methods ***
253
254#define GET_THIS_FLASHER(THISENT) \
255 do { \
256 if (EXPECT_NOT(!JSFlasherGetFlasherEntity(context, OOJS_THIS, &THISENT))) \
257 return OOJSNativeFailure(context); /* Exception */ \
258 if (OOIsStaleEntity(THISENT)) \
259 OOJS_RETURN_VOID; \
260 } while (0)
261
263{
264 OOJS_NATIVE_ENTER(context)
265
266 OOFlasherEntity* thisEnt = nil;
267 GET_THIS_FLASHER(thisEnt);
268
269 Entity* parent = [thisEnt owner];
270 if ([parent isShip]) {
271 [(ShipEntity*)parent removeFlasher:thisEnt];
272 } else {
274 }
275
277
279}
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
static JSBool FlasherGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSPropertySpec sFlasherProperties[]
Definition OOJSFlasher.m:67
@ kFlasher_size
Definition OOJSFlasher.m:64
@ kFlasher_phase
Definition OOJSFlasher.m:63
@ kFlasher_active
Definition OOJSFlasher.m:59
@ kFlasher_fraction
Definition OOJSFlasher.m:61
@ kFlasher_frequency
Definition OOJSFlasher.m:62
@ kFlasher_color
Definition OOJSFlasher.m:60
static JSFunctionSpec sFlasherMethods[]
Definition OOJSFlasher.m:78
static JSBool FlasherSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static BOOL JSFlasherGetFlasherEntity(JSContext *context, JSObject *jsobj, OOFlasherEntity **outEntity)
Definition OOJSFlasher.m:92
static JSBool FlasherRemove(JSContext *context, uintN argc, jsval *vp)
#define GET_THIS_FLASHER(THISENT)
static JSClass sFlasherClass
Definition OOJSFlasher.m:42
void InitOOJSFlasher(JSContext *context, JSObject *global)
Definition OOJSFlasher.m:85
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
#define OOJS_RETURN_VOID
return nil
JSClassID JSEntityClassID(void)
Definition OOJSEntity.m:115
static JSClassID sFlasherClassID
Definition OOJSFlasher.m:33
JSClassID JSFlasherClassID(void)
Definition OOJSFlasher.m:86
#define OOJS_PROPERTY_RW(name, getter, setter, magic)
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_SETTER_ARGS
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)
id owner()
Definition Entity.m:548
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
NSArray * normalizedArray()
Definition OOColor.m:465
void setPhase:(float phase)
void setFrequency:(float frequency)
void setActive:(BOOL active)
void setFraction:(float fraction)
void setColor:(OOColor *color)
void setDiameter:(float diameter)
void removeSubEntity:(Entity< OOSubEntity > *sub)
void removeFlasher:(OOFlasherEntity *flasher)