Module: nashornHost

JavaScript functions for the Nashorn host, which uses Java's Nashorn JavaScript engine. This host supports version 1 accessors.

This host is almost entirely independent of Ptolemy II, except for some utility functions in FileUtilities (which could easily be factored out). Also, modules that are required by accessors are loaded from $PTII/ptolemy/actor/lib/jjs, and some of those modules may have dependencies on Ptolemy II.

To invoke this, the accessors repository has a script in accessors/web/hosts/nashorn called nashornAccessorHost. Execute that script with command-line arguments (e.g. a composite accessor to instantiate and initialize).

Version:
  • $$Id$$
Since:
  • Ptolemy II 11.0
Author:
  • Edward A. Lee, Contributor: Christopher Brooks
Source:

Members

(inner) _accessorClasspath

An array that gives the search path for accessors to be extended.

Source:

(inner) _accessorPath

An array that gives the search path for accessors to be extended.

Source:

(inner) _accessorRoot

A string giving the full path to the root directory for installed accessors.

Source:

(inner) _moduleClasspath

An array that gives the search path for modules to be required relative to the classpath.

Source:

(inner) _modulePath

An array that gives the search path for modules to be required.

Source:

(inner) _moduleRoot

A string giving the full path to the root directory for installed modules.

Source:

(inner) _testAccessors

A string giving the full path to the root directory for test accessors.

Source:

(inner) require

Require the named module. This function imports modules formatted according to the CommonJS standard.

If the name begins with './' or '/', then it is assumed to specify a file or directory on the local disk. If it is a file, the '.js' suffix may be optionally omitted. If it is a directory, then this function will look for a package.json file in that directory and load the file specified by the 'main' property the JSON object defined in that file. If there is no package.json file, then it will load an 'index.js' file, if there is one.

If the name does not begin with './' or '/', then it is assumed to specify a module installed in this accessor host.

In both cases, this function returns an object that includes as properties any properties that have been added to the 'exports' property. For example, to export a function, the module JavaScript file could define the function as follows:

  exports.myFunction = function() {...};

Alternatively, the module JavaScript file can explicitly define the exports object as follows:

  var myFunction = function() {...};
  module.exports = {
      myFunction : myFunction
  };

This implementation uses the requires() function implemented by Walter Higgins, found here: https://github.com/walterhiggins/commonjs-modules-javax-script.

Source:
See:

(inner) util

Require the named accessor. This is a version of require() that looks in a different place for accessors.

Source:
See:

Methods

(inner) alert(message)

Print a message to the console. NOTE: This function is not required by the accessor specification, so accessors should not rely on it being present.

Parameters:
Name Type Description
message

The message

Source:

(inner) clearInterval(handle)

Clear an interval timer with the specified handle.

Parameters:
Name Type Description
handle

The handle.

Source:
See:
  • setInterval().

(inner) clearTimeout(handle)

Clear a timeout with the specified handle.

Parameters:
Name Type Description
handle

The handle.

Source:
See:
  • setTimeout().

(inner) currentTime()

Return the current time as a number (in seconds).

Source:
Returns:

The current time.

(inner) error(message)

Report an error by printing using console.error().

Parameters:
Name Type Description
message

The message for the exception.

Source:

(inner) getAccessorCode(name)

Return the source code for an accessor from its fully qualified name. This will throw an exception if there is no such accessor on the accessor search path.

Parameters:
Name Type Description
name

Fully qualified accessor name, e.g. 'net/REST'.

Source:

(inner) getHostName()

Return the name of this host.

Return the string "Nashorn".

Source:
Returns:

In nashornHost.js, return "Nashorn".

(inner) getResource(path, options, callback)

Get a resource, which may be a relative file name or a URL, and return the value of the resource as a string.

Implementations of this function may restrict the locations from which resources can be retrieved. This implementation restricts relative file names to be in the same directory where the swarmlet model is located or in a subdirectory, or if the resource begins with "$CLASSPATH/", to the classpath of the current Java process.

If the accessor is not restricted, the $KEYSTORE is resolved to $HOME/.ptKeystore.

The options parameter may have the following values:

  • If the type of the options parameter is a Number, then it is assumed to be the timeout in milliseconds.
  • If the type of the options parameter is a String, then it is assumed to be the encoding, for example "UTF-8". If the value is "Raw" or "raw" then the data is returned as an unsigned array of bytes. The default encoding is the default encoding of the system. In CapeCode, the default encoding is returned by Charset.defaultCharset().
  • If the type of the options parameter is an Object, then it may have the following fields: encoding {string} The encoding of the file, see above for values. timeout {number} The timeout in milliseconds.

    If the callback parameter is not present, then getResource() will be synchronous read like Node.js's fs.readFileSync(). If the callback argument is present, then getResource() will be asynchronous like fs.readFile().

Parameters:
Name Type Description
path string

The URI or path to the resource

options

The options for reading the resource

callback

The callback function. The first argument is the error, if any, the second argument is the data, if any.

Source:

(inner) instantiate(accessorName, accessorClass)

Instantiate and return an accessor. If there is no 'actor' variable in scope, then this method assumes there is nothing in charge of execution of this accessor and therefore creates an orchestrator for it and starts an event loop. This will throw an exception if there is no such accessor class on the accessor search path.

Parameters:
Name Type Description
accessorName

The name to give to the instance.

accessorClass

Fully qualified accessor class name, e.g. 'net/REST'.

Source:

(inner) instantiateTopLevel(accessorName, accessorClass)

Instantiate and return a top-level accessor. For now, this is the same as instantiate().

Parameters:
Name Type Description
accessorName

The name to give to the instance.

accessorClass

Fully qualified accessor class name, e.g. 'net/REST'.

Source:

(inner) processCommandLineArguments(argv)

Evaluate command-line arguments by first converting the arguments from a Java array to a JavaScript array, and then invoking main() in commonHost.js.

Parameters:
Name Type Description
argv

Command-line arguments.

Source:
Returns:

True if any standalone accessors with active event loops were instantiated.

(inner) setInterval(func, milliseconds)

Set a timeout to call the specified function after the specified time and repeatedly at multiples of that time.

Return a handle to use in clearInterval(). If there are additional arguments beyond the first two, then those arguments will be passed to the function when it is invoked. This implementation uses fireAt() of the director in charge of the host JavaScript actor in Ptolemy II. Hence, actors that use this should be used with a director that respects fireAt(), such as DE. If the director has synchronizeToRealTime set to true, then it will approximate real-time behavior reasonably closely. Otherwise, the timeout will only be simulated. Either way, the timing is much more precise and well-defined than usual for JavaScript environments. If two actors specify the same timeout time in, say, their initialize() function, then they will be invoked at the same model time, and their outputs will be simultaneous. Any downstream actor will see them simultaneously.

Note with this implementation, it is not necessary to call clearInterval() in the actor's wrapup() function. Nevertheless, it is a good idea to do that in an accessor since other accessor hosts may not work the same way.

Parameters:
Name Type Description
func

The callback function.

milliseconds

The interval in milliseconds.

Source:

(inner) setTimeout(func, milliseconds)

Set a timeout to call the specified function after the specified time. Return a handle to use in clearTimeout(). If there are additional arguments beyond the first two, then those arguments will be passed to the function when it is invoked. This implementation uses fireAt() of the director in charge of the host JavaScript actor in Ptolemy II. Hence, actors that use this should be used with a director that respects fireAt(), such as DE. If the director has synchronizeToRealTime set to true, then it will approximate real-time behavior reasonably closely. Otherwise, the timeout will only be simulated. Either way, the timing is much more precise and well-defined than usual for JavaScript environments. If two actors specify the same timeout time in, say, their initialize() function, then they will be invoked at the same model time, and their outputs will be simultaneous. Any downstream actor will see them simultaneously.

Note with this implementation, it is not necessary to call clearTimeout() in the actor's wrapup() function.

Parameters:
Name Type Description
func

The callback function.

milliseconds

The interval in milliseconds.

Source: