Title: [233759] trunk
Revision
233759
Author
carlo...@webkit.org
Date
2018-07-11 22:13:56 -0700 (Wed, 11 Jul 2018)

Log Message

[GLIB] Add jsc_context_get_global_object() to GLib API
https://bugs.webkit.org/show_bug.cgi?id=187515

Reviewed by Michael Catanzaro.

Source/_javascript_Core:

This wasn't exposed because we have convenient methods in JSCContext to get and set properties on the global
object. However, getting the global object could be useful in some cases, for example to give it a well known
name like 'window' in browsers and GJS.

* API/glib/JSCContext.cpp:
(jsc_context_get_global_object):
* API/glib/JSCContext.h:
* API/glib/docs/jsc-glib-4.0-sections.txt:

Tools:

Add test cases.

* TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp:
(testJSCGlobalObject):
(main):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/glib/JSCContext.cpp (233758 => 233759)


--- trunk/Source/_javascript_Core/API/glib/JSCContext.cpp	2018-07-12 05:11:55 UTC (rev 233758)
+++ trunk/Source/_javascript_Core/API/glib/JSCContext.cpp	2018-07-12 05:13:56 UTC (rev 233759)
@@ -786,6 +786,21 @@
 }
 
 /**
+ * jsc_context_get_global_object:
+ * @context: a #JSCContext
+ *
+ * Get a #JSCValue referencing the @context global object
+ *
+ * Returns: (transfer full): a #JSCValue
+ */
+JSCValue* jsc_context_get_global_object(JSCContext* context)
+{
+    g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
+
+    return jscContextGetOrCreateValue(context, JSContextGetGlobalObject(context->priv->jsContext.get())).leakRef();
+}
+
+/**
  * jsc_context_set_value:
  * @context: a #JSCContext
  * @name: the value name

Modified: trunk/Source/_javascript_Core/API/glib/JSCContext.h (233758 => 233759)


--- trunk/Source/_javascript_Core/API/glib/JSCContext.h	2018-07-12 05:11:55 UTC (rev 233758)
+++ trunk/Source/_javascript_Core/API/glib/JSCContext.h	2018-07-12 05:13:56 UTC (rev 233759)
@@ -113,6 +113,9 @@
                                       gssize              length,
                                       const char         *uri);
 
+JSC_API JSCValue *
+jsc_context_get_global_object        (JSCContext         *context);
+
 JSC_API void
 jsc_context_set_value                (JSCContext         *context,
                                       const char         *name,

Modified: trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt (233758 => 233759)


--- trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt	2018-07-12 05:11:55 UTC (rev 233758)
+++ trunk/Source/_javascript_Core/API/glib/docs/jsc-glib-4.0-sections.txt	2018-07-12 05:13:56 UTC (rev 233759)
@@ -38,6 +38,7 @@
 jsc_context_get_current
 jsc_context_evaluate
 jsc_context_evaluate_with_source_uri
+jsc_context_get_global_object
 jsc_context_set_value
 jsc_context_get_value
 jsc_context_register_class

Modified: trunk/Source/_javascript_Core/ChangeLog (233758 => 233759)


--- trunk/Source/_javascript_Core/ChangeLog	2018-07-12 05:11:55 UTC (rev 233758)
+++ trunk/Source/_javascript_Core/ChangeLog	2018-07-12 05:13:56 UTC (rev 233759)
@@ -1,5 +1,21 @@
 2018-07-11  Carlos Garcia Campos  <cgar...@igalia.com>
 
+        [GLIB] Add jsc_context_get_global_object() to GLib API
+        https://bugs.webkit.org/show_bug.cgi?id=187515
+
+        Reviewed by Michael Catanzaro.
+
+        This wasn't exposed because we have convenient methods in JSCContext to get and set properties on the global
+        object. However, getting the global object could be useful in some cases, for example to give it a well known
+        name like 'window' in browsers and GJS.
+
+        * API/glib/JSCContext.cpp:
+        (jsc_context_get_global_object):
+        * API/glib/JSCContext.h:
+        * API/glib/docs/jsc-glib-4.0-sections.txt:
+
+2018-07-11  Carlos Garcia Campos  <cgar...@igalia.com>
+
         [GLIB] Handle G_TYPE_STRV in glib API
         https://bugs.webkit.org/show_bug.cgi?id=187512
 

Modified: trunk/Tools/ChangeLog (233758 => 233759)


--- trunk/Tools/ChangeLog	2018-07-12 05:11:55 UTC (rev 233758)
+++ trunk/Tools/ChangeLog	2018-07-12 05:13:56 UTC (rev 233759)
@@ -1,5 +1,18 @@
 2018-07-11  Carlos Garcia Campos  <cgar...@igalia.com>
 
+        [GLIB] Add jsc_context_get_global_object() to GLib API
+        https://bugs.webkit.org/show_bug.cgi?id=187515
+
+        Reviewed by Michael Catanzaro.
+
+        Add test cases.
+
+        * TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp:
+        (testJSCGlobalObject):
+        (main):
+
+2018-07-11  Carlos Garcia Campos  <cgar...@igalia.com>
+
         [GLIB] Handle G_TYPE_STRV in glib API
         https://bugs.webkit.org/show_bug.cgi?id=187512
 

Modified: trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp (233758 => 233759)


--- trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp	2018-07-12 05:11:55 UTC (rev 233758)
+++ trunk/Tools/TestWebKitAPI/Tests/_javascript_Core/glib/TestJSC.cpp	2018-07-12 05:13:56 UTC (rev 233759)
@@ -545,6 +545,68 @@
     g_assert_true(result.get() == value.get());
 }
 
+static void testJSCGlobalObject()
+{
+    LeakChecker checker;
+    GRefPtr<JSCContext> context = adoptGRef(jsc_context_new());
+    checker.watch(context.get());
+    ExceptionHandler exceptionHandler(context.get());
+
+    GRefPtr<JSCValue> globalObject = adoptGRef(jsc_context_get_global_object(context.get()));
+    checker.watch(globalObject.get());
+    g_assert_true(jsc_value_is_object(globalObject.get()));
+
+    GRefPtr<JSCValue> foo = adoptGRef(jsc_value_new_number(context.get(), 25));
+    checker.watch(foo.get());
+    jsc_value_object_set_property(globalObject.get(), "foo", foo.get());
+
+    GRefPtr<JSCValue> foo2 = adoptGRef(jsc_context_get_value(context.get(), "foo"));
+    checker.watch(foo2.get());
+    g_assert_true(foo.get() == foo2.get());
+
+    GRefPtr<JSCValue> bar = adoptGRef(jsc_value_new_number(context.get(), 50));
+    checker.watch(bar.get());
+    jsc_context_set_value(context.get(), "bar", bar.get());
+
+    GRefPtr<JSCValue> bar2 = adoptGRef(jsc_value_object_get_property(globalObject.get(), "bar"));
+    checker.watch(bar2.get());
+    g_assert_true(bar.get() == bar2.get());
+
+    GRefPtr<JSCValue> baz = adoptGRef(jsc_context_evaluate(context.get(), "baz = 75", -1));
+    checker.watch(baz.get());
+
+    GRefPtr<JSCValue> baz2 = adoptGRef(jsc_value_object_get_property(globalObject.get(), "baz"));
+    checker.watch(baz2.get());
+    g_assert_true(baz.get() == baz2.get());
+
+    jsc_context_set_value(context.get(), "window", globalObject.get());
+    GRefPtr<JSCValue> window = adoptGRef(jsc_context_evaluate(context.get(), "window", -1));
+    checker.watch(window.get());
+    g_assert_true(window.get() == globalObject.get());
+
+    foo2 = adoptGRef(jsc_context_evaluate(context.get(), "window.foo", -1));
+    checker.watch(foo2.get());
+    g_assert_true(foo.get() == foo2.get());
+
+    GRefPtr<JSCValue> global = adoptGRef(jsc_context_evaluate(context.get(), "window.global = 100", -1));
+    checker.watch(global.get());
+    g_assert_true(jsc_value_is_number(global.get()));
+    g_assert_cmpint(jsc_value_to_int32(global.get()), ==, 100);
+
+    GRefPtr<JSCValue> global2 = adoptGRef(jsc_context_get_value(context.get(), "global"));
+    checker.watch(global2.get());
+    g_assert_true(global.get() == global2.get());
+
+    global2 = adoptGRef(jsc_value_object_get_property(globalObject.get(), "global"));
+    checker.watch(global2.get());
+    g_assert_true(global.get() == global2.get());
+
+    jsc_value_object_define_property_data(globalObject.get(), "window2", static_cast<JSCValuePropertyFlags>(0), globalObject.get());
+    GRefPtr<JSCValue> window2 = adoptGRef(jsc_context_evaluate(context.get(), "window2", -1));
+    checker.watch(window2.get());
+    g_assert_true(window2.get() == globalObject.get());
+}
+
 static int foo(int n)
 {
     return n * 2;
@@ -2738,6 +2800,7 @@
 
     g_test_add_func("/jsc/basic", testJSCBasic);
     g_test_add_func("/jsc/types", testJSCTypes);
+    g_test_add_func("/jsc/global-object", testJSCGlobalObject);
     g_test_add_func("/jsc/function", testJSCFunction);
     g_test_add_func("/jsc/object", testJSCObject);
     g_test_add_func("/jsc/class", testJSCClass);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to