FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/JavaScriptCore/API/glib/JSCContext.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
/
JSCContext.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1114 lines (976 loc) · 42.4 KB
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
API
/
glib
/
JSCContext.cpp
Copy path
File metadata and controls
1114 lines (976 loc) · 42.4 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Igalia S.L.
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* 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
"
JSCContext.h
"
#
include
"
APICast.h
"
#
include
"
IntegrityInlines.h
"
#
include
"
JSCClassPrivate.h
"
#
include
"
JSCContextInternal.h
"
#
include
"
JSCContextPrivate.h
"
#
include
"
JSCExceptionPrivate.h
"
#
include
"
JSCInlines.h
"
#
include
"
JSCValuePrivate.h
"
#
include
"
JSCVirtualMachinePrivate.h
"
#
include
"
JSCWrapperMap.h
"
#
include
"
JSRetainPtr.h
"
#
include
"
JSWithScope.h
"
#
include
"
OpaqueJSString.h
"
#
include
"
Parser.h
"
#
include
<
wtf/glib/GSpanExtras.h
>
#
include
<
wtf/glib/GUniquePtr.h
>
#
include
<
wtf/glib/WTFGType.h
>
#
include
<
wtf/text/MakeString.h
>
/*
*
* JSCContext:
* @short_description: JavaScript execution context
* @title: JSCContext
*
* JSCContext represents a JavaScript execution context, where all operations
* take place and where the values will be associated.
*
* When a new context is created, a global object is allocated and the built-in JavaScript
* objects (Object, Function, String, Array) are populated. You can execute JavaScript in
* the context by using jsc_context_evaluate() or jsc_context_evaluate_with_source_uri().
* It's also possible to register custom objects in the context with jsc_context_register_class().
*/
enum
{
PROP_0
,
PROP_VIRTUAL_MACHINE
,
};
struct
JSCContextExceptionHandler
{
JSCContextExceptionHandler
(JSCExceptionHandler handler,
void
* userData =
nullptr
, GDestroyNotify destroyNotifyFunction =
nullptr
)
: handler(handler)
, userData(userData)
, destroyNotifyFunction(destroyNotifyFunction)
{
}
~JSCContextExceptionHandler
()
{
if
(destroyNotifyFunction)
destroyNotifyFunction
(userData);
}
JSCContextExceptionHandler
(JSCContextExceptionHandler&& other)
{
std::swap
(handler, other.
handler
);
std::swap
(userData, other.
userData
);
std::swap
(destroyNotifyFunction, other.
destroyNotifyFunction
);
}
JSCContextExceptionHandler
(
const
JSCContextExceptionHandler&) =
delete
;
JSCContextExceptionHandler&
operator
=(
const
JSCContextExceptionHandler&) =
delete
;
JSCExceptionHandler handler {
nullptr
};
void
* userData {
nullptr
};
GDestroyNotify destroyNotifyFunction {
nullptr
};
};
struct
_JSCContextPrivate
{
GRefPtr<JSCVirtualMachine> vm;
JSRetainPtr<JSGlobalContextRef> jsContext;
GRefPtr<JSCException> exception;
Vector<JSCContextExceptionHandler> exceptionHandlers;
};
WEBKIT_DEFINE_FINAL_TYPE
(JSCContext, jsc_context,
G_TYPE_OBJECT
, GObject)
static
void
jscContextSetVirtualMachine
(JSCContext* context, GRefPtr<JSCVirtualMachine>&& vm)
{
JSCContextPrivate* priv = context->
priv
;
if
(vm) {
ASSERT
(!priv->
vm
);
priv->
vm
=
WTF::move
(vm);
ASSERT
(!priv->
jsContext
);
GUniquePtr<
char
>
name
(
g_strdup_printf
(
"
%p-jsContext
"
, &
Thread::currentSingleton
()));
if
(
auto
* data =
g_object_get_data
(
G_OBJECT
(priv->
vm
.
get
()), name.
get
())) {
priv->
jsContext
=
static_cast
<JSGlobalContextRef>(data);
g_object_set_data
(
G_OBJECT
(priv->
vm
.
get
()), name.
get
(),
nullptr
);
}
else
priv->
jsContext
= JSRetainPtr<JSGlobalContextRef>(Adopt,
JSGlobalContextCreateInGroup
(
jscVirtualMachineGetContextGroup
(priv->
vm
.
get
()),
nullptr
));
auto
* globalObject =
toJSGlobalObject
(priv->
jsContext
.
get
());
if
(!globalObject->
wrapperMap
())
globalObject->
setWrapperMap
(makeUnique<
JSC
::WrapperMap>(priv->
jsContext
.
get
()));
jscVirtualMachineAddContext
(priv->
vm
.
get
(), context);
}
else
if
(priv->
vm
) {
ASSERT
(priv->
jsContext
);
jscVirtualMachineRemoveContext
(priv->
vm
.
get
(), context);
priv->
jsContext
=
nullptr
;
priv->
vm
=
nullptr
;
}
}
static
void
jscContextGetProperty
(GObject* object, guint propID, GValue* value, GParamSpec* paramSpec)
{
JSCContextPrivate* priv =
JSC_CONTEXT
(object)->
priv
;
switch
(propID) {
case
PROP_VIRTUAL_MACHINE
:
g_value_set_object
(value, priv->
vm
.
get
());
break
;
default
:
G_OBJECT_WARN_INVALID_PROPERTY_ID
(object, propID, paramSpec);
}
}
static
void
jscContextSetProperty
(GObject* object, guint propID,
const
GValue* value, GParamSpec* paramSpec)
{
JSCContext* context =
JSC_CONTEXT
(object);
switch
(propID) {
case
PROP_VIRTUAL_MACHINE
:
if
(gpointer vm =
g_value_get_object
(value))
jscContextSetVirtualMachine
(context, GRefPtr<JSCVirtualMachine>(
JSC_VIRTUAL_MACHINE
(vm)));
break
;
default
:
G_OBJECT_WARN_INVALID_PROPERTY_ID
(object, propID, paramSpec);
}
}
static
void
jscContextConstructed
(GObject* object)
{
G_OBJECT_CLASS
(jsc_context_parent_class)->
constructed
(object);
JSCContext* context =
JSC_CONTEXT
(object);
if
(!context->
priv
->
vm
)
jscContextSetVirtualMachine
(context,
adoptGRef
(
jsc_virtual_machine_new
()));
context->
priv
->
exceptionHandlers
.
append
(
JSCContextExceptionHandler
([](JSCContext* context, JSCException* exception, gpointer) {
jsc_context_throw_exception
(context, exception);
}));
}
static
void
jscContextDispose
(GObject* object)
{
JSCContext* context =
JSC_CONTEXT
(object);
jscContextSetVirtualMachine
(context,
nullptr
);
G_OBJECT_CLASS
(jsc_context_parent_class)->
dispose
(object);
}
static
void
jsc_context_class_init
(JSCContextClass* klass)
{
GObjectClass* objClass =
G_OBJECT_CLASS
(klass);
objClass->
get_property
= jscContextGetProperty;
objClass->
set_property
= jscContextSetProperty;
objClass->
constructed
= jscContextConstructed;
objClass->
dispose
= jscContextDispose;
/*
*
* JSCContext:virtual-machine:
*
* The #JSCVirtualMachine in which the context was created.
*/
g_object_class_install_property
(objClass,
PROP_VIRTUAL_MACHINE
,
g_param_spec_object
(
"
virtual-machine
"
,
nullptr
,
nullptr
,
JSC_TYPE_VIRTUAL_MACHINE
,
static_cast
<GParamFlags>(
WEBKIT_PARAM_READWRITE
|
G_PARAM_CONSTRUCT_ONLY
)));
}
GRefPtr<JSCContext>
jscContextGetOrCreate
(JSGlobalContextRef jsContext)
{
auto
vm =
jscVirtualMachineGetOrCreate
(
toRef
(&
toJS
(jsContext)->
vm
()));
if
(GRefPtr<JSCContext> context =
jscVirtualMachineGetContext
(vm.
get
(), jsContext))
return
context;
GUniquePtr<
char
>
name
(
g_strdup_printf
(
"
%p-jsContext
"
, &
Thread::currentSingleton
()));
g_object_set_data
(
G_OBJECT
(vm.
get
()), name.
get
(), jsContext);
return
adoptGRef
(
jsc_context_new_with_virtual_machine
(vm.
get
()));
}
JSGlobalContextRef
jscContextGetJSContext
(JSCContext* context)
{
ASSERT
(
JSC_IS_CONTEXT
(context));
JSCContextPrivate* priv = context->
priv
;
return
priv->
jsContext
.
get
();
}
static
JSC
::WrapperMap&
wrapperMap
(JSCContext* context)
{
auto
* map =
toJSGlobalObject
(context->
priv
->
jsContext
.
get
())->
wrapperMap
();
ASSERT
(map);
return
*map;
}
GRefPtr<JSCValue>
jscContextGetOrCreateValue
(JSCContext* context, JSValueRef jsValue)
{
return
wrapperMap
(context).
gobjectWrapper
(context, jsValue);
}
void
jscContextValueDestroyed
(JSCContext* context, JSValueRef jsValue)
{
wrapperMap
(context).
unwrap
(jsValue);
}
JSC
::JSObject*
jscContextGetJSWrapper
(JSCContext* context, gpointer wrappedObject)
{
return
wrapperMap
(context).
jsWrapper
(wrappedObject);
}
JSC
::JSObject*
jscContextGetOrCreateJSWrapper
(JSCContext* context, JSClassRef jsClass, JSValueRef prototype, gpointer wrappedObject, GDestroyNotify destroyFunction)
{
if
(
auto
* jsWrapper =
jscContextGetJSWrapper
(context, wrappedObject))
return
jsWrapper;
return
wrapperMap
(context).
createJSWrapper
(context->
priv
->
jsContext
.
get
(), jsClass, prototype, wrappedObject, destroyFunction);
}
JSGlobalContextRef
jscContextCreateContextWithJSWrapper
(JSCContext* context, JSClassRef jsClass, JSValueRef prototype, gpointer wrappedObject, GDestroyNotify destroyFunction)
{
return
wrapperMap
(context).
createContextWithJSWrapper
(
jscVirtualMachineGetContextGroup
(context->
priv
->
vm
.
get
()), jsClass, prototype, wrappedObject, destroyFunction);
}
gpointer
jscContextWrappedObject
(JSCContext* context, JSObjectRef jsObject)
{
return
wrapperMap
(context).
wrappedObject
(context->
priv
->
jsContext
.
get
(), jsObject);
}
JSCClass*
jscContextGetRegisteredClass
(JSCContext* context, JSClassRef jsClass)
{
return
wrapperMap
(context).
registeredClass
(jsClass);
}
CallbackData
jscContextPushCallback
(JSCContext* context, JSValueRef calleeValue, JSValueRef thisValue,
size_t
argumentCount,
const
JSValueRef* arguments)
{
auto
& thread =
Thread::currentSingleton
();
auto
* previousStack =
static_cast
<CallbackData*>(thread.
m_apiData
);
CallbackData data = { context,
WTF::move
(context->
priv
->
exception
), calleeValue, thisValue, argumentCount, arguments, previousStack };
thread.
m_apiData
= &data;
return
data;
}
void
jscContextPopCallback
(JSCContext* context, CallbackData&& data)
{
auto
& thread =
Thread::currentSingleton
();
context->
priv
->
exception
=
WTF::move
(data.
preservedException
);
thread.
m_apiData
= data.
next
;
}
JSValueRef
jscContextGArrayToJSArray
(JSCContext* context, GPtrArray*
gArray
, JSValueRef* exception)
{
JSCContextPrivate* priv = context->
priv
;
JSC
::JSGlobalObject* globalObject =
toJS
(priv->
jsContext
.
get
());
JSC
::JSLockHolder
locker
(globalObject);
auto
* jsArray =
JSObjectMakeArray
(priv->
jsContext
.
get
(),
0
,
nullptr
, exception);
if
(*exception)
return
JSValueMakeUndefined
(priv->
jsContext
.
get
());
if
(!
gArray
)
return
jsArray;
auto
* jsArrayObject =
JSValueToObject
(priv->
jsContext
.
get
(), jsArray, exception);
if
(*exception)
return
JSValueMakeUndefined
(priv->
jsContext
.
get
());
auto
i =
0
;
for
(
auto
& item :
span
(
gArray
)) {
if
(!item)
JSObjectSetPropertyAtIndex
(priv->
jsContext
.
get
(), jsArrayObject, i,
JSValueMakeNull
(priv->
jsContext
.
get
()), exception);
else
if
(
JSC_IS_VALUE
(item))
JSObjectSetPropertyAtIndex
(priv->
jsContext
.
get
(), jsArrayObject, i,
jscValueGetJSValue
(
JSC_VALUE
(item)), exception);
else
*exception =
toRef
(
JSC::createTypeError
(globalObject,
"
invalid item type in GPtrArray
"
_s));
if
(*exception)
return
JSValueMakeUndefined
(priv->
jsContext
.
get
());
i++;
}
return
jsArray;
}
static
GRefPtr<GPtrArray>
jscContextJSArrayToGArray
(JSCContext* context, JSValueRef jsArray, JSValueRef* exception)
{
JSCContextPrivate* priv = context->
priv
;
JSC
::JSGlobalObject* globalObject =
toJS
(priv->
jsContext
.
get
());
JSC
::JSLockHolder
locker
(globalObject);
if
(
JSValueIsNull
(priv->
jsContext
.
get
(), jsArray))
return
nullptr
;
if
(!
JSValueIsArray
(priv->
jsContext
.
get
(), jsArray)) {
*exception =
toRef
(
JSC::createTypeError
(globalObject,
"
invalid js type for GPtrArray
"
_s));
return
nullptr
;
}
auto
* jsArrayObject =
JSValueToObject
(priv->
jsContext
.
get
(), jsArray, exception);
if
(*exception)
return
nullptr
;
JSRetainPtr<JSStringRef>
lengthString
(Adopt,
JSStringCreateWithUTF8CString
(
"
length
"
));
auto
* jsLength =
JSObjectGetProperty
(priv->
jsContext
.
get
(), jsArrayObject, lengthString.
get
(), exception);
if
(*exception)
return
nullptr
;
auto
length =
JSC::toUInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsLength, exception));
if
(*exception)
return
nullptr
;
GRefPtr<GPtrArray>
gArray
=
adoptGRef
(
g_ptr_array_new_with_free_func
(g_object_unref));
for
(
unsigned
i =
0
; i < length; ++i) {
auto
* jsItem =
JSObjectGetPropertyAtIndex
(priv->
jsContext
.
get
(), jsArrayObject, i, exception);
if
(*exception)
return
nullptr
;
g_ptr_array_add
(
gArray
.
get
(), jsItem ?
jscContextGetOrCreateValue
(context, jsItem).
leakRef
() :
nullptr
);
}
return
gArray
;
}
GUniquePtr<
char
*>
jscContextJSArrayToGStrv
(JSCContext* context, JSValueRef jsArray, JSValueRef* exception)
{
JSCContextPrivate* priv = context->
priv
;
JSC
::JSGlobalObject* globalObject =
toJS
(priv->
jsContext
.
get
());
JSC
::JSLockHolder
locker
(globalObject);
if
(
JSValueIsNull
(priv->
jsContext
.
get
(), jsArray))
return
nullptr
;
if
(!
JSValueIsArray
(priv->
jsContext
.
get
(), jsArray)) {
*exception =
toRef
(
JSC::createTypeError
(globalObject,
"
invalid js type for GStrv
"
_s));
return
nullptr
;
}
auto
* jsArrayObject =
JSValueToObject
(priv->
jsContext
.
get
(), jsArray, exception);
if
(*exception)
return
nullptr
;
JSRetainPtr<JSStringRef>
lengthString
(Adopt,
JSStringCreateWithUTF8CString
(
"
length
"
));
auto
* jsLength =
JSObjectGetProperty
(priv->
jsContext
.
get
(), jsArrayObject, lengthString.
get
(), exception);
if
(*exception)
return
nullptr
;
auto
length =
JSC::toUInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsLength, exception));
if
(*exception)
return
nullptr
;
auto
sizeInBytes = checkedSum<
size_t
>(length,
1
);
sizeInBytes *=
sizeof
(
char
*);
if
(sizeInBytes.
hasOverflowed
())
return
nullptr
;
auto
strv = GMallocSpan<
char
*>::
zeroedMalloc
(sizeInBytes);
for
(
unsigned
i =
0
; i < length; ++i) {
auto
* jsItem =
JSObjectGetPropertyAtIndex
(priv->
jsContext
.
get
(), jsArrayObject, i, exception);
if
(*exception)
return
nullptr
;
auto
jsValueItem =
jscContextGetOrCreateValue
(context, jsItem);
if
(!
jsc_value_is_string
(jsValueItem.
get
())) {
*exception =
toRef
(
JSC::createTypeError
(globalObject,
makeString
(
"
invalid js type for GStrv: item
"
_s, i,
"
is not a string
"
_s)));
return
nullptr
;
}
strv[i] =
jsc_value_to_string
(jsValueItem.
get
());
}
return
GUniquePtr<
char
*> { strv.
leakSpan
().
data
() };
}
JSValueRef
jscContextGValueToJSValue
(JSCContext* context,
const
GValue* value, JSValueRef* exception)
{
JSCContextPrivate* priv = context->
priv
;
JSC
::JSGlobalObject* globalObject =
toJS
(priv->
jsContext
.
get
());
JSC
::JSLockHolder
locker
(globalObject);
switch
(
g_type_fundamental
(
G_VALUE_TYPE
(value))) {
case
G_TYPE_BOOLEAN
:
return
JSValueMakeBoolean
(priv->
jsContext
.
get
(),
g_value_get_boolean
(value));
case
G_TYPE_CHAR
:
case
G_TYPE_INT
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_int
);
case
G_TYPE_ENUM
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(),
g_value_get_enum
(value));
case
G_TYPE_FLAGS
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(),
g_value_get_flags
(value));
case
G_TYPE_UCHAR
:
case
G_TYPE_UINT
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_uint
);
case
G_TYPE_FLOAT
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_float
);
case
G_TYPE_DOUBLE
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_double
);
case
G_TYPE_LONG
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_long
);
case
G_TYPE_ULONG
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_ulong
);
case
G_TYPE_INT64
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_int64
);
case
G_TYPE_UINT64
:
return
JSValueMakeNumber
(priv->
jsContext
.
get
(), value->
data
[
0
].
v_uint64
);
case
G_TYPE_STRING
:
if
(
const
char
* stringValue =
g_value_get_string
(value)) {
JSRetainPtr<JSStringRef>
jsString
(Adopt,
JSStringCreateWithUTF8CString
(stringValue));
return
JSValueMakeString
(priv->
jsContext
.
get
(), jsString.
get
());
}
return
JSValueMakeNull
(priv->
jsContext
.
get
());
case
G_TYPE_POINTER
:
case
G_TYPE_OBJECT
:
case
G_TYPE_BOXED
:
if
(
auto
* ptr = value->
data
[
0
].
v_pointer
) {
if
(
auto
* jsWrapper =
jscContextGetJSWrapper
(context, ptr))
return
toRef
(jsWrapper);
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
JSC_TYPE_VALUE
))
return
jscValueGetJSValue
(
JSC_VALUE
(ptr));
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
JSC_TYPE_EXCEPTION
))
return
jscExceptionGetJSValue
(
JSC_EXCEPTION
(ptr));
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
G_TYPE_PTR_ARRAY
))
return
jscContextGArrayToJSArray
(context,
static_cast
<GPtrArray*>(ptr), exception);
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
G_TYPE_STRV
)) {
auto
items =
span
(
static_cast
<
char
**>(ptr));
GRefPtr<GPtrArray>
gArray
=
adoptGRef
(
g_ptr_array_new_full
(items.
size
(), g_object_unref));
for
(
auto
* item : items)
g_ptr_array_add
(
gArray
.
get
(),
jsc_value_new_string
(context, item));
return
jscContextGArrayToJSArray
(context,
gArray
.
get
(), exception);
}
}
else
return
JSValueMakeNull
(priv->
jsContext
.
get
());
break
;
case
G_TYPE_PARAM
:
case
G_TYPE_INTERFACE
:
case
G_TYPE_VARIANT
:
default
:
break
;
}
*exception =
toRef
(
JSC::createTypeError
(globalObject,
makeString
(
"
unsupported type
"
_s,
unsafeSpan
(
g_type_name
(
G_VALUE_TYPE
(value))))));
return
JSValueMakeUndefined
(priv->
jsContext
.
get
());
}
static
inline
gpointer
jscContextJSValueToWrappedObject
(JSCContext* context, JSValueRef jsValue)
{
auto
jsObject =
JSValueToObject
(context->
priv
->
jsContext
.
get
(), jsValue,
nullptr
);
return
jsObject ?
jscContextWrappedObject
(context, jsObject) :
nullptr
;
}
void
jscContextJSValueToGValue
(JSCContext* context, JSValueRef jsValue, GType type, GValue* value, JSValueRef* exception)
{
JSCContextPrivate* priv = context->
priv
;
JSC
::JSGlobalObject* globalObject =
toJS
(priv->
jsContext
.
get
());
JSC
::JSLockHolder
locker
(globalObject);
g_value_init
(value, type);
auto
fundamentalType =
g_type_fundamental
(
G_VALUE_TYPE
(value));
switch
(fundamentalType) {
case
G_TYPE_INT
:
g_value_set_int
(value,
JSC::toInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception)));
break
;
case
G_TYPE_FLOAT
:
g_value_set_float
(value,
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception));
break
;
case
G_TYPE_DOUBLE
:
g_value_set_double
(value,
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception));
break
;
case
G_TYPE_BOOLEAN
:
g_value_set_boolean
(value,
JSValueToBoolean
(priv->
jsContext
.
get
(), jsValue));
break
;
case
G_TYPE_STRING
:
if
(!
JSValueIsNull
(priv->
jsContext
.
get
(), jsValue)) {
JSRetainPtr<JSStringRef>
jsString
(Adopt,
JSValueToStringCopy
(priv->
jsContext
.
get
(), jsValue, exception));
if
(*exception)
return
;
size_t
maxSize =
JSStringGetMaximumUTF8CStringSize
(jsString.
get
());
auto
* string =
static_cast
<
char
*>(
g_malloc
(maxSize));
JSStringGetUTF8CString
(jsString.
get
(), string, maxSize);
g_value_take_string
(value, string);
}
else
g_value_set_string
(value,
nullptr
);
break
;
case
G_TYPE_CHAR
:
g_value_set_schar
(value,
JSC::toInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception)));
break
;
case
G_TYPE_UCHAR
:
g_value_set_uchar
(value,
JSC::toUInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception)));
break
;
case
G_TYPE_UINT
:
g_value_set_uint
(value,
JSC::toUInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception)));
break
;
case
G_TYPE_POINTER
:
case
G_TYPE_OBJECT
:
case
G_TYPE_BOXED
: {
gpointer wrappedObject =
jscContextJSValueToWrappedObject
(context, jsValue);
if
(!wrappedObject) {
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
JSC_TYPE_VALUE
)) {
auto
jscValue =
jscContextGetOrCreateValue
(context, jsValue);
g_value_set_object
(value, jscValue.
get
());
return
;
}
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
JSC_TYPE_EXCEPTION
)) {
auto
exception =
jscExceptionCreate
(context, jsValue);
g_value_set_object
(value, exception.
get
());
return
;
}
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
G_TYPE_PTR_ARRAY
)) {
auto
gArray
=
jscContextJSArrayToGArray
(context, jsValue, exception);
if
(!*exception)
g_value_take_boxed
(value,
gArray
.
leakRef
());
return
;
}
if
(
g_type_is_a
(
G_VALUE_TYPE
(value),
G_TYPE_STRV
)) {
auto
strv =
jscContextJSArrayToGStrv
(context, jsValue, exception);
if
(!*exception)
g_value_take_boxed
(value, strv.
release
());
return
;
}
*exception =
toRef
(
JSC::createTypeError
(globalObject,
"
invalid pointer type
"
_s));
return
;
}
if
(fundamentalType ==
G_TYPE_POINTER
)
g_value_set_pointer
(value, wrappedObject);
else
if
(fundamentalType ==
G_TYPE_BOXED
)
g_value_set_boxed
(value, wrappedObject);
else
if
(
G_IS_OBJECT
(wrappedObject))
g_value_set_object
(value, wrappedObject);
else
*exception =
toRef
(
JSC::createTypeError
(globalObject,
"
wrapped object is not a GObject
"
_s));
break
;
}
case
G_TYPE_LONG
:
g_value_set_long
(value,
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception));
break
;
case
G_TYPE_ULONG
:
g_value_set_ulong
(value,
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception));
break
;
case
G_TYPE_INT64
:
g_value_set_int64
(value,
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception));
break
;
case
G_TYPE_UINT64
:
g_value_set_uint64
(value,
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception));
break
;
case
G_TYPE_ENUM
:
g_value_set_enum
(value,
JSC::toInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception)));
break
;
case
G_TYPE_FLAGS
:
g_value_set_flags
(value,
JSC::toInt32
(
JSValueToNumber
(priv->
jsContext
.
get
(), jsValue, exception)));
break
;
case
G_TYPE_PARAM
:
case
G_TYPE_INTERFACE
:
case
G_TYPE_VARIANT
:
default
:
*exception =
toRef
(
JSC::createTypeError
(globalObject,
makeString
(
"
unsupported type
"
_s,
unsafeSpan
(
g_type_name
(
G_VALUE_TYPE
(value))))));
break
;
}
}
void
jscContextGarbageCollect
(JSCContext* context,
bool
sanitizeStack)
{
auto
* jsContext = context->
priv
->
jsContext
.
get
();
JSC
::JSGlobalObject* globalObject =
toJS
(jsContext);
Ref vm = globalObject->
vm
();
JSC
::JSLockHolder
locker
(vm);
if
(sanitizeStack)
sanitizeStackForVM
(vm);
vm->
heap
.
collectNow
(
JSC
::Sync,
JSC
::CollectionScope::Full);
}
/*
*
* jsc_context_new:
*
* Create a new #JSCContext. The context is created in a new #JSCVirtualMachine.
* Use jsc_context_new_with_virtual_machine() to create a new #JSCContext in an
* existing #JSCVirtualMachine.
*
* Returns: (transfer full): the newly created #JSCContext.
*/
JSCContext*
jsc_context_new
()
{
return
JSC_CONTEXT
(
g_object_new
(
JSC_TYPE_CONTEXT
,
nullptr
));
}
/*
*
* jsc_context_new_with_virtual_machine:
* @vm: a #JSCVirtualMachine
*
* Create a new #JSCContext in @virtual_machine.
*
* Returns: (transfer full): the newly created #JSCContext.
*/
JSCContext*
jsc_context_new_with_virtual_machine
(JSCVirtualMachine* vm)
{
g_return_val_if_fail
(
JSC_IS_VIRTUAL_MACHINE
(vm),
nullptr
);
return
JSC_CONTEXT
(
g_object_new
(
JSC_TYPE_CONTEXT
,
"
virtual-machine
"
, vm,
nullptr
));
}
/*
*
* jsc_context_get_virtual_machine:
* @context: a #JSCContext
*
* Get the #JSCVirtualMachine where @context was created.
*
* Returns: (transfer none): the #JSCVirtualMachine where the #JSCContext was created.
*/
JSCVirtualMachine*
jsc_context_get_virtual_machine
(JSCContext* context)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
return
context->
priv
->
vm
.
get
();
}
/*
*
* jsc_context_get_exception:
* @context: a #JSCContext
*
* Get the last unhandled exception thrown in @context by API functions calls.
*
* Returns: (transfer none) (nullable): a #JSCException or %NULL if there isn't any
* unhandled exception in the #JSCContext.
*/
JSCException*
jsc_context_get_exception
(JSCContext *context)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
return
context->
priv
->
exception
.
get
();
}
/*
*
* jsc_context_throw:
* @context: a #JSCContext
* @error_message: an error message
*
* Throw an exception to @context using the given error message. The created #JSCException
* can be retrieved with jsc_context_get_exception().
*/
void
jsc_context_throw
(JSCContext* context,
const
char
* errorMessage)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
context->
priv
->
exception
=
adoptGRef
(
jsc_exception_new
(context, errorMessage));
}
/*
*
* jsc_context_throw_printf:
* @context: a #JSCContext
* @format: the string format
* @...: the parameters to insert into the format string
*
* Throw an exception to @context using the given formatted string as error message.
* The created #JSCException can be retrieved with jsc_context_get_exception().
*/
void
jsc_context_throw_printf
(JSCContext* context,
const
char
* format, ...)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
va_list args;
va_start
(args, format);
context->
priv
->
exception
=
adoptGRef
(
jsc_exception_new_vprintf
(context, format, args));
va_end
(args);
}
/*
*
* jsc_context_throw_with_name:
* @context: a #JSCContext
* @error_name: the error name
* @error_message: an error message
*
* Throw an exception to @context using the given error name and message. The created #JSCException
* can be retrieved with jsc_context_get_exception().
*/
void
jsc_context_throw_with_name
(JSCContext* context,
const
char
* errorName,
const
char
* errorMessage)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
g_return_if_fail
(errorName);
context->
priv
->
exception
=
adoptGRef
(
jsc_exception_new_with_name
(context, errorName, errorMessage));
}
/*
*
* jsc_context_throw_with_name_printf:
* @context: a #JSCContext
* @error_name: the error name
* @format: the string format
* @...: the parameters to insert into the format string
*
* Throw an exception to @context using the given error name and the formatted string as error message.
* The created #JSCException can be retrieved with jsc_context_get_exception().
*/
void
jsc_context_throw_with_name_printf
(JSCContext* context,
const
char
* errorName,
const
char
* format, ...)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
va_list args;
va_start
(args, format);
context->
priv
->
exception
=
adoptGRef
(
jsc_exception_new_with_name_vprintf
(context, errorName, format, args));
va_end
(args);
}
/*
*
* jsc_context_throw_exception:
* @context: a #JSCContext
* @exception: a #JSCException
*
* Throw @exception to @context.
*/
void
jsc_context_throw_exception
(JSCContext* context, JSCException* exception)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
g_return_if_fail
(
JSC_IS_EXCEPTION
(exception));
context->
priv
->
exception
= exception;
}
/*
*
* jsc_context_clear_exception:
* @context: a #JSCContext
*
* Clear the uncaught exception in @context if any.
*/
void
jsc_context_clear_exception
(JSCContext* context)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
context->
priv
->
exception
=
nullptr
;
}
/*
*
* JSCExceptionHandler:
* @context: a #JSCContext
* @exception: a #JSCException
* @user_data: (closure): user data
*
* Function used to handle JavaScript exceptions in a #JSCContext.
*/
/*
*
* jsc_context_push_exception_handler:
* @context: a #JSCContext
* @handler: (closure user_data): a #JSCExceptionHandler
* @user_data: user data to pass to @handler
* @destroy_notify: (nullable): destroy notifier for @user_data
*
* Push an exception handler in @context. Whenever a JavaScript exception happens in
* the #JSCContext, the given @handler will be called. The default #JSCExceptionHandler
* simply calls jsc_context_throw_exception() to throw the exception to the #JSCContext.
* If you don't want to catch the exception, but only get notified about it, call
* jsc_context_throw_exception() in @handler like the default one does.
* The last exception handler pushed is the only one used by the #JSCContext, use
* jsc_context_pop_exception_handler() to remove it and set the previous one. When @handler
* is removed from the context, @destroy_notify is called with @user_data as parameter.
*/
void
jsc_context_push_exception_handler
(JSCContext* context, JSCExceptionHandler handler, gpointer userData, GDestroyNotify destroyNotify)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
g_return_if_fail
(handler);
context->
priv
->
exceptionHandlers
.
append
({ handler, userData, destroyNotify });
}
/*
*
* jsc_context_pop_exception_handler:
* @context: a #JSCContext
*
* Remove the last #JSCExceptionHandler previously pushed to @context with
* jsc_context_push_exception_handler().
*/
void
jsc_context_pop_exception_handler
(JSCContext* context)
{
g_return_if_fail
(
JSC_IS_CONTEXT
(context));
g_return_if_fail
(context->
priv
->
exceptionHandlers
.
size
() >
1
);
context->
priv
->
exceptionHandlers
.
removeLast
();
}
bool
jscContextHandleExceptionIfNeeded
(JSCContext* context, JSValueRef jsException)
{
if
(!jsException)
return
false
;
auto
exception =
jscExceptionCreate
(context, jsException);
ASSERT
(!context->
priv
->
exceptionHandlers
.
isEmpty
());
const
auto
& exceptionHandler = context->
priv
->
exceptionHandlers
.
last
();
exceptionHandler.
handler
(context, exception.
get
(), exceptionHandler.
userData
);
return
true
;
}
/*
*
* jsc_context_get_current:
*
* Get the #JSCContext that is currently executing a function. This should only be
* called within a function or method callback, otherwise %NULL will be returned.
*
* Returns: (transfer none) (nullable): the #JSCContext that is currently executing.
*/
JSCContext*
jsc_context_get_current
()
{
auto
* data =
static_cast
<CallbackData*>(
Thread::currentSingleton
().
m_apiData
);
return
data ? data->
context
.
get
() :
nullptr
;
}
/*
*
* jsc_context_evaluate:
* @context: a #JSCContext
* @code: a JavaScript script to evaluate
* @length: length of @code, or -1 if @code is a nul-terminated string
*
* Evaluate @code in @context.
*
* Returns: (transfer full): a #JSCValue representing the last value generated by the script.
*/
JSCValue*
jsc_context_evaluate
(JSCContext* context,
const
char
* code, gssize length)
{
return
jsc_context_evaluate_with_source_uri
(context, code, length,
nullptr
,
0
);
}
static
JSValueRef
evaluateScriptInContext
(JSGlobalContextRef jsContext, String&& script,
const
char
* uri,
unsigned
lineNumber, JSValueRef* exception)
{
JSRetainPtr<JSStringRef>
scriptJS
(Adopt,
OpaqueJSString::tryCreate
(
WTF::move
(script)).
leakRef
());
JSRetainPtr<JSStringRef> sourceURI = uri ?
adopt
(
JSStringCreateWithUTF8CString
(uri)) :
nullptr
;
return
JSEvaluateScript
(jsContext, scriptJS.
get
(),
nullptr
, sourceURI.
get
(), lineNumber, exception);
}
/*
*
* jsc_context_evaluate_with_source_uri:
* @context: a #JSCContext
* @code: a JavaScript script to evaluate
* @length: length of @code, or -1 if @code is a nul-terminated string
* @uri: the source URI
* @line_number: the starting line number
*
* Evaluate @code in @context using @uri as the source URI. The @line_number is the starting line number
* in @uri; the value is one-based so the first line is 1. @uri and @line_number will be shown in exceptions and
* they don't affect the behavior of the script.
*
* Returns: (transfer full): a #JSCValue representing the last value generated by the script.
*/
JSCValue*
jsc_context_evaluate_with_source_uri
(JSCContext* context,
const
char
* code, gssize length,
const
char
* uri,
unsigned
lineNumber)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
g_return_val_if_fail
(code,
nullptr
);
JSValueRef exception =
nullptr
;
JSValueRef result =
evaluateScriptInContext
(context->
priv
->
jsContext
.
get
(), length <
0
?
String::fromUTF8
(code) :
String::fromUTF8
(
unsafeMakeSpan
(code, length)), uri, lineNumber, &exception);
if
(
jscContextHandleExceptionIfNeeded
(context, exception))
return
jsc_value_new_undefined
(context);
return
jscContextGetOrCreateValue
(context, result).
leakRef
();
}
/*
*
* jsc_context_evaluate_in_object:
* @context: a #JSCContext
* @code: a JavaScript script to evaluate
* @length: length of @code, or -1 if @code is a nul-terminated string
* @object_instance: (nullable): an object instance
* @object_class: (nullable): a #JSCClass or %NULL to use the default
* @uri: the source URI
* @line_number: the starting line number
* @object: (out) (transfer full): return location for a #JSCValue.
*
* Evaluate @code and create an new object where symbols defined in @code will be added as properties,
* instead of being added to @context global object. The new object is returned as @object parameter.
* Similar to how jsc_value_new_object() works, if @object_instance is not %NULL @object_class must be provided too.
* The @line_number is the starting line number in @uri; the value is one-based so the first line is 1.
* @uri and @line_number will be shown in exceptions and they don't affect the behavior of the script.
*
* Returns: (transfer full): a #JSCValue representing the last value generated by the script.
*/
JSCValue*
jsc_context_evaluate_in_object
(JSCContext* context,
const
char
* code, gssize length, gpointer instance, JSCClass* objectClass,
const
char
* uri,
unsigned
lineNumber, JSCValue** object)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
nullptr
);
g_return_val_if_fail
(code,
nullptr
);
g_return_val_if_fail
(!instance ||
JSC_IS_CLASS
(objectClass),
nullptr
);
g_return_val_if_fail
(object && !*object,
nullptr
);
JSRetainPtr<JSGlobalContextRef>
objectContext
(Adopt,
instance ?
jscClassCreateContextWithJSWrapper
(objectClass, context, instance) :
JSGlobalContextCreateInGroup
(
jscVirtualMachineGetContextGroup
(context->
priv
->
vm
.
get
()),
nullptr
));
JSC
::JSGlobalObject* globalObject =
toJS
(objectContext.
get
());
Ref vm = globalObject->
vm
();
JSC
::JSLockHolder
locker
(globalObject);
globalObject->
setGlobalScopeExtension
(
JSC::JSWithScope::create
(vm, globalObject, globalObject->
globalScope
(),
toJS
(
JSContextGetGlobalObject
(context->
priv
->
jsContext
.
get
()))));
JSValueRef exception =
nullptr
;
JSValueRef result =
evaluateScriptInContext
(objectContext.
get
(), length <
0
?
String::fromUTF8
(code) :
String::fromUTF8
(
unsafeMakeSpan
(code, length)), uri, lineNumber, &exception);
if
(
jscContextHandleExceptionIfNeeded
(context, exception))
return
jsc_value_new_undefined
(context);
*object =
jscContextGetOrCreateValue
(context,
JSContextGetGlobalObject
(objectContext.
get
())).
leakRef
();
return
jscContextGetOrCreateValue
(context, result).
leakRef
();
}
/*
*
* JSCCheckSyntaxMode:
* @JSC_CHECK_SYNTAX_MODE_SCRIPT: mode to check syntax of a script
* @JSC_CHECK_SYNTAX_MODE_MODULE: mode to check syntax of a module
*
* Enum values to specify a mode to check for syntax errors in jsc_context_check_syntax().
*/
/*
*
* JSCCheckSyntaxResult:
* @JSC_CHECK_SYNTAX_RESULT_SUCCESS: no errors
* @JSC_CHECK_SYNTAX_RESULT_RECOVERABLE_ERROR: recoverable syntax error
* @JSC_CHECK_SYNTAX_RESULT_IRRECOVERABLE_ERROR: irrecoverable syntax error
* @JSC_CHECK_SYNTAX_RESULT_UNTERMINATED_LITERAL_ERROR: unterminated literal error
* @JSC_CHECK_SYNTAX_RESULT_OUT_OF_MEMORY_ERROR: out of memory error
* @JSC_CHECK_SYNTAX_RESULT_STACK_OVERFLOW_ERROR: stack overflow error
*
* Enum values to specify the result of jsc_context_check_syntax().
*/
/*
*
* jsc_context_check_syntax:
* @context: a #JSCContext
* @code: a JavaScript script to check
* @length: length of @code, or -1 if @code is a nul-terminated string
* @mode: a #JSCCheckSyntaxMode
* @uri: the source URI
* @line_number: the starting line number
* @exception: (out) (optional) (transfer full): return location for a #JSCException, or %NULL to ignore
*
* Check the given @code in @context for syntax errors. The @line_number is the starting line number in @uri;
* the value is one-based so the first line is 1. @uri and @line_number are only used to fill the @exception.
* In case of errors @exception will be set to a new #JSCException with the details. You can pass %NULL to
* @exception to ignore the error details.
*
* Returns: a #JSCCheckSyntaxResult
*/
JSCCheckSyntaxResult
jsc_context_check_syntax
(JSCContext* context,
const
char
* code, gssize length, JSCCheckSyntaxMode mode,
const
char
* uri,
unsigned
lineNumber, JSCException **exception)
{
g_return_val_if_fail
(
JSC_IS_CONTEXT
(context),
JSC_CHECK_SYNTAX_RESULT_IRRECOVERABLE_ERROR
);
g_return_val_if_fail
(code,
JSC_CHECK_SYNTAX_RESULT_IRRECOVERABLE_ERROR
);
g_return_val_if_fail
(!exception || !*exception,
JSC_CHECK_SYNTAX_RESULT_IRRECOVERABLE_ERROR
);
lineNumber = std::max<
unsigned
>(
1
, lineNumber);
auto
* jsContext = context->
priv
->
jsContext
.
get
();
JSC
::JSGlobalObject* globalObject =
toJS
(jsContext);
Ref vm = globalObject->
vm
();
JSC
::JSLockHolder
locker
(vm);
URL
sourceURL = uri ?
URL
(
String::fromLatin1
(uri)) :
URL
();
JSC
::SourceCode source =
JSC::makeSource
(length <
0
?
String::fromUTF8
(code) :
String::fromUTF8
(
unsafeMakeSpan
(code, length)),
JSC
::SourceOrigin { sourceURL },
JSC
::SourceTaintedOrigin::Untainted,
sourceURL.
string
() ,
TextPosition
(
OrdinalNumber::fromOneBasedInt
(lineNumber),
OrdinalNumber
()));
bool
success =
false
;
JSC
::ParserError error;
switch
(mode) {
case
JSC_CHECK_SYNTAX_MODE_SCRIPT
:
success = !!
JSC
::parse<
JSC
::ProgramNode>(vm, source,
JSC::Identifier
(),
JSC
::ImplementationVisibility::Public,
JSC
::JSParserBuiltinMode::NotBuiltin,
JSC
::NoLexicallyScopedFeatures,
JSC
::JSParserScriptMode::Classic,
JSC
::SourceParseMode::ProgramMode,
JSC
::FunctionMode::None,
JSC
::SuperBinding::NotNeeded, error);
break
;
case
JSC_CHECK_SYNTAX_MODE_MODULE
:
success = !!
JSC
::parse<
JSC
::ModuleProgramNode>(vm, source,
JSC::Identifier
(),
JSC
::ImplementationVisibility::Public,
JSC
::JSParserBuiltinMode::NotBuiltin,
JSC
::StrictModeLexicallyScopedFeature,
JSC
::JSParserScriptMode::Module,
JSC
::SourceParseMode::ModuleAnalyzeMode,
JSC
::FunctionMode::None,
JSC
::SuperBinding::NotNeeded, error);
break
;
}
JSCCheckSyntaxResult result =
JSC_CHECK_SYNTAX_RESULT_SUCCESS
;
if
(success)
return
result;
switch
(error.
type
()) {
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL