-
Notifications
You must be signed in to change notification settings - Fork 1
Various cleanups and enhancements #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Pass -pthread for compilation and linking as this is a more general way to build multi-threaded code than just linking to -lpthread. Fix build of lua and luac (if uncommented) by using identical environments for all compiler invocations.
Add linker flags to export dynamic symbols. Preprocessor macros and
libraries are taken from Lua's Makefile. Without LUA_USE_DLOPEN, the
following is not supported:
package.loadlib("abc", "def")
Fix POSIX build of lua and luac by linking against -lm.
…rdcoded lua utility functions
src/luautils.h
Outdated
| #include <string> | ||
| #include "lua/src/lua.hpp" | ||
|
|
||
| using namespace std; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
»using namespace« in a header file is a no-go: there is no way to undo the effect for its users and it's bound to hide unwanted dependencies between headers: in this case, stringutls.h has errors unless this header is included first because it also uses std:: symbols unprefixed.
Unfortunately, due to the lack of a module system there is no way to avoid namespace prefixes at least in headers.
src/luautils.h
Outdated
| }; | ||
|
|
||
| struct StringPtr{ | ||
| StringPtr(const char* c_, const size_t len_):c(c_),len(len_){} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::size_t (also below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, using a different name for c'tor arguments is common, but unnecessary. It is therefore entirely a question of style to write
StringPtr(const char* c, const std::size_t len)
: c(c)
, len(len)
{}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a constructor taking a std::string would be beneficial for this class:
explicit StringPtr(const std::string& s)
: c(s.data())
, len(s.size())
{}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main purpose of this class is to hand c-strings to the overloaded lua_push function as single arguments. As there is also a lua_push that takes std::strings, the StringPtr would not be needed there. Do y... Would there still be a use for the suggested constructor? To make StringPtr more general maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was intended for the call lua_pushfield(L, rule) in main.cpp line 92, but by the time I got there I realised it could just pass the std::string. So you're right, it would actually be confusing to have a std::string c'tor and this struct should stay as it is ;-)
src/luautils.h
Outdated
|
|
||
| struct StringPtr{ | ||
| StringPtr(const char* c_, const size_t len_):c(c_),len(len_){} | ||
| const char* c; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to be consistent with the const member len, this should be
const char* const c;
src/luautils.h
Outdated
| void lua_push(lua_State *L, const int value){ | ||
| lua_pushinteger(L, value); | ||
| } | ||
| void lua_push(lua_State *L, const string value){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use std::string and const std::string& for efficiency.
src/luautils.h
Outdated
| lua_pushinteger(L, value); | ||
| } | ||
| void lua_push(lua_State *L, const string value){ | ||
| lua_pushstring(L, value.c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more efficient to use
lua_pushlstring(L, value.data(), value.size());- Since the length is already known, there is no need for
lua_pushstringto walk the string again. - Using
.data()instead of.c_str()avoids a potential unnecessary copy in order to add a terminating'\0' - If the string contains embedded
'\0'characters, the original version only copies part of the string.
src/main.cpp
Outdated
| #endif | ||
| return result; | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spurious line
src/stringutils.h
Outdated
|
|
||
| #pragma once | ||
|
|
||
| #include <iostream> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ostream is sufficient.
src/stringutils.h
Outdated
| const char* s; | ||
| size_t num; | ||
| }; | ||
| ostream& operator<< (ostream& stream, const repeat rep){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const repeat& is more efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use std::ostream
src/stringutils.h
Outdated
| } | ||
|
|
||
| // shorten string and remove line breaks and tabs | ||
| string shorten(const string& txt, const size_t numchars){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use std::string here and below.
test/calculator.lua
Outdated
| print(" ", dump(params)) | ||
| return params.matched | ||
| end | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
two spurious lines before EOF
| } | ||
|
|
||
| auto lua_loadutils(lua_State *L){ | ||
| const auto utils = R"( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation looks off
test/calculator.lua
Outdated
| end | ||
| end | ||
|
|
||
| function Num(params) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can go.
|
These commits also contain several whitespace problems, such as indent with spaces in |
src/main.cpp
Outdated
| cerr << "error loading \"" << args[2] << "\": file not found" << endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation looks off
…rmediate 0 characters
Only a single PR since several changes have been submitted at once.
I created the »base« branch in order to separate previously reviewed changes. Going forward, it would be useful to retire branches once their PRs and reviews are finished.