Make MiString

This commit is contained in:
Zack Radisic
2025-09-10 21:16:36 -07:00
parent a3d91477a8
commit 29b6faadf8
3 changed files with 61 additions and 61 deletions

View File

@@ -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();
}

View File

@@ -3,68 +3,12 @@
#include "headers-handwritten.h"
#include "JavaScriptCore/SourceOrigin.h"
#include "ZigGlobalObject.h"
#include <mimalloc.h>
#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<char*>(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<char*>(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;
};

View File

@@ -0,0 +1,56 @@
#pragma once
#include "root.h"
#include <mimalloc.h>
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<char*>(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<char*>(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<const char> m_span;
};