<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[243283] trunk</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/243283">243283</a></dd>
<dt>Author</dt> <dd>carlosgc@webkit.org</dd>
<dt>Date</dt> <dd>2019-03-21 02:43:15 -0700 (Thu, 21 Mar 2019)</dd>
</dl>
<h3>Log Message</h3>
<pre>[GLib] Returning G_TYPE_OBJECT from a method does not work
https://bugs.webkit.org/show_bug.cgi?id=195574
Reviewed by Michael Catanzaro.
Source/JavaScriptCore:
Add more documentation to clarify the ownership of wrapped objects when created and when returned by functions.
* API/glib/JSCCallbackFunction.cpp:
(JSC::JSCCallbackFunction::construct): Also allow to return boxed types from a constructor.
* API/glib/JSCClass.cpp:
* API/glib/JSCValue.cpp:
Tools:
Add new test cases to check the behavior of constructors and functions returning GObject and boxed types.
* TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp:
(getGFile):
(getParent):
(createGString):
(getGString):
(getGStringCopyWillRaise):
(getGStringCopy):
(getGStringStr):
(getGStringLen):
(freeGString):
(testJSCClass):</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreAPIglibJSCCallbackFunctioncpp">trunk/Source/JavaScriptCore/API/glib/JSCCallbackFunction.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreAPIglibJSCClasscpp">trunk/Source/JavaScriptCore/API/glib/JSCClass.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreAPIglibJSCValuecpp">trunk/Source/JavaScriptCore/API/glib/JSCValue.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsJavaScriptCoreglibTestJSCcpp">trunk/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreAPIglibJSCCallbackFunctioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/API/glib/JSCCallbackFunction.cpp (243282 => 243283)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/API/glib/JSCCallbackFunction.cpp 2019-03-21 09:24:31 UTC (rev 243282)
+++ trunk/Source/JavaScriptCore/API/glib/JSCCallbackFunction.cpp 2019-03-21 09:43:15 UTC (rev 243283)
</span><span class="lines">@@ -204,6 +204,7 @@
</span><span class="cx">
</span><span class="cx"> switch (g_type_fundamental(G_VALUE_TYPE(&returnValue))) {
</span><span class="cx"> case G_TYPE_POINTER:
</span><ins>+ case G_TYPE_BOXED:
</ins><span class="cx"> case G_TYPE_OBJECT:
</span><span class="cx"> if (auto* ptr = returnValue.data[0].v_pointer)
</span><span class="cx"> return toRef(jscClassGetOrCreateJSWrapper(m_class.get(), ptr));
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreAPIglibJSCClasscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/API/glib/JSCClass.cpp (243282 => 243283)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/API/glib/JSCClass.cpp 2019-03-21 09:24:31 UTC (rev 243282)
+++ trunk/Source/JavaScriptCore/API/glib/JSCClass.cpp 2019-03-21 09:43:15 UTC (rev 243283)
</span><span class="lines">@@ -589,6 +589,9 @@
</span><span class="cx"> * This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use
</span><span class="cx"> * jsc_context_set_value() to make the constructor available in the global object.
</span><span class="cx"> *
</span><ins>+ * Note that the value returned by @callback is adopted by @jsc_class, and the #GDestroyNotify passed to
+ * jsc_context_register_class() is responsible for disposing of it.
+ *
</ins><span class="cx"> * Returns: (transfer full): a #JSCValue representing the class constructor.
</span><span class="cx"> */
</span><span class="cx"> JSCValue* jsc_class_add_constructor(JSCClass* jscClass, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType, unsigned paramCount, ...)
</span><span class="lines">@@ -635,6 +638,9 @@
</span><span class="cx"> * This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use
</span><span class="cx"> * jsc_context_set_value() to make the constructor available in the global object.
</span><span class="cx"> *
</span><ins>+ * Note that the value returned by @callback is adopted by @jsc_class, and the #GDestroyNotify passed to
+ * jsc_context_register_class() is responsible for disposing of it.
+ *
</ins><span class="cx"> * Returns: (transfer full): a #JSCValue representing the class constructor.
</span><span class="cx"> */
</span><span class="cx"> JSCValue* jsc_class_add_constructorv(JSCClass* jscClass, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType, unsigned parametersCount, GType* parameterTypes)
</span><span class="lines">@@ -676,6 +682,9 @@
</span><span class="cx"> * This function creates the constructor, which needs to be added to an object as a property to be able to use it. Use
</span><span class="cx"> * jsc_context_set_value() to make the constructor available in the global object.
</span><span class="cx"> *
</span><ins>+ * Note that the value returned by @callback is adopted by @jsc_class, and the #GDestroyNotify passed to
+ * jsc_context_register_class() is responsible for disposing of it.
+ *
</ins><span class="cx"> * Returns: (transfer full): a #JSCValue representing the class constructor.
</span><span class="cx"> */
</span><span class="cx"> JSCValue* jsc_class_add_constructor_variadic(JSCClass* jscClass, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType)
</span><span class="lines">@@ -722,6 +731,11 @@
</span><span class="cx"> * @callback is called receiving the class instance as first parameter, followed by the method parameters and then
</span><span class="cx"> * @user_data as last parameter. When the method is cleared in the #JSCClass context, @destroy_notify is called with
</span><span class="cx"> * @user_data as parameter.
</span><ins>+ *
+ * Note that the value returned by @callback must be transfer full. In case of non-refcounted boxed types, you should use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as the instance parameter.
</ins><span class="cx"> */
</span><span class="cx"> void jsc_class_add_method(JSCClass* jscClass, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType, unsigned paramCount, ...)
</span><span class="cx"> {
</span><span class="lines">@@ -758,6 +772,11 @@
</span><span class="cx"> * @callback is called receiving the class instance as first parameter, followed by the method parameters and then
</span><span class="cx"> * @user_data as last parameter. When the method is cleared in the #JSCClass context, @destroy_notify is called with
</span><span class="cx"> * @user_data as parameter.
</span><ins>+ *
+ * Note that the value returned by @callback must be transfer full. In case of non-refcounted boxed types, you should use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as the instance parameter.
</ins><span class="cx"> */
</span><span class="cx"> void jsc_class_add_methodv(JSCClass* jscClass, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType, unsigned parametersCount, GType *parameterTypes)
</span><span class="cx"> {
</span><span class="lines">@@ -790,6 +809,11 @@
</span><span class="cx"> * @callback is called receiving the class instance as first parameter, followed by a #GPtrArray of #JSCValue<!-- -->s
</span><span class="cx"> * with the method arguments and then @user_data as last parameter. When the method is cleared in the #JSCClass context,
</span><span class="cx"> * @destroy_notify is called with @user_data as parameter.
</span><ins>+ *
+ * Note that the value returned by @callback must be transfer full. In case of non-refcounted boxed types, you should use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as the instance parameter.
</ins><span class="cx"> */
</span><span class="cx"> void jsc_class_add_method_variadic(JSCClass* jscClass, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType)
</span><span class="cx"> {
</span><span class="lines">@@ -816,6 +840,11 @@
</span><span class="cx"> * value needs to be set, @setter is called receiving the the class instance as first parameter, followed
</span><span class="cx"> * by the value to be set and then @user_data as the last parameter. When the property is cleared in the
</span><span class="cx"> * #JSCClass context, @destroy_notify is called with @user_data as parameter.
</span><ins>+ *
+ * Note that the value returned by @getter must be transfer full. In case of non-refcounted boxed types, you should use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as the instance parameter.
</ins><span class="cx"> */
</span><span class="cx"> void jsc_class_add_property(JSCClass* jscClass, const char* name, GType propertyType, GCallback getter, GCallback setter, gpointer userData, GDestroyNotify destroyNotify)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreAPIglibJSCValuecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/API/glib/JSCValue.cpp (243282 => 243283)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/API/glib/JSCValue.cpp 2019-03-21 09:24:31 UTC (rev 243282)
+++ trunk/Source/JavaScriptCore/API/glib/JSCValue.cpp 2019-03-21 09:43:15 UTC (rev 243283)
</span><span class="lines">@@ -588,11 +588,12 @@
</span><span class="cx"> /**
</span><span class="cx"> * jsc_value_new_object:
</span><span class="cx"> * @context: a #JSCContext
</span><del>- * @instance: (nullable): an object instance or %NULL
</del><ins>+ * @instance: (nullable) (transfer full): an object instance or %NULL
</ins><span class="cx"> * @jsc_class: (nullable): the #JSCClass of @instance
</span><span class="cx"> *
</span><span class="cx"> * Create a new #JSCValue from @instance. If @instance is %NULL a new empty object is created.
</span><del>- * When @instance is provided, @jsc_class must be provided too.
</del><ins>+ * When @instance is provided, @jsc_class must be provided too. @jsc_class takes ownership of
+ * @instance that will be freed by the #GDestroyNotify passed to jsc_context_register_class().
</ins><span class="cx"> *
</span><span class="cx"> * Returns: (transfer full): a #JSCValue.
</span><span class="cx"> */
</span><span class="lines">@@ -1083,6 +1084,11 @@
</span><span class="cx"> * When the property is cleared in the #JSCClass context, @destroy_notify is called with
</span><span class="cx"> * @user_data as parameter. This is equivalent to JavaScript <function>Object.defineProperty()</function>
</span><span class="cx"> * when used with an accessor descriptor.
</span><ins>+ *
+ * Note that the value returned by @getter must be fully transferred. In case of boxed types, you could use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as instance parameter.
</ins><span class="cx"> */
</span><span class="cx"> void jsc_value_object_define_property_accessor(JSCValue* value, const char* propertyName, JSCValuePropertyFlags flags, GType propertyType, GCallback getter, GCallback setter, gpointer userData, GDestroyNotify destroyNotify)
</span><span class="cx"> {
</span><span class="lines">@@ -1159,6 +1165,11 @@
</span><span class="cx"> * receiving the function parameters and then @user_data as last parameter. When the function is
</span><span class="cx"> * cleared in @context, @destroy_notify is called with @user_data as parameter.
</span><span class="cx"> *
</span><ins>+ * Note that the value returned by @callback must be fully transferred. In case of boxed types, you could use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as instance parameter.
+ *
</ins><span class="cx"> * Returns: (transfer full): a #JSCValue.
</span><span class="cx"> */
</span><span class="cx"> JSCValue* jsc_value_new_function(JSCContext* context, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType, unsigned paramCount, ...)
</span><span class="lines">@@ -1195,6 +1206,11 @@
</span><span class="cx"> * receiving the function parameters and then @user_data as last parameter. When the function is
</span><span class="cx"> * cleared in @context, @destroy_notify is called with @user_data as parameter.
</span><span class="cx"> *
</span><ins>+ * Note that the value returned by @callback must be fully transferred. In case of boxed types, you could use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as instance parameter.
+ *
</ins><span class="cx"> * Returns: (transfer full): a #JSCValue.
</span><span class="cx"> */
</span><span class="cx"> JSCValue* jsc_value_new_functionv(JSCContext* context, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType, unsigned parametersCount, GType *parameterTypes)
</span><span class="lines">@@ -1227,6 +1243,11 @@
</span><span class="cx"> * receiving an #GPtrArray of #JSCValue<!-- -->s with the arguments and then @user_data as last parameter.
</span><span class="cx"> * When the function is cleared in @context, @destroy_notify is called with @user_data as parameter.
</span><span class="cx"> *
</span><ins>+ * Note that the value returned by @callback must be fully transferred. In case of boxed types, you could use
+ * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used.
+ * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created
+ * with jsc_value_new_object() that receives the copy as instance parameter.
+ *
</ins><span class="cx"> * Returns: (transfer full): a #JSCValue.
</span><span class="cx"> */
</span><span class="cx"> JSCValue* jsc_value_new_function_variadic(JSCContext* context, const char* name, GCallback callback, gpointer userData, GDestroyNotify destroyNotify, GType returnType)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (243282 => 243283)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog 2019-03-21 09:24:31 UTC (rev 243282)
+++ trunk/Source/JavaScriptCore/ChangeLog 2019-03-21 09:43:15 UTC (rev 243283)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2019-03-21 Carlos Garcia Campos <cgarcia@igalia.com>
+
+ [GLib] Returning G_TYPE_OBJECT from a method does not work
+ https://bugs.webkit.org/show_bug.cgi?id=195574
+
+ Reviewed by Michael Catanzaro.
+
+ Add more documentation to clarify the ownership of wrapped objects when created and when returned by functions.
+
+ * API/glib/JSCCallbackFunction.cpp:
+ (JSC::JSCCallbackFunction::construct): Also allow to return boxed types from a constructor.
+ * API/glib/JSCClass.cpp:
+ * API/glib/JSCValue.cpp:
+
</ins><span class="cx"> 2019-03-21 Mark Lam <mark.lam@apple.com>
</span><span class="cx">
</span><span class="cx"> Cap length of an array with spread to MIN_ARRAY_STORAGE_CONSTRUCTION_LENGTH.
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (243282 => 243283)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog 2019-03-21 09:24:31 UTC (rev 243282)
+++ trunk/Tools/ChangeLog 2019-03-21 09:43:15 UTC (rev 243283)
</span><span class="lines">@@ -1,3 +1,24 @@
</span><ins>+2019-03-21 Carlos Garcia Campos <cgarcia@igalia.com>
+
+ [GLib] Returning G_TYPE_OBJECT from a method does not work
+ https://bugs.webkit.org/show_bug.cgi?id=195574
+
+ Reviewed by Michael Catanzaro.
+
+ Add new test cases to check the behavior of constructors and functions returning GObject and boxed types.
+
+ * TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp:
+ (getGFile):
+ (getParent):
+ (createGString):
+ (getGString):
+ (getGStringCopyWillRaise):
+ (getGStringCopy):
+ (getGStringStr):
+ (getGStringLen):
+ (freeGString):
+ (testJSCClass):
+
</ins><span class="cx"> 2019-03-20 Saam Barati <sbarati@apple.com>
</span><span class="cx">
</span><span class="cx"> DFG::AbstractValue::validateOSREntry is wrong when isHeapTop and the incoming value is Empty
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsJavaScriptCoreglibTestJSCcpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp (243282 => 243283)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp 2019-03-21 09:24:31 UTC (rev 243282)
+++ trunk/Tools/TestWebKitAPI/Tests/JavaScriptCore/glib/TestJSC.cpp 2019-03-21 09:43:15 UTC (rev 243283)
</span><span class="lines">@@ -1597,6 +1597,60 @@
</span><span class="cx"> return file;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+static GFile* getGFile(GFile* file)
+{
+ return G_FILE(g_object_ref(file));
+}
+
+static JSCValue* getParent(GFile* file, JSCClass* jscClass)
+{
+ auto* checker = static_cast<LeakChecker*>(g_object_get_data(G_OBJECT(jsc_context_get_current()), "leak-checker"));
+ GFile* parent = g_file_get_parent(file);
+ checker->watch(parent);
+ auto* value = jsc_value_new_object(jsc_context_get_current(), parent, jscClass);
+ checker->watch(value);
+ return value;
+}
+
+static GString* createGString(const char* str)
+{
+ return g_string_new(str);
+}
+
+static GString* getGString(GString* str)
+{
+ return str;
+}
+
+static GString* getGStringCopyWillRaise(GString* str)
+{
+ return static_cast<GString*>(g_boxed_copy(G_TYPE_GSTRING, str));
+}
+
+static JSCValue* getGStringCopy(GString *str, JSCClass* jscClass)
+{
+ auto* checker = static_cast<LeakChecker*>(g_object_get_data(G_OBJECT(jsc_context_get_current()), "leak-checker"));
+ auto* copy = getGStringCopyWillRaise(str);
+ auto* value = jsc_value_new_object(jsc_context_get_current(), copy, jscClass);
+ checker->watch(value);
+ return value;
+}
+
+static char* getGStringStr(GString* str)
+{
+ return g_strdup(str->str);
+}
+
+static guint64 getGStringLen(GString* str)
+{
+ return str->len;
+}
+
+static void freeGString(GString* str)
+{
+ g_string_free(str, TRUE);
+}
+
</ins><span class="cx"> static void testJSCClass()
</span><span class="cx"> {
</span><span class="cx"> {
</span><span class="lines">@@ -2296,7 +2350,136 @@
</span><span class="cx"> g_assert_true(jsc_value_is_string(value2.get()));
</span><span class="cx"> resultString.reset(jsc_value_to_string(value2.get()));
</span><span class="cx"> g_assert_cmpstr(resultString.get(), ==, currentDirectory.get());
</span><ins>+
+ jsc_class_add_method(jscClass, "getGFile", G_CALLBACK(getGFile), nullptr, nullptr, G_TYPE_OBJECT, 0, G_TYPE_NONE);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "f = new GFile('.'); f2 = f.getGFile(); f2.getPath()", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_string(result.get()));
+ resultString.reset(jsc_value_to_string(result.get()));
+ g_assert_cmpstr(resultString.get(), ==, currentDirectory.get());
+
+ value = adoptGRef(jsc_value_object_invoke_method(file.get(), "getGFile", G_TYPE_NONE));
+ checker.watch(value.get());
+ g_assert_true(value.get() == file.get());
+
+ jsc_class_add_method(jscClass, "getParent", G_CALLBACK(getParent), jscClass, nullptr, JSC_TYPE_VALUE, 0, G_TYPE_NONE);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "f = new GFile('.'); p = f.getParent(); p.getPath()", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_string(result.get()));
+ resultString.reset(jsc_value_to_string(result.get()));
+ GUniquePtr<char> parentDirectory(g_path_get_dirname(currentDirectory.get()));
+ g_assert_cmpstr(resultString.get(), ==, parentDirectory.get());
+
+ jsc_class_add_method(jscClass, "equal", G_CALLBACK(g_file_equal), nullptr, nullptr, G_TYPE_BOOLEAN, 1, G_TYPE_OBJECT);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "f1 = new GFile('.'); f2 = new GFile('.'); f1.equal(f2);", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
+
+ GFile* fileObject = g_file_new_for_path(".");
+ checker.watch(fileObject);
+ GRefPtr<JSCValue> fileValue = adoptGRef(jsc_value_new_object(context.get(), fileObject, jscClass));
+ checker.watch(fileValue.get());
+
+ result = adoptGRef(jsc_value_object_invoke_method(file.get(), "equal", G_TYPE_OBJECT, fileObject, G_TYPE_NONE));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
+
+ result = adoptGRef(jsc_value_object_invoke_method(file.get(), "equal", JSC_TYPE_VALUE, fileValue.get(), G_TYPE_NONE));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
</ins><span class="cx"> }
</span><ins>+
+ {
+ LeakChecker checker;
+ GRefPtr<JSCContext> context = adoptGRef(jsc_context_new());
+ checker.watch(context.get());
+ g_object_set_data(G_OBJECT(context.get()), "leak-checker", &checker);
+ ExceptionHandler exceptionHandler(context.get());
+
+ JSCClass* jscClass = jsc_context_register_class(context.get(), "GString", nullptr, nullptr, reinterpret_cast<GDestroyNotify>(freeGString));
+ checker.watch(jscClass);
+
+ GRefPtr<JSCValue> constructor = adoptGRef(jsc_class_add_constructor(jscClass, nullptr, G_CALLBACK(createGString), nullptr, nullptr, G_TYPE_GSTRING, 1, G_TYPE_STRING));
+ checker.watch(constructor.get());
+ g_assert_true(jsc_value_is_constructor(constructor.get()));
+
+ jsc_class_add_property(jscClass, "str", G_TYPE_STRING, G_CALLBACK(getGStringStr), nullptr, nullptr, nullptr);
+ jsc_class_add_property(jscClass, "len", G_TYPE_UINT64, G_CALLBACK(getGStringLen), nullptr, nullptr, nullptr);
+
+ jsc_context_set_value(context.get(), jsc_class_get_name(jscClass), constructor.get());
+
+ GRefPtr<JSCValue> str = adoptGRef(jsc_context_evaluate(context.get(), "s = new GString('Foo');", -1));
+ checker.watch(str.get());
+ g_assert_true(jsc_value_is_object(str.get()));
+ g_assert_true(jsc_value_object_is_instance_of(str.get(), jsc_class_get_name(jscClass)));
+ GRefPtr<JSCValue> result = adoptGRef(jsc_context_evaluate(context.get(), "s instanceof GString;", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
+
+ g_assert_true(jsc_value_object_has_property(str.get(), "str"));
+ GRefPtr<JSCValue> value = adoptGRef(jsc_value_object_get_property(str.get(), "str"));
+ checker.watch(value.get());
+ g_assert_true(jsc_value_is_string(value.get()));
+ GUniquePtr<char> resultString(jsc_value_to_string(value.get()));
+ g_assert_cmpstr(resultString.get(), ==, "Foo");
+
+ GRefPtr<JSCValue> value2 = adoptGRef(jsc_context_evaluate(context.get(), "s.str", -1));
+ checker.watch(value2.get());
+ g_assert_true(jsc_value_is_string(value2.get()));
+ resultString.reset(jsc_value_to_string(value2.get()));
+ g_assert_cmpstr(resultString.get(), ==, "Foo");
+
+ GRefPtr<JSCValue> value3 = adoptGRef(jsc_context_evaluate(context.get(), "s.len", -1));
+ checker.watch(value3.get());
+ g_assert_true(jsc_value_is_number(value3.get()));
+ g_assert_cmpint(jsc_value_to_int32(value3.get()), ==, 3);
+
+ jsc_class_add_method(jscClass, "getGString", G_CALLBACK(getGString), nullptr, nullptr, G_TYPE_POINTER, 0, G_TYPE_NONE);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "s = new GString('Self'); s2 = s.getGString(); s2.str;", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_string(result.get()));
+ resultString.reset(jsc_value_to_string(result.get()));
+ g_assert_cmpstr(resultString.get(), ==, "Self");
+
+ jsc_class_add_method(jscClass, "getGStringCopy", G_CALLBACK(getGStringCopy), jscClass, nullptr, JSC_TYPE_VALUE, 0, G_TYPE_NONE);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "s = new GString('Copy'); s2 = s.getGStringCopy(); s2.str;", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_string(result.get()));
+ resultString.reset(jsc_value_to_string(result.get()));
+ g_assert_cmpstr(resultString.get(), ==, "Copy");
+
+ jsc_class_add_method(jscClass, "getGStringCopyWillRaise", G_CALLBACK(getGStringCopyWillRaise), nullptr, nullptr, G_TYPE_GSTRING, 0, G_TYPE_NONE);
+ bool didThrow = false;
+ g_assert_throw_begin(exceptionHandler, didThrow);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "s = new GString('Copy'); s2 = s.getGStringCopyWillRaise(); s2.str;", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_undefined(result.get()));
+ g_assert_did_throw(exceptionHandler, didThrow);
+
+ jsc_class_add_method(jscClass, "equal", G_CALLBACK(g_string_equal), nullptr, nullptr, G_TYPE_BOOLEAN, 1, G_TYPE_GSTRING);
+ result = adoptGRef(jsc_context_evaluate(context.get(), "s1 = new GString('Bar'); s2 = new GString('Bar'); s1.equal(s2);", -1));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
+
+ GString* strBoxed = g_string_new("Foo");
+ GRefPtr<JSCValue> strValue = adoptGRef(jsc_value_new_object(context.get(), strBoxed, jscClass));
+ checker.watch(strValue.get());
+
+ result = adoptGRef(jsc_value_object_invoke_method(str.get(), "equal", G_TYPE_GSTRING, strBoxed, G_TYPE_NONE));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
+
+ result = adoptGRef(jsc_value_object_invoke_method(str.get(), "equal", JSC_TYPE_VALUE, strValue.get(), G_TYPE_NONE));
+ checker.watch(result.get());
+ g_assert_true(jsc_value_is_boolean(result.get()));
+ g_assert_true(jsc_value_to_boolean(result.get()));
+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> typedef struct {
</span></span></pre>
</div>
</div>
</body>
</html>