-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuickJSValue.cs
More file actions
1049 lines (961 loc) · 38.5 KB
/
QuickJSValue.cs
File metadata and controls
1049 lines (961 loc) · 38.5 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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using QuickJS.Native;
using static QuickJS.Native.QuickJSNativeApi;
namespace QuickJS
{
/// <summary>
/// Wraps a <see cref="JSValue"/> with a reference count.
/// </summary>
public class QuickJSValue : IDisposable
{
private delegate void CreateVoidDelegate();
private QuickJSRefcounted _refcounted;
/// <summary>
/// A read-only field that represents the JavaScript undefined value.
/// </summary>
public static readonly object Undefined = QuickJSUndefined.Value;
/// <summary>
/// Wraps a <see cref="JSValue"/> with a reference count.
/// </summary>
/// <param name="context">
/// The context in which the <paramref name="value"/> was created.
/// </param>
/// <param name="value">A <see cref="JSValue"/> with a reference count.</param>
/// <returns>
/// An instance of the <see cref="QuickJSValue"/> that is the wrapper
/// for the specified <paramref name="value"/>.
/// </returns>
public static QuickJSValue Wrap(QuickJSContext context, JSValue value)
{
return new QuickJSValue(context, value);
}
/// <summary>
/// Creates a new JavaScript object.
/// </summary>
/// <param name="context">The context in which to create the new object.</param>
/// <returns>The new JavaScript object.</returns>
/// <exception cref="QuickJSException">Cannot create a new object.</exception>
public static QuickJSValue Create(QuickJSContext context)
{
if (context is null)
throw new ArgumentOutOfRangeException(nameof(context));
return new QuickJSValue(context, JSValue.CreateObject(context.NativeInstance));
}
/// <summary>
/// Creates a new JavaScript object.
/// </summary>
/// <param name="context">The context in which to create the new object.</param>
/// <param name="classId">The class ID.</param>
/// <returns>The new JavaScript object.</returns>
/// <exception cref="QuickJSException">Cannot create a new object.</exception>
public static QuickJSValue Create(QuickJSContext context, JSClassID classId)
{
if (context is null)
throw new ArgumentOutOfRangeException(nameof(context));
if (!context.Runtime.IsRegisteredClass(classId))
throw new ArgumentOutOfRangeException(nameof(classId));
return new QuickJSValue(context, JSValue.CreateObject(context.NativeInstance, classId));
}
/// <summary>
/// Creates a new JavaScript Array object.
/// </summary>
/// <param name="context">The context in which to create the new Array object.</param>
/// <returns>The new JavaScript Array object.</returns>
/// <exception cref="QuickJSException">Cannot create a new array.</exception>
public static QuickJSValue CreateArray(QuickJSContext context)
{
if (context is null)
throw new ArgumentOutOfRangeException(nameof(context));
return new QuickJSValue(context, JSValue.CreateArray(context.NativeInstance));
}
/// <summary>
/// Parses the specified JSON string, constructing the JavaScript value or object described by the string.
/// </summary>
/// <param name="context">A pointer to the context in which to create the new object.</param>
/// <param name="json">The string to parse as JSON.</param>
/// <param name="filename">The name of the JSON file.</param>
/// <returns>The <see cref="QuickJSValue"/> corresponding to the given JSON text.</returns>
public static QuickJSValue FromJSON(QuickJSContext context, string json, string filename)
{
if (json == null)
return null;
return new QuickJSValue(context, JSValue.FromJSON(context.NativeInstance, json, filename));
}
private QuickJSValue(QuickJSContext context, JSValue value)
{
if (context is null)
throw new ArgumentOutOfRangeException(nameof(context));
if (value.Tag != JSTag.Object)
throw new ArgumentOutOfRangeException(nameof(value));
_refcounted = new QuickJSRefcounted(context, value);
context.AddValue(_refcounted);
}
~QuickJSValue()
{
Dispose(false);
}
/// <summary>
/// Determines whether a <paramref name="value"/> is undefined or not.
/// </summary>
/// <param name="value">The value to be tested.</param>
/// <returns>true if the given value is undefined; otherwise, false.</returns>
public static bool IsUndefined(object value)
{
return value is QuickJSUndefined;
}
/// <summary>
/// Gets tag of this value.
/// </summary>
public JSTag Tag
{
get { return this.NativeInstance.Tag; }
}
/// <summary>
/// Gets a value indicating whether this object is a function object.
/// </summary>
public bool IsFunction
{
get
{
return JS_IsFunction(Context.NativeInstance, this.NativeInstance);
}
}
/// <summary>
/// Gets a value indicating whether this object is an array object.
/// </summary>
public bool IsArray
{
get
{
return JS_IsArray(Context.NativeInstance, this.NativeInstance) == 1;
}
}
/// <summary>
/// Gets a <see cref="JSValue"/> referenced by this instance.
/// </summary>
public JSValue NativeInstance
{
get
{
if (_refcounted is null)
throw new ObjectDisposedException(this.GetType().Name);
return _refcounted.NativeValue;
}
}
/// <summary>
/// Increments the reference count and returns a <see cref="JSValue"/>.
/// </summary>
/// <returns><see cref="JSValue"/> referenced by this instance.</returns>
public JSValue GetNativeInstance()
{
return JS_DupValue(Context.NativeInstance, this.NativeInstance);
}
internal QuickJSContext Context
{
get
{
if (_refcounted is null)
throw new ObjectDisposedException(this.GetType().Name);
return _refcounted.Context;
}
}
internal QuickJSRefcounted GetRefcounted()
{
return _refcounted;
}
/// <summary>
/// Creates a native function and assigns it as a property to this JS object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="func">The function associated with the property.</param>
/// <param name="argCount">The number of arguments the function expects to receive.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public unsafe bool DefineFunction(string name, JSCFunction func, int argCount, JSPropertyFlags flags)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return DefinePropertyInternal(aName, Context.CreateFunctionRawInternal(aName, func, argCount), flags);
}
}
/// <summary>
/// Creates a native function and assigns it as a property to this JS object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="func">The function associated with the property.</param>
/// <param name="argCount">The number of arguments the function expects to receive.</param>
/// <param name="magic">A magic value.</param>
/// <param name="data">An array containing data to be used by the method.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public unsafe bool DefineFunction(string name, JSCFunctionData func, int argCount, int magic, JSValue[] data, JSPropertyFlags flags)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return DefinePropertyInternal(aName, Context.CreateFunctionRaw(func, argCount, magic, data), flags);
}
}
/// <summary>
/// Creates a native function and assigns it as a property to this JS object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="func">The function associated with the property.</param>
/// <param name="argCount">The number of arguments the function expects to receive.</param>
/// <param name="magic">A magic value.</param>
/// <param name="data">An array containing data to be used by the method.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public unsafe bool DefineFunction(string name, JSCFunctionDataDelegate func, int argCount, int magic, JSValue[] data, JSPropertyFlags flags)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return DefinePropertyInternal(aName, Context.CreateFunctionRaw(func, argCount, magic, data), flags);
}
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public bool DefineProperty(string name, QuickJSValue value, JSPropertyFlags flags)
{
if (value is null)
{
return DefineProperty(name, JSValue.Null, flags);
}
else
{
if (!Context.IsCompatibleWith(value.Context))
throw new ArgumentOutOfRangeException(nameof(value));
return DefineProperty(name, value.GetNativeInstance(), flags);
}
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public bool DefineProperty(string name, int value, JSPropertyFlags flags)
{
return DefineProperty(name, JS_NewInt32(Context.NativeInstance, value), flags);
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public bool DefineProperty(string name, long value, JSPropertyFlags flags)
{
return DefineProperty(name, JS_NewInt64(Context.NativeInstance, value), flags);
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public bool DefineProperty(string name, double value, JSPropertyFlags flags)
{
return DefineProperty(name, JS_NewFloat64(Context.NativeInstance, value), flags);
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public bool DefineProperty(string name, bool value, JSPropertyFlags flags)
{
return DefineProperty(name, JS_NewBool(Context.NativeInstance, value), flags);
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
public bool DefineProperty(string name, string value, JSPropertyFlags flags)
{
return DefineProperty(name, JSValue.Create(Context.NativeInstance, value), flags);
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="value">The value associated with the property.</param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
[MethodImpl(AggressiveInlining)]
public unsafe bool DefineProperty(string name, JSValue value, JSPropertyFlags flags)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return DefinePropertyInternal(aName, value, flags);
}
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="getter">
/// The property getter callback, which the JavaScript engine will call each time
/// the property's value is accessed; or null.
/// </param>
/// <param name="setter">
/// The property setter callback, which the JavaScript engine will call each time
/// the property is assigned; or null.
/// </param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
[MethodImpl(AggressiveInlining)]
public unsafe bool DefineProperty(string name, JSCFunction getter, JSCFunction setter, JSPropertyFlags flags)
{
JSValue getterVal, setterVal;
getterVal = getter is null ? JSValue.Undefined : Context.CreateFunctionRaw("get_" + name, getter, 0);
try
{
setterVal = setter is null ? JSValue.Undefined : Context.CreateFunctionRaw("set_" + name, setter, 1);
}
catch
{
JS_FreeValue(Context.NativeInstance, getterVal);
throw;
}
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return DefinePropertyInternal(aName, getterVal, setterVal, flags);
}
}
/// <summary>
/// Defines a new property directly on an object, or modifies an existing property on an object.
/// </summary>
/// <param name="name">The name of the property to be defined or modified.</param>
/// <param name="getter">
/// A <see cref="JSValue"/> containing the property getter callback, which the JavaScript engine
/// will call each time the property's value is accessed; or <see cref="JSValue.Undefined"/>.
/// </param>
/// <param name="setter">
/// A <see cref="JSValue"/> containing the property setter callback, which the JavaScript engine
/// will call each time the property is assigned; or <see cref="JSValue.Undefined"/>.
/// </param>
/// <param name="flags">A bitwise combination of the <see cref="JSPropertyFlags"/>.</param>
/// <returns>true if the property has been defined or redefined; otherwise false.</returns>
[MethodImpl(AggressiveInlining)]
public unsafe bool DefineProperty(string name, JSValue getter, JSValue setter, JSPropertyFlags flags)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return DefinePropertyInternal(aName, getter, setter, flags);
}
}
private unsafe bool DefinePropertyInternal(byte* name, JSValue getter, JSValue setter, JSPropertyFlags flags)
{
JSContext context = Context.NativeInstance;
if (name == null)
{
JS_FreeValue(context, getter);
JS_FreeValue(context, setter);
throw new ArgumentNullException(nameof(name));
}
JSAtom prop = JS_NewAtom(context, name);
int rv = JS_DefinePropertyGetSet(context, this.NativeInstance, prop, getter, setter, flags & JSPropertyFlags.CWE);
JS_FreeAtom(context, prop);
if (rv == -1)
context.ThrowPendingException();
return rv == 1;
}
private unsafe bool DefinePropertyInternal(byte* name, JSValue value, JSPropertyFlags flags)
{
if (name == null)
{
JS_FreeValue(Context.NativeInstance, value);
throw new ArgumentNullException(nameof(name));
}
int rv = JS_DefinePropertyValueStr(Context.NativeInstance, this.NativeInstance, name, value, flags & JSPropertyFlags.CWE);
if (rv == -1)
Context.NativeInstance.ThrowPendingException();
return rv == 1;
}
/// <summary>
/// Assigns an <see cref="Int32"/> value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public unsafe bool SetProperty(string name, int value)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return SetPropertyInternal(aName, JS_NewInt32(Context.NativeInstance, value));
}
}
/// <summary>
/// Assigns an <see cref="Int64"/> value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public unsafe bool SetProperty(string name, long value)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return SetPropertyInternal(aName, JS_NewInt64(Context.NativeInstance, value));
}
}
/// <summary>
/// Assigns a <see cref="Double"/> value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public unsafe bool SetProperty(string name, double value)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return SetPropertyInternal(aName, JS_NewFloat64(Context.NativeInstance, value));
}
}
/// <summary>
/// Assigns a <see cref="Boolean"/> value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public unsafe bool SetProperty(string name, bool value)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return SetPropertyInternal(aName, JS_NewBool(Context.NativeInstance, value));
}
}
/// <summary>
/// Assigns a <see cref="QuickJSValue"/> value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(string name, QuickJSValue value)
{
if (value is null)
{
return SetProperty(name, JSValue.Null);
}
else
{
if (!Context.IsCompatibleWith(value.Context))
throw new ArgumentOutOfRangeException(nameof(value));
return SetProperty(name, value.GetNativeInstance());
}
}
/// <summary>
/// Assigns a <see cref="QuickJSValue"/> value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(string name, string value)
{
return SetProperty(name, JSValue.Create(Context.NativeInstance, value));
}
/// <summary>
/// Assigns a value to a property of an object.
/// </summary>
/// <param name="name">Name of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public unsafe bool SetProperty(string name, JSValue value)
{
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return SetPropertyInternal(aName, value);
}
}
private unsafe bool SetPropertyInternal(byte* name, JSValue value)
{
if (name == null)
{
JS_FreeValue(Context.NativeInstance, value);
throw new ArgumentNullException(nameof(name));
}
int rv = JS_SetPropertyStr(Context.NativeInstance, this.NativeInstance, name, value);
if (rv == -1)
Context.NativeInstance.ThrowPendingException();
return rv == 1;
}
/// <summary>
/// Assigns an <see cref="Int32"/> value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, int value)
{
return SetProperty(index, JS_NewInt32(Context.NativeInstance, value));
}
/// <summary>
/// Assigns an <see cref="Int64"/> value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, long value)
{
return SetProperty(index, JS_NewInt64(Context.NativeInstance, value));
}
/// <summary>
/// Assigns a <see cref="Double"/> value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, double value)
{
return SetProperty(index, JS_NewFloat64(Context.NativeInstance, value));
}
/// <summary>
/// Assigns a <see cref="Boolean"/> value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, bool value)
{
return SetProperty(index, JS_NewBool(Context.NativeInstance, value));
}
/// <summary>
/// Assigns a <see cref="QuickJSValue"/> value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, QuickJSValue value)
{
if (value is null)
{
return SetProperty(index, JSValue.Null);
}
else
{
if (!Context.IsCompatibleWith(value.Context))
throw new ArgumentOutOfRangeException(nameof(value));
return SetProperty(index, value.GetNativeInstance());
}
}
/// <summary>
/// Assigns a <see cref="QuickJSValue"/> value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, string value)
{
return SetProperty(index, JSValue.Create(Context.NativeInstance, value));
}
/// <summary>
/// Assigns a value to a property of an object.
/// </summary>
/// <param name="index">The index of the property to set.</param>
/// <param name="value">The value to assign to the property.</param>
/// <returns>On success, returns true; otherwise, false.</returns>
public bool SetProperty(int index, JSValue value)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
int rv = JS_SetPropertyUint32(Context.NativeInstance, this.NativeInstance, unchecked((uint)index), value);
if (rv == -1)
Context.NativeInstance.ThrowPendingException();
return rv == 1;
}
/// <summary>
/// Searches for the property with the specified name.
/// </summary>
/// <param name="name">The string containing the name of the property to get.</param>
/// <returns>
/// An object representing the property with the specified name, if found;
/// otherwise, <see cref="Undefined"/>.
/// </returns>
public unsafe object GetProperty(string name)
{
if (name is null)
throw new ArgumentNullException(nameof(name));
fixed (byte* aName = Utils.StringToManagedUTF8(name))
{
return Context.ConvertJSValueToClrObject(JS_GetPropertyStr(Context.NativeInstance, this.NativeInstance, aName), true);
}
}
/// <summary>
/// Searches for the property with the specified index.
/// </summary>
/// <param name="index">The index of the property.</param>
/// <returns>
/// An object representing the property with the specified name, if found;
/// otherwise, <see cref="Undefined"/>.
/// </returns>
public object GetProperty(int index)
{
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index));
return Context.ConvertJSValueToClrObject(JS_GetPropertyUint32(Context.NativeInstance, this.NativeInstance, unchecked((uint)index)), true);
}
/// <summary>
/// Gets or sets the property with the specified name.
/// </summary>
/// <param name="name">The string containing the name of the property.</param>
/// <returns>
/// An object representing the property with the specified name, if found;
/// otherwise, <see cref="Undefined"/>.
/// </returns>
public object this[string name]
{
get
{
return GetProperty(name);
}
set
{
SetProperty(name, Context.ConvertClrObjectToJSValue(value));
}
}
/// <summary>
/// Gets or sets the property with the specified name.
/// </summary>
/// <param name="index">The index of the property.</param>
/// <returns>
/// An object representing the property with the specified name, if found;
/// otherwise, <see cref="Undefined"/>.
/// </returns>
public object this[int index]
{
get
{
return GetProperty(index);
}
set
{
SetProperty(index, Context.ConvertClrObjectToJSValue(value));
}
}
/// <summary>
/// Invokes the function represented by the current instance on the global object.
/// </summary>
/// <returns>An object containing the return value of the invoked function.</returns>
public object Call()
{
JSValue value = JS_GetGlobalObject(Context.NativeInstance);
if (JS_IsException(value))
Context.NativeInstance.ThrowPendingException();
try
{
return Call(value);
}
finally
{
JS_FreeValue(Context.NativeInstance, value);
}
}
/// <summary>
/// Invokes the function represented by the current instance.
/// </summary>
/// <param name="thisArg">
/// The object on which to invoke the function; is 'this'
/// when the function executes.
/// </param>
/// <returns>
/// An object containing the return value of the invoked function.
/// </returns>
public object Call(QuickJSValue thisArg)
{
return Call(thisArg is null ? JSValue.Null : thisArg.NativeInstance);
}
/// <summary>
/// Invokes the function represented by the current instance.
/// </summary>
/// <param name="thisArg">
/// The object on which to invoke the function; is 'this'
/// when the function executes.
/// </param>
/// <param name="args">
/// The array of argument values to pass to the function.
/// </param>
/// <returns>
/// An object containing the return value of the invoked function.
/// </returns>
public object Call(QuickJSValue thisArg, params JSValue[] args)
{
return Call(thisArg is null ? JSValue.Null : thisArg.NativeInstance, args);
}
/// <summary>
/// Invokes the function represented by the current instance.
/// </summary>
/// <param name="thisArg">
/// The object on which to invoke the function; is 'this'
/// when the function executes.
/// </param>
/// <returns>
/// An object containing the return value of the invoked function.
/// </returns>
/// <exception cref="QuickJSException">An exception has occurred.</exception>
public unsafe object Call(JSValue thisArg)
{
JSValue value = JS_Call(Context.NativeInstance, this.NativeInstance, thisArg, 0, default(JSValue*));
if (JS_IsException(value))
Context.ThrowPendingException();
return Context.ConvertJSValueToClrObject(value, true);
}
/// <summary>
/// Invokes the function represented by the current instance.
/// </summary>
/// <param name="thisArg">
/// The object on which to invoke the function; is 'this'
/// when the function executes.
/// </param>
/// <param name="args">
/// The array of argument values to pass to the function.
/// </param>
/// <returns>
/// An object containing the return value of the invoked function.
/// </returns>
/// <exception cref="QuickJSException">An exception has occurred.</exception>
public object Call(JSValue thisArg, params JSValue[] args)
{
if (args is null)
return Call(thisArg);
JSValue value = JS_Call(Context.NativeInstance, this.NativeInstance, thisArg, args.Length, args);
if (JS_IsException(value))
Context.ThrowPendingException();
return Context.ConvertJSValueToClrObject(value, true);
}
/// <summary>
/// Create an object as though by using the new keyword and
/// the constructor represented by the current instance.
/// </summary>
/// <param name="args">
/// The array of argument values to pass to the constructor.
/// </param>
/// <returns>The new object that this constructor creates.</returns>
/// <exception cref="QuickJSException">An exception has occurred.</exception>
public QuickJSValue CallConstructor(params JSValue[] args)
{
JSValue value = JS_CallConstructor(Context.NativeInstance, this.NativeInstance, args != null ? args.Length : 0, args);
if (JS_IsException(value))
Context.ThrowPendingException();
return (QuickJSValue)Context.ConvertJSValueToClrObject(value, true);
}
/// <summary>
/// Converts a JavaScript object or value to a JSON string, optionally replacing values if
/// a <paramref name="replacer"/> function is specified or optionally including only
/// the specified properties if a <paramref name="replacer"/> array is specified.
/// </summary>
/// <param name="replacer">
/// A function that alters the behavior of the stringification process, or an array of String
/// and Number that serve as a whitelist for selecting/filtering the properties of the value
/// object to be included in the JSON string.<para/>
/// If this value is null or not provided, all properties of the object are included in the
/// resulting JSON string.
/// </param>
/// <param name="space">
/// The string (or the first 10 characters of the string, if it's longer than that)
/// is used as white space. If this parameter is null, no white space is used.
/// </param>
/// <param name="json">
/// When the method returns, the JSON string representing this <see cref="JSValue"/>.
/// </param>
/// <returns>true if the operation is successful; otherwise, false.</returns>
public bool TryGetJSON(QuickJSValue replacer, string space, out string json)
{
if (replacer is null)
return NativeInstance.TryGetJSON(Context.NativeInstance, JSValue.Undefined, space, out json);
if (!Context.IsCompatibleWith(replacer.Context))
throw new ArgumentOutOfRangeException(nameof(replacer));
return NativeInstance.TryGetJSON(Context.NativeInstance, replacer.NativeInstance, space, out json);
}
/// <summary>
/// Converts a JavaScript object or value to a JSON string, optionally replacing values if
/// a <paramref name="replacer"/> function is specified or optionally including only
/// the specified properties if a <paramref name="replacer"/> array is specified.
/// </summary>
/// <param name="replacer">
/// A function that alters the behavior of the stringification process, or an array of String
/// and Number that serve as a whitelist for selecting/filtering the properties of the value
/// object to be included in the JSON string.<para/>
/// If this value is null or not provided, all properties of the object are included in the
/// resulting JSON string.
/// </param>
/// <param name="space">
/// Indicates the number of space characters to use as white space;
/// this number is capped at 10 (if it is greater, the value is just 10).
/// Values less than 1 indicate that no space should be used.
/// </param>
/// <param name="json">
/// When the method returns, the JSON string representing this <see cref="JSValue"/>.
/// </param>
/// <returns>true if the operation is successful; otherwise, false.</returns>
public bool TryGetJSON(QuickJSValue replacer, int space, out string json)
{
if (replacer is null)
return NativeInstance.TryGetJSON(Context.NativeInstance, JSValue.Undefined, space, out json);
if (!Context.IsCompatibleWith(replacer.Context))
throw new ArgumentOutOfRangeException(nameof(replacer));
return NativeInstance.TryGetJSON(Context.NativeInstance, replacer.NativeInstance, space, out json);
}
/// <summary>
/// Converts a JavaScript object or value to a JSON string.
/// </summary>
/// <param name="json">
/// When the method returns, the JSON string representing this <see cref="JSValue"/>.
/// </param>
/// <returns>true if the operation is successful; otherwise, false.</returns>
public bool TryGetJSON(out string json)
{
return NativeInstance.TryGetJSON(Context.NativeInstance, out json);
}
/// <summary>
/// Converts a JavaScript object or value to a JSON string, optionally replacing values if
/// a <paramref name="replacer"/> function is specified or optionally including only
/// the specified properties if a <paramref name="replacer"/> array is specified.
/// </summary>
/// <param name="replacer">
/// A function that alters the behavior of the stringification process, or an array of String
/// and Number that serve as a whitelist for selecting/filtering the properties of the value
/// object to be included in the JSON string.<para/>
/// If this value is null or not provided, all properties of the object are included in the
/// resulting JSON string.
/// </param>
/// <param name="space">
/// The string (or the first 10 characters of the string, if it's longer than that)
/// is used as white space. If this parameter is null, no white space is used.
/// </param>
/// <returns>The JSON string representing this <see cref="JSValue"/>.</returns>
public string ToJSON(QuickJSValue replacer, string space)
{
if (replacer is null)
return NativeInstance.ToJSON(Context.NativeInstance, JSValue.Undefined, space);
if (!Context.IsCompatibleWith(replacer.Context))
throw new ArgumentOutOfRangeException(nameof(replacer));
return NativeInstance.ToJSON(Context.NativeInstance, replacer.NativeInstance, space);
}
/// <summary>
/// Converts a JavaScript object or value to a JSON string, optionally replacing values if
/// a <paramref name="replacer"/> function is specified or optionally including only
/// the specified properties if a <paramref name="replacer"/> array is specified.
/// </summary>
/// <param name="replacer">
/// A function that alters the behavior of the stringification process, or an array of String
/// and Number that serve as a whitelist for selecting/filtering the properties of the value
/// object to be included in the JSON string.<para/>
/// If this value is null or not provided, all properties of the object are included in the
/// resulting JSON string.
/// </param>
/// <param name="space">
/// Indicates the number of space characters to use as white space;
/// this number is capped at 10 (if it is greater, the value is just 10).
/// Values less than 1 indicate that no space should be used.
/// </param>
/// <returns>The JSON string representing this <see cref="JSValue"/>.</returns>
public string ToJSON(QuickJSValue replacer, int space)
{
if (replacer is null)
return NativeInstance.ToJSON(Context.NativeInstance, JSValue.Undefined, space);
if (!Context.IsCompatibleWith(replacer.Context))
throw new ArgumentOutOfRangeException(nameof(replacer));
return NativeInstance.ToJSON(Context.NativeInstance, replacer.NativeInstance, space);
}
/// <summary>
/// Converts a JavaScript object or value to a JSON string.
/// </summary>
/// <returns>The JSON string representing this <see cref="JSValue"/>.</returns>
public string ToJSON()
{
return NativeInstance.ToJSON(Context.NativeInstance);
}
/// <summary>
/// Gets an array of all properties found directly in this object.
/// </summary>
/// <param name="flags">
/// A bitwise combination of <see cref="JSGetPropertyNamesFlags"/> values.
/// </param>
/// <returns>An array of strings that corresponds to the properties found directly in this object.</returns>
/// <exception cref="QuickJSException">An exception occurred.</exception>
public string[] GetOwnPropertyNames(JSGetPropertyNamesFlags flags)
{
JSContext ctx = Context.NativeInstance;
JSPropertyEnum[] props = null;
try
{
if (0 != JS_GetOwnPropertyNames(ctx, out props, this.NativeInstance, flags))
Context.ThrowPendingException();
if (props == null)
return null;
var names = new string[props.Length];
for (int i = 0; i < names.Length; i++)
{
IntPtr str = JS_AtomToCString(ctx, props[i].atom);
if (str == IntPtr.Zero)
continue;
try
{
names[i] = Utils.PtrToStringUTF8(str);
}
finally
{
JS_FreeCString(ctx, str);