123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /*
- * Copyright (c) Contributors, http://opensimulator.org/
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the OpenSimulator Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- using System;
- using System.Reflection.Emit;
- using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
- using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
- using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
- using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
- using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
- using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
- using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
- namespace OpenSim.Region.ScriptEngine.Yengine
- {
- public class XMRSDTypeClObj
- {
- /*
- * Which script instance we are part of so we can access
- * the script's global variables and functions.
- */
- public XMRInstAbstract xmrInst;
- /*
- * What class we actually are in the hierarchy
- * used for casting.
- */
- public TokenDeclSDTypeClass sdtcClass;
- /*
- * Our VTable array, used for calling virtual functions.
- * And ITable array, used for calling our implementation of interface functions.
- */
- public Delegate[] sdtcVTable;
- public Delegate[][] sdtcITable;
- /*
- * These arrays hold the insance variable values.
- * The array lengths are determined by the script compilation,
- * and are found in TokenDeclSDTypeClass.instSizes.
- */
- public XMRInstArrays instVars;
- /**
- * @brief Called by script's $new() to initialize a new object.
- */
- public XMRSDTypeClObj(XMRInstAbstract inst, int classindex)
- {
- Construct(inst, classindex);
- instVars.AllocVarArrays(sdtcClass.instSizes);
- }
- /**
- * @brief Set up everything except the instVars arrays.
- * @param inst = script instance this object is part of
- * @param classindex = which script-defined type class this object is an onstance of
- * @returns with the vtables filled in
- */
- private void Construct(XMRInstAbstract inst, int classindex)
- {
- Delegate[] thisMid = null;
- TokenDeclSDTypeClass clas = (TokenDeclSDTypeClass)inst.m_ObjCode.sdObjTypesIndx[classindex];
- xmrInst = inst;
- sdtcClass = clas;
- instVars = new XMRInstArrays(inst);
- /*
- * VTable consists of delegates built from DynamicMethods and the 'this' pointer.
- * Yes, yes, lots of shitty little mallocs.
- */
- DynamicMethod[] vDynMeths = clas.vDynMeths;
- if(vDynMeths != null)
- {
- int n = vDynMeths.Length;
- Type[] vMethTypes = clas.vMethTypes;
- sdtcVTable = new Delegate[n];
- for(int i = 0; i < n; i++)
- {
- sdtcVTable[i] = vDynMeths[i].CreateDelegate(vMethTypes[i], this);
- }
- }
- /*
- * Fill in interface vtables.
- * There is one array of delegates for each implemented interface.
- * The array of delegates IS the interface's object, ie, the 'this' value of the interface.
- * To cast from the class type to the interface type, just get the correct array from the table.
- * To cast from the interface type to the class type, just get Target of entry 0.
- *
- * So we end up with this:
- * sdtcITable[interfacenumber][methodofintfnumber] = delegate of this.ourimplementationofinterfacesmethod
- */
- if(clas.iDynMeths != null)
- {
- int nIFaces = clas.iDynMeths.Length;
- sdtcITable = new Delegate[nIFaces][];
- for(int i = 0; i < nIFaces; i++)
- {
- // get vector of entrypoints of our instance methods that implement that interface
- DynamicMethod[] iDynMeths = clas.iDynMeths[i];
- Type[] iMethTypes = clas.iMethTypes[i];
- // allocate an array with a slot for each method the interface defines
- int nMeths = iDynMeths.Length;
- Delegate[] ivec;
- if(nMeths > 0)
- {
- // fill in the array with delegates that reference back to this class instance
- ivec = new Delegate[nMeths];
- for(int j = 0; j < nMeths; j++)
- {
- ivec[j] = iDynMeths[j].CreateDelegate(iMethTypes[j], this);
- }
- }
- else
- {
- // just a marker interface with no methods,
- // allocate a one-element array and fill
- // with a dummy entry. this will allow casting
- // back to the original class instance (this)
- // by reading Target of entry 0.
- if(thisMid == null)
- {
- thisMid = new Delegate[1];
- thisMid[0] = markerInterfaceDummy.CreateDelegate(typeof(MarkerInterfaceDummy), this);
- }
- ivec = thisMid;
- }
- // save whatever we ended up allocating
- sdtcITable[i] = ivec;
- }
- }
- }
- private delegate void MarkerInterfaceDummy();
- private static DynamicMethod markerInterfaceDummy = MakeMarkerInterfaceDummy();
- private static DynamicMethod MakeMarkerInterfaceDummy()
- {
- DynamicMethod dm = new DynamicMethod("XMRSDTypeClObj.MarkerInterfaceDummy", null, new Type[] { typeof(XMRSDTypeClObj) });
- ILGenerator ilGen = dm.GetILGenerator();
- ilGen.Emit(OpCodes.Ret);
- return dm;
- }
- /**
- * @brief Perform runtime casting of script-defined interface object to
- * its corresponding script-defined class object.
- * @param da = interface object (array of delegates pointing to class's implementations of interface's methods)
- * @param classindex = what class those implementations are supposedly part of
- * @returns original script-defined class object
- */
- public static XMRSDTypeClObj CastIFace2Class(Delegate[] da, int classindex)
- {
- return CastClass2Class(da[0].Target, classindex);
- }
- /**
- * @brief Perform runtime casting of XMRSDTypeClObj's.
- * @param ob = XMRSDTypeClObj of unknown script-defined class to cast
- * @param classindex = script-defined class to cast it to
- * @returns ob is a valid instance of classindex; else exception thrown
- */
- public static XMRSDTypeClObj CastClass2Class(object ob, int classindex)
- {
- /*
- * Let mono check to see if we at least have an XMRSDTypeClObj.
- */
- XMRSDTypeClObj ci = (XMRSDTypeClObj)ob;
- if(ci != null)
- {
- /*
- * This is the target class, ie, what we are hoping the object can cast to.
- */
- TokenDeclSDTypeClass tc = (TokenDeclSDTypeClass)ci.xmrInst.m_ObjCode.sdObjTypesIndx[classindex];
- /*
- * Step from the object's actual class rootward.
- * If we find the target class along the way, the cast is valid.
- * If we run off the end of the root, the cast is not valid.
- */
- for(TokenDeclSDTypeClass ac = ci.sdtcClass; ac != tc; ac = ac.extends)
- {
- if(ac == null)
- throw new InvalidCastException("invalid cast from " + ci.sdtcClass.longName.val +
- " to " + tc.longName.val);
- }
- /*
- * The target class is at or rootward of the actual class,
- * so the cast is valid.
- */
- }
- return ci;
- }
- /**
- * @brief Cast an arbitrary object to the given interface.
- * @param ob = object to be cast of unknown type
- * @returns ob cast to the interface type
- */
- public static Delegate[] CastObj2IFace(object ob, string ifacename)
- {
- if(ob == null)
- return null;
- /*
- * If it is already one of our interfaces, extract the script-defined class object from it.
- */
- if(ob is Delegate[])
- {
- Delegate[] da = (Delegate[])ob;
- ob = da[0].Target;
- }
- /*
- * Now that we have a presumed script-defined class object, cast that to the requested interface
- * by picking the array of delegates that corresponds to the requested interface.
- */
- XMRSDTypeClObj ci = (XMRSDTypeClObj)ob;
- int iFaceIndex = ci.sdtcClass.intfIndices[ifacename];
- return ci.sdtcITable[iFaceIndex];
- }
- /**
- * @brief Write the whole thing out to a stream.
- */
- public void Capture(XMRInstArrays.Sender sendValue)
- {
- sendValue(this.sdtcClass.sdTypeIndex);
- this.instVars.SendArrays(sendValue);
- }
- /**
- * @brief Read the whole thing in from a stream.
- */
- public XMRSDTypeClObj()
- {
- }
- public void Restore(XMRInstAbstract inst, XMRInstArrays.Recver recvValue)
- {
- int classindex = (int)recvValue();
- Construct(inst, classindex);
- this.instVars.RecvArrays(recvValue);
- }
- }
- }
|