Skip to content

Commit bf0737a

Browse files
committed
Cleanup search for chaiscript_stdlib and fix some bugs
1 parent 304e340 commit bf0737a

File tree

3 files changed

+69
-43
lines changed

3 files changed

+69
-43
lines changed

CMakeLists.txt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ else()
99
option(MULTITHREAD_SUPPORT_ENABLED "Multithreaded Support Enabled" TRUE)
1010
endif()
1111

12+
if (CMAKE_COMPILER_IS_GNUCC)
13+
option(ENABLE_COVERAGE "Enable Coverage Reporting in GCC" FALSE)
14+
endif()
15+
16+
17+
1218
option(BUILD_MODULES "Build Extra Modules (stl, reflection)" TRUE)
1319
option(BUILD_SAMPLES "Build Samples Folder" FALSE)
1420

@@ -70,17 +76,24 @@ else(READLINE_LIBRARY)
7076
set (READLINE_FLAG )
7177
endif(READLINE_LIBRARY)
7278

79+
SET(EXTRA_LINKER_FLAGS "")
7380

7481
if (CMAKE_COMPILER_IS_GNUCC)
75-
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
76-
OUTPUT_VARIABLE GCC_VERSION)
77-
82+
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
7883

7984
if (GCC_VERSION VERSION_LESS 4.8)
8085
SET(CPP11_FLAG "-std=c++0x")
8186
else()
8287
SET(CPP11_FLAG "-std=c++11")
8388
endif()
89+
90+
if (ENABLE_COVERAGE)
91+
add_definitions(-fprofile-arcs -ftest-coverage)
92+
SET(EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} "-fprofile-arcs -ftest-coverage")
93+
# SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "-fprofile-arcs -ftest-coverage")
94+
endif()
95+
96+
8497
else()
8598
SET(CPP11_FLAG "-std=c++11")
8699
endif()
@@ -105,17 +118,16 @@ else()
105118
endif()
106119

107120
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
108-
109121
option(USE_LIBCXX "Use clang's libcxx" TRUE)
110122

111123
if (USE_LIBCXX)
112124
add_definitions(-stdlib=libc++)
113-
set (EXTRA_LINKER_FLAGS ${CPP11_FLAG} -stdlib=libc++)
125+
set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG} -stdlib=libc++)
114126
else ()
115-
set (EXTRA_LINKER_FLAGS ${CPP11_FLAG})
127+
set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG})
116128
endif()
117129
elseif(CMAKE_COMPILER_IS_GNUCC)
118-
set (EXTRA_LINKER_FLAGS ${CPP11_FLAG})
130+
set (EXTRA_LINKER_FLAGS ${EXTRA_LINKER_FLAGS} ${CPP11_FLAG})
119131
endif()
120132

121133
# limitations in MinGW require us to make an optimized build

include/chaiscript/language/chaiscript_engine.hpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ namespace chaiscript
337337
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_type_name, std::ref(m_engine)), "name");
338338

339339

340-
typedef void (ChaiScript::*load_mod_1)(const std::string&);
340+
typedef std::string (ChaiScript::*load_mod_1)(const std::string&);
341341
typedef void (ChaiScript::*load_mod_2)(const std::string&, const std::string&);
342342

343343
m_engine.add(fun(static_cast<load_mod_1>(&ChaiScript::load_module), this), "load_module");
@@ -449,7 +449,7 @@ namespace chaiscript
449449
dllpath = std::string(&buf.front(), pathlen);
450450
}
451451

452-
m_modulepaths.push_back(dllpath+"/");
452+
m_modulepaths.insert(m_modulepaths.begin(), dllpath+"/");
453453
}
454454
#endif
455455

@@ -652,7 +652,7 @@ namespace chaiscript
652652
/// (the symbol mentioned above), an exception is thrown.
653653
///
654654
/// \throw chaiscript::exception::load_module_error In the event that no matching module can be found.
655-
void load_module(const std::string &t_module_name)
655+
std::string load_module(const std::string &t_module_name)
656656
{
657657
std::vector<exception::load_module_error> errors;
658658

@@ -665,23 +665,25 @@ namespace chaiscript
665665
postfixes.push_back(".so");
666666
postfixes.push_back("");
667667

668-
for (size_t i = 0; i < m_modulepaths.size(); ++i)
668+
for (size_t i = 0; i < m_modulepaths.size(); ++i)
669+
{
670+
for (size_t j = 0; j < prefixes.size(); ++j)
669671
{
670-
for (size_t j = 0; j < prefixes.size(); ++j)
671-
{
672-
for (size_t k = 0; k < postfixes.size(); ++k)
673-
{
674-
try {
675-
std::string name = m_modulepaths[i] + prefixes[j] + t_module_name + postfixes[k];
676-
load_module(t_module_name, name);
677-
return;
678-
} catch (const chaiscript::exception::load_module_error &e) {
679-
errors.push_back(e);
680-
// Try next set
681-
}
682-
}
672+
for (size_t k = 0; k < postfixes.size(); ++k)
673+
{
674+
try {
675+
std::string name = m_modulepaths[i] + prefixes[j] + t_module_name + postfixes[k];
676+
// std::cerr << "trying location: " << name << std::endl;
677+
load_module(t_module_name, name);
678+
return name;
679+
} catch (const chaiscript::exception::load_module_error &e) {
680+
// std::cerr << "error: " << e.what() << std::endl;
681+
errors.push_back(e);
682+
// Try next set
683683
}
684+
}
684685
}
686+
}
685687

686688
std::string errstring;
687689

src/main.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ void using_history(){}
4343

4444

4545

46-
void *cast_module_symbol(std::string (*t_path)())
46+
void *cast_module_symbol(std::vector<std::string> (*t_path)())
4747
{
4848
union cast_union
4949
{
50-
std::string (*in_ptr)();
50+
std::vector<std::string> (*in_ptr)();
5151
void *out_ptr;
5252
};
5353

@@ -56,22 +56,27 @@ void *cast_module_symbol(std::string (*t_path)())
5656
return c.out_ptr;
5757
}
5858

59-
std::string default_search_path()
59+
std::vector<std::string> default_search_paths()
6060
{
61+
std::vector<std::string> paths;
62+
6163
#ifdef CHAISCRIPT_WINDOWS // force no unicode
6264
CHAR path[4096];
6365
int size = GetModuleFileNameA(0, path, sizeof(path)-1);
6466

6567
std::string exepath(path, size);
6668

67-
size_t secondtolastslash = exepath.rfind('\\', exepath.rfind('\\') - 1);
68-
if (secondtolastslash != std::string::npos)
69+
size_t lastslash = exepath.rfind('\\');
70+
size_t secondtolastslash = exepath.rfind('\\', lastslash - 1);
71+
if (lastslash != std::string::npos)
6972
{
70-
return exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\";
71-
} else {
72-
return "";
73+
paths.push_back(exepath.substr(0, lastslash));
7374
}
7475

76+
if (secondtolastslash != std::string::npos)
77+
{
78+
return {exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\"};
79+
}
7580
#else
7681

7782
std::string exepath;
@@ -102,23 +107,30 @@ std::string default_search_path()
102107

103108
if (exepath.empty())
104109
{
105-
Dl_info rInfo;
110+
Dl_info rInfo;
106111
memset( &rInfo, 0, sizeof(rInfo) );
107-
if ( !dladdr(cast_module_symbol(&default_search_path), &rInfo) || !rInfo.dli_fname ) {
108-
return "";
112+
if ( !dladdr(cast_module_symbol(&default_search_paths), &rInfo) || !rInfo.dli_fname ) {
113+
return paths;
109114
}
110115

111116
exepath = std::string(rInfo.dli_fname);
112117
}
113118

114-
size_t secondtolastslash = exepath.rfind('/', exepath.rfind('/') - 1);
119+
size_t lastslash = exepath.rfind('/');
120+
121+
size_t secondtolastslash = exepath.rfind('/', lastslash - 1);
122+
if (lastslash != std::string::npos)
123+
{
124+
paths.push_back(exepath.substr(0, lastslash));
125+
}
126+
115127
if (secondtolastslash != std::string::npos)
116128
{
117-
return exepath.substr(0, secondtolastslash) + "/lib/chaiscript/";
118-
} else {
119-
return "";
129+
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
120130
}
121131
#endif
132+
133+
return paths;
122134
}
123135

124136
void help(int n) {
@@ -239,8 +251,6 @@ void interactive(chaiscript::ChaiScript& chai)
239251

240252
int main(int argc, char *argv[])
241253
{
242-
std::vector<std::string> usepaths;
243-
std::vector<std::string> modulepaths;
244254

245255
// Disable deprecation warning for getenv call.
246256
#ifdef CHAISCRIPT_MSVC
@@ -255,14 +265,16 @@ int main(int argc, char *argv[])
255265
#pragma warning(pop)
256266
#endif
257267

268+
std::vector<std::string> usepaths;
258269
usepaths.push_back("");
259270
if (usepath)
260271
{
261272
usepaths.push_back(usepath);
262273
}
263274

264-
std::string searchpath = default_search_path();
265-
modulepaths.push_back(searchpath);
275+
std::vector<std::string> modulepaths;
276+
std::vector<std::string> searchpaths = default_search_paths();
277+
modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
266278
modulepaths.push_back("");
267279
if (modulepath)
268280
{

0 commit comments

Comments
 (0)