55
66#include < config.h>
77
8+ #include < stddef.h> // for size_t
9+ #include < stdint.h>
10+
811#include < string>
12+ #include < vector>
913
1014#include < glib.h>
1115
2125#include < js/Value.h>
2226#include < jsapi.h> // for JS_NewPlainObject
2327
28+ #include " cjs/deprecation.h"
2429#include " cjs/global.h"
2530#include " cjs/jsapi-util.h"
2631#include " cjs/macros.h"
@@ -182,6 +187,48 @@ static bool get_pretty_print_function(JSContext*, unsigned argc,
182187 return true ;
183188}
184189
190+ GJS_JSAPI_RETURN_CONVENTION
191+ static bool warn_deprecated_once_per_callsite (JSContext* cx, unsigned argc,
192+ JS::Value* vp) {
193+ JS::CallArgs args = JS::CallArgsFromVp (argc, vp);
194+
195+ g_assert (args.length () >= 1 &&
196+ " warnDeprecatedOncePerCallsite takes at least 1 argument" );
197+
198+ g_assert (
199+ args[0 ].isInt32 () &&
200+ " warnDeprecatedOncePerCallsite argument 1 must be a message ID number" );
201+ int32_t message_id = args[0 ].toInt32 ();
202+ g_assert (
203+ message_id >= 0 &&
204+ uint32_t (message_id) < GjsDeprecationMessageId::LastValue &&
205+ " warnDeprecatedOncePerCallsite argument 1 must be a message ID number" );
206+
207+ if (args.length () == 1 ) {
208+ _gjs_warn_deprecated_once_per_callsite (
209+ cx, GjsDeprecationMessageId (message_id));
210+ return true ;
211+ }
212+
213+ std::vector<std::string> format_args_str;
214+ std::vector<const char *> format_args;
215+ for (size_t ix = 1 ; ix < args.length (); ix++) {
216+ g_assert (args[ix].isString () &&
217+ " warnDeprecatedOncePerCallsite subsequent arguments must be "
218+ " strings" );
219+ JS::RootedString v_format_arg{cx, args[ix].toString ()};
220+ JS::UniqueChars format_arg = JS_EncodeStringToUTF8 (cx, v_format_arg);
221+ if (!format_arg)
222+ return false ;
223+ format_args_str.emplace_back (format_arg.get ());
224+ format_args.emplace_back (format_args_str.back ().c_str ());
225+ }
226+
227+ _gjs_warn_deprecated_once_per_callsite (
228+ cx, GjsDeprecationMessageId (message_id), format_args);
229+ return true ;
230+ }
231+
185232// clang-format off
186233static constexpr JSFunctionSpec funcs[] = {
187234 JS_FN (" log" , gjs_log, 1 , GJS_MODULE_PROP_FLAGS),
@@ -190,13 +237,22 @@ static constexpr JSFunctionSpec funcs[] = {
190237 JS_FN (" printerr" , gjs_printerr, 0 , GJS_MODULE_PROP_FLAGS),
191238 JS_FN (" setPrettyPrintFunction" , set_pretty_print_function, 1 , GJS_MODULE_PROP_FLAGS),
192239 JS_FN (" getPrettyPrintFunction" , get_pretty_print_function, 1 , GJS_MODULE_PROP_FLAGS),
240+ JS_FN (" warnDeprecatedOncePerCallsite" , warn_deprecated_once_per_callsite, 1 ,
241+ GJS_MODULE_PROP_FLAGS),
193242 JS_FS_END};
194243// clang-format on
195244
245+ static constexpr JSPropertySpec props[] = {
246+ JSPropertySpec::int32Value (" PLATFORM_SPECIFIC_TYPELIB" ,
247+ GJS_MODULE_PROP_FLAGS,
248+ GjsDeprecationMessageId::PlatformSpecificTypelib),
249+ JS_PS_END};
250+
196251bool gjs_define_print_stuff (JSContext* context,
197252 JS::MutableHandleObject module ) {
198253 module .set (JS_NewPlainObject (context));
199254 if (!module )
200255 return false ;
201- return JS_DefineFunctions (context, module , funcs);
256+ return JS_DefineFunctions (context, module , funcs) &&
257+ JS_DefineProperties (context, module , props);
202258}
0 commit comments