summaryrefslogtreecommitdiff
path: root/gjs/native.h
blob: 8943547f2a84621ea79a2f0e9ba2f94609f892cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* -*- 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

#ifndef GJS_NATIVE_H_
#define GJS_NATIVE_H_

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

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

#include "gjs/macros.h"

namespace Gjs {
class NativeModuleRegistry {
    NativeModuleRegistry() {}
    typedef bool (*GjsDefineModuleFunc)(JSContext* context,
                                        JS::MutableHandleObject module_out);

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

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

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

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

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

#endif  // GJS_NATIVE_H_