Line data Source code
1 0 : /*
2 : OOJSWormhole.m
3 :
4 : Oolite
5 : Copyright (C) 2004-2013 Giles C Williams and contributors
6 :
7 : This program is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License
9 : as published by the Free Software Foundation; either version 2
10 : of the License, or (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 : MA 02110-1301, USA.
21 :
22 : */
23 :
24 : #import "WormholeEntity.h"
25 : #import "OOJSWormhole.h"
26 : #import "OOJSEntity.h"
27 : #import "OOJSVector.h"
28 : #import "OOJavaScriptEngine.h"
29 : #import "OOCollectionExtractors.h"
30 : #import "EntityOOJavaScriptExtensions.h"
31 :
32 :
33 0 : static JSObject *sWormholePrototype;
34 :
35 : static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *stationObj, WormholeEntity **outEntity);
36 :
37 :
38 : static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
39 : static JSBool WormholeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
40 :
41 :
42 0 : static JSClass sWormholeClass =
43 : {
44 : "Wormhole",
45 : JSCLASS_HAS_PRIVATE,
46 :
47 : JS_PropertyStub, // addProperty
48 : JS_PropertyStub, // delProperty
49 : WormholeGetProperty, // getProperty
50 : WormholeSetProperty, // setProperty
51 : JS_EnumerateStub, // enumerate
52 : JS_ResolveStub, // resolve
53 : JS_ConvertStub, // convert
54 : OOJSObjectWrapperFinalize,// finalize
55 : JSCLASS_NO_OPTIONAL_MEMBERS
56 : };
57 :
58 :
59 0 : enum
60 : {
61 : // Property IDs
62 : kWormhole_arrivalTime,
63 : kWormhole_destination,
64 : kWormhole_expiryTime,
65 : kWormhole_origin
66 :
67 : };
68 :
69 :
70 0 : static JSPropertySpec sWormholeProperties[] =
71 : {
72 : // JS name ID flags
73 : { "arrivalTime", kWormhole_arrivalTime, OOJS_PROP_READONLY_CB },
74 : { "destination", kWormhole_destination, OOJS_PROP_READONLY_CB },
75 : { "expiryTime", kWormhole_expiryTime, OOJS_PROP_READONLY_CB },
76 : { "origin", kWormhole_origin, OOJS_PROP_READONLY_CB },
77 : { 0 }
78 : };
79 :
80 :
81 0 : static JSFunctionSpec sWormholeMethods[] =
82 : {
83 : // JS name Function min args
84 : // { "", WormholeDoStuff, 0 },
85 : { 0 }
86 : };
87 :
88 :
89 0 : void InitOOJSWormhole(JSContext *context, JSObject *global)
90 : {
91 : sWormholePrototype = JS_InitClass(context, global, JSEntityPrototype(), &sWormholeClass, OOJSUnconstructableConstruct, 0, sWormholeProperties, sWormholeMethods, NULL, NULL);
92 : OOJSRegisterObjectConverter(&sWormholeClass, OOJSBasicPrivateObjectConverter);
93 : OOJSRegisterSubclass(&sWormholeClass, JSEntityClass());
94 : }
95 :
96 :
97 0 : static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *wormholeObj, WormholeEntity **outEntity)
98 : {
99 : OOJS_PROFILE_ENTER
100 :
101 : BOOL result;
102 : Entity *entity = nil;
103 :
104 : if (outEntity == NULL) return NO;
105 : *outEntity = nil;
106 :
107 : result = OOJSEntityGetEntity(context, wormholeObj, &entity);
108 : if (!result) return NO;
109 :
110 : if (![entity isKindOfClass:[WormholeEntity class]]) return NO;
111 :
112 : *outEntity = (WormholeEntity *)entity;
113 : return YES;
114 :
115 : OOJS_PROFILE_EXIT
116 : }
117 :
118 :
119 : @implementation WormholeEntity (OOJavaScriptExtensions)
120 :
121 : - (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
122 : {
123 : *outClass = &sWormholeClass;
124 : *outPrototype = sWormholePrototype;
125 : }
126 :
127 :
128 : - (NSString *) oo_jsClassName
129 : {
130 : return @"Wormhole";
131 : }
132 :
133 : - (BOOL) isVisibleToScripts
134 : {
135 : return YES;
136 : }
137 :
138 : @end
139 :
140 :
141 0 : static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
142 : {
143 : if (!JSID_IS_INT(propID)) return YES;
144 :
145 : OOJS_NATIVE_ENTER(context)
146 :
147 : WormholeEntity *entity = nil;
148 : id result = nil;
149 :
150 : if (!JSWormholeGetWormholeEntity(context, this, &entity)) return NO;
151 : if (entity == nil) { *value = JSVAL_VOID; return YES; }
152 :
153 : switch (JSID_TO_INT(propID))
154 : {
155 : case kWormhole_arrivalTime:
156 : return JS_NewNumberValue(context, [entity arrivalTime], value);
157 :
158 : case kWormhole_destination:
159 : return JS_NewNumberValue(context, [entity destination], value);
160 :
161 : case kWormhole_expiryTime:
162 : return JS_NewNumberValue(context, [entity expiryTime], value);
163 :
164 : case kWormhole_origin:
165 : return JS_NewNumberValue(context, [entity origin], value);
166 :
167 : default:
168 : OOJSReportBadPropertySelector(context, this, propID, sWormholeProperties);
169 : return NO;
170 : }
171 :
172 : *value = OOJSValueFromNativeObject(context, result);
173 : return YES;
174 :
175 : OOJS_NATIVE_EXIT
176 : }
177 :
178 :
179 0 : static JSBool WormholeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
180 : {
181 : if (!JSID_IS_INT(propID)) return YES;
182 :
183 : OOJS_NATIVE_ENTER(context)
184 :
185 : WormholeEntity *entity = nil;
186 :
187 : if (!JSWormholeGetWormholeEntity(context, this, &entity)) return NO;
188 : if (entity == nil) return YES;
189 :
190 : switch (JSID_TO_INT(propID))
191 : {
192 :
193 : default:
194 : OOJSReportBadPropertySelector(context, this, propID, sWormholeProperties);
195 : return NO;
196 : }
197 :
198 : OOJSReportBadPropertyValue(context, this, propID, sWormholeProperties, *value);
199 : return NO;
200 :
201 : OOJS_NATIVE_EXIT
202 : }
203 :
204 :
205 : // *** Methods ***
|