LCOV - code coverage report
Current view: top level - gjs - native.cpp (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 73.3 % 15 11
Test Date: 2024-04-16 04:37:39 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50.0 % 4 2

             Branch data     Line data    Source code
       1                 :             : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
       2                 :             : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
       3                 :             : // SPDX-FileCopyrightText: 2008-2010 litl, LLC
       4                 :             : 
       5                 :             : #include <config.h>
       6                 :             : 
       7                 :             : #include <string>
       8                 :             : #include <tuple>  // for tie
       9                 :             : #include <unordered_map>
      10                 :             : #include <utility>  // for pair
      11                 :             : 
      12                 :             : #include <glib.h>
      13                 :             : 
      14                 :             : #include <js/RootingAPI.h>
      15                 :             : #include <js/TypeDecls.h>
      16                 :             : 
      17                 :             : #include "gjs/jsapi-util.h"
      18                 :             : #include "gjs/native.h"
      19                 :             : #include "util/log.h"
      20                 :             : 
      21                 :         927 : void Gjs::NativeModuleDefineFuncs::add(const char* module_id,
      22                 :             :                                        GjsDefineModuleFunc func) {
      23                 :             :     bool inserted;
      24                 :         927 :     std::tie(std::ignore, inserted) = m_modules.insert({module_id, func});
      25         [ -  + ]:         927 :     if (!inserted) {
      26                 :           0 :         g_warning("A second native module tried to register the same id '%s'",
      27                 :             :                   module_id);
      28                 :           0 :         return;
      29                 :             :     }
      30                 :             : 
      31                 :         927 :     gjs_debug(GJS_DEBUG_NATIVE,
      32                 :             :               "Registered native JS module '%s'",
      33                 :             :               module_id);
      34                 :             : }
      35                 :             : 
      36                 :             : /**
      37                 :             :  * is_registered:
      38                 :             :  * @name: name of the module
      39                 :             :  *
      40                 :             :  * Checks if a native module corresponding to @name has already
      41                 :             :  * been registered. This is used to check to see if a name is a
      42                 :             :  * builtin module without starting to try and load it.
      43                 :             :  */
      44                 :         898 : bool Gjs::NativeModuleDefineFuncs::is_registered(const char* name) const {
      45                 :        2694 :     return m_modules.count(name) > 0;
      46                 :             : }
      47                 :             : 
      48                 :             : /**
      49                 :             :  * define:
      50                 :             :  * @context: the #JSContext
      51                 :             :  * @id: Name under which the module was registered with add()
      52                 :             :  * @module_out: Return location for a #JSObject
      53                 :             :  *
      54                 :             :  * Loads a builtin native-code module called @name into @module_out by calling
      55                 :             :  * the function to define it.
      56                 :             :  *
      57                 :             :  * Returns: true on success, false if an exception was thrown.
      58                 :             :  */
      59                 :         619 : bool Gjs::NativeModuleDefineFuncs::define(
      60                 :             :     JSContext* context, const char* id,
      61                 :             :     JS::MutableHandleObject module_out) const {
      62                 :         619 :     gjs_debug(GJS_DEBUG_NATIVE, "Defining native module '%s'", id);
      63                 :             : 
      64                 :        1238 :     const auto& iter = m_modules.find(id);
      65                 :             : 
      66         [ -  + ]:         619 :     if (iter == m_modules.end()) {
      67                 :           0 :         gjs_throw(context, "No native module '%s' has registered itself", id);
      68                 :           0 :         return false;
      69                 :             :     }
      70                 :             : 
      71                 :         619 :     return iter->second(context, module_out);
      72                 :             : }
        

Generated by: LCOV version 2.0-1