MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / load_script

Method load_script

libraries/AP_Scripting/lua_scripts.cpp:169–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

167}
168
169lua_scripts::script_info *lua_scripts::load_script(lua_State *L, char *filename) {
170 if (int error = luaL_loadfile(L, filename)) {
171 switch (error) {
172 case LUA_ERRSYNTAX:
173 set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Error: %s", get_error_object_message(L));
174 lua_pop(L, lua_gettop(L));
175 return nullptr;
176 case LUA_ERRMEM:
177 set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Insufficent memory loading %s", filename);
178 lua_pop(L, lua_gettop(L));
179 return nullptr;
180 case LUA_ERRFILE:
181 set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Unable to load the file: %s", get_error_object_message(L));
182 lua_pop(L, lua_gettop(L));
183 return nullptr;
184 default:
185 set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Unknown error (%d) loading %s", error, filename);
186 lua_pop(L, lua_gettop(L));
187 return nullptr;
188 }
189 }
190
191 const int loadMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
192 const uint32_t loadStart = AP_HAL::micros();
193
194 script_info *new_script = (script_info *)_heap.allocate(sizeof(script_info));
195 if (new_script == nullptr) {
196 // No memory, shouldn't happen, we even attempted to do a GC
197 set_and_print_new_error_message(MAV_SEVERITY_CRITICAL, "Insufficent memory loading %s", filename);
198 lua_pop(L, 1); // we can't use the function we just loaded, so ditch it
199 return nullptr;
200 }
201
202
203 create_sandbox(L);
204 lua_pushvalue(L, -1); // duplicate environment for reference below
205 lua_setupvalue(L, -3, 1);
206
207 const uint32_t loadEnd = AP_HAL::micros();
208 const int endMem = lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
209
210 update_stats(filename, loadEnd-loadStart, endMem, loadMem);
211
212 new_script->name = filename;
213 new_script->env_ref = luaL_ref(L, LUA_REGISTRYINDEX); // store reference to script's environment
214 new_script->run_ref = luaL_ref(L, LUA_REGISTRYINDEX); // store reference to function to run
215 new_script->next_run_ms = AP_HAL::millis64() - 1; // force the script to be stale
216
217 // Get checksum of file
218 uint32_t crc = 0;
219 if (AP::FS().crc32(filename, crc)) {
220 // Record crc of this script
221 new_script->crc = crc;
222 {
223 // Apply crc to checksum of all scripts
224 WITH_SEMAPHORE(crc_sem);
225 loaded_checksum ^= crc;
226 running_checksum ^= crc;

Callers

nothing calls this directly

Calls 10

get_error_object_messageFunction · 0.85
lua_gettopFunction · 0.85
lua_gcFunction · 0.85
lua_pushvalueFunction · 0.85
lua_setupvalueFunction · 0.85
luaL_refFunction · 0.85
crc32Method · 0.80
microsFunction · 0.50
millis64Function · 0.50
allocateMethod · 0.45

Tested by

no test coverage detected