---
name: "JavaScript::Context"
section: "3pm"
platform: "linux"
package: "libjavascript-perl"
version: "1.16-3"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/JavaScript::Context"
---

# JavaScript::Context(3pm)

An object in which we can execute JavaScript

## NAME

JavaScript::Context - An object in which we can execute JavaScript

## SYNOPSIS

```
  use JavaScript;
  # Create a runtime and a context
  my $rt = JavaScript::Runtime->new();
  my $cx = $rt->create_context();
  # Add a function which we can call from JavaScript
  $cx->bind_function(print => sub { print @_; });
  my $result = $cx->eval($source);
```

## INTERFACE

### INSTANCE METHODS

- bind\_class ( %args )

  Defines a new class that can be used from JavaScript in the contet.

  It expects the following arguments

  - name

    The name of the class in JavaScript.

    ```
      name => "MyPackage",
    ```

  - constructor

    A reference to a subroutine that returns the Perl object that represents the JavaScript object. If omitted a default constructor will be supplied that calls the method `"new"` on the defined *package* (or *name* if no package is defined).

    ```
      constructor => sub { MyPackage->new(@_); },
    ```

  - package

    The name of the Perl package that represents this class. It will be passed as first argument to any class methods and also used in the default constructor.

    ```
      package => "My::Package",
    ```

  - methods (fs)

    A hash reference of methods that we define for instances of the class. In JavaScript this would be `"o = new MyClass(); o.method()"`.

    The key is used as the name of the function and the value should be either a reference to a subroutine or the name of the Perl subroutine to call.

    ```
      methods => { to_string => \&My::Package::to_string,
                   random    => "randomize"
      }
    ```

  - static\_methods (static\_ps)

    Like *fs* but these are called on the class itself. In JavaScript this would be `"MyClass.method()"`.

  - properties (ps)

    A hash reference of properties that we define for instances of the class. In JavaScript this would be `"o = new MyClass(); f = o.property;"`

    The key is used as the name of the property and the value is used to specify what method to call as a get-operation and as a set-operation. These can either be specified using references to subroutines or name of subroutines. If the getter is undefined the property will be write-only and if the setter is undefined the property will be read-only. You can specify the getter/setter using either an array reference, `"[\&MyClass::get_property, \&MyClass::set_property]"`, a string, `"MyClass::set_property MyClass::get_property"` or a hash reference, `"{ getter ="` "MyClass::get\_property", setter =\> "MyClass::set\_property" }\>.

    ```
      ps => { length => [qw(get_length)],
              parent => { getter => \&MyClass::get_parent, setter => \&MyClass::set_parent },
            }
    ```

  - static\_properties (static\_ps)

    Like *ps* but these are defined on the class itself. In JavaScript this would be `"f = MyClass.property"`.

  - flags

    A bitmask of attributes for the class. Valid attributes are:

    - JS\_CLASS\_NO\_INSTANCE

      Makes the class throw an exception if JavaScript tries to instansiate the class.

- bind\_function ( name =\> $name, func =\> $subroutine )

  - bind\_function ( $name =\> $subroutine )

  Defines a Perl subroutine ($subroutine\_ref) as a native function with the given *$name*. The argument `$subroutine` can either be the name of a subroutine or a reference to one.

- bind\_object ( $name =\> $object )

  Binds a Perl object to the context under a given name.

- bind\_value ( $name =\> $value )

  Defines a value with a given name and value. Trying to redefine an already existing property throws an exception.

- unbind\_value ( $name )

  Removed a property from the context or a specified object.

- call ( $name, @arguments )

  - call ( $function, @arguments )

  Calls a function with the given name *$name* or the **JavaScript::Function**-object *$function* and  passes the rest of the arguments to the JavaScript function.

- can ( $name )

  Returns true if there is a function with a given *$name*, otherwise it returns false.

- compile ( $source )

  Pre-compiles the JavaScript given in *$source* and returns a `"JavaScript::Script"`-object that can be executed over and over again. If an error occures because of a compilation error it returns undef and $@ is set.

- eval ( $source )

  Evaluates the JavaScript code given in *$source* and returns the result from the last statement.

  If there is a compilation error (such as a syntax error) or an uncaught exception is thrown in JavaScript this method returns undef and $@ is set.

- eval\_file ( $path )

  Evaluates the JavaScript code in the file specified by *$path* and returns the result from the last statement.

  If there is a compilation error (such as a syntax error) or an uncaught exception is thrown in JavaScript this method returns undef and $@ is set.

- find ( $native\_context )

  Returns the `"JavaScript::Context"`-object associated with a given native context.

- set\_pending\_exception ( $value )

  Converts the *$value* to JavaScript and sets it as the pending exception for the context.

- get\_version ( )

  Returns the runtime version of the context as a string, for exmaple `1.7` or or `"ECMAv3"`.

- set\_version ( $version )

  Sets the runtime version of the context to that specified in the string *$version*. Some features  such as `"let"` and `"yield"` might not be enabled by default and thus must be turned on by  specifying what JS version we're using.

  A list of these can be found at \<http://developer.mozilla.org/en/docs/JSVersion\> but may vary  depending on the version of your runtime.

- get\_options ( )

  Returns a list of the options currently enabled on the context.

- has\_options ( OPTION, ... )

  Tests if the options are eneabled on the context.

- toggle\_options ( OPTION, ... )

  Toggles the options on the context.

### OPTIONS

A number of options can be set on contexts. The following are understood (case-insensitive):

- strict

  Warn on dubious practice.

- xml

  ECMAScript for XML (E4X) support: parse \<!-- --\> as a token, not backward compatible with the comment-hiding hack used in HTML script tags.

- jit

  Enable JIT compilation. Requires a SpiderMonkey with TraceMonkey.

(Descriptions copied from jsapi.h and thus copyrighted under its license)
