Skip to content

Commit 73baade

Browse files
authored
0.30.0 (#350)
* Add loading lib/init.chai through require() (#348) * Update ChaiScript_Extras and fix module load warning * Update libretro-common
1 parent 3ca62ba commit 73baade

File tree

10 files changed

+72
-30
lines changed

10 files changed

+72
-30
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ All notable changes to [ChaiLove](https://github.com/RobLoach/ChaiLove) will be
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

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

1215
## 0.29.1 - 2018-11-05
1316
### Chores

docs/Doxyfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PROJECT_NAME = "ChaiLove API"
2323
# This could be handy for archiving the generated documentation or
2424
# if some version control system is used.
2525

26-
PROJECT_NUMBER = "0.29.1"
26+
PROJECT_NUMBER = "0.30.0"
2727

2828
# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
2929
# base path where the generated documentation will be put.

src/ChaiLove.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
#define SRC_CHAILOVE_H_
4848

4949
#define CHAILOVE_VERSION_MAJOR 0
50-
#define CHAILOVE_VERSION_MINOR 29
51-
#define CHAILOVE_VERSION_PATCH 1
52-
#define CHAILOVE_VERSION_STRING "0.29.1"
50+
#define CHAILOVE_VERSION_MINOR 30
51+
#define CHAILOVE_VERSION_PATCH 0
52+
#define CHAILOVE_VERSION_STRING "0.30.0"
5353

5454
#include "SDL.h"
5555
#include "libretro.h"

src/docs/Globals.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ class List {
7171
* @see love.filesystem.load
7272
*
7373
* @code
74-
* // The following will load lib/player.chai.
74+
* // The following will load "lib/player.chai".
7575
* require("lib.player")
7676
*
7777
* // Calling require("lib.player") again will not re-load it.
7878
* require("lib.player")
79+
*
80+
* // Calling a directory name will attempt to load the directory's "init.chai"
81+
* // file. The following example will load "lib/init.chai" if "lib.chai"
82+
* // doesn't exist.
83+
* require("lib")
7984
* @endcode
8085
*/
8186
bool require(const std::string& module);
@@ -110,10 +115,10 @@ class String {
110115
* var hello = " Hello World! "
111116
* var result = hello.trim()
112117
* // => "Hello World!"
113-
* @endcode
118+
* @endcode
114119
*/
115120
std::string trim();
116-
121+
117122
/**
118123
* Splits a string by the given token.
119124
*/

src/love/script.cpp

+43-18
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,42 @@ using love::graphics;
2323

2424
namespace love {
2525

26+
std::string script::findModule(const std::string& filename) {
27+
ChaiLove* app = ChaiLove::getInstance();
28+
std::string possibilities[5] = {
29+
filename,
30+
filename + ".chai",
31+
// Allow loading lua files as ChaiScript?
32+
// filename + ".lua",
33+
// Attempt to load a directory's init.chai, if available.
34+
filename + "/init.chai"
35+
// Allow loading .lua files?
36+
// filename + "/init.lua"
37+
};
38+
for (const std::string& possibility : possibilities) {
39+
// Make sure the file exists and is a file.
40+
if (app->filesystem.exists(possibility) && app->filesystem.isFile(possibility)) {
41+
return possibility;
42+
}
43+
}
44+
return "";
45+
}
46+
2647
bool script::loadModule(const std::string& moduleName) {
2748
#ifdef __HAVE_CHAISCRIPT__
2849
ChaiLove* app = ChaiLove::getInstance();
2950

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

33-
// Make sure it exists.
34-
if (!app->filesystem.exists(filename)) {
35-
// See if we are to append .chai.
36-
filename = filename + ".chai";
37-
if (!app->filesystem.exists(filename)) {
38-
std::cout << "[ChaiLove] [script] Module " << filename << " not found." << std::endl;
39-
return false;
40-
}
57+
// Store a filename for the module.
58+
std::string filename = findModule(moduleName);
59+
if (filename.empty()) {
60+
std::cout << "[ChaiLove] [script] Module " << moduleName << " not found." << std::endl;
61+
return false;
4162
}
4263

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

73+
// Run the script.
5274
eval(contents, filename);
5375
return true;
54-
5576
#endif
5677
return false;
5778
}
5879

59-
bool script::loadModuleRequire(const std::string& moduleName) {
60-
// Check if the module has already been loaded.
61-
std::string filename = replaceString(replaceString(moduleName, ".chai", ""), ".", "/");
80+
bool script::require(const std::string& moduleName) {
81+
// Find what the cleansed module name is.
82+
std::string noExtension = replaceString(replaceString(moduleName, ".chai", ""), ".lua", "");
83+
std::string filename = replaceString(noExtension, ".", "/");
84+
85+
// Ensure we only load the script once.
6286
if (std::find(m_requiremodules.begin(), m_requiremodules.end(), filename) != m_requiremodules.end()) {
6387
return true;
6488
}
@@ -68,6 +92,7 @@ bool script::loadModuleRequire(const std::string& moduleName) {
6892
if (loaded) {
6993
m_requiremodules.push_back(filename);
7094
}
95+
7196
return loaded;
7297
}
7398

@@ -310,7 +335,7 @@ script::script(const std::string& file) {
310335
chai.add(fun<std::vector<std::string>, filesystem, const std::string&>(&filesystem::lines), "lines");
311336
chai.add(fun<std::vector<std::string>, filesystem, const std::string&, const std::string&>(&filesystem::lines), "lines");
312337
chai.add(fun(&filesystem::load), "load");
313-
chai.add(fun(&script::loadModuleRequire, this), "require");
338+
chai.add(fun(&script::require, this), "require");
314339
chai.add(fun(&filesystem::getFileExtension), "getFileExtension");
315340
chai.add(fun(&filesystem::getBasename), "getBasename");
316341
chai.add(fun(&filesystem::getParentDirectory), "getParentDirectory");
@@ -395,15 +420,15 @@ script::script(const std::string& file) {
395420
mainLoaded = true;
396421
} else {
397422
// Load the main.chai file.
398-
loadModuleRequire("conf");
423+
require("conf");
399424

400425
std::string extension(app->filesystem.getFileExtension(file));
401426
if (extension == "chailove" || extension == "chaigame") {
402-
mainLoaded = loadModuleRequire("main");
427+
mainLoaded = require("main");
403428
} else {
404429
// Otherwise, load the actual file.
405430
std::string filename(app->filesystem.getBasename(file));
406-
mainLoaded = loadModuleRequire(filename);
431+
mainLoaded = require(filename);
407432
}
408433
}
409434

src/love/script.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,11 @@ class script {
471471
bool hascheatset = true;
472472
#endif
473473

474+
std::string findModule(const std::string& filename);
474475
std::string replaceString(std::string subject, const std::string& search, const std::string& replace);
475-
bool loadModuleRequire(const std::string& moduleName);
476+
bool require(const std::string& moduleName);
477+
478+
// Properties
476479
std::list<std::string> m_requiremodules;
477480
};
478481

test/unittests/assets/init.chai

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requiretestFileLoaded = true

test/unittests/filesystem.chai

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ assert_not(love.filesystem.isSymlink("keyboard.chai"), "love.filesystem.isSymlin
3434

3535
// getDirectoryItems()
3636
var getDirectoriesItems = love.filesystem.getDirectoryItems("assets").size()
37-
assert_equal(getDirectoriesItems, 5, "love.filesystem.getDirectoryItems()")
37+
assert_equal(getDirectoriesItems, 6, "love.filesystem.getDirectoryItems()")
3838

3939
// lines()
4040
var theLines = love.filesystem.lines("filesystem.chai")
@@ -103,6 +103,11 @@ requireReturn = require("assets.requiretest")
103103
assert(requireReturn, " double call")
104104
assert_not(requiretestFileLoaded, " not loaded twice")
105105

106+
// require() - dir/init.chai
107+
requiretestFileLoaded = false
108+
require("assets")
109+
assert(requiretestFileLoaded, " loaded assets/init.chai")
110+
106111
// getFileExtension()
107112
assert_equal(love.filesystem.getFileExtension("/opt/var/something.txt"), "txt", "love.filesystem.getFileExtension()")
108113
assert_equal(love.filesystem.getFileExtension("/opt/var/something.tar.gz"), "gz", "love.filesystem.getFileExtension()")

0 commit comments

Comments
 (0)