Customizing the Interpreter's Implementation

Pnuts scripts are typically compiled into Java bytecode when they are executed. The way of executing scripts can be customized by setting an attribute (pnutsImpl) of the Context. For example, Pnuts scripts can be executed without compiling, or compiled once and cached for subsequent executions.

pnuts.lang.Implementation Object

pnuts.lang.Implementation interface defines the interface of script interpreter's implementation. An instance of the Implementation interface is associated with a Context. A pair of methods Context.setImplementation() and Context.getImplementation() are the settter/getter method respectively.

Predefined Implementation Classes

The following classes are the implementation classes of the interface provided in the Pnuts distribution.

pnuts.lang.PnutsImpl
The AST interpreter.
pnuts.compiler.CompilerPnutsImpl
The on-the-fly Compiler. The default class for pnuts command.
pnuts.ext.CachedPnutsImpl
Mixed mode, which caches compiled (or parsed) scripts and reuse them. The default class for Pnuts Servlet.
pnuts.security.SecurePnutsImpl
Secure mode, which executes scripts safely using Java2 Security API
pnuts.security.JAASPnutsImpl
Secure mode, which executes scripts safely using JAAS.

See Performance Hints on how to choose Implementation class in terms of efficiency.

import pnuts.lang.*;
import pnuts.ext.*;

Context context = new Context();
context.setImplementation(new CachedPnutsImpl());
Pnuts.loadFile(fileName, context);

SecurePnutsImpl and JAASPnutsImpl are wrapper classes of other Implementation classes to add securty functionalities. See Building Secure Scripting Environment for more detail.

context.setImplementation(new SecurePnutsImpl(new CompilerPnutsImpl()));
Pnuts.load(new URL("..."), context);

The pnuts.lang.defaultPnutsImpl Property

If the system property, pnuts.lang.defaultPnutsImpl, is a class that implements Implementation interface when Pnuts API is first loaded, an instance of the specified class is create with the default constructor and used as the default Implementation.

Since SecurePnutsImpl and JAASPnutsImpl do not have the default constructor, they can not be specified for the property.

To see what Implementation is being used, call Context.getImplementation().

> getContext().getImplementation()
pnuts.compiler.CompilerPnutsImpl@1764be1
>