.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "JE::Object::Function 3pm" .TH JE::Object::Function 3pm "2023-08-25" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" JE::Object::Function \- JavaScript function class .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& 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); .Ve .SH "DESCRIPTION" .IX Header "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(CW\*(C`$f\->()\*(C'\fR) or use the \f(CW\*(C`call_with\*(C'\fR method (\f(CW\*(C`$f\->call_with($obj, @args)\*(C'\fR) if you want to specify the invocant (the 'this' value). .SH "OBJECT CREATION" .IX Header "OBJECT CREATION" .IP "new" 4 .IX Item "new" Creates and returns a new function (see the next few items for its usage). The new function will have a \f(CW\*(C`prototype\*(C'\fR property that is an object with a \f(CW\*(C`constructor\*(C'\fR property that refers to the function itself. .Sp The return value of the function will be upgraded if necessary (see \&\s-1UPGRADING VALUES\s0 in the JE::Types man page), which is why \f(CW\*(C`new\*(C'\fR \fIhas\fR to be given a reference to the global object or the scope chain. (But see also \*(L"new_function\*(R" in \s-1JE\s0 and \*(L"new_method\*(R" in \s-1JE\s0.) .Sp A function written in Perl can return an lvalue if it wants to. Use \&\f(CW\*(C`new JE::LValue($object, \*(Aqproperty name\*(Aq)\*(C'\fR to create it. To create an lvalue that refers to a variable visible within the function's scope, use \&\f(CW\*(C`$scope\->var(\*(Aqvarname\*(Aq)\*(C'\fR (this assumes that you have shifted the scope object off \f(CW@_\fR and called it \f(CW$scope\fR; you also need to call \f(CW\*(C`new\*(C'\fR with hashref syntax and specify the \f(CW\*(C`function_args\*(C'\fR [see below]). .ie n .IP "new JE::Object::Function $scope_or_global, @argnames, $function;" 4 .el .IP "new JE::Object::Function \f(CW$scope_or_global\fR, \f(CW@argnames\fR, \f(CW$function\fR;" 4 .IX Item "new JE::Object::Function $scope_or_global, @argnames, $function;" .PD 0 .ie n .IP "new JE::Object::Function $scope_or_global, $function;" 4 .el .IP "new JE::Object::Function \f(CW$scope_or_global\fR, \f(CW$function\fR;" 4 .IX Item "new JE::Object::Function $scope_or_global, $function;" .PD \&\f(CW$scope_or_global\fR is one of the following: .Sp .Vb 2 \& \- a global (JE) object \& \- a scope chain (JE::Scope) object .Ve .Sp \&\f(CW@argnames\fR is a list of argument names, that JavaScript functions use to access the arguments. .Sp \&\f(CW$function\fR is one of .Sp .Vb 3 \& \- a string containing the body of the function (JavaScript code) \& \- a JE::Code object \& \- a coderef .Ve .IP "new JE::Object::Function { ... };" 4 .IX Item "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 \f(CW\*(C`new\*(C'\fR are as follows (they are all optional, except for \f(CW\*(C`scope\*(C'\fR): .RS 4 .IP "name" 4 .IX Item "name" The name of the function. This is used only by \f(CW\*(C`toString\*(C'\fR. .IP "scope" 4 .IX Item "scope" A global object or scope chain object. .IP "length" 4 .IX Item "length" The number of arguments expected. If this is omitted, the number of elements of \f(CW\*(C`argnames\*(C'\fR 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 \f(CW\*(C`length\*(C'\fR property (and possibly, later, an \f(CW\*(C`arity\*(C'\fR property) for inquisitive scripts to look at. .IP "argnames" 4 .IX Item "argnames" An array ref containing the variable names that a \s-1JS\s0 function uses to access the arguments. .IP "function" 4 .IX Item "function" A coderef, string of \s-1JS\s0 code or JE::Code object (the body of the function). .Sp This will be run when the function is called from JavaScript without the \&\f(CW\*(C`new\*(C'\fR keyword, or from Perl via the \f(CW\*(C`call\*(C'\fR method. .IP "function_args" 4 .IX Item "function_args" This only applies when \f(CW\*(C`function\*(C'\fR is a code ref. \f(CW\*(C`function_args\*(C'\fR 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: .Sp .Vb 7 \& 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) .Ve .Sp If \f(CW\*(C`function_args\*(C'\fR is omitted, 'args' will be assumed. .IP "constructor" 4 .IX Item "constructor" A code ref that creates and initialises a new object. This is called when the \f(CW\*(C`new\*(C'\fR keyword is used in JavaScript, or when the \f(CW\*(C`construct\*(C'\fR method is used in Perl. .Sp If this is omitted, when \f(CW\*(C`new\*(C'\fR or \f(CW\*(C`construct\*(C'\fR is used, a new empty object will be created and passed to the sub specified under \f(CW\*(C`function\*(C'\fR as its 'this' value. The return value of the sub will be returned \fIif\fR it is an object; the (possibly modified) object originally passed to the function will be returned otherwise. .IP "constructor_args" 4 .IX Item "constructor_args" Like \f(CW\*(C`function_args\*(C'\fR, but the \f(CW\*(Aqthis\*(Aq\fR string does not apply. If \&\f(CW\*(C`constructor_args\*(C'\fR is omitted, the arg list will be set to \&\f(CW\*(C`[ qw( scope args ) ]\*(C'\fR (\fBthis might change\fR). .Sp This is completely ignored if \f(CW\*(C`constructor\*(C'\fR is omitted. .IP "downgrade (not yet implemented)" 4 .IX Item "downgrade (not yet implemented)" This applies only when \f(CW\*(C`function\*(C'\fR or \f(CW\*(C`constructor\*(C'\fR is a code ref. This is a boolean indicating whether the arguments to the function should have their \f(CW\*(C`value\*(C'\fR methods called automatically.; i.e., as though \&\f(CW\*(C`map $_\->value, @args\*(C'\fR were used instead of \f(CW@args\fR. .IP "no_proto" 4 .IX Item "no_proto" If this is set to true, the returned function will have no \f(CW\*(C`prototype\*(C'\fR property. .RE .RS 4 .RE .SH "METHODS" .IX Header "METHODS" .IP "new JE::Object::Function" 4 .IX Item "new JE::Object::Function" See \*(L"\s-1OBJECT CREATION\*(R"\s0. .ie n .IP "call_with ( $obj, @args )" 4 .el .IP "call_with ( \f(CW$obj\fR, \f(CW@args\fR )" 4 .IX Item "call_with ( $obj, @args )" Calls a function with the given arguments. The \f(CW$obj\fR becomes the function's invocant. This method is intended for general use from the Perl side. The arguments (including \f(CW$obj\fR) are automatically upgraded. .ie n .IP "call ( @args )" 4 .el .IP "call ( \f(CW@args\fR )" 4 .IX Item "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 \f(CW\*(C`apply\*(C'\fR. .Sp This method is very badly named and will probably be renamed in a future version. Does anyone have any suggestions? .IP "construct" 4 .IX Item "construct" This method, likewise intended mainly for internal use, calls the constructor, if this function has one (functions written in \s-1JS\s0 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. .ie n .IP "apply ( $obj, @args )" 4 .el .IP "apply ( \f(CW$obj\fR, \f(CW@args\fR )" 4 .IX Item "apply ( $obj, @args )" This method, intended mainly for internal use just like the two above, calls the function with \f(CW$obj\fR as the invocant and \f(CW@args\fR as the args. No upgrading occurs. .Sp This method is very badly named and will probably be renamed in a future version. Does anyone have any suggestions? .IP "typeof" 4 .IX Item "typeof" This returns the string 'function'. .IP "class" 4 .IX Item "class" This returns the string 'Function'. .IP "value" 4 .IX Item "value" Not yet implemented. .SH "OVERLOADING" .IX Header "OVERLOADING" You can use a JE::Object::Function as a coderef. The sub returned simply invokes the \f(CW\*(C`call\*(C'\fR method, so the following are equivalent: .PP .Vb 2 \& $function\->call( $function\->global\->upgrade(@args) ) \& $function\->(@args) .Ve .PP The stringification, numification, boolification, and hash dereference ops are also overloaded. See JE::Object, which this class inherits from. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\s-1JE\s0" 4 .IX Item "JE" .PD 0 .IP "JE::Object" 4 .IX Item "JE::Object" .IP "JE::Types" 4 .IX Item "JE::Types" .IP "JE::Scope" 4 .IX Item "JE::Scope" .IP "JE::LValue" 4 .IX Item "JE::LValue"