<commit_before>#include "duktape.h"
#include "dukbind.h"
#include "dukbindconf.h"
#include "dukbind_proxy_function.h"
#include "dukbind_private.h"
#include "utils/dukbind_debug.h"

namespace dukbind
{

    struct Box
    {
        void * ObjectPointer;
        size_t ClassIndex;
        finalizer_t Finalizer;
    };

    void Push( duk_context * ctx, const bool value )
    {
        duk_push_boolean( ctx, value );
    }

    void Push( duk_context * ctx, const char * value )
    {
        duk_push_string( ctx, value );
    }

    void Push( duk_context * ctx, const int value )
    {
        dukbind_assert(
            ( (int)(duk_double_t) value ) == value,
            "Value does not fit into a duk_double_t"
            );

        duk_push_number( ctx, (duk_double_t)value );
    }

    void Push( duk_context * ctx, const float value )
    {
        dukbind_assert(
            ( (float)(duk_double_t) value ) == value,
            "Value does not fit into a duk_double_t"
            );

         duk_push_number( ctx, (duk_double_t)value );
    }

    void Push( duk_context * ctx, const double value )
    {
        dukbind_assert(
            ( (double)(duk_double_t) value ) == value,
            "Value does not fit into a duk_double_t"
            );
         duk_push_number( ctx, (duk_double_t)value );
    }

    bool Get( duk_context * ctx, const int index, const bool * )
    {
        dukbind_assert( duk_is_boolean( ctx, index ), "No conversion is allowed in Get methods" );
        return duk_to_boolean( ctx, index );
    }

    const char * Get( duk_context * ctx, const int index, const char ** )
    {
        dukbind_assert( duk_is_string( ctx, index ), "No conversion is allowed in Get methods" );
        return duk_to_string( ctx, index );
    }

    int Get( duk_context * ctx, const int index, const int * )
    {
        dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" );

        duk_double_t double_result = duk_to_number( ctx, index );
        int int_result = static_cast<int>( double_result );

        dukbind_assert( double_result == (duk_double_t)int_result, "Number is not a valid integer" );

        return int_result;
    }

    float Get( duk_context * ctx, const int index, const float * )
    {
        dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" );

        duk_double_t double_result = duk_to_number( ctx, index );
        float float_result = static_cast<float>( double_result );

        #if DUKBIND_NO_FLOAT_CONVERSION
            dukbind_assert( double_result == (duk_double_t)float_result, "Number is not a valid integer" );
        #endif

        return float_result;
    }

    double Get( duk_context * ctx, const int index, const double * )
    {
        dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" );

        return duk_to_number( ctx, index );
    }

    static Box * PrivatePush( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer )
    {
        duk_push_global_object( ctx );
        duk_get_prop_string( ctx, -1, "Proxy" );
        duk_remove( ctx, -2 );

        duk_push_object( ctx );

        size_t require_size = sizeof( Box ) + object_size;

        Box * box = reinterpret_cast<Box*>( duk_push_fixed_buffer( ctx, require_size ) );

        duk_put_prop_string( ctx, -2, "\xFF" "Box" );

        duk_push_c_function( ctx, &internal::ClassFinalizer, 1 );
        duk_set_finalizer( ctx, -2 );

        duk_push_heap_stash( ctx );
        duk_get_prop_string( ctx, -1, "InstanceHandler" );
        duk_remove( ctx, -2 );
        duk_new( ctx, 2 );

        return box;
    }

    void * Push( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer )
    {
        Box * box = PrivatePush( ctx, class_index, object_size, finalizer );
        box->ClassIndex = class_index;
        box->ObjectPointer = box + 1;
        box->Finalizer = finalizer;

        return box->ObjectPointer;
    }

    void * Push( duk_context * ctx, const size_t class_index, void * object_pointer, finalizer_t finalizer )
    {
        Box * box = PrivatePush( ctx, class_index, 0, finalizer );
        box->ClassIndex = class_index;
        box->ObjectPointer = object_pointer;
        box->Finalizer = finalizer;

        return box->ObjectPointer;
    }

    void * Get( duk_context * ctx, duk_idx_t index, size_t & class_index, finalizer_t & finalizer )
    {
        duk_size_t size;
        duk_get_prop_string( ctx, index, "\xFF" "Box" );
        dukbind_assert( duk_is_fixed_buffer( ctx, -1 ), "Boxing is always implemented as fixed buffer" );
        Box * box = reinterpret_cast<Box*>( duk_to_buffer( ctx, -1, &size ) );
        duk_pop( ctx );

        dukbind_assert( sizeof( Box ) <= size, "Fixed buffer is not a box" );

        class_index = box->ClassIndex;
        finalizer = box->Finalizer;
        return box->ObjectPointer;
    }
<commit_msg>warnings<commit_after>#include "duktape.h"
#include "dukbind.h"
#include "dukbindconf.h"
#include "dukbind_proxy_function.h"
#include "dukbind_private.h"
#include "utils/dukbind_debug.h"

namespace dukbind
{

    struct Box
    {
        void * ObjectPointer;
        size_t ClassIndex;
        finalizer_t Finalizer;
    };

    void Push( duk_context * ctx, const bool value )
    {
        duk_push_boolean( ctx, value );
    }

    void Push( duk_context * ctx, const char * value )
    {
        duk_push_string( ctx, value );
    }

    void Push( duk_context * ctx, const int value )
    {
        dukbind_assert(
            ( (int)(duk_double_t) value ) == value,
            "Value does not fit into a duk_double_t"
            );

        duk_push_number( ctx, (duk_double_t)value );
    }

    void Push( duk_context * ctx, const float value )
    {
        dukbind_assert(
            ( (float)(duk_double_t) value ) == value,
            "Value does not fit into a duk_double_t"
            );

         duk_push_number( ctx, (duk_double_t)value );
    }

    void Push( duk_context * ctx, const double value )
    {
        dukbind_assert(
            ( (double)(duk_double_t) value ) == value,
            "Value does not fit into a duk_double_t"
            );
         duk_push_number( ctx, (duk_double_t)value );
    }

    bool Get( duk_context * ctx, const int index, const bool * )
    {
        dukbind_assert( duk_is_boolean( ctx, index ), "No conversion is allowed in Get methods" );
        return duk_to_boolean( ctx, index );
    }

    const char * Get( duk_context * ctx, const int index, const char ** )
    {
        dukbind_assert( duk_is_string( ctx, index ), "No conversion is allowed in Get methods" );
        return duk_to_string( ctx, index );
    }

    int Get( duk_context * ctx, const int index, const int * )
    {
        dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" );

        duk_double_t double_result = duk_to_number( ctx, index );
        int int_result = static_cast<int>( double_result );

        dukbind_assert( double_result == (duk_double_t)int_result, "Number is not a valid integer" );

        return int_result;
    }

    float Get( duk_context * ctx, const int index, const float * )
    {
        dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" );

        duk_double_t double_result = duk_to_number( ctx, index );
        float float_result = static_cast<float>( double_result );

        #if DUKBIND_NO_FLOAT_CONVERSION
            dukbind_assert( double_result == (duk_double_t)float_result, "Number is not a valid integer" );
        #endif

        return float_result;
    }

    double Get( duk_context * ctx, const int index, const double * )
    {
        dukbind_assert( duk_is_number( ctx, index ), "No conversion is allowed in Get methods" );

        return duk_to_number( ctx, index );
    }

    static Box * PrivatePush( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer )
    {
        duk_push_global_object( ctx );
        duk_get_prop_string( ctx, -1, "Proxy" );
        duk_remove( ctx, -2 );

        duk_push_object( ctx );

        size_t require_size = sizeof( Box ) + object_size;

        Box * box = reinterpret_cast<Box*>( duk_push_fixed_buffer( ctx, require_size ) );
        box->ClassIndex = class_index;
        box->Finalizer = finalizer;

        duk_put_prop_string( ctx, -2, "\xFF" "Box" );

        duk_push_c_function( ctx, &internal::ClassFinalizer, 1 );
        duk_set_finalizer( ctx, -2 );

        duk_push_heap_stash( ctx );
        duk_get_prop_string( ctx, -1, "InstanceHandler" );
        duk_remove( ctx, -2 );
        duk_new( ctx, 2 );

        return box;
    }

    void * Push( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer )
    {
        Box * box = PrivatePush( ctx, class_index, object_size, finalizer );
        box->ObjectPointer = box + 1;

        return box->ObjectPointer;
    }

    void * Push( duk_context * ctx, const size_t class_index, void * object_pointer, finalizer_t finalizer )
    {
        Box * box = PrivatePush( ctx, class_index, 0, finalizer );
        box->ObjectPointer = object_pointer;

        return box->ObjectPointer;
    }

    void * Get( duk_context * ctx, duk_idx_t index, size_t & class_index, finalizer_t & finalizer )
    {
        duk_size_t size;
        duk_get_prop_string( ctx, index, "\xFF" "Box" );
        dukbind_assert( duk_is_fixed_buffer( ctx, -1 ), "Boxing is always implemented as fixed buffer" );
        Box * box = reinterpret_cast<Box*>( duk_to_buffer( ctx, -1, &size ) );
        duk_pop( ctx );

        dukbind_assert( sizeof( Box ) <= size, "Fixed buffer is not a box" );

        class_index = box->ClassIndex;
        finalizer = box->Finalizer;
        return box->ObjectPointer;
    }

{'fraction_non_alphanumeric': 0.10056202328382176, 'fraction_numerical': 0.0024086712163789645, 'mean_word_length': 2.480614739783444, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, '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': '6462173', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3236, 'n_words': 854}