diff --git a/libraries/AP_Scripting/AP_Scripting.cpp b/libraries/AP_Scripting/AP_Scripting.cpp index c7d3d5cf16..580760fd9f 100644 --- a/libraries/AP_Scripting/AP_Scripting.cpp +++ b/libraries/AP_Scripting/AP_Scripting.cpp @@ -87,7 +87,7 @@ bool AP_Scripting::init(void) { if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&AP_Scripting::thread, void), "Scripting", SCRIPTING_STACK_SIZE, AP_HAL::Scheduler::PRIORITY_SCRIPTING, 0)) { gcs().send_text(MAV_SEVERITY_CRITICAL, "Could not create scripting stack (%d)", SCRIPTING_STACK_SIZE); - _enable = 0; + _init_failed = true; return false; } @@ -96,8 +96,9 @@ bool AP_Scripting::init(void) { void AP_Scripting::thread(void) { lua_scripts *lua = new lua_scripts(_script_vm_exec_count, _script_heap_size, _debug_level); - if (lua == nullptr) { + if (lua == nullptr || !lua->heap_allocated()) { gcs().send_text(MAV_SEVERITY_CRITICAL, "Unable to allocate scripting memory"); + _init_failed = true; return; } lua->run(); diff --git a/libraries/AP_Scripting/AP_Scripting.h b/libraries/AP_Scripting/AP_Scripting.h index 7044307ae9..ee2e1a9d0a 100644 --- a/libraries/AP_Scripting/AP_Scripting.h +++ b/libraries/AP_Scripting/AP_Scripting.h @@ -29,6 +29,7 @@ public: AP_Scripting &operator=(const AP_Scripting&) = delete; bool init(void); + bool init_failed(void) const { return _init_failed; } bool enabled(void) const { return _enable != 0; }; @@ -46,6 +47,8 @@ private: AP_Int32 _script_heap_size; AP_Int8 _debug_level; + bool _init_failed; // true if memory allocation failed + static AP_Scripting *_singleton; }; diff --git a/libraries/AP_Scripting/lua_scripts.h b/libraries/AP_Scripting/lua_scripts.h index b3ac68eaac..361fa09f32 100644 --- a/libraries/AP_Scripting/lua_scripts.h +++ b/libraries/AP_Scripting/lua_scripts.h @@ -30,6 +30,9 @@ public: lua_scripts(const lua_scripts &other) = delete; lua_scripts &operator=(const lua_scripts&) = delete; + // return true if initialisation failed + bool heap_allocated() const { return _heap != nullptr; } + // run scripts, does not return unless an error occured void run(void);