diff --git a/src/Scripting/NasalPositioned.cxx b/src/Scripting/NasalPositioned.cxx index 2f00eed4e..e4227950f 100644 --- a/src/Scripting/NasalPositioned.cxx +++ b/src/Scripting/NasalPositioned.cxx @@ -718,8 +718,8 @@ static void flightplanGhostSetMember(naContext c, void* g, naRef field, naRef va fp->setIdent(naStr_data(value)); } else if (!strcmp(fieldName, "current")) { int index = static_cast(value.num); - if ((index < 0) || (index >= fp->numLegs())) { - return; + if ((index < -1) || (index >= fp->numLegs())) { + naRuntimeError(c, "flightplan.current must be a valid index or -1"); } fp->setCurrentIndex(index); } else if (!strcmp(fieldName, "departure")) { @@ -2158,7 +2158,9 @@ public: _plan(fp), _instance(ins) { - _gcSaveKey = _nasal->gcSave(ins); + assert(fp); + assert(sys); + _gcSaveKey = _nasal->gcSave(ins); } ~NasalFPDelegate() override diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index d8534e483..c888837a3 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -283,12 +283,23 @@ naRef FGNasalSys::callWithContext(naContext ctx, naRef code, int argc, naRef* ar naRef FGNasalSys::callMethod(naRef code, naRef self, int argc, naRef* args, naRef locals) { - return naCallMethod(code, self, argc, args, locals); + try { + return naCallMethod(code, self, argc, args, locals); + } catch (sg_exception& e) { + SG_LOG(SG_NASAL, SG_DEV_ALERT, "caught exception invoking nasal method:" << e.what()); + return naNil(); + } } naRef FGNasalSys::callMethodWithContext(naContext ctx, naRef code, naRef self, int argc, naRef* args, naRef locals) { - return naCallMethodCtx(ctx, code, self, argc, args, locals); + try { + return naCallMethodCtx(ctx, code, self, argc, args, locals); + } catch (sg_exception& e) { + SG_LOG(SG_NASAL, SG_DEV_ALERT, "caught exception invoking nasal method:" << e.what()); + logNasalStack(ctx); + return naNil(); + } } FGNasalSys::~FGNasalSys() @@ -296,7 +307,7 @@ FGNasalSys::~FGNasalSys() if (_inited) { SG_LOG(SG_GENERAL, SG_ALERT, "Nasal was not shutdown"); } - nasalSys = 0; + nasalSys = nullptr; } bool FGNasalSys::parseAndRunWithOutput(const std::string& source, std::string& output, std::string& errors) @@ -1189,16 +1200,23 @@ void FGNasalSys::loadPropertyScripts(SGPropertyNode* n) void FGNasalSys::logError(naContext context) { SG_LOG(SG_NASAL, SG_ALERT, "Nasal runtime error: " << naGetError(context)); - int stack_depth = naStackDepth(context); - if( stack_depth < 1 ) + logNasalStack(context); +} + +void FGNasalSys::logNasalStack(naContext context) +{ + const int stack_depth = naStackDepth(context); + if (stack_depth < 1) return; + SG_LOG(SG_NASAL, SG_ALERT, " at " << naStr_data(naGetSourceFile(context, 0)) << ", line " << naGetLine(context, 0)); - for(int i=1; i