/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2008 litl, LLC

#pragma once

#include <config.h>
#include <string>
#include <unordered_map>

#include <js/RootingAPI.h>  // for MutableHandle
#include <js/TypeDecls.h>

#include "gjs/macros.h"

namespace Gjs {
class NativeModuleDefineFuncs {
    NativeModuleDefineFuncs() {}
    using DefineModuleFunc = bool (*)(JSContext*,
                                      JS::MutableHandleObject module_out);

    std::unordered_map<std::string, DefineModuleFunc> m_modules;

 public:
    static NativeModuleDefineFuncs& get() {
        static NativeModuleDefineFuncs the_singleton;
        return the_singleton;
    }

    // called on context init
    void add(const char* module_id, DefineModuleFunc);

    // called by importer.cpp to to check for already loaded modules
    [[nodiscard]] bool is_registered(const char* name) const;

    // called by importer.cpp to load a built-in native module
    GJS_JSAPI_RETURN_CONVENTION
    bool define(JSContext*, const char* name,
                JS::MutableHandleObject module_out) const;
};
};  // namespace Gjs
