Line data Source code
1 0 : /*
2 :
3 : OOJSSun.m
4 :
5 :
6 : Oolite
7 : Copyright (C) 2004-2013 Giles C Williams and contributors
8 :
9 : This program is free software; you can redistribute it and/or
10 : modify it under the terms of the GNU General Public License
11 : as published by the Free Software Foundation; either version 2
12 : of the License, or (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program; if not, write to the Free Software
21 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 : MA 02110-1301, USA.
23 :
24 : */
25 :
26 : #import "OOJSSun.h"
27 : #import "OOJSEntity.h"
28 : #import "OOJavaScriptEngine.h"
29 :
30 : #import "OOSunEntity.h"
31 :
32 :
33 0 : static JSObject *sSunPrototype;
34 :
35 :
36 : static JSBool SunGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
37 : static JSBool SunGoNova(JSContext *context, uintN argc, jsval *vp);
38 : static JSBool SunCancelNova(JSContext *context, uintN argc, jsval *vp);
39 :
40 :
41 0 : static 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
53 : OOJSObjectWrapperFinalize,// finalize
54 : JSCLASS_NO_OPTIONAL_MEMBERS
55 : };
56 :
57 :
58 0 : enum
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 :
68 0 : static JSPropertySpec sSunProperties[] =
69 : {
70 : // JS name ID flags
71 : { "hasGoneNova", kSun_hasGoneNova, OOJS_PROP_READONLY_CB },
72 : { "isGoingNova", kSun_isGoingNova, OOJS_PROP_READONLY_CB },
73 : { "name", kSun_name, OOJS_PROP_READONLY_CB },
74 : { "radius", kSun_radius, OOJS_PROP_READONLY_CB },
75 : { 0 }
76 : };
77 :
78 :
79 0 : static JSFunctionSpec sSunMethods[] =
80 : {
81 : // JS name Function min args
82 : { "cancelNova", SunCancelNova, 0 },
83 : { "goNova", SunGoNova, 1 },
84 : { 0 }
85 : };
86 :
87 :
88 : DEFINE_JS_OBJECT_GETTER(JSSunGetSunEntity, &sSunClass, sSunPrototype, OOSunEntity)
89 :
90 :
91 0 : void InitOOJSSun(JSContext *context, JSObject *global)
92 : {
93 : sSunPrototype = JS_InitClass(context, global, JSEntityPrototype(), &sSunClass, OOJSUnconstructableConstruct, 0, sSunProperties, sSunMethods, NULL, NULL);
94 : OOJSRegisterObjectConverter(&sSunClass, OOJSBasicPrivateObjectConverter);
95 : OOJSRegisterSubclass(&sSunClass, JSEntityClass());
96 : }
97 :
98 :
99 : @implementation OOSunEntity (OOJavaScriptExtensions)
100 :
101 0 : - (BOOL) isVisibleToScripts
102 : {
103 : return YES;
104 : }
105 :
106 :
107 0 : - (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
108 : {
109 : *outClass = &sSunClass;
110 : *outPrototype = sSunPrototype;
111 : }
112 :
113 :
114 0 : - (NSString *) oo_jsClassName
115 : {
116 : return @"Sun";
117 : }
118 :
119 : @end
120 :
121 :
122 0 : static 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 :
154 : OOJS_NATIVE_EXIT
155 : }
156 :
157 :
158 : // *** Methods ***
159 :
160 : // goNova([delay : Number])
161 0 : static 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];
172 : OOJS_RETURN_VOID;
173 :
174 : OOJS_NATIVE_EXIT
175 : }
176 :
177 :
178 : // cancelNova()
179 0 : static 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 : }
191 : OOJS_RETURN_VOID;
192 :
193 : OOJS_NATIVE_EXIT
194 : }
|