Wren Plugin for Xojo

VM.CallMethod Method

Calls a method that has been made ready in a Method handle, taking class and parameters from current slot stack.

CallMethod(
   methodHandle as EinhugurWren.Handle) as EinhugurWren.InterpretResult

Parameters

methodHandle
Handle to the method.

Returns

EinhugurWren.InterpretResult

Remarks


using EinhugurWren

// Both parameters in the Configuration constructor are optional
var conf as new Configuration(addressof WriteCallback, addressof ErrorCallback)


var vm as new VM(conf)

var script as string = _
   "class MyClass {" + EndOfLine + _
   "static myMethod() {" + EndOfLine + _
   "System.print(""Hello from first method"")" + EndOfLine + _
   "}" + EndOfLine + _
   "static myMethod2(x) {" + EndOfLine + _
   "System.print(""Hello from second method "" + x.toString)" + EndOfLine + _
   "}" + EndOfLine + _
   "}" + EndOfLine

var result as InterpretResult = vm.Interpret("main", script)

if result = InterpretResult.SUCCESS then
    // We only load the class handle once
    var classHandle as Handle = vm.GetClassHandle("main", "MyClass")
   
    // Call method with no parameters.
    var methodHandle as Handle = vm.CreateCallHandle("myMethod()") // Method can be called many times with this handle
   
    vm.EnsureSlots(1) // Only one slot since we got no parameters
    vm.SlotHandle(0) = classHandle // Class handle always goes in first slot
   
    var callResult as InterpretResult = vm.CallMethod(methodHandle)
   
    if callResult <> InterpretResult.SUCCESS then
       MessageBox "We got error"
    end if
   
    // Lets call 2nd method with parameter.
    var methodHandle2 as Handle = vm.CreateCallHandle("myMethod2(_)") // Method can be called many times with this handle
   
    vm.EnsureSlots(2) // Two slots, since we got 1 parameter.
    vm.SlotHandle(0) = classHandle // Class handle always goes in first slot
    vm.SlotDouble(1) = 123 // We pass in 123 as parameter
   
    callResult = vm.CallMethod(methodHandle2)
   
    if callResult <> InterpretResult.SUCCESS then
       MessageBox "We got error"
    end if
end if

See Also

VM Class