forked from paulbartrum/jurassic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptCompiler.cs
188 lines (149 loc) · 6.45 KB
/
ScriptCompiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jurassic
{
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using Compiler;
using Library;
public class ScriptCompiler
{
private List<string> codesInputs = new List<string>();
public void IncludeInput(string code)
{
codesInputs.Add(code);
}
public void Save(string dllPath)
{
var fullpath = Path.GetFullPath(dllPath);
var assemblyName = Path.GetFileNameWithoutExtension(fullpath);
var assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName(assemblyName),
AssemblyBuilderAccess.RunAndSave,
Path.GetDirectoryName(fullpath));
var module = assemblyBuilder.DefineDynamicModule("Module", Path.GetFileName(fullpath), true);
var engine = new ScriptEngine
{
EnableDebugging = true,
ReflectionEmitInfo = new ScriptEngine.ReflectionEmitModuleInfo() { AssemblyBuilder = assemblyBuilder, ModuleBuilder = module }
};
foreach (var code in codesInputs)
{
engine.Execute(code);
}
var generatedTypes = module.GetTypes();
var userType = module.DefineType(assemblyName, TypeAttributes.Public);
var restoreMethod = userType.DefineMethod("RestoreScriptEngine", MethodAttributes.Public | MethodAttributes.Static, typeof(ScriptEngine), new Type[] { });
var restoreMethodBody = restoreMethod.GetILGenerator();
var generator = new ReflectionEmitILGenerator(restoreMethodBody);
// Local : engine = new ScriptEngine()
var loadedEngine = restoreMethodBody.DeclareLocal(typeof(ScriptEngine));
restoreMethodBody.EmitCall(OpCodes.Call, typeof(Assembly).GetMethod(nameof(Assembly.GetExecutingAssembly)), new Type[] { });
restoreMethodBody.EmitCall(OpCodes.Call, typeof(ScriptCompiler).GetMethod("Load"), new Type[] { });
restoreMethodBody.Emit(OpCodes.Ret);
userType.CreateType();
assemblyBuilder.Save(Path.GetFileName(dllPath));
}
public static void AddToFunctionCache(long id, FunctionDelegate functionDelegate, GeneratedMethod[] dependencies)
{
GeneratedMethod.AddToMethodCache(id, new GeneratedMethod(functionDelegate, dependencies));
}
public static ScriptEngine Load(Assembly assembly)
{
var engine = new ScriptEngine();
var globalScope = /*engine.CreateGlobalScope();*/
Jurassic.Compiler.ObjectScope.CreateRuntimeScope(null, engine.Global, false, false);
var sw = new Stopwatch();
sw.Start();
var methods = assembly
.GetTypes()
.Where(t => t.GetMethods().Any(IsJavaScriptFunction))
.Select(t =>
{
return
new
{
Id = (long)t.GetMethod("GetFunctionId").Invoke(null, new object[] { }),
Function = t.GetMethods().SingleOrDefault(IsJavaScriptFunction),
Dependencies = (long[])t.GetMethod("GetDependencyIds").Invoke(null, new object[] { })
};
}
).ToList();
GeneratedMethod.generatedMethodID = 0;
GeneratedMethod.generatedMethodCache = new Dictionary<long, WeakReference>();
int i = 0;
while (methods.Count != 0)
{
bool foundDependency = true;
var func = methods[i];
var functionDelegate = (FunctionDelegate)Delegate.CreateDelegate(typeof(FunctionDelegate), func.Function);
List<GeneratedMethod> dependencies = new List<GeneratedMethod>();
foreach (var m in func.Dependencies)
{
WeakReference refer;
foundDependency = GeneratedMethod.generatedMethodCache.TryGetValue(m, out refer);
if (!foundDependency)
{
break;
}
dependencies.Add((GeneratedMethod)refer.Target);
}
if (foundDependency)
{
AddToFunctionCache(func.Id, functionDelegate, dependencies.ToArray());
GeneratedMethod.generatedMethodID++;
methods.Remove(func);
i = 0;
}
else
{
i = i < methods.Count - 1 ? i + 1 : 0;
}
}
Console.WriteLine("Functions" + sw.ElapsedMilliseconds / 1000.0);
var globals = assembly
.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => m.Name == "global_")
.Select(m => (Func<ScriptEngine, Scope, object, object>)Delegate.CreateDelegate(typeof(Func<ScriptEngine, Scope, object, object>), m))
.ToList();
sw.Reset();
sw.Start();
object obj = engine.Global;
foreach (var global in globals)
{
global(engine, globalScope, obj);
}
Console.WriteLine("globals: " + sw.ElapsedMilliseconds / 1000.0);
return engine;
}
public static bool IsJavaScriptFunction(MethodInfo info)
{
if (info == null)
{
return false;
}
if (info.ReturnParameter?.ParameterType != typeof(object))
{
return false;
}
var parameters = info.GetParameters();
if (parameters.Length != 5)
{
return false;
}
var result = true;
result |= parameters[0].ParameterType == typeof(ScriptEngine);
result |= parameters[1].ParameterType == typeof(Scope);
result |= parameters[1].ParameterType == typeof(object);
result |= parameters[1].ParameterType == typeof(FunctionInstance);
result |= parameters[1].ParameterType == typeof(object[]);
return result;
}
}
}