Permalink
Browse files

core, refactor: use static class to init module.

  • Loading branch information...
xicilion committed May 28, 2018
1 parent f1a4589 commit e6058640c202b31cfc9221312f910774ad2c702e
View
@@ -22,10 +22,13 @@ namespace fibjs {
static int32_t s_tls_rt;
void init_rt()
{
s_tls_rt = exlib::Fiber::tlsAlloc();
}
class rt_initer {
public:
rt_initer()
{
s_tls_rt = exlib::Fiber::tlsAlloc();
}
} s_rt_initer;
void Runtime::reg()
{
View
@@ -18,18 +18,10 @@
namespace fibjs {
void init_date();
void init_rt();
void init_argv(int32_t argc, char** argv);
void init_start_argv(int32_t argc, char** argv);
void init_prof();
void init_cipher();
void init_acThread();
void init_logger();
void init_aio();
void init_fs();
void init_fiber();
void init_signal();
void init_color();
void init_process();
void options(int32_t& pos, char* argv[]);
result_t ifZipFile(exlib::string filename, bool& retVal);
@@ -52,16 +44,9 @@ void init()
exlib::Service::init(cpus + 1);
// init_prof();
init_date();
init_cipher();
init_acThread();
init_logger();
init_aio();
init_fs();
init_fiber();
init_signal();
init_color();
init_process();
srand((unsigned int)time(0));
@@ -183,8 +168,6 @@ void main(int32_t argc, char** argv)
char** m_argv;
};
init_rt();
MainThread* main_thread = new MainThread(argc, argv);
main_thread->start();
@@ -75,34 +75,37 @@ exlib::string COLOR_LIGHTWHITE = "";
exlib::string COLOR_TITLE = "";
void init_color()
{
bool isatty = false;
tty_base::isatty(1, isatty);
if (isatty) {
COLOR_RESET = "\x1b[0m";
COLOR_BLACK = "\x1b[0;30m"; /* Black */
COLOR_RED = "\x1b[0;31m"; /* Red */
COLOR_GREEN = "\x1b[0;32m"; /* Green */
COLOR_YELLOW = "\x1b[0;33m"; /* Yellow */
COLOR_BLUE = "\x1b[0;34m"; /* Blue */
COLOR_MAGENTA = "\x1b[0;35m"; /* Magenta */
COLOR_CYAN = "\x1b[0;36m"; /* Cyan */
COLOR_WHITE = "\x1b[0;37m"; /* White */
COLOR_NORMAL = "\x1b[0;39m"; /* Normal */
COLOR_GREY = "\x1B[90m"; /* Grey */
COLOR_LIGHTRED = "\x1b[1;31m"; /* Red */
COLOR_LIGHTGREEN = "\x1b[1;32m"; /* Green */
COLOR_LIGHTYELLOW = "\x1b[1;33m"; /* Yellow */
COLOR_LIGHTBLUE = "\x1b[1;34m"; /* Blue */
COLOR_LIGHTMAGENTA = "\x1b[1;35m"; /* Magenta */
COLOR_LIGHTCYAN = "\x1b[1;36m"; /* Cyan */
COLOR_LIGHTWHITE = "\x1b[1;37m"; /* White */
COLOR_TITLE = "\x1B[1;39m";
class color_initer {
public:
color_initer()
{
bool isatty = false;
tty_base::isatty(1, isatty);
if (isatty) {
COLOR_RESET = "\x1b[0m";
COLOR_BLACK = "\x1b[0;30m"; /* Black */
COLOR_RED = "\x1b[0;31m"; /* Red */
COLOR_GREEN = "\x1b[0;32m"; /* Green */
COLOR_YELLOW = "\x1b[0;33m"; /* Yellow */
COLOR_BLUE = "\x1b[0;34m"; /* Blue */
COLOR_MAGENTA = "\x1b[0;35m"; /* Magenta */
COLOR_CYAN = "\x1b[0;36m"; /* Cyan */
COLOR_WHITE = "\x1b[0;37m"; /* White */
COLOR_NORMAL = "\x1b[0;39m"; /* Normal */
COLOR_GREY = "\x1B[90m"; /* Grey */
COLOR_LIGHTRED = "\x1b[1;31m"; /* Red */
COLOR_LIGHTGREEN = "\x1b[1;32m"; /* Green */
COLOR_LIGHTYELLOW = "\x1b[1;33m"; /* Yellow */
COLOR_LIGHTBLUE = "\x1b[1;34m"; /* Blue */
COLOR_LIGHTMAGENTA = "\x1b[1;35m"; /* Magenta */
COLOR_LIGHTCYAN = "\x1b[1;36m"; /* Cyan */
COLOR_LIGHTWHITE = "\x1b[1;37m"; /* White */
COLOR_TITLE = "\x1B[1;39m";
}
}
}
} s_color_initer;
extern std_logger* s_std;
void asyncLog(int32_t priority, exlib::string msg);
@@ -16,10 +16,13 @@ stream_logger* s_stream;
#define MAX_LOGGER 10
static logger* s_logs[MAX_LOGGER];
void init_logger()
{
s_std = new std_logger;
}
class logger_initer {
public:
logger_initer()
{
s_std = new std_logger;
}
} s_logger_initer;
void outLog(int32_t priority, exlib::string msg)
{
@@ -19,11 +19,14 @@ namespace fibjs {
int32_t g_spareFibers;
static int32_t g_tlsCurrent;
void init_fiber()
{
g_spareFibers = MAX_IDLE;
g_tlsCurrent = exlib::Fiber::tlsAlloc();
}
class fiber_initer {
public:
fiber_initer()
{
g_spareFibers = MAX_IDLE;
g_tlsCurrent = exlib::Fiber::tlsAlloc();
}
} s_fiber_initer;
void JSFiber::fiber_proc(void* p)
{
@@ -43,22 +43,25 @@ static struct _cipher_size {
{ "ARC4-128", 0, {} } }
};
void init_cipher()
{
int32_t i, j, k;
for (i = 0; i < PROVIDER_COUNT; i++)
for (j = 0; j < SIZE_COUNT; j++)
if (s_sizes[i][j].name)
for (k = 1; k < MODE_COUNT; k++) {
exlib::string name = s_sizes[i][j].name;
name.append(s_modes[k]);
s_sizes[i][j].cis[k] = mbedtls_cipher_info_from_string(name.c_str());
if (s_sizes[i][j].cis[k])
s_sizes[i][j].size = s_sizes[i][j].cis[k]->key_bitlen;
}
}
class cipher_initer {
public:
cipher_initer()
{
int32_t i, j, k;
for (i = 0; i < PROVIDER_COUNT; i++)
for (j = 0; j < SIZE_COUNT; j++)
if (s_sizes[i][j].name)
for (k = 1; k < MODE_COUNT; k++) {
exlib::string name = s_sizes[i][j].name;
name.append(s_modes[k]);
s_sizes[i][j].cis[k] = mbedtls_cipher_info_from_string(name.c_str());
if (s_sizes[i][j].cis[k])
s_sizes[i][j].size = s_sizes[i][j].cis[k]->key_bitlen;
}
}
} s_cipher_initer;
result_t Cipher_base::_new(int32_t provider, int32_t mode, Buffer_base* key,
Buffer_base* iv, obj_ptr<Cipher_base>& retVal,
@@ -156,8 +159,7 @@ result_t Cipher::init(exlib::string& key, exlib::string& iv)
m_key = key;
m_iv = iv;
if (m_iv.length() && mbedtls_cipher_set_iv(&m_ctx, (unsigned char*)m_iv.c_str(),
m_iv.length())) {
if (m_iv.length() && mbedtls_cipher_set_iv(&m_ctx, (unsigned char*)m_iv.c_str(), m_iv.length())) {
m_iv.resize(0);
return CHECK_ERROR(Runtime::setError("Cipher: Invalid iv size"));
}
View
@@ -66,10 +66,13 @@ inline int _copyfile(int ifd, int ofd)
namespace fibjs {
void init_fs()
{
::umask(0);
}
class fs_initer {
public:
fs_initer()
{
::umask(0);
}
} s_fs_initer;
result_t fs_base::exists(exlib::string path, bool& retVal, AsyncEvent* ac)
{
@@ -215,7 +218,7 @@ result_t fs_base::fdatasync(int32_t fd, AsyncEvent* ac)
return CHECK_ERROR(CALL_E_NOSYNC);
#if defined(Darwin) || defined(FreeBSD)
#ifdef F_FULLFSYNC
#ifdef F_FULLFSYNC
if (::fcntl(fd, F_FULLFSYNC))
return CHECK_ERROR(LastError());
#else
View
@@ -52,16 +52,19 @@ static BOOLEAN(WINAPI* pCreateSymbolicLink)(
static DWORD(WINAPI* pGetFinalPathNameByHandle)(
HANDLE hFile, LPCWSTR lpszFilePath, DWORD cchFilePath, DWORD dwFlags);
void init_fs()
{
HMODULE hKernel = GetModuleHandleA("KERNEL32");
class fs_initer {
public:
fs_initer()
{
HMODULE hKernel = GetModuleHandleA("KERNEL32");
pCreateSymbolicLink = (BOOLEAN(WINAPI*)(LPCWSTR, LPCWSTR, DWORD))
GetProcAddress(hKernel, "CreateSymbolicLinkW");
pCreateSymbolicLink = (BOOLEAN(WINAPI*)(LPCWSTR, LPCWSTR, DWORD))
GetProcAddress(hKernel, "CreateSymbolicLinkW");
pGetFinalPathNameByHandle = (DWORD(WINAPI*)(HANDLE, LPCWSTR, DWORD, DWORD))
GetProcAddress(hKernel, "GetFinalPathNameByHandleW");
}
pGetFinalPathNameByHandle = (DWORD(WINAPI*)(HANDLE, LPCWSTR, DWORD, DWORD))
GetProcAddress(hKernel, "GetFinalPathNameByHandleW");
}
} s_fs_initer;
result_t fs_base::exists(exlib::string path, bool& retVal, AsyncEvent* ac)
{
@@ -21,6 +21,7 @@ namespace fibjs {
static exlib::spinlock s_lock;
static std::map<pid_t, obj_ptr<SubProcess>> s_ids;
void init_signal();
static result_t async_signal_handler(int32_t n)
{
@@ -59,6 +60,7 @@ static void async_init_sprocess(void* v)
void init_process()
{
init_signal();
AsyncIO::run(async_init_sprocess);
}
@@ -17,8 +17,11 @@
namespace fibjs {
void init_signal();
void init_process()
{
init_signal();
}
class PSTimer : public Timer {
@@ -21,7 +21,7 @@
namespace fibjs {
extern exlib::LockedList<Isolate> s_isolates;
exlib::atomic s_check_callback;
static exlib::atomic s_check_callback;
static void _InterruptCallback(v8::Isolate* v8_isolate, void* data)
{
@@ -47,7 +47,7 @@ static result_t async_signal(const char* name)
return 0;
}
void on_signal(int32_t s)
static void on_signal(int32_t s)
{
const char* name = NULL;
@@ -80,7 +80,7 @@ typedef BOOL(WINAPI* MINIDUMPWRITEDUMP)(HANDLE hProcess, DWORD dwPid, HANDLE hFi
static MINIDUMPWRITEDUMP s_pDump;
HANDLE CreateUniqueDumpFile()
static HANDLE CreateUniqueDumpFile()
{
char fname[MAX_PATH];
int32_t l, i;

0 comments on commit e605864

Please sign in to comment.