Oolite
Loading...
Searching...
No Matches
OOJSWormhole.m
Go to the documentation of this file.
1/*
2OOJSWormhole.m
3
4Oolite
5Copyright (C) 2004-2013 Giles C Williams and contributors
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; either version 2
10of the License, or (at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20MA 02110-1301, USA.
21
22 */
23
24#import "OOJSWormhole.h"
27#import "OOJSEntity.h"
28#import "OOJSVector.h"
30
31static JSObject* sWormholePrototype;
32
33static BOOL JSWormholeGetWormholeEntity(JSContext* context, JSObject* stationObj, WormholeEntity** outEntity);
34
35static JSBool WormholeGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value);
36static JSBool WormholeSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value);
37
38static JSClass sWormholeClass = {
39 "Wormhole",
40 JSCLASS_HAS_PRIVATE,
41
42 JS_PropertyStub, // addProperty
43 JS_PropertyStub, // delProperty
44 WormholeGetProperty, // getProperty
45 WormholeSetProperty, // 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
59
60};
61
62static JSPropertySpec sWormholeProperties[] = {
63 // JS name ID flags
68 { 0 }
69};
70
71static JSFunctionSpec sWormholeMethods[] = {
72 // JS name Function min args
73 // { "", WormholeDoStuff, 0 },
74 { 0 }
75};
76
83
84static BOOL JSWormholeGetWormholeEntity(JSContext* context, JSObject* wormholeObj, WormholeEntity** outEntity)
85{
87
88 BOOL result;
89 Entity* entity = nil;
90
91 if (outEntity == NULL)
92 return NO;
93 *outEntity = nil;
94
95 result = OOJSEntityGetEntity(context, wormholeObj, &entity);
96 if (!result)
97 return NO;
98
99 if (![entity isKindOfClass:[WormholeEntity class]])
100 return NO;
101
102 *outEntity = (WormholeEntity*)entity;
103 return YES;
104
106}
107
108@implementation WormholeEntity (OOJavaScriptExtensions)
109
110- (void)getJSClass:(JSClass**)outClass andPrototype:(JSObject**)outPrototype
111{
112 *outClass = &sWormholeClass;
113 *outPrototype = sWormholePrototype;
114}
115
116- (NSString*)oo_jsClassName
117{
118 return @"Wormhole";
119}
120
121- (BOOL)isVisibleToScripts
122{
123 return YES;
124}
125
126@end
127
128static JSBool WormholeGetProperty(JSContext* context, JSObject* this, jsid propID, jsval* value)
129{
130 if (!JSID_IS_INT(propID))
131 return YES;
132
133 OOJS_NATIVE_ENTER(context)
134
135 WormholeEntity* entity = nil;
136 id result = nil;
137
138 if (!JSWormholeGetWormholeEntity(context, this, &entity))
139 return NO;
140 if (entity == nil) {
141 *value = JSVAL_VOID;
142 return YES;
143 }
144
145 switch (JSID_TO_INT(propID)) {
147 return JS_NewNumberValue(context, [entity arrivalTime], value);
148
150 return JS_NewNumberValue(context, [entity destination], value);
151
153 return JS_NewNumberValue(context, [entity expiryTime], value);
154
155 case kWormhole_origin:
156 return JS_NewNumberValue(context, [entity origin], value);
157
158 default:
160 return NO;
161 }
162
163 *value = OOJSValueFromNativeObject(context, result);
164 return YES;
165
167}
168
169static JSBool WormholeSetProperty(JSContext* context, JSObject* this, jsid propID, JSBool strict, jsval* value)
170{
171 if (!JSID_IS_INT(propID))
172 return YES;
173
174 OOJS_NATIVE_ENTER(context)
175
176 WormholeEntity* entity = nil;
177
178 if (!JSWormholeGetWormholeEntity(context, this, &entity))
179 return NO;
180 if (entity == nil)
181 return YES;
182
183 switch (JSID_TO_INT(propID)) {
184
185 default:
187 return NO;
188 }
189
190 OOJSReportBadPropertyValue(context, this, propID, sWormholeProperties, *value);
191 return NO;
192
194}
195
196// *** Methods ***
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
OOINLINE JSClass * JSEntityClass(void)
Definition OOJSEntity.h:42
OOINLINE JSObject * JSEntityPrototype(void)
Definition OOJSEntity.h:43
static JSBool WormholeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
void InitOOJSWormhole(JSContext *context, JSObject *global)
static JSPropertySpec sWormholeProperties[]
static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSObject * sWormholePrototype
@ kWormhole_destination
@ kWormhole_expiryTime
@ kWormhole_arrivalTime
@ kWormhole_origin
static JSFunctionSpec sWormholeMethods[]
static JSClass sWormholeClass
static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *stationObj, WormholeEntity **outEntity)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
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)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
#define OOJS_PROP_READONLY_CB
return nil
voidpf uLong int origin
Definition ioapi.h:132