function.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package core
  2. type Function interface {
  3. Call(args ([] Object), ctx ([] Object), h RuntimeHandle) Object
  4. }
  5. type NativeFunction func(args ([] Object), ctx ([] Object), h RuntimeHandle) Object
  6. func (f NativeFunction) Call(args ([] Object), ctx ([] Object), h RuntimeHandle) Object {
  7. return f(args, ctx, h)
  8. }
  9. type FieldValueGetterFunction struct { Index int }
  10. func (f FieldValueGetterFunction) Call(args ([] Object), _ ([] Object), _ RuntimeHandle) Object {
  11. var arg = args[0]
  12. var record = (*arg).(Record)
  13. return record.Objects[f.Index]
  14. }
  15. func FunctionToLambda(op Function, unpack bool, ctx ([] Object), h RuntimeHandle) Lambda {
  16. if unpack {
  17. return Lambda {
  18. Call: func(arg Object) Object {
  19. var args = (*arg).(Record).Objects
  20. return op.Call(args, ctx, h)
  21. },
  22. }
  23. } else {
  24. return Lambda {
  25. Call: func(arg Object) Object {
  26. var args = [] Object { arg }
  27. return op.Call(args, ctx, h)
  28. },
  29. }
  30. }
  31. }
  32. func FunctionToLambdaObject(op Function, unpack bool, ctx ([] Object), h RuntimeHandle) Object {
  33. var o = ObjectImpl(FunctionToLambda(op, unpack, ctx, h))
  34. return &o
  35. }