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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
From: Philip Chimento <philip.chimento@gmail.com>
Date: Mon, 26 Feb 2024 23:46:58 -0800
Subject: Add JS API wrapping warn_deprecated_once_per_callsite
Expose it in the print module
[smcv: Adjust for gjs 1.82.x based on changes contributed to cjs]
Co-Authored-By: <marco.trevisan@canonical.com>
Origin: backport, 1.85.90, commit:1b6a60afa254bb5751cd43ad18548037d999d855
---
modules/print.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 57 insertions(+), 1 deletion(-)
diff --git a/modules/print.cpp b/modules/print.cpp
index 7e0504f..df6cac5 100644
--- a/modules/print.cpp
+++ b/modules/print.cpp
@@ -5,7 +5,11 @@
#include <config.h>
+#include <stddef.h> // for size_t
+#include <stdint.h>
+
#include <string>
+#include <vector>
#include <glib.h>
@@ -21,6 +25,7 @@
#include <js/Value.h>
#include <jsapi.h> // for JS_NewPlainObject
+#include "gjs/deprecation.h"
#include "gjs/global.h"
#include "gjs/jsapi-util.h"
#include "gjs/macros.h"
@@ -182,6 +187,48 @@ static bool get_pretty_print_function(JSContext*, unsigned argc,
return true;
}
+GJS_JSAPI_RETURN_CONVENTION
+static bool warn_deprecated_once_per_callsite(JSContext* cx, unsigned argc,
+ JS::Value* vp) {
+ JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+
+ g_assert(args.length() >= 1 &&
+ "warnDeprecatedOncePerCallsite takes at least 1 argument");
+
+ g_assert(
+ args[0].isInt32() &&
+ "warnDeprecatedOncePerCallsite argument 1 must be a message ID number");
+ int32_t message_id = args[0].toInt32();
+ g_assert(
+ message_id >= 0 &&
+ uint32_t(message_id) < GjsDeprecationMessageId::LastValue &&
+ "warnDeprecatedOncePerCallsite argument 1 must be a message ID number");
+
+ if (args.length() == 1) {
+ _gjs_warn_deprecated_once_per_callsite(
+ cx, GjsDeprecationMessageId(message_id));
+ return true;
+ }
+
+ std::vector<std::string> format_args_str;
+ std::vector<const char *> format_args;
+ for (size_t ix = 1; ix < args.length(); ix++) {
+ g_assert(args[ix].isString() &&
+ "warnDeprecatedOncePerCallsite subsequent arguments must be "
+ "strings");
+ JS::RootedString v_format_arg{cx, args[ix].toString()};
+ JS::UniqueChars format_arg = JS_EncodeStringToUTF8(cx, v_format_arg);
+ if (!format_arg)
+ return false;
+ format_args_str.emplace_back(format_arg.get());
+ format_args.emplace_back(format_args_str.back().c_str());
+ }
+
+ _gjs_warn_deprecated_once_per_callsite(
+ cx, GjsDeprecationMessageId(message_id), format_args);
+ return true;
+}
+
// clang-format off
static constexpr JSFunctionSpec funcs[] = {
JS_FN("log", gjs_log, 1, GJS_MODULE_PROP_FLAGS),
@@ -190,7 +237,15 @@ static constexpr JSFunctionSpec funcs[] = {
JS_FN("printerr", gjs_printerr, 0, GJS_MODULE_PROP_FLAGS),
JS_FN("setPrettyPrintFunction", set_pretty_print_function, 1, GJS_MODULE_PROP_FLAGS),
JS_FN("getPrettyPrintFunction", get_pretty_print_function, 1, GJS_MODULE_PROP_FLAGS),
+ JS_FN("warnDeprecatedOncePerCallsite", warn_deprecated_once_per_callsite, 1,
+ GJS_MODULE_PROP_FLAGS),
JS_FS_END};
+
+static constexpr JSPropertySpec props[] = {
+ JSPropertySpec::int32Value("PLATFORM_SPECIFIC_TYPELIB",
+ GJS_MODULE_PROP_FLAGS,
+ GjsDeprecationMessageId::PlatformSpecificTypelib),
+ JS_PS_END};
// clang-format on
bool gjs_define_print_stuff(JSContext* context,
@@ -198,5 +253,6 @@ bool gjs_define_print_stuff(JSContext* context,
module.set(JS_NewPlainObject(context));
if (!module)
return false;
- return JS_DefineFunctions(context, module, funcs);
+ return JS_DefineFunctions(context, module, funcs) &&
+ JS_DefineProperties(context, module, props);
}
|