Add read-only representation for wormholes (issue #34)

wormhole.arrivalTime
wormhole.expiryTime
wormhole.destination
wormhole.origin
entity.isWormhole
This commit is contained in:
cim 2013-06-30 10:22:15 +01:00
parent 1c793e906a
commit bc97187a11
6 changed files with 265 additions and 0 deletions

View File

@ -298,6 +298,7 @@ OOLITE_SCRIPTING_FILES = \
OOJSVisualEffect.m \
OOJSVector.m \
OOJSWorldScripts.m \
OOJSWormhole.m \
OOLegacyScriptWhitelist.m \
OOPListScript.m \
OOScript.m \

View File

@ -33,6 +33,8 @@ MA 02110-1301, USA.
#import "OOPlanetEntity.h"
#import "OOVisualEffectEntity.h"
#import "OOJSVisualEffect.h"
#import "WormholeEntity.h"
#import "OOJSWormhole.h"
@implementation Entity (OOJavaScriptExtensions)

View File

@ -88,6 +88,7 @@ enum
kEntity_isInSpace, // is in space, boolean, read-only.
kEntity_isVisible, // is within drawing distance, boolean, read-only.
kEntity_isVisualEffect, // is visual effect, boolean, read-only.
kEntity_isWormhole, // is visual effect, boolean, read-only.
};
@ -117,6 +118,7 @@ static JSPropertySpec sEntityProperties[] =
{ "isInSpace", kEntity_isInSpace, OOJS_PROP_READONLY_CB },
{ "isVisible", kEntity_isVisible, OOJS_PROP_READONLY_CB },
{ "isVisualEffect", kEntity_isVisualEffect, OOJS_PROP_READONLY_CB },
{ "isWormhole", kEntity_isWormhole, OOJS_PROP_READONLY_CB },
{ 0 }
};
@ -289,6 +291,10 @@ static JSBool EntityGetProperty(JSContext *context, JSObject *this, jsid propID,
case kEntity_isVisualEffect:
*value = OOJSValueFromBOOL([entity isVisualEffect]);
return YES;
case kEntity_isWormhole:
*value = OOJSValueFromBOOL([entity isWormhole]);
return YES;
case kEntity_distanceTravelled:
return JS_NewNumberValue(context, [entity distanceTravelled], value);

View File

@ -0,0 +1,41 @@
/*
OOJSWormhole.h
JavaScript proxy for WormholeEntities.
Oolite
Copyright (C) 2004-2013 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import <Foundation/Foundation.h>
#include <jsapi.h>
@class OOWormholeEntity;
void InitOOJSWormhole(JSContext *context, JSObject *global);
@interface WormholeEntity (OOJavaScriptExtensions)
- (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype;
- (NSString *) oo_jsClassName;
- (BOOL) isVisibleToScripts;
@end

View File

@ -0,0 +1,213 @@
/*
OOJSWormhole.m
Oolite
Copyright (C) 2004-2013 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import "WormholeEntity.h"
#import "OOJSWormhole.h"
#import "OOJSEntity.h"
#import "OOJSVector.h"
#import "OOJavaScriptEngine.h"
#import "OOCollectionExtractors.h"
#import "EntityOOJavaScriptExtensions.h"
static JSObject *sWormholePrototype;
static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *stationObj, WormholeEntity **outEntity);
static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value);
static JSBool WormholeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value);
static JSClass sWormholeClass =
{
"Wormhole",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, // addProperty
JS_PropertyStub, // delProperty
WormholeGetProperty, // getProperty
WormholeSetProperty, // setProperty
JS_EnumerateStub, // enumerate
JS_ResolveStub, // resolve
JS_ConvertStub, // convert
OOJSObjectWrapperFinalize,// finalize
JSCLASS_NO_OPTIONAL_MEMBERS
};
enum
{
// Property IDs
kWormhole_arrivalTime,
kWormhole_destination,
kWormhole_expiryTime,
kWormhole_origin
};
static JSPropertySpec sWormholeProperties[] =
{
// JS name ID flags
{ "arrivalTime", kWormhole_arrivalTime, OOJS_PROP_READONLY_CB },
{ "destination", kWormhole_destination, OOJS_PROP_READONLY_CB },
{ "expiryTime", kWormhole_expiryTime, OOJS_PROP_READONLY_CB },
{ "origin", kWormhole_origin, OOJS_PROP_READONLY_CB },
{ 0 }
};
static JSFunctionSpec sWormholeMethods[] =
{
// JS name Function min args
// { "", WormholeDoStuff, 0 },
{ 0 }
};
void InitOOJSWormhole(JSContext *context, JSObject *global)
{
sWormholePrototype = JS_InitClass(context, global, JSEntityPrototype(), &sWormholeClass, OOJSUnconstructableConstruct, 0, sWormholeProperties, sWormholeMethods, NULL, NULL);
OOJSRegisterObjectConverter(&sWormholeClass, OOJSBasicPrivateObjectConverter);
OOJSRegisterSubclass(&sWormholeClass, JSEntityClass());
}
static BOOL JSWormholeGetWormholeEntity(JSContext *context, JSObject *wormholeObj, WormholeEntity **outEntity)
{
OOJS_PROFILE_ENTER
BOOL result;
Entity *entity = nil;
if (outEntity == NULL) return NO;
*outEntity = nil;
result = OOJSEntityGetEntity(context, wormholeObj, &entity);
if (!result) return NO;
if (![entity isKindOfClass:[WormholeEntity class]]) return NO;
*outEntity = (WormholeEntity *)entity;
return YES;
OOJS_PROFILE_EXIT
}
@implementation WormholeEntity (OOJavaScriptExtensions)
- (void)getJSClass:(JSClass **)outClass andPrototype:(JSObject **)outPrototype
{
*outClass = &sWormholeClass;
*outPrototype = sWormholePrototype;
}
- (NSString *) oo_jsClassName
{
return @"Wormhole";
}
- (BOOL) isVisibleToScripts
{
return YES;
}
@end
static JSBool WormholeGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
{
if (!JSID_IS_INT(propID)) return YES;
OOJS_NATIVE_ENTER(context)
WormholeEntity *entity = nil;
id result = nil;
if (!JSWormholeGetWormholeEntity(context, this, &entity)) return NO;
if (entity == nil) { *value = JSVAL_VOID; return YES; }
switch (JSID_TO_INT(propID))
{
case kWormhole_arrivalTime:
return JS_NewNumberValue(context, [entity arrivalTime], value);
case kWormhole_destination:
return JS_NewNumberValue(context, [UNIVERSE systemIDForSystemSeed:[entity destination]], value);
case kWormhole_expiryTime:
return JS_NewNumberValue(context, [entity expiryTime], value);
case kWormhole_origin:
return JS_NewNumberValue(context, [UNIVERSE systemIDForSystemSeed:[entity origin]], value);
default:
OOJSReportBadPropertySelector(context, this, propID, sWormholeProperties);
return NO;
}
*value = OOJSValueFromNativeObject(context, result);
return YES;
OOJS_NATIVE_EXIT
}
static JSBool WormholeSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
{
if (!JSID_IS_INT(propID)) return YES;
OOJS_NATIVE_ENTER(context)
WormholeEntity *entity = nil;
if (!JSWormholeGetWormholeEntity(context, this, &entity)) return NO;
if (entity == nil) return YES;
switch (JSID_TO_INT(propID))
{
default:
OOJSReportBadPropertySelector(context, this, propID, sWormholeProperties);
return NO;
}
OOJSReportBadPropertyValue(context, this, propID, sWormholeProperties, *value);
return NO;
OOJS_NATIVE_EXIT
}
// *** Methods ***
#define GET_THIS_WORMHOLE(THISENT) do { \
if (EXPECT_NOT(!JSWormholeGetWormholeEntity(context, OOJS_THIS, &THISENT))) return NO; /* Exception */ \
if (OOIsStaleEntity(THISENT)) OOJS_RETURN_VOID; \
} while (0)

View File

@ -48,6 +48,7 @@ MA 02110-1301, USA.
#import "OOJSStation.h"
#import "OOJSDock.h"
#import "OOJSVisualEffect.h"
#import "OOJSWormhole.h"
#import "OOJSPlayer.h"
#import "OOJSPlayerShip.h"
#import "OOJSManifest.h"
@ -349,6 +350,7 @@ static void ReportJSError(JSContext *context, const char *message, JSErrorReport
InitOOJSStation(gOOJSMainThreadContext, _globalObject);
InitOOJSDock(gOOJSMainThreadContext, _globalObject);
InitOOJSVisualEffect(gOOJSMainThreadContext, _globalObject);
InitOOJSWormhole(gOOJSMainThreadContext, _globalObject);
InitOOJSPlayer(gOOJSMainThreadContext, _globalObject);
InitOOJSPlayerShip(gOOJSMainThreadContext, _globalObject);
InitOOJSManifest(gOOJSMainThreadContext, _globalObject);