After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 636283 - Provide a useful toString for importer and module objects.
Provide a useful toString for importer and module objects.
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: Philip Chimento
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2010-12-02 10:48 UTC by Jasper St. Pierre (not reading bugmail)
Modified: 2017-01-06 04:06 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Provide a useful toString for importer and module objects. (7.27 KB, patch)
2010-12-02 10:48 UTC, Jasper St. Pierre (not reading bugmail)
reviewed Details | Review
Provide a useful toString for importer and module objects. (7.68 KB, patch)
2010-12-02 15:50 UTC, Jasper St. Pierre (not reading bugmail)
none Details | Review
Provide a useful toString for importer and module objects. (7.74 KB, patch)
2010-12-03 17:57 UTC, Jasper St. Pierre (not reading bugmail)
none Details | Review
Provide a useful toString for importer and module objects. (8.88 KB, patch)
2010-12-29 07:27 UTC, Jasper St. Pierre (not reading bugmail)
none Details | Review
Provide a useful toString for importer and module objects. (8.89 KB, patch)
2011-01-06 13:24 UTC, Jasper St. Pierre (not reading bugmail)
reviewed Details | Review
Provide a useful toString for importer and module objects. (8.92 KB, patch)
2011-02-01 15:40 UTC, Jasper St. Pierre (not reading bugmail)
none Details | Review
Provide a useful toString for importer and module objects. (8.92 KB, patch)
2011-02-17 17:32 UTC, Jasper St. Pierre (not reading bugmail)
none Details | Review
importer: Useful toString for importer and module objects (8.21 KB, patch)
2017-01-05 04:57 UTC, Philip Chimento
committed Details | Review

Description Jasper St. Pierre (not reading bugmail) 2010-12-02 10:48:57 UTC
The default SpiderMonkey toString isn't very useful.
Comment 1 Jasper St. Pierre (not reading bugmail) 2010-12-02 10:48:59 UTC
Created attachment 175701 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default
toString for identification: [object Object]. This
patch provides a more useful toString and also uses
the string created for it during the native module
importer.
Comment 2 Colin Walters 2010-12-02 14:54:18 UTC
Review of attachment 175701 [details] [review]:

It'd be nice to know a specific reason you want this; you could say for example "When debugging, it's convenient to be able to see the file path to the module" (or something).

::: gjs/importer.c
@@ +65,3 @@
+        strval = g_strdup("[Root Module]");
+    else
+        strval = g_strdup_printf("[Module %s]", gjs_string_get_ascii(context, module_path));

Rather than "Module", I suggest toString() overrides use JS_GetClass(), then class->name so there's consistency.  In this case that will be "GjsFileImporter"

@@ +67,3 @@
+        strval = g_strdup_printf("[Module %s]", gjs_string_get_ascii(context, module_path));
+
+    retval = STRING_TO_JSVAL(JS_NewStringCopyZ(context, strval));

You need to g_free(strval);

@@ +131,3 @@
+            module_path_buf = g_strdup_printf("%s.%s",
+                                              gjs_string_get_ascii(context, parent_module_path),
+                                              module_name);

You're not handling the possibility here that __modulePath__ is not null, but also not a string (then gjs_string_get_ascii will assert or crash).

I suggest:

if (JSVAL_IS_STRING(parent_module_path))
  ..
else
  ..

Also note that after bug 635707 lands, gjs_string_get_ascii will malloc().  I plan to land that patch before this one, so be prepared to rebase.  If you don't it will leak here.

@@ +132,3 @@
+                                              gjs_string_get_ascii(context, parent_module_path),
+                                              module_name);
+        module_path = STRING_TO_JSVAL(JS_NewStringCopyZ(context, module_path_buf));

g_free(module_path_buf);

::: gjs/native.c
@@ +137,3 @@
+    if (gjs_object_get_property(context, module_obj, "__modulePath__", &value) &&
+        JSVAL_IS_STRING(value))
+        module_id = gjs_string_get_ascii(context, value);

This will malloc() after bug 635707.
Comment 3 Jasper St. Pierre (not reading bugmail) 2010-12-02 15:50:20 UTC
Created attachment 175718 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default
toString for identification: [object Object]. This
patch provides a more useful toString and also uses
the string created for it during the native module
importer.
Comment 4 Jasper St. Pierre (not reading bugmail) 2010-12-03 17:57:36 UTC
Created attachment 175797 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default
toString for identification: [object Object]. This
patch provides a more useful toString and also uses
the string created for it during the native module
importer.
Comment 5 Jasper St. Pierre (not reading bugmail) 2010-12-29 07:27:47 UTC
Created attachment 177175 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default
toString for identification: [object Object]. When
debugging, it's useful to have information like
the imported path of a module, which we don't store.
Comment 6 Jasper St. Pierre (not reading bugmail) 2011-01-06 13:24:58 UTC
Created attachment 177652 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default toString for
identification: [object Object]. When debugging, it's useful to have
information like the imported path of a module, which we currently
don't display.
Comment 7 Colin Walters 2011-02-01 15:34:42 UTC
Review of attachment 177652 [details] [review]:

Two memory leaks and a style comment.

::: gjs/importer.c
@@ +132,3 @@
+        else
+            module_path_buf = g_strdup_printf("%s.%s",
+                                              gjs_string_get_ascii(context, parent_module_path),

Leaks.

@@ +300,3 @@
+
+    retval = STRING_TO_JSVAL(JS_NewStringCopyZ(context, strval));
+    g_free (strval);

Inconsistent style; gjs has no space between identifier and paren.

::: gjs/native.c
@@ +201,3 @@
     {
+        char *path = get_module_path(context, module_obj);
+        native_module = lookup_native_module (context, path, TRUE);

Leaks path.
Comment 8 Jasper St. Pierre (not reading bugmail) 2011-02-01 15:40:11 UTC
Created attachment 179802 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default toString for
identification: [object Object]. When debugging, it's useful to have
information like the imported path of a module, which we currently
don't display.
Comment 9 Johan (not receiving bugmail) Dahlin 2011-02-17 17:24:12 UTC
Review of attachment 179802 [details] [review]:

::: gjs/importer.c
@@ +126,3 @@
+            return JS_FALSE;
+
+        char *module_path_buf;

ANSI C only, do this in the beginning of a branch.

@@ +296,3 @@
+    g_assert(!JSVAL_IS_NULL(module_path));
+
+    char *path = gjs_string_get_ascii(context, module_path);

Ditto see above
Comment 10 Jasper St. Pierre (not reading bugmail) 2011-02-17 17:32:44 UTC
Created attachment 181152 [details] [review]
Provide a useful toString for importer and module objects.

SpiderMonkey provides a disgustingly horrible default toString for
identification: [object Object]. When debugging, it's useful to have
information like the imported path of a module, which we currently
don't display.
Comment 11 Philip Chimento 2017-01-05 04:57:41 UTC
Created attachment 342920 [details] [review]
importer: Useful toString for importer and module objects

SpiderMonkey doesn't have a useful default toString(): [object Object].
When debugging, it's useful to have information like the imported path of
a module, which we previously didn't display.

Original patch by Jasper St. Pierre; rewritten for current GJS by Philip
Chimento.
Comment 12 Philip Chimento 2017-01-05 04:58:42 UTC
I rebased this, but it needed enough rewriting that it's worth a review by someone else.
Comment 13 Cosimo Cecchi 2017-01-05 17:45:52 UTC
Review of attachment 342920 [details] [review]:

Feel free to push with this nit fixed.

::: gjs/importer.cpp
@@ +126,3 @@
+        if (parent_module_path.isNull())
+            module_path_buf = g_strdup(module_name);
+                                           &parent_module_path))

Nit: it's a bit awkward to have the if block without curly braces and the else block with curly braces.
Comment 14 Philip Chimento 2017-01-06 04:06:23 UTC
Attachment 342920 [details] pushed as 4c74e71 - importer: Useful toString for importer and module objects