pnuts.lang
Class PnutsFunction

java.lang.Object
  extended by pnuts.lang.Runtime
      extended by pnuts.lang.PnutsFunction
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Callable, Executable

public class PnutsFunction
extends Runtime
implements Callable, java.lang.Cloneable, java.io.Serializable

A PnutsFunction represents a group of Pnuts functions with a same name.

This class is serializable, whether the function is compiled or not. When an PnutsFunction object is serialized, the function definitions are written to the object stream, along with its attributes such as configuration, import environment, current package, and module list. Note that the current package is deeply copied, but the module list are written as an array of module names.

When the function is deserialized, the function definition is restored from the function definition read from the object stream. If the function had been compiled, the script is compiled. If modules referenced by the function are not used in the current context, the module is initialized for the function in such a way that it does not affect the current context.

On AST interpreter, nested functions with lexical scope can be serialized/deserialized. But with bytecode compiler, only top-level functions can be serialized/deserialized.

Serialized objects of this class will not be compatible with future releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class pnuts.lang.Runtime
Runtime.Accessor, Runtime.Break, Runtime.Continue, Runtime.FunctionSerializer, Runtime.TypeMap
 
Field Summary
static PnutsFunction AUTOLOAD
           
static PnutsFunction CATCH
           
static PnutsFunction CLASS
           
static PnutsFunction DEFINED
           
static PnutsFunction EVAL
           
static PnutsFunction GET_CONTEXT
           
static PnutsFunction IMPORT
           
static PnutsFunction LOAD
           
static PnutsFunction LOAD_FILE
           
protected  java.lang.String name
          the name
static PnutsFunction PACKAGE
           
protected  Package pkg
           
static PnutsFunction QUIT
           
static PnutsFunction REQUIRE
           
static PnutsFunction THROW
           
static PnutsFunction UNUSE
           
static PnutsFunction USE
           
 
Fields inherited from class pnuts.lang.Runtime
BOOLEAN_SYMBOL, BYTE_SYMBOL, CHAR_SYMBOL, CLONE, DOUBLE_SYMBOL, EXCEPTOIN_FIELD_SYMBOL, FLOAT_SYMBOL, INT_SYMBOL, LONG_SYMBOL, SHORT_SYMBOL, VOID_SYMBOL
 
Constructor Summary
protected PnutsFunction()
           
protected PnutsFunction(java.lang.String name)
          Constructor
protected PnutsFunction(java.lang.String name, PnutsFunction parent)
          Constructor
 
Method Summary
 java.lang.Object accept(int narg, Visitor visitor, Context context)
           
protected  void added(int narg)
          This method is called when a Function object is registered to this object.
 java.lang.Object call(java.lang.Object[] args, Context context)
          Call a function in "context" with arguments "args".
static java.lang.Object call(java.lang.String name, java.lang.Object[] args, Context context)
          call a function "name" in "context" with arguments "args"
 java.lang.Object clone()
           
 boolean defined(int narg)
          Check if the function with narg parameter is defined
protected  java.util.Enumeration elements()
           
protected  java.lang.Object exec(java.lang.Object[] args, Context context)
          Call a function in "context" with arguments "args".
protected static java.lang.Object exec(PnutsFunction func, java.lang.Object[] args, Context context)
           
 Function get(int narg)
           
 java.lang.String[] getImportEnv(int narg)
           
 java.lang.String getName()
           
 Package getPackage()
          Deprecated.  
 boolean isBuiltin()
           
 void setPackage(Package pkg)
           
 java.lang.String toString()
           
protected  void undefined(java.lang.Object[] args, Context context)
           
 java.lang.String unparse(int narg)
          Retrieve the symbolic definition of the function.
 
Methods inherited from class pnuts.lang.Runtime
_callConstructor, _callMethod, add, add, add1, add1, addImport, addStaticMembers, and, and, applyGenerator, arraydim, arrayType, call, call, callConstructor, callFunction, callMethod, cast, catchException, checkException, checkException, compareObjects, compareTo, compareTo, createList, createMap, defineTopLevelFunction, defineUnboundFunction, divide, divide, eq, eq, escape, exec, execute, fileToURL, findCallableMethod, format, format, ge, ge, getArrayLength, getBeanProperty, getBeanProperty, getBeanProperty, getBeanPropertyType, getBeginColumn, getBeginLine, getBottomType, getCompiledScript, getConstructors, getElement, getElementAt, getEndLine, getField, getField, getFunction, getFunction, getFunctions, getMessage, getMethods, getProperty, getRange, getRuntime, getScriptReader, getScriptSource, getScriptURL, getStaticField, getThreadContext, gt, gt, isArray, isGenerator, jump, le, le, loadNode, lt, lt, makeArray, matchType, mod, mod, multiply, multiply, ne, ne, negate, negate, newInstance, not, not, or, or, parseChar, parseFloat, parseInt, parseString, primitive, printError, putField, putStaticField, quantity, replaceChar, run, saveNode, setBeanProperty, setBeanProperty, setBeanProperty, setElement, setExitHook, setLine, setLine, setPackage, setRange, setThreadContext, shiftArithmetic, shiftArithmetic, shiftLeft, shiftLeft, shiftRight, shiftRight, subtract, subtract, subtract1, subtract1, throwException, toBoolean, toEnumeration, transform, transform, unparse, xor, xor
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected java.lang.String name
the name


GET_CONTEXT

public static final PnutsFunction GET_CONTEXT

PACKAGE

public static final PnutsFunction PACKAGE

IMPORT

public static final PnutsFunction IMPORT

CATCH

public static final PnutsFunction CATCH

THROW

public static final PnutsFunction THROW

EVAL

public static final PnutsFunction EVAL

LOAD_FILE

public static final PnutsFunction LOAD_FILE

LOAD

public static final PnutsFunction LOAD

AUTOLOAD

public static final PnutsFunction AUTOLOAD

QUIT

public static final PnutsFunction QUIT

DEFINED

public static final PnutsFunction DEFINED

USE

public static final PnutsFunction USE

UNUSE

public static final PnutsFunction UNUSE

CLASS

public static final PnutsFunction CLASS

REQUIRE

public static final PnutsFunction REQUIRE

pkg

protected transient Package pkg
Constructor Detail

PnutsFunction

protected PnutsFunction()

PnutsFunction

protected PnutsFunction(java.lang.String name)
Constructor

Parameters:
name - the name of the function

PnutsFunction

protected PnutsFunction(java.lang.String name,
                        PnutsFunction parent)
Constructor

Parameters:
name - the name of the function
parent - the parent function
Method Detail

added

protected void added(int narg)
This method is called when a Function object is registered to this object. This method just returns. Subclass can override this as a hook method.

Parameters:
narg - the number of parameters

get

public final Function get(int narg)

defined

public boolean defined(int narg)
Check if the function with narg parameter is defined

Parameters:
narg - the number of paramters.
Returns:
true if a function with narg, otherwise false

getName

public java.lang.String getName()
Returns:
the name of functions

call

public final java.lang.Object call(java.lang.Object[] args,
                                   Context context)
Call a function in "context" with arguments "args". Increments the counter for Pnuts.evalDepth() during the execution.

Specified by:
call in interface Callable
Parameters:
args - the arguments
context - the context in which the object is called
Returns:
the result of the call

exec

protected java.lang.Object exec(java.lang.Object[] args,
                                Context context)
Call a function in "context" with arguments "args". Subclasses of this class should override this method.

Returns:
the result of the call

undefined

protected void undefined(java.lang.Object[] args,
                         Context context)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

call

public static java.lang.Object call(java.lang.String name,
                                    java.lang.Object[] args,
                                    Context context)
call a function "name" in "context" with arguments "args"


exec

protected static java.lang.Object exec(PnutsFunction func,
                                       java.lang.Object[] args,
                                       Context context)

unparse

public java.lang.String unparse(int narg)
Retrieve the symbolic definition of the function.

Parameters:
narg - the number of paramters. -1 means a arbitrary length parameter.
Returns:
the function definition

getPackage

public Package getPackage()
Deprecated. 

Returns:
Package in which the function is defined

setPackage

public void setPackage(Package pkg)

getImportEnv

public java.lang.String[] getImportEnv(int narg)
Parameters:
narg - the number of paramters. -1 means a arbitrary length parameter.
Returns:
imports of the function (array of Class or String)

isBuiltin

public boolean isBuiltin()

accept

public java.lang.Object accept(int narg,
                               Visitor visitor,
                               Context context)
Parameters:
narg - the number of paramters. -1 means a arbitrary length parameter.

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

elements

protected java.util.Enumeration elements()