Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support struct functions with ToValue #62

Closed
shoriwe opened this issue Nov 6, 2022 · 1 comment · Fixed by #64
Closed

Support struct functions with ToValue #62

shoriwe opened this issue Nov 6, 2022 · 1 comment · Fixed by #64
Labels
feature help wanted Extra attention is needed

Comments

@shoriwe
Copy link
Owner

shoriwe commented Nov 6, 2022

Is your feature request related to a problem? Please describe.

Map Struct function to Plasma Objects

Describe the solution you'd like

Using reflection, also scan for the functions of an struct an map them to the plasma object

@shoriwe shoriwe added the feature label Nov 6, 2022
@shoriwe shoriwe self-assigned this Nov 6, 2022
@shoriwe shoriwe added the help wanted Extra attention is needed label Nov 6, 2022
@shoriwe
Copy link
Owner Author

shoriwe commented Nov 6, 2022

The basic idea is that type functions don't directly compile as they look like, the first argument is always the pointer or value to the type that implements it. For example:

type MyType struct{}

func (mt *MyType) SayHello(arg1 int) {}

// Will compile to something like...

func MyType_SayHello(mt *MyType, arg1 int) {}

Having this in consideration, the solution will need some kind of callback since the current implementation only has Value assignment mapping to Plasma Objects, a conceptual workaround could be:

// This is passed to the VM as plasma.ToValue(objVirtualTable, newMemberCaller(self, function))
type memberCaller func(arguments ...any) []any

func newMemberCaller(self, function reflect.Value) memberCaller {
    return func(arguments ...any) []any {
        callArguments := make([]reflect.Value, 0, function.Type().NumIn())
        callArguments = append(callArguments, self) // Add self
        for index := 0; index < function.Type().NumIn(); index++ {
            in := function.Type().In(index)
            asValue := reflect.ValueOf(arguments[index])
            asValue = asValue.Convert(in)
            callArguments = append(callArguments, asValue)
        }
        // Some corrections need to be made to also support Variadic function calls
        asValuesResults := function.Call(callArguments)
        result := make([]any, 0, len(asValueResults))
        for _, v := range asValuesResults {
            result = append(result, v.Interface())
        }
        return result
    }
}

@shoriwe shoriwe removed their assignment Nov 6, 2022
@shoriwe shoriwe linked a pull request Nov 6, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant