XMRInstAbstract.cs 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Reflection.Emit;
  33. using System.Runtime.Serialization;
  34. using System.Text;
  35. using System.Threading;
  36. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  37. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  38. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  39. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  40. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  41. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  42. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  43. namespace OpenSim.Region.ScriptEngine.Yengine
  44. {
  45. public class XMRInstArrays
  46. {
  47. public XMR_Array[] iarArrays;
  48. public char[] iarChars;
  49. public double[] iarFloats;
  50. public int[] iarIntegers;
  51. public LSL_List[] iarLists;
  52. public object[] iarObjects;
  53. public LSL_Rotation[] iarRotations;
  54. public string[] iarStrings;
  55. public LSL_Vector[] iarVectors;
  56. public XMRSDTypeClObj[] iarSDTClObjs;
  57. public Delegate[][] iarSDTIntfObjs;
  58. private XMRInstAbstract instance;
  59. public int arraysHeapUse;
  60. private static readonly XMR_Array[] noArrays = new XMR_Array[0];
  61. private static readonly char[] noChars = new char[0];
  62. private static readonly double[] noFloats = new double[0];
  63. private static readonly int[] noIntegers = new int[0];
  64. private static readonly LSL_List[] noLists = new LSL_List[0];
  65. private static readonly object[] noObjects = new object[0];
  66. private static readonly LSL_Rotation[] noRotations = new LSL_Rotation[0];
  67. private static readonly string[] noStrings = new string[0];
  68. private static readonly LSL_Vector[] noVectors = new LSL_Vector[0];
  69. private static readonly XMRSDTypeClObj[] noSDTClObjs = new XMRSDTypeClObj[0];
  70. private static readonly Delegate[][] noSDTIntfObjs = new Delegate[0][];
  71. public XMRInstArrays(XMRInstAbstract inst)
  72. {
  73. instance = inst;
  74. }
  75. /*
  76. ~XMRInstArrays()
  77. {
  78. arraysHeapUse = instance.UpdateArraysHeapUse(arraysHeapUse, 0);
  79. }
  80. */
  81. public void Clear()
  82. {
  83. int newheapUse = 0;
  84. if(iarArrays != null)
  85. {
  86. foreach(XMR_Array xa in iarArrays)
  87. xa.__pub_clear();
  88. }
  89. if(iarChars != null)
  90. iarChars = new char[iarChars.Length];
  91. if (iarLists != null)
  92. iarLists = new LSL_List[iarLists.Length];
  93. if (iarObjects != null)
  94. iarObjects = new object[iarObjects.Length];
  95. if(iarStrings != null)
  96. iarStrings = new string[iarStrings.Length];
  97. if (iarFloats != null)
  98. newheapUse += iarFloats.Length * HeapTrackerObject.HT_DOUB;
  99. if (iarIntegers != null)
  100. newheapUse += iarIntegers.Length * HeapTrackerObject.HT_INT;
  101. if (iarRotations != null)
  102. newheapUse += iarRotations.Length * HeapTrackerObject.HT_ROT;
  103. if (iarVectors != null)
  104. newheapUse += iarVectors.Length * HeapTrackerObject.HT_VEC;
  105. arraysHeapUse = instance.UpdateArraysHeapUse(0, newheapUse);
  106. }
  107. public void AllocVarArrays(XMRInstArSizes ars)
  108. {
  109. ClearOldArrays();
  110. int newuse = arraysHeapUse +
  111. ars.iasChars* HeapTrackerObject.HT_CHAR +
  112. ars.iasFloats * HeapTrackerObject.HT_DOUB +
  113. ars.iasIntegers * HeapTrackerObject.HT_INT +
  114. ars.iasRotations * HeapTrackerObject.HT_ROT +
  115. ars.iasVectors * HeapTrackerObject.HT_VEC +
  116. ars.iasSDTIntfObjs * HeapTrackerObject.HT_DELE;
  117. arraysHeapUse = instance.UpdateArraysHeapUse(arraysHeapUse, newuse);
  118. iarArrays = (ars.iasArrays > 0) ? new XMR_Array[ars.iasArrays] : noArrays;
  119. iarChars = (ars.iasChars > 0) ? new char[ars.iasChars] : noChars;
  120. iarFloats = (ars.iasFloats > 0) ? new double[ars.iasFloats] : noFloats;
  121. iarIntegers = (ars.iasIntegers > 0) ? new int[ars.iasIntegers] : noIntegers;
  122. iarLists = (ars.iasLists > 0) ? new LSL_List[ars.iasLists] : noLists;
  123. iarObjects = (ars.iasObjects > 0) ? new object[ars.iasObjects] : noObjects;
  124. iarRotations = (ars.iasRotations > 0) ? new LSL_Rotation[ars.iasRotations] : noRotations;
  125. iarStrings = (ars.iasStrings > 0) ? new string[ars.iasStrings] : noStrings;
  126. iarVectors = (ars.iasVectors > 0) ? new LSL_Vector[ars.iasVectors] : noVectors;
  127. iarSDTClObjs = (ars.iasSDTClObjs > 0) ? new XMRSDTypeClObj[ars.iasSDTClObjs] : noSDTClObjs;
  128. iarSDTIntfObjs = (ars.iasSDTIntfObjs > 0) ? new Delegate[ars.iasSDTIntfObjs][] : noSDTIntfObjs;
  129. }
  130. /**
  131. * @brief Do not write directly to iarLists[index], rather use this method.
  132. */
  133. public void PopList(int index, LSL_List lis)
  134. {
  135. int delta = HeapTrackerObject.Size(lis) - HeapTrackerObject.Size(iarLists[index]);
  136. instance.UpdateArraysHeapUse(0, delta);
  137. Interlocked.Add(ref arraysHeapUse, delta);
  138. iarLists[index] = lis;
  139. }
  140. /**
  141. * @brief Do not write directly to iarObjects[index], rather use this method.
  142. */
  143. public void PopObject(int index, object obj)
  144. {
  145. int delta = HeapTrackerObject.Size(obj) - HeapTrackerObject.Size(iarObjects[index]);
  146. instance.UpdateArraysHeapUse(0, delta);
  147. Interlocked.Add(ref arraysHeapUse, delta);
  148. iarObjects[index] = obj;
  149. }
  150. /**
  151. * @brief Do not write directly to iarStrings[index], rather use this method.
  152. */
  153. public void PopString(int index, string str)
  154. {
  155. int delta = HeapTrackerString.Size(str) - HeapTrackerString.Size(iarStrings[index]);
  156. instance.UpdateArraysHeapUse(0, delta);
  157. Interlocked.Add(ref arraysHeapUse, delta);
  158. iarStrings[index] = str;
  159. }
  160. /**
  161. * @brief Write all arrays out to a file.
  162. */
  163. public delegate void Sender(object value);
  164. public void SendArrays(Sender sender)
  165. {
  166. sender(iarArrays);
  167. sender(iarChars);
  168. sender(iarFloats);
  169. sender(iarIntegers);
  170. sender(iarLists);
  171. sender(iarObjects);
  172. sender(iarRotations);
  173. sender(iarStrings);
  174. sender(iarVectors);
  175. sender(iarSDTClObjs);
  176. sender(iarSDTIntfObjs);
  177. }
  178. /**
  179. * @brief Read all arrays in from a file.
  180. */
  181. public delegate object Recver();
  182. public void RecvArrays(Recver recver)
  183. {
  184. ClearOldArrays();
  185. iarArrays = (XMR_Array[])recver();
  186. char[] chrs = (char[])recver();
  187. double[] flts = (double[])recver();
  188. int[] ints = (int[])recver();
  189. LSL_List[] liss = (LSL_List[])recver();
  190. object[] objs = (object[])recver();
  191. LSL_Rotation[] rots = (LSL_Rotation[])recver();
  192. string[] strs = (string[])recver();
  193. LSL_Vector[] vecs = (LSL_Vector[])recver();
  194. iarSDTClObjs = (XMRSDTypeClObj[])recver();
  195. Delegate[][] dels = (Delegate[][])recver();
  196. int newheapuse = arraysHeapUse;
  197. // value types simply are the size of the value * number of values
  198. newheapuse += chrs.Length * HeapTrackerObject.HT_CHAR;
  199. newheapuse += flts.Length * HeapTrackerObject.HT_DOUB;
  200. newheapuse += ints.Length * HeapTrackerObject.HT_INT;
  201. newheapuse += rots.Length * HeapTrackerObject.HT_ROT;
  202. newheapuse += vecs.Length * HeapTrackerObject.HT_VEC;
  203. newheapuse += dels.Length * HeapTrackerObject.HT_DELE;
  204. // lists, objects, strings are the sum of the size of each element
  205. foreach(LSL_List lis in liss)
  206. newheapuse += HeapTrackerList.Size(lis);
  207. foreach(object obj in objs)
  208. newheapuse += HeapTrackerObject.Size(obj);
  209. foreach(string str in strs)
  210. newheapuse += HeapTrackerString.Size(str);
  211. // others (XMR_Array, XMRSDTypeClObj) keep track of their own heap usage
  212. // update script heap usage, throwing an exception before finalizing changes
  213. arraysHeapUse = instance.UpdateArraysHeapUse(arraysHeapUse, newheapuse);
  214. iarChars = chrs;
  215. iarFloats = flts;
  216. iarIntegers = ints;
  217. iarLists = liss;
  218. iarObjects = objs;
  219. iarRotations = rots;
  220. iarStrings = strs;
  221. iarVectors = vecs;
  222. iarSDTIntfObjs = dels;
  223. }
  224. private void ClearOldArrays()
  225. {
  226. int newheapuse = arraysHeapUse;
  227. iarArrays = null;
  228. if(iarChars != null)
  229. {
  230. newheapuse -= iarChars.Length * HeapTrackerObject.HT_CHAR;
  231. iarChars = null;
  232. }
  233. if(iarFloats != null)
  234. {
  235. newheapuse -= iarFloats.Length * HeapTrackerObject.HT_DOUB;
  236. iarFloats = null;
  237. }
  238. if(iarIntegers != null)
  239. {
  240. newheapuse -= iarIntegers.Length * HeapTrackerObject.HT_INT;
  241. iarIntegers = null;
  242. }
  243. if(iarLists != null)
  244. {
  245. foreach(LSL_List lis in iarLists)
  246. newheapuse -= HeapTrackerList.Size(lis);
  247. iarLists = null;
  248. }
  249. if(iarObjects != null)
  250. {
  251. foreach(object obj in iarObjects)
  252. newheapuse -= HeapTrackerObject.Size(obj);
  253. iarObjects = null;
  254. }
  255. if(iarRotations != null)
  256. {
  257. newheapuse -= iarRotations.Length * HeapTrackerObject.HT_ROT;
  258. iarRotations = null;
  259. }
  260. if(iarStrings != null)
  261. {
  262. foreach(string str in iarStrings)
  263. newheapuse -= HeapTrackerString.Size(str);
  264. iarStrings = null;
  265. }
  266. if(iarVectors != null)
  267. {
  268. newheapuse -= iarVectors.Length * HeapTrackerObject.HT_VEC;
  269. iarVectors = null;
  270. }
  271. iarSDTClObjs = null;
  272. if(iarSDTIntfObjs != null)
  273. {
  274. newheapuse -= iarSDTIntfObjs.Length * HeapTrackerObject.HT_DELE;
  275. iarSDTIntfObjs = null;
  276. }
  277. arraysHeapUse = instance.UpdateArraysHeapUse(arraysHeapUse, newheapuse);
  278. }
  279. }
  280. public class XMRInstArSizes
  281. {
  282. public int iasArrays;
  283. public int iasChars;
  284. public int iasFloats;
  285. public int iasIntegers;
  286. public int iasLists;
  287. public int iasObjects;
  288. public int iasRotations;
  289. public int iasStrings;
  290. public int iasVectors;
  291. public int iasSDTClObjs;
  292. public int iasSDTIntfObjs;
  293. public void WriteAsmFile(TextWriter asmFileWriter, string label)
  294. {
  295. asmFileWriter.WriteLine(" {0}Arrays {1}", label, iasArrays);
  296. asmFileWriter.WriteLine(" {0}Chars {1}", label, iasChars);
  297. asmFileWriter.WriteLine(" {0}Floats {1}", label, iasFloats);
  298. asmFileWriter.WriteLine(" {0}Integers {1}", label, iasIntegers);
  299. asmFileWriter.WriteLine(" {0}Lists {1}", label, iasLists);
  300. asmFileWriter.WriteLine(" {0}Objects {1}", label, iasObjects);
  301. asmFileWriter.WriteLine(" {0}Rotations {1}", label, iasRotations);
  302. asmFileWriter.WriteLine(" {0}Strings {1}", label, iasStrings);
  303. asmFileWriter.WriteLine(" {0}Vectors {1}", label, iasVectors);
  304. asmFileWriter.WriteLine(" {0}SDTClObjs {1}", label, iasSDTClObjs);
  305. asmFileWriter.WriteLine(" {0}SDTIntfObjs {1}", label, iasSDTIntfObjs);
  306. }
  307. public void WriteToFile(BinaryWriter objFileWriter)
  308. {
  309. objFileWriter.Write(iasArrays);
  310. objFileWriter.Write(iasChars);
  311. objFileWriter.Write(iasFloats);
  312. objFileWriter.Write(iasIntegers);
  313. objFileWriter.Write(iasLists);
  314. objFileWriter.Write(iasObjects);
  315. objFileWriter.Write(iasRotations);
  316. objFileWriter.Write(iasStrings);
  317. objFileWriter.Write(iasVectors);
  318. objFileWriter.Write(iasSDTClObjs);
  319. objFileWriter.Write(iasSDTIntfObjs);
  320. }
  321. public void ReadFromFile(BinaryReader objFileReader)
  322. {
  323. iasArrays = objFileReader.ReadInt32();
  324. iasChars = objFileReader.ReadInt32();
  325. iasFloats = objFileReader.ReadInt32();
  326. iasIntegers = objFileReader.ReadInt32();
  327. iasLists = objFileReader.ReadInt32();
  328. iasObjects = objFileReader.ReadInt32();
  329. iasRotations = objFileReader.ReadInt32();
  330. iasStrings = objFileReader.ReadInt32();
  331. iasVectors = objFileReader.ReadInt32();
  332. iasSDTClObjs = objFileReader.ReadInt32();
  333. iasSDTIntfObjs = objFileReader.ReadInt32();
  334. }
  335. }
  336. public class XMRStackFrame
  337. {
  338. public XMRStackFrame nextSF;
  339. public string funcName;
  340. public int callNo;
  341. public object[] objArray;
  342. }
  343. /*
  344. * Contains only items required by the stand-alone compiler
  345. * so the compiler doesn't need to pull in all of OpenSim.
  346. *
  347. * Inherit from ScriptBaseClass so we can be used as 'this'
  348. * parameter for backend-API calls, eg llSay().
  349. */
  350. public abstract class XMRInstAbstract: ScriptBaseClass
  351. {
  352. public const int CallMode_NORMAL = 0; // when function is called, it proceeds normally
  353. public const int CallMode_SAVE = 1; // StackSaveException() was thrown, push args/locals to stackFrames
  354. public const int CallMode_RESTORE = 2; // when function is called, it pops state from stackFrames
  355. public bool suspendOnCheckRunHold; // suspend script execution until explicitly set false
  356. public bool suspendOnCheckRunTemp; // suspend script execution for single step only
  357. public int stackLimit; // stack must have at least this many bytes free on entry to functions
  358. public int m_StackLeft; // total number of stack bytes yet to be used (init to stacksize)
  359. public ScriptObjCode m_ObjCode; // script object code this instance was created from
  360. public object[] ehArgs; // event handler argument array
  361. public bool doGblInit = true; // default state_entry() needs to initialize global variables
  362. public int stateCode = 0; // state the script is in (0 = 'default')
  363. public int newStateCode = -1; // if >= 0, in the middle of exiting 'stateCode' and entering 'newStateCode'
  364. public ScriptEventCode eventCode = ScriptEventCode.None;
  365. // what event handler is executing (or None if not)
  366. public int callMode = CallMode_NORMAL;
  367. // to capture stack frames on stackFrames:
  368. // set to CallMode_SAVE just before throwing StackSaveException()
  369. // from within CheckRun() and cleared to CallMode_NORMAL when
  370. // the exception is caught
  371. // to restore stack frames from stackFrames:
  372. // set to CallMode_RESTORE just before calling CallSEH() and
  373. // cleared to CallMode_NORMAL by CheckRun()
  374. public XMRStackFrame stackFrames; // stack frames being saved/restored
  375. private static readonly char[] justacomma = { ',' };
  376. /*
  377. * These arrays hold the global variable values for the script instance.
  378. * The array lengths are determined by the script compilation,
  379. * and are found in ScriptObjCode.glblSizes.
  380. */
  381. public XMRInstArrays glblVars;
  382. public XMRInstAbstract()
  383. {
  384. glblVars = new XMRInstArrays(this);
  385. }
  386. /****************************************************************\
  387. * Abstract function prototypes. *
  388. * These functions require access to the OpenSim environment. *
  389. \****************************************************************/
  390. public abstract void CheckRunWork();
  391. public abstract void StateChange();
  392. [xmrMethodCallsCheckRunAttribute] // calls CheckRun()
  393. [xmrMethodIsNoisyAttribute] // calls Stub<somethingorother>()
  394. public abstract LSL_List xmrEventDequeue(double timeout, int returnMask1, int returnMask2,
  395. int backgroundMask1, int backgroundMask2);
  396. [xmrMethodIsNoisyAttribute] // calls Stub<somethingorother>()
  397. public abstract void xmrEventEnqueue(LSL_List ev);
  398. [xmrMethodIsNoisyAttribute] // calls Stub<somethingorother>()
  399. public abstract LSL_List xmrEventSaveDets();
  400. [xmrMethodIsNoisyAttribute] // calls Stub<somethingorother>()
  401. public abstract void xmrEventLoadDets(LSL_List dpList);
  402. /**************************************************\
  403. * Functions what don't require runtime support *
  404. * beyond what the compiler provides. *
  405. \**************************************************/
  406. protected int heapLimit;
  407. public int m_localsHeapUsed;
  408. public int m_arraysHeapUsed;
  409. public virtual int UpdateLocalsHeapUse(int olduse, int newuse)
  410. {
  411. int newtotal = Interlocked.Add(ref m_localsHeapUsed, newuse - olduse);
  412. if (newtotal + glblVars.arraysHeapUse > heapLimit)
  413. throw new OutOfHeapException(m_arraysHeapUsed + newtotal + olduse - newuse, newtotal, heapLimit);
  414. return newuse;
  415. }
  416. // not in use
  417. public virtual int UpdateArraysHeapUse(int olduse, int newuse)
  418. {
  419. //int newtotal = Interlocked.Add(ref m_arraysheapUsed, newuse - olduse);
  420. if(newuse + glblVars.arraysHeapUse > heapLimit)
  421. throw new OutOfHeapException(m_arraysHeapUsed + newuse + olduse - newuse, newuse, heapLimit);
  422. return newuse;
  423. }
  424. public virtual void AddLocalsHeapUse(int delta)
  425. {
  426. Interlocked.Add(ref m_localsHeapUsed, delta);
  427. }
  428. public virtual void AddArraysHeapUse(int delta)
  429. {
  430. Interlocked.Add(ref m_arraysHeapUsed, delta);
  431. }
  432. public int xmrHeapLeft()
  433. {
  434. return heapLimit - m_localsHeapUsed - glblVars.arraysHeapUse;
  435. }
  436. public int xmrHeapUsed()
  437. {
  438. return m_localsHeapUsed + glblVars.arraysHeapUse;
  439. }
  440. /**
  441. * @brief Call script's event handler function from the very beginning.
  442. * @param instance.stateCode = which state the event is happening in
  443. * @param instance.eventCode = which event is happening in that state
  444. * @returns when event handler has completed or throws an exception
  445. * with instance.eventCode = ScriptEventCode.None
  446. */
  447. public void CallSEH()
  448. {
  449. ScriptEventHandler seh;
  450. // CallMode_NORMAL: run event handler from the beginning normally
  451. // CallMode_RESTORE: restore event handler stack from stackFrames
  452. callMode = (stackFrames == null) ? XMRInstAbstract.CallMode_NORMAL :
  453. XMRInstAbstract.CallMode_RESTORE;
  454. while(true)
  455. {
  456. if(this.newStateCode < 0)
  457. {
  458. // Process event given by 'stateCode' and 'eventCode'.
  459. // The event handler should call CheckRun() as often as convenient.
  460. int newState = this.stateCode;
  461. seh = this.m_ObjCode.scriptEventHandlerTable[newState, (int)this.eventCode];
  462. if(seh != null)
  463. {
  464. try
  465. {
  466. seh(this);
  467. }
  468. catch(ScriptChangeStateException scse)
  469. {
  470. newState = scse.newState;
  471. }
  472. }
  473. this.ehArgs = null; // we are done with them and no args for
  474. // exit_state()/enter_state() anyway
  475. // The usual case is no state change.
  476. // Even a 'state <samestate>;' statement has no effect except to exit out.
  477. // It does not execute the state_exit() or state_entry() handlers.
  478. // See http://wiki.secondlife.com/wiki/State
  479. if(newState == this.stateCode)
  480. break;
  481. // Save new state in a more permanent location in case we
  482. // get serialized out while in the state_exit() handler.
  483. this.newStateCode = newState;
  484. }
  485. // Call old state's state_exit() handler.
  486. this.eventCode = ScriptEventCode.state_exit;
  487. seh = this.m_ObjCode.scriptEventHandlerTable[this.stateCode, (int)ScriptEventCode.state_exit];
  488. if(seh != null)
  489. {
  490. try
  491. {
  492. seh(this);
  493. }
  494. catch(ScriptChangeStateException scse)
  495. {
  496. this.newStateCode = scse.newState;
  497. }
  498. }
  499. // Switch over to the new state's state_entry() handler.
  500. this.stateCode = this.newStateCode;
  501. this.eventCode = ScriptEventCode.state_entry;
  502. this.newStateCode = -1;
  503. // Now that the old state can't possibly start any more activity,
  504. // cancel any listening handlers, etc, of the old state.
  505. this.StateChange();
  506. // Loop back to execute new state's state_entry() handler.
  507. }
  508. // Event no longer being processed.
  509. this.eventCode = ScriptEventCode.None;
  510. }
  511. /**
  512. * @brief For compatibility with old code.
  513. */
  514. public void CheckRun(int line)
  515. {
  516. CheckRunStack();
  517. }
  518. /**
  519. * @brief Called at beginning of complex functions to see if they
  520. * are nested too deep possibly in a recursive loop.
  521. */
  522. public void CheckRunStack()
  523. {
  524. if(m_StackLeft < stackLimit)
  525. throw new OutOfStackException();
  526. CheckRunQuick();
  527. }
  528. /**
  529. * @brief Called in each iteration of a loop to see if running too long.
  530. */
  531. public void CheckRunQuick()
  532. {
  533. // if (suspendOnCheckRunHold || suspendOnCheckRunTemp)
  534. CheckRunWork();
  535. }
  536. /**
  537. * @brief Called during CallMode_SAVE to create a stackframe save object that saves
  538. * local variables and calling point within the function.
  539. * @param funcName = name of function whose frame is being saved
  540. * @param callNo = call number (ie, return address) within function to restart at
  541. * @param nSaves = number of variables the function will save
  542. * @returns an object[nSaves] where function can save variables
  543. */
  544. public object[] CaptureStackFrame(string funcName, int callNo, int nSaves)
  545. {
  546. XMRStackFrame sf = new XMRStackFrame();
  547. sf.nextSF = stackFrames;
  548. sf.funcName = funcName;
  549. sf.callNo = callNo;
  550. sf.objArray = new object[nSaves];
  551. stackFrames = sf;
  552. return sf.objArray;
  553. }
  554. /**
  555. * @brief Called during CallMode_RESTORE to pop a stackframe object to restore
  556. * local variables and calling point within the function.
  557. * @param funcName = name of function whose frame is being restored
  558. * @returns the object[nSaves] where function can retrieve variables
  559. * callNo = as passed to CaptureStackFrame() indicating restart point
  560. */
  561. public object[] RestoreStackFrame(string funcName, out int callNo)
  562. {
  563. XMRStackFrame sf = stackFrames;
  564. if(sf.funcName != funcName)
  565. throw new Exception("frame mismatch " + sf.funcName + " vs " + funcName);
  566. callNo = sf.callNo;
  567. stackFrames = sf.nextSF;
  568. return sf.objArray;
  569. }
  570. /**
  571. * @brief Convert all LSL_Integers in a list to System.Int32s,
  572. * as required by llParcelMediaQuery().
  573. */
  574. public static LSL_List FixLLParcelMediaQuery(LSL_List oldlist)
  575. {
  576. object[] oldarray = oldlist.Data;
  577. int len = oldarray.Length;
  578. object[] newarray = new object[len];
  579. for(int i = 0; i < len; i++)
  580. {
  581. object obj = oldarray[i];
  582. if(obj is LSL_Integer)
  583. obj = (int)(LSL_Integer)obj;
  584. newarray[i] = obj;
  585. }
  586. return new LSL_List(newarray);
  587. }
  588. /**
  589. * @brief Convert *SOME* LSL_Integers in a list to System.Int32s,
  590. * as required by llParcelMediaCommandList().
  591. */
  592. public static LSL_List FixLLParcelMediaCommandList(LSL_List oldlist)
  593. {
  594. object[] oldarray = oldlist.Data;
  595. int len = oldarray.Length;
  596. object[] newarray = new object[len];
  597. int verbatim = 0;
  598. for(int i = 0; i < len; i++)
  599. {
  600. object obj = oldarray[i];
  601. if(--verbatim < 0)
  602. {
  603. if(obj is LSL_Integer)
  604. obj = (int)(LSL_Integer)obj;
  605. if(obj is int)
  606. {
  607. switch((int)obj)
  608. {
  609. case ScriptBaseClass.PARCEL_MEDIA_COMMAND_AUTO_ALIGN:
  610. // leave next integer as LSL_Integer
  611. verbatim = 1;
  612. break;
  613. case ScriptBaseClass.PARCEL_MEDIA_COMMAND_SIZE:
  614. // leave next two integers as LSL_Integer
  615. verbatim = 2;
  616. break;
  617. }
  618. }
  619. }
  620. newarray[i] = obj;
  621. }
  622. return new LSL_List(newarray);
  623. }
  624. public static int xmrHashCode(int i)
  625. {
  626. return i.GetHashCode();
  627. }
  628. public static int xmrHashCode(double f)
  629. {
  630. return f.GetHashCode();
  631. }
  632. public static int xmrHashCode(object o)
  633. {
  634. return o.GetHashCode();
  635. }
  636. public static int xmrHashCode(string s)
  637. {
  638. return s.GetHashCode();
  639. }
  640. public string xmrTypeName(object o)
  641. {
  642. /*
  643. * Basic types return constant strings of the script-visible type name.
  644. */
  645. if(o is XMR_Array)
  646. return "array";
  647. if(o is bool)
  648. return "bool";
  649. if(o is char)
  650. return "char";
  651. if(o is Exception)
  652. return "exception";
  653. if(o is double)
  654. return "float";
  655. if(o is float)
  656. return "float";
  657. if(o is LSL_Float)
  658. return "float";
  659. if(o is int)
  660. return "integer";
  661. if(o is LSL_Integer)
  662. return "integer";
  663. if(o is LSL_List)
  664. return "list";
  665. if(o is LSL_Rotation)
  666. return "rotation";
  667. if(o is LSL_String)
  668. return "string";
  669. if(o is string)
  670. return "string";
  671. if(o is LSL_Vector)
  672. return "vector";
  673. // A script-defined interface is represented as an array of delegates.
  674. // If that is the case, convert it to the object of the script-defined
  675. // class that is implementing the interface. This should let the next
  676. // step get the script-defined type name of the object.
  677. if(o is Delegate[])
  678. o = ((Delegate[])o)[0].Target;
  679. // If script-defined class instance, get the script-defined
  680. // type name.
  681. if(o is XMRSDTypeClObj)
  682. return ((XMRSDTypeClObj)o).sdtcClass.longName.val;
  683. // If it's a delegate, maybe we can look up its script-defined type name.
  684. Type ot = o.GetType();
  685. if(o is Delegate)
  686. {
  687. String os;
  688. if(m_ObjCode.sdDelTypes.TryGetValue(ot, out os))
  689. return os;
  690. }
  691. // Don't know what it is, get the C#-level type name.
  692. return ot.ToString();
  693. }
  694. /**
  695. * @brief Call the current state's event handler.
  696. * @param ev = as returned by xmrEventDequeue saying which event handler to call
  697. * and what argument list to pass to it. The llDetect...() parameters
  698. * are as currently set for the script (use xmrEventLoadDets to set how
  699. * you want them to be different).
  700. */
  701. public void xmrEventCallHandler(LSL_List ev)
  702. {
  703. object[] data = ev.Data;
  704. int evc = (int)(ev.GetLSLIntegerItem(0).value & 0xFFFFFFFF);
  705. ScriptEventHandler seh = m_ObjCode.scriptEventHandlerTable[stateCode, evc];
  706. if(seh != null)
  707. {
  708. int nargs = data.Length - 1;
  709. object[] args = new object[nargs];
  710. Array.Copy(data, 1, args, 0, nargs);
  711. object[] saveEHArgs = this.ehArgs;
  712. ScriptEventCode saveEventCode = this.eventCode;
  713. this.ehArgs = args;
  714. this.eventCode = (ScriptEventCode)evc;
  715. seh(this);
  716. this.ehArgs = saveEHArgs;
  717. this.eventCode = saveEventCode;
  718. }
  719. }
  720. /**
  721. * @brief These conversions throw exceptions if there is anything stinky...
  722. */
  723. public double xmrString2Float(string s)
  724. {
  725. return double.Parse(s, CultureInfo.InvariantCulture);
  726. }
  727. public int xmrString2Integer(string s)
  728. {
  729. s = s.Trim();
  730. if(s.StartsWith("0x") || s.StartsWith("0X"))
  731. return int.Parse(s.Substring(2), NumberStyles.HexNumber);
  732. return int.Parse(s, CultureInfo.InvariantCulture);
  733. }
  734. public LSL_Rotation xmrString2Rotation(string s)
  735. {
  736. s = s.Trim();
  737. if(!s.StartsWith("<") || !s.EndsWith(">"))
  738. throw new FormatException("doesn't begin with < and end with >");
  739. s = s.Substring(1, s.Length - 2);
  740. string[] splitup = s.Split(justacomma, 5);
  741. if(splitup.Length != 4)
  742. throw new FormatException("doesn't have exactly 3 commas");
  743. double x = double.Parse(splitup[0], CultureInfo.InvariantCulture);
  744. double y = double.Parse(splitup[1], CultureInfo.InvariantCulture);
  745. double z = double.Parse(splitup[2], CultureInfo.InvariantCulture);
  746. double w = double.Parse(splitup[3], CultureInfo.InvariantCulture);
  747. return new LSL_Rotation(x, y, z, w);
  748. }
  749. public LSL_Vector xmrString2Vector(string s)
  750. {
  751. s = s.Trim();
  752. if(!s.StartsWith("<") || !s.EndsWith(">"))
  753. throw new FormatException("doesn't begin with < and end with >");
  754. s = s.Substring(1, s.Length - 2);
  755. string[] splitup = s.Split(justacomma, 4);
  756. if(splitup.Length != 3)
  757. throw new FormatException("doesn't have exactly 2 commas");
  758. double x = double.Parse(splitup[0], CultureInfo.InvariantCulture);
  759. double y = double.Parse(splitup[1], CultureInfo.InvariantCulture);
  760. double z = double.Parse(splitup[2], CultureInfo.InvariantCulture);
  761. return new LSL_Vector(x, y, z);
  762. }
  763. /**
  764. * @brief Access C#-style formatted numeric conversions.
  765. */
  766. public string xmrFloat2String(double val, string fmt)
  767. {
  768. return val.ToString(fmt, CultureInfo.InvariantCulture);
  769. }
  770. public string xmrInteger2String(int val, string fmt)
  771. {
  772. return val.ToString(fmt, CultureInfo.InvariantCulture);
  773. }
  774. public string xmrRotation2String(LSL_Rotation val, string fmt)
  775. {
  776. return "<" + val.x.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  777. val.y.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  778. val.z.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  779. val.s.ToString(fmt, CultureInfo.InvariantCulture) + ">";
  780. }
  781. public string xmrVector2String(LSL_Vector val, string fmt)
  782. {
  783. return "<" + val.x.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  784. val.y.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  785. val.z.ToString(fmt, CultureInfo.InvariantCulture) + ">";
  786. }
  787. /**
  788. * @brief Get a delegate for a script-defined function.
  789. * @param name = name of the function including arg types, eg,
  790. * "Verify(array,list,string)"
  791. * @param sig = script-defined type name
  792. * @param targ = function's 'this' pointer or null if static
  793. * @returns delegate for the script-defined function
  794. */
  795. public Delegate GetScriptMethodDelegate(string name, string sig, object targ)
  796. {
  797. DynamicMethod dm = m_ObjCode.dynamicMethods[name];
  798. TokenDeclSDTypeDelegate dt = (TokenDeclSDTypeDelegate)m_ObjCode.sdObjTypesName[sig];
  799. return dm.CreateDelegate(dt.GetSysType(), targ);
  800. }
  801. /**
  802. * @brief Try to cast the thrown object to the given script-defined type.
  803. * @param thrown = what object was thrown
  804. * @param inst = what script instance we are running in
  805. * @param sdtypeindex = script-defined type to try to cast it to
  806. * @returns null: thrown is not castable to sdtypename
  807. * else: an object casted to sdtypename
  808. */
  809. public static object XMRSDTypeCatchTryCastToSDType(object thrown, XMRInstAbstract inst, int sdtypeindex)
  810. {
  811. TokenDeclSDType sdType = inst.m_ObjCode.sdObjTypesIndx[sdtypeindex];
  812. // If it is a script-defined interface object, convert to the original XMRSDTypeClObj.
  813. if(thrown is Delegate[])
  814. {
  815. thrown = ((Delegate[])thrown)[0].Target;
  816. }
  817. // If it is a script-defined delegate object, make sure it is an instance of the expected type.
  818. if(thrown is Delegate)
  819. {
  820. Type ot = thrown.GetType();
  821. Type tt = sdType.GetSysType();
  822. return (ot == tt) ? thrown : null;
  823. }
  824. // If it is a script-defined class object, make sure it is an instance of the expected class.
  825. if(thrown is XMRSDTypeClObj)
  826. {
  827. // Step from the object's actual class rootward.
  828. // If we find the requested class along the way, the cast is valid.
  829. // If we run off the end of the root, the cast is not valid.
  830. for(TokenDeclSDTypeClass ac = ((XMRSDTypeClObj)thrown).sdtcClass; ac != null; ac = ac.extends)
  831. {
  832. if(ac == sdType)
  833. return thrown;
  834. }
  835. }
  836. // Don't know what it is, assume it is not what caller wants.
  837. return null;
  838. }
  839. /**
  840. * @brief Allocate and access fixed-dimension arrays.
  841. */
  842. public static object xmrFixedArrayAllocC(int len)
  843. {
  844. return new char[len];
  845. }
  846. public static object xmrFixedArrayAllocF(int len)
  847. {
  848. return new double[len];
  849. }
  850. public static object xmrFixedArrayAllocI(int len)
  851. {
  852. return new int[len];
  853. }
  854. public static object xmrFixedArrayAllocO(int len)
  855. {
  856. return new object[len];
  857. }
  858. public static char xmrFixedArrayGetC(object arr, int idx)
  859. {
  860. return ((char[])arr)[idx];
  861. }
  862. public static double xmrFixedArrayGetF(object arr, int idx)
  863. {
  864. return ((double[])arr)[idx];
  865. }
  866. public static int xmrFixedArrayGetI(object arr, int idx)
  867. {
  868. return ((int[])arr)[idx];
  869. }
  870. public static object xmrFixedArrayGetO(object arr, int idx)
  871. {
  872. return ((object[])arr)[idx];
  873. }
  874. public static void xmrFixedArraySetC(object arr, int idx, char val)
  875. {
  876. ((char[])arr)[idx] = val;
  877. }
  878. public static void xmrFixedArraySetF(object arr, int idx, double val)
  879. {
  880. ((double[])arr)[idx] = val;
  881. }
  882. public static void xmrFixedArraySetI(object arr, int idx, int val)
  883. {
  884. ((int[])arr)[idx] = val;
  885. }
  886. public static void xmrFixedArraySetO(object arr, int idx, object val)
  887. {
  888. ((object[])arr)[idx] = val;
  889. }
  890. /**
  891. * @brief Copy from one script-defined array to another.
  892. * @param srcobj = source script-defined array class object pointer
  893. * @param srcstart = offset in source array to start copying from
  894. * @param dstobj = destination script-defined array class object pointer
  895. * @param dststart = offset in destination arry to start copying to
  896. * @param count = number of elements to copy
  897. */
  898. public static void xmrArrayCopy(object srcobj, int srcstart, object dstobj, int dststart, int count)
  899. {
  900. // The script writer should only pass us script-defined class objects.
  901. // Throw exception otherwise.
  902. XMRSDTypeClObj srcsdt = (XMRSDTypeClObj)srcobj;
  903. XMRSDTypeClObj dstsdt = (XMRSDTypeClObj)dstobj;
  904. // Get the script-visible type name of the arrays, brackets and all.
  905. string srctypename = srcsdt.sdtcClass.longName.val;
  906. string dsttypename = dstsdt.sdtcClass.longName.val;
  907. // The part before the first '[' of each should match exactly,
  908. // meaning the basic data type (eg, float, List<string>) is the same.
  909. // And there must be a '[' in each meaning that it is a script-defined array type.
  910. int i = srctypename.IndexOf('[');
  911. int j = dsttypename.IndexOf('[');
  912. if((i < 0) || (j < 0))
  913. throw new InvalidCastException("non-array passed: " + srctypename + " and/or " + dsttypename);
  914. if((i != j) || !srctypename.StartsWith(dsttypename.Substring(0, j)))
  915. throw new ArrayTypeMismatchException(srctypename + " vs " + dsttypename);
  916. // The number of brackets must match exactly.
  917. // This permits copying from something like a float[,][] to something like a float[][].
  918. // But you cannot copy from a float[][] to a float[] or wisa wersa.
  919. // Counting either '[' or ']' would work equally well.
  920. int srclen = srctypename.Length;
  921. int dstlen = dsttypename.Length;
  922. int srcjags = 0;
  923. int dstjags = 0;
  924. while(++i < srclen)
  925. if(srctypename[i] == ']')
  926. srcjags++;
  927. while(++j < dstlen)
  928. if(dsttypename[j] == ']')
  929. dstjags++;
  930. if(dstjags != srcjags)
  931. throw new ArrayTypeMismatchException(srctypename + " vs " + dsttypename);
  932. // Perform the copy.
  933. Array srcarray = (Array)srcsdt.instVars.iarObjects[0];
  934. Array dstarray = (Array)dstsdt.instVars.iarObjects[0];
  935. Array.Copy(srcarray, srcstart, dstarray, dststart, count);
  936. }
  937. /**
  938. * @brief Copy from an array to a list.
  939. * @param srcar = the array to copy from
  940. * @param start = where to start in the array
  941. * @param count = number of elements
  942. * @returns the list
  943. */
  944. public static LSL_List xmrArray2List(object srcar, int start, int count)
  945. {
  946. // Get the script-visible type of the array.
  947. // We only do arrays.
  948. XMRSDTypeClObj array = (XMRSDTypeClObj)srcar;
  949. TokenDeclSDTypeClass sdtClass = array.sdtcClass;
  950. if(sdtClass.arrayOfRank == 0)
  951. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  952. // Validate objects they want to put in the list.
  953. // We can't allow anything funky that OpenSim runtime doesn't expect.
  954. Array srcarray = (Array)array.instVars.iarObjects[0];
  955. object[] output = new object[count];
  956. for(int i = 0; i < count; i++)
  957. {
  958. object src = srcarray.GetValue(i + start);
  959. if(src == null)
  960. throw new NullReferenceException("null element " + i);
  961. if(src is double)
  962. {
  963. output[i] = new LSL_Float((double)src);
  964. continue;
  965. }
  966. if(src is int)
  967. {
  968. output[i] = new LSL_Integer((int)src);
  969. continue;
  970. }
  971. if(src is LSL_Rotation)
  972. {
  973. output[i] = src;
  974. continue;
  975. }
  976. if(src is LSL_Vector)
  977. {
  978. output[i] = src;
  979. continue;
  980. }
  981. if(src is string)
  982. {
  983. output[i] = new LSL_String((string)src);
  984. continue;
  985. }
  986. throw new InvalidCastException("invalid element " + i + " type " + src.GetType().Name);
  987. }
  988. // Make a list out of that now immutable array.
  989. return new LSL_List(output);
  990. }
  991. /**
  992. * @brief Copy from a list to an array.
  993. * @param srclist = list to copy from
  994. * @param srcstart = where to start in the list
  995. * @param dstobj = array to copy to
  996. * @param dststart = where to start in the array
  997. * @param count = number of elements
  998. */
  999. public static void xmrList2Array(LSL_List srclist, int srcstart, object dstobj, int dststart, int count)
  1000. {
  1001. // Get the script-visible type of the destination.
  1002. // We only do arrays.
  1003. XMRSDTypeClObj dstarray = (XMRSDTypeClObj)dstobj;
  1004. TokenDeclSDTypeClass sdtClass = dstarray.sdtcClass;
  1005. if(sdtClass.arrayOfType == null)
  1006. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  1007. // Copy from the immutable array to the mutable array.
  1008. // Strip off any LSL wrappers as the script code doesn't expect any.
  1009. object[] srcarr = srclist.Data;
  1010. Array dstarr = (Array)dstarray.instVars.iarObjects[0];
  1011. for(int i = 0; i < count; i++)
  1012. {
  1013. object obj = srcarr[i + srcstart];
  1014. if(obj is LSL_Float)
  1015. obj = ((LSL_Float)obj).value;
  1016. else if(obj is LSL_Integer)
  1017. obj = ((LSL_Integer)obj).value;
  1018. else if(obj is LSL_String)
  1019. obj = ((LSL_String)obj).m_string;
  1020. dstarr.SetValue(obj, i + dststart);
  1021. }
  1022. }
  1023. /**
  1024. * @brief Copy from an array of characters to a string.
  1025. * @param srcar = the array to copy from
  1026. * @param start = where to start in the array
  1027. * @param count = number of elements
  1028. * @returns the string
  1029. */
  1030. public static string xmrChars2String(object srcar, int start, int count)
  1031. {
  1032. // Make sure they gave us a script-defined array object.
  1033. XMRSDTypeClObj array = (XMRSDTypeClObj)srcar;
  1034. TokenDeclSDTypeClass sdtClass = array.sdtcClass;
  1035. if(sdtClass.arrayOfRank == 0)
  1036. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  1037. // We get a type cast error from mono if they didn't give us a character array.
  1038. // But if it is ok, create a string from the requested characters.
  1039. char[] srcarray = (char[])array.instVars.iarObjects[0];
  1040. return new string(srcarray, start, count);
  1041. }
  1042. /**
  1043. * @brief Copy from a string to a character array.
  1044. * @param srcstr = string to copy from
  1045. * @param srcstart = where to start in the string
  1046. * @param dstobj = array to copy to
  1047. * @param dststart = where to start in the array
  1048. * @param count = number of elements
  1049. */
  1050. public static void xmrString2Chars(string srcstr, int srcstart, object dstobj, int dststart, int count)
  1051. {
  1052. // Make sure they gave us a script-defined array object.
  1053. XMRSDTypeClObj dstarray = (XMRSDTypeClObj)dstobj;
  1054. TokenDeclSDTypeClass sdtClass = dstarray.sdtcClass;
  1055. if(sdtClass.arrayOfType == null)
  1056. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  1057. // We get a type cast error from mono if they didn't give us a character array.
  1058. // But if it is ok, copy from the string to the character array.
  1059. char[] dstarr = (char[])dstarray.instVars.iarObjects[0];
  1060. for(int i = 0; i < count; i++)
  1061. dstarr[i + dststart] = srcstr[i + srcstart];
  1062. }
  1063. /**
  1064. * @brief Exception-related runtime calls.
  1065. */
  1066. // Return exception message (no type information just the message)
  1067. public static string xmrExceptionMessage(Exception ex)
  1068. {
  1069. return ex.Message;
  1070. }
  1071. // Return stack trace (no type or message, just stack trace lines: at ... \n)
  1072. public string xmrExceptionStackTrace(Exception ex)
  1073. {
  1074. return XMRExceptionStackString(ex);
  1075. }
  1076. // Return value thrown by a throw statement
  1077. public static object xmrExceptionThrownValue(Exception ex)
  1078. {
  1079. return ((ScriptThrownException)ex).thrown;
  1080. }
  1081. // Return exception's short type name, eg, NullReferenceException, ScriptThrownException, etc.
  1082. public static string xmrExceptionTypeName(Exception ex)
  1083. {
  1084. return ex.GetType().Name;
  1085. }
  1086. // internal use only: converts any IL addresses in script-defined methods to source location equivalent
  1087. // Mono ex.StackTrace:
  1088. // at OpenSim.Region.ScriptEngine.YEngine.TypeCast.ObjectToInteger (System.Object x) [0x0005e] in /home/kunta/opensim-0.9/addon-modules/YEngine/Module/MMRScriptTypeCast.cs:750
  1089. // at (wrapper dynamic-method) System.Object:default state_entry (OpenSim.Region.ScriptEngine.YEngine.XMRInstAbstract) [0x00196]
  1090. // Microsoft ex.StackTrace:
  1091. // at OpenSim.Region.ScriptEngine.YEngine.TypeCast.ObjectToInteger(Object x) in C:\Users\mrieker\opensim-0.9-source\addon-modules\YEngine\Module\MMRScriptTypeCast.cs:line 750
  1092. // at default state_entry (XMRInstAbstract )
  1093. public string XMRExceptionStackString(Exception ex)
  1094. {
  1095. string stwhole = ex.StackTrace;
  1096. string[] stlines = stwhole.Split(new char[] { '\n' });
  1097. StringBuilder sb = new StringBuilder();
  1098. foreach(string st in stlines)
  1099. {
  1100. string stline = st.Trim();
  1101. if(stline == "")
  1102. continue;
  1103. // strip 'at' off the front of line
  1104. if(stline.StartsWith("at "))
  1105. {
  1106. stline = stline.Substring(3);
  1107. }
  1108. // strip '(wrapper ...' off front of line
  1109. if(stline.StartsWith("(wrapper dynamic-method) System.Object:"))
  1110. {
  1111. stline = stline.Substring(39);
  1112. }
  1113. // strip the (systemargtypes...) from our dynamic method names cuz it's messy
  1114. // 'default state_entry (XMRInstAbstract )'
  1115. // => 'default state_entry'
  1116. // 'CallSomethingThatThrows(string) (OpenSim.Region.ScriptEngine.YEngine.XMRInstance,string)'
  1117. // => 'CallSomethingThatThrows(string)'
  1118. int kwin = stline.IndexOf(" in ");
  1119. int br0x = stline.IndexOf(" [0x");
  1120. int pastCloseParen = stline.Length;
  1121. if((kwin >= 0) && (br0x >= 0))
  1122. pastCloseParen = Math.Min(kwin, br0x);
  1123. else if(kwin >= 0)
  1124. pastCloseParen = kwin;
  1125. else if(br0x >= 0)
  1126. pastCloseParen = br0x;
  1127. else
  1128. pastCloseParen = stline.Length;
  1129. int endFuncName = pastCloseParen;
  1130. while(endFuncName > 0)
  1131. {
  1132. if(stline[--endFuncName] == '(')
  1133. break;
  1134. }
  1135. while(endFuncName > 0)
  1136. {
  1137. if(stline[endFuncName - 1] != ' ')
  1138. break;
  1139. --endFuncName;
  1140. }
  1141. string funcName = stline.Substring(0, endFuncName);
  1142. KeyValuePair<int, ScriptSrcLoc>[] srcLocs;
  1143. if(m_ObjCode.scriptSrcLocss.TryGetValue(funcName, out srcLocs))
  1144. {
  1145. stline = stline.Substring(0, endFuncName) + stline.Substring(pastCloseParen);
  1146. kwin = stline.IndexOf(" in ");
  1147. br0x = stline.IndexOf(" [0x");
  1148. }
  1149. // keyword 'in' is just before filename:linenumber that goes to end of line
  1150. // trim up the corresponding filename (ie, remove useless path info)
  1151. if(kwin >= 0)
  1152. {
  1153. int begfn = kwin + 4;
  1154. int slash = begfn;
  1155. for(int i = begfn; i < stline.Length; i++)
  1156. {
  1157. char c = stline[i];
  1158. if((c == '/') || (c == '\\'))
  1159. slash = i + 1;
  1160. }
  1161. stline = stline.Substring(0, begfn) + stline.Substring(slash);
  1162. }
  1163. else if(srcLocs != null)
  1164. {
  1165. // no filename:linenumber info, try to convert IL offset
  1166. if(br0x >= 0)
  1167. {
  1168. try
  1169. {
  1170. int begiloffs = br0x + 4;
  1171. int endiloffs = stline.IndexOf("]", begiloffs);
  1172. int iloffset = int.Parse(stline.Substring(begiloffs, endiloffs - begiloffs),
  1173. System.Globalization.NumberStyles.HexNumber);
  1174. int srcLocIdx;
  1175. int srcLocLen = srcLocs.Length;
  1176. for(srcLocIdx = 0; ++srcLocIdx < srcLocLen;)
  1177. {
  1178. if(iloffset < srcLocs[srcLocIdx].Key)
  1179. break;
  1180. }
  1181. ScriptSrcLoc srcLoc = srcLocs[--srcLocIdx].Value;
  1182. stline = stline.Substring(0, br0x) + " <" +
  1183. srcLoc.file + '(' + srcLoc.line + ',' + srcLoc.posn + ")>";
  1184. }
  1185. catch
  1186. {
  1187. }
  1188. }
  1189. }
  1190. // put edited line in output string
  1191. if(sb.Length > 0)
  1192. sb.AppendLine();
  1193. sb.Append(" at ");
  1194. sb.Append(stline);
  1195. }
  1196. return sb.ToString();
  1197. }
  1198. /**
  1199. * @brief List fonts available.
  1200. */
  1201. public LSL_List xmrFontsAvailable()
  1202. {
  1203. System.Drawing.FontFamily[] families = System.Drawing.FontFamily.Families;
  1204. object[] output = new object[families.Length];
  1205. for(int i = 0; i < families.Length; i++)
  1206. output[i] = new LSL_String(families[i].Name);
  1207. return new LSL_List(output);
  1208. }
  1209. /************************\
  1210. * Used by decompiler *
  1211. \************************/
  1212. public bool xmrRotationToBool(LSL_Rotation x)
  1213. {
  1214. return TypeCast.RotationToBool(x);
  1215. }
  1216. public bool xmrStringToBool(string x)
  1217. {
  1218. return TypeCast.StringToBool(x);
  1219. }
  1220. public bool xmrVectorToBool(LSL_Vector x)
  1221. {
  1222. return TypeCast.VectorToBool(x);
  1223. }
  1224. public bool xmrKeyToBool(string x)
  1225. {
  1226. return TypeCast.KeyToBool(x);
  1227. }
  1228. public bool xmrListToBool(LSL_List x)
  1229. {
  1230. return TypeCast.ListToBool(x);
  1231. }
  1232. public int xmrStringCompare(string x, string y)
  1233. {
  1234. return string.Compare(x, y);
  1235. }
  1236. /**
  1237. * @brief types of data we serialize
  1238. */
  1239. private enum Ser: byte
  1240. {
  1241. NULL,
  1242. EVENTCODE,
  1243. LSLFLOAT,
  1244. LSLINT,
  1245. LSLKEY,
  1246. LSLLIST,
  1247. LSLROT,
  1248. LSLSTR,
  1249. LSLVEC,
  1250. SYSARRAY,
  1251. SYSDOUB,
  1252. SYSFLOAT,
  1253. SYSINT,
  1254. SYSSTR,
  1255. XMRARRAY,
  1256. DUPREF,
  1257. SYSBOOL,
  1258. XMRINST,
  1259. DELEGATE,
  1260. SDTCLOBJ,
  1261. SYSCHAR,
  1262. SYSERIAL,
  1263. THROWNEX
  1264. }
  1265. /**
  1266. * @brief Write state out to a stream.
  1267. * Do not change script state.
  1268. */
  1269. public void MigrateOut(BinaryWriter mow)
  1270. {
  1271. try
  1272. {
  1273. this.migrateOutWriter = mow;
  1274. this.migrateOutObjects = new Dictionary<object, int>();
  1275. this.migrateOutLists = new Dictionary<object[], ObjLslList>();
  1276. this.SendObjValue(this.ehArgs);
  1277. mow.Write(this.doGblInit);
  1278. mow.Write(this.stateCode);
  1279. mow.Write((int)this.eventCode);
  1280. this.glblVars.SendArrays(this.SendObjValue);
  1281. if(this.newStateCode >= 0)
  1282. {
  1283. mow.Write("**newStateCode**");
  1284. mow.Write(this.newStateCode);
  1285. }
  1286. for(XMRStackFrame thisSF = this.stackFrames; thisSF != null; thisSF = thisSF.nextSF)
  1287. {
  1288. mow.Write(thisSF.funcName);
  1289. mow.Write(thisSF.callNo);
  1290. this.SendObjValue(thisSF.objArray);
  1291. }
  1292. mow.Write("");
  1293. }
  1294. finally
  1295. {
  1296. this.migrateOutWriter = null;
  1297. this.migrateOutObjects = null;
  1298. this.migrateOutLists = null;
  1299. }
  1300. }
  1301. /**
  1302. * @brief Write an object to the output stream.
  1303. * @param graph = object to send
  1304. */
  1305. private BinaryWriter migrateOutWriter;
  1306. private Dictionary<object, int> migrateOutObjects;
  1307. private Dictionary<object[], ObjLslList> migrateOutLists;
  1308. public void SendObjValue(object graph)
  1309. {
  1310. BinaryWriter mow = this.migrateOutWriter;
  1311. // Value types (including nulls) are always output directly.
  1312. if(graph == null)
  1313. {
  1314. mow.Write((byte)Ser.NULL);
  1315. return;
  1316. }
  1317. if(graph is ScriptEventCode)
  1318. {
  1319. mow.Write((byte)Ser.EVENTCODE);
  1320. mow.Write((int)graph);
  1321. return;
  1322. }
  1323. if(graph is LSL_Float)
  1324. {
  1325. mow.Write((byte)Ser.LSLFLOAT);
  1326. mow.Write((double)((LSL_Float)graph).value);
  1327. return;
  1328. }
  1329. if(graph is LSL_Integer)
  1330. {
  1331. mow.Write((byte)Ser.LSLINT);
  1332. mow.Write((int)((LSL_Integer)graph).value);
  1333. return;
  1334. }
  1335. if(graph is LSL_Key)
  1336. {
  1337. mow.Write((byte)Ser.LSLKEY);
  1338. LSL_Key key = (LSL_Key)graph;
  1339. SendObjValue(key.m_string); // m_string can be null
  1340. return;
  1341. }
  1342. if(graph is LSL_Rotation)
  1343. {
  1344. mow.Write((byte)Ser.LSLROT);
  1345. mow.Write((double)((LSL_Rotation)graph).x);
  1346. mow.Write((double)((LSL_Rotation)graph).y);
  1347. mow.Write((double)((LSL_Rotation)graph).z);
  1348. mow.Write((double)((LSL_Rotation)graph).s);
  1349. return;
  1350. }
  1351. if(graph is LSL_String)
  1352. {
  1353. mow.Write((byte)Ser.LSLSTR);
  1354. LSL_String str = (LSL_String)graph;
  1355. SendObjValue(str.m_string); // m_string can be null
  1356. return;
  1357. }
  1358. if(graph is LSL_Vector)
  1359. {
  1360. mow.Write((byte)Ser.LSLVEC);
  1361. mow.Write((double)((LSL_Vector)graph).x);
  1362. mow.Write((double)((LSL_Vector)graph).y);
  1363. mow.Write((double)((LSL_Vector)graph).z);
  1364. return;
  1365. }
  1366. if(graph is bool)
  1367. {
  1368. mow.Write((byte)Ser.SYSBOOL);
  1369. mow.Write((bool)graph);
  1370. return;
  1371. }
  1372. if(graph is double)
  1373. {
  1374. mow.Write((byte)Ser.SYSDOUB);
  1375. mow.Write((double)graph);
  1376. return;
  1377. }
  1378. if(graph is float)
  1379. {
  1380. mow.Write((byte)Ser.SYSFLOAT);
  1381. mow.Write((float)graph);
  1382. return;
  1383. }
  1384. if(graph is int)
  1385. {
  1386. mow.Write((byte)Ser.SYSINT);
  1387. mow.Write((int)graph);
  1388. return;
  1389. }
  1390. if(graph is char)
  1391. {
  1392. mow.Write((byte)Ser.SYSCHAR);
  1393. mow.Write((char)graph);
  1394. return;
  1395. }
  1396. // Script instance pointer is always just that.
  1397. if(graph == this)
  1398. {
  1399. mow.Write((byte)Ser.XMRINST);
  1400. return;
  1401. }
  1402. // Convert lists to object type.
  1403. // This is compatible with old migration data and also
  1404. // two vars pointing to same list won't duplicate it.
  1405. if(graph is LSL_List)
  1406. {
  1407. object[] data = ((LSL_List)graph).Data;
  1408. ObjLslList oll;
  1409. if(!this.migrateOutLists.TryGetValue(data, out oll))
  1410. {
  1411. oll = new ObjLslList();
  1412. oll.objarray = data;
  1413. this.migrateOutLists[data] = oll;
  1414. }
  1415. graph = oll;
  1416. }
  1417. // If this same exact object was already serialized,
  1418. // just output an index telling the receiver to use
  1419. // that same old object, rather than creating a whole
  1420. // new object with the same values. Also this prevents
  1421. // self-referencing objects (like arrays) from causing
  1422. // an infinite loop.
  1423. int ident;
  1424. if(this.migrateOutObjects.TryGetValue(graph, out ident))
  1425. {
  1426. mow.Write((byte)Ser.DUPREF);
  1427. mow.Write(ident);
  1428. return;
  1429. }
  1430. // Object not seen before, save its address with an unique
  1431. // ident number that the receiver can easily regenerate.
  1432. ident = this.migrateOutObjects.Count;
  1433. this.migrateOutObjects.Add(graph, ident);
  1434. // Now output the object's value(s).
  1435. // If the object self-references, the object is alreay entered
  1436. // in the dictionary and so the self-reference will just emit
  1437. // a DUPREF tag instead of trying to output the whole object
  1438. // again.
  1439. if(graph is ObjLslList)
  1440. {
  1441. mow.Write((byte)Ser.LSLLIST);
  1442. ObjLslList oll = (ObjLslList)graph;
  1443. SendObjValue(oll.objarray);
  1444. }
  1445. else if(graph is XMR_Array)
  1446. {
  1447. mow.Write((byte)Ser.XMRARRAY);
  1448. ((XMR_Array)graph).SendArrayObj(this.SendObjValue);
  1449. }
  1450. else if(graph is Array)
  1451. {
  1452. Array array = (Array)graph;
  1453. mow.Write((byte)Ser.SYSARRAY);
  1454. mow.Write(SysType2String(array.GetType().GetElementType()));
  1455. mow.Write((int)array.Length);
  1456. for(int i = 0; i < array.Length; i++)
  1457. this.SendObjValue(array.GetValue(i));
  1458. }
  1459. else if(graph is string)
  1460. {
  1461. mow.Write((byte)Ser.SYSSTR);
  1462. mow.Write((string)graph);
  1463. }
  1464. else if(graph is Delegate)
  1465. {
  1466. Delegate del = (Delegate)graph;
  1467. mow.Write((byte)Ser.DELEGATE);
  1468. mow.Write(del.Method.Name);
  1469. Type delType = del.GetType();
  1470. foreach(KeyValuePair<string, TokenDeclSDType> kvp in m_ObjCode.sdObjTypesName)
  1471. {
  1472. TokenDeclSDType sdt = kvp.Value;
  1473. if(sdt is TokenDeclSDTypeDelegate)
  1474. {
  1475. TokenDeclSDTypeDelegate sdtd = (TokenDeclSDTypeDelegate)sdt;
  1476. if(sdtd.GetSysType() == delType)
  1477. {
  1478. mow.Write(kvp.Key);
  1479. goto found;
  1480. }
  1481. }
  1482. }
  1483. throw new Exception("cant find script-defined delegate for " + del.Method.Name + " type " + del.GetType());
  1484. found:
  1485. SendObjValue(del.Target);
  1486. }
  1487. else if(graph is XMRSDTypeClObj)
  1488. {
  1489. mow.Write((byte)Ser.SDTCLOBJ);
  1490. ((XMRSDTypeClObj)graph).Capture(this.SendObjValue);
  1491. }
  1492. else if(graph is ScriptThrownException)
  1493. {
  1494. MemoryStream memoryStream = new MemoryStream();
  1495. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1496. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1497. bformatter.Serialize(memoryStream, graph);
  1498. byte[] rawBytes = memoryStream.ToArray();
  1499. mow.Write((byte)Ser.THROWNEX);
  1500. mow.Write((int)rawBytes.Length);
  1501. mow.Write(rawBytes);
  1502. SendObjValue(((ScriptThrownException)graph).thrown);
  1503. }
  1504. else
  1505. {
  1506. MemoryStream memoryStream = new MemoryStream();
  1507. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1508. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1509. bformatter.Serialize(memoryStream, graph);
  1510. byte[] rawBytes = memoryStream.ToArray();
  1511. mow.Write((byte)Ser.SYSERIAL);
  1512. mow.Write((int)rawBytes.Length);
  1513. mow.Write(rawBytes);
  1514. }
  1515. }
  1516. /**
  1517. * @brief Use short strings for known type names.
  1518. */
  1519. private static string SysType2String(Type type)
  1520. {
  1521. if(type.IsArray && (type.GetArrayRank() == 1))
  1522. {
  1523. string str = KnownSysType2String(type.GetElementType());
  1524. if(str != null)
  1525. return str + "[]";
  1526. }
  1527. else
  1528. {
  1529. string str = KnownSysType2String(type);
  1530. if(str != null)
  1531. return str;
  1532. }
  1533. return type.ToString();
  1534. }
  1535. private static string KnownSysType2String(Type type)
  1536. {
  1537. if(type == typeof(bool))
  1538. return "bo";
  1539. if(type == typeof(char))
  1540. return "ch";
  1541. if(type == typeof(Delegate))
  1542. return "de";
  1543. if(type == typeof(double))
  1544. return "do";
  1545. if(type == typeof(float))
  1546. return "fl";
  1547. if(type == typeof(int))
  1548. return "in";
  1549. if(type == typeof(LSL_List))
  1550. return "li";
  1551. if(type == typeof(object))
  1552. return "ob";
  1553. if(type == typeof(LSL_Rotation))
  1554. return "ro";
  1555. if(type == typeof(XMRSDTypeClObj))
  1556. return "sc";
  1557. if(type == typeof(string))
  1558. return "st";
  1559. if(type == typeof(LSL_Vector))
  1560. return "ve";
  1561. if(type == typeof(XMR_Array))
  1562. return "xa";
  1563. return null;
  1564. }
  1565. private static Type String2SysType(string str)
  1566. {
  1567. if(str.EndsWith("[]"))
  1568. return String2SysType(str.Substring(0, str.Length - 2)).MakeArrayType();
  1569. if(str == "bo")
  1570. return typeof(bool);
  1571. if(str == "ch")
  1572. return typeof(char);
  1573. if(str == "de")
  1574. return typeof(Delegate);
  1575. if(str == "do")
  1576. return typeof(double);
  1577. if(str == "fl")
  1578. return typeof(float);
  1579. if(str == "in")
  1580. return typeof(int);
  1581. if(str == "li")
  1582. return typeof(LSL_List);
  1583. if(str == "ob")
  1584. return typeof(object);
  1585. if(str == "ro")
  1586. return typeof(LSL_Rotation);
  1587. if(str == "sc")
  1588. return typeof(XMRSDTypeClObj);
  1589. if(str == "st")
  1590. return typeof(string);
  1591. if(str == "ve")
  1592. return typeof(LSL_Vector);
  1593. if(str == "xa")
  1594. return typeof(XMR_Array);
  1595. return Type.GetType(str, true);
  1596. }
  1597. /**
  1598. * @brief Read state in from a stream.
  1599. */
  1600. public void MigrateIn(BinaryReader mir)
  1601. {
  1602. try
  1603. {
  1604. this.migrateInReader = mir;
  1605. this.migrateInObjects = new Dictionary<int, object>();
  1606. this.ehArgs = (object[])this.RecvObjValue();
  1607. this.doGblInit = mir.ReadBoolean();
  1608. this.stateCode = mir.ReadInt32();
  1609. this.eventCode = (ScriptEventCode)mir.ReadInt32();
  1610. this.newStateCode = -1;
  1611. this.glblVars.RecvArrays(this.RecvObjValue);
  1612. XMRStackFrame lastSF = null;
  1613. string funcName;
  1614. while((funcName = mir.ReadString()) != "")
  1615. {
  1616. if(funcName == "**newStateCode**")
  1617. {
  1618. this.newStateCode = mir.ReadInt32();
  1619. continue;
  1620. }
  1621. XMRStackFrame thisSF = new XMRStackFrame();
  1622. thisSF.funcName = funcName;
  1623. thisSF.callNo = mir.ReadInt32();
  1624. thisSF.objArray = (object[])this.RecvObjValue();
  1625. if(lastSF == null)
  1626. this.stackFrames = thisSF;
  1627. else
  1628. lastSF.nextSF = thisSF;
  1629. lastSF = thisSF;
  1630. }
  1631. }
  1632. finally
  1633. {
  1634. this.migrateInReader = null;
  1635. this.migrateInObjects = null;
  1636. }
  1637. }
  1638. /**
  1639. * @brief Read a single value from the stream.
  1640. * @returns value (boxed as needed)
  1641. */
  1642. private BinaryReader migrateInReader;
  1643. private Dictionary<int, object> migrateInObjects;
  1644. public object RecvObjValue()
  1645. {
  1646. BinaryReader mir = this.migrateInReader;
  1647. int ident = this.migrateInObjects.Count;
  1648. Ser code = (Ser)mir.ReadByte();
  1649. switch(code)
  1650. {
  1651. case Ser.NULL:
  1652. return null;
  1653. case Ser.EVENTCODE:
  1654. return (ScriptEventCode)mir.ReadInt32();
  1655. case Ser.LSLFLOAT:
  1656. return new LSL_Float(mir.ReadDouble());
  1657. case Ser.LSLINT:
  1658. return new LSL_Integer(mir.ReadInt32());
  1659. case Ser.LSLKEY:
  1660. return new LSL_Key((string)RecvObjValue());
  1661. case Ser.LSLLIST:
  1662. {
  1663. this.migrateInObjects.Add(ident, null); // placeholder
  1664. object[] data = (object[])RecvObjValue(); // read data, maybe using another index
  1665. LSL_List list = new LSL_List(data); // make LSL-level list
  1666. this.migrateInObjects[ident] = list; // fill in slot
  1667. return list;
  1668. }
  1669. case Ser.LSLROT:
  1670. {
  1671. double x = mir.ReadDouble();
  1672. double y = mir.ReadDouble();
  1673. double z = mir.ReadDouble();
  1674. double w = mir.ReadDouble();
  1675. return new LSL_Rotation(x, y, z, w);
  1676. }
  1677. case Ser.LSLSTR:
  1678. return new LSL_String((string)RecvObjValue());
  1679. case Ser.LSLVEC:
  1680. {
  1681. double x = mir.ReadDouble();
  1682. double y = mir.ReadDouble();
  1683. double z = mir.ReadDouble();
  1684. return new LSL_Vector(x, y, z);
  1685. }
  1686. case Ser.SYSARRAY:
  1687. {
  1688. Type eletype = String2SysType(mir.ReadString());
  1689. int length = mir.ReadInt32();
  1690. Array array = Array.CreateInstance(eletype, length);
  1691. this.migrateInObjects.Add(ident, array);
  1692. for(int i = 0; i < length; i++)
  1693. array.SetValue(RecvObjValue(), i);
  1694. return array;
  1695. }
  1696. case Ser.SYSBOOL:
  1697. return mir.ReadBoolean();
  1698. case Ser.SYSDOUB:
  1699. return mir.ReadDouble();
  1700. case Ser.SYSFLOAT:
  1701. return mir.ReadSingle();
  1702. case Ser.SYSINT:
  1703. return mir.ReadInt32();
  1704. case Ser.SYSCHAR:
  1705. return mir.ReadChar();
  1706. case Ser.SYSSTR:
  1707. string s = mir.ReadString();
  1708. this.migrateInObjects.Add(ident, s);
  1709. return s;
  1710. case Ser.XMRARRAY:
  1711. {
  1712. XMR_Array array = new XMR_Array(this);
  1713. this.migrateInObjects.Add(ident, array);
  1714. array.RecvArrayObj(this.RecvObjValue);
  1715. return array;
  1716. }
  1717. case Ser.DUPREF:
  1718. {
  1719. ident = mir.ReadInt32();
  1720. object obj = this.migrateInObjects[ident];
  1721. if(obj is ObjLslList)
  1722. obj = new LSL_List(((ObjLslList)obj).objarray);
  1723. return obj;
  1724. }
  1725. case Ser.XMRINST:
  1726. return this;
  1727. case Ser.DELEGATE:
  1728. this.migrateInObjects.Add(ident, null); // placeholder
  1729. string name = mir.ReadString(); // function name
  1730. string sig = mir.ReadString(); // delegate type
  1731. object targ = this.RecvObjValue(); // 'this' object
  1732. Delegate del = this.GetScriptMethodDelegate(name, sig, targ);
  1733. this.migrateInObjects[ident] = del; // actual value
  1734. return del;
  1735. case Ser.SDTCLOBJ:
  1736. XMRSDTypeClObj clobj = new XMRSDTypeClObj();
  1737. this.migrateInObjects.Add(ident, clobj);
  1738. clobj.Restore(this, this.RecvObjValue);
  1739. return clobj;
  1740. case Ser.SYSERIAL:
  1741. {
  1742. int rawLength = mir.ReadInt32();
  1743. byte[] rawBytes = mir.ReadBytes(rawLength);
  1744. MemoryStream memoryStream = new MemoryStream(rawBytes);
  1745. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1746. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1747. object graph = bformatter.Deserialize(memoryStream);
  1748. this.migrateInObjects.Add(ident, graph);
  1749. return graph;
  1750. }
  1751. case Ser.THROWNEX:
  1752. {
  1753. int rawLength = mir.ReadInt32();
  1754. byte[] rawBytes = mir.ReadBytes(rawLength);
  1755. MemoryStream memoryStream = new MemoryStream(rawBytes);
  1756. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1757. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1758. object graph = bformatter.Deserialize(memoryStream);
  1759. this.migrateInObjects.Add(ident, graph);
  1760. ((ScriptThrownException)graph).thrown = RecvObjValue();
  1761. return graph;
  1762. }
  1763. default:
  1764. throw new Exception("bad stream code " + code.ToString());
  1765. }
  1766. }
  1767. // wrapper around list object arrays to make sure they are always object types for migration purposes
  1768. private class ObjLslList
  1769. {
  1770. public object[] objarray;
  1771. }
  1772. }
  1773. // Any xmr...() methods that call CheckRun() must be tagged with this attribute
  1774. // so the ScriptCodeGen will know the method is non-trivial.
  1775. public class xmrMethodCallsCheckRunAttribute: Attribute
  1776. {
  1777. }
  1778. // Any xmr...() methods in xmrengtest that call Stub<somethingorother>() must be
  1779. // tagged with this attribute so the -builtins option will tell the user that
  1780. // they are a stub function.
  1781. public class xmrMethodIsNoisyAttribute: Attribute
  1782. {
  1783. }
  1784. // Any script callable methods that really return a key not a string should be
  1785. // tagged with this attribute so the compiler will know they return type key and
  1786. // not type string.
  1787. public class xmrMethodReturnsKeyAttribute: Attribute
  1788. {
  1789. }
  1790. [SerializableAttribute]
  1791. public class OutOfHeapException: Exception
  1792. {
  1793. public OutOfHeapException(int oldtotal, int newtotal, int limit)
  1794. : base("oldtotal=" + oldtotal + ", newtotal=" + newtotal + ", limit=" + limit)
  1795. {
  1796. }
  1797. }
  1798. [SerializableAttribute]
  1799. public class OutOfStackException: Exception
  1800. {
  1801. }
  1802. }