forked from WebKit/WebKit-http
-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathJSCValue.cpp
More file actions
2204 lines (1937 loc) · 82.4 KB
/
JSCValue.cpp
File metadata and controls
2204 lines (1937 loc) · 82.4 KB
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.
*
* 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 "JSCValue.h"
#include "APICast.h"
#include "APIUtils.h"
#include "JSArrayBuffer.h"
#include "JSCCallbackFunction.h"
#include "JSCClassPrivate.h"
#include "JSCContextPrivate.h"
#include "JSCInlines.h"
#include "JSCValuePrivate.h"
#include "JSRetainPtr.h"
#include "JSTypedArray.h"
#include "LiteralParser.h"
#include "OpaqueJSString.h"
#include "TypedArrayType.h"
#include <array>
#include <gobject/gvaluecollector.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/MakeString.h>
/**
* JSCValue:
* @short_description: JavaScript value
* @title: JSCValue
* @see_also: JSCContext
*
* JSCValue represents a reference to a value in a #JSCContext. The JSCValue
* protects the referenced value from being garbage collected.
*/
enum {
PROP_0,
PROP_CONTEXT,
};
struct _JSCValuePrivate {
GRefPtr<JSCContext> context;
JSValueRef jsValue;
};
WEBKIT_DEFINE_FINAL_TYPE(JSCValue, jsc_value, G_TYPE_OBJECT, GObject)
static void jscValueGetProperty(GObject* object, guint propID, GValue* value, GParamSpec* paramSpec)
{
JSCValuePrivate* priv = JSC_VALUE(object)->priv;
switch (propID) {
case PROP_CONTEXT:
g_value_set_object(value, priv->context.get());
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void jscValueSetProperty(GObject* object, guint propID, const GValue* value, GParamSpec* paramSpec)
{
JSCValuePrivate* priv = JSC_VALUE(object)->priv;
switch (propID) {
case PROP_CONTEXT:
priv->context = JSC_CONTEXT(g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propID, paramSpec);
}
}
static void jscValueDispose(GObject* object)
{
JSCValuePrivate* priv = JSC_VALUE(object)->priv;
if (priv->context) {
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueUnprotect(jsContext, priv->jsValue);
jscContextValueDestroyed(priv->context.get(), priv->jsValue);
priv->jsValue = nullptr;
priv->context = nullptr;
}
G_OBJECT_CLASS(jsc_value_parent_class)->dispose(object);
}
static void jsc_value_class_init(JSCValueClass* klass)
{
GObjectClass* objClass = G_OBJECT_CLASS(klass);
objClass->get_property = jscValueGetProperty;
objClass->set_property = jscValueSetProperty;
objClass->dispose = jscValueDispose;
/**
* JSCValue:context:
*
* The #JSCContext in which the value was created.
*/
g_object_class_install_property(objClass,
PROP_CONTEXT,
g_param_spec_object(
"context",
nullptr, nullptr,
JSC_TYPE_CONTEXT,
static_cast<GParamFlags>(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
}
JSValueRef jscValueGetJSValue(JSCValue* value)
{
return value->priv->jsValue;
}
JSCValue* jscValueCreate(JSCContext* context, JSValueRef jsValue)
{
auto* value = JSC_VALUE(g_object_new(JSC_TYPE_VALUE, "context", context, nullptr));
JSValueProtect(jscContextGetJSContext(context), jsValue);
value->priv->jsValue = jsValue;
return value;
}
/**
* jsc_value_get_context:
* @value: a #JSCValue
*
* Get the #JSCContext in which @value was created.
*
* Returns: (transfer none): the #JSCValue context.
*/
JSCContext* jsc_value_get_context(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
return value->priv->context.get();
}
/**
* jsc_value_new_undefined:
* @context: a #JSCContext
*
* Create a new #JSCValue referencing <function>undefined</function> in @context.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_undefined(JSCContext* context)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
return jscContextGetOrCreateValue(context, JSValueMakeUndefined(jscContextGetJSContext(context))).leakRef();
}
/**
* jsc_value_is_undefined:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is <function>undefined</function>.
*
* Returns: whether the value is undefined.
*/
gboolean jsc_value_is_undefined(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsUndefined(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_new_null:
* @context: a #JSCContext
*
* Create a new #JSCValue referencing <function>null</function> in @context.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_null(JSCContext* context)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
return jscContextGetOrCreateValue(context, JSValueMakeNull(jscContextGetJSContext(context))).leakRef();
}
/**
* jsc_value_is_null:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is <function>null</function>.
*
* Returns: whether the value is null.
*/
gboolean jsc_value_is_null(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsNull(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_new_number:
* @context: a #JSCContext
* @number: a number
*
* Create a new #JSCValue from @number.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_number(JSCContext* context, double number)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
return jscContextGetOrCreateValue(context, JSValueMakeNumber(jscContextGetJSContext(context), number)).leakRef();
}
/**
* jsc_value_is_number:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is a number.
*
* Returns: whether the value is a number.
*/
gboolean jsc_value_is_number(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsNumber(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_to_double:
* @value: a #JSCValue
*
* Convert @value to a double.
*
* Returns: a #gdouble result of the conversion.
*/
double jsc_value_to_double(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), std::numeric_limits<double>::quiet_NaN());
JSCValuePrivate* priv = value->priv;
JSValueRef exception = nullptr;
auto result = JSValueToNumber(jscContextGetJSContext(priv->context.get()), priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return std::numeric_limits<double>::quiet_NaN();
return result;
}
/**
* jsc_value_to_int32:
* @value: a #JSCValue
*
* Convert @value to a #gint32.
*
* Returns: a #gint32 result of the conversion.
*/
gint32 jsc_value_to_int32(JSCValue* value)
{
return JSC::toInt32(jsc_value_to_double(value));
}
/**
* jsc_value_new_boolean:
* @context: a #JSCContext
* @value: a #gboolean
*
* Create a new #JSCValue from @value
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_boolean(JSCContext* context, gboolean value)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
return jscContextGetOrCreateValue(context, JSValueMakeBoolean(jscContextGetJSContext(context), value)).leakRef();
}
/**
* jsc_value_is_boolean:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is a boolean.
*
* Returns: whether the value is a boolean.
*/
gboolean jsc_value_is_boolean(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsBoolean(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_to_boolean:
* @value: a #JSCValue
*
* Convert @value to a boolean.
*
* Returns: a #gboolean result of the conversion.
*/
gboolean jsc_value_to_boolean(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueToBoolean(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_new_string:
* @context: a #JSCContext
* @string: (nullable): a null-terminated string
*
* Create a new #JSCValue from @string. If you need to create a #JSCValue from a
* string containing null characters, use jsc_value_new_string_from_bytes() instead.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_string(JSCContext* context, const char* string)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
JSValueRef jsStringValue;
if (string) {
JSRetainPtr<JSStringRef> jsString(Adopt, JSStringCreateWithUTF8CString(string));
jsStringValue = JSValueMakeString(jscContextGetJSContext(context), jsString.get());
} else
jsStringValue = JSValueMakeString(jscContextGetJSContext(context), nullptr);
return jscContextGetOrCreateValue(context, jsStringValue).leakRef();
}
/**
* jsc_value_new_string_from_bytes:
* @context: a #JSCContext
* @bytes: (nullable): a #GBytes
*
* Create a new #JSCValue from @bytes.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_string_from_bytes(JSCContext* context, GBytes* bytes)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
if (!bytes)
return jsc_value_new_string(context, nullptr);
gsize dataSize;
const auto* data = static_cast<const char*>(g_bytes_get_data(bytes, &dataSize));
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib port
auto string = String::fromUTF8(std::span(data, dataSize));
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
JSRetainPtr<JSStringRef> jsString(Adopt, OpaqueJSString::tryCreate(WTFMove(string)).leakRef());
return jscContextGetOrCreateValue(context, JSValueMakeString(jscContextGetJSContext(context), jsString.get())).leakRef();
}
/**
* jsc_value_is_string:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is a string
*
* Returns: whether the value is a string
*/
gboolean jsc_value_is_string(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsString(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_to_string:
* @value: a #JSCValue
*
* Convert @value to a string. Use jsc_value_to_string_as_bytes() instead, if you need to
* handle strings containing null characters.
*
* Returns: (transfer full): a null-terminated string result of the conversion.
*/
char* jsc_value_to_string(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
JSCValuePrivate* priv = value->priv;
JSValueRef exception = nullptr;
JSRetainPtr<JSStringRef> jsString(Adopt, JSValueToStringCopy(jscContextGetJSContext(priv->context.get()), priv->jsValue, &exception));
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return nullptr;
if (!jsString)
return nullptr;
size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString.get());
auto* string = static_cast<char*>(g_malloc(maxSize));
if (!JSStringGetUTF8CString(jsString.get(), string, maxSize)) {
g_free(string);
return nullptr;
}
return string;
}
/**
* jsc_value_to_string_as_bytes:
* @value: a #JSCValue
*
* Convert @value to a string and return the results as #GBytes. This is needed
* to handle strings with null characters.
*
* Returns: (transfer full): a #GBytes with the result of the conversion.
*/
GBytes* jsc_value_to_string_as_bytes(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
JSCValuePrivate* priv = value->priv;
JSValueRef exception = nullptr;
JSRetainPtr<JSStringRef> jsString(Adopt, JSValueToStringCopy(jscContextGetJSContext(priv->context.get()), priv->jsValue, &exception));
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return nullptr;
if (!jsString)
return nullptr;
size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString.get());
if (maxSize == 1)
return g_bytes_new_static("", 0);
auto* string = static_cast<char*>(fastMalloc(maxSize));
auto stringSize = JSStringGetUTF8CString(jsString.get(), string, maxSize);
if (!stringSize) {
fastFree(string);
return nullptr;
}
// Ignore the null character added by JSStringGetUTF8CString.
return g_bytes_new_with_free_func(string, stringSize - 1, fastFree, string);
}
/**
* jsc_value_new_array: (skip)
* @context: a #JSCContext
* @first_item_type: #GType of first item, or %G_TYPE_NONE
* @...: value of the first item, followed optionally by more type/value pairs, followed by %G_TYPE_NONE.
*
* Create a new #JSCValue referencing an array with the given items. If @first_item_type
* is %G_TYPE_NONE an empty array is created.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_array(JSCContext* context, GType firstItemType, ...)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
auto* jsContext = jscContextGetJSContext(context);
JSC::JSGlobalObject* globalObject = toJS(jsContext);
JSC::JSLockHolder locker(globalObject);
JSValueRef exception = nullptr;
auto* jsArray = JSObjectMakeArray(jsContext, 0, nullptr, &exception);
if (jscContextHandleExceptionIfNeeded(context, exception))
return nullptr;
auto* jsArrayObject = JSValueToObject(jsContext, jsArray, &exception);
if (jscContextHandleExceptionIfNeeded(context, exception))
return nullptr;
unsigned index = 0;
va_list args;
va_start(args, firstItemType);
GType itemType = firstItemType;
while (itemType != G_TYPE_NONE) {
GValue item;
GUniqueOutPtr<char> error;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib port
G_VALUE_COLLECT_INIT(&item, itemType, args, G_VALUE_NOCOPY_CONTENTS, &error.outPtr());
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
if (error) {
exception = toRef(JSC::createTypeError(globalObject, makeString("failed to collect array item: "_s, unsafeSpan(error.get()))));
jscContextHandleExceptionIfNeeded(context, exception);
jsArray = nullptr;
break;
}
auto* jsValue = jscContextGValueToJSValue(context, &item, &exception);
g_value_unset(&item);
if (jscContextHandleExceptionIfNeeded(context, exception)) {
jsArray = nullptr;
break;
}
JSObjectSetPropertyAtIndex(jsContext, jsArrayObject, index, jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(context, exception)) {
jsArray = nullptr;
break;
}
itemType = va_arg(args, GType);
index++;
}
va_end(args);
return jsArray ? jscContextGetOrCreateValue(context, jsArray).leakRef() : nullptr;
}
/**
* jsc_value_new_array_from_garray:
* @context: a #JSCContext
* @array: (nullable) (element-type JSCValue): a #GPtrArray
*
* Create a new #JSCValue referencing an array with the items from @array. If @array
* is %NULL or empty a new empty array will be created. Elements of @array should be
* pointers to a #JSCValue.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_array_from_garray(JSCContext* context, GPtrArray* gArray)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
if (!gArray || !gArray->len)
return jsc_value_new_array(context, G_TYPE_NONE);
JSValueRef exception = nullptr;
auto* jsArray = jscContextGArrayToJSArray(context, gArray, &exception);
if (jscContextHandleExceptionIfNeeded(context, exception))
return nullptr;
return jscContextGetOrCreateValue(context, jsArray).leakRef();
}
/**
* jsc_value_new_array_from_strv:
* @context: a #JSCContext
* @strv: (array zero-terminated=1) (element-type utf8): a %NULL-terminated array of strings
*
* Create a new #JSCValue referencing an array of strings with the items from @strv. If @array
* is %NULL or empty a new empty array will be created.
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_array_from_strv(JSCContext* context, const char* const* strv)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
auto strvLength = strv ? g_strv_length(const_cast<char**>(strv)) : 0;
if (!strvLength)
return jsc_value_new_array(context, G_TYPE_NONE);
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib port
GRefPtr<GPtrArray> gArray = adoptGRef(g_ptr_array_new_full(strvLength, g_object_unref));
for (unsigned i = 0; i < strvLength; i++)
g_ptr_array_add(gArray.get(), jsc_value_new_string(context, strv[i]));
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
return jsc_value_new_array_from_garray(context, gArray.get());
}
/**
* jsc_value_is_array:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is an array.
*
* Returns: whether the value is an array.
*/
gboolean jsc_value_is_array(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsArray(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_new_object:
* @context: a #JSCContext
* @instance: (nullable) (transfer full): an object instance or %NULL
* @jsc_class: (nullable): the #JSCClass of @instance
*
* Create a new #JSCValue from @instance. If @instance is %NULL a new empty object is created.
* 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().
*
* Returns: (transfer full): a #JSCValue.
*/
JSCValue* jsc_value_new_object(JSCContext* context, gpointer instance, JSCClass* jscClass)
{
g_return_val_if_fail(JSC_IS_CONTEXT(context), nullptr);
g_return_val_if_fail(!instance || JSC_IS_CLASS(jscClass), nullptr);
return jscContextGetOrCreateValue(context, instance ? toRef(jscClassGetOrCreateJSWrapper(jscClass, context, instance)) : JSObjectMake(jscContextGetJSContext(context), nullptr, nullptr)).leakRef();
}
/**
* jsc_value_is_object:
* @value: a #JSCValue
*
* Get whether the value referenced by @value is an object.
*
* Returns: whether the value is an object.
*/
gboolean jsc_value_is_object(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
JSCValuePrivate* priv = value->priv;
return JSValueIsObject(jscContextGetJSContext(priv->context.get()), priv->jsValue);
}
/**
* jsc_value_object_is_instance_of:
* @value: a #JSCValue
* @name: a class name
*
* Get whether the value referenced by @value is an instance of class @name.
*
* Returns: whether the value is an object instance of class @name.
*/
gboolean jsc_value_object_is_instance_of(JSCValue* value, const char* name)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
g_return_val_if_fail(name, FALSE);
JSCValuePrivate* priv = value->priv;
// We use evaluate here and not get_value because classes are not necessarily a property of the global object.
// http://www.ecma-international.org/ecma-262/6.0/index.html#sec-global-environment-records
GRefPtr<JSCValue> constructor = adoptGRef(jsc_context_evaluate(priv->context.get(), name, -1));
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, constructor->priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return FALSE;
gboolean returnValue = JSValueIsInstanceOfConstructor(jsContext, priv->jsValue, object, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return FALSE;
return returnValue;
}
/**
* jsc_value_object_set_property:
* @value: a #JSCValue
* @name: the property name
* @property: the #JSCValue to set
*
* Set @property with @name on @value.
*/
void jsc_value_object_set_property(JSCValue* value, const char* name, JSCValue* property)
{
g_return_if_fail(JSC_IS_VALUE(value));
g_return_if_fail(name);
g_return_if_fail(JSC_IS_VALUE(property));
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return;
JSRetainPtr<JSStringRef> propertyName(Adopt, JSStringCreateWithUTF8CString(name));
JSObjectSetProperty(jsContext, object, propertyName.get(), property->priv->jsValue, kJSPropertyAttributeNone, &exception);
jscContextHandleExceptionIfNeeded(priv->context.get(), exception);
}
/**
* jsc_value_object_get_property:
* @value: a #JSCValue
* @name: the property name
*
* Get property with @name from @value.
*
* Returns: (transfer full): the property #JSCValue.
*/
JSCValue* jsc_value_object_get_property(JSCValue* value, const char* name)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
g_return_val_if_fail(name, nullptr);
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
JSRetainPtr<JSStringRef> propertyName(Adopt, JSStringCreateWithUTF8CString(name));
JSValueRef result = JSObjectGetProperty(jsContext, object, propertyName.get(), &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
return jscContextGetOrCreateValue(priv->context.get(), result).leakRef();
}
/**
* jsc_value_object_set_property_at_index:
* @value: a #JSCValue
* @index: the property index
* @property: the #JSCValue to set
*
* Set @property at @index on @value.
*/
void jsc_value_object_set_property_at_index(JSCValue* value, unsigned index, JSCValue* property)
{
g_return_if_fail(JSC_IS_VALUE(value));
g_return_if_fail(JSC_IS_VALUE(property));
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return;
JSObjectSetPropertyAtIndex(jsContext, object, index, property->priv->jsValue, &exception);
jscContextHandleExceptionIfNeeded(priv->context.get(), exception);
}
/**
* jsc_value_object_get_property_at_index:
* @value: a #JSCValue
* @index: the property index
*
* Get property at @index from @value.
*
* Returns: (transfer full): the property #JSCValue.
*/
JSCValue* jsc_value_object_get_property_at_index(JSCValue* value, unsigned index)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
JSValueRef result = JSObjectGetPropertyAtIndex(jsContext, object, index, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
return jscContextGetOrCreateValue(priv->context.get(), result).leakRef();
}
/**
* jsc_value_object_has_property:
* @value: a #JSCValue
* @name: the property name
*
* Get whether @value has property with @name.
*
* Returns: %TRUE if @value has a property with @name, or %FALSE otherwise
*/
gboolean jsc_value_object_has_property(JSCValue* value, const char* name)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
g_return_val_if_fail(name, FALSE);
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return FALSE;
JSRetainPtr<JSStringRef> propertyName(Adopt, JSStringCreateWithUTF8CString(name));
return JSObjectHasProperty(jsContext, object, propertyName.get());
}
/**
* jsc_value_object_delete_property:
* @value: a #JSCValue
* @name: the property name
*
* Try to delete property with @name from @value. This function will return %FALSE if
* the property was defined without %JSC_VALUE_PROPERTY_CONFIGURABLE flag.
*
* Returns: %TRUE if the property was deleted, or %FALSE otherwise.
*/
gboolean jsc_value_object_delete_property(JSCValue* value, const char* name)
{
g_return_val_if_fail(JSC_IS_VALUE(value), FALSE);
g_return_val_if_fail(name, FALSE);
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return FALSE;
JSRetainPtr<JSStringRef> propertyName(Adopt, JSStringCreateWithUTF8CString(name));
gboolean result = JSObjectDeleteProperty(jsContext, object, propertyName.get(), &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return FALSE;
return result;
}
/**
* jsc_value_object_enumerate_properties:
* @value: a #JSCValue
*
* Get the list of property names of @value. Only properties defined with %JSC_VALUE_PROPERTY_ENUMERABLE
* flag will be collected.
*
* Returns: (array zero-terminated=1) (transfer full) (nullable): a %NULL-terminated array of strings containing the
* property names, or %NULL if @value doesn't have enumerable properties. Use g_strfreev() to free.
*/
char** jsc_value_object_enumerate_properties(JSCValue* value)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return nullptr;
auto* propertiesArray = JSObjectCopyPropertyNames(jsContext, object);
if (!propertiesArray)
return nullptr;
auto propertiesArraySize = JSPropertyNameArrayGetCount(propertiesArray);
if (!propertiesArraySize) {
JSPropertyNameArrayRelease(propertiesArray);
return nullptr;
}
auto* result = static_cast<char**>(g_new0(char*, propertiesArraySize + 1));
for (unsigned i = 0; i < propertiesArraySize; ++i) {
RefPtr jsString = JSPropertyNameArrayGetNameAtIndex(propertiesArray, i);
size_t maxSize = JSStringGetMaximumUTF8CStringSize(jsString.get());
auto* string = static_cast<char*>(g_malloc(maxSize));
JSStringGetUTF8CString(jsString.get(), string, maxSize);
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib port
result[i] = string;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
}
JSPropertyNameArrayRelease(propertiesArray);
return result;
}
static JSValueRef jsObjectCall(JSGlobalContextRef jsContext, JSObjectRef function, JSC::JSCCallbackFunction::Type functionType, JSObjectRef thisObject, const Vector<JSValueRef>& arguments, JSValueRef* exception)
{
switch (functionType) {
case JSC::JSCCallbackFunction::Type::Constructor:
return JSObjectCallAsConstructor(jsContext, function, arguments.size(), arguments.data(), exception);
case JSC::JSCCallbackFunction::Type::Method:
ASSERT(thisObject);
FALLTHROUGH;
case JSC::JSCCallbackFunction::Type::Function:
return JSObjectCallAsFunction(jsContext, function, thisObject, arguments.size(), arguments.data(), exception);
}
RELEASE_ASSERT_NOT_REACHED();
}
static GRefPtr<JSCValue> jscValueCallFunction(JSCValue* value, JSObjectRef function, JSC::JSCCallbackFunction::Type functionType, JSObjectRef thisObject, GType firstParameterType, va_list args)
{
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSC::JSGlobalObject* globalObject = toJS(jsContext);
JSC::JSLockHolder locker(globalObject);
JSValueRef exception = nullptr;
Vector<JSValueRef> arguments;
GType parameterType = firstParameterType;
while (parameterType != G_TYPE_NONE) {
GValue parameter;
GUniqueOutPtr<char> error;
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib port
G_VALUE_COLLECT_INIT(¶meter, parameterType, args, G_VALUE_NOCOPY_CONTENTS, &error.outPtr());
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
if (error) {
exception = toRef(JSC::createTypeError(globalObject, makeString("failed to collect function paramater: "_s, unsafeSpan(error.get()))));
jscContextHandleExceptionIfNeeded(priv->context.get(), exception);
return adoptGRef(jsc_value_new_undefined(priv->context.get()));
}
auto* jsValue = jscContextGValueToJSValue(priv->context.get(), ¶meter, &exception);
g_value_unset(¶meter);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jscContextGetOrCreateValue(priv->context.get(), jsValue);
arguments.append(jsValue);
parameterType = va_arg(args, GType);
}
auto result = jsObjectCall(jsContext, function, functionType, thisObject, arguments, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return adoptGRef(jsc_value_new_undefined(priv->context.get()));
return jscContextGetOrCreateValue(priv->context.get(), result);
}
/**
* jsc_value_object_invoke_method: (skip)
* @value: a #JSCValue
* @name: the method name
* @first_parameter_type: #GType of first parameter, or %G_TYPE_NONE
* @...: value of the first parameter, followed optionally by more type/value pairs, followed by %G_TYPE_NONE
*
* Invoke method with @name on object referenced by @value, passing the given parameters. If
* @first_parameter_type is %G_TYPE_NONE no parameters will be passed to the method.
* The object instance will be handled automatically even when the method is a custom one
* registered with jsc_class_add_method(), so it should never be passed explicitly as parameter
* of this function.
*
* This function always returns a #JSCValue, in case of void methods a #JSCValue referencing
* <function>undefined</function> is returned.
*
* Returns: (transfer full): a #JSCValue with the return value of the method.
*/
JSCValue* jsc_value_object_invoke_method(JSCValue* value, const char* name, GType firstParameterType, ...)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
g_return_val_if_fail(name, nullptr);
JSCValuePrivate* priv = value->priv;
auto* jsContext = jscContextGetJSContext(priv->context.get());
JSValueRef exception = nullptr;
JSObjectRef object = JSValueToObject(jsContext, priv->jsValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
JSRetainPtr<JSStringRef> methodName(Adopt, JSStringCreateWithUTF8CString(name));
JSValueRef functionValue = JSObjectGetProperty(jsContext, object, methodName.get(), &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
JSObjectRef function = JSValueToObject(jsContext, functionValue, &exception);
if (jscContextHandleExceptionIfNeeded(priv->context.get(), exception))
return jsc_value_new_undefined(priv->context.get());
va_list args;
va_start(args, firstParameterType);
auto result = jscValueCallFunction(value, function, JSC::JSCCallbackFunction::Type::Method, object, firstParameterType, args);
va_end(args);
return result.leakRef();
}
/**
* jsc_value_object_invoke_methodv: (rename-to jsc_value_object_invoke_method)
* @value: a #JSCValue
* @name: the method name
* @n_parameters: the number of parameters
* @parameters: (nullable) (array length=n_parameters) (element-type JSCValue): the #JSCValue<!-- -->s to pass as parameters to the method, or %NULL
*
* Invoke method with @name on object referenced by @value, passing the given @parameters. If
* @n_parameters is 0 no parameters will be passed to the method.
* The object instance will be handled automatically even when the method is a custom one
* registered with jsc_class_add_method(), so it should never be passed explicitly as parameter
* of this function.
*
* This function always returns a #JSCValue, in case of void methods a #JSCValue referencing
* <function>undefined</function> is returned.
*
* Returns: (transfer full): a #JSCValue with the return value of the method.
*/
JSCValue* jsc_value_object_invoke_methodv(JSCValue* value, const char* name, unsigned parametersCount, JSCValue** parameters)
{
g_return_val_if_fail(JSC_IS_VALUE(value), nullptr);
g_return_val_if_fail(name, nullptr);
g_return_val_if_fail(!parametersCount || parameters, nullptr);
JSCValuePrivate* priv = value->priv;