World model is now operational

This commit is contained in:
2021-01-16 01:24:33 -05:00
parent d7d633bd96
commit 313e78067a
11 changed files with 313 additions and 188 deletions

View File

@@ -1,6 +1,6 @@
////////////////////////////////////////////////////////////
//
// SOURCE
// SOURCEDB
//
// This module manages the loading of lua source files into the Lua environment.
// Since the source files can be reloaded over and over, this module doesn't
@@ -9,7 +9,6 @@
// that should be there. That way, if you delete something from a lua source
// file, it gets removed from the lua global environment.
//
//
// THE MAKECLASS OPERATOR
//
// This module provides a new lua 'builtin' operator: "makeclass". This creates
@@ -31,12 +30,15 @@
//
// THE LUA SOURCE DATABASE
//
// The function 'source_update' loads the lua source code from disk, and stores
// it in the "source database." The source database is a table inside the lua
// registry.
// Class SourceDB only contains a single pointer to the lua environment. That's
// because all the data for SourceDB is stored in the lua registry.
// Specifically, the registry contains these keys:
//
// The source database is a lua table where the keys are filenames, and the
// values are information about that file:
// 1. The source database proper (registry key "sourcedb")
// 2. The snapshot of builtins (registry key "source_snapshot_builtins")
//
// The source database proper is a table where the keys are filenames, and the
// values are records containing information about that file:
//
// "foo.lua" : {
// "name": "foo.lua",
@@ -55,23 +57,24 @@
// error message. Finally, "sequence" indicates the order in which the source
// files are meant to be loaded.
//
// The operation "source_update" refreshes the source database from disk. It
// The operation SourceDB::update refreshes the source database from disk. It
// doesn't reread files whose fingerprints have not changed. In a synchronous
// model, we don't call source_update - instead, we update the source database
// by difference transmission.
// model, we don't call SourceDB::update - instead, we update the source
// database by difference transmission.
//
// Note that updating the source database has *no effect* on the lua global
// variables (or lua function definitions). The source_update operation calls
// lua's "load" on the code, but all that does is return a loaded closure.
// variables (or lua function definitions). The SourceDB::update operation
// calls lua's "load" on the code, but all that does is return a loaded closure.
// Nothing happens to the lua invironment until you *invoke* the loaded closure.
// That doesn't happen until you do a source_rebuild operation, described below.
// That doesn't happen until you do a SourceDB::rebuild operation, described
// below.
//
//
// SOURCE REBUILDS, IN DEPTH
//
// The function source_rebuild clears and then rebuilds the entire contents of
// the global environment. The reason to clear the environment is that if we
// didn't clear it, then removing a function from the lua source would not
// The function SourceDB::rebuild clears and then rebuilds the entire contents
// of the lua global environment. The reason to clear the environment is that
// if we didn't clear it, then removing a function from the lua source would not
// remove it from the lua environment. Rebuilding the lua environment is a
// multi-step process:
//
@@ -83,7 +86,7 @@
//
// * Lua Builtin functions are reinstalled in the global environment.
//
// - To make this possible, a snapshot of these builtins is kept.
// - To make this possible, the snapshot of builtins is used.
//
// * C++ functions registered with "LuaDefine" are reinstalled.
//
@@ -102,15 +105,15 @@
// UNITTESTS
//
// We reserve the lua class name 'unittests' for storing unit tests. Any
// function placed into this class is considered a unit test. We don't
// separate unit tests from the rest of the code - they're compiled right
// into the main binary.
// function placed into this class is considered a unit test. We don't separate
// unit tests from the rest of the code - they're compiled right into the main
// binary.
//
// Unit tests can be either lua functions, or Lua-registered C functions.
// Unit tests are executed by calling 'source_run_unittests'.
// Unit tests can be either lua functions, or Lua-registered C functions. Unit
// tests are executed by calling 'source_run_unittests'.
//
// Each unit test is run in a protected 'pcall' environment. Any errors
// are printed out to console. (At least for now).
// Each unit test is run in a protected 'pcall' environment. Any errors are
// printed out to console. (At least for now).
//
////////////////////////////////////////////////////////////
@@ -119,6 +122,46 @@
#include "luastack.hpp"
class SourceDB {
private:
lua_State *lua_state_;
public:
// Initialize
//
// This must be called while the lua state is still pristine.
// This will install the builtin operators into the lua state
// and will snapshot those builtins to the registry.
//
void initialize(lua_State *L);
// Update
//
// Read all the lua source files from disk and store them in the
// source database. Also compiles these files using lua's "load"
// function. Efficient: if a source file is already in the database
// and hasn't been modified, it is not reloaded.
//
void update();
// Rebuild
//
// Rebuild the lua environment: clear it out, then reinstall all the
// functions that should be there. See above for more information.
// If an error exists in any of the source files, or when loading any
// of the closures, the error is (currently) printed. We'll come up
// with better error handling later.
//
void rebuild();
// run_unittests
//
// Run all the unit tests. Print any errors to console. If there
// are any errors, exits the program.
//
void run_unittests();
};
// The Lua 'makeclass' operator.
//
// Creates a table in the global environment with the specified name.
@@ -128,41 +171,6 @@
//
int source_makeclass(lua_State *L);
// source_install_and_snapshot_builtins.
//
// Install the lua builtins into a brand new lua state, and then create the
// snapshot of the builtins (in the registry). This uses lua_openlibs, which
// only works if the lua environment is brand new. Do not try to use
// source_snapshot_builtins unless the lua environment has just been created
// using lua_newstate!
//
int source_install_and_snapshot_builtins(lua_State *L);
// source_update
//
// Read all the lua source files from disk and store them in the
// source database. Also compiles these files using lua's "load"
// function. Efficient: if a source file is already in the database
// and hasn't been modified, it is not reloaded.
//
int source_update(lua_State *L);
// source_rebuild
//
// Rebuild the lua environment: clear it out, then reinstall all the
// functions that should be there. See above for more information.
// If an error exists in any of the source files, or when loading any
// of the closures, the error is (currently) printed. We'll come up
// with better error handling later.
//
int source_rebuild(lua_State *L);
// source_run_unittests
//
// Run all the unit tests. Print any errors to console. Pushes zero
// onto the lua stack if there are no errors, or one if there are.
//
int source_run_unittests(lua_State *L);
#endif // SOURCE_HPP