Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOJSSun.m
Go to the documentation of this file.
1/*
2
3OOJSSun.m
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 "OOJSSun.h"
27#import "OOJSEntity.h"
29
30#import "OOSunEntity.h"
31
32
33static JSObject *sSunPrototype;
34
35
36static JSBool SunGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
37static JSBool SunGoNova(JSContext *context, uintN argc, jsval *vp);
38static JSBool SunCancelNova(JSContext *context, uintN argc, jsval *vp);
39
40
41static JSClass sSunClass =
42{
43 "Sun",
44 JSCLASS_HAS_PRIVATE,
45
46 JS_PropertyStub, // addProperty
47 JS_PropertyStub, // delProperty
48 SunGetProperty, // getProperty
49 JS_StrictPropertyStub, // setProperty
50 JS_EnumerateStub, // enumerate
51 JS_ResolveStub, // resolve
52 JS_ConvertStub, // convert
54 JSCLASS_NO_OPTIONAL_MEMBERS
55};
56
57
58enum
59{
60 // Property IDs
61 kSun_radius, // Radius of sun in metres, number, read-only
62 kSun_hasGoneNova, // Has sun gone nova, boolean, read-only
63 kSun_isGoingNova, // Will sun go nova, boolean, read-only
64 kSun_name // Name of sun, string, read-only (writable via systeminfo)
65};
66
67
68static JSPropertySpec sSunProperties[] =
69{
70 // JS name ID flags
71 { "hasGoneNova", kSun_hasGoneNova, OOJS_PROP_READONLY_CB },
72 { "isGoingNova", kSun_isGoingNova, OOJS_PROP_READONLY_CB },
75 { 0 }
76};
77
78
79static JSFunctionSpec sSunMethods[] =
80{
81 // JS name Function min args
82 { "cancelNova", SunCancelNova, 0 },
83 { "goNova", SunGoNova, 1 },
84 { 0 }
85};
86
87
89
90
97
98
99@implementation OOSunEntity (OOJavaScriptExtensions)
100
101- (BOOL) isVisibleToScripts
102{
103 return YES;
104}
105
106
107- (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
108{
109 *outClass = &sSunClass;
110 *outPrototype = sSunPrototype;
111}
112
113
114- (NSString *) oo_jsClassName
115{
116 return @"Sun";
117}
118
119@end
120
121
122static JSBool SunGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
123{
124 if (!JSID_IS_INT(propID)) return YES;
125
126 OOJS_NATIVE_ENTER(context)
127
128 OOSunEntity *sun = nil;
129
130 if (EXPECT_NOT(!JSSunGetSunEntity(context, this, &sun))) return NO;
131
132 switch (JSID_TO_INT(propID))
133 {
134 case kSun_radius:
135 return JS_NewNumberValue(context, [sun radius], value);
136
137 case kSun_name:
138 *value = OOJSValueFromNativeObject(context, [sun name]);
139 return YES;
140
141 case kSun_hasGoneNova:
142 *value = OOJSValueFromBOOL([sun goneNova]);
143 return YES;
144
145 case kSun_isGoingNova:
146 *value = OOJSValueFromBOOL([sun willGoNova] && ![sun goneNova]);
147 return YES;
148
149 default:
150 OOJSReportBadPropertySelector(context, this, propID, sSunProperties);
151 return NO;
152 }
153
155}
156
157
158// *** Methods ***
159
160// goNova([delay : Number])
161static JSBool SunGoNova(JSContext *context, uintN argc, jsval *vp)
162{
163 OOJS_NATIVE_ENTER(context)
164
165 OOSunEntity *sun = nil;
166 jsdouble delay = 0;
167
168 if (EXPECT_NOT(!JSSunGetSunEntity(context, OOJS_THIS, &sun))) return NO;
169 if (argc > 0 && EXPECT_NOT(!JS_ValueToNumber(context, OOJS_ARGV[0], &delay))) return NO;
170
171 [sun setGoingNova:YES inTime:delay];
173
175}
176
177
178// cancelNova()
179static JSBool SunCancelNova(JSContext *context, uintN argc, jsval *vp)
180{
181 OOJS_NATIVE_ENTER(context)
182
183 OOSunEntity *sun = nil;
184
185 if (EXPECT_NOT(!JSSunGetSunEntity(context, OOJS_THIS, &sun))) return NO;
186
187 if ([sun willGoNova] && ![sun goneNova])
188 {
189 [sun setGoingNova:NO inTime:0];
190 }
192
194}
#define EXPECT_NOT(x)
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
static JSBool SunGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSSun.m:122
static JSBool SunCancelNova(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSun.m:179
static JSClass sSunClass
Definition OOJSSun.m:41
static JSBool SunGoNova(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSun.m:161
static JSObject * sSunPrototype
Definition OOJSSun.m:33
static JSFunctionSpec sSunMethods[]
Definition OOJSSun.m:79
@ kSun_name
Definition OOJSSun.m:64
@ kSun_radius
Definition OOJSSun.m:61
@ kSun_hasGoneNova
Definition OOJSSun.m:62
@ kSun_isGoingNova
Definition OOJSSun.m:63
void InitOOJSSun(JSContext *context, JSObject *global)
Definition OOJSSun.m:91
static JSPropertySpec sSunProperties[]
Definition OOJSSun.m:68
#define OOJS_THIS
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)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
#define OOJS_ARGV
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
#define OOJS_PROP_READONLY_CB
#define OOJS_RETURN_VOID
return nil
void setGoingNova:inTime:(BOOL yesno,[inTime] double interval)