MCPcopy Create free account
hub / github.com/buke/quickjs-go / createPointClass

Function createPointClass

class_test.go:40–123  ·  view source on GitHub ↗

createPointClass creates a Point class for testing with SCHEME C constructor

(ctx *Context)

Source from the content-addressed store, hash-verified

38
39// createPointClass creates a Point class for testing with SCHEME C constructor
40func createPointClass(ctx *Context) (*Value, uint32) {
41 return NewClassBuilder("Point").
42 Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
43 x, y := 0.0, 0.0
44 if len(args) > 0 {
45 x = args[0].ToFloat64() // Updated: Use ToFloat64()
46 }
47 if len(args) > 1 {
48 y = args[1].ToFloat64() // Updated: Use ToFloat64()
49 }
50
51 // SCHEME C: Create Go object and return it for automatic association
52 point := &Point{X: x, Y: y}
53 return point, nil
54 }).
55 Method("norm", func(ctx *Context, this *Value, args []*Value) *Value {
56 obj, err := this.GetGoObject()
57 if err != nil {
58 return ctx.ThrowError(err)
59 }
60 point := obj.(*Point)
61 norm := math.Sqrt(point.X*point.X + point.Y*point.Y)
62 return ctx.NewFloat64(norm) // Updated: Use NewFloat64()
63 }).
64 Method("toString", func(ctx *Context, this *Value, args []*Value) *Value {
65 obj, err := this.GetGoObject()
66 if err != nil {
67 return ctx.ThrowError(err)
68 }
69 point := obj.(*Point)
70 return ctx.NewString(point.String()) // Updated: Use NewString()
71 }).
72 Accessor("x",
73 func(ctx *Context, this *Value) *Value { // getter
74 obj, err := this.GetGoObject()
75 if err != nil {
76 return ctx.ThrowError(err)
77 }
78 point := obj.(*Point)
79 return ctx.NewFloat64(point.X) // Updated: Use NewFloat64()
80 },
81 func(ctx *Context, this *Value, value *Value) *Value { // setter
82 obj, err := this.GetGoObject()
83 if err != nil {
84 return ctx.ThrowError(err)
85 }
86 point := obj.(*Point)
87 point.X = value.ToFloat64() // Updated: Use ToFloat64()
88 return ctx.NewUndefined() // Updated: Use NewUndefined()
89 }).
90 Accessor("y",
91 func(ctx *Context, this *Value) *Value { // getter
92 obj, err := this.GetGoObject()
93 if err != nil {
94 return ctx.ThrowError(err)
95 }
96 point := obj.(*Point)
97 return ctx.NewFloat64(point.Y) // Updated: Use NewFloat64()

Callers 15

TestBasicClassCreationFunction · 0.85
TestInstanceMethodsFunction · 0.85
TestAccessorsFunction · 0.85
TestStaticMethodsFunction · 0.85
TestStaticAccessorsFunction · 0.85
TestPropertiesFunction · 0.85
TestStaticPropertiesFunction · 0.85
TestPropertyFlagsFunction · 0.85

Calls 15

StringMethod · 0.95
NewClassBuilderFunction · 0.85
StaticPropertyMethod · 0.80
PropertyMethod · 0.80
StaticAccessorMethod · 0.80
StaticMethodMethod · 0.80
AccessorMethod · 0.80
MethodMethod · 0.80
ConstructorMethod · 0.80
ToFloat64Method · 0.80
GetGoObjectMethod · 0.80
ThrowErrorMethod · 0.80

Tested by

no test coverage detected