Skip to content

Commit

Permalink
core, refactor: use static class to init module.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed May 28, 2018
1 parent f1a4589 commit e605864
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 91 deletions.
11 changes: 7 additions & 4 deletions fibjs/src/base/Runtime.cpp
Expand Up @@ -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()
{
Expand Down
17 changes: 0 additions & 17 deletions fibjs/src/base/fibjs.cpp
Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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();

Expand Down
57 changes: 30 additions & 27 deletions fibjs/src/console/console.cpp
Expand Up @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions fibjs/src/console/console_log.cpp
Expand Up @@ -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)
{
Expand Down
13 changes: 8 additions & 5 deletions fibjs/src/coroutine/Fiber.cpp
Expand Up @@ -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)
{
Expand Down
38 changes: 20 additions & 18 deletions fibjs/src/crypto/Cipher.cpp
Expand Up @@ -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,
Expand Down Expand Up @@ -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"));
}
Expand Down
13 changes: 8 additions & 5 deletions fibjs/src/fs/fs_posix.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions fibjs/src/fs/fs_win32.cpp
Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions fibjs/src/process/SubProcess_posix.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -59,6 +60,7 @@ static void async_init_sprocess(void* v)

void init_process()
{
init_signal();
AsyncIO::run(async_init_sprocess);
}

Expand Down
3 changes: 3 additions & 0 deletions fibjs/src/process/SubProcess_win.cpp
Expand Up @@ -17,8 +17,11 @@

namespace fibjs {

void init_signal();

void init_process()
{
init_signal();
}

class PSTimer : public Timer {
Expand Down
6 changes: 3 additions & 3 deletions fibjs/src/process/process_signal.cpp
Expand Up @@ -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)
{
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e605864

Please sign in to comment.