Skip to content

Commit

Permalink
0.30.0 (#350)
Browse files Browse the repository at this point in the history
* Add loading lib/init.chai through require() (#348)
* Update ChaiScript_Extras and fix module load warning
* Update libretro-common
  • Loading branch information
RobLoach committed Nov 14, 2018
1 parent 3ca62ba commit 73baade
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 30 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,10 +4,13 @@ All notable changes to [ChaiLove](https://github.com/RobLoach/ChaiLove) will be
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.29.2 - Unreleased
## 0.30.0 - 2018-11-14
### Features
- Added support for classic_armv7_a7
- By [@classicmods](https://github.com/classicmods) and [@swingflip](https://github.com/swingflip)
- Added `lib/init.chai` loading with `require("lib")`
- Updated ChaiScript/ChaiScript_Extras
- Updated libretro/libretro-common

## 0.29.1 - 2018-11-05
### Chores
Expand Down
2 changes: 1 addition & 1 deletion docs/Doxyfile
Expand Up @@ -23,7 +23,7 @@ PROJECT_NAME = "ChaiLove API"
# This could be handy for archiving the generated documentation or
# if some version control system is used.

PROJECT_NUMBER = "0.29.1"
PROJECT_NUMBER = "0.30.0"

# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
# base path where the generated documentation will be put.
Expand Down
6 changes: 3 additions & 3 deletions src/ChaiLove.h
Expand Up @@ -47,9 +47,9 @@
#define SRC_CHAILOVE_H_

#define CHAILOVE_VERSION_MAJOR 0
#define CHAILOVE_VERSION_MINOR 29
#define CHAILOVE_VERSION_PATCH 1
#define CHAILOVE_VERSION_STRING "0.29.1"
#define CHAILOVE_VERSION_MINOR 30
#define CHAILOVE_VERSION_PATCH 0
#define CHAILOVE_VERSION_STRING "0.30.0"

#include "SDL.h"
#include "libretro.h"
Expand Down
11 changes: 8 additions & 3 deletions src/docs/Globals.h
Expand Up @@ -71,11 +71,16 @@ class List {
* @see love.filesystem.load
*
* @code
* // The following will load lib/player.chai.
* // The following will load "lib/player.chai".
* require("lib.player")
*
* // Calling require("lib.player") again will not re-load it.
* require("lib.player")
*
* // Calling a directory name will attempt to load the directory's "init.chai"
* // file. The following example will load "lib/init.chai" if "lib.chai"
* // doesn't exist.
* require("lib")
* @endcode
*/
bool require(const std::string& module);
Expand Down Expand Up @@ -110,10 +115,10 @@ class String {
* var hello = " Hello World! "
* var result = hello.trim()
* // => "Hello World!"
* @endcode
* @endcode
*/
std::string trim();

/**
* Splits a string by the given token.
*/
Expand Down
61 changes: 43 additions & 18 deletions src/love/script.cpp
Expand Up @@ -23,21 +23,42 @@ using love::graphics;

namespace love {

std::string script::findModule(const std::string& filename) {
ChaiLove* app = ChaiLove::getInstance();
std::string possibilities[5] = {
filename,
filename + ".chai",
// Allow loading lua files as ChaiScript?
// filename + ".lua",
// Attempt to load a directory's init.chai, if available.
filename + "/init.chai"
// Allow loading .lua files?
// filename + "/init.lua"
};
for (const std::string& possibility : possibilities) {
// Make sure the file exists and is a file.
if (app->filesystem.exists(possibility) && app->filesystem.isFile(possibility)) {
return possibility;
}
}
return "";
}

bool script::loadModule(const std::string& moduleName) {
#ifdef __HAVE_CHAISCRIPT__
ChaiLove* app = ChaiLove::getInstance();

// Store a filename for the module.
std::string filename = moduleName;
// Ensure we're loading a valid module name.
if (moduleName.empty()) {
std::cout << "[ChaiLove] [script] loadModule was called with an empty moduleName." << std::endl;
return false;
}

// Make sure it exists.
if (!app->filesystem.exists(filename)) {
// See if we are to append .chai.
filename = filename + ".chai";
if (!app->filesystem.exists(filename)) {
std::cout << "[ChaiLove] [script] Module " << filename << " not found." << std::endl;
return false;
}
// Store a filename for the module.
std::string filename = findModule(moduleName);
if (filename.empty()) {
std::cout << "[ChaiLove] [script] Module " << moduleName << " not found." << std::endl;
return false;
}

// Load the contents of the file.
Expand All @@ -49,16 +70,19 @@ bool script::loadModule(const std::string& moduleName) {
return false;
}

// Run the script.
eval(contents, filename);
return true;

#endif
return false;
}

bool script::loadModuleRequire(const std::string& moduleName) {
// Check if the module has already been loaded.
std::string filename = replaceString(replaceString(moduleName, ".chai", ""), ".", "/");
bool script::require(const std::string& moduleName) {
// Find what the cleansed module name is.
std::string noExtension = replaceString(replaceString(moduleName, ".chai", ""), ".lua", "");
std::string filename = replaceString(noExtension, ".", "/");

// Ensure we only load the script once.
if (std::find(m_requiremodules.begin(), m_requiremodules.end(), filename) != m_requiremodules.end()) {
return true;
}
Expand All @@ -68,6 +92,7 @@ bool script::loadModuleRequire(const std::string& moduleName) {
if (loaded) {
m_requiremodules.push_back(filename);
}

return loaded;
}

Expand Down Expand Up @@ -310,7 +335,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");
chai.add(fun(&script::require, this), "require");
chai.add(fun(&filesystem::getFileExtension), "getFileExtension");
chai.add(fun(&filesystem::getBasename), "getBasename");
chai.add(fun(&filesystem::getParentDirectory), "getParentDirectory");
Expand Down Expand Up @@ -395,15 +420,15 @@ script::script(const std::string& file) {
mainLoaded = true;
} else {
// Load the main.chai file.
loadModuleRequire("conf");
require("conf");

std::string extension(app->filesystem.getFileExtension(file));
if (extension == "chailove" || extension == "chaigame") {
mainLoaded = loadModuleRequire("main");
mainLoaded = require("main");
} else {
// Otherwise, load the actual file.
std::string filename(app->filesystem.getBasename(file));
mainLoaded = loadModuleRequire(filename);
mainLoaded = require(filename);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/love/script.h
Expand Up @@ -471,8 +471,11 @@ class script {
bool hascheatset = true;
#endif

std::string findModule(const std::string& filename);
std::string replaceString(std::string subject, const std::string& search, const std::string& replace);
bool loadModuleRequire(const std::string& moduleName);
bool require(const std::string& moduleName);

// Properties
std::list<std::string> m_requiremodules;
};

Expand Down
1 change: 1 addition & 0 deletions test/unittests/assets/init.chai
@@ -0,0 +1 @@
requiretestFileLoaded = true
7 changes: 6 additions & 1 deletion test/unittests/filesystem.chai
Expand Up @@ -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, 5, "love.filesystem.getDirectoryItems()")
assert_equal(getDirectoriesItems, 6, "love.filesystem.getDirectoryItems()")

// lines()
var theLines = love.filesystem.lines("filesystem.chai")
Expand Down Expand Up @@ -103,6 +103,11 @@ requireReturn = require("assets.requiretest")
assert(requireReturn, " double call")
assert_not(requiretestFileLoaded, " not loaded twice")

// require() - dir/init.chai
requiretestFileLoaded = false
require("assets")
assert(requiretestFileLoaded, " loaded assets/init.chai")

// getFileExtension()
assert_equal(love.filesystem.getFileExtension("/opt/var/something.txt"), "txt", "love.filesystem.getFileExtension()")
assert_equal(love.filesystem.getFileExtension("/opt/var/something.tar.gz"), "gz", "love.filesystem.getFileExtension()")
Expand Down
2 changes: 1 addition & 1 deletion vendor/ChaiScript_Extras
2 changes: 1 addition & 1 deletion vendor/libretro-common

0 comments on commit 73baade

Please sign in to comment.