Better diagnostics for script failures during load, and some maths fiddling from my holiday.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@2925 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2010-01-01 16:34:52 +00:00
parent f502f30e66
commit be99b4b933
4 changed files with 68 additions and 28 deletions

View File

@ -38,14 +38,27 @@ typedef struct Vector
} Vector;
typedef struct Vector2D
{
OOScalar x;
OOScalar y;
} Vector2D;
extern const Vector kZeroVector, /* 0, 0, 0 */
kBasisXVector, /* 1, 0, 0 */
kBasisYVector, /* 0, 1, 0 */
kBasisZVector; /* 0, 0, 1 */
extern const Vector2D kZeroVector2D, /* 0, 0 */
kBasisXVector2D, /* 1, 0 */
kBasisYVector2D; /* 0, 1 */
/* Construct vector */
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC;
OOINLINE Vector2D MakeVector2D(OOScalar vx, OOScalar vy) INLINE_CONST_FUNC;
#if !OOMATHS_STANDALONE
/* Generate random vectors. */
@ -138,6 +151,15 @@ OOINLINE Vector make_vector (OOScalar vx, OOScalar vy, OOScalar vz)
}
OOINLINE Vector2D MakeVector2D(OOScalar vx, OOScalar vy)
{
Vector2D result;
result.x = vx;
result.y = vy;
return result;
}
OOINLINE void scale_vector(Vector *vec, OOScalar factor)
{
vec->x *= factor;

View File

@ -30,6 +30,10 @@ const Vector kBasisXVector = { 1.0f, 0.0f, 0.0f };
const Vector kBasisYVector = { 0.0f, 1.0f, 0.0f };
const Vector kBasisZVector = { 0.0f, 0.0f, 1.0f };
const Vector2D kZeroVector2D = { 0.0f, 0.0f };
const Vector2D kBasisXVector2D = { 1.0f, 0.0f };
const Vector2D kBasisYVector2D = { 0.0f, 1.0f };
#if !OOMATHS_STANDALONE
const BoundingBox kZeroBoundingBox = {{ 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }};
#endif

View File

@ -165,14 +165,32 @@ static JSFunctionSpec sScriptMethods[] =
}
}
/* Set initial name (in case of script error during initial run).
The "name" ivar is not set here, so the property can be fetched from JS
if we fail during setup. However, the "name" ivar is set later so that
the script object can't be renamed after the initial run. This could
probably also be achieved by fiddling with JS property attributes.
*/
[self setProperty:[self scriptNameFromPath:path] named:@"name"];
// Run the script (allowing it to set up the properties we need, as well as setting up those event handlers)
if (!problem)
{
// Push self on stack of running scripts.
RunningStack stackElement =
{
.back = sRunningStack,
.current = self
};
sRunningStack = &stackElement;
if (!JS_ExecuteScript(context, _jsSelf, script, &returnValue))
{
problem = @"could not run script";
}
sRunningStack = stackElement.back;
// We don't need the script any more - the event handlers hang around as long as the JS object exists.
JS_DestroyScript(context, script);
}
@ -180,6 +198,7 @@ static JSFunctionSpec sScriptMethods[] =
if (!problem)
{
// Get display attributes from script
DESTROY(name);
name = [[[self propertyNamed:@"name"] description] copy];
if (name == nil)
{
@ -261,6 +280,7 @@ static JSFunctionSpec sScriptMethods[] =
- (NSString *) name
{
if (name == nil) name = [[self propertyNamed:@"name"] copy];
return name;
}
@ -352,7 +372,6 @@ static JSFunctionSpec sScriptMethods[] =
JSFunction *function;
uintN i, argc;
jsval *argv = NULL;
RunningStack stackElement;
OOJavaScriptEngine *engine = nil;
JSContext *context = NULL;
@ -363,8 +382,11 @@ static JSFunctionSpec sScriptMethods[] =
if (function != NULL)
{
// Push self on stack of running scripts.
stackElement.back = sRunningStack;
stackElement.current = self;
RunningStack stackElement =
{
.back = sRunningStack,
.current = self
};
sRunningStack = &stackElement;
// Convert arguments to JS values and make them temporarily un-garbage-collectable.
@ -454,9 +476,9 @@ static JSFunctionSpec sScriptMethods[] =
{
jsval jsValue;
JSContext *context = NULL;
BOOL result = NO;
if (value == nil || propName == nil) return NO;
BOOL result = NO;
context = [[OOJavaScriptEngine sharedEngine] acquireContext];
jsValue = [value javaScriptValueInContext:context];

View File

@ -222,33 +222,25 @@ void make_pseudo_random_seed (Random_Seed *seed_ptr)
void rotate_seed (Random_Seed *seed_ptr)
{
unsigned int x;
unsigned int y;
uint_fast16_t x;
uint_fast16_t y;
x = seed_ptr->a + seed_ptr->c;
y = seed_ptr->b + seed_ptr->d;
if (x > 0xFF) y++;
x &= 0xFF;
y &= 0xFF;
/* Note: this is equivalent to adding three (little-endian) 16-bit values
together, rotating the three numbers and replacing one of them with
the sum. The byte-oriented approach is presumably because it was
reverse-engineered from eight-bit machine code. Switching to a plain
sixteen-bit representation is more trouble than it's worth since so
much code uses byte values from the seed struct directly.
*/
x = seed_ptr->a + seed_ptr->c + seed_ptr->e;
y = seed_ptr->b + seed_ptr->d + seed_ptr->f;
seed_ptr->a = seed_ptr->c;
seed_ptr->b = seed_ptr->d;
seed_ptr->c = seed_ptr->e;
seed_ptr->d = seed_ptr->f;
x += seed_ptr->c;
y += seed_ptr->d;
if (x > 0xFF)
y++;
x &= 0xFF;
y &= 0xFF;
seed_ptr->e = x;
seed_ptr->f = y;
seed_ptr->f = y + (x >> 8);
}