Oolite
Loading...
Searching...
No Matches
OOJSExhaustPlume.m
Go to the documentation of this file.
1/*
2OOJSExhaustPlume.m
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 "OOJSExhaustPlume.h"
27#import "OOJSEntity.h"
28#import "OOJSVector.h"
30#import "ShipEntity.h"
31
32static JSObject* sExhaustPlumePrototype;
33
34static BOOL JSExhaustPlumeGetExhaustPlumeEntity(JSContext* context, JSObject* jsobj, OOExhaustPlumeEntity** outEntity);
35
36static JSBool ExhaustPlumeGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
37static JSBool ExhaustPlumeSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
38
39static JSBool ExhaustPlumeRemove(JSContext* context, uintN argc, jsval* vp);
40
41static JSClass sExhaustPlumeClass = {
42 "ExhaustPlume",
43 JSCLASS_HAS_PRIVATE,
44
45 JS_PropertyStub, // addProperty
46 JS_PropertyStub, // delProperty
47 ExhaustPlumeGetProperty, // getProperty
48 ExhaustPlumeSetProperty, // setProperty
49 JS_EnumerateStub, // enumerate
50 JS_ResolveStub, // resolve
51 JS_ConvertStub, // convert
52 OOJSObjectWrapperFinalize, // finalize
53 JSCLASS_NO_OPTIONAL_MEMBERS
54};
55
56enum {
57 // Property IDs
59};
60
61static JSPropertySpec sExhaustPlumeProperties[] = {
62 // JS name ID flags
64 { 0 }
65};
66
67static JSFunctionSpec sExhaustPlumeMethods[] = {
68 // JS name Function min args
69 { "remove", ExhaustPlumeRemove, 0 },
70
71 { 0 }
72};
73
80
81static BOOL JSExhaustPlumeGetExhaustPlumeEntity(JSContext* context, JSObject* jsobj, OOExhaustPlumeEntity** outEntity)
82{
84
85 BOOL result;
86 Entity* entity = nil;
87
88 if (outEntity == NULL)
89 return NO;
90 *outEntity = nil;
91
92 result = OOJSEntityGetEntity(context, jsobj, &entity);
93 if (!result)
94 return NO;
95
96 if (![entity isKindOfClass:[OOExhaustPlumeEntity class]])
97 return NO;
98
99 *outEntity = (OOExhaustPlumeEntity*)entity;
100 return YES;
101
103}
104
105@implementation OOExhaustPlumeEntity (OOJavaScriptExtensions)
106
107- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
108{
109 *outClass = &sExhaustPlumeClass;
110 *outPrototype = sExhaustPlumePrototype;
111}
112
113- (NSString*)oo_jsClassName
114{
115 return @"ExhaustPlume";
116}
117
118- (BOOL)isVisibleToScripts
119{
120 return YES;
121}
122
123@end
124
125static JSBool ExhaustPlumeGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
126{
127 if (!JSID_IS_INT(propID))
128 return YES;
129
130 OOJS_NATIVE_ENTER(context)
131
132 OOExhaustPlumeEntity* entity = nil;
133 id result = nil;
134
135 if (!JSExhaustPlumeGetExhaustPlumeEntity(context, this, &entity))
136 return NO;
137 if (entity == nil) {
138 *value = JSVAL_VOID;
139 return YES;
140 }
141
142 switch (JSID_TO_INT(propID)) {
144 return VectorToJSValue(context, [entity scale], value);
145
146 default:
148 return NO;
149 }
150
151 *value = OOJSValueFromNativeObject(context, result);
152 return YES;
153
155}
156
157static JSBool ExhaustPlumeSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
158{
159 if (!JSID_IS_INT(propID))
160 return YES;
161
162 OOJS_NATIVE_ENTER(context)
163
164 OOExhaustPlumeEntity* entity = nil;
165 Vector vValue;
166
167 if (!JSExhaustPlumeGetExhaustPlumeEntity(context, this, &entity))
168 return NO;
169 if (entity == nil)
170 return YES;
171
172 switch (JSID_TO_INT(propID)) {
174 if (JSValueToVector(context, *value, &vValue)) {
175 [entity setScale:vValue];
176 return YES;
177 }
178 break;
179
180 default:
182 return NO;
183 }
184
185 OOJSReportBadPropertyValue(context, this, propID, sExhaustPlumeProperties, *value);
186 return NO;
187
189}
190
191// *** Methods ***
192
193#define GET_THIS_EXHAUSTPLUME(THISENT) \
194 do { \
195 if (EXPECT_NOT(!JSExhaustPlumeGetExhaustPlumeEntity(context, OOJS_THIS, &THISENT))) \
196 return NO; /* Exception */ \
197 if (OOIsStaleEntity(THISENT)) \
198 OOJS_RETURN_VOID; \
199 } while (0)
200
201static JSBool ExhaustPlumeRemove(JSContext* context, uintN argc, jsval* vp)
202{
203 OOJS_NATIVE_ENTER(context)
204
205 OOExhaustPlumeEntity* thisEnt = nil;
206 GET_THIS_EXHAUSTPLUME(thisEnt);
207
208 ShipEntity* parent = [thisEnt owner];
209 [parent removeExhaust:thisEnt];
210
212
214}
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
@ kExhaustPlume_size
static JSBool ExhaustPlumeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
#define GET_THIS_EXHAUSTPLUME(THISENT)
void InitOOJSExhaustPlume(JSContext *context, JSObject *global)
static JSPropertySpec sExhaustPlumeProperties[]
static BOOL JSExhaustPlumeGetExhaustPlumeEntity(JSContext *context, JSObject *jsobj, OOExhaustPlumeEntity **outEntity)
static JSBool ExhaustPlumeRemove(JSContext *context, uintN argc, jsval *vp)
static JSClass sExhaustPlumeClass
static JSObject * sExhaustPlumePrototype
static JSBool ExhaustPlumeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSFunctionSpec sExhaustPlumeMethods[]
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
Definition OOJSVector.m:341
BOOL VectorToJSValue(JSContext *context, Vector vector, jsval *outValue) NONNULL_FUNC
Definition OOJSVector.m:264
#define OOJS_PROP_READWRITE_CB
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)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
#define OOJS_RETURN_VOID
return nil
id owner()
Definition Entity.m:548
void setScale:(Vector scale)
void removeExhaust:(OOExhaustPlumeEntity *exhaust)