Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
92 lines (80 loc) · 3.17 KB

File metadata and controls

92 lines (80 loc) · 3.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using System;
using System.Text;
using ClearScript.Manager.Caching;
using ClearScript.Manager.Loaders;
using Microsoft.ClearScript.V8;
namespace ClearScript.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)
{
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 (!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;
}
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;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.