From 2e970bedd11b27ae22fdcf6bb3994b0942f582b1 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Thu, 22 Mar 2018 13:00:33 -0400 Subject: jsauthority: redo how global objects are set up This commit drops usage of JS_AddObjectRoot and switches the global object over to being wrapped in a JS::Heap pointer. It stops using JS_DefineObject which no longer seems to be available, and adds a new JS::FireOnNewGlobalHook which seems to be required. --- src/polkitbackend/polkitbackendjsauthority.cpp | 48 ++++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/polkitbackend/polkitbackendjsauthority.cpp b/src/polkitbackend/polkitbackendjsauthority.cpp index 5f1ac37..b781a75 100644 --- a/src/polkitbackend/polkitbackendjsauthority.cpp +++ b/src/polkitbackend/polkitbackendjsauthority.cpp @@ -75,9 +75,9 @@ struct _PolkitBackendJsAuthorityPrivate GFileMonitor **dir_monitors; /* NULL-terminated array of GFileMonitor instances */ JSContext *cx; - JSObject *js_global; + JS::Heap *js_global; JSAutoCompartment *ac; - JSObject *js_polkit; + JS::Heap *js_polkit; GThread *runaway_killer_thread; GMutex rkt_init_mutex; @@ -479,38 +479,42 @@ polkit_backend_js_authority_constructed (GObject *object) { JS::CompartmentOptions compart_opts; compart_opts.behaviors().setVersion(JSVERSION_LATEST); - authority->priv->js_global = JS_NewGlobalObject (authority->priv->cx, &js_global_class, NULL, compart_opts); + JS::RootedObject global(authority->priv->cx); - if (authority->priv->js_global == NULL) + authority->priv->js_global = new JS::Heap (JS_NewGlobalObject (authority->priv->cx, &js_global_class, NULL, JS::FireOnNewGlobalHook, compart_opts)); + + global = authority->priv->js_global->get (); + + if (global == NULL) goto fail; - authority->priv->ac = new JSAutoCompartment(authority->priv->cx, authority->priv->js_global); + authority->priv->ac = new JSAutoCompartment(authority->priv->cx, global); if (authority->priv->ac == NULL) goto fail; - JS_AddObjectRoot (authority->priv->cx, &authority->priv->js_global); + if (!JS_InitStandardClasses (authority->priv->cx, global)) + goto fail; + + JS::RootedObject polkit(authority->priv->cx); + + authority->priv->js_polkit = new JS::Heap (JS_NewObject (authority->priv->cx, &js_polkit_class)); - if (!JS_InitStandardClasses (authority->priv->cx, authority->priv->js_global)) + polkit = authority->priv->js_polkit->get (); + + if (polkit == NULL) goto fail; - authority->priv->js_polkit = JS_DefineObject (authority->priv->cx, - authority->priv->js_global, - "polkit", - &js_polkit_class, - NULL, - JSPROP_ENUMERATE); - if (authority->priv->js_polkit == NULL) + if (!JS_DefineProperty(authority->priv->cx, global, "polkit", polkit, JSPROP_ENUMERATE)) goto fail; - JS_AddObjectRoot (authority->priv->cx, &authority->priv->js_polkit); if (!JS_DefineFunctions (authority->priv->cx, - authority->priv->js_polkit, + polkit, js_polkit_functions)) goto fail; if (!JS_EvaluateScript (authority->priv->cx, - authority->priv->js_global, + global, init_js, strlen (init_js), /* init.js */ "init.js", /* filename */ 0, /* lineno */ @@ -584,11 +588,7 @@ polkit_backend_js_authority_finalize (GObject *object) g_free (authority->priv->dir_monitors); g_strfreev (authority->priv->rules_dirs); - JS_BeginRequest (authority->priv->cx); - JS_RemoveObjectRoot (authority->priv->cx, &authority->priv->js_polkit); delete authority->priv->ac; - JS_RemoveObjectRoot (authority->priv->cx, &authority->priv->js_global); - JS_EndRequest (authority->priv->cx); JS_DestroyContext (authority->priv->cx); /* JS_ShutDown (); */ @@ -761,10 +761,11 @@ subject_to_jsval (PolkitBackendJsAuthority *authority, struct passwd *passwd; char *seat_str = NULL; char *session_str = NULL; + JS::RootedObject global(authority->priv->cx, authority->priv->js_global->get ()); src = "new Subject();"; if (!JS_EvaluateScript (authority->priv->cx, - authority->priv->js_global, + global, src, strlen (src), __FILE__, __LINE__, &ret_jsval)) @@ -886,10 +887,11 @@ action_and_details_to_jsval (PolkitBackendJsAuthority *authority, JSObject *obj; gchar **keys; guint n; + JS::RootedObject global(authority->priv->cx, authority->priv->js_global->get ()); src = "new Action();"; if (!JS_EvaluateScript (authority->priv->cx, - authority->priv->js_global, + global, src, strlen (src), __FILE__, __LINE__, &ret_jsval)) -- cgit v1.2.1