<commit_before>// Copyright (c) 2010-present Bifrost Entertainment AS and <NAME>
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)

#ifndef SCRIPT_JAVASCRIPT_MODULE_H_
#define SCRIPT_JAVASCRIPT_MODULE_H_

#include <string>

#include <duktape.h>

extern "C" {  // TODO: Remove workaround with Duktape v2.3.0
#include <duk_module_node.h>
}

#include "FileSystem/File.h"

namespace rainbow::duk::module
{
    template <typename... Args>
    auto throw_if(duk_context* ctx,
                  bool condition,
                  duk_errcode_t err_code,
                  Args&&... args)
    {
        if (condition)
        {
#ifdef __GNUC__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
            duk_push_error_object(ctx, err_code, std::forward<Args>(args)...);
            NOT_USED(duk_throw(ctx));
            UNREACHABLE();
#ifdef __GNUC__
#   pragma GCC diagnostic pop
#endif
        }

        return 0;
    }

    auto load(duk_context* ctx) -> duk_ret_t
    {
        constexpr char kModuleExtension[] = ".js";

        // Entry stack: [ resolved_id exports module ]
        auto resolved_id = duk_get_string(ctx, 0);
        throw_if(ctx,
                 is_empty(resolved_id),
                 DUK_ERR_TYPE_ERROR,
                 "invalid module name");

        thread_local std::string module;
        module = resolved_id;
        module += kModuleExtension;
<commit_msg>Append ".js" only when needed.<commit_after>// Copyright (c) 2010-present Bifrost Entertainment AS and <NAME>
// Distributed under the MIT License.
// (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)

#ifndef SCRIPT_JAVASCRIPT_MODULE_H_
#define SCRIPT_JAVASCRIPT_MODULE_H_

#include <string>

#include <duktape.h>

extern "C" {  // TODO: Remove workaround with Duktape v2.3.0
#include <duk_module_node.h>
}

#include "FileSystem/File.h"

namespace rainbow::duk::module
{
    template <typename... Args>
    auto throw_if(duk_context* ctx,
                  bool condition,
                  duk_errcode_t err_code,
                  Args&&... args)
    {
        if (condition)
        {
#ifdef __GNUC__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
            duk_push_error_object(ctx, err_code, std::forward<Args>(args)...);
            NOT_USED(duk_throw(ctx));
            UNREACHABLE();
#ifdef __GNUC__
#   pragma GCC diagnostic pop
#endif
        }

        return 0;
    }

    auto load(duk_context* ctx) -> duk_ret_t
    {
        constexpr char kModuleExtension[] = ".js";

        // Entry stack: [ resolved_id exports module ]
        auto resolved_id = duk_get_string(ctx, 0);
        throw_if(ctx,
                 is_empty(resolved_id),
                 DUK_ERR_TYPE_ERROR,
                 "invalid module name");

        thread_local std::string module;
        module = resolved_id;
        if (!ends_with(module, kModuleExtension))
            module += kModuleExtension;

{'fraction_non_alphanumeric': 0.1030995106035889, 'fraction_numerical': 0.005872756933115824, 'mean_word_length': 2.3766519823788546, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3688739', 'n_tokens_mistral': 1005, 'n_tokens_neox': 942, 'n_words': 235}