XMRInstAbstract.cs 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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. sf.nextSF = null;
  569. return sf.objArray;
  570. }
  571. /**
  572. * @brief Convert all LSL_Integers in a list to System.Int32s,
  573. * as required by llParcelMediaQuery().
  574. */
  575. public static LSL_List FixLLParcelMediaQuery(LSL_List oldlist)
  576. {
  577. object[] oldarray = oldlist.Data;
  578. int len = oldarray.Length;
  579. object[] newarray = new object[len];
  580. for(int i = 0; i < len; i++)
  581. {
  582. object obj = oldarray[i];
  583. if(obj is LSL_Integer)
  584. obj = (int)(LSL_Integer)obj;
  585. newarray[i] = obj;
  586. }
  587. return new LSL_List(newarray);
  588. }
  589. /**
  590. * @brief Convert *SOME* LSL_Integers in a list to System.Int32s,
  591. * as required by llParcelMediaCommandList().
  592. */
  593. public static LSL_List FixLLParcelMediaCommandList(LSL_List oldlist)
  594. {
  595. object[] oldarray = oldlist.Data;
  596. int len = oldarray.Length;
  597. object[] newarray = new object[len];
  598. int verbatim = 0;
  599. for(int i = 0; i < len; i++)
  600. {
  601. object obj = oldarray[i];
  602. if(--verbatim < 0)
  603. {
  604. if(obj is LSL_Integer)
  605. obj = (int)(LSL_Integer)obj;
  606. if(obj is int)
  607. {
  608. switch((int)obj)
  609. {
  610. case ScriptBaseClass.PARCEL_MEDIA_COMMAND_AUTO_ALIGN:
  611. // leave next integer as LSL_Integer
  612. verbatim = 1;
  613. break;
  614. case ScriptBaseClass.PARCEL_MEDIA_COMMAND_SIZE:
  615. // leave next two integers as LSL_Integer
  616. verbatim = 2;
  617. break;
  618. }
  619. }
  620. }
  621. newarray[i] = obj;
  622. }
  623. return new LSL_List(newarray);
  624. }
  625. public static int xmrHashCode(int i)
  626. {
  627. return i.GetHashCode();
  628. }
  629. public static int xmrHashCode(double f)
  630. {
  631. return f.GetHashCode();
  632. }
  633. public static int xmrHashCode(object o)
  634. {
  635. return o.GetHashCode();
  636. }
  637. public static int xmrHashCode(string s)
  638. {
  639. return s.GetHashCode();
  640. }
  641. public string xmrTypeName(object o)
  642. {
  643. /*
  644. * Basic types return constant strings of the script-visible type name.
  645. */
  646. if(o is XMR_Array)
  647. return "array";
  648. if(o is bool)
  649. return "bool";
  650. if(o is char)
  651. return "char";
  652. if(o is Exception)
  653. return "exception";
  654. if(o is double)
  655. return "float";
  656. if(o is float)
  657. return "float";
  658. if(o is LSL_Float)
  659. return "float";
  660. if(o is int)
  661. return "integer";
  662. if(o is LSL_Integer)
  663. return "integer";
  664. if(o is LSL_List)
  665. return "list";
  666. if(o is LSL_Rotation)
  667. return "rotation";
  668. if(o is LSL_String)
  669. return "string";
  670. if(o is string)
  671. return "string";
  672. if(o is LSL_Vector)
  673. return "vector";
  674. // A script-defined interface is represented as an array of delegates.
  675. // If that is the case, convert it to the object of the script-defined
  676. // class that is implementing the interface. This should let the next
  677. // step get the script-defined type name of the object.
  678. if(o is Delegate[])
  679. o = ((Delegate[])o)[0].Target;
  680. // If script-defined class instance, get the script-defined
  681. // type name.
  682. if(o is XMRSDTypeClObj)
  683. return ((XMRSDTypeClObj)o).sdtcClass.longName.val;
  684. // If it's a delegate, maybe we can look up its script-defined type name.
  685. Type ot = o.GetType();
  686. if(o is Delegate)
  687. {
  688. String os;
  689. if(m_ObjCode.sdDelTypes.TryGetValue(ot, out os))
  690. return os;
  691. }
  692. // Don't know what it is, get the C#-level type name.
  693. return ot.ToString();
  694. }
  695. /**
  696. * @brief Call the current state's event handler.
  697. * @param ev = as returned by xmrEventDequeue saying which event handler to call
  698. * and what argument list to pass to it. The llDetect...() parameters
  699. * are as currently set for the script (use xmrEventLoadDets to set how
  700. * you want them to be different).
  701. */
  702. public void xmrEventCallHandler(LSL_List ev)
  703. {
  704. object[] data = ev.Data;
  705. int evc = (int)(ev.GetLSLIntegerItem(0).value & 0xFFFFFFFF);
  706. ScriptEventHandler seh = m_ObjCode.scriptEventHandlerTable[stateCode, evc];
  707. if(seh != null)
  708. {
  709. int nargs = data.Length - 1;
  710. object[] args = new object[nargs];
  711. Array.Copy(data, 1, args, 0, nargs);
  712. object[] saveEHArgs = this.ehArgs;
  713. ScriptEventCode saveEventCode = this.eventCode;
  714. this.ehArgs = args;
  715. this.eventCode = (ScriptEventCode)evc;
  716. seh(this);
  717. this.ehArgs = saveEHArgs;
  718. this.eventCode = saveEventCode;
  719. }
  720. }
  721. /**
  722. * @brief These conversions throw exceptions if there is anything stinky...
  723. */
  724. public double xmrString2Float(string s)
  725. {
  726. return double.Parse(s, CultureInfo.InvariantCulture);
  727. }
  728. public int xmrString2Integer(string s)
  729. {
  730. s = s.Trim();
  731. if(s.StartsWith("0x") || s.StartsWith("0X"))
  732. return int.Parse(s.Substring(2), NumberStyles.HexNumber);
  733. return int.Parse(s, CultureInfo.InvariantCulture);
  734. }
  735. public LSL_Rotation xmrString2Rotation(string s)
  736. {
  737. s = s.Trim();
  738. if(!s.StartsWith("<") || !s.EndsWith(">"))
  739. throw new FormatException("doesn't begin with < and end with >");
  740. s = s.Substring(1, s.Length - 2);
  741. string[] splitup = s.Split(justacomma, 5);
  742. if(splitup.Length != 4)
  743. throw new FormatException("doesn't have exactly 3 commas");
  744. double x = double.Parse(splitup[0], CultureInfo.InvariantCulture);
  745. double y = double.Parse(splitup[1], CultureInfo.InvariantCulture);
  746. double z = double.Parse(splitup[2], CultureInfo.InvariantCulture);
  747. double w = double.Parse(splitup[3], CultureInfo.InvariantCulture);
  748. return new LSL_Rotation(x, y, z, w);
  749. }
  750. public LSL_Vector xmrString2Vector(string s)
  751. {
  752. s = s.Trim();
  753. if(!s.StartsWith("<") || !s.EndsWith(">"))
  754. throw new FormatException("doesn't begin with < and end with >");
  755. s = s.Substring(1, s.Length - 2);
  756. string[] splitup = s.Split(justacomma, 4);
  757. if(splitup.Length != 3)
  758. throw new FormatException("doesn't have exactly 2 commas");
  759. double x = double.Parse(splitup[0], CultureInfo.InvariantCulture);
  760. double y = double.Parse(splitup[1], CultureInfo.InvariantCulture);
  761. double z = double.Parse(splitup[2], CultureInfo.InvariantCulture);
  762. return new LSL_Vector(x, y, z);
  763. }
  764. /**
  765. * @brief Access C#-style formatted numeric conversions.
  766. */
  767. public string xmrFloat2String(double val, string fmt)
  768. {
  769. return val.ToString(fmt, CultureInfo.InvariantCulture);
  770. }
  771. public string xmrInteger2String(int val, string fmt)
  772. {
  773. return val.ToString(fmt, CultureInfo.InvariantCulture);
  774. }
  775. public string xmrRotation2String(LSL_Rotation val, string fmt)
  776. {
  777. return "<" + val.x.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  778. val.y.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  779. val.z.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  780. val.s.ToString(fmt, CultureInfo.InvariantCulture) + ">";
  781. }
  782. public string xmrVector2String(LSL_Vector val, string fmt)
  783. {
  784. return "<" + val.x.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  785. val.y.ToString(fmt, CultureInfo.InvariantCulture) + "," +
  786. val.z.ToString(fmt, CultureInfo.InvariantCulture) + ">";
  787. }
  788. /**
  789. * @brief Get a delegate for a script-defined function.
  790. * @param name = name of the function including arg types, eg,
  791. * "Verify(array,list,string)"
  792. * @param sig = script-defined type name
  793. * @param targ = function's 'this' pointer or null if static
  794. * @returns delegate for the script-defined function
  795. */
  796. public Delegate GetScriptMethodDelegate(string name, string sig, object targ)
  797. {
  798. DynamicMethod dm = m_ObjCode.dynamicMethods[name];
  799. TokenDeclSDTypeDelegate dt = (TokenDeclSDTypeDelegate)m_ObjCode.sdObjTypesName[sig];
  800. return dm.CreateDelegate(dt.GetSysType(), targ);
  801. }
  802. /**
  803. * @brief Try to cast the thrown object to the given script-defined type.
  804. * @param thrown = what object was thrown
  805. * @param inst = what script instance we are running in
  806. * @param sdtypeindex = script-defined type to try to cast it to
  807. * @returns null: thrown is not castable to sdtypename
  808. * else: an object casted to sdtypename
  809. */
  810. public static object XMRSDTypeCatchTryCastToSDType(object thrown, XMRInstAbstract inst, int sdtypeindex)
  811. {
  812. TokenDeclSDType sdType = inst.m_ObjCode.sdObjTypesIndx[sdtypeindex];
  813. // If it is a script-defined interface object, convert to the original XMRSDTypeClObj.
  814. if(thrown is Delegate[])
  815. {
  816. thrown = ((Delegate[])thrown)[0].Target;
  817. }
  818. // If it is a script-defined delegate object, make sure it is an instance of the expected type.
  819. if(thrown is Delegate)
  820. {
  821. Type ot = thrown.GetType();
  822. Type tt = sdType.GetSysType();
  823. return (ot == tt) ? thrown : null;
  824. }
  825. // If it is a script-defined class object, make sure it is an instance of the expected class.
  826. if(thrown is XMRSDTypeClObj)
  827. {
  828. // Step from the object's actual class rootward.
  829. // If we find the requested class along the way, the cast is valid.
  830. // If we run off the end of the root, the cast is not valid.
  831. for(TokenDeclSDTypeClass ac = ((XMRSDTypeClObj)thrown).sdtcClass; ac != null; ac = ac.extends)
  832. {
  833. if(ac == sdType)
  834. return thrown;
  835. }
  836. }
  837. // Don't know what it is, assume it is not what caller wants.
  838. return null;
  839. }
  840. /**
  841. * @brief Allocate and access fixed-dimension arrays.
  842. */
  843. public static object xmrFixedArrayAllocC(int len)
  844. {
  845. return new char[len];
  846. }
  847. public static object xmrFixedArrayAllocF(int len)
  848. {
  849. return new double[len];
  850. }
  851. public static object xmrFixedArrayAllocI(int len)
  852. {
  853. return new int[len];
  854. }
  855. public static object xmrFixedArrayAllocO(int len)
  856. {
  857. return new object[len];
  858. }
  859. public static char xmrFixedArrayGetC(object arr, int idx)
  860. {
  861. return ((char[])arr)[idx];
  862. }
  863. public static double xmrFixedArrayGetF(object arr, int idx)
  864. {
  865. return ((double[])arr)[idx];
  866. }
  867. public static int xmrFixedArrayGetI(object arr, int idx)
  868. {
  869. return ((int[])arr)[idx];
  870. }
  871. public static object xmrFixedArrayGetO(object arr, int idx)
  872. {
  873. return ((object[])arr)[idx];
  874. }
  875. public static void xmrFixedArraySetC(object arr, int idx, char val)
  876. {
  877. ((char[])arr)[idx] = val;
  878. }
  879. public static void xmrFixedArraySetF(object arr, int idx, double val)
  880. {
  881. ((double[])arr)[idx] = val;
  882. }
  883. public static void xmrFixedArraySetI(object arr, int idx, int val)
  884. {
  885. ((int[])arr)[idx] = val;
  886. }
  887. public static void xmrFixedArraySetO(object arr, int idx, object val)
  888. {
  889. ((object[])arr)[idx] = val;
  890. }
  891. /**
  892. * @brief Copy from one script-defined array to another.
  893. * @param srcobj = source script-defined array class object pointer
  894. * @param srcstart = offset in source array to start copying from
  895. * @param dstobj = destination script-defined array class object pointer
  896. * @param dststart = offset in destination arry to start copying to
  897. * @param count = number of elements to copy
  898. */
  899. public static void xmrArrayCopy(object srcobj, int srcstart, object dstobj, int dststart, int count)
  900. {
  901. // The script writer should only pass us script-defined class objects.
  902. // Throw exception otherwise.
  903. XMRSDTypeClObj srcsdt = (XMRSDTypeClObj)srcobj;
  904. XMRSDTypeClObj dstsdt = (XMRSDTypeClObj)dstobj;
  905. // Get the script-visible type name of the arrays, brackets and all.
  906. string srctypename = srcsdt.sdtcClass.longName.val;
  907. string dsttypename = dstsdt.sdtcClass.longName.val;
  908. // The part before the first '[' of each should match exactly,
  909. // meaning the basic data type (eg, float, List<string>) is the same.
  910. // And there must be a '[' in each meaning that it is a script-defined array type.
  911. int i = srctypename.IndexOf('[');
  912. int j = dsttypename.IndexOf('[');
  913. if((i < 0) || (j < 0))
  914. throw new InvalidCastException("non-array passed: " + srctypename + " and/or " + dsttypename);
  915. if((i != j) || !srctypename.StartsWith(dsttypename.Substring(0, j)))
  916. throw new ArrayTypeMismatchException(srctypename + " vs " + dsttypename);
  917. // The number of brackets must match exactly.
  918. // This permits copying from something like a float[,][] to something like a float[][].
  919. // But you cannot copy from a float[][] to a float[] or wisa wersa.
  920. // Counting either '[' or ']' would work equally well.
  921. int srclen = srctypename.Length;
  922. int dstlen = dsttypename.Length;
  923. int srcjags = 0;
  924. int dstjags = 0;
  925. while(++i < srclen)
  926. if(srctypename[i] == ']')
  927. srcjags++;
  928. while(++j < dstlen)
  929. if(dsttypename[j] == ']')
  930. dstjags++;
  931. if(dstjags != srcjags)
  932. throw new ArrayTypeMismatchException(srctypename + " vs " + dsttypename);
  933. // Perform the copy.
  934. Array srcarray = (Array)srcsdt.instVars.iarObjects[0];
  935. Array dstarray = (Array)dstsdt.instVars.iarObjects[0];
  936. Array.Copy(srcarray, srcstart, dstarray, dststart, count);
  937. }
  938. /**
  939. * @brief Copy from an array to a list.
  940. * @param srcar = the array to copy from
  941. * @param start = where to start in the array
  942. * @param count = number of elements
  943. * @returns the list
  944. */
  945. public static LSL_List xmrArray2List(object srcar, int start, int count)
  946. {
  947. // Get the script-visible type of the array.
  948. // We only do arrays.
  949. XMRSDTypeClObj array = (XMRSDTypeClObj)srcar;
  950. TokenDeclSDTypeClass sdtClass = array.sdtcClass;
  951. if(sdtClass.arrayOfRank == 0)
  952. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  953. // Validate objects they want to put in the list.
  954. // We can't allow anything funky that OpenSim runtime doesn't expect.
  955. Array srcarray = (Array)array.instVars.iarObjects[0];
  956. object[] output = new object[count];
  957. for(int i = 0; i < count; i++)
  958. {
  959. object src = srcarray.GetValue(i + start);
  960. if(src == null)
  961. throw new NullReferenceException("null element " + i);
  962. if(src is double)
  963. {
  964. output[i] = new LSL_Float((double)src);
  965. continue;
  966. }
  967. if(src is int)
  968. {
  969. output[i] = new LSL_Integer((int)src);
  970. continue;
  971. }
  972. if(src is LSL_Rotation)
  973. {
  974. output[i] = src;
  975. continue;
  976. }
  977. if(src is LSL_Vector)
  978. {
  979. output[i] = src;
  980. continue;
  981. }
  982. if(src is string)
  983. {
  984. output[i] = new LSL_String((string)src);
  985. continue;
  986. }
  987. throw new InvalidCastException("invalid element " + i + " type " + src.GetType().Name);
  988. }
  989. // Make a list out of that now immutable array.
  990. return new LSL_List(output);
  991. }
  992. /**
  993. * @brief Copy from a list to an array.
  994. * @param srclist = list to copy from
  995. * @param srcstart = where to start in the list
  996. * @param dstobj = array to copy to
  997. * @param dststart = where to start in the array
  998. * @param count = number of elements
  999. */
  1000. public static void xmrList2Array(LSL_List srclist, int srcstart, object dstobj, int dststart, int count)
  1001. {
  1002. // Get the script-visible type of the destination.
  1003. // We only do arrays.
  1004. XMRSDTypeClObj dstarray = (XMRSDTypeClObj)dstobj;
  1005. TokenDeclSDTypeClass sdtClass = dstarray.sdtcClass;
  1006. if(sdtClass.arrayOfType == null)
  1007. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  1008. // Copy from the immutable array to the mutable array.
  1009. // Strip off any LSL wrappers as the script code doesn't expect any.
  1010. object[] srcarr = srclist.Data;
  1011. Array dstarr = (Array)dstarray.instVars.iarObjects[0];
  1012. for(int i = 0; i < count; i++)
  1013. {
  1014. object obj = srcarr[i + srcstart];
  1015. if(obj is LSL_Float)
  1016. obj = ((LSL_Float)obj).value;
  1017. else if(obj is LSL_Integer)
  1018. obj = ((LSL_Integer)obj).value;
  1019. else if(obj is LSL_String)
  1020. obj = ((LSL_String)obj).m_string;
  1021. dstarr.SetValue(obj, i + dststart);
  1022. }
  1023. }
  1024. /**
  1025. * @brief Copy from an array of characters to a string.
  1026. * @param srcar = the array to copy from
  1027. * @param start = where to start in the array
  1028. * @param count = number of elements
  1029. * @returns the string
  1030. */
  1031. public static string xmrChars2String(object srcar, int start, int count)
  1032. {
  1033. // Make sure they gave us a script-defined array object.
  1034. XMRSDTypeClObj array = (XMRSDTypeClObj)srcar;
  1035. TokenDeclSDTypeClass sdtClass = array.sdtcClass;
  1036. if(sdtClass.arrayOfRank == 0)
  1037. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  1038. // We get a type cast error from mono if they didn't give us a character array.
  1039. // But if it is ok, create a string from the requested characters.
  1040. char[] srcarray = (char[])array.instVars.iarObjects[0];
  1041. return new string(srcarray, start, count);
  1042. }
  1043. /**
  1044. * @brief Copy from a string to a character array.
  1045. * @param srcstr = string to copy from
  1046. * @param srcstart = where to start in the string
  1047. * @param dstobj = array to copy to
  1048. * @param dststart = where to start in the array
  1049. * @param count = number of elements
  1050. */
  1051. public static void xmrString2Chars(string srcstr, int srcstart, object dstobj, int dststart, int count)
  1052. {
  1053. // Make sure they gave us a script-defined array object.
  1054. XMRSDTypeClObj dstarray = (XMRSDTypeClObj)dstobj;
  1055. TokenDeclSDTypeClass sdtClass = dstarray.sdtcClass;
  1056. if(sdtClass.arrayOfType == null)
  1057. throw new InvalidCastException("only do arrays not " + sdtClass.longName.val);
  1058. // We get a type cast error from mono if they didn't give us a character array.
  1059. // But if it is ok, copy from the string to the character array.
  1060. char[] dstarr = (char[])dstarray.instVars.iarObjects[0];
  1061. for(int i = 0; i < count; i++)
  1062. dstarr[i + dststart] = srcstr[i + srcstart];
  1063. }
  1064. /**
  1065. * @brief Exception-related runtime calls.
  1066. */
  1067. // Return exception message (no type information just the message)
  1068. public static string xmrExceptionMessage(Exception ex)
  1069. {
  1070. return ex.Message;
  1071. }
  1072. public static string yExceptionMessage(Exception ex)
  1073. {
  1074. return ex.Message;
  1075. }
  1076. // Return stack trace (no type or message, just stack trace lines: at ... \n)
  1077. public string xmrExceptionStackTrace(Exception ex)
  1078. {
  1079. return XMRExceptionStackString(ex);
  1080. }
  1081. public string yExceptionStackTrace(Exception ex)
  1082. {
  1083. return XMRExceptionStackString(ex);
  1084. }
  1085. // Return value thrown by a throw statement
  1086. public static object xmrExceptionThrownValue(Exception ex)
  1087. {
  1088. return ((ScriptThrownException)ex).thrown;
  1089. }
  1090. public static object yExceptionThrownValue(Exception ex)
  1091. {
  1092. return ((ScriptThrownException)ex).thrown;
  1093. }
  1094. // Return exception's short type name, eg, NullReferenceException, ScriptThrownException, etc.
  1095. public static string xmrExceptionTypeName(Exception ex)
  1096. {
  1097. return ex.GetType().Name;
  1098. }
  1099. public static string yExceptionTypeName(Exception ex)
  1100. {
  1101. return ex.GetType().Name;
  1102. }
  1103. // internal use only: converts any IL addresses in script-defined methods to source location equivalent
  1104. // Mono ex.StackTrace:
  1105. // at OpenSim.Region.ScriptEngine.YEngine.TypeCast.ObjectToInteger (System.Object x) [0x0005e] in /home/kunta/opensim-0.9/addon-modules/YEngine/Module/MMRScriptTypeCast.cs:750
  1106. // at (wrapper dynamic-method) System.Object:default state_entry (OpenSim.Region.ScriptEngine.YEngine.XMRInstAbstract) [0x00196]
  1107. // Microsoft ex.StackTrace:
  1108. // 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
  1109. // at default state_entry (XMRInstAbstract )
  1110. public string XMRExceptionStackString(Exception ex)
  1111. {
  1112. string stwhole = ex.StackTrace;
  1113. string[] stlines = stwhole.Split(new char[] { '\n' });
  1114. StringBuilder sb = new StringBuilder();
  1115. foreach(string st in stlines)
  1116. {
  1117. string stline = st.Trim();
  1118. if(stline == "")
  1119. continue;
  1120. // strip 'at' off the front of line
  1121. if(stline.StartsWith("at "))
  1122. {
  1123. stline = stline.Substring(3);
  1124. }
  1125. // strip '(wrapper ...' off front of line
  1126. if(stline.StartsWith("(wrapper dynamic-method) System.Object:"))
  1127. {
  1128. stline = stline.Substring(39);
  1129. }
  1130. // strip the (systemargtypes...) from our dynamic method names cuz it's messy
  1131. // 'default state_entry (XMRInstAbstract )'
  1132. // => 'default state_entry'
  1133. // 'CallSomethingThatThrows(string) (OpenSim.Region.ScriptEngine.YEngine.XMRInstance,string)'
  1134. // => 'CallSomethingThatThrows(string)'
  1135. int kwin = stline.IndexOf(" in ");
  1136. int br0x = stline.IndexOf(" [0x");
  1137. int pastCloseParen = stline.Length;
  1138. if((kwin >= 0) && (br0x >= 0))
  1139. pastCloseParen = Math.Min(kwin, br0x);
  1140. else if(kwin >= 0)
  1141. pastCloseParen = kwin;
  1142. else if(br0x >= 0)
  1143. pastCloseParen = br0x;
  1144. else
  1145. pastCloseParen = stline.Length;
  1146. int endFuncName = pastCloseParen;
  1147. while(endFuncName > 0)
  1148. {
  1149. if(stline[--endFuncName] == '(')
  1150. break;
  1151. }
  1152. while(endFuncName > 0)
  1153. {
  1154. if(stline[endFuncName - 1] != ' ')
  1155. break;
  1156. --endFuncName;
  1157. }
  1158. string funcName = stline.Substring(0, endFuncName);
  1159. KeyValuePair<int, ScriptSrcLoc>[] srcLocs;
  1160. if(m_ObjCode.scriptSrcLocss.TryGetValue(funcName, out srcLocs))
  1161. {
  1162. stline = stline.Substring(0, endFuncName) + stline.Substring(pastCloseParen);
  1163. kwin = stline.IndexOf(" in ");
  1164. br0x = stline.IndexOf(" [0x");
  1165. }
  1166. // keyword 'in' is just before filename:linenumber that goes to end of line
  1167. // trim up the corresponding filename (ie, remove useless path info)
  1168. if(kwin >= 0)
  1169. {
  1170. int begfn = kwin + 4;
  1171. int slash = begfn;
  1172. for(int i = begfn; i < stline.Length; i++)
  1173. {
  1174. char c = stline[i];
  1175. if((c == '/') || (c == '\\'))
  1176. slash = i + 1;
  1177. }
  1178. stline = stline.Substring(0, begfn) + stline.Substring(slash);
  1179. }
  1180. else if(srcLocs != null)
  1181. {
  1182. // no filename:linenumber info, try to convert IL offset
  1183. if(br0x >= 0)
  1184. {
  1185. try
  1186. {
  1187. int begiloffs = br0x + 4;
  1188. int endiloffs = stline.IndexOf("]", begiloffs);
  1189. int iloffset = int.Parse(stline.Substring(begiloffs, endiloffs - begiloffs),
  1190. System.Globalization.NumberStyles.HexNumber);
  1191. int srcLocIdx;
  1192. int srcLocLen = srcLocs.Length;
  1193. for(srcLocIdx = 0; ++srcLocIdx < srcLocLen;)
  1194. {
  1195. if(iloffset < srcLocs[srcLocIdx].Key)
  1196. break;
  1197. }
  1198. ScriptSrcLoc srcLoc = srcLocs[--srcLocIdx].Value;
  1199. stline = stline.Substring(0, br0x) + " <" +
  1200. srcLoc.file + '(' + srcLoc.line + ',' + srcLoc.posn + ")>";
  1201. }
  1202. catch
  1203. {
  1204. }
  1205. }
  1206. }
  1207. // put edited line in output string
  1208. if(sb.Length > 0)
  1209. sb.AppendLine();
  1210. sb.Append(" at ");
  1211. sb.Append(stline);
  1212. }
  1213. return sb.ToString();
  1214. }
  1215. /**
  1216. * @brief List fonts available.
  1217. */
  1218. public LSL_List xmrFontsAvailable()
  1219. {
  1220. System.Drawing.FontFamily[] families = System.Drawing.FontFamily.Families;
  1221. object[] output = new object[families.Length];
  1222. for(int i = 0; i < families.Length; i++)
  1223. output[i] = new LSL_String(families[i].Name);
  1224. return new LSL_List(output);
  1225. }
  1226. /************************\
  1227. * Used by decompiler *
  1228. \************************/
  1229. public bool xmrRotationToBool(LSL_Rotation x)
  1230. {
  1231. return TypeCast.RotationToBool(x);
  1232. }
  1233. public bool xmrStringToBool(string x)
  1234. {
  1235. return TypeCast.StringToBool(x);
  1236. }
  1237. public bool xmrVectorToBool(LSL_Vector x)
  1238. {
  1239. return TypeCast.VectorToBool(x);
  1240. }
  1241. public bool xmrKeyToBool(string x)
  1242. {
  1243. return TypeCast.KeyToBool(x);
  1244. }
  1245. public bool xmrListToBool(LSL_List x)
  1246. {
  1247. return TypeCast.ListToBool(x);
  1248. }
  1249. public int xmrStringCompare(string x, string y)
  1250. {
  1251. return string.Compare(x, y);
  1252. }
  1253. /**
  1254. * @brief types of data we serialize
  1255. */
  1256. private enum Ser: byte
  1257. {
  1258. NULL,
  1259. EVENTCODE,
  1260. LSLFLOAT,
  1261. LSLINT,
  1262. LSLKEY,
  1263. LSLLIST,
  1264. LSLROT,
  1265. LSLSTR,
  1266. LSLVEC,
  1267. SYSARRAY,
  1268. SYSDOUB,
  1269. SYSFLOAT,
  1270. SYSINT,
  1271. SYSSTR,
  1272. XMRARRAY,
  1273. DUPREF,
  1274. SYSBOOL,
  1275. XMRINST,
  1276. DELEGATE,
  1277. SDTCLOBJ,
  1278. SYSCHAR,
  1279. SYSERIAL,
  1280. THROWNEX
  1281. }
  1282. /**
  1283. * @brief Write state out to a stream.
  1284. * Do not change script state.
  1285. */
  1286. public void MigrateOut(BinaryWriter mow)
  1287. {
  1288. try
  1289. {
  1290. this.migrateOutWriter = mow;
  1291. this.migrateOutObjects = new Dictionary<object, int>();
  1292. this.migrateOutLists = new Dictionary<object[], ObjLslList>();
  1293. this.SendObjValue(this.ehArgs);
  1294. mow.Write(this.doGblInit);
  1295. mow.Write(this.stateCode);
  1296. mow.Write((int)this.eventCode);
  1297. this.glblVars.SendArrays(this.SendObjValue);
  1298. if(this.newStateCode >= 0)
  1299. {
  1300. mow.Write("**newStateCode**");
  1301. mow.Write(this.newStateCode);
  1302. }
  1303. for(XMRStackFrame thisSF = this.stackFrames; thisSF != null; thisSF = thisSF.nextSF)
  1304. {
  1305. mow.Write(thisSF.funcName);
  1306. mow.Write(thisSF.callNo);
  1307. this.SendObjValue(thisSF.objArray);
  1308. }
  1309. mow.Write("");
  1310. }
  1311. finally
  1312. {
  1313. this.migrateOutWriter = null;
  1314. this.migrateOutObjects = null;
  1315. this.migrateOutLists = null;
  1316. }
  1317. }
  1318. /**
  1319. * @brief Write an object to the output stream.
  1320. * @param graph = object to send
  1321. */
  1322. private BinaryWriter migrateOutWriter;
  1323. private Dictionary<object, int> migrateOutObjects;
  1324. private Dictionary<object[], ObjLslList> migrateOutLists;
  1325. public void SendObjValue(object graph)
  1326. {
  1327. BinaryWriter mow = this.migrateOutWriter;
  1328. // Value types (including nulls) are always output directly.
  1329. if(graph == null)
  1330. {
  1331. mow.Write((byte)Ser.NULL);
  1332. return;
  1333. }
  1334. if(graph is ScriptEventCode)
  1335. {
  1336. mow.Write((byte)Ser.EVENTCODE);
  1337. mow.Write((int)graph);
  1338. return;
  1339. }
  1340. if(graph is LSL_Float)
  1341. {
  1342. mow.Write((byte)Ser.LSLFLOAT);
  1343. mow.Write((double)((LSL_Float)graph).value);
  1344. return;
  1345. }
  1346. if(graph is LSL_Integer)
  1347. {
  1348. mow.Write((byte)Ser.LSLINT);
  1349. mow.Write((int)((LSL_Integer)graph).value);
  1350. return;
  1351. }
  1352. if(graph is LSL_Key)
  1353. {
  1354. mow.Write((byte)Ser.LSLKEY);
  1355. LSL_Key key = (LSL_Key)graph;
  1356. SendObjValue(key.m_string); // m_string can be null
  1357. return;
  1358. }
  1359. if(graph is LSL_Rotation)
  1360. {
  1361. mow.Write((byte)Ser.LSLROT);
  1362. mow.Write((double)((LSL_Rotation)graph).x);
  1363. mow.Write((double)((LSL_Rotation)graph).y);
  1364. mow.Write((double)((LSL_Rotation)graph).z);
  1365. mow.Write((double)((LSL_Rotation)graph).s);
  1366. return;
  1367. }
  1368. if(graph is LSL_String)
  1369. {
  1370. mow.Write((byte)Ser.LSLSTR);
  1371. LSL_String str = (LSL_String)graph;
  1372. SendObjValue(str.m_string); // m_string can be null
  1373. return;
  1374. }
  1375. if(graph is LSL_Vector)
  1376. {
  1377. mow.Write((byte)Ser.LSLVEC);
  1378. mow.Write((double)((LSL_Vector)graph).x);
  1379. mow.Write((double)((LSL_Vector)graph).y);
  1380. mow.Write((double)((LSL_Vector)graph).z);
  1381. return;
  1382. }
  1383. if(graph is bool)
  1384. {
  1385. mow.Write((byte)Ser.SYSBOOL);
  1386. mow.Write((bool)graph);
  1387. return;
  1388. }
  1389. if(graph is double)
  1390. {
  1391. mow.Write((byte)Ser.SYSDOUB);
  1392. mow.Write((double)graph);
  1393. return;
  1394. }
  1395. if(graph is float)
  1396. {
  1397. mow.Write((byte)Ser.SYSFLOAT);
  1398. mow.Write((float)graph);
  1399. return;
  1400. }
  1401. if(graph is int)
  1402. {
  1403. mow.Write((byte)Ser.SYSINT);
  1404. mow.Write((int)graph);
  1405. return;
  1406. }
  1407. if(graph is char)
  1408. {
  1409. mow.Write((byte)Ser.SYSCHAR);
  1410. mow.Write((char)graph);
  1411. return;
  1412. }
  1413. // Script instance pointer is always just that.
  1414. if(graph == this)
  1415. {
  1416. mow.Write((byte)Ser.XMRINST);
  1417. return;
  1418. }
  1419. // Convert lists to object type.
  1420. // This is compatible with old migration data and also
  1421. // two vars pointing to same list won't duplicate it.
  1422. if(graph is LSL_List)
  1423. {
  1424. object[] data = ((LSL_List)graph).Data;
  1425. ObjLslList oll;
  1426. if(!this.migrateOutLists.TryGetValue(data, out oll))
  1427. {
  1428. oll = new ObjLslList();
  1429. oll.objarray = data;
  1430. this.migrateOutLists[data] = oll;
  1431. }
  1432. graph = oll;
  1433. }
  1434. // If this same exact object was already serialized,
  1435. // just output an index telling the receiver to use
  1436. // that same old object, rather than creating a whole
  1437. // new object with the same values. Also this prevents
  1438. // self-referencing objects (like arrays) from causing
  1439. // an infinite loop.
  1440. int ident;
  1441. if(this.migrateOutObjects.TryGetValue(graph, out ident))
  1442. {
  1443. mow.Write((byte)Ser.DUPREF);
  1444. mow.Write(ident);
  1445. return;
  1446. }
  1447. // Object not seen before, save its address with an unique
  1448. // ident number that the receiver can easily regenerate.
  1449. ident = this.migrateOutObjects.Count;
  1450. this.migrateOutObjects.Add(graph, ident);
  1451. // Now output the object's value(s).
  1452. // If the object self-references, the object is alreay entered
  1453. // in the dictionary and so the self-reference will just emit
  1454. // a DUPREF tag instead of trying to output the whole object
  1455. // again.
  1456. if(graph is ObjLslList)
  1457. {
  1458. mow.Write((byte)Ser.LSLLIST);
  1459. ObjLslList oll = (ObjLslList)graph;
  1460. SendObjValue(oll.objarray);
  1461. }
  1462. else if(graph is XMR_Array)
  1463. {
  1464. mow.Write((byte)Ser.XMRARRAY);
  1465. ((XMR_Array)graph).SendArrayObj(this.SendObjValue);
  1466. }
  1467. else if(graph is Array)
  1468. {
  1469. Array array = (Array)graph;
  1470. mow.Write((byte)Ser.SYSARRAY);
  1471. mow.Write(SysType2String(array.GetType().GetElementType()));
  1472. mow.Write((int)array.Length);
  1473. for(int i = 0; i < array.Length; i++)
  1474. this.SendObjValue(array.GetValue(i));
  1475. }
  1476. else if(graph is string)
  1477. {
  1478. mow.Write((byte)Ser.SYSSTR);
  1479. mow.Write((string)graph);
  1480. }
  1481. else if(graph is Delegate)
  1482. {
  1483. Delegate del = (Delegate)graph;
  1484. mow.Write((byte)Ser.DELEGATE);
  1485. mow.Write(del.Method.Name);
  1486. Type delType = del.GetType();
  1487. foreach(KeyValuePair<string, TokenDeclSDType> kvp in m_ObjCode.sdObjTypesName)
  1488. {
  1489. TokenDeclSDType sdt = kvp.Value;
  1490. if(sdt is TokenDeclSDTypeDelegate)
  1491. {
  1492. TokenDeclSDTypeDelegate sdtd = (TokenDeclSDTypeDelegate)sdt;
  1493. if(sdtd.GetSysType() == delType)
  1494. {
  1495. mow.Write(kvp.Key);
  1496. goto found;
  1497. }
  1498. }
  1499. }
  1500. throw new Exception("cant find script-defined delegate for " + del.Method.Name + " type " + del.GetType());
  1501. found:
  1502. SendObjValue(del.Target);
  1503. }
  1504. else if(graph is XMRSDTypeClObj)
  1505. {
  1506. mow.Write((byte)Ser.SDTCLOBJ);
  1507. ((XMRSDTypeClObj)graph).Capture(this.SendObjValue);
  1508. }
  1509. else if(graph is ScriptThrownException)
  1510. {
  1511. MemoryStream memoryStream = new MemoryStream();
  1512. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1513. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1514. bformatter.Serialize(memoryStream, graph);
  1515. byte[] rawBytes = memoryStream.ToArray();
  1516. mow.Write((byte)Ser.THROWNEX);
  1517. mow.Write((int)rawBytes.Length);
  1518. mow.Write(rawBytes);
  1519. SendObjValue(((ScriptThrownException)graph).thrown);
  1520. }
  1521. else
  1522. {
  1523. MemoryStream memoryStream = new MemoryStream();
  1524. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1525. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1526. bformatter.Serialize(memoryStream, graph);
  1527. byte[] rawBytes = memoryStream.ToArray();
  1528. mow.Write((byte)Ser.SYSERIAL);
  1529. mow.Write((int)rawBytes.Length);
  1530. mow.Write(rawBytes);
  1531. }
  1532. }
  1533. /**
  1534. * @brief Use short strings for known type names.
  1535. */
  1536. private static string SysType2String(Type type)
  1537. {
  1538. if(type.IsArray && (type.GetArrayRank() == 1))
  1539. {
  1540. string str = KnownSysType2String(type.GetElementType());
  1541. if(str != null)
  1542. return str + "[]";
  1543. }
  1544. else
  1545. {
  1546. string str = KnownSysType2String(type);
  1547. if(str != null)
  1548. return str;
  1549. }
  1550. return type.ToString();
  1551. }
  1552. private static string KnownSysType2String(Type type)
  1553. {
  1554. if(type == typeof(bool))
  1555. return "bo";
  1556. if(type == typeof(char))
  1557. return "ch";
  1558. if(type == typeof(Delegate))
  1559. return "de";
  1560. if(type == typeof(double))
  1561. return "do";
  1562. if(type == typeof(float))
  1563. return "fl";
  1564. if(type == typeof(int))
  1565. return "in";
  1566. if(type == typeof(LSL_List))
  1567. return "li";
  1568. if(type == typeof(object))
  1569. return "ob";
  1570. if(type == typeof(LSL_Rotation))
  1571. return "ro";
  1572. if(type == typeof(XMRSDTypeClObj))
  1573. return "sc";
  1574. if(type == typeof(string))
  1575. return "st";
  1576. if(type == typeof(LSL_Vector))
  1577. return "ve";
  1578. if(type == typeof(XMR_Array))
  1579. return "xa";
  1580. return null;
  1581. }
  1582. private static Type String2SysType(string str)
  1583. {
  1584. if(str.EndsWith("[]"))
  1585. return String2SysType(str.Substring(0, str.Length - 2)).MakeArrayType();
  1586. if(str == "bo")
  1587. return typeof(bool);
  1588. if(str == "ch")
  1589. return typeof(char);
  1590. if(str == "de")
  1591. return typeof(Delegate);
  1592. if(str == "do")
  1593. return typeof(double);
  1594. if(str == "fl")
  1595. return typeof(float);
  1596. if(str == "in")
  1597. return typeof(int);
  1598. if(str == "li")
  1599. return typeof(LSL_List);
  1600. if(str == "ob")
  1601. return typeof(object);
  1602. if(str == "ro")
  1603. return typeof(LSL_Rotation);
  1604. if(str == "sc")
  1605. return typeof(XMRSDTypeClObj);
  1606. if(str == "st")
  1607. return typeof(string);
  1608. if(str == "ve")
  1609. return typeof(LSL_Vector);
  1610. if(str == "xa")
  1611. return typeof(XMR_Array);
  1612. return Type.GetType(str, true);
  1613. }
  1614. /**
  1615. * @brief Read state in from a stream.
  1616. */
  1617. public void MigrateIn(BinaryReader mir)
  1618. {
  1619. try
  1620. {
  1621. this.migrateInReader = mir;
  1622. this.migrateInObjects = new Dictionary<int, object>();
  1623. this.ehArgs = (object[])this.RecvObjValue();
  1624. this.doGblInit = mir.ReadBoolean();
  1625. this.stateCode = mir.ReadInt32();
  1626. this.eventCode = (ScriptEventCode)mir.ReadInt32();
  1627. this.newStateCode = -1;
  1628. this.glblVars.RecvArrays(this.RecvObjValue);
  1629. XMRStackFrame lastSF = null;
  1630. string funcName;
  1631. while((funcName = mir.ReadString()) != "")
  1632. {
  1633. if(funcName == "**newStateCode**")
  1634. {
  1635. this.newStateCode = mir.ReadInt32();
  1636. continue;
  1637. }
  1638. XMRStackFrame thisSF = new XMRStackFrame();
  1639. thisSF.funcName = funcName;
  1640. thisSF.callNo = mir.ReadInt32();
  1641. thisSF.objArray = (object[])this.RecvObjValue();
  1642. if(lastSF == null)
  1643. this.stackFrames = thisSF;
  1644. else
  1645. lastSF.nextSF = thisSF;
  1646. lastSF = thisSF;
  1647. }
  1648. }
  1649. finally
  1650. {
  1651. this.migrateInReader = null;
  1652. this.migrateInObjects = null;
  1653. }
  1654. }
  1655. /**
  1656. * @brief Read a single value from the stream.
  1657. * @returns value (boxed as needed)
  1658. */
  1659. private BinaryReader migrateInReader;
  1660. private Dictionary<int, object> migrateInObjects;
  1661. public object RecvObjValue()
  1662. {
  1663. BinaryReader mir = this.migrateInReader;
  1664. int ident = this.migrateInObjects.Count;
  1665. Ser code = (Ser)mir.ReadByte();
  1666. switch(code)
  1667. {
  1668. case Ser.NULL:
  1669. return null;
  1670. case Ser.EVENTCODE:
  1671. return (ScriptEventCode)mir.ReadInt32();
  1672. case Ser.LSLFLOAT:
  1673. return new LSL_Float(mir.ReadDouble());
  1674. case Ser.LSLINT:
  1675. return new LSL_Integer(mir.ReadInt32());
  1676. case Ser.LSLKEY:
  1677. return new LSL_Key((string)RecvObjValue());
  1678. case Ser.LSLLIST:
  1679. {
  1680. this.migrateInObjects.Add(ident, null); // placeholder
  1681. object[] data = (object[])RecvObjValue(); // read data, maybe using another index
  1682. LSL_List list = new LSL_List(data); // make LSL-level list
  1683. this.migrateInObjects[ident] = list; // fill in slot
  1684. return list;
  1685. }
  1686. case Ser.LSLROT:
  1687. {
  1688. double x = mir.ReadDouble();
  1689. double y = mir.ReadDouble();
  1690. double z = mir.ReadDouble();
  1691. double w = mir.ReadDouble();
  1692. return new LSL_Rotation(x, y, z, w);
  1693. }
  1694. case Ser.LSLSTR:
  1695. return new LSL_String((string)RecvObjValue());
  1696. case Ser.LSLVEC:
  1697. {
  1698. double x = mir.ReadDouble();
  1699. double y = mir.ReadDouble();
  1700. double z = mir.ReadDouble();
  1701. return new LSL_Vector(x, y, z);
  1702. }
  1703. case Ser.SYSARRAY:
  1704. {
  1705. Type eletype = String2SysType(mir.ReadString());
  1706. int length = mir.ReadInt32();
  1707. Array array = Array.CreateInstance(eletype, length);
  1708. this.migrateInObjects.Add(ident, array);
  1709. for(int i = 0; i < length; i++)
  1710. array.SetValue(RecvObjValue(), i);
  1711. return array;
  1712. }
  1713. case Ser.SYSBOOL:
  1714. return mir.ReadBoolean();
  1715. case Ser.SYSDOUB:
  1716. return mir.ReadDouble();
  1717. case Ser.SYSFLOAT:
  1718. return mir.ReadSingle();
  1719. case Ser.SYSINT:
  1720. return mir.ReadInt32();
  1721. case Ser.SYSCHAR:
  1722. return mir.ReadChar();
  1723. case Ser.SYSSTR:
  1724. string s = mir.ReadString();
  1725. this.migrateInObjects.Add(ident, s);
  1726. return s;
  1727. case Ser.XMRARRAY:
  1728. {
  1729. XMR_Array array = new XMR_Array(this);
  1730. this.migrateInObjects.Add(ident, array);
  1731. array.RecvArrayObj(this.RecvObjValue);
  1732. return array;
  1733. }
  1734. case Ser.DUPREF:
  1735. {
  1736. ident = mir.ReadInt32();
  1737. object obj = this.migrateInObjects[ident];
  1738. if(obj is ObjLslList)
  1739. obj = new LSL_List(((ObjLslList)obj).objarray);
  1740. return obj;
  1741. }
  1742. case Ser.XMRINST:
  1743. return this;
  1744. case Ser.DELEGATE:
  1745. this.migrateInObjects.Add(ident, null); // placeholder
  1746. string name = mir.ReadString(); // function name
  1747. string sig = mir.ReadString(); // delegate type
  1748. object targ = this.RecvObjValue(); // 'this' object
  1749. Delegate del = this.GetScriptMethodDelegate(name, sig, targ);
  1750. this.migrateInObjects[ident] = del; // actual value
  1751. return del;
  1752. case Ser.SDTCLOBJ:
  1753. XMRSDTypeClObj clobj = new XMRSDTypeClObj();
  1754. this.migrateInObjects.Add(ident, clobj);
  1755. clobj.Restore(this, this.RecvObjValue);
  1756. return clobj;
  1757. case Ser.SYSERIAL:
  1758. {
  1759. int rawLength = mir.ReadInt32();
  1760. byte[] rawBytes = mir.ReadBytes(rawLength);
  1761. MemoryStream memoryStream = new MemoryStream(rawBytes);
  1762. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1763. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1764. object graph = bformatter.Deserialize(memoryStream);
  1765. this.migrateInObjects.Add(ident, graph);
  1766. return graph;
  1767. }
  1768. case Ser.THROWNEX:
  1769. {
  1770. int rawLength = mir.ReadInt32();
  1771. byte[] rawBytes = mir.ReadBytes(rawLength);
  1772. MemoryStream memoryStream = new MemoryStream(rawBytes);
  1773. System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
  1774. new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  1775. object graph = bformatter.Deserialize(memoryStream);
  1776. this.migrateInObjects.Add(ident, graph);
  1777. ((ScriptThrownException)graph).thrown = RecvObjValue();
  1778. return graph;
  1779. }
  1780. default:
  1781. throw new Exception("bad stream code " + code.ToString());
  1782. }
  1783. }
  1784. // wrapper around list object arrays to make sure they are always object types for migration purposes
  1785. private class ObjLslList
  1786. {
  1787. public object[] objarray;
  1788. }
  1789. }
  1790. // Any xmr...() methods that call CheckRun() must be tagged with this attribute
  1791. // so the ScriptCodeGen will know the method is non-trivial.
  1792. public class xmrMethodCallsCheckRunAttribute: Attribute
  1793. {
  1794. }
  1795. // Any xmr...() methods in xmrengtest that call Stub<somethingorother>() must be
  1796. // tagged with this attribute so the -builtins option will tell the user that
  1797. // they are a stub function.
  1798. public class xmrMethodIsNoisyAttribute: Attribute
  1799. {
  1800. }
  1801. // Any script callable methods that really return a key not a string should be
  1802. // tagged with this attribute so the compiler will know they return type key and
  1803. // not type string.
  1804. public class xmrMethodReturnsKeyAttribute: Attribute
  1805. {
  1806. }
  1807. [SerializableAttribute]
  1808. public class OutOfHeapException: Exception
  1809. {
  1810. public OutOfHeapException(int oldtotal, int newtotal, int limit)
  1811. : base("oldtotal=" + oldtotal + ", newtotal=" + newtotal + ", limit=" + limit)
  1812. {
  1813. }
  1814. }
  1815. [SerializableAttribute]
  1816. public class OutOfStackException: Exception
  1817. {
  1818. }
  1819. }