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: 2018 Philip Chimento <philip.chimento@gmail.com>
4 : :
5 : : #pragma once
6 : :
7 : : #include <config.h>
8 : :
9 : : #include <stdint.h>
10 : :
11 : : #include <format>
12 : : #include <iterator> // for size
13 : : #include <string>
14 : : #include <string_view>
15 : : #include <type_traits>
16 : : #include <utility> // for forward
17 : :
18 : : struct JSContext;
19 : :
20 : : enum GjsDeprecationMessageId : uint8_t {
21 : : None,
22 : : ByteArrayInstanceToString,
23 : : DeprecatedGObjectProperty,
24 : : ModuleExportedLetOrConst,
25 : : PlatformSpecificTypelib,
26 : : Renamed,
27 : : MakeProxyWrapperFunctionCall,
28 : : LastValue, // insert new elements before this one
29 : : };
30 : :
31 : : namespace Gjs::detail {
32 : :
33 : : inline constexpr const std::string_view messages[] = {
34 : : // None:
35 : : "(invalid message)",
36 : :
37 : : // ByteArrayInstanceToString:
38 : : "Some code called array.toString() on a Uint8Array instance. Previously "
39 : : "this would have interpreted the bytes of the array as a string, but that "
40 : : "is nonstandard. In the future this will return the bytes as "
41 : : "comma-separated digits. For the time being, the old behavior has been "
42 : : "preserved, but please fix your code anyway to use TextDecoder.\n"
43 : : "(Note that array.toString() may have been called implicitly.)",
44 : :
45 : : // DeprecatedGObjectProperty:
46 : : "The GObject property {}.{} is deprecated.",
47 : :
48 : : // ModuleExportedLetOrConst:
49 : : "Some code accessed the property '{}' on the module '{}'. That property "
50 : : "was defined with 'let' or 'const' inside the module. This was previously "
51 : : "supported, but is not correct according to the ES6 standard. Any symbols "
52 : : "to be exported from a module must be defined with 'var'. The property "
53 : : "access will work as previously for the time being, but please fix your "
54 : : "code anyway.",
55 : :
56 : : // PlatformSpecificTypelib:
57 : : "{} has been moved to a separate platform-specific library. Please update "
58 : : "your code to use {} instead.",
59 : :
60 : : // Renamed:
61 : : "{} has been renamed. Please update your code to use {} instead.",
62 : :
63 : : // MakeProxyWrapperFunctionCall:
64 : : "Gio.DBusProxy.makeProxyWrapper for '{}' returns a class expression. Do "
65 : : "not call it as a function without 'new'.",
66 : : };
67 : : static_assert(std::size(messages) == GjsDeprecationMessageId::LastValue);
68 : :
69 : : void warn_deprecated_internal(JSContext*, GjsDeprecationMessageId,
70 : : const char* msg, unsigned max_frames);
71 : :
72 : : } // namespace Gjs::detail
73 : :
74 : : /* Note, this can only be called from the JS thread because it uses the full
75 : : * stack dump API and not the "safe" gjs_dumpstack() which can only print to
76 : : * stdout or stderr. Do not use this function during GC, for example. */
77 : : template <GjsDeprecationMessageId ID, typename... Args>
78 : 8 : void gjs_warn_deprecated_once_per_callsite(JSContext* cx, Args&&... args) {
79 : 20 : std::string msg =
80 : : std::format(std::format_string<Args...>{Gjs::detail::messages[ID]},
81 : : std::forward<Args>(args)...);
82 : 8 : Gjs::detail::warn_deprecated_internal(cx, ID, msg.c_str(), 1);
83 : 8 : }
|