Skip to content

Commit bfedb44

Browse files
3v1n0mtwebster
authored andcommitted
gi/function: Implement name property for functions
JS functions have a name properties by default, but we did not implement it for our function wrappers. Given that there's no a defined specification for it and that it can be used for debugging purposes, use it to store the original C function name.
1 parent 624b686 commit bfedb44

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

gi/function.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class Function : public CWrapper<Function> {
116116
GJS_JSAPI_RETURN_CONVENTION
117117
static bool get_length(JSContext* cx, unsigned argc, JS::Value* vp);
118118

119+
GJS_JSAPI_RETURN_CONVENTION
120+
static bool get_name(JSContext* cx, unsigned argc, JS::Value* vp);
121+
119122
GJS_JSAPI_RETURN_CONVENTION
120123
static bool to_string(JSContext* cx, unsigned argc, JS::Value* vp);
121124

@@ -1251,6 +1254,16 @@ bool Function::get_length(JSContext* cx, unsigned argc, JS::Value* vp) {
12511254
return true;
12521255
}
12531256

1257+
bool Function::get_name(JSContext* cx, unsigned argc, JS::Value* vp) {
1258+
GJS_CHECK_WRAPPER_PRIV(cx, argc, vp, rec, this_obj, Function, priv);
1259+
1260+
if (priv->m_info.type() == GI_INFO_TYPE_FUNCTION)
1261+
return gjs_string_from_utf8(cx, g_function_info_get_symbol(priv->m_info),
1262+
rec.rval());
1263+
1264+
return gjs_string_from_utf8(cx, priv->format_name().c_str(), rec.rval());
1265+
}
1266+
12541267
bool Function::to_string(JSContext* context, unsigned argc, JS::Value* vp) {
12551268
GJS_CHECK_WRAPPER_PRIV(context, argc, vp, rec, this_obj, Function, priv);
12561269
return priv->to_string_impl(context, rec.rval());
@@ -1302,6 +1315,7 @@ const JSClassOps Function::class_ops = {
13021315

13031316
const JSPropertySpec Function::proto_props[] = {
13041317
JS_PSG("length", &Function::get_length, JSPROP_PERMANENT),
1318+
JS_PSG("name", &Function::get_name, JSPROP_PERMANENT),
13051319
JS_STRING_SYM_PS(toStringTag, "GIRepositoryFunction", JSPROP_READONLY),
13061320
JS_PS_END};
13071321

0 commit comments

Comments
 (0)