Skip to content

Commit 4c74e35

Browse files
committed
[GLIB] Add jsc_context_get_global_object() to GLib API
https://bugs.webkit.org/show_bug.cgi?id=187515 Reviewed by Michael Catanzaro. Source/JavaScriptCore: 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/JavaScriptCore/glib/TestJSC.cpp: (testJSCGlobalObject): (main): Canonical link: https://commits.webkit.org/202810@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@233759 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent e5719f8 commit 4c74e35

6 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎Source/JavaScriptCore/API/glib/JSCContext.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,21 @@ JSCValue* jsc_context_evaluate_with_source_uri(JSCContext* context, const char*
785785
return jscContextGetOrCreateValue(context, result).leakRef();
786786
}
787787

788+
/**
789+
* jsc_context_get_global_object:
790+
* @context: a #JSCContext
791+
*
792+
* Get a #JSCValue referencing the @context global object
793+
*
794+
* Returns: (transfer full): a #JSCValue
795+
*/
796+
JSCValue* jsc_context_get_global_object(JSCContext* context)
797+
{
798+
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
799+
800+
return jscContextGetOrCreateValue(context, JSContextGetGlobalObject(context->priv->jsContext.get())).leakRef();
801+
}
802+
788803
/**
789804
* jsc_context_set_value:
790805
* @context: a #JSCContext

‎Source/JavaScriptCore/API/glib/JSCContext.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ jsc_context_evaluate_with_source_uri (JSCContext *context,
113113
gssize length,
114114
const char *uri);
115115

116+
JSC_API JSCValue *
117+
jsc_context_get_global_object (JSCContext *context);
118+
116119
JSC_API void
117120
jsc_context_set_value (JSCContext *context,
118121
const char *name,

‎Source/JavaScriptCore/API/glib/docs/jsc-glib-4.0-sections.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jsc_context_pop_exception_handler
3838
jsc_context_get_current
3939
jsc_context_evaluate
4040
jsc_context_evaluate_with_source_uri
41+
jsc_context_get_global_object
4142
jsc_context_set_value
4243
jsc_context_get_value
4344
jsc_context_register_class

‎Source/JavaScriptCore/ChangeLog‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2018-07-11 Carlos Garcia Campos <cgarcia@igalia.com>
2+
3+
[GLIB] Add jsc_context_get_global_object() to GLib API
4+
https://bugs.webkit.org/show_bug.cgi?id=187515
5+
6+
Reviewed by Michael Catanzaro.
7+
8+
This wasn't exposed because we have convenient methods in JSCContext to get and set properties on the global
9+
object. However, getting the global object could be useful in some cases, for example to give it a well known
10+
name like 'window' in browsers and GJS.
11+
12+
* API/glib/JSCContext.cpp:
13+
(jsc_context_get_global_object):
14+
* API/glib/JSCContext.h:
15+
* API/glib/docs/jsc-glib-4.0-sections.txt:
16+
117
2018-07-11 Carlos Garcia Campos <cgarcia@igalia.com>
218

319
[GLIB] Handle G_TYPE_STRV in glib API

‎Tools/ChangeLog‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2018-07-11 Carlos Garcia Campos <cgarcia@igalia.com>
2+
3+
[GLIB] Add jsc_context_get_global_object() to GLib API
4+
https://bugs.webkit.org/show_bug.cgi?id=187515
5+
6+
Reviewed by Michael Catanzaro.
7+
8+
Add test cases.
9+
10+
* TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp:
11+
(testJSCGlobalObject):
12+
(main):
13+
114
2018-07-11 Carlos Garcia Campos <cgarcia@igalia.com>
215

316
[GLIB] Handle G_TYPE_STRV in glib API

‎Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,68 @@ static void testJSCTypes()
545545
g_assert_true(result.get() == value.get());
546546
}
547547

548+
static void testJSCGlobalObject()
549+
{
550+
LeakChecker checker;
551+
GRefPtr<JSCContext> context = adoptGRef(jsc_context_new());
552+
checker.watch(context.get());
553+
ExceptionHandler exceptionHandler(context.get());
554+
555+
GRefPtr<JSCValue> globalObject = adoptGRef(jsc_context_get_global_object(context.get()));
556+
checker.watch(globalObject.get());
557+
g_assert_true(jsc_value_is_object(globalObject.get()));
558+
559+
GRefPtr<JSCValue> foo = adoptGRef(jsc_value_new_number(context.get(), 25));
560+
checker.watch(foo.get());
561+
jsc_value_object_set_property(globalObject.get(), "foo", foo.get());
562+
563+
GRefPtr<JSCValue> foo2 = adoptGRef(jsc_context_get_value(context.get(), "foo"));
564+
checker.watch(foo2.get());
565+
g_assert_true(foo.get() == foo2.get());
566+
567+
GRefPtr<JSCValue> bar = adoptGRef(jsc_value_new_number(context.get(), 50));
568+
checker.watch(bar.get());
569+
jsc_context_set_value(context.get(), "bar", bar.get());
570+
571+
GRefPtr<JSCValue> bar2 = adoptGRef(jsc_value_object_get_property(globalObject.get(), "bar"));
572+
checker.watch(bar2.get());
573+
g_assert_true(bar.get() == bar2.get());
574+
575+
GRefPtr<JSCValue> baz = adoptGRef(jsc_context_evaluate(context.get(), "baz = 75", -1));
576+
checker.watch(baz.get());
577+
578+
GRefPtr<JSCValue> baz2 = adoptGRef(jsc_value_object_get_property(globalObject.get(), "baz"));
579+
checker.watch(baz2.get());
580+
g_assert_true(baz.get() == baz2.get());
581+
582+
jsc_context_set_value(context.get(), "window", globalObject.get());
583+
GRefPtr<JSCValue> window = adoptGRef(jsc_context_evaluate(context.get(), "window", -1));
584+
checker.watch(window.get());
585+
g_assert_true(window.get() == globalObject.get());
586+
587+
foo2 = adoptGRef(jsc_context_evaluate(context.get(), "window.foo", -1));
588+
checker.watch(foo2.get());
589+
g_assert_true(foo.get() == foo2.get());
590+
591+
GRefPtr<JSCValue> global = adoptGRef(jsc_context_evaluate(context.get(), "window.global = 100", -1));
592+
checker.watch(global.get());
593+
g_assert_true(jsc_value_is_number(global.get()));
594+
g_assert_cmpint(jsc_value_to_int32(global.get()), ==, 100);
595+
596+
GRefPtr<JSCValue> global2 = adoptGRef(jsc_context_get_value(context.get(), "global"));
597+
checker.watch(global2.get());
598+
g_assert_true(global.get() == global2.get());
599+
600+
global2 = adoptGRef(jsc_value_object_get_property(globalObject.get(), "global"));
601+
checker.watch(global2.get());
602+
g_assert_true(global.get() == global2.get());
603+
604+
jsc_value_object_define_property_data(globalObject.get(), "window2", static_cast<JSCValuePropertyFlags>(0), globalObject.get());
605+
GRefPtr<JSCValue> window2 = adoptGRef(jsc_context_evaluate(context.get(), "window2", -1));
606+
checker.watch(window2.get());
607+
g_assert_true(window2.get() == globalObject.get());
608+
}
609+
548610
static int foo(int n)
549611
{
550612
return n * 2;
@@ -2738,6 +2800,7 @@ int main(int argc, char** argv)
27382800

27392801
g_test_add_func("/jsc/basic", testJSCBasic);
27402802
g_test_add_func("/jsc/types", testJSCTypes);
2803+
g_test_add_func("/jsc/global-object", testJSCGlobalObject);
27412804
g_test_add_func("/jsc/function", testJSCFunction);
27422805
g_test_add_func("/jsc/object", testJSCObject);
27432806
g_test_add_func("/jsc/class", testJSCClass);

0 commit comments

Comments
 (0)