diff --git a/src/bake/DevServerSourceProvider.cpp b/src/bake/DevServerSourceProvider.cpp index 3667664384..291e0883ea 100644 --- a/src/bake/DevServerSourceProvider.cpp +++ b/src/bake/DevServerSourceProvider.cpp @@ -11,7 +11,7 @@ extern "C" BunString DevServerSourceProvider__getSourceSlice(Bake::DevServerSour return Bun::toStringView(provider->source()); } -extern "C" Bake::SourceMapData DevServerSourceProvider__getSourceMapJSON(Bake::DevServerSourceProvider* provider) +extern "C" MiCString DevServerSourceProvider__getSourceMapJSON(Bake::DevServerSourceProvider* provider) { return provider->sourceMapJSON(); } diff --git a/src/bake/DevServerSourceProvider.h b/src/bake/DevServerSourceProvider.h index 111764f143..837c045742 100644 --- a/src/bake/DevServerSourceProvider.h +++ b/src/bake/DevServerSourceProvider.h @@ -3,68 +3,12 @@ #include "headers-handwritten.h" #include "JavaScriptCore/SourceOrigin.h" #include "ZigGlobalObject.h" -#include +#include "MiString.h" namespace Bake { class DevServerSourceProvider; -class SourceMapJSONString { -public: - SourceMapJSONString(const char* ptr, size_t length) - : m_ptr(ptr) - , m_length(length) - { - } - - ~SourceMapJSONString() - { - if (m_ptr) { - mi_free(const_cast(m_ptr)); - } - } - - // Delete copy constructor and assignment operator to prevent double free - SourceMapJSONString(const SourceMapJSONString&) = delete; - SourceMapJSONString& operator=(const SourceMapJSONString&) = delete; - - // Move constructor and assignment - SourceMapJSONString(SourceMapJSONString&& other) noexcept - : m_ptr(other.m_ptr) - , m_length(other.m_length) - { - other.m_ptr = nullptr; - other.m_length = 0; - } - - SourceMapJSONString& operator=(SourceMapJSONString&& other) noexcept - { - if (this != &other) { - if (m_ptr) { - mi_free(const_cast(m_ptr)); - } - m_ptr = other.m_ptr; - m_length = other.m_length; - other.m_ptr = nullptr; - other.m_length = 0; - } - return *this; - } - - const char* ptr() const { return m_ptr; } - size_t length() const { return m_length; } - -private: - const char* m_ptr; - size_t m_length; -}; - -// Struct to return source map data to Zig -struct SourceMapData { - const char* ptr; - size_t length; -}; - // Function to be implemented in Zig to register the source provider extern "C" void Bun__addDevServerSourceProvider(void* bun_vm, DevServerSourceProvider* opaque_source_provider, BunString* specifier); extern "C" void Bun__removeDevServerSourceProvider(void* bun_vm, DevServerSourceProvider* opaque_source_provider, BunString* specifier); @@ -90,9 +34,9 @@ public: return provider; } - SourceMapData sourceMapJSON() const + MiCString sourceMapJSON() const { - return SourceMapData { m_sourceMapJSON.ptr(), m_sourceMapJSON.length() }; + return m_sourceMapJSON.asCString(); } private: @@ -121,7 +65,7 @@ private: } } - SourceMapJSONString m_sourceMapJSON; + MiString m_sourceMapJSON; Zig::GlobalObject* m_globalObject; BunString m_specifier; }; diff --git a/src/bun.js/bindings/MiString.h b/src/bun.js/bindings/MiString.h new file mode 100644 index 0000000000..7502328692 --- /dev/null +++ b/src/bun.js/bindings/MiString.h @@ -0,0 +1,56 @@ +#pragma once +#include "root.h" +#include + + +struct MiCString { + const char* ptr; + size_t length; +}; + +/// A string which is owned by mimalloc and can be mi_free'd +class MiString { +public: + MiString(const char* ptr, size_t length) + : m_span(ptr, length) + { + } + + ~MiString() + { + if (m_span.data()) { + mi_free(const_cast(m_span.data())); + } + } + + // Delete copy constructor and assignment operator to prevent double free + MiString(const MiString&) = delete; + MiString& operator=(const MiString&) = delete; + + // Move constructor and assignment + MiString(MiString&& other) noexcept + : m_span(other.m_span) + { + other.m_span = {}; + } + + MiString& operator=(MiString&& other) noexcept + { + if (this != &other) { + if (m_span.data()) { + mi_free(const_cast(m_span.data())); + } + m_span = other.m_span; + other.m_span = {}; + } + return *this; + } + + MiCString asCString() const + { + return MiCString { m_span.data(), m_span.size() }; + } + +private: + std::span m_span; +}; \ No newline at end of file