[gjs: 9/10] atoms: Make init functions fallible



commit 9b40242201ddcb45f437caeaa0533ef63c693529
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Nov 7 21:53:02 2018 -0500

    atoms: Make init functions fallible
    
    Due to the recently merged error handling work, I realized we need to do
    some error checking here as well, and the init functions need to be
    fallible.

 gjs/atoms.cpp   | 35 +++++++++++++++++++++--------------
 gjs/atoms.h     | 13 +++++++------
 gjs/context.cpp |  5 ++++-
 3 files changed, 32 insertions(+), 21 deletions(-)
---
diff --git a/gjs/atoms.cpp b/gjs/atoms.cpp
index 2eef10a9..9c8be155 100644
--- a/gjs/atoms.cpp
+++ b/gjs/atoms.cpp
@@ -27,27 +27,34 @@
 #include "gjs/atoms.h"
 #include "gjs/jsapi-wrapper.h"
 
-void GjsAtom::init(JSContext* cx, const char* str) {
-    m_jsid = INTERNED_STRING_TO_JSID(cx, JS_AtomizeAndPinString(cx, str));
+bool GjsAtom::init(JSContext* cx, const char* str) {
+    JSString* s = JS_AtomizeAndPinString(cx, str);
+    if (!s)
+        return false;
+    m_jsid = INTERNED_STRING_TO_JSID(cx, s);
+    return true;
 }
 
-void GjsSymbolAtom::init(JSContext* cx, const char* str) {
+bool GjsSymbolAtom::init(JSContext* cx, const char* str) {
     JS::RootedString descr(cx, JS_AtomizeAndPinString(cx, str));
-    m_jsid = SYMBOL_TO_JSID(JS::NewSymbol(cx, descr));
+    if (!descr)
+        return false;
+    JS::Symbol* symbol = JS::NewSymbol(cx, descr);
+    if (!symbol)
+        return false;
+    m_jsid = SYMBOL_TO_JSID(symbol);
+    return true;
 }
 
-#define INITIALIZE_ATOM(identifier, str) identifier.init(cx, str);
-
-GjsAtoms::GjsAtoms(JSContext* cx) {
-    JSAutoRequest ar(cx);
+/* Requires a current compartment. This can GC, so it needs to be done after the
+ * tracing has been set up. */
+bool GjsAtoms::init_atoms(JSContext* cx) {
+#define INITIALIZE_ATOM(identifier, str) \
+    if (!identifier.init(cx, str))       \
+        return false;
     FOR_EACH_ATOM(INITIALIZE_ATOM)
-}
-
-/* Requires a current compartment. Unlike the atoms initialized in the
- * constructor, this can GC, so it needs to be done after the tracing has been
- * set up. */
-void GjsAtoms::init_symbols(JSContext* cx) {
     FOR_EACH_SYMBOL_ATOM(INITIALIZE_ATOM)
+    return true;
 }
 
 void GjsAtoms::trace(JSTracer* trc) {
diff --git a/gjs/atoms.h b/gjs/atoms.h
index f7c80015..674a2289 100644
--- a/gjs/atoms.h
+++ b/gjs/atoms.h
@@ -26,6 +26,7 @@
 #define GJS_ATOMS_H_
 
 #include "gjs/jsapi-wrapper.h"
+#include "gjs/macros.h"
 
 // clang-format off
 #define FOR_EACH_ATOM(macro) \
@@ -76,29 +77,29 @@
 // clang-format on
 
 struct GjsAtom {
-    void init(JSContext* cx, const char* str);
+    GJS_JSAPI_RETURN_CONVENTION bool init(JSContext* cx, const char* str);
 
     /* It's OK to return JS::HandleId here, to avoid an extra root, with the
      * caveat that you should not use this value after the GjsContext has been
      * destroyed.*/
-    JS::HandleId operator()() const {
+    GJS_USE JS::HandleId operator()() const {
         return JS::HandleId::fromMarkedLocation(&m_jsid.get());
     }
 
-    JS::Heap<jsid>* id() { return &m_jsid; }
+    GJS_USE JS::Heap<jsid>* id() { return &m_jsid; }
 
  protected:
     JS::Heap<jsid> m_jsid;
 };
 
 struct GjsSymbolAtom : GjsAtom {
-    void init(JSContext* cx, const char* str);
+    GJS_JSAPI_RETURN_CONVENTION bool init(JSContext* cx, const char* str);
 };
 
 class GjsAtoms {
  public:
-    explicit GjsAtoms(JSContext* cx);
-    void init_symbols(JSContext* cx);
+    explicit GjsAtoms(JSContext* cx) {}
+    GJS_JSAPI_RETURN_CONVENTION bool init_atoms(JSContext* cx);
 
     void trace(JSTracer* trc);
 
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 999279c4..3f70d04f 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -462,7 +462,10 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
     m_global = global;
     JS_AddExtraGCRootsTracer(m_cx, &GjsContextPrivate::trace, this);
 
-    m_atoms.init_symbols(m_cx);
+    if (!m_atoms.init_atoms(m_cx)) {
+        gjs_log_exception(m_cx);
+        g_error("Failed to initialize global strings");
+    }
 
     JS::RootedObject importer(m_cx,
                               gjs_create_root_importer(m_cx, m_search_path));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]