Oolite
Loading...
Searching...
No Matches
OOJSPlanet.m
Go to the documentation of this file.
1/*
2
3OOJSPlanet.m (QuickJS-ng)
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSPlanet.h"
28#import "OOJSEntity.h"
29#import "OOJSQuaternion.h"
30#import "OOJSVector.h"
32#import "OOPlanetEntity.h"
33
34static JSClassID sPlanetClassID;
35
38
39static JSClassDef sPlanetClass = {
40 .class_name = "Planet",
41 .finalizer = OOJSObjectWrapperFinalize,
42};
43
44enum {
45 // Property IDs
46 kPlanet_airColor, // air color, read/write
47 kPlanet_airColorMixRatio, // air color mix ratio, float, read/write
48 kPlanet_airDensity, // air density, float, read/write
50 kPlanet_illuminationColor, // illumination color, read/write
51 kPlanet_isMainPlanet, // Is [UNIVERSE planet], boolean, read-only
52 kPlanet_name, // Name of planet, string, read/write
53 kPlanet_radius, // Radius of planet in metres, read-only
54 kPlanet_texture, // Planet texture read / write
55 kPlanet_orientation, // orientation, quaternion, read/write
58};
59
75
76DEFINE_JS_OBJECT_GETTER(JSPlanetGetPlanetEntity, sPlanetClassID, OOPlanetEntity)
77
78void InitOOJSPlanet(JSContext* context, JSValueConst global)
79{
81
82 JSValue parentPrototype = JS_GetClassProto(context, JSEntityClassID());
83 NSCAssert(JS_IsObject(parentPrototype), @"Entity must be initialized before Planet.");
84 OOJSInitClass(context, global, sPlanetClassID, parentPrototype, OOJSUnconstructableConstruct, 0,
86 JS_FreeValue(context, parentPrototype);
87
90}
91
92JSClassID JSPlanetClassID(void)
93{
94 return sPlanetClassID;
95}
96
97@implementation OOPlanetEntity (OOJavaScriptExtensions)
98
100{
101 OOStellarBodyType type = [self planetType];
102 return type == STELLAR_TYPE_NORMAL_PLANET || type == STELLAR_TYPE_MOON;
103}
104
105- (JSClassID)oo_jsClassID
106{
107 return sPlanetClassID;
108}
109
110- (NSString*)oo_jsClassName
111{
112 switch ([self planetType]) {
114 return @"Planet";
116 return @"Moon";
117 default:
118 return @"Unknown";
119 }
120}
121
122@end
123
125{
126 OOJS_NATIVE_ENTER(context)
127
128 OOPlanetEntity* planet = nil;
129 if (!JSPlanetGetPlanetEntity(context, this, &planet))
130 return OOJSNativeFailure(context);
131
132 switch (magic) {
133 case kPlanet_airColor:
134 return OOJSValueFromNativeObject(context, [[planet airColor] normalizedArray]);
135
137 return JS_NewNumber(context, [planet airColorMixRatio]);
138
140 return JS_NewNumber(context, [planet airDensity]);
141
143 return OOJSValueFromNativeObject(context, [[planet illuminationColor] normalizedArray]);
144
146 return OOJSValueFromBOOL(planet == (id)[UNIVERSE planet]);
147
148 case kPlanet_radius:
149 return JS_NewNumber(context, [planet radius]);
150
152 return OOJSValueFromBOOL([planet hasAtmosphere]);
153
154 case kPlanet_texture:
155 return OOJSValueFromNativeObject(context, [planet textureFileName]);
156
157 case kPlanet_name:
158 return OOJSValueFromNativeObject(context, [planet name]);
159
161 OOJS_RETURN_QUATERNION([planet normalOrientation]);
162
164 return JS_NewNumber(context, [planet rotationalVelocity]);
165
167 OOJS_RETURN_VECTOR([planet terminatorThresholdVector]);
168
169 default:
171 return OOJSNativeFailure(context);
172 }
173
175}
176
178{
179 OOJS_NATIVE_ENTER(context)
180
181 OOPlanetEntity* planet = nil;
182 NSString* sValue = nil;
183 Quaternion qValue;
184 Vector vValue;
185 double dValue;
186 OOColor* colorForScript = nil;
187
188 if (!JSPlanetGetPlanetEntity(context, this, &planet))
189 return OOJSNativeFailure(context);
190
191 switch (magic) {
192 case kPlanet_airColor:
193 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, value)];
194 if (colorForScript != nil || JS_IsNull(value)) {
195 [planet setAirColor:colorForScript];
196 return JS_UNDEFINED;
197 }
198 break;
199
201 if (JS_ToFloat64(context, &dValue, value) == 0) {
202 [planet setAirColorMixRatio:dValue];
203 return JS_UNDEFINED;
204 }
205 break;
206
208 if (JS_ToFloat64(context, &dValue, value) == 0) {
209 [planet setAirDensity:dValue];
210 return JS_UNDEFINED;
211 }
212 break;
213
215 colorForScript = [OOColor colorWithDescription:OOJSNativeObjectFromJSValue(context, value)];
216 if (colorForScript != nil || JS_IsNull(value)) {
217 [planet setIlluminationColor:colorForScript];
218 return JS_UNDEFINED;
219 }
220 break;
221
222 case kPlanet_name:
223 sValue = OOStringFromJSValue(context, value);
224 [planet setName:sValue];
225 return JS_UNDEFINED;
226
227 case kPlanet_texture: {
228 BOOL OK = NO;
229 sValue = OOStringFromJSValue(context, value);
230
232
233 if ([planet isKindOfClass:[OOPlanetEntity class]]) {
234 if (sValue == nil) {
235 OOJSReportWarning(context, @"Expected texture string. Value not set.");
236 } else {
237 OK = YES;
238 }
239 }
240
241 if (OK) {
242 OK = [planet setUpPlanetFromTexture:sValue];
243 if (!OK)
244 OOJSReportWarning(context, @"Cannot find texture \"%@\". Value not set.", sValue);
245 }
246
248
249 return JS_UNDEFINED; // Even if !OK, no exception was raised.
250 }
251
253 if (JSValueToQuaternion(context, value, &qValue)) {
254 quaternion_normalize(&qValue);
255 [planet setOrientation:qValue];
256 return JS_UNDEFINED;
257 }
258 break;
259
261 if (JS_ToFloat64(context, &dValue, value) == 0) {
262 [planet setRotationalVelocity:dValue];
263 return JS_UNDEFINED;
264 }
265 break;
266
268 if (JSValueToVector(context, value, &vValue)) {
269 [planet setTerminatorThresholdVector:vValue];
270 return JS_UNDEFINED;
271 }
272 break;
273
274 default:
276 return OOJSNativeFailure(context);
277 }
278
280 return OOJSNativeFailure(context);
281
283}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
void InitOOJSPlanet(JSContext *context, JSObject *global)
Definition OOJSPlanet.m:87
static JSClass sPlanetClass
Definition OOJSPlanet.m:37
static JSPropertySpec sPlanetProperties[]
Definition OOJSPlanet.m:68
@ kPlanet_airColor
Definition OOJSPlanet.m:54
@ kPlanet_orientation
Definition OOJSPlanet.m:63
@ kPlanet_radius
Definition OOJSPlanet.m:61
@ kPlanet_airColorMixRatio
Definition OOJSPlanet.m:55
@ kPlanet_isMainPlanet
Definition OOJSPlanet.m:59
@ kPlanet_terminatorThresholdVector
Definition OOJSPlanet.m:65
@ kPlanet_airDensity
Definition OOJSPlanet.m:56
@ kPlanet_name
Definition OOJSPlanet.m:60
@ kPlanet_rotationalVelocity
Definition OOJSPlanet.m:64
@ kPlanet_hasAtmosphere
Definition OOJSPlanet.m:57
@ kPlanet_illuminationColor
Definition OOJSPlanet.m:58
@ kPlanet_texture
Definition OOJSPlanet.m:62
static JSBool PlanetGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSPlanet.m:122
static JSBool PlanetSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
Definition OOJSPlanet.m:184
BOOL JSValueToQuaternion(JSContext *context, jsval value, Quaternion *outQuaternion) NONNULL_FUNC
BOOL JSValueToVector(JSContext *context, jsval value, Vector *outVector) NONNULL_FUNC
Definition OOJSVector.m:341
void OOJSPauseTimeLimiter(void)
void OOJSReportWarning(JSContext *context, NSString *format,...)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_RETURN_QUATERNION(value)
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)
void OOJSResumeTimeLimiter(void)
#define OOJS_RETURN_VECTOR(value)
return nil
OOINLINE void quaternion_normalize(Quaternion *quat) NONNULL_FUNC ALWAYS_INLINE_FUNC
OOStellarBodyType
@ STELLAR_TYPE_MOON
@ STELLAR_TYPE_NORMAL_PLANET
JSClassID JSEntityClassID(void)
Definition OOJSEntity.m:115
static JSClassID sPlanetClassID
Definition OOJSPlanet.m:34
JSClassID JSPlanetClassID(void)
Definition OOJSPlanet.m:92
#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
#define OOJS_PROPERTY_RO(name, getter, magic)
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_COUNT(array)
#define UNIVERSE
Definition Universe.h:827
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135