createPointClass creates a Point class for testing with SCHEME C constructor
(ctx *Context)
| 38 | |
| 39 | // createPointClass creates a Point class for testing with SCHEME C constructor |
| 40 | func 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() |
no test coverage detected