forked from yuzd/ClearScript.Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCompiler.cs
More file actions
105 lines (91 loc) · 3.67 KB
/
ScriptCompiler.cs
File metadata and controls
105 lines (91 loc) · 3.67 KB
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
using JavaScript.Manager.Caching;
using JavaScript.Manager.Loaders;
using Microsoft.ClearScript.V8;
using System;
using System.Text;
namespace JavaScript.Manager
{
internal class ScriptCompiler
{
private readonly LruCache<string, CachedV8Script> _scriptCache;
private readonly IManagerSettings _settings;
private readonly V8Runtime _v8Runtime;
public ScriptCompiler(V8Runtime v8Runtime, IManagerSettings settings)
{
_settings = settings;
_v8Runtime = v8Runtime;
_scriptCache = new LruCache<string, CachedV8Script>(LurchTableOrder.Access, settings.ScriptCacheMaxCount);
}
public V8Script Compile(string scriptId, string code, bool addToCache = true, int? cacheExpirationSeconds = null)
{
CachedV8Script cachedScript;
if (TryGetCached(scriptId, out cachedScript))
{
return cachedScript.Script;
}
V8Script compiledScript = _v8Runtime.Compile(scriptId, code);
if (addToCache)
{
if (!cacheExpirationSeconds.HasValue)
{
cacheExpirationSeconds = _settings.ScriptCacheExpirationSeconds;
}
if (cacheExpirationSeconds > 0)
{
var cacheEntry = new CachedV8Script(compiledScript, cacheExpirationSeconds.Value);
_scriptCache.AddOrUpdate(scriptId, cacheEntry, (key, original) => cacheEntry);
}
}
return compiledScript;
}
public V8Script Compile(IncludeScript script, bool addToCache = true, int? cacheExpirationSeconds = null)
{
try
{
CachedV8Script cachedScript;
script.EnsureScriptId();
if (TryGetCached(script.ScriptId, out cachedScript))
{
return cachedScript.Script;
}
if (string.IsNullOrEmpty(script.Code) && !string.IsNullOrEmpty(script.Uri))
{
script.LoadScript();
}
if (!string.IsNullOrEmpty(script.Code))
{
if (script.RequiredPackage != null && !string.IsNullOrEmpty(script.RequiredPackage.PackageId))
{
script.Code = script.Code.Replace("this.exports", script.RequiredPackage.PackageId + ".exports");
}
if (!string.IsNullOrEmpty(script.PrependCode) || !string.IsNullOrEmpty(script.AppendCode))
{
var builder = new StringBuilder();
builder.Append(script.PrependCode).Append(script.Code).Append(script.AppendCode);
return Compile(script.ScriptId, builder.ToString(), addToCache, cacheExpirationSeconds);
}
return Compile(script.ScriptId, script.Code, addToCache, cacheExpirationSeconds);
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
public bool TryGetCached(string scriptId, out CachedV8Script cachedScript)
{
if (_scriptCache.TryGetValue(scriptId, out cachedScript))
{
if (cachedScript.ExpiresOn > DateTime.UtcNow)
{
cachedScript.CacheHits++;
return true;
}
_scriptCache.TryRemove(scriptId, out cachedScript);
}
cachedScript = null;
return false;
}
}
}