aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptCompile.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/XMREngine/MMRScriptCompile.cs216
1 files changed, 216 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptCompile.cs b/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptCompile.cs
new file mode 100644
index 0000000..017d2c5
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptCompile.cs
@@ -0,0 +1,216 @@
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
28/**
29 * @brief Compile a script to produce a ScriptObjCode object
30 */
31
32using System;
33using System.IO;
34using System.IO.Compression;
35using System.Reflection;
36using System.Security.Cryptography;
37using System.Text;
38
39namespace OpenSim.Region.ScriptEngine.XMREngine
40{
41 public partial class XMRInstance
42 {
43 /**
44 * @brief Compile a script to produce a ScriptObjCode object
45 * @returns object code pointer or null if compile error
46 * also can throw compile error exception
47 */
48 public ScriptObjCode Compile ()
49 {
50 bool oldObjFile = false;
51 Stream objFileStream = null;
52 StreamWriter asmFileWriter = null;
53 string envar = null;
54 string sourceHash = null;
55 TextWriter saveSource = null;
56
57 string asmFileName = GetScriptFileName (m_ScriptObjCodeKey + ".xmrasm");
58 string lslFileName = GetScriptFileName (m_ScriptObjCodeKey + ".lsl");
59 string objFileName = GetScriptFileName (m_ScriptObjCodeKey + ".xmrobj");
60 string tmpFileName = GetScriptFileName (m_ScriptObjCodeKey + ".xmrtmp");
61
62 /*
63 * If we already have an object file, don't bother compiling.
64 */
65 if (!m_ForceRecomp && File.Exists (objFileName)) {
66 objFileStream = File.OpenRead (objFileName);
67 oldObjFile = true;
68 } else {
69
70 /*
71 * If source file empty, try to read from asset server.
72 */
73 if (EmptySource (m_SourceCode)) {
74 m_SourceCode = FetchSource (m_CameFrom);
75 }
76
77 /*
78 * Maybe write script source to a file for debugging.
79 */
80 envar = Environment.GetEnvironmentVariable ("MMRScriptCompileSaveSource");
81 if ((envar != null) && ((envar[0] & 1) != 0)) {
82 m_log.Debug ("[XMREngine]: MMRScriptCompileSaveSource: saving to " + lslFileName);
83 saveSource = File.CreateText (lslFileName);
84 }
85
86 /*
87 * Parse source string into tokens.
88 */
89 TokenBegin tokenBegin;
90 try {
91 tokenBegin = TokenBegin.Construct(m_CameFrom, saveSource, ErrorHandler, m_SourceCode, out sourceHash);
92 } finally {
93 if (saveSource != null) saveSource.Close ();
94 }
95 if (tokenBegin == null) {
96 m_log.Debug ("[XMREngine]: parsing errors on " + m_ScriptObjCodeKey);
97 return null;
98 }
99
100 /*
101 * Create object file one way or another.
102 */
103 try {
104 objFileStream = File.Create (tmpFileName);
105
106 /*
107 * Create abstract syntax tree from raw tokens.
108 */
109 TokenScript tokenScript = ScriptReduce.Reduce(tokenBegin);
110 if (tokenScript == null) {
111 m_log.Warn ("[XMREngine]: reduction errors on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
112 PrintCompilerErrors ();
113 return null;
114 }
115
116 /*
117 * Compile abstract syntax tree to write object file.
118 */
119 BinaryWriter objFileWriter = new BinaryWriter (objFileStream);
120 bool ok = ScriptCodeGen.CodeGen(tokenScript, objFileWriter, sourceHash);
121 if (!ok) {
122 m_log.Warn ("[XMREngine]: compile error on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
123 PrintCompilerErrors ();
124 objFileStream.Close ();
125 return null;
126 }
127 objFileStream.Close ();
128
129 /*
130 * File has been completely written.
131 * If there is an old one laying around, delete it now.
132 * Then re-open the new file for reading from the beginning.
133 */
134 if (File.Exists (objFileName)) {
135 File.Replace (tmpFileName, objFileName, null);
136 } else {
137 File.Move (tmpFileName, objFileName);
138 }
139 objFileStream = File.OpenRead (objFileName);
140 } finally {
141
142 /*
143 * In case something went wrong writing temp file, delete it.
144 */
145 try {
146 File.Delete (tmpFileName);
147 } catch {
148 }
149 }
150
151 /*
152 * Since we just wrote the .xmrobj file, maybe save disassembly.
153 */
154 envar = Environment.GetEnvironmentVariable ("MMRScriptCompileSaveILGen");
155 if ((envar != null) && ((envar[0] & 1) != 0)) {
156 m_log.Debug ("[XMREngine]: MMRScriptCompileSaveILGen: saving to " + asmFileName);
157 asmFileWriter = File.CreateText (asmFileName);
158 }
159 }
160
161 /*
162 * Read object file to create ScriptObjCode object.
163 * Maybe also write disassembly to a file for debugging.
164 */
165 BinaryReader objFileReader = new BinaryReader (objFileStream);
166 ScriptObjCode scriptObjCode = null;
167 try {
168 scriptObjCode = new ScriptObjCode (objFileReader, asmFileWriter, null);
169 if (scriptObjCode != null) {
170 scriptObjCode.fileDateUtc = File.GetLastWriteTimeUtc (objFileName);
171 }
172 } finally {
173 objFileReader.Close ();
174 if (asmFileWriter != null) {
175 asmFileWriter.Flush ();
176 asmFileWriter.Close ();
177 }
178 }
179
180 /*
181 * Maybe an old object file has reached its expiration date.
182 */
183 if (oldObjFile && (scriptObjCode != null) && scriptObjCode.IsExpired ()) {
184 m_log.Debug ("[XMREngine]: expiration reached on " + m_ScriptObjCodeKey + ", reloading");
185 m_ForceRecomp = true;
186 scriptObjCode = Compile ();
187 }
188
189 return scriptObjCode;
190 }
191
192 private void PrintCompilerErrors ()
193 {
194 m_log.Info ("[XMREngine]: - " + m_Part.GetWorldPosition () + " " + m_DescName);
195 foreach (string error in m_CompilerErrors) {
196 m_log.Info ("[XMREngine]: - " + error);
197 }
198 }
199
200 /**
201 * @brief Check for empty source, allowing for a first line of //... script engine selector.
202 */
203 public static bool EmptySource (string source)
204 {
205 int len = source.Length;
206 bool skipeol = false;
207 for (int i = 0; i < len; i ++) {
208 char c = source[i];
209 skipeol &= c != '\n';
210 skipeol |= (c == '/') && (i + 1 < len) && (source[i+1] == '/');
211 if ((c > ' ') && !skipeol) return false;
212 }
213 return true;
214 }
215 }
216}