forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChakraCorePrecompiledScript.cs
More file actions
162 lines (138 loc) · 4.17 KB
/
ChakraCorePrecompiledScript.cs
File metadata and controls
162 lines (138 loc) · 4.17 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
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
using System.Text;
using JavaScriptEngineSwitcher.Core;
using JavaScriptEngineSwitcher.ChakraCore.JsRt;
using OriginalException = JavaScriptEngineSwitcher.ChakraCore.JsRt.JsException;
namespace JavaScriptEngineSwitcher.ChakraCore
{
/// <summary>
/// Represents a pre-compiled script that can be executed by different instances of the ChakraCore JS engine
/// </summary>
internal sealed class ChakraCorePrecompiledScript : IPrecompiledScript
{
/// <summary>
/// Source code of the script
/// </summary>
private readonly string _code;
/// <summary>
/// Attribute mask for parsing the script
/// </summary>
private readonly JsParseScriptAttributes _parseAttributes;
/// <summary>
/// Cached data for accelerated recompilation
/// </summary>
private readonly byte[] _cachedBytes;
/// <summary>
/// Document name
/// </summary>
private readonly string _documentName;
/// <summary>
/// Callback to load the source code of the serialized script
/// </summary>
private readonly JsSerializedLoadScriptCallback _loadScriptSourceCodeCallback;
/// <summary>
/// Source code of the script as an array of bytes
/// </summary>
private byte[] _codeBytes;
/// <summary>
/// Synchronizer of the script source code loading
/// </summary>
private readonly object _scriptLoadingSynchronizer = new object();
/// <summary>
/// Gets a source code of the script
/// </summary>
public string Code
{
get { return _code; }
}
/// <summary>
/// Gets a attribute mask for parsing the script
/// </summary>
public JsParseScriptAttributes ParseAttributes
{
get { return _parseAttributes; }
}
/// <summary>
/// Gets a cached data for accelerated recompilation
/// </summary>
public byte[] CachedBytes
{
get { return _cachedBytes; }
}
/// <summary>
/// Gets a document name
/// </summary>
public string DocumentName
{
get { return _documentName; }
}
/// <summary>
/// Gets a callback to load the source code of the serialized script
/// </summary>
public JsSerializedLoadScriptCallback LoadScriptSourceCodeCallback
{
get { return _loadScriptSourceCodeCallback; }
}
/// <summary>
/// Constructs an instance of pre-compiled script
/// </summary>
/// <param name="code">The source code of the script</param>
/// <param name="parseAttributes">Attribute mask for parsing the script</param>
/// <param name="cachedBytes">Cached data for accelerated recompilation</param>
/// <param name="documentName">Document name</param>
public ChakraCorePrecompiledScript(string code, JsParseScriptAttributes parseAttributes, byte[] cachedBytes,
string documentName)
{
_code = code;
_parseAttributes = parseAttributes;
_cachedBytes = cachedBytes;
_documentName = documentName;
_loadScriptSourceCodeCallback = LoadScriptSourceCode;
}
/// <summary>
/// Loads a source code of the serialized script
/// </summary>
/// <param name="sourceContext">A cookie identifying the script that can be used
/// by debuggable script contexts</param>
/// <param name="value">The script returned</param>
/// <param name="parseAttributes">Attribute mask for parsing the script</param>
/// <returns>true if the operation succeeded, false otherwise</returns>
private bool LoadScriptSourceCode(JsSourceContext sourceContext, out JsValue value,
out JsParseScriptAttributes parseAttributes)
{
if (_codeBytes == null)
{
lock (_scriptLoadingSynchronizer)
{
if (_codeBytes == null)
{
Encoding encoding = _parseAttributes.HasFlag(JsParseScriptAttributes.ArrayBufferIsUtf16Encoded) ?
Encoding.Unicode : Encoding.UTF8;
_codeBytes = encoding.GetBytes(_code);
}
}
}
bool result;
parseAttributes = _parseAttributes;
try
{
value = JsValue.CreateExternalArrayBuffer(_codeBytes);
result = true;
}
catch (OriginalException)
{
value = JsValue.Invalid;
result = false;
}
return result;
}
#region IPrecompiledScript implementation
/// <summary>
/// Gets a name of JS engine for which the pre-compiled script was created
/// </summary>
public string EngineName
{
get { return ChakraCoreJsEngine.EngineName; }
}
#endregion
}
}