The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

JE::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, sub { ... };
  $f = new_method JE::Object::Function $scope, sub { ... };

  # 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(@args);
  $f->construct(@args); # if this is a constructor function
  $f->apply($obj, @args);

DESCRIPTION

All JavaScript functions are instances of this class.

OBJECT CREATION

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.)

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).

new JE::Object::Function $scope_or_global, @argnames, $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 arguments.

$function is one of

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

If $function is a coderef (Perl subroutine), the arguments passed to it when the function is invoked will be

  0) a scope chain object (see L<JE::Scope>)
  1) the invocant (the object through which the function is invoked)
  2..) the arguments

The function object itself can be accessed via $_[0]->var('arguments')->prop('callee') (though I admit that is a bit much to type).

new JE::Object::Function $scope_or_global, sub { ... };

In this case, the subroutine will be called with the arguments the function is invoked with, but with no invocant or scope chain.

new_method JE::Object::Function $scope_or_global, sub { ... };

(not yet implemented)

If you are writing a method in Perl and are not interested in the scope, use this method. The first argument to the sub will be the invocant. The remaining arguments will be those with which JavaScript called the function.

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):

name

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

scope

A global object or scope chain object. If this is omitted, the body of the function (the function element) must be a Perl coderef, and not a string of JS code, and it must return a JavaScript value or a simple scalar (not an unblessed array or hash ref).

length

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 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 new keyword, or from Perl via the call method.

function_args

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
  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_args is omitted, the first argument will be the scope chain, followed by the invocant, and then the arguments (as individual elements in @_, not as an array ref).

constructor

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 Perl.

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. 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. If constructor_args is omitted, but constructor is not, the arg list will be set to [ qw( scope args ) ].

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.

no_proto

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

METHODS

new JE::Object::Function
new_method JE::Object::Function (not yet implemented)

See "OBJECT CREATION".

call ( @args )

Calls a function with the given arguments. The invocant (the 'this' value) will be the global object. This is just a wrapper around apply.

construct

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 )

Calls the function with $obj as the invocant and @args as the args.

typeof

This returns the string 'function'.

class

This returns the string 'Function'.

value

Not yet implemented.

SEE ALSO

JE
JE::Object
JE::Types
JE::Scope
JE::LValue

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 587:

You forgot a '=back' before '=head1'

Around line 589:

=over without closing =back