add a hook func that is called on every code invocation

master
Daniel Kolesa 2016-09-05 21:34:48 +02:00
parent 87a961d8a7
commit 605efea9e2
4 changed files with 26 additions and 1 deletions

View File

@ -29,7 +29,7 @@ $(LIBCS_LIB): $(LIBCS_OBJ)
repl: $(LIBCS_LIB) tools/repl.cc tools/linenoise.cc tools/linenoise.hh
$(CXX) $(CXXFLAGS) $(LIBCS_CXXFLAGS) $(LDFLAGS) \
-Itools -DCS_REPL_USE_LINENOISE -DCS_REPL_HAS_COMPLETE -DCS_REPL_HAS_HINTS \
-Itools -DCS_REPL_USE_READLINE -L/usr/local/lib -lreadline -I/usr/local/include -DCS_REPL_HAS_HINTS -DCS_REPL_HAS_COMPLETE \
tools/linenoise.cc tools/repl.cc -o repl $(LIBCS_LIB)
clean:

View File

@ -581,6 +581,10 @@ static ostd::Uint32 *runcode(CsState &cs, ostd::Uint32 *code, CsValue &result) {
++rundepth;
int numargs = 0;
CsValue args[MaxArguments + MaxResults];
auto &chook = cs.get_call_hook();
if (chook) {
chook();
}
for (;;) {
ostd::Uint32 op = *code++;
switch (op & 0xFF) {

View File

@ -381,6 +381,20 @@ void CsState::set_err(CsStream &s) {
p_err = &s;
}
CsHookCb CsState::set_call_hook(CsHookCb func) {
auto hk = ostd::move(p_callhook);
p_callhook = ostd::move(func);
return hk;
}
CsHookCb const &CsState::get_call_hook() const {
return p_callhook;
}
CsHookCb &CsState::get_call_hook() {
return p_callhook;
}
void CsState::clear_override(CsIdent &id) {
if (!(id.get_flags() & IDF_OVERRIDDEN)) {
return;

View File

@ -315,6 +315,8 @@ enum {
CS_LIB_ALL = 0b111
};
using CsHookCb = ostd::Function<void()>;
struct OSTD_EXPORT CsState {
CsMap<ostd::ConstCharRange, CsIdent *> idents;
CsVector<CsIdent *> identmap;
@ -336,6 +338,10 @@ struct OSTD_EXPORT CsState {
CsStream &get_err();
void set_err(CsStream &s);
CsHookCb set_call_hook(CsHookCb func);
CsHookCb const &get_call_hook() const;
CsHookCb &get_call_hook();
void init_libs(int libs = CS_LIB_ALL);
void clear_override(CsIdent &id);
@ -454,6 +460,7 @@ struct OSTD_EXPORT CsState {
private:
CsIdent *add_ident(CsIdent *id);
CsHookCb p_callhook;
CsStream *p_out, *p_err;
};