FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/JavaScriptCore/API/glib/JSCException.cpp at main · WebKit/WebKit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebKit
/
WebKit
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Pull requests
2.6k
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
API
/
glib
/
JSCException.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
409 lines (359 loc) · 12.7 KB
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
API
/
glib
/
JSCException.cpp
Copy path
File metadata and controls
409 lines (359 loc) · 12.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (C) 2018 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#
include
"
config.h
"
#
include
"
JSCException.h
"
#
include
"
APICast.h
"
#
include
"
JSCContextPrivate.h
"
#
include
"
JSCExceptionPrivate.h
"
#
include
"
JSCInlines.h
"
#
include
"
JSRetainPtr.h
"
#
include
"
StrongInlines.h
"
#
include
<
glib/gprintf.h
>
#
include
<
wtf/glib/GUniquePtr.h
>
#
include
<
wtf/glib/GWeakPtr.h
>
#
include
<
wtf/glib/WTFGType.h
>
/*
*
* JSCException:
* @short_description: JavaScript exception
* @title: JSCException
* @see_also: JSCContext
*
* JSCException represents a JavaScript exception.
*/
struct
_JSCExceptionPrivate
{
GWeakPtr<JSCContext> context;
JSC
::Strong<
JSC
::JSObject> jsException;
bool
cached;
GUniquePtr<
char
> errorName;
GUniquePtr<
char
> message;
unsigned
lineNumber;
unsigned
columnNumber;
GUniquePtr<
char
> sourceURI;
GUniquePtr<
char
> backtrace;
};
WEBKIT_DEFINE_FINAL_TYPE
(JSCException, jsc_exception,
G_TYPE_OBJECT
, GObject)
static
void
jsc_exception_class_init
(JSCExceptionClass*)
{
}
GRefPtr<JSCException>
jscExceptionCreate
(JSCContext* context, JSValueRef jsException)
{
GRefPtr<JSCException> exception =
adoptGRef
(
JSC_EXCEPTION
(
g_object_new
(
JSC_TYPE_EXCEPTION
,
nullptr
)));
auto
* jsContext =
jscContextGetJSContext
(context);
JSC
::JSGlobalObject* globalObject =
toJS
(jsContext);
Ref vm = globalObject->
vm
();
JSC
::JSLockHolder
locker
(vm);
exception->
priv
->
jsException
.
set
(vm,
toJS
(
JSValueToObject
(jsContext, jsException,
nullptr
)));
//
The context has a strong reference to the exception, so we can't ref the context. We use a weak
//
pointer instead to invalidate the exception if the context is destroyed before.
exception->
priv
->
context
.
reset
(context);
return
exception;
}
JSValueRef
jscExceptionGetJSValue
(JSCException* exception)
{
return
toRef
(exception->
priv
->
jsException
.
get
());
}
void
jscExceptionEnsureProperties
(JSCException* exception)
{
JSCExceptionPrivate* priv = exception->
priv
;
if
(priv->
cached
)
return
;
priv->
cached
=
true
;
auto
value =
jscContextGetOrCreateValue
(priv->
context
.
get
(),
toRef
(priv->
jsException
.
get
()));
auto
propertyValue =
adoptGRef
(
jsc_value_object_get_property
(value.
get
(),
"
name
"
));
if
(!
jsc_value_is_undefined
(propertyValue.
get
()))
priv->
errorName
.
reset
(
jsc_value_to_string
(propertyValue.
get
()));
propertyValue =
adoptGRef
(
jsc_value_object_get_property
(value.
get
(),
"
message
"
));
if
(!
jsc_value_is_undefined
(propertyValue.
get
()))
priv->
message
.
reset
(
jsc_value_to_string
(propertyValue.
get
()));
propertyValue =
adoptGRef
(
jsc_value_object_get_property
(value.
get
(),
"
line
"
));
if
(!
jsc_value_is_undefined
(propertyValue.
get
()))
priv->
lineNumber
=
jsc_value_to_int32
(propertyValue.
get
());
propertyValue =
adoptGRef
(
jsc_value_object_get_property
(value.
get
(),
"
column
"
));
if
(!
jsc_value_is_undefined
(propertyValue.
get
()))
priv->
columnNumber
=
jsc_value_to_int32
(propertyValue.
get
());
propertyValue =
adoptGRef
(
jsc_value_object_get_property
(value.
get
(),
"
sourceURL
"
));
if
(!
jsc_value_is_undefined
(propertyValue.
get
()))
priv->
sourceURI
.
reset
(
jsc_value_to_string
(propertyValue.
get
()));
propertyValue =
adoptGRef
(
jsc_value_object_get_property
(value.
get
(),
"
stack
"
));
if
(!
jsc_value_is_undefined
(propertyValue.
get
()))
priv->
backtrace
.
reset
(
jsc_value_to_string
(propertyValue.
get
()));
}
/*
*
* jsc_exception_new:
* @context: a #JSCContext
* @message: the error message
*
* Create a new #JSCException in @context with @message.
*
* Returns: (transfer full): a new #JSCException.
*/
JSCException*
jsc_exception_new
(JSCContext* context,
const
char
* message)
{
return
jsc_exception_new_with_name
(context,
nullptr
, message);
}
/*
*
* jsc_exception_new_printf:
* @context: a #JSCContext
* @format: the string format
* @...: the parameters to insert into the format string
*
* Create a new #JSCException in @context using a formatted string
* for the message.
*
* Returns: (transfer full): a new #JSCException.
*/
JSCException*
jsc_exception_new_printf
(JSCContext* context,
const
char
* format, ...)
{
va_list args;
va_start
(args, format);
auto
* exception =
jsc_exception_new_vprintf
(context, format, args);
va_end
(args);
return
exception;
}
/*
*
* jsc_exception_new_vprintf:
* @context: a #JSCContext
* @format: the string format
* @args: the parameters to insert into the format string
*
* Create a new #JSCException in @context using a formatted string
* for the message. This is similar to jsc_exception_new_printf()
* except that the arguments to the format string are passed as a va_list.
*
* Returns: (transfer full): a new #JSCException.
*/
JSCException*
jsc_exception_new_vprintf
(JSCContext* context,
const
char
* format, va_list args)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
GUniqueOutPtr<
char
> buffer;
g_vasprintf
(&buffer.
outPtr
(), format, args);
return
jsc_exception_new
(context, buffer.
get
());
}
/*
*
* jsc_exception_new_with_name:
* @context: a #JSCContext
* @name: the error name
* @message: the error message
*
* Create a new #JSCException in @context with @name and @message.
*
* Returns: (transfer full): a new #JSCException.
*/
JSCException*
jsc_exception_new_with_name
(JSCContext* context,
const
char
* name,
const
char
* message)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
auto
* jsContext =
jscContextGetJSContext
(context);
JSValueRef jsMessage =
nullptr
;
if
(message) {
JSRetainPtr<JSStringRef>
jsMessageString
(Adopt,
JSStringCreateWithUTF8CString
(message));
jsMessage =
JSValueMakeString
(jsContext, jsMessageString.
get
());
}
auto
exception =
jscExceptionCreate
(context,
JSObjectMakeError
(jsContext, jsMessage ?
1
:
0
, &jsMessage,
nullptr
));
if
(name) {
auto
value =
jscContextGetOrCreateValue
(context,
toRef
(exception->
priv
->
jsException
.
get
()));
GRefPtr<JSCValue> nameValue =
adoptGRef
(
jsc_value_new_string
(context, name));
jsc_value_object_set_property
(value.
get
(),
"
name
"
, nameValue.
get
());
}
return
exception.
leakRef
();
}
/*
*
* jsc_exception_new_with_name_printf:
* @context: a #JSCContext
* @name: the error name
* @format: the string format
* @...: the parameters to insert into the format string
*
* Create a new #JSCException in @context with @name and using a formatted string
* for the message.
*
* Returns: (transfer full): a new #JSCException.
*/
JSCException*
jsc_exception_new_with_name_printf
(JSCContext* context,
const
char
* name,
const
char
* format, ...)
{
va_list args;
va_start
(args, format);
auto
* exception =
jsc_exception_new_with_name_vprintf
(context, name, format, args);
va_end
(args);
return
exception;
}
/*
*
* jsc_exception_new_with_name_vprintf:
* @context: a #JSCContext
* @name: the error name
* @format: the string format
* @args: the parameters to insert into the format string
*
* Create a new #JSCException in @context with @name and using a formatted string
* for the message. This is similar to jsc_exception_new_with_name_printf()
* except that the arguments to the format string are passed as a va_list.
*
* Returns: (transfer full): a new #JSCException.
*/
JSCException*
jsc_exception_new_with_name_vprintf
(JSCContext* context,
const
char
* name,
const
char
* format, va_list args)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
GUniqueOutPtr<
char
> buffer;
g_vasprintf
(&buffer.
outPtr
(), format, args);
return
jsc_exception_new_with_name
(context, name, buffer.
get
());
}
/*
*
* jsc_exception_get_name:
* @exception: a #JSCException
*
* Get the error name of @exception
*
* Returns: the @exception error name.
*/
const
char
*
jsc_exception_get_name
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
nullptr
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
nullptr
);
jscExceptionEnsureProperties
(exception);
return
priv->
errorName
.
get
();
}
/*
*
* jsc_exception_get_message:
* @exception: a #JSCException
*
* Get the error message of @exception.
*
* Returns: the @exception error message.
*/
const
char
*
jsc_exception_get_message
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
nullptr
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
nullptr
);
jscExceptionEnsureProperties
(exception);
return
priv->
message
.
get
();
}
/*
*
* jsc_exception_get_line_number:
* @exception: a #JSCException
*
* Get the line number at which @exception happened.
*
* Returns: the line number of @exception.
*/
guint
jsc_exception_get_line_number
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
0
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
0
);
jscExceptionEnsureProperties
(exception);
return
priv->
lineNumber
;
}
/*
*
* jsc_exception_get_column_number:
* @exception: a #JSCException
*
* Get the column number at which @exception happened.
*
* Returns: the column number of @exception.
*/
guint
jsc_exception_get_column_number
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
0
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
0
);
jscExceptionEnsureProperties
(exception);
return
priv->
columnNumber
;
}
/*
*
* jsc_exception_get_source_uri:
* @exception: a #JSCException
*
* Get the source URI of @exception.
*
* Returns: (nullable): the the source URI of @exception, or %NULL.
*/
const
char
*
jsc_exception_get_source_uri
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
nullptr
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
nullptr
);
jscExceptionEnsureProperties
(exception);
return
priv->
sourceURI
.
get
();
}
/*
*
* jsc_exception_get_backtrace_string:
* @exception: a #JSCException
*
* Get a string with the exception backtrace.
*
* Returns: (nullable): the exception backtrace string or %NULL.
*/
const
char
*
jsc_exception_get_backtrace_string
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
nullptr
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
nullptr
);
jscExceptionEnsureProperties
(exception);
return
priv->
backtrace
.
get
();
}
/*
*
* jsc_exception_to_string:
* @exception: a #JSCException
*
* Get the string representation of @exception error.
*
* Returns: (transfer full): the string representation of @exception.
*/
char
*
jsc_exception_to_string
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
nullptr
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
nullptr
);
auto
value =
jscContextGetOrCreateValue
(priv->
context
.
get
(),
toRef
(priv->
jsException
.
get
()));
return
jsc_value_to_string
(value.
get
());
}
/*
*
* jsc_exception_report:
* @exception: a #JSCException
*
* Return a report message of @exception, containing all the possible details such us
* source URI, line, column and backtrace, and formatted to be printed.
*
* Returns: (transfer full): a new string with the exception report
*/
char
*
jsc_exception_report
(JSCException* exception)
{
g_return_val_if_fail
(
JSC_IS_EXCEPTION
(exception),
nullptr
);
JSCExceptionPrivate* priv = exception->
priv
;
g_return_val_if_fail
(priv->
context
,
nullptr
);
jscExceptionEnsureProperties
(exception);
auto
report =
StringBuilder
();
if
(priv->
sourceURI
)
report.
append
(
unsafeSpan
(priv->
sourceURI
.
get
()));
if
(priv->
lineNumber
)
report.
append
(
'
:
'
, priv->
lineNumber
);
if
(priv->
columnNumber
)
report.
append
(
'
:
'
, priv->
columnNumber
);
report.
append
(
'
'
);
GUniquePtr<
char
>
errorMessage
(
jsc_exception_to_string
(exception));
if
(errorMessage)
report.
append
(
unsafeSpan
(errorMessage.
get
()));
report.
append
(
'
\n
'
);
if
(priv->
backtrace
) {
for
(
auto
line :
StringView::fromLatin1
(priv->
backtrace
.
get
()).
split
(
'
\n
'
))
report.
append
(
"
"
_s, line,
'
\n
'
);
}
return
g_strdup
(report.
toString
().
utf8
().
data
());
}
Back
|
FazBrowse Home
|
New Git URL