-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathvalue.go
More file actions
1225 lines (1051 loc) · 35.3 KB
/
value.go
File metadata and controls
1225 lines (1051 loc) · 35.3 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
package quickjs
/*
#include "bridge.h"
*/
import "C"
import (
"encoding/binary"
"errors"
"math"
"math/big"
"unsafe"
)
// Value represents a Javascript value which can be a primitive type or an object.
// Reference counting is used, so it is important to explicitly duplicate (JS_DupValue(),
// increment the reference count) or free (JS_FreeValue(), decrement the reference count) JSValues.
type Value struct {
ctx *Context
ref C.JSValue
// borrowed indicates ref is an alias to a JSValue owned elsewhere.
// Free should only invalidate this Go handle, not decrement QuickJS refcount.
borrowed bool
}
// PropertyDescriptor mirrors QuickJS property descriptor semantics.
// Value/Getter/Setter returned by OwnProperty are owned by caller and must be freed.
type PropertyDescriptor struct {
Flags int
Value *Value
Getter *Value
Setter *Value
}
// Property descriptor flags matching QuickJS JS_PROP_* constants.
const (
PropConfigurable = 1 << 0
PropWritable = 1 << 1
PropEnumerable = 1 << 2
PropHasConfigurable = 1 << 8
PropHasWritable = 1 << 9
PropHasEnumerable = 1 << 10
PropHasGet = 1 << 11
PropHasSet = 1 << 12
PropHasValue = 1 << 13
)
// hasValidContext reports whether a Value still has a usable context pointer.
func (v *Value) hasValidContext() bool {
return v != nil && v.ctx != nil && v.ctx.hasValidRef()
}
// isAlive reports whether a Value can safely reach QuickJS.
func (v *Value) isAlive() bool {
return v != nil && v.ctx != nil && v.ctx.isAlive() && v.ctx.hasValidRef()
}
// belongsTo reports whether a Value belongs to the given live Context.
func (v *Value) belongsTo(ctx *Context) bool {
return v != nil && ctx != nil && v.ctx == ctx && v.hasValidContext()
}
func sameContextRef(a *Value, b *Value) bool {
return a != nil && b != nil && a.ctx != nil && b.ctx != nil && a.ctx == b.ctx
}
// sameContext reports whether two values belong to the same live context.
func sameContext(a *Value, b *Value) bool {
if !sameContextRef(a, b) {
return false
}
return a.hasValidContext() && b.hasValidContext()
}
// Equal returns true if two values are abstractly equal (JS == semantics).
func (v *Value) Equal(other *Value) bool {
if !sameContext(v, other) {
return false
}
return C.JS_IsEqual(v.ctx.ref, v.ref, other.ref) == 1
}
// StrictEqual returns true if two values are strictly equal (JS === semantics).
func (v *Value) StrictEqual(other *Value) bool {
if !sameContext(v, other) {
return false
}
return bool(C.JS_IsStrictEqual(v.ctx.ref, v.ref, other.ref))
}
// SameValue returns true if two values are the same according to JS SameValue.
func (v *Value) SameValue(other *Value) bool {
if !sameContext(v, other) {
return false
}
return bool(C.JS_IsSameValue(v.ctx.ref, v.ref, other.ref))
}
// SameValueZero returns true if two values are the same according to JS SameValueZero.
func (v *Value) SameValueZero(other *Value) bool {
if !sameContext(v, other) {
return false
}
return bool(C.JS_IsSameValueZero(v.ctx.ref, v.ref, other.ref))
}
// Free the value.
func (v *Value) Free() {
if !v.hasValidContext() || bool(C.JS_IsUndefined(v.ref)) {
return // No context or undefined value, nothing to free
}
if v.borrowed {
v.ref = C.JS_NewUndefined()
v.borrowed = false
return
}
C.JS_FreeValue(v.ctx.ref, v.ref)
v.ref = C.JS_NewUndefined()
}
// Context returns the context of the value.
func (v *Value) Context() *Context {
return v.ctx
}
// Deprecated: Use ToBool instead.
func (v *Value) Bool() bool {
return v.ToBool()
}
// ToBool returns the boolean value of the value.
func (v *Value) ToBool() bool {
return C.JS_ToBool(v.ctx.ref, v.ref) == 1
}
// String returns the string representation of the value.
// This method implements the fmt.Stringer interface.
func (v *Value) String() string {
return v.ToString()
}
// ToString returns the string representation of the value.
func (v *Value) ToString() string {
ptr := C.JS_ToCString(v.ctx.ref, v.ref)
defer C.JS_FreeCString(v.ctx.ref, ptr)
return C.GoString(ptr)
}
// JSONStringify returns the JSON string representation of the value.
func (v *Value) JSONStringify() string {
ref := C.JS_JSONStringify(v.ctx.ref, v.ref, C.JS_NewNull(), C.JS_NewNull())
ptr := C.JS_ToCString(v.ctx.ref, ref)
defer C.JS_FreeCString(v.ctx.ref, ptr)
return C.GoString(ptr)
}
func (v *Value) ToByteArray(size uint) ([]byte, error) {
if v.ByteLen() < int64(size) {
return nil, errors.New("exceeds the maximum length of the current binary array")
}
cSize := C.size_t(size)
outBuf := C.JS_GetArrayBuffer(v.ctx.ref, &cSize, v.ref)
if outBuf == nil {
return nil, errors.New("failed to get ArrayBuffer data")
}
return C.GoBytes(unsafe.Pointer(outBuf), C.int(size)), nil
}
// Deprecated: Use ToInt64 instead.
func (v *Value) Int64() int64 {
return v.ToInt64()
}
// ToInt64 returns the int64 value of the value.
func (v *Value) ToInt64() int64 {
val := C.int64_t(0)
C.JS_ToInt64(v.ctx.ref, &val, v.ref)
return int64(val)
}
// Deprecated: Use ToInt32 instead.
func (v *Value) Int32() int32 {
return v.ToInt32()
}
// ToInt32 returns the int32 value of the value.
func (v *Value) ToInt32() int32 {
val := C.int32_t(0)
C.JS_ToInt32(v.ctx.ref, &val, v.ref)
return int32(val)
}
// Deprecated: Use ToUint32 instead.
func (v *Value) Uint32() uint32 {
return v.ToUint32()
}
// ToUint32 returns the uint32 value of the value.
func (v *Value) ToUint32() uint32 {
val := C.uint32_t(0)
C.JS_ToUint32(v.ctx.ref, &val, v.ref)
return uint32(val)
}
// Deprecated: Use ToFloat64 instead.
func (v *Value) Float64() float64 {
return v.ToFloat64()
}
// ToFloat64 returns the float64 value of the value.
func (v *Value) ToFloat64() float64 {
val := C.double(0)
C.JS_ToFloat64(v.ctx.ref, &val, v.ref)
return float64(val)
}
// Deprecated: Use ToBigInt instead.
func (v *Value) BigInt() *big.Int {
return v.ToBigInt()
}
// ToBigInt returns the big.Int value of the value.
func (v *Value) ToBigInt() *big.Int {
if !v.IsBigInt() {
return nil
}
val, _ := new(big.Int).SetString(v.ToString(), 10)
return val
}
// Len returns the length of the array.
func (v *Value) Len() int64 {
length := v.Get("length")
defer length.Free()
return length.ToInt64()
}
// ByteLen returns the length of the ArrayBuffer.
func (v *Value) ByteLen() int64 {
byteLength := v.Get("byteLength")
defer byteLength.Free()
return byteLength.ToInt64()
}
// Set sets the value of the property with the given name.
func (v *Value) Set(name string, val *Value) {
if !v.isAlive() || !val.belongsTo(v.ctx) {
return
}
var namePtr *C.char
if len(name) > 0 {
namePtr = (*C.char)(unsafe.Pointer(unsafe.StringData(name)))
}
C.SetPropertyByNameLen(v.ctx.ref, v.ref, namePtr, C.size_t(len(name)), val.ref)
}
// SetIdx sets the value of the property with the given index.
func (v *Value) SetIdx(idx int64, val *Value) {
if !v.isAlive() || !val.belongsTo(v.ctx) {
return
}
C.JS_SetPropertyUint32(v.ctx.ref, v.ref, C.uint32_t(idx), val.ref)
}
// Get returns the value of the property with the given name.
func (v *Value) Get(name string) *Value {
if !v.hasValidContext() {
return nil
}
var namePtr *C.char
if len(name) > 0 {
namePtr = (*C.char)(unsafe.Pointer(unsafe.StringData(name)))
}
return &Value{ctx: v.ctx, ref: C.GetPropertyByNameLen(v.ctx.ref, v.ref, namePtr, C.size_t(len(name)))}
}
// GetIdx returns the value of the property with the given index.
func (v *Value) GetIdx(idx int64) *Value {
if !v.hasValidContext() {
return nil
}
return &Value{ctx: v.ctx, ref: C.JS_GetPropertyUint32(v.ctx.ref, v.ref, C.uint32_t(idx))}
}
// Call calls the function with the given arguments.
func (v *Value) Call(fname string, args ...*Value) *Value {
if !v.isAlive() {
return nil
}
var fnamePtr *C.char
if len(fname) > 0 {
fnamePtr = (*C.char)(unsafe.Pointer(unsafe.StringData(fname)))
}
cargs := []C.JSValue{}
for _, x := range args {
if !x.belongsTo(v.ctx) {
return nil
}
cargs = append(cargs, x.ref)
}
var ref C.JSValue
if len(cargs) == 0 {
ref = C.CallPropertyByNameLen(v.ctx.ref, v.ref, fnamePtr, C.size_t(len(fname)), C.int(0), nil)
} else {
ref = C.CallPropertyByNameLen(v.ctx.ref, v.ref, fnamePtr, C.size_t(len(fname)), C.int(len(cargs)), &cargs[0])
}
return &Value{ctx: v.ctx, ref: ref}
}
// Execute the function with the given arguments.
func (v *Value) Execute(this *Value, args ...*Value) *Value {
if !v.isAlive() || !this.belongsTo(v.ctx) {
return nil
}
cargs := []C.JSValue{}
for _, x := range args {
if !x.belongsTo(v.ctx) {
return nil
}
cargs = append(cargs, x.ref)
}
var val *Value
if len(cargs) == 0 {
val = &Value{ctx: v.ctx, ref: C.JS_Call(v.ctx.ref, v.ref, this.ref, C.int(0), nil)}
} else {
val = &Value{ctx: v.ctx, ref: C.JS_Call(v.ctx.ref, v.ref, this.ref, C.int(len(cargs)), &cargs[0])}
}
return val
}
// New calls the constructor with the given arguments.
func (v *Value) New(args ...*Value) *Value {
return v.CallConstructor(args...)
}
// CallConstructor calls the constructor with the given arguments.
// SCHEME C: For class instances, use this method to create instances.
// The class constructor function will receive a pre-created instance and initialize it.
// Instance properties declared in ClassBuilder.Property() are automatically bound to the instance.
//
// Example usage:
//
// constructor := ctx.Eval("MyClass")
// instance := constructor.CallConstructor(arg1, arg2)
//
// This replaces the previous NewInstance method and provides automatic property binding
// and simplified constructor semantics where constructors work with pre-created instances.
func (v *Value) CallConstructor(args ...*Value) *Value {
if !v.isAlive() {
return nil
}
cargs := []C.JSValue{}
for _, x := range args {
if !x.belongsTo(v.ctx) {
return nil
}
cargs = append(cargs, x.ref)
}
var val *Value
if len(cargs) == 0 {
val = &Value{ctx: v.ctx, ref: C.JS_CallConstructor(v.ctx.ref, v.ref, C.int(0), nil)}
} else {
val = &Value{ctx: v.ctx, ref: C.JS_CallConstructor(v.ctx.ref, v.ref, C.int(len(cargs)), &cargs[0])}
}
return val
}
// Deprecated: Use ToError() instead.
func (v *Value) Error() error {
return v.ToError()
}
// ToError returns the error value of the value.
func (v *Value) ToError() error {
if !v.IsError() {
return nil
}
err := &Error{}
name := v.Get("name")
defer name.Free()
if !name.IsUndefined() {
err.Name = name.ToString()
}
message := v.Get("message")
defer message.Free()
if !message.IsUndefined() {
err.Message = message.ToString()
}
cause := v.Get("cause")
defer cause.Free()
if !cause.IsUndefined() {
err.Cause = cause.ToString()
}
stack := v.Get("stack")
defer stack.Free()
if !stack.IsUndefined() {
err.Stack = stack.ToString()
}
jsonString := v.JSONStringify()
if jsonString != "" {
err.JSONString = jsonString
}
return err
}
// propertyEnum is a wrapper around JSValue.
func (v *Value) propertyEnum() ([]*propertyEnum, error) {
var ptr *C.JSPropertyEnum
var size C.uint32_t
result := int(C.JS_GetOwnPropertyNames(v.ctx.ref, &ptr, &size, v.ref, C.int(1<<0|1<<1|1<<2)))
if result < 0 {
return nil, errors.New("value does not contain properties")
}
defer C.js_free(v.ctx.ref, unsafe.Pointer(ptr))
entries := unsafe.Slice(ptr, size) // Go 1.17 and later
names := make([]*propertyEnum, len(entries))
for i := 0; i < len(names); i++ {
names[i] = &propertyEnum{
IsEnumerable: bool(entries[i].is_enumerable),
atom: &Atom{ctx: v.ctx, ref: entries[i].atom},
}
names[i].atom.Free()
}
return names, nil
}
// PropertyNames returns the names of the properties of the value.
func (v *Value) PropertyNames() ([]string, error) {
pList, err := v.propertyEnum()
if err != nil {
return nil, err
}
names := make([]string, len(pList))
for i := 0; i < len(names); i++ {
names[i] = pList[i].ToString()
}
return names, nil
}
// Has returns true if the value has the property with the given name.
func (v *Value) Has(name string) bool {
prop := v.ctx.NewAtom(name)
defer prop.Free()
return C.JS_HasProperty(v.ctx.ref, v.ref, prop.ref) == 1
}
// HasIdx returns true if the value has the property with the given index.
func (v *Value) HasIdx(idx uint32) bool {
prop := v.ctx.NewAtomIdx(idx)
defer prop.Free()
return C.JS_HasProperty(v.ctx.ref, v.ref, prop.ref) == 1
}
// Delete deletes the property with the given name.
func (v *Value) Delete(name string) bool {
if !v.Has(name) {
return false // Property does not exist, nothing to delete
}
prop := v.ctx.NewAtom(name)
defer prop.Free()
return C.JS_DeleteProperty(v.ctx.ref, v.ref, prop.ref, C.int(1)) == 1
}
// DeleteIdx deletes the property with the given index.
func (v *Value) DeleteIdx(idx uint32) bool {
if !v.HasIdx(idx) {
return false // Property does not exist, nothing to delete
}
return C.JS_DeletePropertyInt64(v.ctx.ref, v.ref, C.int64_t(idx), C.int(1)) == 1
}
// DefineProperty defines a property using a full descriptor.
func (v *Value) DefineProperty(name string, desc PropertyDescriptor) bool {
if !v.isAlive() {
return false
}
atom := v.ctx.NewAtom(name)
defer atom.Free()
return v.DefinePropertyAtom(atom, desc)
}
// DefinePropertyAtom defines a property using a full descriptor and a pre-built atom.
func (v *Value) DefinePropertyAtom(atom *Atom, desc PropertyDescriptor) bool {
if !v.isAlive() || atom == nil || atom.ctx == nil || atom.ctx != v.ctx || !atom.ctx.hasValidRef() {
return false
}
if desc.Value != nil && !desc.Value.belongsTo(v.ctx) {
return false
}
if desc.Getter != nil && !desc.Getter.belongsTo(v.ctx) {
return false
}
if desc.Setter != nil && !desc.Setter.belongsTo(v.ctx) {
return false
}
value := C.JS_NewUndefined()
if desc.Value != nil {
// JS_DefineProperty borrows descriptor values; do not transfer caller ownership.
value = desc.Value.ref
}
getter := C.JS_NewUndefined()
if desc.Getter != nil {
// Borrowed argument: keep caller-managed getter handle valid after the call.
getter = desc.Getter.ref
}
setter := C.JS_NewUndefined()
if desc.Setter != nil {
// Borrowed argument: keep caller-managed setter handle valid after the call.
setter = desc.Setter.ref
}
ret := C.JS_DefineProperty(v.ctx.ref, v.ref, atom.ref, value, getter, setter, C.int(desc.Flags))
if ret < 0 {
return false
}
return ret == 1
}
// DefinePropertyValue defines a value property by name.
func (v *Value) DefinePropertyValue(name string, value *Value, flags int) bool {
if !v.isAlive() || !value.belongsTo(v.ctx) {
return false
}
atom := v.ctx.NewAtom(name)
defer atom.Free()
// JS_DefinePropertyValue consumes `dup` but does not consume `atom`.
dup := C.JS_DupValue(v.ctx.ref, value.ref)
ret := C.JS_DefinePropertyValue(v.ctx.ref, v.ref, atom.ref, dup, C.int(flags))
if ret < 0 {
return false
}
return ret == 1
}
// DefinePropertyGetSet defines an accessor property by name.
func (v *Value) DefinePropertyGetSet(name string, getter *Value, setter *Value, flags int) bool {
if !v.isAlive() {
return false
}
if getter != nil && !getter.belongsTo(v.ctx) {
return false
}
if setter != nil && !setter.belongsTo(v.ctx) {
return false
}
atom := v.ctx.NewAtom(name)
defer atom.Free()
getterRef := C.JS_NewUndefined()
setterRef := C.JS_NewUndefined()
if getter != nil {
// JS_DefinePropertyGetSet consumes getter/setter values.
getterRef = C.JS_DupValue(v.ctx.ref, getter.ref)
}
if setter != nil {
// JS_DefinePropertyGetSet consumes getter/setter values.
setterRef = C.JS_DupValue(v.ctx.ref, setter.ref)
}
ret := C.JS_DefinePropertyGetSet(v.ctx.ref, v.ref, atom.ref, getterRef, setterRef, C.int(flags))
if ret < 0 {
return false
}
return ret == 1
}
// OwnProperty gets a full own property descriptor by name.
func (v *Value) OwnProperty(name string) (*PropertyDescriptor, bool) {
if !v.isAlive() {
return nil, false
}
atom := v.ctx.NewAtom(name)
defer atom.Free()
var desc C.JSPropertyDescriptor
ret := C.JS_GetOwnProperty(v.ctx.ref, &desc, v.ref, atom.ref)
if ret <= 0 {
return nil, false
}
result := &PropertyDescriptor{
Flags: int(desc.flags),
Value: &Value{ctx: v.ctx, ref: desc.value},
Getter: &Value{ctx: v.ctx, ref: desc.getter},
Setter: &Value{ctx: v.ctx, ref: desc.setter},
}
return result, true
}
// GetAtom gets a property by atom key.
func (v *Value) GetAtom(atom *Atom) *Value {
if !v.isAlive() || atom == nil || atom.ctx == nil || atom.ctx != v.ctx || !atom.ctx.hasValidRef() {
return nil
}
// JS_GetProperty borrows atom; caller retains atom ownership.
return &Value{ctx: v.ctx, ref: C.JS_GetProperty(v.ctx.ref, v.ref, atom.ref)}
}
// SetAtom sets a property by atom key.
func (v *Value) SetAtom(atom *Atom, val *Value) bool {
if !v.isAlive() || atom == nil || atom.ctx == nil || atom.ctx != v.ctx || !atom.ctx.hasValidRef() || !val.belongsTo(v.ctx) {
return false
}
// JS_SetProperty consumes `dup` but borrows `atom`.
dup := C.JS_DupValue(v.ctx.ref, val.ref)
ret := C.JS_SetProperty(v.ctx.ref, v.ref, atom.ref, dup)
if ret < 0 {
return false
}
return ret == 1
}
// GetInt64 gets a property by int64 key.
func (v *Value) GetInt64(idx int64) *Value {
if !v.hasValidContext() {
return nil
}
return &Value{ctx: v.ctx, ref: C.JS_GetPropertyInt64(v.ctx.ref, v.ref, C.int64_t(idx))}
}
// SetInt64 sets a property by int64 key.
func (v *Value) SetInt64(idx int64, val *Value) bool {
if !v.isAlive() || !val.belongsTo(v.ctx) {
return false
}
dup := C.JS_DupValue(v.ctx.ref, val.ref)
ret := C.JS_SetPropertyInt64(v.ctx.ref, v.ref, C.int64_t(idx), dup)
if ret < 0 {
return false
}
return ret == 1
}
// Prototype returns the object's prototype value.
func (v *Value) Prototype() *Value {
if !v.isAlive() {
return nil
}
return &Value{ctx: v.ctx, ref: C.JS_GetPrototype(v.ctx.ref, v.ref)}
}
// SetPrototype sets the object's prototype.
func (v *Value) SetPrototype(proto *Value) bool {
if !v.isAlive() || !proto.belongsTo(v.ctx) {
return false
}
return C.JS_SetPrototype(v.ctx.ref, v.ref, proto.ref) == 1
}
// IsExtensible returns true if new properties can still be added.
func (v *Value) IsExtensible() bool {
if !v.isAlive() {
return false
}
ret := C.JS_IsExtensible(v.ctx.ref, v.ref)
if ret < 0 {
return false
}
return ret == 1
}
// PreventExtensions marks object as non-extensible.
func (v *Value) PreventExtensions() bool {
if !v.isAlive() {
return false
}
ret := C.JS_PreventExtensions(v.ctx.ref, v.ref)
if ret < 0 {
return false
}
return ret == 1
}
// Seal seals object properties and prevents extensions.
func (v *Value) Seal() bool {
if !v.isAlive() {
return false
}
ret := C.JS_SealObject(v.ctx.ref, v.ref)
if ret < 0 {
return false
}
return ret == 1
}
// Freeze freezes object properties and prevents extensions.
func (v *Value) Freeze() bool {
if !v.isAlive() {
return false
}
ret := C.JS_FreezeObject(v.ctx.ref, v.ref)
if ret < 0 {
return false
}
return ret == 1
}
// GlobalInstanceof checks if the value is an instance of the given global constructor
func (v *Value) GlobalInstanceof(name string) bool {
ctor := v.ctx.Globals().Get(name)
defer ctor.Free()
if ctor.IsUndefined() {
return false
}
return C.JS_IsInstanceOf(v.ctx.ref, v.ref, ctor.ref) == 1
}
// getTypedArrayInfo is a helper function to extract TypedArray information using C API
func (v *Value) getTypedArrayInfo() (buffer *Value, byteOffset, byteLength, bytesPerElement int) {
var cByteOffset, cByteLength, cBytesPerElement C.size_t
bufferRef := C.JS_GetTypedArrayBuffer(v.ctx.ref, v.ref, &cByteOffset, &cByteLength, &cBytesPerElement)
return &Value{ctx: v.ctx, ref: bufferRef},
int(cByteOffset), int(cByteLength), int(cBytesPerElement)
}
// ToInt8Array converts the value to int8 slice if it's an Int8Array.
func (v *Value) ToInt8Array() ([]int8, error) {
if !v.IsInt8Array() {
return nil, errors.New("value is not an Int8Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]int8, len(data))
for i, b := range data {
result[i] = int8(b)
}
return result, nil
}
// ToUint8Array converts the value to uint8 slice if it's a Uint8Array or Uint8ClampedArray.
func (v *Value) ToUint8Array() ([]uint8, error) {
if !v.IsUint8Array() && !v.IsUint8ClampedArray() {
return nil, errors.New("value is not a Uint8Array or Uint8ClampedArray")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
return bytes[byteOffset : byteOffset+byteLength], nil
}
// ToInt16Array converts the value to int16 slice if it's an Int16Array.
func (v *Value) ToInt16Array() ([]int16, error) {
if !v.IsInt16Array() {
return nil, errors.New("value is not an Int16Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]int16, len(data)/2)
for i := 0; i < len(result); i++ {
result[i] = int16(binary.LittleEndian.Uint16(data[i*2:]))
}
return result, nil
}
// ToUint16Array converts the value to uint16 slice if it's a Uint16Array.
func (v *Value) ToUint16Array() ([]uint16, error) {
if !v.IsUint16Array() {
return nil, errors.New("value is not a Uint16Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]uint16, len(data)/2)
for i := 0; i < len(result); i++ {
result[i] = binary.LittleEndian.Uint16(data[i*2:])
}
return result, nil
}
// ToInt32Array converts the value to int32 slice if it's an Int32Array.
func (v *Value) ToInt32Array() ([]int32, error) {
if !v.IsInt32Array() {
return nil, errors.New("value is not an Int32Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]int32, len(data)/4)
for i := 0; i < len(result); i++ {
result[i] = int32(binary.LittleEndian.Uint32(data[i*4:]))
}
return result, nil
}
// ToUint32Array converts the value to uint32 slice if it's a Uint32Array.
func (v *Value) ToUint32Array() ([]uint32, error) {
if !v.IsUint32Array() {
return nil, errors.New("value is not a Uint32Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]uint32, len(data)/4)
for i := 0; i < len(result); i++ {
result[i] = binary.LittleEndian.Uint32(data[i*4:])
}
return result, nil
}
// ToFloat32Array converts the value to float32 slice if it's a Float32Array.
func (v *Value) ToFloat32Array() ([]float32, error) {
if !v.IsFloat32Array() {
return nil, errors.New("value is not a Float32Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]float32, len(data)/4)
for i := 0; i < len(result); i++ {
bits := binary.LittleEndian.Uint32(data[i*4:])
result[i] = math.Float32frombits(bits)
}
return result, nil
}
// ToFloat64Array converts the value to float64 slice if it's a Float64Array.
func (v *Value) ToFloat64Array() ([]float64, error) {
if !v.IsFloat64Array() {
return nil, errors.New("value is not a Float64Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]float64, len(data)/8)
for i := 0; i < len(result); i++ {
bits := binary.LittleEndian.Uint64(data[i*8:])
result[i] = math.Float64frombits(bits)
}
return result, nil
}
// ToBigInt64Array converts the value to int64 slice if it's a BigInt64Array.
func (v *Value) ToBigInt64Array() ([]int64, error) {
if !v.IsBigInt64Array() {
return nil, errors.New("value is not a BigInt64Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]int64, len(data)/8)
for i := 0; i < len(result); i++ {
result[i] = int64(binary.LittleEndian.Uint64(data[i*8:]))
}
return result, nil
}
// ToBigUint64Array converts the value to uint64 slice if it's a BigUint64Array.
func (v *Value) ToBigUint64Array() ([]uint64, error) {
if !v.IsBigUint64Array() {
return nil, errors.New("value is not a BigUint64Array")
}
buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
defer buffer.Free()
totalSize := uint(byteOffset + byteLength)
bytes, err := buffer.ToByteArray(totalSize)
if err != nil {
return nil, err
}
data := bytes[byteOffset : byteOffset+byteLength]
result := make([]uint64, len(data)/8)
for i := 0; i < len(result); i++ {
result[i] = binary.LittleEndian.Uint64(data[i*8:])
}
return result, nil
}
// =============================================================================
// BASIC TYPE CHECKING METHODS
// =============================================================================
func (v *Value) IsNumber() bool { return v != nil && bool(C.JS_IsNumber(v.ref)) }
func (v *Value) IsBigInt() bool { return v != nil && bool(C.JS_IsBigInt(v.ref)) }
func (v *Value) IsBool() bool { return v != nil && bool(C.JS_IsBool(v.ref)) }
func (v *Value) IsNull() bool { return v != nil && bool(C.JS_IsNull(v.ref)) }
func (v *Value) IsUndefined() bool { return v != nil && bool(C.JS_IsUndefined(v.ref)) }
func (v *Value) IsException() bool { return v != nil && bool(C.JS_IsException(v.ref)) }
func (v *Value) IsUninitialized() bool { return v != nil && bool(C.JS_IsUninitialized(v.ref)) }
func (v *Value) IsString() bool { return v != nil && bool(C.JS_IsString(v.ref)) }
func (v *Value) IsSymbol() bool { return v != nil && bool(C.JS_IsSymbol(v.ref)) }
func (v *Value) IsObject() bool { return v != nil && bool(C.JS_IsObject(v.ref)) }
func (v *Value) IsArray() bool { return v != nil && bool(C.JS_IsArray(v.ref)) }
func (v *Value) IsDate() bool { return v != nil && bool(C.JS_IsDate(v.ref)) }
func (v *Value) IsError() bool { return v != nil && bool(C.JS_IsError(v.ref)) }
func (v *Value) IsFunction() bool { return v != nil && bool(C.JS_IsFunction(v.ctx.ref, v.ref)) }
func (v *Value) IsAsyncFunction() bool { return v != nil && bool(C.JS_IsAsyncFunction(v.ref)) }
func (v *Value) IsConstructor() bool { return v != nil && bool(C.JS_IsConstructor(v.ctx.ref, v.ref)) }
// =============================================================================
// PROMISE SUPPORT METHODS (replaced constants with getter functions)
// =============================================================================
func (v *Value) IsPromise() bool {
if v == nil {
return false
}
state := C.JS_PromiseState(v.ctx.ref, v.ref)
pending := C.int(C.JS_PROMISE_PENDING)
fulfilled := C.int(C.JS_PROMISE_FULFILLED)
rejected := C.int(C.JS_PROMISE_REJECTED)
return C.int(state) == pending || C.int(state) == fulfilled || C.int(state) == rejected
}
// Promise state enumeration matching QuickJS
type PromiseState int