Provided by: libje-perl_0.066-1_all bug


       JE::Object::Function - JavaScript function class


         use JE::Object::Function;

         # simple constructors:

         $f = new JE::Object::Function $scope, @argnames, $function;
         $f = new JE::Object::Function $scope, $function;

         # constructor that lets you do anything:

         $f = new JE::Object::Function {
                 name             => $name,
                 scope            => $scope,
                 length           => $number_of_args,
                 argnames         => [ @argnames ],
                 function         => $function,
                 function_args    => [ $arglist ],
                 constructor      => sub { ... },
                 constructor_args => [ $arglist ],
                 downgrade        => 0,

         $f->call_with($obj, @args);


       All JavaScript functions are instances of this class. If you want to call a JavaScript
       function from Perl, just treat is as a coderef ("$f->()") or use the "call_with" method
       ("$f->call_with($obj, @args)") if you want to specify the invocant (the 'this' value).


       new Creates and returns a new function (see the next few items for its usage).  The new
           function will have a "prototype" property that is an object with a "constructor"
           property that refers to the function itself.

           The return value of the function will be upgraded if necessary (see UPGRADING VALUES
           in the JE::Types man page), which is why "new" has to be given a reference to the
           global object or the scope chain. (But see also "new_function" in JE and "new_method"
           in JE.)

           A function written in Perl can return an lvalue if it wants to. Use
           "new JE::LValue($object, 'property name')" to create it. To create an lvalue that
           refers to a variable visible within the function's scope, use "$scope->var('varname')"
           (this assumes that you have shifted the scope object off @_ and called it $scope; you
           also need to call "new" with hashref syntax and specify the "function_args" [see

       new JE::Object::Function $scope_or_global, @argnames, $function;
       new JE::Object::Function $scope_or_global, $function;
           $scope_or_global is one of the following:

             - a global (JE) object
             - a scope chain (JE::Scope) object

           @argnames is a list of argument names, that JavaScript functions use to access the

           $function is one of

             - a string containing the body of the function (JavaScript code)
             - a JE::Code object
             - a coderef

       new JE::Object::Function { ... };
           This is the big fancy way of creating a function that lets you do anything.  The
           elements of the hash ref passed to "new" are as follows (they are all optional, except
           for "scope"):

               The name of the function. This is used only by "toString".

               A global object or scope chain object.

               The number of arguments expected. If this is omitted, the number of elements of
               "argnames" will be used. If that is omitted, 0 will be used.  Note that this does
               not cause the argument list to be checked. It only provides the "length" property
               (and possibly, later, an "arity" property) for inquisitive scripts to look at.

               An array ref containing the variable names that a JS function uses to access the

               A coderef, string of JS code or JE::Code object (the body of the function).

               This will be run when the function is called from JavaScript without the "new"
               keyword, or from Perl via the "call" method.

               This only applies when "function" is a code ref. "function_args" is an array ref,
               the elements being strings that indicated what arguments should be passed to the
               Perl subroutine. The strings, and what they mean, are as follows:

                 self    the function object itself
                 scope   the scope chain
                 global  the global object
                 this    the invocant
                 args    the arguments passed to the function (as individual
                 [args]  the arguments passed to the function (as an array ref)

               If "function_args" is omitted, 'args' will be assumed.

               A code ref that creates and initialises a new object. This is called when the
               "new" keyword is used in JavaScript, or when the "construct" method is used in

               If this is omitted, when "new" or "construct" is used, a new empty object will be
               created and passed to the sub specified under "function" as its 'this' value. The
               return value of the sub will be returned if it is an object; the (possibly
               modified) object originally passed to the function will be returned otherwise.

               Like "function_args", but the 'this' string does not apply. If "constructor_args"
               is omitted, the arg list will be set to "[ qw( scope args ) ]" (this might

               This is completely ignored if "constructor" is omitted.

           downgrade (not yet implemented)
               This applies only when "function" or "constructor" is a code ref. This is a
               boolean indicating whether the arguments to the function should have their "value"
               methods called automatically.; i.e., as though "map $_->value, @args" were used
               instead of @args.

               If this is set to true, the returned function will have no "prototype" property.


       new JE::Object::Function
           See "OBJECT CREATION".

       call_with ( $obj, @args )
           Calls a function with the given arguments. The $obj becomes the function's invocant.
           This method is intended for general use from the Perl side. The arguments (including
           $obj) are automatically upgraded.

       call ( @args )
           This method, intended mainly for internal use, calls a function with the given
           arguments, without upgrading them. The invocant (the 'this' value) will be the global
           object. This is just a wrapper around "apply".

           This method is very badly named and will probably be renamed in a future version. Does
           anyone have any suggestions?

           This method, likewise intended mainly for internal use, calls the constructor, if this
           function has one (functions written in JS don't have this). Otherwise, an object will
           be created and passed to the function as its invocant. The return value of the
           function will be discarded, and the object (possibly modified) will be returned

       apply ( $obj, @args )
           This method, intended mainly for internal use just like the two above, calls the
           function with $obj as the invocant and @args as the args. No upgrading occurs.

           This method is very badly named and will probably be renamed in a future version. Does
           anyone have any suggestions?

           This returns the string 'function'.

           This returns the string 'Function'.

           Not yet implemented.


       You can use a JE::Object::Function as a coderef. The sub returned simply invokes the
       "call" method, so the following are equivalent:

         $function->call( $function->global->upgrade(@args) )

       The stringification, numification, boolification, and hash dereference ops are also
       overloaded. See JE::Object, which this class inherits from.