Truly independent web browser
/*
* Copyright (c) 2022-2023, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Noncopyable.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/RefPtr.h>
#include <AK/Utf16String.h>
#include <AK/Vector.h>
#include <LibGC/Forward.h>
#include <LibJS/Export.h>
#include <LibJS/Forward.h>
#include <LibJS/Position.h>
namespace JS::FFI {
struct WeakSourceCode;
}
namespace JS {
// The source code that scripts, modules and functions are compiled from. A pointer to it is the Rust runtime's source
// code itself, which the runtime and its embedder share by reference counting. Only the runtime creates and destroys
// it, and only the VM's thread may reference or release it.
class JS_API SourceCode {
AK_MAKE_NONCOPYABLE(SourceCode);
AK_MAKE_NONMOVABLE(SourceCode);
public:
GC_HOLDS_NO_CELL_EDGES;
static NonnullRefPtr<SourceCode const> create(Utf16String filename, Utf16String code);
// Source code under another filename that shares the code of the given source code, parked or not.
static NonnullRefPtr<SourceCode const> create_sharing_code(Utf16String filename, SourceCode const&);
Utf16String const& filename() const;
// NB: The runtime may hold the code compressed, so this is a string of its own, not a reference to one.
Utf16String code() const;
size_t length_in_code_units() const;
void ref() const;
void unref() const;
SourceCode() = delete;
~SourceCode() = delete;
};
// A weak reference to source code, which tells whether anything still holds it. Only the VM's thread may use it.
class JS_API WeakSourceCode {
AK_MAKE_NONCOPYABLE(WeakSourceCode);
public:
explicit WeakSourceCode(SourceCode const&);
WeakSourceCode(WeakSourceCode&&);
WeakSourceCode& operator=(WeakSourceCode&&);
~WeakSourceCode();
bool is_alive() const;
RefPtr<SourceCode const> strong_ref() const;
private:
FFI::WeakSourceCode* m_weak { nullptr };
};
}
template<>
inline constexpr bool AllocatedWithCustomAllocator<JS::SourceCode> = true;