• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

diegoarjz / aswpp / 13214353970

08 Feb 2025 09:24AM UTC coverage: 80.412% (-1.5%) from 81.867%
13214353970

push

github

web-flow
Merge pull request #12 from diegoarjz/add_bool_args_and_retur

Add functionality to set bool args and return values

31 of 44 new or added lines in 3 files covered. (70.45%)

9 existing lines in 2 files now uncovered.

624 of 776 relevant lines covered (80.41%)

10.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.4
/src/script_engine.cpp
1
#include "aswpp/script_engine.h"
2

3
#include "scriptstdstring.h"
4

5
#include <iostream>
6
#include <map>
7

8
namespace aswpp {
9
//----------------------------------------
10
// Engine class
11
static void StaticMessageCallback(const asSMessageInfo *msg) {
2✔
12
  switch (msg->type) {
2✔
13
  case asMSGTYPE_ERROR:
2✔
14
    std::cerr << "[Error] (" << msg->section << "@" << msg->row << ":"
2✔
15
              << msg->col << ")" << msg->message << std::endl;
2✔
16
    break;
2✔
UNCOV
17
  case asMSGTYPE_WARNING:
×
UNCOV
18
    std::cerr << "[Warning] (" << msg->section << "@" << msg->row << ":"
×
UNCOV
19
              << msg->col << ")" << msg->message << std::endl;
×
UNCOV
20
    break;
×
UNCOV
21
  case asMSGTYPE_INFORMATION:
×
UNCOV
22
    std::cerr << "[Info] (" << msg->section << "@" << msg->row << ":"
×
UNCOV
23
              << msg->col << ")" << msg->message << std::endl;
×
UNCOV
24
    break;
×
25
  };
26
}
2✔
27

28
class Engine::Impl {
29
public:
30
  Impl(Engine &engine) : m_scriptEngine{engine} {
48✔
31
    m_asEngine = asCreateScriptEngine();
48✔
32

33
    int r = m_asEngine->SetMessageCallback(asFUNCTION(StaticMessageCallback), 0,
48✔
34
                                           asCALL_CDECL);
35

36
    m_asEngine->SetMessageCallback(asFUNCTION(StaticMessageCallback), nullptr,
48✔
37
                                   asCALL_CDECL);
38

39
    if (r == asINVALID_ARG) {
48✔
NEW
40
      std::cerr << "invalid arg" << std::endl;
×
41
    } else if (r == asNOT_SUPPORTED) {
48✔
NEW
42
      std::cerr << "not supported arg" << std::endl;
×
43
    }
44

45
    m_context = m_asEngine->CreateContext();
48✔
46

47
    if (m_context == nullptr) {
48✔
48
      m_asEngine->ShutDownAndRelease();
×
49
      m_asEngine = nullptr;
×
50
    }
51

52
    RegisterStdString(m_asEngine);
48✔
53
  }
48✔
54

55
  ~Impl() {
48✔
56
    if (m_asEngine != nullptr) {
48✔
57
      m_asEngine->ShutDownAndRelease();
48✔
58
    }
59
  }
48✔
60

61
  bool prepare(const std::string &moduleName, const std::string &function) {
37✔
62
    if (m_asEngine == nullptr) {
37✔
63
      std::cerr << "Tried running an invalid script." << std::endl;
×
64
      return false;
×
65
    }
66

67
    asIScriptModule *module = m_asEngine->GetModule(moduleName.c_str());
37✔
68

69
    auto *func = module->GetFunctionByDecl(function.c_str());
37✔
70
    if (func == nullptr) {
37✔
71
      std::cerr << "Could not find function '" << function << "'." << std::endl;
×
72
      return false;
×
73
    }
74

75
    if (m_context == nullptr) {
37✔
76
      std::cerr << "Could not create context." << std::endl;
×
77
      return false;
×
78
    }
79

80
    if (m_context->Prepare(func) != 0) {
37✔
81
      std::cerr << "Could not prepare context." << std::endl;
×
82
      m_context->Release();
×
83
      return false;
×
84
    }
85

86
    return true;
37✔
87
  }
88

89
  bool run(const std::string &moduleName, const std::string &function) {
37✔
90
    if (m_context->Execute() != asEXECUTION_FINISHED) {
37✔
91
      std::cerr << "Could not execute context." << std::endl;
×
92
      return false;
×
93
    }
94
    return true;
37✔
95
  }
96

97
  bool release() { 
48✔
98
    const auto result = m_context->Release() == 0;
48✔
99
    m_context = nullptr;
48✔
100
    return result;
48✔
101
  }
102

103
  Engine &m_scriptEngine;
104
  asIScriptEngine *m_asEngine{nullptr};
105
  asIScriptContext *m_context{nullptr};
106
  std::map<std::string, ModulePtr> m_modules;
107
};
108

109
Engine::Engine() : m_impl(std::make_unique<Engine::Impl>(*this)) {}
48✔
110

111
Engine::~Engine() {
48✔
112
  release();
48✔
113
}
48✔
114

115
bool Engine::IsValid() const { return m_impl->m_asEngine != nullptr; }
1✔
116

117
ModulePtr Engine::CreateModule(const std::string &name,
11✔
118
                               const std::string &source) {
119
  auto module = std::make_shared<Module>(name, source);
11✔
120
  if (!Attach(module)) {
11✔
121
    return nullptr;
×
122
  }
123
  return module;
11✔
124
}
11✔
125

126
bool Engine::Attach(const ModulePtr &module) {
44✔
127
  // Keep track of which engine this module is attached to
128
  if (module->IsAttached()) {
44✔
129
    return false;
1✔
130
  }
131

132
  const bool wasInserted =
133
      m_impl->m_modules.emplace(module->GetName(), module).second;
43✔
134
  if (!wasInserted) {
43✔
135
    return false;
×
136
  }
137
  module->attachTo(this);
43✔
138

139
  if (!module->prepareModule(
86✔
140
          reinterpret_cast<asScriptEngineHandle *>(m_impl->m_asEngine))) {
43✔
141
    return false;
1✔
142
  }
143

144
  return true;
42✔
145
}
146

147
//----------------------------------------
148
//! \section Register Enum Methods
149
bool Engine::RegisterEnum(const std::string &enumName) {
6✔
150
  const int r = m_impl->m_asEngine->RegisterEnum(enumName.c_str());
6✔
151
  return r >= 0;
6✔
152
}
153
bool Engine::RegisterEnumValue(const std::string &enumName,
10✔
154
                               const std::string &valueName, int value) {
155
  const int r = m_impl->m_asEngine->RegisterEnumValue(enumName.c_str(),
10✔
156
                                                      valueName.c_str(), value);
157
  return r >= 0;
10✔
158
}
159

160
bool Engine::prepare(const std::string &moduleName,
37✔
161
                     const std::string &function) {
162
  return m_impl->prepare(moduleName, function);
37✔
163
}
164

165
bool Engine::run(const std::string &module, const std::string &function) {
37✔
166
  return m_impl->run(module, function);
37✔
167
}
168

169
bool Engine::release() { return m_impl->release(); }
48✔
170

171
asIScriptEngine *Engine::engine() { return m_impl->m_asEngine; }
19✔
172

173
namespace {
174
void reportSetFunctionError(int error) {
×
175
  if (error != asSUCCESS) {
×
176
    if (error == asCONTEXT_NOT_PREPARED) {
×
177
      std::cerr << " Context not prepared" << std::endl;
×
178
    } else if (error == asINVALID_ARG) {
×
179
      std::cerr << " Argument number is larger than the number of arguments in "
180
                   "the prepared function."
×
181
                << std::endl;
×
182
    } else if (error == asINVALID_TYPE) {
×
183
      std::cerr << " The argument is not an object or handle." << std::endl;
×
184
    }
185
  }
186
}
×
187
} // namespace
188

189
template <> bool Engine::setFunctionArg<int64_t>(int i, int64_t val) {
1✔
190
  const int r = m_impl->m_context->SetArgQWord(i, val);
1✔
191
  if (r != asSUCCESS) {
1✔
192
    std::cerr << "Unable to set argument " << i << " to value at address "
×
193
              << val << std::endl;
×
194
    reportSetFunctionError(r);
×
195
  }
196
  return r == asSUCCESS;
1✔
197
}
198

199
template <> bool Engine::setFunctionArg<uint64_t>(int i, uint64_t val) {
1✔
200
  const int r = m_impl->m_context->SetArgQWord(i, val);
1✔
201
  if (r != asSUCCESS) {
1✔
202
    std::cerr << "Unable to set argument " << i << " to value at address "
×
203
              << val << std::endl;
×
204
    reportSetFunctionError(r);
×
205
  }
206
  return r == asSUCCESS;
1✔
207
}
208

209
template <> bool Engine::setFunctionArg<int32_t>(int i, int32_t val) {
3✔
210
  const int r = m_impl->m_context->SetArgDWord(i, val);
3✔
211
  if (r != asSUCCESS) {
3✔
212
    std::cerr << "Unable to set argument " << i << " to value at address "
×
213
              << val << std::endl;
×
214
    reportSetFunctionError(r);
×
215
  }
216
  return r == asSUCCESS;
3✔
217
}
218

219
template <> bool Engine::setFunctionArg<uint32_t>(int i, uint32_t val) {
1✔
220
  const int r = m_impl->m_context->SetArgDWord(i, val);
1✔
221
  if (r != asSUCCESS) {
1✔
222
    std::cerr << "Unable to set argument " << i << " to value at address "
×
223
              << val << std::endl;
×
224
    reportSetFunctionError(r);
×
225
  }
226
  return r == asSUCCESS;
1✔
227
}
228

229
template <> bool Engine::setFunctionArg<int16_t>(int i, int16_t val) {
1✔
230
  const int r = m_impl->m_context->SetArgWord(i, val);
1✔
231
  if (r != asSUCCESS) {
1✔
232
    std::cerr << "Unable to set argument " << i << " to value at address "
×
233
              << val << std::endl;
×
234
    reportSetFunctionError(r);
×
235
  }
236
  return r == asSUCCESS;
1✔
237
}
238

239
template <> bool Engine::setFunctionArg<uint16_t>(int i, uint16_t val) {
1✔
240
  const int r = m_impl->m_context->SetArgWord(i, val);
1✔
241
  if (r != asSUCCESS) {
1✔
242
    std::cerr << "Unable to set argument " << i << " to value at address "
×
243
              << val << std::endl;
×
244
    reportSetFunctionError(r);
×
245
  }
246
  return r == asSUCCESS;
1✔
247
}
248

249
template <> bool Engine::setFunctionArg<int8_t>(int i, int8_t val) {
1✔
250
  const int r = m_impl->m_context->SetArgByte(i, val);
1✔
251
  if (r != asSUCCESS) {
1✔
252
    std::cerr << "Unable to set argument " << i << " to value at address "
×
253
              << val << std::endl;
×
254
    reportSetFunctionError(r);
×
255
  }
256
  return r == asSUCCESS;
1✔
257
}
258

259
template <> bool Engine::setFunctionArg<uint8_t>(int i, uint8_t val) {
1✔
260
  const int r = m_impl->m_context->SetArgByte(i, val);
1✔
261
  if (r != asSUCCESS) {
1✔
262
    std::cerr << "Unable to set argument " << i << " to value at address "
×
263
              << val << std::endl;
×
264
    reportSetFunctionError(r);
×
265
  }
266
  return r == asSUCCESS;
1✔
267
}
268

269
template <> bool Engine::setFunctionArg<char>(int i, char val) {
×
270
  const int r = m_impl->m_context->SetArgByte(i, val);
×
271
  if (r != asSUCCESS) {
×
272
    std::cerr << "Unable to set argument " << i << " to value at address "
×
273
              << val << std::endl;
×
274
    reportSetFunctionError(r);
×
275
  }
276
  return r == asSUCCESS;
×
277
}
278

279
template <> bool Engine::setFunctionArg<float>(int i, float val) {
2✔
280
  const int r = m_impl->m_context->SetArgFloat(i, val);
2✔
281
  if (r != asSUCCESS) {
2✔
282
    std::cerr << "Unable to set argument " << i << " to value at address "
×
283
              << val << std::endl;
×
284
    reportSetFunctionError(r);
×
285
  }
286
  return r == asSUCCESS;
2✔
287
}
288

289
template <> bool Engine::setFunctionArg<double>(int i, double val) {
1✔
290
  const int r = m_impl->m_context->SetArgDouble(i, val);
1✔
291
  if (r != asSUCCESS) {
1✔
292
    std::cerr << "Unable to set argument " << i << " to value at address "
×
293
              << val << std::endl;
×
294
    reportSetFunctionError(r);
×
295
  }
296
  return r == asSUCCESS;
1✔
297
}
298

299
template <> bool Engine::setFunctionArg<bool>(int i, bool val) {
1✔
300
  const int r = m_impl->m_context->SetArgByte(i, val);
1✔
301
  if (r != asSUCCESS) {
1✔
NEW
302
    std::cerr << "Unable to set argument " << i << " to value at address "
×
NEW
303
              << val << std::endl;
×
NEW
304
    reportSetFunctionError(r);
×
305
  }
306
  return r == asSUCCESS;
1✔
307
}
308

309
bool Engine::setFunctionObjectArg(int i, void *val) {
×
310
  const int r = m_impl->m_context->SetArgObject(i, val);
×
311
  if (r != asSUCCESS) {
×
312
    std::cerr << "Unable to set argument " << i << " to value at address "
×
313
              << val << std::endl;
×
314
    reportSetFunctionError(r);
×
315
  }
316
  return r == asSUCCESS;
×
317
}
318

NEW
319
bool Engine::setFunctionObjectArg(int i, const void *val) {
×
NEW
320
  const int r = m_impl->m_context->SetArgObject(i, const_cast<void*>(val));
×
NEW
321
  if (r != asSUCCESS) {
×
NEW
322
    std::cerr << "Unable to set argument " << i << " to value at address "
×
NEW
323
              << val << std::endl;
×
NEW
324
    reportSetFunctionError(r);
×
325
  }
NEW
326
  return r == asSUCCESS;
×
327
}
328

329
template <> void Engine::getReturnValue(int64_t *value) {
×
330
  *value = m_impl->m_context->GetReturnQWord();
×
331
}
×
332

333
template <> void Engine::getReturnValue(uint64_t *value) {
×
334
  *value = m_impl->m_context->GetReturnQWord();
×
335
}
×
336

337
template <> void Engine::getReturnValue(int32_t *value) {
4✔
338
  *value = m_impl->m_context->GetReturnDWord();
4✔
339
}
4✔
340

341
template <> void Engine::getReturnValue(uint32_t *value) {
1✔
342
  *value = m_impl->m_context->GetReturnDWord();
1✔
343
}
1✔
344

345
template <> void Engine::getReturnValue(int16_t *value) {
1✔
346
  *value = m_impl->m_context->GetReturnWord();
1✔
347
}
1✔
348

349
template <> void Engine::getReturnValue(uint16_t *value) {
1✔
350
  *value = m_impl->m_context->GetReturnWord();
1✔
351
}
1✔
352

353
template <> void Engine::getReturnValue(int8_t *value) {
2✔
354
  *value = m_impl->m_context->GetReturnByte();
2✔
355
}
2✔
356

357
template <> void Engine::getReturnValue(uint8_t *value) {
2✔
358
  *value = m_impl->m_context->GetReturnByte();
2✔
359
}
2✔
360

361
template <> void Engine::getReturnValue(float *value) {
10✔
362
  *value = m_impl->m_context->GetReturnFloat();
10✔
363
}
10✔
364

365
template <> void Engine::getReturnValue(double *value) {
1✔
366
  *value = m_impl->m_context->GetReturnDouble();
1✔
367
}
1✔
368

369
template <> void Engine::getReturnValue(bool *value) {
1✔
370
  *value = m_impl->m_context->GetReturnByte();
1✔
371
}
1✔
372
} // namespace aswpp
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc