<commit_before>
/***************************************************************
 * Name:      Core.cpp
 * Purpose:   Codes for Node-CEF Core Class
 * Author:    <NAME> (<EMAIL>)
 * Created:   2016-05-24
 * Copyright: Studio GPBeta (www.gpbeta.com)
 * License:
 **************************************************************/

/// ============================================================================
/// declarations
/// ============================================================================

/// ----------------------------------------------------------------------------
/// Headers
/// ----------------------------------------------------------------------------

#include "ncjs/Core.h"
#include "ncjs/Process.h"
#include "ncjs/ModuleManager.h"
#include "ncjs/module.h"
#include "ncjs/constants.h"

#include <include/cef_command_line.h>
#include <include/cef_parser.h>

#include <sstream>

namespace ncjs {

/// ----------------------------------------------------------------------------
/// forward declaration
/// ----------------------------------------------------------------------------

NCJS_DECLARE_BUILTIN_MODULE(constants);
NCJS_DECLARE_BUILTIN_MODULE(natives);
NCJS_DECLARE_BUILTIN_MODULE(buffer);
NCJS_DECLARE_BUILTIN_MODULE(util);
NCJS_DECLARE_BUILTIN_MODULE(fs);
NCJS_DECLARE_BUILTIN_MODULE(os);
NCJS_DECLARE_BUILTIN_MODULE(uv);

// defined in natives.cpp
const CefString& GetMainSource();

/// ----------------------------------------------------------------------------
/// variables
/// ----------------------------------------------------------------------------

Core* Core::s_instance = NULL;

uv_loop_t* s_loop = NULL;

static CefRefPtr<CefCommandLine> s_argsNode;
static CefRefPtr<CefCommandLine> s_argsExec;

static CefString s_extension(L"\'use strict\'\n\
Object.defineProperty(this, \"ncjs\", {\n\
    get: function() {\n\
        native function init();\n\
        delete this.ncjs;\n\
        return this.ncjs = init();\n\
    },\n\
    configurable : true\n\
});");

static inline bool Url2Path(const CefString& url, CefString& filePath)
{
    CefURLParts parts;

    if (!CefParseURL(url, parts))
        return false;

    if (consts::str_file != parts.scheme.str)
        return false;
    
    cef_char_t* path = parts.path.str;
    size_t len = parts.path.length;
    
    // windows : /C:/A/B.html
    if (len > 3 && path[2] == NCJS_TEXT(':')) {
        // convert '/' to '\\'
        for (size_t i = 0; i < len; ++i) {
            if (path[i] == NCJS_TEXT('/'))
                path[i] = NCJS_TEXT('\\');
        }
        // remove first slash
        path += 1;
    } // else /root/A/B.html

    filePath = path;

    return true;
}

static class ExtensionHandler : public CefV8Handler {
public:
    virtual bool Execute(const CefString& name, CefRefPtr<CefV8Value> object,
        const CefV8ValueList& args, CefRefPtr<CefV8Value>& retval, CefString& except) OVERRIDE;

    NCJS_IMPL_NO_REFCOUNTING(ExtensionHandler);
} s_handler;

/// ============================================================================
/// implementation
/// ============================================================================

bool ExtensionHandler::Execute(const CefString& name, CefRefPtr<CefV8Value> object,
        const CefV8ValueList& args, CefRefPtr<CefV8Value>& retval, CefString& except)
{
    if (!Core::IsInitialized()) {
        except = NCJS_TEXT("Node-CEF not initialized.");
        return true;
    }

    CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();<commit_msg>Fix Node.js can't find local modules when page URL contains escaped characters<commit_after>
/***************************************************************
 * Name:      Core.cpp
 * Purpose:   Codes for Node-CEF Core Class
 * Author:    <NAME> (<EMAIL>)
 * Created:   2016-05-24
 * Copyright: Studio GPBeta (www.gpbeta.com)
 * License:
 **************************************************************/

/// ============================================================================
/// declarations
/// ============================================================================

/// ----------------------------------------------------------------------------
/// Headers
/// ----------------------------------------------------------------------------

#include "ncjs/Core.h"
#include "ncjs/Process.h"
#include "ncjs/ModuleManager.h"
#include "ncjs/module.h"
#include "ncjs/constants.h"

#include <include/cef_command_line.h>
#include <include/cef_parser.h>

#include <sstream>

namespace ncjs {

/// ----------------------------------------------------------------------------
/// forward declaration
/// ----------------------------------------------------------------------------

NCJS_DECLARE_BUILTIN_MODULE(constants);
NCJS_DECLARE_BUILTIN_MODULE(natives);
NCJS_DECLARE_BUILTIN_MODULE(buffer);
NCJS_DECLARE_BUILTIN_MODULE(util);
NCJS_DECLARE_BUILTIN_MODULE(fs);
NCJS_DECLARE_BUILTIN_MODULE(os);
NCJS_DECLARE_BUILTIN_MODULE(uv);

// defined in natives.cpp
const CefString& GetMainSource();

/// ----------------------------------------------------------------------------
/// variables
/// ----------------------------------------------------------------------------

Core* Core::s_instance = NULL;

uv_loop_t* s_loop = NULL;

static CefRefPtr<CefCommandLine> s_argsNode;
static CefRefPtr<CefCommandLine> s_argsExec;

static CefString s_extension(L"\'use strict\'\n\
Object.defineProperty(this, \"ncjs\", {\n\
    get: function() {\n\
        native function init();\n\
        delete this.ncjs;\n\
        return this.ncjs = init();\n\
    },\n\
    configurable : true\n\
});");

static inline bool Url2Path(const CefString& url, CefString& filePath)
{
    CefURLParts parts;

    if (!CefParseURL(url, parts))
        return false;

    if (consts::str_file != parts.scheme.str)
        return false;
    
    cef_char_t* path = parts.path.str;
    size_t len = parts.path.length;
    
    // windows : /C:/A/B.html
    if (len > 3 && path[2] == NCJS_TEXT(':')) {
        // convert '/' to '\\'
        for (size_t i = 0; i < len; ++i) {
            if (path[i] == NCJS_TEXT('/'))
                path[i] = NCJS_TEXT('\\');
        }
        // remove first slash
        path += 1;
        len -=1;
    } // else /root/A/B.html

    filePath = CefURIDecode(CefString(path, len, false), true, UU_SPACES);

    return true;
}

static class ExtensionHandler : public CefV8Handler {
public:
    virtual bool Execute(const CefString& name, CefRefPtr<CefV8Value> object,
        const CefV8ValueList& args, CefRefPtr<CefV8Value>& retval, CefString& except) OVERRIDE;

    NCJS_IMPL_NO_REFCOUNTING(ExtensionHandler);
} s_handler;

/// ============================================================================
/// implementation
/// ============================================================================

bool ExtensionHandler::Execute(const CefString& name, CefRefPtr<CefV8Value> object,
        const CefV8ValueList& args, CefRefPtr<CefV8Value>& retval, CefString& except)
{
    if (!Core::IsInitialized()) {
        except = NCJS_TEXT("Node-CEF not initialized.");
        return true;
    }

    CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
{'fraction_non_alphanumeric': 0.35394190871369297, 'fraction_numerical': 0.006224066390041493, 'mean_word_length': 2.2166370106761564, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5234034', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2022, 'n_words': 404}