Oops, I did it again.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@1216 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2007-10-15 14:26:07 +00:00
parent 60f34500d7
commit 359ebaf43b
2 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/*
OOJSSun.h
JavaScript proxy for suns (currently PlanetEntities of type PLANET_TYPE_SUN,
expected to become a separate entity class in future).
Oolite
Copyright (C) 2004-2007 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>
#import <jsapi.h>
void InitOOJSSun(JSContext *context, JSObject *global);
void OOSunGetClassAndPrototype(JSClass **outClass, JSObject **outPrototype);

View File

@ -0,0 +1,136 @@
/*
OOJSSun.m
Oolite
Copyright (C) 2004-2007 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 "OOJSSun.h"
#import "OOJSEntity.h"
#import "OOJavaScriptEngine.h"
#import "PlanetEntity.h"
static JSObject *sSunPrototype;
static BOOL JSSunGetPlanetEntity(JSContext *context, JSObject *SunObj, PlanetEntity **outEntity);
static JSBool SunGetProperty(JSContext *context, JSObject *this, jsval name, jsval *outValue);
static JSExtendedClass sSunClass =
{
{
"Sun",
JSCLASS_HAS_PRIVATE | JSCLASS_IS_EXTENDED,
JS_PropertyStub, // addProperty
JS_PropertyStub, // delProperty
SunGetProperty, // getProperty
JS_PropertyStub, // setProperty
JS_EnumerateStub, // enumerate
JS_ResolveStub, // resolve
JS_ConvertStub, // convert
JSObjectWrapperFinalize,// finalize
JSCLASS_NO_OPTIONAL_MEMBERS
},
JSEntityEquality, // equality
NULL, // outerObject
NULL, // innerObject
JSCLASS_NO_RESERVED_MEMBERS
};
enum
{
// Property IDs
kSun_radius, // Radius of sun in metres.
};
static JSPropertySpec sSunProperties[] =
{
// JS name ID flags
{ "radius", kSun_radius, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ 0 }
};
static JSFunctionSpec sSunMethods[] =
{
// JS name Function min args
{ 0 }
};
void InitOOJSSun(JSContext *context, JSObject *global)
{
sSunPrototype = JS_InitClass(context, global, JSEntityPrototype(), &sSunClass.base, NULL, 0, sSunProperties, sSunMethods, NULL, NULL);
JSRegisterObjectConverter(&sSunClass.base, JSBasicPrivateObjectConverter);
}
void OOSunGetClassAndPrototype(JSClass **outClass, JSObject **outPrototype)
{
*outClass = &sSunClass.base;
*outPrototype = sSunPrototype;
}
static BOOL JSSunGetPlanetEntity(JSContext *context, JSObject *stationObj, PlanetEntity **outEntity)
{
BOOL result;
Entity *entity = nil;
if (outEntity != NULL) *outEntity = nil;
result = JSEntityGetEntity(context, stationObj, &entity);
if (!result) return NO;
if (![entity isKindOfClass:[PlanetEntity class]]) return NO;
*outEntity = (PlanetEntity *)entity;
return YES;
}
static JSBool SunGetProperty(JSContext *context, JSObject *this, jsval name, jsval *outValue)
{
PlanetEntity *sun = nil;
if (!JSVAL_IS_INT(name)) return YES;
if (!JSSunGetPlanetEntity(context, this, &sun)) return NO;
switch (JSVAL_TO_INT(name))
{
case kSun_radius:
JS_NewDoubleValue(context, [sun radius], outValue);
break;
default:
OOReportJavaScriptBadPropertySelector(context, @"Sun", JSVAL_TO_INT(name));
return NO;
}
return YES;
}