NAME
JavaScript::SpiderMonkey - Perl interface to the JavaScript Engine
SYNOPSIS
use JavaScript::SpiderMonkey;
my $js = JavaScript::SpiderMonkey->new();
$js->init(); # Initialize engine
# Define a perl callback for a new JavaScript function
$js->function_set("print_to_perl", sub { print "@_\n"; });
# Create a new (nested) object and a property
$js->property_by_path("document.location.href");
# Execute some code
my $rc = $js->eval(q!
document.location.href = append("http://", "www.aol.com");
print_to_perl("URL is ", document.location.href);
function append(first, second) {
return first + second;
}
!);
# Get the value of a property set in JS
my $url = $js->property_get("document.location.href");
$js->destroy();
INSTALL
JavaScript::SpiderMonkey requires the mozjs60 (SpiderMonkey 60) library. On RPM-based systems such as Rocky Linux, CentOS, or Fedora, install the development package:
sudo dnf install mozjs60-devel
Then build in the standard way:
perl Makefile.PL
make
make test
make install
The build system uses pkg-config to locate the mozjs-60 headers and libraries automatically.
DESCRIPTION
JavaScript::SpiderMonkey is a Perl interface to the Mozilla SpiderMonkey JavaScript engine (mozjs60). It offers two levels of access:
This document describes [2], for [1], please check SpiderMonkey.xs.
new()
$js = JavaScript::SpiderMonkey->new() creates a new object to work with. To initialize the JS engine, call $js->init() afterwards.
$js->destroy()
$js->destroy() destroys the current JS context and frees up all associated memory. This is called automatically when the object goes out of scope, but can be called explicitly if desired.
$js->init()
$js->init() initializes the SpiderMonkey engine by creating a JS context, the global object, standard classes, and an error reporter.
Note: mozjs60 supports only one active JS context at a time. Calling init() on a new object will automatically destroy any previously active context.
$js->array_by_path($name)
Creates an object of type Array in the JS engine:
$js->array_by_path("document.form");
will first create an object with the name document (unless it exists already) and then define a property named form to it, which is an object of type Array. Therefore, in the JS code, you're going to be able define things like
document.form[0] = "value";
$js->function_set($name, $funcref, [$obj])
Binds a Perl function provided as a coderef ($funcref) to a newly created JS function named $name in JS land. It's a real function (therefore bound to the global object) if $obj is omitted. However, if $obj is ref to a JS object (retrieved via $js->object_by_path($path) or the like), the function will be a method of the specified object.
$js->function_set("write", sub { print @_ });
# write("hello"); // In JS land
$obj = $j->object_by_path("navigator");
$js->function_set("write", sub { print @_ }, $obj);
# navigator.write("hello"); // In JS land
$js->array_set_element($obj, $idx, $val)
Sets the element of the array $obj at index position $idx to the value $val. $obj is a reference to an object of type array (retrieved via $js->object_by_path($path) or the like).
$js->array_set_element_as_object($obj, $idx, $elobj)
Sets the element of the array $obj at index position $idx to the object $elobj (both $obj and $elobj have been retrieved via $js->object_by_path($path) or the like).
$js->array_get_element($obj, $idx)
Gets the value of of the element at index $idx of the object of type Array $obj.
$js->property_by_path($path, $value, [$getter], [$setter])
Sets the specified property of an object in $path to the value $value. $path is the full name of the property, including the object(s) in JS land it belongs to:
$js-E<gt>property_by_path("document.location.href", "abc");
This first creates the object document (if it doesn't exist already), then the object document.location, then attaches the property href to it and sets it to "abc".
$getter and $setter are coderefs that will be called by the JavaScript engine when the respective property's value is requested or set:
sub getter {
my($property_path, $value) = @_;
print "$property_path has value $value\n";
}
sub setter {
my($property_path, $value) = @_;
print "$property_path set to value $value\n";
}
$js->property_by_path("document.location.href", "abc",
\&getter, \&setter);
If you leave out $getter and $setter, no callbacks are going to be triggered while the property is set or queried. If you just want to specify a $setter, but no $getter, set the $getter to undef.
$js->object_by_path($path, [$newobj])
Get a pointer to an object with the path specified. Create it if it's not there yet. If $newobj is provided, the ref is used to bind the existing object to the name in $path.
$js->property_get($path)
Fetch the property specified by the $path.
my $val = $js->property_get("document.location.href");
$js->eval($code)
Runs the specified piece of <$code> in the JS engine. Afterwards, property values of objects previously defined will be available via $j->property_get($path) and the like.
my $rc = $js->eval("write('hello');");
The method returns 1 on success or undef if there was an error in JS land. In case of an error, the error text will be available in $@.
$js->set_max_branch_operations($max_branch_operations)
Set the maximum number of allowed branch operations. This protects against infinite loops and guarantees that the eval operation will terminate.
LIMITATIONS
Single instance only. mozjs60 supports only one active JS context at a time. You may create multiple
JavaScript::SpiderMonkeyobjects, but callinginit()on a new one will automatically destroy the previous context. Design your application around a single active instance.Not thread-safe. The module uses global state internally (function and property dispatch tables) and should only be used from a single thread.
AUTHORS
Mike Schilli, <m at perlmeister dot com>
Thomas Busch, <tbusch at cpan dot org> (current maintainer)
COPYRIGHT AND LICENSE
Copyright (c) 2002-2005 Mike Schilli
Copyright (c) 2006-2026 Thomas Busch
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.