File: invoke.ss

package info (click to toggle)
opensurge 0.6.1.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 47,404 kB
  • sloc: ansic: 62,066; sh: 739; makefile: 193; java: 110; xml: 66; javascript: 53
file content (56 lines) | stat: -rw-r--r-- 2,328 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// -----------------------------------------------------------------------------
// File: invoke.ss
// Description: a function object that invokes a function of another object
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------

//
// Invoke is a function object that invokes a function of another object. This
// is particularly useful when combined with a FunctionEvent (see below).
//
// Arguments:
// - obj: object. The target object.
// - method: string. The name of the function to be invoked.
// - params: array. The parameters to be passed to the function. It may be empty.
//
// Example:
//
// using SurgeEngine.Events.FunctionEvent;
// using SurgeEngine.Web;
// ...
// fn = FunctionEvent("Invoke")
//      .withArgument(Web)
//      .withArgument("launchURL")
//      .withArgument([ "http://opensurge2d.org" ]);
//
// Calling fn() is equivalent to calling Web.launchURL("http://opensurge2d.org").
// The difference is that fn may be passed as a parameter to other functions!
// This technique lets you store a function call in a variable. In addition,
// you can wrap similar function events in an EventList to store multiple calls.
//
// Note that events do not return a value, whereas Invoke returns the returned
// value of the invoked function, or null on error. A wrapper can be created to
// get the returned value of the invoked function.
//

object "Invoke"
{
    fun call(obj, method, params)
    {
        if(obj === null)
            Console.print("Invoke: target object is null");
        else if(typeof obj != "object")
            Console.print("Invoke: " + obj.toString() + " is not an object");
        else if(!obj.hasFunction(method))
            Console.print("Invoke: " + obj.__name + "." + method + "() doesn't exist");
        else if(typeof params != "object" || params.__name != "Array")
            Console.print("Invoke: an array of parameters is expected to call " + obj.__name + "." + method + "()");
        else if(obj.__arity(method) != params.length)
            Console.print("Invoke: " + obj.__name + "." + method + "() expects " + obj.__arity(method) + " parameters, not " + params.length);
        else
            return obj.__invoke(method, params);

        return null;
    }
}