forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsValue.cs
More file actions
965 lines (844 loc) · 25.7 KB
/
JsValue.cs
File metadata and controls
965 lines (844 loc) · 25.7 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
using System;
using System.Runtime.InteropServices;
using System.Text;
using JavaScriptEngineSwitcher.Core.Utilities;
namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
{
/// <summary>
/// The JavaScript value
/// </summary>
/// <remarks>
/// The JavaScript value is one of the following types of values: Undefined, Null, Boolean,
/// String, Number, or Object.
/// </remarks>
internal struct JsValue
{
/// <summary>
/// The reference
/// </summary>
private readonly IntPtr _reference;
/// <summary>
/// Gets a invalid value
/// </summary>
public static JsValue Invalid
{
get { return new JsValue(IntPtr.Zero); }
}
/// <summary>
/// Gets a value of <c>undefined</c> in the current script context
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public static JsValue Undefined
{
get
{
JsValue value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetUndefinedValue(out value));
return value;
}
}
/// <summary>
/// Gets a value of <c>null</c> in the current script context
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public static JsValue Null
{
get
{
JsValue value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetNullValue(out value));
return value;
}
}
/// <summary>
/// Gets a value of <c>true</c> in the current script context
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public static JsValue True
{
get
{
JsValue value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetTrueValue(out value));
return value;
}
}
/// <summary>
/// Gets a value of <c>false</c> in the current script context
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public static JsValue False
{
get
{
JsValue value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetFalseValue(out value));
return value;
}
}
/// <summary>
/// Gets a global object in the current script context
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public static JsValue GlobalObject
{
get
{
JsValue value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetGlobalObject(out value));
return value;
}
}
/// <summary>
/// Gets a value indicating whether the value is valid
/// </summary>
public bool IsValid
{
get { return _reference != IntPtr.Zero; }
}
/// <summary>
/// Gets a JavaScript type of the value
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The type of the value</returns>
public JsValueType ValueType
{
get
{
JsValueType type;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetValueType(this, out type));
return type;
}
}
/// <summary>
/// Gets a length of a <c>String</c> value
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The length of the string</returns>
public int StringLength
{
get
{
int length;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetStringLength(this, out length));
return length;
}
}
/// <summary>
/// Gets or sets a prototype of an object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public JsValue Prototype
{
get
{
JsValue prototypeReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetPrototype(this, out prototypeReference));
return prototypeReference;
}
set
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetPrototype(this, value));
}
}
/// <summary>
/// Gets a value indicating whether an object is extensible or not
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public bool IsExtensionAllowed
{
get
{
bool allowed;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetExtensionAllowed(this, out allowed));
return allowed;
}
}
/// <summary>
/// Gets a value indicating whether an object is an external object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public bool HasExternalData
{
get
{
bool hasExternalData;
JsErrorHelpers.ThrowIfError(NativeMethods.JsHasExternalData(this, out hasExternalData));
return hasExternalData;
}
}
/// <summary>
/// Gets or sets a data in an external object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
public IntPtr ExternalData
{
get
{
IntPtr data;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetExternalData(this, out data));
return data;
}
set
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetExternalData(this, value));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="JsValue"/> struct
/// </summary>
/// <param name="reference">The reference</param>
private JsValue(IntPtr reference)
{
_reference = reference;
}
/// <summary>
/// Creates a <c>Boolean</c> value from a <c>bool</c> value
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="value">The value to be converted</param>
/// <returns>The converted value</returns>
public static JsValue FromBoolean(bool value)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsBoolToBoolean(value, out reference));
return reference;
}
/// <summary>
/// Creates a <c>Number</c> value from a <c>double</c> value
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="value">The value to be converted</param>
/// <returns>The new <c>Number</c> value</returns>
public static JsValue FromDouble(double value)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsDoubleToNumber(value, out reference));
return reference;
}
/// <summary>
/// Creates a <c>Number</c> value from a <c>int</c> value
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="value">The value to be converted</param>
/// <returns>The new <c>Number</c> value</returns>
public static JsValue FromInt32(int value)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsIntToNumber(value, out reference));
return reference;
}
/// <summary>
/// Creates a <c>String</c> value from a string pointer
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="value">The string to convert to a <c>String</c> value</param>
/// <returns>The new <c>String</c> value</returns>
public static JsValue FromString(string value)
{
JsValue reference;
JsErrorCode errorCode;
if (Utils.IsWindows())
{
var stringLength = new UIntPtr((uint)value.Length);
errorCode = NativeMethods.JsPointerToString(value, stringLength, out reference);
}
else
{
var byteCount = new UIntPtr((uint)Encoding.GetEncoding(0).GetByteCount(value));
errorCode = NativeMethods.JsCreateString(value, byteCount, out reference);
}
JsErrorHelpers.ThrowIfError(errorCode);
return reference;
}
/// <summary>
/// Creates a new <c>Object</c>
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The new <c>Object</c></returns>
public static JsValue CreateObject()
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateObject(out reference));
return reference;
}
/// <summary>
/// Creates a new <c>Object</c> that stores some external data
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="data">External data that the object will represent. May be null</param>
/// <param name="finalizer">The callback for when the object is finalized. May be null.</param>
/// <returns>The new <c>Object</c></returns>
public static JsValue CreateExternalObject(IntPtr data, JsObjectFinalizeCallback finalizer)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateExternalObject(data, finalizer, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript function
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="function">The method to call when the function is invoked</param>
/// <returns>The new function object</returns>
public static JsValue CreateFunction(JsNativeFunction function)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateFunction(function, IntPtr.Zero, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript function
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="function">The method to call when the function is invoked</param>
/// <param name="callbackData">Data to be provided to all function callbacks</param>
/// <returns>The new function object</returns>
public static JsValue CreateFunction(JsNativeFunction function, IntPtr callbackData)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateFunction(function, callbackData, out reference));
return reference;
}
/// <summary>
/// Creates a JavaScript array object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="length">The initial length of the array</param>
/// <returns>The new array object</returns>
public static JsValue CreateArray(uint length)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateArray(length, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">Message for the error object</param>
/// <returns>The new error object</returns>
public static JsValue CreateError(JsValue message)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateError(message, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript RangeError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">Message for the error object</param>
/// <returns>The new error object</returns>
public static JsValue CreateRangeError(JsValue message)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateRangeError(message, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript ReferenceError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">Message for the error object</param>
/// <returns>The new error object</returns>
public static JsValue CreateReferenceError(JsValue message)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateReferenceError(message, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript SyntaxError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">Message for the error object</param>
/// <returns>The new error object</returns>
public static JsValue CreateSyntaxError(JsValue message)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateSyntaxError(message, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript TypeError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">Message for the error object</param>
/// <returns>The new error object</returns>
public static JsValue CreateTypeError(JsValue message)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateTypeError(message, out reference));
return reference;
}
/// <summary>
/// Creates a new JavaScript URIError error object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="message">Message for the error object</param>
/// <returns>The new error object</returns>
public static JsValue CreateUriError(JsValue message)
{
JsValue reference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateURIError(message, out reference));
return reference;
}
/// <summary>
/// Adds a reference to the object
/// </summary>
/// <remarks>
/// This only needs to be called on objects that are not going to be stored somewhere on
/// the stack. Calling AddRef ensures that the JavaScript object the value refers to will not be freed
/// until Release is called
/// </remarks>
/// <returns>The object's new reference count</returns>
public uint AddRef()
{
uint count;
JsErrorHelpers.ThrowIfError(NativeMethods.JsAddRef(this, out count));
return count;
}
/// <summary>
/// Releases a reference to the object
/// </summary>
/// <remarks>
/// Removes a reference that was created by AddRef.
/// </remarks>
/// <returns>The object's new reference count</returns>
public uint Release()
{
uint count;
JsErrorHelpers.ThrowIfError(NativeMethods.JsRelease(this, out count));
return count;
}
/// <summary>
/// Retrieves a <c>bool</c> value of a <c>Boolean</c> value
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The converted value</returns>
public bool ToBoolean()
{
bool value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsBooleanToBool(this, out value));
return value;
}
/// <summary>
/// Retrieves a <c>double</c> value of a <c>Number</c> value
/// </summary>
/// <remarks>
/// <para>
/// This function retrieves the value of a Number value. It will fail with
/// <c>InvalidArgument</c> if the type of the value is not <c>Number</c>.
/// </para>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
/// <returns>The <c>double</c> value</returns>
public double ToDouble()
{
double value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsNumberToDouble(this, out value));
return value;
}
/// <summary>
/// Retrieves a <c>int</c> value of a <c>Number</c> value
/// </summary>
/// <remarks>
/// <para>
/// This function retrieves the value of a Number value. It will fail with
/// <c>InvalidArgument</c> if the type of the value is not <c>Number</c>.
/// </para>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
/// <returns>The <c>int</c> value</returns>
public int ToInt32()
{
int value;
JsErrorHelpers.ThrowIfError(NativeMethods.JsNumberToInt(this, out value));
return value;
}
/// <summary>
/// Retrieves a string pointer of a <c>String</c> value
/// </summary>
/// <remarks>
/// <para>
/// This function retrieves the string pointer of a <c>String</c> value. It will fail with
/// <c>InvalidArgument</c> if the type of the value is not <c>String</c>.
/// </para>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
/// <returns>The string</returns>
public new string ToString()
{
string result;
JsErrorCode errorCode;
if (Utils.IsWindows())
{
IntPtr ptr;
UIntPtr stringLength;
errorCode = NativeMethods.JsStringToPointer(this, out ptr, out stringLength);
JsErrorHelpers.ThrowIfError(errorCode);
result = Marshal.PtrToStringUni(ptr, (int)stringLength);
}
else
{
byte[] buffer = null;
UIntPtr bufferSize = UIntPtr.Zero;
UIntPtr written;
errorCode = NativeMethods.JsCopyString(this, buffer, bufferSize, out written);
JsErrorHelpers.ThrowIfError(errorCode);
buffer = new byte[(int)written];
bufferSize = new UIntPtr((uint)written);
errorCode = NativeMethods.JsCopyString(this, buffer, bufferSize, out written);
JsErrorHelpers.ThrowIfError(errorCode);
result = Encoding.GetEncoding(0).GetString(buffer);
}
return result;
}
/// <summary>
/// Converts a value to <c>Boolean</c> using regular JavaScript semantics
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The converted value</returns>
public JsValue ConvertToBoolean()
{
JsValue booleanReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToBoolean(this, out booleanReference));
return booleanReference;
}
/// <summary>
/// Converts a value to <c>Number</c> using regular JavaScript semantics
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The converted value</returns>
public JsValue ConvertToNumber()
{
JsValue numberReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToNumber(this, out numberReference));
return numberReference;
}
/// <summary>
/// Converts a value to <c>String</c> using regular JavaScript semantics
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The converted value</returns>
public JsValue ConvertToString()
{
JsValue stringReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToString(this, out stringReference));
return stringReference;
}
/// <summary>
/// Converts a value to <c>Object</c> using regular JavaScript semantics
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>The converted value</returns>
public JsValue ConvertToObject()
{
JsValue objectReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToObject(this, out objectReference));
return objectReference;
}
/// <summary>
/// Sets an object to not be extensible
/// </summary>
/// <remarks>
/// Requires an active script context
/// </remarks>
public void PreventExtension()
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsPreventExtension(this));
}
/// <summary>
/// Gets a property descriptor for an object's own property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="propertyId">The ID of the property</param>
/// <returns>The property descriptor</returns>
public JsValue GetOwnPropertyDescriptor(JsPropertyId propertyId)
{
JsValue descriptorReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetOwnPropertyDescriptor(this, propertyId, out descriptorReference));
return descriptorReference;
}
/// <summary>
/// Gets a list of all properties on the object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <returns>An array of property names</returns>
public JsValue GetOwnPropertyNames()
{
JsValue propertyNamesReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetOwnPropertyNames(this, out propertyNamesReference));
return propertyNamesReference;
}
/// <summary>
/// Determines whether an object has a property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="propertyId">The ID of the property</param>
/// <returns>Whether the object (or a prototype) has the property</returns>
public bool HasProperty(JsPropertyId propertyId)
{
bool hasProperty;
JsErrorHelpers.ThrowIfError(NativeMethods.JsHasProperty(this, propertyId, out hasProperty));
return hasProperty;
}
/// <summary>
/// Gets an object's property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="id">The ID of the property</param>
/// <returns>The value of the property</returns>
public JsValue GetProperty(JsPropertyId id)
{
JsValue propertyReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetProperty(this, id, out propertyReference));
return propertyReference;
}
/// <summary>
/// Sets an object's property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="id">The ID of the property</param>
/// <param name="value">The new value of the property</param>
/// <param name="useStrictRules">The property set should follow strict mode rules</param>
public void SetProperty(JsPropertyId id, JsValue value, bool useStrictRules)
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetProperty(this, id, value, useStrictRules));
}
/// <summary>
/// Deletes an object's property
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="propertyId">The ID of the property</param>
/// <param name="useStrictRules">The property set should follow strict mode rules</param>
/// <returns>Whether the property was deleted</returns>
public JsValue DeleteProperty(JsPropertyId propertyId, bool useStrictRules)
{
JsValue returnReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsDeleteProperty(this, propertyId, useStrictRules, out returnReference));
return returnReference;
}
/// <summary>
/// Defines a new object's own property from a property descriptor
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="propertyId">The ID of the property</param>
/// <param name="propertyDescriptor">The property descriptor</param>
/// <returns>Whether the property was defined</returns>
public bool DefineProperty(JsPropertyId propertyId, JsValue propertyDescriptor)
{
bool result;
JsErrorHelpers.ThrowIfError(NativeMethods.JsDefineProperty(this, propertyId, propertyDescriptor, out result));
return result;
}
/// <summary>
/// Test if an object has a value at the specified index
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="index">The index to test</param>
/// <returns>Whether the object has an value at the specified index</returns>
public bool HasIndexedProperty(JsValue index)
{
bool hasProperty;
JsErrorHelpers.ThrowIfError(NativeMethods.JsHasIndexedProperty(this, index, out hasProperty));
return hasProperty;
}
/// <summary>
/// Retrieves a value at the specified index of an object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="index">The index to retrieve</param>
/// <returns>The retrieved value</returns>
public JsValue GetIndexedProperty(JsValue index)
{
JsValue propertyReference;
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetIndexedProperty(this, index, out propertyReference));
return propertyReference;
}
/// <summary>
/// Sets a value at the specified index of an object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="index">The index to set</param>
/// <param name="value">The value to set</param>
public void SetIndexedProperty(JsValue index, JsValue value)
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetIndexedProperty(this, index, value));
}
/// <summary>
/// Deletes d value at the specified index of an object
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="index">The index to delete</param>
public void DeleteIndexedProperty(JsValue index)
{
JsErrorHelpers.ThrowIfError(NativeMethods.JsDeleteIndexedProperty(this, index));
}
/// <summary>
/// Compare two JavaScript values for equality
/// </summary>
/// <remarks>
/// <para>
/// This function is equivalent to the "==" operator in JavaScript.
/// </para>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
/// <param name="other">The object to compare</param>
/// <returns>Whether the values are equal</returns>
public bool Equals(JsValue other)
{
bool equals;
JsErrorHelpers.ThrowIfError(NativeMethods.JsEquals(this, other, out equals));
return equals;
}
/// <summary>
/// Compare two JavaScript values for strict equality
/// </summary>
/// <remarks>
/// <para>
/// This function is equivalent to the "===" operator in JavaScript.
/// </para>
/// <para>
/// Requires an active script context.
/// </para>
/// </remarks>
/// <param name="other">The object to compare</param>
/// <returns>Whether the values are strictly equal</returns>
public bool StrictEquals(JsValue other)
{
bool equals;
JsErrorHelpers.ThrowIfError(NativeMethods.JsStrictEquals(this, other, out equals));
return equals;
}
/// <summary>
/// Invokes a function
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="arguments">The arguments to the call</param>
/// <returns>The <c>Value</c> returned from the function invocation, if any</returns>
public JsValue CallFunction(params JsValue[] arguments)
{
JsValue returnReference;
if (arguments.Length > ushort.MaxValue)
{
throw new ArgumentOutOfRangeException("arguments");
}
JsErrorHelpers.ThrowIfError(NativeMethods.JsCallFunction(this, arguments, (ushort)arguments.Length, out returnReference));
return returnReference;
}
/// <summary>
/// Invokes a function as a constructor
/// </summary>
/// <remarks>
/// Requires an active script context.
/// </remarks>
/// <param name="arguments">The arguments to the call</param>
/// <returns>The <c>Value</c> returned from the function invocation</returns>
public JsValue ConstructObject(params JsValue[] arguments)
{
JsValue returnReference;
if (arguments.Length > ushort.MaxValue)
{
throw new ArgumentOutOfRangeException("arguments");
}
JsErrorHelpers.ThrowIfError(NativeMethods.JsConstructObject(this, arguments, (ushort)arguments.Length, out returnReference));
return returnReference;
}
}
}