NAME
JE::Object::Function - JavaScript function class
SYNOPSIS
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->(@args);
$f->call_with($obj, @args);
DESCRIPTION
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).
OBJECT CREATION
- new
-
Creates and returns a new function (see the next few items for its usage). The new function will have a
prototypeproperty that is an object with aconstructorproperty 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
newhas 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 callnewwith hashref syntax and specify thefunction_args[see below]). - new JE::Object::Function $scope_or_global, @argnames, $function;
- new JE::Object::Function $scope_or_global, $function;
-
$scope_or_globalis one of the following:- a global (JE) object - a scope chain (JE::Scope) object@argnamesis a list of argument names, that JavaScript functions use to access the arguments.$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
neware as follows (they are all optional, except forscope):- name
-
The name of the function. This is used only by
toString. - scope
-
A global object or scope chain object.
- length
-
The number of arguments expected. If this is omitted, the number of elements of
argnameswill 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 thelengthproperty (and possibly, later, anarityproperty) for inquisitive scripts to look at. - argnames
-
An array ref containing the variable names that a JS function uses to access the arguments.
- function
-
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
newkeyword, or from Perl via thecallmethod. - function_args
-
This only applies when
functionis a code ref.function_argsis 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 arguments) [args] the arguments passed to the function (as an array ref)If
function_argsis omitted, 'args' will be assumed. - constructor
-
A code ref that creates and initialises a new object. This is called when the
newkeyword is used in JavaScript, or when theconstructmethod is used in Perl.If this is omitted, when
neworconstructis used, a new empty object will be created and passed to the sub specified underfunctionas 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. - constructor_args
-
Like
function_args, but the'this'string does not apply. Ifconstructor_argsis omitted, the arg list will be set to[ qw( scope args ) ](this might change).This is completely ignored if
constructoris omitted. - downgrade (not yet implemented)
-
This applies only when
functionorconstructoris a code ref. This is a boolean indicating whether the arguments to the function should have theirvaluemethods called automatically.; i.e., as thoughmap $_->value, @argswere used instead of@args. - no_proto
-
If this is set to true, the returned function will have no
prototypeproperty.
METHODS
- new JE::Object::Function
-
See "OBJECT CREATION".
- call_with ( $obj, @args )
-
Calls a function with the given arguments. The
$objbecomes 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?
- construct
-
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 instead.
- 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?
- typeof
-
This returns the string 'function'.
- class
-
This returns the string 'Function'.
- value
-
Not yet implemented.
OVERLOADING
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) )
$function->(@args)
The stringification, numification, boolification, and hash dereference ops are also overloaded. See JE::Object, which this class inherits from.