edu.vub.at.objects.mirrors
Class NativeClosure

java.lang.Object
  extended byedu.vub.at.objects.natives.NATNil
      extended byedu.vub.at.objects.natives.NATByRef
          extended byedu.vub.at.objects.natives.NATClosure
              extended byedu.vub.at.objects.mirrors.NativeClosure
All Implemented Interfaces:
ATAbstractGrammar, ATClosure, ATConversions, ATExpression, ATNil, ATObject, ATStatement, java.io.Serializable

public class NativeClosure
extends NATClosure

A NativeClosure is a wrapper class for a piece of Java code. The Java code is presented to the AmbientTalk system as a closure. A NativeClosure is represented as follows: - Its encapsulating method is a 'dummy' NativeMethod ATMethod, with the following properties: - name = "nativelambda" (to distinguish it from 'ordinary' lambdas) - arguments = [ \@args ] (it takes an arbitrary number of arguments) - body = "Native implementation in {class}" (a string telling an inspector that this closure is natively implemented in the given Java class) - applying a nativelambda directly (without going through this NativeClosure) results in an error - Its enclosed context consists of a triple (obj, obj, obj.super), where 'obj' is the Java object (implementing ATObject) that created this NativeClosure. The method and context fields of a NativeClosure are lazily generated on demand for efficiency reasons. Most of the time, a NativeClosure will not be introspected, but only applied. A NativeClosure can be used in two ways by Java code: 1) As a generator for anonymous classes to generate 'anonymous lambdas': new NativeClosure(this) { public ATObject meta_apply(ATTable args) throws NATException { ... } } 2) As a wrapper for an already existing NativeMethod: new NativeClosure(this, aJavaMethod);

Author:
tvc
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class edu.vub.at.objects.natives.NATClosure
 
Field Summary
protected  ATObject scope_
           
 
Fields inherited from class edu.vub.at.objects.natives.NATClosure
context_, method_
 
Fields inherited from class edu.vub.at.objects.natives.NATNil
_INSTANCE_
 
Constructor Summary
NativeClosure(ATObject scope)
          Create a new NativeClosure where meta_apply will be overridden by anonymous subclasses.
NativeClosure(ATObject scope, NativeMethod meth)
          Create a new NativeClosure where meta_apply will invoke the given Java Method.
 
Method Summary
 ATObject base_apply(ATTable arguments)
          Apply the NativeClosure, which either gives rise to executing a native piece of code supplied by an anonymous subclass, or executes the wrapped NativeMethod.
 ATObject base_applyInScope(ATTable args, ATObject scope)
          A NativeClosure can also be directed to execute its wrapped NativeMethod in an externally specified scope.
 ATContext base_getContext()
          Overridden to allow for lazy instantiation of the context.
 ATMethod base_getMethod()
          Overridden to allow for lazy instantiation of the method.
 void checkArity(ATTable args, int required)
           
 ATObject get(ATTable args, int n)
          Auxiliary method to more easily extract arguments from an ATTable
 boolean getBln(ATTable args, int n)
           
 double getFrc(ATTable args, int n)
           
 int getNbr(ATTable args, int n)
           
 java.lang.Object[] getTab(ATTable args, int n)
           
 java.lang.String getTxt(ATTable args, int n)
           
 NATText meta_print()
          Prints out the object in a human-readable way.
 
Methods inherited from class edu.vub.at.objects.natives.NATClosure
base_asClosure, base_escape, base_isClosure, base_whileTrue_, meta_getStripes
 
Methods inherited from class edu.vub.at.objects.natives.NATByRef
meta_pass, meta_resolve
 
Methods inherited from class edu.vub.at.objects.natives.NATNil
asAmbientTalkObject, asJavaClassUnderSymbiosis, asJavaObjectUnderSymbiosis, asNativeBoolean, asNativeException, asNativeFarReference, asNativeFraction, asNativeNumber, asNativeNumeric, asNativeTable, asNativeText, base__opeql__opeql_, base_asActorMirror, base_asAsyncMessage, base_asBegin, base_asBoolean, base_asDefinition, base_asExpression, base_asFarReference, base_asField, base_asHandler, base_asMessage, base_asMessageCreation, base_asMethod, base_asMirror, base_asNumber, base_asSplice, base_asStatement, base_asStripe, base_asSymbol, base_asTable, base_asUnquoteSplice, base_asVariableAssignment, base_init, base_isBoolean, base_isCallFrame, base_isFarReference, base_isMessageCreation, base_isMethod, base_isMirror, base_isSplice, base_isStripe, base_isSymbol, base_isTable, base_isUnquoteSplice, base_isVariableAssignment, base_new, createChild, equals, isAmbientTalkObject, isJavaObjectUnderSymbiosis, isNativeBoolean, isNativeField, isNativeText, meta_addField, meta_addMethod, meta_assignField, meta_assignVariable, meta_clone, meta_defineField, meta_doesNotUnderstand, meta_eval, meta_extend, meta_getDynamicParent, meta_getLexicalParent, meta_grabField, meta_grabMethod, meta_invoke, meta_isCloneOf, meta_isRelatedTo, meta_isStripedWith, meta_listFields, meta_listMethods, meta_lookup, meta_newInstance, meta_quote, meta_receive, meta_respondsTo, meta_select, meta_send, meta_share, readResolve, toString, writeReplace
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.vub.at.objects.ATObject
base__opeql__opeql_, base_init, base_new, meta_addField, meta_addMethod, meta_assignField, meta_assignVariable, meta_clone, meta_defineField, meta_doesNotUnderstand, meta_eval, meta_extend, meta_getDynamicParent, meta_getLexicalParent, meta_grabField, meta_grabMethod, meta_invoke, meta_isCloneOf, meta_isRelatedTo, meta_isStripedWith, meta_listFields, meta_listMethods, meta_lookup, meta_newInstance, meta_pass, meta_quote, meta_receive, meta_resolve, meta_respondsTo, meta_select, meta_send, meta_share
 
Methods inherited from interface edu.vub.at.objects.coercion.ATConversions
asAmbientTalkObject, asJavaClassUnderSymbiosis, asJavaObjectUnderSymbiosis, asNativeBoolean, asNativeException, asNativeFarReference, asNativeFraction, asNativeNumber, asNativeNumeric, asNativeTable, asNativeText, base_asActorMirror, base_asAsyncMessage, base_asBegin, base_asBoolean, base_asDefinition, base_asExpression, base_asFarReference, base_asField, base_asHandler, base_asMessage, base_asMessageCreation, base_asMethod, base_asMirror, base_asNumber, base_asSplice, base_asStatement, base_asStripe, base_asSymbol, base_asTable, base_asUnquoteSplice, base_asVariableAssignment, base_isBoolean, base_isCallFrame, base_isFarReference, base_isMessageCreation, base_isMethod, base_isMirror, base_isSplice, base_isStripe, base_isSymbol, base_isTable, base_isUnquoteSplice, base_isVariableAssignment, isAmbientTalkObject, isJavaObjectUnderSymbiosis, isNativeBoolean, isNativeField, isNativeText
 

Field Detail

scope_

protected final ATObject scope_
Constructor Detail

NativeClosure

public NativeClosure(ATObject scope)
Create a new NativeClosure where meta_apply will be overridden by anonymous subclasses.

Parameters:
scope - the object creating this NativeClosure.

NativeClosure

public NativeClosure(ATObject scope,
                     NativeMethod meth)
Create a new NativeClosure where meta_apply will invoke the given Java Method.

Parameters:
scope - the object that should be used as a receiver for the corresponding method.
meth - a presumably native method
Method Detail

base_getMethod

public ATMethod base_getMethod()
Overridden to allow for lazy instantiation of the method. If receiver is an anonymous NativeClosure, an 'anonymous' NativeMethod is returned.

Specified by:
base_getMethod in interface ATClosure
Overrides:
base_getMethod in class NATClosure
Returns:
a NativeMethod wrapped by this NativeClosure.

base_getContext

public ATContext base_getContext()
                          throws InterpreterException
Overridden to allow for lazy instantiation of the context. A 'default' context is lazily constructed and returned.

Specified by:
base_getContext in interface ATClosure
Overrides:
base_getContext in class NATClosure
Throws:
InterpreterException

base_apply

public ATObject base_apply(ATTable arguments)
                    throws InterpreterException
Apply the NativeClosure, which either gives rise to executing a native piece of code supplied by an anonymous subclass, or executes the wrapped NativeMethod.

Specified by:
base_apply in interface ATClosure
Overrides:
base_apply in class NATClosure
Throws:
InterpreterException

base_applyInScope

public ATObject base_applyInScope(ATTable args,
                                  ATObject scope)
                           throws InterpreterException
A NativeClosure can also be directed to execute its wrapped NativeMethod in an externally specified scope. Of course, for native calls or symbiotic calls, the call will only succeed if the externally specified object is of the correct native type.

Specified by:
base_applyInScope in interface ATClosure
Overrides:
base_applyInScope in class NATClosure
Throws:
InterpreterException

meta_print

public NATText meta_print()
                   throws InterpreterException
Description copied from interface: ATObject
Prints out the object in a human-readable way.

Specified by:
meta_print in interface ATObject
Overrides:
meta_print in class NATClosure
Throws:
InterpreterException

get

public ATObject get(ATTable args,
                    int n)
             throws InterpreterException
Auxiliary method to more easily extract arguments from an ATTable

Throws:
InterpreterException

getNbr

public int getNbr(ATTable args,
                  int n)
           throws InterpreterException
Throws:
InterpreterException

getFrc

public double getFrc(ATTable args,
                     int n)
              throws InterpreterException
Throws:
InterpreterException

getTxt

public java.lang.String getTxt(ATTable args,
                               int n)
                        throws InterpreterException
Throws:
InterpreterException

getBln

public boolean getBln(ATTable args,
                      int n)
               throws InterpreterException
Throws:
InterpreterException

getTab

public java.lang.Object[] getTab(ATTable args,
                                 int n)
                          throws InterpreterException
Throws:
InterpreterException

checkArity

public void checkArity(ATTable args,
                       int required)
                throws InterpreterException
Throws:
InterpreterException