dlang-sqlite inital
This commit is contained in:
commit
ce0a6c08e4
160 files changed
+362540
No files matched your search
@@ -0,0 +1,124 @@
|
||||
#ifndef DLANG_LIBS_DEV_HPP
|
||||
#define DLANG_LIBS_DEV_HPP
|
||||
|
||||
#include "../vm.hpp"
|
||||
#include "../helpers.hpp"
|
||||
|
||||
static void NativePrint(DlangVM* vm)
|
||||
{
|
||||
std::stringstream ss;
|
||||
auto argCount = dlang_top(vm);
|
||||
|
||||
std::vector<StackValue> values;
|
||||
for (int i = 0; i < argCount; ++i)
|
||||
values.push_back(dlang_totoken(vm));
|
||||
|
||||
for (auto it = values.rbegin(); it != values.rend(); ++it)
|
||||
{
|
||||
if (it->type == StackValueType::NIL)
|
||||
{
|
||||
printf("nil");
|
||||
}
|
||||
else {
|
||||
std::visit([&ss](auto&& arg) {
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
if constexpr (std::is_same_v<T, bool>) {
|
||||
ss << (arg ? "true" : "false") << " ";
|
||||
}
|
||||
else if constexpr (std::is_same_v<T, double>) {
|
||||
ss << DoubleToString(arg) << " ";
|
||||
}
|
||||
else {
|
||||
ss << arg << " ";
|
||||
}
|
||||
}, it->data);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ss.str() << '\n';
|
||||
}
|
||||
|
||||
static void NativeToNumber(DlangVM* vm)
|
||||
{
|
||||
auto content = dlang_tostring(vm);
|
||||
try {
|
||||
dlang_pushnumber(vm, std::stod(content));
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
dlang_pushnumber(vm, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void NativeIsNil(DlangVM* vm)
|
||||
{
|
||||
dlang_pushboolean(vm, dlang_isnil(vm));
|
||||
}
|
||||
|
||||
static void NativeIsString(DlangVM* vm)
|
||||
{
|
||||
dlang_pushboolean(vm, dlang_isstring(vm));
|
||||
}
|
||||
|
||||
static void NativeIsNumber(DlangVM* vm)
|
||||
{
|
||||
dlang_pushboolean(vm, dlang_isnumber(vm));
|
||||
}
|
||||
|
||||
static void NativeIsBoolean(DlangVM* vm)
|
||||
{
|
||||
dlang_pushboolean(vm, dlang_isboolean(vm));
|
||||
}
|
||||
|
||||
static void NativeRequire(DlangVM* vm)
|
||||
{
|
||||
auto name = dlang_tostring(vm);
|
||||
|
||||
if (vm->loadedPlugins.Contains(name))
|
||||
{
|
||||
dlang_pushstring(vm, "plugin already loaded!");
|
||||
return;
|
||||
}
|
||||
|
||||
void* rawPtr = pluginManager->GetOrLoadInitFunc("./plugins/" + name);
|
||||
if (rawPtr)
|
||||
{
|
||||
vm->loadedPlugins.push(name);
|
||||
|
||||
using InitFunc = void(*)(DlangVM*);
|
||||
auto init = reinterpret_cast<InitFunc>(rawPtr);
|
||||
init(vm);
|
||||
|
||||
dlang_pushstring(vm, "plugin loaded!");
|
||||
return;
|
||||
}
|
||||
|
||||
dlang_pushstring(vm, "Failed to load plugin!");
|
||||
return;
|
||||
}
|
||||
|
||||
inline void openlib_dev(DlangVM* vm)
|
||||
{
|
||||
dlang_pushcfunction(vm, NativePrint);
|
||||
dlang_setglobal(vm, "print");
|
||||
|
||||
dlang_pushcfunction(vm, NativeToNumber);
|
||||
dlang_setglobal(vm, "tonumber");
|
||||
|
||||
dlang_pushcfunction(vm, NativeIsNil);
|
||||
dlang_setglobal(vm, "isnil");
|
||||
|
||||
dlang_pushcfunction(vm, NativeIsString);
|
||||
dlang_setglobal(vm, "isstring");
|
||||
|
||||
dlang_pushcfunction(vm, NativeIsNumber);
|
||||
dlang_setglobal(vm, "isnumber");
|
||||
|
||||
dlang_pushcfunction(vm, NativeIsBoolean);
|
||||
dlang_setglobal(vm, "isbool");
|
||||
|
||||
dlang_pushcfunction(vm, NativeRequire);
|
||||
dlang_setglobal(vm, "require");
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user