oscript.data
Class BasicScope

java.lang.Object
  extended byoscript.data.Value
      extended byoscript.data.Scope
          extended byoscript.data.BasicScope
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
FunctionScope, GlobalScope, ScriptObject

public class BasicScope
extends Scope

Scope is an internal object use to represent a scope of execution. This class implements a basic scope which can have members created and accessed via lookupInScope or getMember.

Different instances of a scope that represent the same part of the syntax tree can share the hashtable that maps member name to member index. This reduces the number of hashtables that need to be created as scopes are created, to improve performance. The member index is an index into each scope instances array of members.

In cases where it is safe to allocate storage for this scope from the stack, the current StackFrame may be passed to the constructor, in which case this object will try and allocate it's member storage from the stack. It is only safe to use the stack for storage allocation in cases where this scope does not enclose a function instance, so if the scope does enclose a function declaration, null must be passed in to the constructor instead of the current stack frame.

Author:
Rob Clark (rob@ti.com)
See Also:
Serialized Form

Field Summary
protected  MemberTable members
          The table of members of the scope.
protected  Value[] mixins
          List of mixed in vars, or null if none.
protected  SymbolTable smit
          The shared member index table maps member name to an index into the members array.
 
Fields inherited from class oscript.data.Scope
previous
 
Fields inherited from class oscript.data.Value
DEBUG, MEMBER_NAMES, NULL, PARENT_TYPE_NAME, TYPE, TYPE_NAME, UNDEFINED
 
Constructor Summary
  BasicScope(Scope previous)
          Class Constructor.
  BasicScope(Scope previous, SymbolTable smit)
          Class Constructor.
protected BasicScope(Scope previous, SymbolTable smit, MemberTable members)
          Class Constructor.
 
Method Summary
 Value __getInstanceMember(int id)
           
 Value createMember(int id, int attr)
          Create a member of this object with the specified value.
 void free()
          Indicate that this scope is no longer needed
 Value getMember(int id, boolean exception)
          Get a member of this object.
protected  Value getMemberImpl(int id)
           
 Scope getSafeCopy()
          In case a scope has any resource allocated from a source which will no long be valid after a stack frame has returned (ie.
 Value getType()
          Get the type of this object.
protected  Value getTypeImpl()
          Get the type of this object.
 boolean isSafe()
           
 Value lookupInScope(int id)
          Get a member from this scope.
 void mixin(Value val)
          "mixin" the specified variable into the current scope.
protected  void populateMemberSet(java.util.Set s, boolean debugger)
          Derived classes that implement getMember(int, boolean) should also implement this.
 void reset()
          Reset this scope object.
 
Methods inherited from class oscript.data.Scope
__setJavaObject, createMember, createMember, findDesc, getCallee, getPreviousScope, getThis, lookupInScope, lookupInScope
 
Methods inherited from class oscript.data.Value
_getTypeMember, _populateTypeMemberSet, bopBitwiseAnd, bopBitwiseAndR, bopBitwiseOr, bopBitwiseOrR, bopBitwiseXor, bopBitwiseXorR, bopCast, bopCastR, bopDivide, bopDivideR, bopEquals, bopEqualsR, bopGreaterThan, bopGreaterThanOrEquals, bopGreaterThanOrEqualsR, bopGreaterThanR, bopInstanceOf, bopInstanceOfR, bopLeftShift, bopLeftShiftR, bopLessThan, bopLessThanOrEquals, bopLessThanOrEqualsR, bopLessThanR, bopLogicalAnd, bopLogicalAndR, bopLogicalOr, bopLogicalOrR, bopMinus, bopMinusR, bopMultiply, bopMultiplyR, bopNotEquals, bopNotEqualsR, bopPlus, bopPlusR, bopRemainder, bopRemainderR, bopSignedRightShift, bopSignedRightShiftR, bopUnsignedRightShift, bopUnsignedRightShiftR, callAsConstructor, callAsConstructor, callAsConstructor, callAsExtends, callAsExtends, callAsExtends, callAsFunction, callAsFunction, callAsFunction, castToBoolean, castToExactNumber, castToInexactNumber, castToJavaObject, castToString, elementAt, elementsAt, getMember, getMember, getMember, getMember, getMember, getMonitor, getTypeMember, getTypeMember, isA, length, memberSet, noSuchMember, opAssign, populateTypeMemberSet, readExternal, toString, unhand, uopBitwiseNot, uopDecrement, uopIncrement, uopLogicalNot, uopMinus, uopPlus, writeExternal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

smit

protected SymbolTable smit
The shared member index table maps member name to an index into the members array.


members

protected MemberTable members
The table of members of the scope. This is unique to each scope instance.


mixins

protected Value[] mixins
List of mixed in vars, or null if none.

Constructor Detail

BasicScope

public BasicScope(Scope previous)
Class Constructor. Construct a new element in the scope chain. This constructs a "regular" element in the scope chain, as opposed to the element that is created when a function is called.

Parameters:
previous - previous in environment scope chain

BasicScope

public BasicScope(Scope previous,
                  SymbolTable smit)
Class Constructor. Construct a new element in the scope chain. This constructs a "regular" element in the scope chain, as opposed to the element that is created when a function is called.

Parameters:
previous - previous in environment scope chain
smit - shared member idx table

BasicScope

protected BasicScope(Scope previous,
                     SymbolTable smit,
                     MemberTable members)
Class Constructor.

Parameters:
previous - previous scope
smit - shared-member-index-table
members - members, can be used by function scope to directly map arguments to the function into the function's scope
Method Detail

getTypeImpl

protected Value getTypeImpl()
Get the type of this object. The returned type doesn't have to take into account the possibility of a script type extending a built-in type, since that is handled by getType().

Specified by:
getTypeImpl in class Value
Returns:
the object's type

getSafeCopy

public Scope getSafeCopy()
In case a scope has any resource allocated from a source which will no long be valid after a stack frame has returned (ie. resource allocated from stack), return a copy of the scope that is safe to keep after the stack frame returns.

Overrides:
getSafeCopy in class Scope

isSafe

public boolean isSafe()
Specified by:
isSafe in class Scope

getMemberImpl

protected Value getMemberImpl(int id)
Overrides:
getMemberImpl in class Scope

__getInstanceMember

public Value __getInstanceMember(int id)
Specified by:
__getInstanceMember in class Scope

reset

public final void reset()
Reset this scope object. When program execution has left this scope block, it must be reset to ensure that any reference to it's members be freed.


getType

public Value getType()
Get the type of this object. This is overloaded so that mixed in objects have an appropriate effect on instanceof

Overrides:
getType in class Value
Returns:
the object's type

createMember

public Value createMember(int id,
                          int attr)
Create a member of this object with the specified value.

Note that the theory behind not synchronizing this is that it can only be a race condition against itself, not against getMember, etc, and that this will only be called from a single thread context.

Specified by:
createMember in class Scope
Parameters:
id - the id of the symbol that maps to the member
attr - the attributes of the object (see Reference.)
See Also:
Reference

mixin

public void mixin(Value val)
"mixin" the specified variable into the current scope.

Specified by:
mixin in class Scope
Parameters:
val - the value to mixin to this scope

getMember

public Value getMember(int id,
                       boolean exception)
                throws PackagedScriptObjectException
Get a member of this object.

Overrides:
getMember in class Value
Parameters:
id - the id of the symbol that maps to the member
exception - whether an exception should be thrown if the member object is not resolved
Returns:
a reference to the member
Throws:
PackagedScriptObjectException(NoSuchMethodException)
PackagedScriptObjectException(NoSuchMemberException)
PackagedScriptObjectException

lookupInScope

public Value lookupInScope(int id)
                    throws PackagedScriptObjectException
Get a member from this scope. This is used to access local variables and object attributes from methods of the object. If the attribute isn't in this node in the scope chain, then the previous node in the scope chain is checked.

Specified by:
lookupInScope in class Scope
Parameters:
id - the id of the symbol that maps to the member
Throws:
PackagedScriptObjectException(NoSuchMemberException)
PackagedScriptObjectException

free

public void free()
Indicate that this scope is no longer needed

Specified by:
free in class Scope

populateMemberSet

protected void populateMemberSet(java.util.Set s,
                                 boolean debugger)
Derived classes that implement getMember(int, boolean) should also implement this.

Overrides:
populateMemberSet in class Value
Parameters:
s - the set to populate
debugger - true if being used by debugger, in which case both public and private/protected field names should be returned
See Also:
getMember(int, boolean)