Oolite 1.91.0.7646-241128-10e222e
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 "WormholeEntity.h"
25#import "OOJSWormhole.h"
26#import "OOJSEntity.h"
27#import "OOJSVector.h"
31
32
33static JSObject *sWormholePrototype;
34
35static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *stationObj, WormholeEntity **outEntity);
36
37
38static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
39static JSBool WormholeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
40
41
42static 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
55 JSCLASS_NO_OPTIONAL_MEMBERS
56};
57
58
59enum
60{
61 // Property IDs
66
67};
68
69
70static JSPropertySpec sWormholeProperties[] =
71{
72 // JS name ID flags
77 { 0 }
78};
79
80
81static JSFunctionSpec sWormholeMethods[] =
82{
83 // JS name Function min args
84// { "", WormholeDoStuff, 0 },
85 { 0 }
86};
87
88
95
96
97static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *wormholeObj, WormholeEntity **outEntity)
98{
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
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
141static 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 {
156 return JS_NewNumberValue(context, [entity arrivalTime], value);
157
159 return JS_NewNumberValue(context, [entity destination], value);
160
162 return JS_NewNumberValue(context, [entity expiryTime], value);
163
164 case kWormhole_origin:
165 return JS_NewNumberValue(context, [entity origin], value);
166
167 default:
169 return NO;
170 }
171
172 *value = OOJSValueFromNativeObject(context, result);
173 return YES;
174
176}
177
178
179static 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:
195 return NO;
196 }
197
198 OOJSReportBadPropertyValue(context, this, propID, sWormholeProperties, *value);
199 return NO;
200
202}
203
204
205// *** 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)
@ kWormhole_destination
@ kWormhole_expiryTime
@ kWormhole_arrivalTime
@ kWormhole_origin
static JSPropertySpec sWormholeProperties[]
static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSObject * sWormholePrototype
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:140