Oolite
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
32static JSObject* sSunPrototype;
33
34static JSBool SunGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
35static JSBool SunGoNova(JSContext* context, uintN argc, jsval* vp);
36static JSBool SunCancelNova(JSContext* context, uintN argc, jsval* vp);
37
38static JSClass sSunClass = {
39 "Sun",
40 JSCLASS_HAS_PRIVATE,
41
42 JS_PropertyStub, // addProperty
43 JS_PropertyStub, // delProperty
44 SunGetProperty, // getProperty
45 JS_StrictPropertyStub, // setProperty
46 JS_EnumerateStub, // enumerate
47 JS_ResolveStub, // resolve
48 JS_ConvertStub, // convert
49 OOJSObjectWrapperFinalize, // finalize
50 JSCLASS_NO_OPTIONAL_MEMBERS
51};
52
53enum {
54 // Property IDs
55 kSun_radius, // Radius of sun in metres, number, read-only
56 kSun_hasGoneNova, // Has sun gone nova, boolean, read-only
57 kSun_isGoingNova, // Will sun go nova, boolean, read-only
58 kSun_name // Name of sun, string, read-only (writable via systeminfo)
59};
60
61static JSPropertySpec sSunProperties[] = {
62 // JS name ID flags
63 { "hasGoneNova", kSun_hasGoneNova, OOJS_PROP_READONLY_CB },
64 { "isGoingNova", kSun_isGoingNova, OOJS_PROP_READONLY_CB },
67 { 0 }
68};
69
70static JSFunctionSpec sSunMethods[] = {
71 // JS name Function min args
72 { "cancelNova", SunCancelNova, 0 },
73 { "goNova", SunGoNova, 1 },
74 { 0 }
75};
76
78
85
86@implementation OOSunEntity (OOJavaScriptExtensions)
87
89{
90 return YES;
91}
92
93- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
94{
95 *outClass = &sSunClass;
96 *outPrototype = sSunPrototype;
97}
98
99- (NSString*)oo_jsClassName
100{
101 return @"Sun";
102}
103
104@end
105
106static JSBool SunGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
107{
108 if (!JSID_IS_INT(propID))
109 return YES;
110
111 OOJS_NATIVE_ENTER(context)
112
113 OOSunEntity* sun = nil;
114
115 if (EXPECT_NOT(!JSSunGetSunEntity(context, this, &sun)))
116 return NO;
117
118 switch (JSID_TO_INT(propID)) {
119 case kSun_radius:
120 return JS_NewNumberValue(context, [sun radius], value);
121
122 case kSun_name:
123 *value = OOJSValueFromNativeObject(context, [sun name]);
124 return YES;
125
126 case kSun_hasGoneNova:
127 *value = OOJSValueFromBOOL([sun goneNova]);
128 return YES;
129
130 case kSun_isGoingNova:
131 *value = OOJSValueFromBOOL([sun willGoNova] && ![sun goneNova]);
132 return YES;
133
134 default:
135 OOJSReportBadPropertySelector(context, this, propID, sSunProperties);
136 return NO;
137 }
138
140}
141
142// *** Methods ***
143
144// goNova([delay : Number])
145static JSBool SunGoNova(JSContext* context, uintN argc, jsval* vp)
146{
147 OOJS_NATIVE_ENTER(context)
148
149 OOSunEntity* sun = nil;
150 jsdouble delay = 0;
151
152 if (EXPECT_NOT(!JSSunGetSunEntity(context, OOJS_THIS, &sun)))
153 return NO;
154 if (argc > 0 && EXPECT_NOT(!JS_ValueToNumber(context, OOJS_ARGV[0], &delay)))
155 return NO;
156
157 [sun setGoingNova:YES inTime:delay];
159
161}
162
163// cancelNova()
164static JSBool SunCancelNova(JSContext* context, uintN argc, jsval* vp)
165{
166 OOJS_NATIVE_ENTER(context)
167
168 OOSunEntity* sun = nil;
169
170 if (EXPECT_NOT(!JSSunGetSunEntity(context, OOJS_THIS, &sun)))
171 return NO;
172
173 if ([sun willGoNova] && ![sun goneNova]) {
174 [sun setGoingNova:NO inTime:0];
175 }
177
179}
#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
void InitOOJSSun(JSContext *context, JSObject *global)
Definition OOJSSun.m:79
static JSBool SunGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
Definition OOJSSun.m:106
@ kSun_name
Definition OOJSSun.m:58
@ kSun_radius
Definition OOJSSun.m:55
@ kSun_hasGoneNova
Definition OOJSSun.m:56
@ kSun_isGoingNova
Definition OOJSSun.m:57
static JSBool SunCancelNova(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSun.m:164
static JSClass sSunClass
Definition OOJSSun.m:38
static JSBool SunGoNova(JSContext *context, uintN argc, jsval *vp)
Definition OOJSSun.m:145
static JSObject * sSunPrototype
Definition OOJSSun.m:32
static JSFunctionSpec sSunMethods[]
Definition OOJSSun.m:70
static JSPropertySpec sSunProperties[]
Definition OOJSSun.m:61
#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)