public void BinDumpBinaryStreams_TestStringWrites() { string[] values = new string[] { "hello", "you", "fool", "hello", "I", "love", "you" }; using (MemoryStream ms_orig = new MemoryStream()) { UndisposableStream ms = new UndisposableStream(ms_orig); using (BinDumpBinaryWriter bdbw = new BinDumpBinaryWriter(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { bdbw.Write(values[i]); } } ms.Seek(0, SeekOrigin.Begin); using (BinDumpBinaryReader bdbr = new BinDumpBinaryReader(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { string v = bdbr.ReadString(); Assert.AreEqual(values[i], v, "i = " + i.ToString()); } } } }
public void BinDumpBinaryStreams_TestUIntWrites() { uint[] values = new uint[] { 0, 1, 0x7F, 10, 0x7E, 32767, 32768, uint.MinValue, uint.MaxValue }; using (MemoryStream ms_orig = new MemoryStream()) { UndisposableStream ms = new UndisposableStream(ms_orig); using (BinDumpBinaryWriter bdbw = new BinDumpBinaryWriter(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { bdbw.Write(values[i]); } } ms.Seek(0, SeekOrigin.Begin); using (BinDumpBinaryReader bdbr = new BinDumpBinaryReader(ms, Encoding.UTF8)) { for (int i = 0; i < values.Length; i++) { uint v = bdbr.ReadUInt32(); Assert.AreEqual(values[i], v, "i = " + i.ToString()); } } } }
/// <summary> /// Dumps on the specified stream. /// </summary> /// <param name="function">The function.</param> /// <param name="stream">The stream.</param> /// <exception cref="System.ArgumentException"> /// function arg is not a function! /// or /// stream is readonly! /// or /// function arg has upvalues other than _ENV /// </exception> public void Dump(DynValue function, Stream stream) { this.CheckScriptOwnership(function); if (function.Type != DataType.Function) throw new ArgumentException("function arg is not a function!"); if (!stream.CanWrite) throw new ArgumentException("stream is readonly!"); Closure.UpvaluesType upvaluesType = function.Function.GetUpvaluesType(); if (upvaluesType == Closure.UpvaluesType.Closure) throw new ArgumentException("function arg has upvalues other than _ENV"); UndisposableStream outStream = new UndisposableStream(stream); m_MainProcessor.Dump(outStream, function.Function.EntryPointByteCodeLocation, upvaluesType == Closure.UpvaluesType.Environment); }
/// <summary> /// Loads a Lua/MoonSharp script from a System.IO.Stream. NOTE: This will *NOT* close the stream! /// </summary> /// <param name="stream">The stream containing code.</param> /// <param name="globalTable">The global table to bind to this chunk.</param> /// <param name="codeFriendlyName">Name of the code - used to report errors, etc.</param> /// <returns> /// A DynValue containing a function which will execute the loaded code. /// </returns> public DynValue LoadStream(Stream stream, Table globalTable = null, string codeFriendlyName = null) { this.CheckScriptOwnership(globalTable); Stream codeStream = new UndisposableStream(stream); if (!Processor.IsDumpStream(codeStream)) { using (StreamReader sr = new StreamReader(codeStream)) { string scriptCode = sr.ReadToEnd(); return LoadString(scriptCode, globalTable, codeFriendlyName); } } else { string chunkName = string.Format("{0}", codeFriendlyName ?? "dump_" + m_Sources.Count.ToString()); SourceCode source = new SourceCode(codeFriendlyName ?? chunkName, string.Format("-- This script was decoded from a binary dump - dump_{0}", m_Sources.Count), m_Sources.Count, this); m_Sources.Add(source); bool hasUpvalues; int address = m_MainProcessor.Undump(codeStream, m_Sources.Count - 1, globalTable ?? m_GlobalTable, out hasUpvalues); SignalSourceCodeChange(source); SignalByteCodeChange(); if (hasUpvalues) return MakeClosure(address, globalTable ?? m_GlobalTable); else return MakeClosure(address); } }