Skip to content

Commit 67e8169

Browse files
committed
Updated to match API; added async
1 parent 803c353 commit 67e8169

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

pluginmain.cc

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static std::set<std::string> commands;
3232
static std::set<std::string> workspace_set;
3333
static std::vector<std::string> workspace;
3434

35+
std::thread chaiAsyncThread;
3536
duint chaiEvalDirect(const char* cmd) {
3637
try {
3738
auto bv = chai.eval(cmd);
@@ -41,6 +42,9 @@ duint chaiEvalDirect(const char* cmd) {
4142
if(bv.get_type_info().name() == "bool") {
4243
return chai.boxed_cast<bool>(bv);
4344
}
45+
if(bv.get_type_info().name() == "void") {
46+
return 0;
47+
}
4448
_plugin_logprintf(" >>> bv type is: %s \n" , bv.get_type_info().name().c_str() );
4549
return 0;
4650
}
@@ -51,6 +55,19 @@ duint chaiEvalDirect(const char* cmd) {
5155
return 0;
5256
}
5357

58+
duint chaiEvalDirect(const char* cmd, bool async) {
59+
if(async) {
60+
if(chaiAsyncThread.joinable())
61+
chaiAsyncThread.join();
62+
chaiAsyncThread = std::thread([] (std::string cmd) {
63+
chaiEvalDirect(cmd.c_str());
64+
}, std::string(cmd));
65+
return 0;
66+
} else {
67+
return chaiEvalDirect(cmd);
68+
}
69+
}
70+
5471
bool chaiEvalCommand(int argc, char* argv[]) {
5572
if(argc == 0)
5673
return false;
@@ -70,14 +87,20 @@ bool chaiEvalCommand(int argc, char* argv[]) {
7087
return chaiEvalDirect(cmd.str().c_str());
7188
}
7289

73-
7490
bool chaiEval(int argc, char* argv[]) {
7591
if(argc < 2)
7692
return false;
7793

7894
return chaiEvalDirect(argv[1]);
7995
}
8096

97+
bool chaiEvalAsync(int argc, char* argv[]) {
98+
if(argc < 2)
99+
return false;
100+
101+
return chaiEvalDirect(argv[1], true);
102+
}
103+
81104
bool chaiShowEnv(int argc, char* argv[]) {
82105
_plugin_logprintf("Chai locals: \n");
83106

@@ -178,8 +201,8 @@ bool chaiRegisterCommand(const char* cmd) {
178201
const auto funcs = chai.get_state().engine_state.m_boxed_functions;
179202
try {
180203
const chaiscript::Boxed_Value& c = chai.eval(std::string(cmd) + ".get_arity()");
181-
int arity = chaiscript::boxed_cast<int>(c);
182-
_plugin_registerexprfunctionuserdata(pluginHandle, cmd, arity, chaiExpr, new std::string(cmd));
204+
int arity = chaiscript::boxed_cast<int>(c);
205+
_plugin_registerexprfunction(pluginHandle, cmd, arity, chaiExpr, new std::string(cmd));
183206
}
184207
catch ( const std::exception &e ) {
185208
_plugin_logprintf(" >>> Exception thrown: %s \n" , e.what( ) );
@@ -296,6 +319,9 @@ extern "C" DLL_EXPORT bool pluginit(PLUG_INITSTRUCT* initStruct) {
296319
if(!_plugin_registercommand(pluginHandle, "chaiEval", chaiEval, false))
297320
_plugin_logputs("error registering the \"chaiEval\" command!");
298321

322+
if(!_plugin_registercommand(pluginHandle, "chaiEvalAsync", chaiEvalAsync, false))
323+
_plugin_logputs("error registering the \"chaiEvalAsync\" command!");
324+
299325
if(!_plugin_registercommand(pluginHandle, "chaiLoad", chaiLoad, false))
300326
_plugin_logputs("error registering the \"chaiLoad\" command!");
301327

@@ -331,6 +357,8 @@ extern "C" DLL_EXPORT void plugsetup(PLUG_SETUPSTRUCT* setupStruct) {
331357
extern "C" DLL_EXPORT bool plugstop() {
332358
delete fileWatcher;
333359
fileWatcher = 0;
360+
if(chaiAsyncThread.joinable())
361+
chaiAsyncThread.join();
334362
return true;
335363
}
336364

0 commit comments

Comments
 (0)