From be99b4b9330a9f63b6d1ba09fababf8636982728 Mon Sep 17 00:00:00 2001 From: Jens Ayton Date: Fri, 1 Jan 2010 16:34:52 +0000 Subject: [PATCH] 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 --- src/Core/OOVector.h | 22 ++++++++++++++++++ src/Core/OOVector.m | 4 ++++ src/Core/Scripting/OOJSScript.m | 30 +++++++++++++++++++++---- src/Core/legacy_random.c | 40 +++++++++++++-------------------- 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/src/Core/OOVector.h b/src/Core/OOVector.h index 41173ddf..00211e48 100644 --- a/src/Core/OOVector.h +++ b/src/Core/OOVector.h @@ -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; diff --git a/src/Core/OOVector.m b/src/Core/OOVector.m index 9d2be99f..4b858805 100644 --- a/src/Core/OOVector.m +++ b/src/Core/OOVector.m @@ -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 diff --git a/src/Core/Scripting/OOJSScript.m b/src/Core/Scripting/OOJSScript.m index 8f9dbf0c..bf7049f8 100644 --- a/src/Core/Scripting/OOJSScript.m +++ b/src/Core/Scripting/OOJSScript.m @@ -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]; diff --git a/src/Core/legacy_random.c b/src/Core/legacy_random.c index 298b7d70..f7af8c34 100644 --- a/src/Core/legacy_random.c +++ b/src/Core/legacy_random.c @@ -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; - - x = seed_ptr->a + seed_ptr->c; - y = seed_ptr->b + seed_ptr->d; - - - if (x > 0xFF) y++; - - x &= 0xFF; - y &= 0xFF; - + uint_fast16_t x; + uint_fast16_t y; + + /* 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); }