forked from RbxStu/RbxStu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Execution.cpp
58 lines (46 loc) · 1.81 KB
/
Execution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Created by Dottik on 14/11/2023.
//
#include <iostream>
#include <Execution.hpp>
#include <Luau/Compiler.h>
#include "Environment/Environment.hpp"
#include "Security.hpp"
#include "Utilities.hpp"
#include "oxorany.hpp"
Execution *Execution::singleton = nullptr;
Execution *Execution::get_singleton() {
if (Execution::singleton == nullptr)
Execution::singleton = new Execution();
return Execution::singleton;
}
std::string Execution::compile_to_bytecode(const std::string &code) {
auto opts = Luau::CompileOptions{};
opts.debugLevel = 1;
opts.optimizationLevel = 2;
// const char *mutableGlobals[] = {"_G", nullptr};
// opts.mutableGlobals = mutableGlobals;
return Luau::compile(code, opts);
}
int Execution::lua_loadstring(lua_State *L, const std::string &code, std::string chunkName, RBX::Identity identity) {
auto utilities{Module::Utilities::get_singleton()};
auto wCode = utilities->to_wchar(code.c_str());
delete[] wCode;
auto bytecode = this->compile_to_bytecode(code);
if (chunkName.empty())
chunkName = utilities->get_random_string(32);
if (luau_load(L, chunkName.c_str(), bytecode.c_str(), bytecode.size(), 0) != LUA_OK) {
const char *err = lua_tostring(L, -1);
printf("[Execution::lua_loadstring] luau_load failed with error \'%s\'", err);
lua_pop(L, 1);
lua_pushnil(L);
lua_pushlstring(L, err, strlen(err));
return 2;
}
auto *pClosure = const_cast<Closure *>(static_cast<const Closure *>(lua_topointer(L, -1)));
RBX::Security::Bypasses::set_luaclosure_security(pClosure, identity);
return 1;
}
int Execution::register_environment(lua_State *L, bool useInitScript, _In_ _Out_ int *schedulerKey) {
return Environment::get_singleton()->register_env(L, useInitScript, schedulerKey);
}