Skip to content

Commit

Permalink
Implementation of the sys.sleep function in standard library.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Dec 12, 2024
1 parent 7be0e9d commit 8fd640c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions std/n8std/Sys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <myshell.hpp>
#include <unordered_map>

#if defined(_WIN32) || defined(_WIN64) || defined(WIN32) || defined(WIN64)
# include <windows.h>
#endif

std::unordered_map<std::string, std::shared_ptr<MyShell>> shellMap;

N8_FUNC(sys_quickShell) {
Expand Down Expand Up @@ -235,3 +239,27 @@ N8_FUNC(sys_shellClose) {
shellMap.erase(uuid);
return DynamicObject(uuid);
}

N8_FUNC(sys_sleep) {
if(args.size() != 1)
throw TerminativeThrowSignal(
std::move(address),
"Expecting 1 argument, got " +
std::to_string(args.size())
);

DynamicObject value = args.at(0);
#if defined(_WIN32) || defined(_WIN64)
Sleep(
static_cast<DWORD>(value.getNumber())
);
#else
std::this_thread::sleep_for(
std::chrono::milliseconds(
static_cast<int64_t>(value.getNumber())
)
);
#endif

return DynamicObject();
}
2 changes: 2 additions & 0 deletions std/n8std/Sys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ N8_FUNC(sys_shellExitCode);
N8_FUNC(sys_shellProcessId);
N8_FUNC(sys_shellClose);

N8_FUNC(sys_sleep);

N8_LIB_END

#ifdef __clang__
Expand Down

0 comments on commit 8fd640c

Please sign in to comment.