Oolite
Loading...
Searching...
No Matches
OOJSWormhole.m
Go to the documentation of this file.
1/*
2OOJSWormhole.m (QuickJS-ng)
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#import "WormholeEntity.h"
31
32static JSClassID sWormholeClassID;
33
34static BOOL JSWormholeGetWormholeEntity(JSContext* context, JSValueConst wormholeObj, WormholeEntity** outEntity);
35
37
38static JSClassDef sWormholeClass = {
39 .class_name = "Wormhole",
40 .finalizer = OOJSObjectWrapperFinalize,
41};
42
43enum {
44 // Property IDs
49
50};
51
52/* All Wormhole properties are read-only, so the SpiderMonkey setProperty hook
53 (which only reported a bad property selector) was unreachable for them
54 and is not ported.
55*/
56static const JSCFunctionListEntry sWormholeProperties[] = {
57 // JS name, getter, setter, ID
62};
63
64// No Wormhole methods.
65
66void InitOOJSWormhole(JSContext* context, JSValueConst global)
67{
69
70 JSValue parentPrototype = JS_GetClassProto(context, JSEntityClassID());
71 NSCAssert(JS_IsObject(parentPrototype), @"Entity must be initialized before Wormhole.");
72 OOJSInitClass(context, global, sWormholeClassID, parentPrototype, OOJSUnconstructableConstruct, 0,
74 JS_FreeValue(context, parentPrototype);
75
78}
79
80JSClassID JSWormholeClassID(void)
81{
82 return sWormholeClassID;
83}
84
85static BOOL JSWormholeGetWormholeEntity(JSContext* context, JSValueConst wormholeObj, WormholeEntity** outEntity)
86{
88
89 BOOL result;
90 Entity* entity = nil;
91
92 if (outEntity == NULL)
93 return NO;
94 *outEntity = nil;
95
96 result = OOJSEntityGetEntity(context, wormholeObj, &entity);
97 if (!result)
98 return NO;
99
100 if (![entity isKindOfClass:[WormholeEntity class]])
101 return NO;
102
103 *outEntity = (WormholeEntity*)entity;
104 return YES;
105
107}
108
109@implementation WormholeEntity (OOJavaScriptExtensions)
110
111- (JSClassID)oo_jsClassID
112{
113 return sWormholeClassID;
114}
115
116- (NSString*)oo_jsClassName
117{
118 return @"Wormhole";
119}
120
122{
123 return YES;
124}
125
126@end
127
129{
130 OOJS_NATIVE_ENTER(context)
131
132 WormholeEntity* entity = nil;
133 id result = nil;
134
135 if (!JSWormholeGetWormholeEntity(context, this, &entity))
136 return OOJSNativeFailure(context);
137 if (entity == nil) {
138 return JS_UNDEFINED;
139 }
140
141 switch (magic) {
143 return JS_NewNumber(context, [entity arrivalTime]);
144
146 return JS_NewNumber(context, [entity destination]);
147
149 return JS_NewNumber(context, [entity expiryTime]);
150
151 case kWormhole_origin:
152 return JS_NewNumber(context, [entity origin]);
153
154 default:
156 return OOJSNativeFailure(context);
157 }
158
159 return OOJSValueFromNativeObject(context, result);
160
162}
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
void InitOOJSWormhole(JSContext *context, JSObject *global)
static JSPropertySpec sWormholeProperties[]
static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
@ kWormhole_destination
@ kWormhole_expiryTime
@ kWormhole_arrivalTime
@ kWormhole_origin
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)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
return nil
JSClassID JSEntityClassID(void)
Definition OOJSEntity.m:115
static JSClassID sWormholeClassID
JSClassID JSWormholeClassID(void)
JSValue OOJSNativeFailure(JSContext *context)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
#define OOJS_PROPERTY_RO(name, getter, magic)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_GETTER_ARGS
#define OOJS_COUNT(array)
voidpf uLong int origin
Definition ioapi.h:132