Unverified Commit 32315e22 authored by RobLoach's avatar RobLoach Committed by GitHub
Browse files

Merge pull request #308 from libretro/require

Add require()
parents 07aa3f1f a63d637c
#ifndef SRC_CHAILOVEDOCS_H_
#define SRC_CHAILOVEDOCS_H_
#include <string>
/**
* Provides a Linked List data structure.
*
......@@ -50,4 +52,25 @@ class List {
void pop_back();
};
/**
* Loads the given module, only once.
*
* While love.filesystem.load will load and execute a module any number of times, the `require()` function will only load and execute any given module just one.
*
* @param module The name of the module to load. "." will be replaced by "/".
*
* @return True if the module was loaded.
*
* @see love.filesystem.load
*
* @code
* // The following will load lib/player.chai.
* require("lib.player")
*
* // Calling require("lib.player") again will not re-load it.
* require("lib.player")
* @endcode
*/
bool require(const std::string& module);
#endif // SRC_CHAILOVEDOCS_H_
#include "script.h"
#include "../ChaiLove.h"
#include <filesystem/path.h>
#include <algorithm>
#ifdef __HAVE_CHAISCRIPT__
#include "chaiscript/extras/math.hpp"
......@@ -55,6 +56,21 @@ bool script::loadModule(const std::string& moduleName) {
return false;
}
bool script::loadModuleRequire(const std::string& moduleName) {
// Check if the module has already been loaded.
std::string filename = replaceString(moduleName, ".", "/");
if (std::find(m_requiremodules.begin(), m_requiremodules.end(), filename) != m_requiremodules.end()) {
return true;
}
// Attempt to load the module.
bool loaded = loadModule(filename);
if (loaded) {
m_requiremodules.push_back(filename);
}
return loaded;
}
chaiscript::Boxed_Value script::eval(const std::string& code, const std::string& filename) {
// Replace possible problematic tabs, and evaluate the script.
std::string contents = replaceString(code, "\t", " ");
......@@ -284,6 +300,7 @@ script::script(const std::string& file) {
chai.add(fun<std::vector<std::string>, filesystem, const std::string&>(&filesystem::lines), "lines");
chai.add(fun<std::vector<std::string>, filesystem, const std::string&, const std::string&>(&filesystem::lines), "lines");
chai.add(fun(&filesystem::load), "load");
chai.add(fun(&script::loadModuleRequire, this), "require");
// System
chai.add(fun(&system::getOS), "getOS");
......
......@@ -2,6 +2,7 @@
#define SRC_LOVE_SCRIPT_H_
#include <string>
#include <list>
#include "config.h"
#ifdef __HAVE_CHAISCRIPT__
......@@ -469,6 +470,8 @@ class script {
#endif
std::string replaceString(std::string subject, const std::string& search, const std::string& replace);
bool loadModuleRequire(const std::string& moduleName);
std::list<std::string> m_requiremodules;
};
} // namespace love
......
requiretestFileLoaded = true
......@@ -34,7 +34,7 @@ assert_not(love.filesystem.isSymlink("keyboard.chai"), "love.filesystem.isSymlin
// getDirectoryItems()
var getDirectoriesItems = love.filesystem.getDirectoryItems("assets").size()
assert_equal(getDirectoriesItems, 4, "love.filesystem.getDirectoryItems()")
assert_equal(getDirectoriesItems, 5, "love.filesystem.getDirectoryItems()")
// lines()
var theLines = love.filesystem.lines("filesystem.chai")
......@@ -76,3 +76,13 @@ assert(mountLoadResultType, "love.filesystem.mount() & read() == 'string'")
if (mountLoadResultType) {
assert_equal(mountLoadResult.trim(), "Hello World!", "love.filesystem.mount() & read()")
}
// require()
global requiretestFileLoaded = false
var requireReturn = require("assets.requiretest")
assert(requireReturn, "require()")
assert(requiretestFileLoaded, " - global")
requiretestFileLoaded = false
requireReturn = require("assets.requiretest")
assert(requireReturn, " - double call")
assert_not(requiretestFileLoaded, " - not loaded twice")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment