Skip to content

File runtime.h

File List > bindings > runtime.h

Go to the documentation of this file

#pragma once

#include <systemc.h>

// This shows C++ exception what()
#define SOL_EXCEPTIONS_SAFE_PROPAGATION 1
#define SOL_EXCEPTIONS_ALWAYS_UNSAFE 1

#include <sol/sol.hpp>

namespace lua {

sol::state &Runtime();

class Ctor {
 public:
  Ctor(const Ctor &) = delete;
  Ctor &operator=(const Ctor &) = delete;

  template <class TFunc>
  Ctor(TFunc init, const char *scope) {
    auto start = std::chrono::high_resolution_clock::now();
    init(Runtime()[scope].get_or_create<sol::table>());
    auto end = std::chrono::high_resolution_clock::now();
    auto duration =
        std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    load_time_us_ += duration.count();
    num_modules_++;
  }

  static uint32_t num_modules() { return num_modules_; }
  static double load_time_us() { return load_time_us_; }

 private:
  inline static uint32_t num_modules_ = 0;
  inline static double load_time_us_ = 0;
};

}  // namespace lua

#define LUA_CTOR_NAME(name) _##name##_lua_ctor

#define LUA_CTOR_FUNC_NAME(name) _##name##_lua_func_ctor

#define LUA_CTOR(scope, name)                                             \
  static void LUA_CTOR_FUNC_NAME(name)(sol::table);                       \
  static lua::Ctor LUA_CTOR_NAME(name){LUA_CTOR_FUNC_NAME(name), #scope}; \
  static void LUA_CTOR_FUNC_NAME(name)(sol::table scope)