<commit_before>﻿#ifndef _BUILD_NATIVE_CONTROL_V8_BASE_H_
#define _BUILD_NATIVE_CONTROL_V8_BASE_H_

#include "../js_base.h"
#include <iostream>

#include "v8.h"
#include "libplatform/libplatform.h"

#ifdef V8_VERSION_87_PLUS
#define kV8NormalString v8::NewStringType::kNormal
#define kV8ProduceCodeCache v8::ScriptCompiler::kEagerCompile
#define V8ContextFirstArg CV8Worker::GetCurrentContext(),
#define V8ContextOneArg CV8Worker::GetCurrentContext()
#define V8IsolateFirstArg CV8Worker::GetCurrent(),
#define V8IsolateOneArg CV8Worker::GetCurrent()
#define V8ToChecked ToChecked
#else
#define kV8NormalString v8::NewStringType::kNormal
#define kV8ProduceCodeCache v8::ScriptCompiler::kProduceCodeCache
#define V8ContextFirstArg CV8Worker::GetCurrentContext(),
#define V8ContextOneArg CV8Worker::GetCurrentContext()
#define V8IsolateFirstArg
#define V8IsolateOneArg CV8Worker::GetCurrent()
#ifdef V8_OS_XP
#define V8ToChecked FromJust
#else
#define V8ToChecked ToChecked
#endif
#endif

v8::Local<v8::String> CreateV8String(v8::Isolate* i, const char* str, const int& len = -1);
v8::Local<v8::String> CreateV8String(v8::Isolate* i, const std::string& str);

#ifdef __ANDROID__
    #include <JniLogUtils.h>
#endif

#ifdef V8_OS_XP
class MallocArrayBufferAllocator : public v8::ArrayBuffer::Allocator
{
public:
    virtual void* Allocate(size_t length)
    {
        void* ret = malloc(length);
        memset(ret, 0, length);
        return ret;
    }
    virtual void* AllocateUninitialized(size_t length)
    {
        return malloc(length);
    }
    virtual void Free(void* data, size_t length)
    {
        free(data);
    }
};
#endif

class CV8Initializer
{
private:
#ifdef V8_VERSION_87_PLUS
    std::unique_ptr<v8::Platform> m_platform;
#else
    v8::Platform* m_platform;
#endif
    v8::ArrayBuffer::Allocator* m_pAllocator;

public:
    CV8Initializer()
    {
        std::wstring sPrW = NSFile::GetProcessPath();
        std::string sPrA = U_TO_UTF8(sPrW);

        m_pAllocator = NULL;

    #ifndef V8_OS_XP
        v8::V8::InitializeICUDefaultLocation(sPrA.c_str());
        v8::V8::InitializeExternalStartupData(sPrA.c_str());
        #ifdef V8_VERSION_87_PLUS
        m_platform = v8::platform::NewDefaultPlatform();
        v8::V8::InitializePlatform(m_platform.get());
        #else
        m_platform = v8::platform::CreateDefaultPlatform();
        v8::V8::InitializePlatform(m_platform);
        #endif
        v8::V8::Initialize();
    #else
        m_platform = v8::platform::CreateDefaultPlatform();
        v8::V8::InitializePlatform(m_platform);
        v8::V8::Initialize();
        v8::V8::InitializeICU();
    #endif
    }
    ~CV8Initializer()
    {
        v8::V8::Dispose();
        v8::V8::ShutdownPlatform();
        #ifndef V8_VERSION_87_PLUS
        delete m_platform;
        #endif
        if (m_pAllocator)
            delete m_pAllocator;
    }

    v8::ArrayBuffer::Allocator* getAllocator()
    {
        return m_pAllocator;
    }

    v8::Isolate* CreateNew()
    {
        v8::Isolate::CreateParams create_params;
    #ifndef V8_OS_XP
        m_pAllocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    #else
        m_pAllocator = new MallocArrayBufferAllocator();
    #endif
        create_params.array_buffer_allocator = m_pAllocator;
        return v8::Isolate::New(create_params);
    }<commit_msg>Fix bug 51756<commit_after>﻿#ifndef _BUILD_NATIVE_CONTROL_V8_BASE_H_
#define _BUILD_NATIVE_CONTROL_V8_BASE_H_

#include "../js_base.h"
#include <iostream>

#include "v8.h"
#include "libplatform/libplatform.h"
#include "src/base/sys-info.h"

#ifdef V8_VERSION_87_PLUS
#define kV8NormalString v8::NewStringType::kNormal
#define kV8ProduceCodeCache v8::ScriptCompiler::kEagerCompile
#define V8ContextFirstArg CV8Worker::GetCurrentContext(),
#define V8ContextOneArg CV8Worker::GetCurrentContext()
#define V8IsolateFirstArg CV8Worker::GetCurrent(),
#define V8IsolateOneArg CV8Worker::GetCurrent()
#define V8ToChecked ToChecked
#else
#define kV8NormalString v8::NewStringType::kNormal
#define kV8ProduceCodeCache v8::ScriptCompiler::kProduceCodeCache
#define V8ContextFirstArg CV8Worker::GetCurrentContext(),
#define V8ContextOneArg CV8Worker::GetCurrentContext()
#define V8IsolateFirstArg
#define V8IsolateOneArg CV8Worker::GetCurrent()
#ifdef V8_OS_XP
#define V8ToChecked FromJust
#else
#define V8ToChecked ToChecked
#endif
#endif

v8::Local<v8::String> CreateV8String(v8::Isolate* i, const char* str, const int& len = -1);
v8::Local<v8::String> CreateV8String(v8::Isolate* i, const std::string& str);

#ifdef __ANDROID__
    #include <JniLogUtils.h>
#endif

#ifdef V8_OS_XP
class MallocArrayBufferAllocator : public v8::ArrayBuffer::Allocator
{
public:
    virtual void* Allocate(size_t length)
    {
        void* ret = malloc(length);
        memset(ret, 0, length);
        return ret;
    }
    virtual void* AllocateUninitialized(size_t length)
    {
        return malloc(length);
    }
    virtual void Free(void* data, size_t length)
    {
        free(data);
    }
};
#endif

class CV8Initializer
{
private:
#ifdef V8_VERSION_87_PLUS
    std::unique_ptr<v8::Platform> m_platform;
#else
    v8::Platform* m_platform;
#endif
    v8::ArrayBuffer::Allocator* m_pAllocator;

public:
    CV8Initializer()
    {
        std::wstring sPrW = NSFile::GetProcessPath();
        std::string sPrA = U_TO_UTF8(sPrW);

        m_pAllocator = NULL;

    #ifndef V8_OS_XP
        v8::V8::InitializeICUDefaultLocation(sPrA.c_str());
        v8::V8::InitializeExternalStartupData(sPrA.c_str());
        #ifdef V8_VERSION_87_PLUS
        m_platform = v8::platform::NewDefaultPlatform();
        v8::V8::InitializePlatform(m_platform.get());
        #else
        m_platform = v8::platform::CreateDefaultPlatform();
        v8::V8::InitializePlatform(m_platform);
        #endif
        v8::V8::Initialize();
    #else
        m_platform = v8::platform::CreateDefaultPlatform();
        v8::V8::InitializePlatform(m_platform);
        v8::V8::Initialize();
        v8::V8::InitializeICU();
    #endif
    }
    ~CV8Initializer()
    {
        v8::V8::Dispose();
        v8::V8::ShutdownPlatform();
        #ifndef V8_VERSION_87_PLUS
        delete m_platform;
        #endif
        if (m_pAllocator)
            delete m_pAllocator;
    }

    v8::ArrayBuffer::Allocator* getAllocator()
    {
        return m_pAllocator;
    }

    v8::Isolate* CreateNew()
    {
        v8::Isolate::CreateParams create_params;
    #ifndef V8_OS_XP
        m_pAllocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    #else
        m_pAllocator = new MallocArrayBufferAllocator();
    #endif
        create_params.array_buffer_allocator = m_pAllocator;

        int64_t nMaxVirtualMemory = v8::base::SysInfo::AmountOfVirtualMemory();
        if (0 == nMaxVirtualMemory)
            nMaxVirtualMemory = 4000000000; // 4Gb

        create_params.constraints.ConfigureDefaults(
              v8::base::SysInfo::AmountOfPhysicalMemory(),
              nMaxVirtualMemory);

        return v8::Isolate::New(create_params);
    }
{'fraction_non_alphanumeric': 0.11577157432336686, 'fraction_numerical': 0.028765764489159698, 'mean_word_length': 4.005673758865248, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '3429023', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2419, 'n_words': 408}