diff --git a/Makefile b/Makefile index 5861cbe..bfc1fca 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/cs_vm.cc b/cs_vm.cc index 11252af..283052a 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -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) { diff --git a/cubescript.cc b/cubescript.cc index d7ca295..83dc7f0 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -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; diff --git a/cubescript.hh b/cubescript.hh index 009b5e0..e9099d1 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -315,6 +315,8 @@ enum { CS_LIB_ALL = 0b111 }; +using CsHookCb = ostd::Function; + struct OSTD_EXPORT CsState { CsMap idents; CsVector 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; };