From 301cf7b3243c07fa544614390218fe17689b54e4 Mon Sep 17 00:00:00 2001 From: xx Date: Tue, 18 Jun 2024 11:26:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89LUA=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/module/globalfuns.cpp | 33 +++++++++++++++++++++++++++++++++ src/module/globalfuns.h | 5 +++++ 2 files changed, 38 insertions(+) diff --git a/src/module/globalfuns.cpp b/src/module/globalfuns.cpp index acf9822..be95a71 100644 --- a/src/module/globalfuns.cpp +++ b/src/module/globalfuns.cpp @@ -16,17 +16,49 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web */ #include "globalfuns.h" +#include #include "util/counter.hpp" #include "util/codec.h" #include "util/time.h" #include "core/global.h" #include "core/app.h" +#include "core/statemanager.h" static ylib::counter s_counter_guid; std::string module::globalfuncs::website_dir(sol::this_state ts) { GET_APP; return app->config->website.dir; } +void module::globalfuncs::create_env(const std::string& lua_filepath,sol::this_state ts) +{ + GET_APP; + std::thread t([](std::string lua_filepath,fastweb::app* a) { + auto lua = a->state->get(); + std::string exception_string; + try + { + sol::load_result script = lua->state->load_file(a->config->website.dir + lua_filepath); + if (!script.valid()) { + sol::error err = script; + throw ylib::exception(err.what()); + } + sol::protected_function_result result = script(); + if (!result.valid()) { + sol::error err = result; + throw ylib::exception(err.what()); + } + } + catch (const std::exception& e) + { + if (a->config->website.debug) + a->log->error("[create_env][" + lua_filepath + "]: " + e.what(), __FILE__, __func__, __LINE__); + } + lua->state->collect_garbage(); + a->state->push(lua); + },lua_filepath,app); + + t.detach(); +} void module::globalfuncs::regist(sol::state* lua) { lua->set_function("fw_set_ptr", module::globalfuncs::set_ptr); @@ -39,6 +71,7 @@ void module::globalfuncs::regist(sol::state* lua) lua->set_function("fw_sleep_msec",ylib::system::sleep_msec); lua->set_function("fw_now_msec", ylib::time::now_msec); lua->set_function("fw_now_sec", ylib::time::now_sec); + lua->set_function("fw_create_env", module::globalfuncs::create_env); } std::string module::globalfuncs::make_software_guid() diff --git a/src/module/globalfuns.h b/src/module/globalfuns.h index 7b91a05..0741783 100644 --- a/src/module/globalfuns.h +++ b/src/module/globalfuns.h @@ -48,6 +48,11 @@ namespace module /// 网站目录 /// static std::string website_dir(sol::this_state ts); + /// + /// 创建环境 + /// + /// + static void create_env(const std::string& lua_filepath,sol::this_state ts); static void regist(sol::state* lua); };