@@ -51,6 +51,16 @@ public sealed class VroomJsEngine : JsEngineBase
51
51
/// </summary>
52
52
private readonly Dictionary < string , object > _hostItems = new Dictionary < string , object > ( ) ;
53
53
54
+ /// <summary>
55
+ /// Synchronizer of JS engine initialization
56
+ /// </summary>
57
+ private static readonly object _initializationSynchronizer = new object ( ) ;
58
+
59
+ /// <summary>
60
+ /// Flag indicating whether the JS engine is initialized
61
+ /// </summary>
62
+ private static bool _initialized ;
63
+
54
64
/// <summary>
55
65
/// Unique document name manager
56
66
/// </summary>
@@ -82,17 +92,6 @@ public override bool SupportsGarbageCollection
82
92
}
83
93
84
94
85
- /// <summary>
86
- /// Static constructor
87
- /// </summary>
88
- static VroomJsEngine ( )
89
- {
90
- if ( Utils . IsWindows ( ) )
91
- {
92
- OriginalAssemblyLoader . EnsureLoaded ( ) ;
93
- }
94
- }
95
-
96
95
/// <summary>
97
96
/// Constructs an instance of adapter for the Vroom JS engine
98
97
/// (cross-platform bridge to the V8 JS engine)
@@ -108,23 +107,66 @@ public VroomJsEngine()
108
107
/// <param name="settings">Settings of the Vroom JS engine</param>
109
108
public VroomJsEngine ( VroomSettings settings )
110
109
{
110
+ Initialize ( ) ;
111
+
111
112
VroomSettings vroomSettings = settings ?? new VroomSettings ( ) ;
112
113
113
114
try
114
115
{
115
- _jsEngine = new OriginalJsEngine ( vroomSettings . MaxYoungSpaceSize ,
116
- vroomSettings . MaxOldSpaceSize ) ;
116
+ _jsEngine = new OriginalJsEngine ( vroomSettings . MaxYoungSpaceSize , vroomSettings . MaxOldSpaceSize ) ;
117
117
_jsContext = _jsEngine . CreateContext ( ) ;
118
118
}
119
119
catch ( Exception e )
120
120
{
121
121
throw new JsEngineLoadException (
122
- string . Format ( CoreStrings . Runtime_JsEngineNotLoaded ,
123
- EngineName , e . Message ) , EngineName , EngineVersion , e ) ;
122
+ string . Format ( CoreStrings . Runtime_JsEngineNotLoaded , EngineName , e . Message ) ,
123
+ EngineName , EngineVersion , e ) ;
124
+ }
125
+ finally
126
+ {
127
+ if ( _jsContext == null )
128
+ {
129
+ Dispose ( ) ;
130
+ }
124
131
}
125
132
}
126
133
127
134
135
+ /// <summary>
136
+ /// Initializes a JS engine
137
+ /// </summary>
138
+ private static void Initialize ( )
139
+ {
140
+ if ( _initialized )
141
+ {
142
+ return ;
143
+ }
144
+
145
+ lock ( _initializationSynchronizer )
146
+ {
147
+ if ( _initialized )
148
+ {
149
+ return ;
150
+ }
151
+
152
+ if ( Utils . IsWindows ( ) )
153
+ {
154
+ try
155
+ {
156
+ OriginalAssemblyLoader . EnsureLoaded ( ) ;
157
+ }
158
+ catch ( Exception e )
159
+ {
160
+ throw new JsEngineLoadException (
161
+ string . Format ( CoreStrings . Runtime_JsEngineNotLoaded , EngineName , e . Message ) ,
162
+ EngineName , EngineVersion , e ) ;
163
+ }
164
+ }
165
+
166
+ _initialized = true ;
167
+ }
168
+ }
169
+
128
170
/// <summary>
129
171
/// Makes a mapping from the host type to a Vroom type
130
172
/// </summary>
@@ -235,17 +277,14 @@ protected override void InnerExecute(string code, string documentName)
235
277
236
278
protected override object InnerCallFunction ( string functionName , params object [ ] args )
237
279
{
238
- string serializedArguments = string . Empty ;
280
+ string functionCallExpression ;
239
281
int argumentCount = args . Length ;
240
282
241
- if ( argumentCount == 1 )
283
+ if ( argumentCount > 0 )
242
284
{
243
- object value = args [ 0 ] ;
244
- serializedArguments = SimplisticJsSerializer . Serialize ( value ) ;
245
- }
246
- else if ( argumentCount > 1 )
247
- {
248
- var serializedArgumentsBuilder = new StringBuilder ( ) ;
285
+ var functionCallBuilder = new StringBuilder ( ) ;
286
+ functionCallBuilder . Append ( functionName ) ;
287
+ functionCallBuilder . Append ( "(" ) ;
249
288
250
289
for ( int argumentIndex = 0 ; argumentIndex < argumentCount ; argumentIndex ++ )
251
290
{
@@ -254,15 +293,22 @@ protected override object InnerCallFunction(string functionName, params object[]
254
293
255
294
if ( argumentIndex > 0 )
256
295
{
257
- serializedArgumentsBuilder . Append ( ", " ) ;
296
+ functionCallBuilder . Append ( ", " ) ;
258
297
}
259
- serializedArgumentsBuilder . Append ( serializedValue ) ;
298
+ functionCallBuilder . Append ( serializedValue ) ;
260
299
}
261
300
262
- serializedArguments = serializedArgumentsBuilder . ToString ( ) ;
301
+ functionCallBuilder . Append ( ");" ) ;
302
+
303
+ functionCallExpression = functionCallBuilder . ToString ( ) ;
304
+ functionCallBuilder . Clear ( ) ;
305
+ }
306
+ else
307
+ {
308
+ functionCallExpression = string . Format ( "{0}();" , functionName ) ;
263
309
}
264
310
265
- object result = Evaluate ( string . Format ( "{0}({1});" , functionName , serializedArguments ) ) ;
311
+ object result = Evaluate ( functionCallExpression ) ;
266
312
267
313
return result ;
268
314
}
0 commit comments