1/*
2 * Copyright (C) 2012 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitJavascriptResult.h"
22
23#include "APISerializedScriptValue.h"
24#include "WebKitJavascriptResultPrivate.h"
25#include <jsc/JSCContextPrivate.h>
26#include <jsc/JSCValuePrivate.h>
27
28struct _WebKitJavascriptResult {
29 explicit _WebKitJavascriptResult(WebCore::SerializedScriptValue& serializedScriptValue)
30 {
31 auto* jsContext = SharedJavascriptContext::singleton().getOrCreateContext();
32 jsValue = jscContextGetOrCreateValue(jsContext, serializedScriptValue.deserialize(jscContextGetJSContext(jsContext), nullptr));
33 }
34
35 GRefPtr<JSCValue> jsValue;
36
37 int referenceCount { 1 };
38};
39
40G_DEFINE_BOXED_TYPE(WebKitJavascriptResult, webkit_javascript_result, webkit_javascript_result_ref, webkit_javascript_result_unref)
41
42WebKitJavascriptResult* webkitJavascriptResultCreate(WebCore::SerializedScriptValue& serializedScriptValue)
43{
44 WebKitJavascriptResult* result = static_cast<WebKitJavascriptResult*>(fastMalloc(sizeof(WebKitJavascriptResult)));
45 new (result) WebKitJavascriptResult(serializedScriptValue);
46 return result;
47}
48
49/**
50 * webkit_javascript_result_ref:
51 * @js_result: a #WebKitJavascriptResult
52 *
53 * Atomically increments the reference count of @js_result by one. This
54 * function is MT-safe and may be called from any thread.
55 *
56 * Returns: The passed in #WebKitJavascriptResult
57 */
58WebKitJavascriptResult* webkit_javascript_result_ref(WebKitJavascriptResult* javascriptResult)
59{
60 g_atomic_int_inc(&javascriptResult->referenceCount);
61 return javascriptResult;
62}
63
64/**
65 * webkit_javascript_result_unref:
66 * @js_result: a #WebKitJavascriptResult
67 *
68 * Atomically decrements the reference count of @js_result by one. If the
69 * reference count drops to 0, all memory allocated by the #WebKitJavascriptResult is
70 * released. This function is MT-safe and may be called from any
71 * thread.
72 */
73void webkit_javascript_result_unref(WebKitJavascriptResult* javascriptResult)
74{
75 if (g_atomic_int_dec_and_test(&javascriptResult->referenceCount)) {
76 javascriptResult->~WebKitJavascriptResult();
77 fastFree(javascriptResult);
78 }
79}
80
81#if PLATFORM(GTK)
82/**
83 * webkit_javascript_result_get_global_context: (skip)
84 * @js_result: a #WebKitJavascriptResult
85 *
86 * Get the global Javascript context that should be used with the
87 * <function>JSValueRef</function> returned by webkit_javascript_result_get_value().
88 *
89 * Returns: the <function>JSGlobalContextRef</function> for the #WebKitJavascriptResult
90 *
91 * Deprecated: 2.22: Use jsc_value_get_context() instead.
92 */
93JSGlobalContextRef webkit_javascript_result_get_global_context(WebKitJavascriptResult* javascriptResult)
94{
95 g_return_val_if_fail(javascriptResult, nullptr);
96 return jscContextGetJSContext(jsc_value_get_context(javascriptResult->jsValue.get()));
97}
98
99/**
100 * webkit_javascript_result_get_value: (skip)
101 * @js_result: a #WebKitJavascriptResult
102 *
103 * Get the value of @js_result. You should use the <function>JSGlobalContextRef</function>
104 * returned by webkit_javascript_result_get_global_context() to use the <function>JSValueRef</function>.
105 *
106 * Returns: the <function>JSValueRef</function> of the #WebKitJavascriptResult
107 *
108 * Deprecated: 2.22: Use webkit_javascript_result_get_js_value() instead.
109 */
110JSValueRef webkit_javascript_result_get_value(WebKitJavascriptResult* javascriptResult)
111{
112 g_return_val_if_fail(javascriptResult, nullptr);
113 return jscValueGetJSValue(javascriptResult->jsValue.get());
114}
115#endif
116
117/**
118 * webkit_javascript_result_get_js_value:
119 * @js_result: a #WebKitJavascriptResult
120 *
121 * Get the #JSCValue of @js_result.
122 *
123 * Returns: (transfer none): the #JSCValue of the #WebKitJavascriptResult
124 *
125 * Since: 2.22
126 */
127JSCValue* webkit_javascript_result_get_js_value(WebKitJavascriptResult* javascriptResult)
128{
129 g_return_val_if_fail(javascriptResult, nullptr);
130 return javascriptResult->jsValue.get();
131}
132