forked from moisespsena-go/ugo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core!: 1) add Pipe expression; 2) Converters at VM level; 3) IndexGet…
… Proxy
- Loading branch information
1 parent
7ba056a
commit 9dbacdc
Showing
27 changed files
with
531 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package gad | ||
|
||
import "reflect" | ||
|
||
type ObjectConverters struct { | ||
ToGoHandlers map[ObjectType]func(vm *VM, v Object) any | ||
ToObjectHandlers map[reflect.Type]func(vm *VM, v any) (Object, error) | ||
} | ||
|
||
func NewObjectConverters() *ObjectConverters { | ||
return &ObjectConverters{ | ||
ToGoHandlers: make(map[ObjectType]func(vm *VM, v Object) any), | ||
ToObjectHandlers: make(map[reflect.Type]func(vm *VM, v any) (Object, error)), | ||
} | ||
} | ||
|
||
func (oc *ObjectConverters) Register(objType ObjectType, togo func(vm *VM, v Object) any, goType reflect.Type, toObject func(vm *VM, v any) (Object, error)) *ObjectConverters { | ||
if objType != nil { | ||
oc.ToGoHandlers[objType] = togo | ||
} | ||
if goType != nil { | ||
oc.ToObjectHandlers[goType] = toObject | ||
} | ||
return oc | ||
} | ||
|
||
func (oc *ObjectConverters) ToObject(vm *VM, v any) (Object, error) { | ||
typ := reflect.TypeOf(v) | ||
for typ.Kind() == reflect.Ptr { | ||
typ = typ.Elem() | ||
} | ||
|
||
if h := oc.ToObjectHandlers[typ]; h != nil { | ||
return h(vm, v) | ||
} | ||
|
||
return ToObject(v) | ||
} | ||
|
||
func (oc *ObjectConverters) ToInterface(vm *VM, v Object) any { | ||
if h := oc.ToGoHandlers[v.Type()]; h != nil { | ||
return h(vm, v) | ||
} | ||
return ToInterface(v) | ||
} | ||
|
||
func (vm *VM) ToObject(v any) (Object, error) { | ||
return vm.ObjectConverters.ToObject(vm, v) | ||
} | ||
|
||
func (vm *VM) ToInterface(v Object) any { | ||
return vm.ObjectConverters.ToInterface(vm, v) | ||
} |
Oops, something went wrong.