11
11
using OriginalClrProxy = YantraJS . Core . Clr . ClrProxy ;
12
12
using OriginalClrType = YantraJS . Core . Clr . ClrType ;
13
13
using OriginalContext = YantraJS . Core . JSContext ;
14
+ using OriginalDate = YantraJS . Core . JSDate ;
14
15
using OriginalException = YantraJS . Core . JSException ;
15
16
using OriginalFunction = YantraJS . Core . JSFunction ;
17
+ using OriginalJsonObject = YantraJS . Core . JSJSON ;
16
18
using OriginalTypeConverter = YantraJS . Utils . TypeConverter ;
17
19
using OriginalUndefined = YantraJS . Core . JSUndefined ;
18
20
using OriginalValue = YantraJS . Core . JSValue ;
@@ -59,6 +61,11 @@ public sealed class YantraJsEngine : JsEngineBase
59
61
/// </summary>
60
62
private OriginalContext _jsContext ;
61
63
64
+ /// <summary>
65
+ /// JS debugging console callback
66
+ /// </summary>
67
+ private YantraJsConsoleCallback _consoleCallback ;
68
+
62
69
/// <summary>
63
70
/// Synchronizer of code execution
64
71
/// </summary>
@@ -75,11 +82,35 @@ public sealed class YantraJsEngine : JsEngineBase
75
82
/// Constructs an instance of adapter for the Yantra JS engine
76
83
/// </summary>
77
84
public YantraJsEngine ( )
85
+ : this ( new YantraSettings ( ) )
86
+ { }
87
+
88
+ /// <summary>
89
+ /// Constructs an instance of adapter for the Yantra JS engine
90
+ /// </summary>
91
+ /// <param name="settings">Settings of the Yantra JS engine</param>
92
+ public YantraJsEngine ( YantraSettings settings )
78
93
{
79
- _jsContext = new OriginalContext ( )
94
+ YantraSettings yantraSettings = settings ?? new YantraSettings ( ) ;
95
+ _consoleCallback = yantraSettings . ConsoleCallback ;
96
+
97
+ try
98
+ {
99
+ _jsContext = new OriginalContext ( )
100
+ {
101
+ ClrMemberNamingConvention = OriginalClrMemberNamingConvention . Declared ,
102
+ Debugger = yantraSettings . Debugger
103
+ } ;
104
+
105
+ if ( _consoleCallback != null )
106
+ {
107
+ _jsContext . ConsoleEvent += OnConsoleWrite ;
108
+ }
109
+ }
110
+ catch ( Exception e )
80
111
{
81
- ClrMemberNamingConvention = OriginalClrMemberNamingConvention . Declared
82
- } ;
112
+ throw JsErrorHelpers . WrapEngineLoadException ( e , EngineName , EngineVersion , true ) ;
113
+ }
83
114
}
84
115
85
116
@@ -139,6 +170,21 @@ private static object MapToHostType(OriginalValue value)
139
170
{
140
171
result = value . ToString ( ) ;
141
172
}
173
+ else if ( value is OriginalDate )
174
+ {
175
+ var jsDate = ( OriginalDate ) value ;
176
+ result = jsDate . DateTime ;
177
+ }
178
+ else if ( value is OriginalClrProxy )
179
+ {
180
+ var clrProxy = ( OriginalClrProxy ) value ;
181
+ result = clrProxy . Target ;
182
+ }
183
+ else if ( value is OriginalClrType )
184
+ {
185
+ var clrType = ( OriginalClrType ) value ;
186
+ result = clrType . Type ;
187
+ }
142
188
else
143
189
{
144
190
result = value ;
@@ -201,7 +247,7 @@ private static OriginalFunction CreateEmbeddedFunction(Delegate del)
201
247
{
202
248
MethodInfo method = del . GetMethodInfo ( ) ;
203
249
ParameterInfo [ ] parameters = method . GetParameters ( ) ;
204
- object [ ] processedArgs = GetHostDelegateArguments ( args . ToArray ( ) , parameters . Length ) ;
250
+ object [ ] processedArgs = GetHostDelegateArguments ( args , parameters . Length ) ;
205
251
206
252
ReflectionHelpers . FixArgumentTypes ( ref processedArgs , parameters ) ;
207
253
@@ -231,28 +277,21 @@ private static OriginalFunction CreateEmbeddedFunction(Delegate del)
231
277
return originalFunction ;
232
278
}
233
279
234
- private static object [ ] GetHostDelegateArguments ( OriginalValue [ ] args , int maxArgCount )
280
+ private static object [ ] GetHostDelegateArguments ( in OriginalArguments args , int maxArgCount )
235
281
{
236
- if ( args == null )
282
+ int argCount = args . Length ;
283
+ if ( argCount == 0 )
237
284
{
238
- throw new ArgumentNullException ( nameof ( args ) ) ;
285
+ return new object [ 0 ] ;
239
286
}
240
287
241
- int argCount = args . Length ;
242
- int processedArgCount = argCount > maxArgCount ? maxArgCount : argCount ;
243
- object [ ] processedArgs ;
288
+ int processedArgCount = Math . Min ( argCount , maxArgCount ) ;
289
+ var processedArgs = new object [ processedArgCount ] ;
244
290
245
- if ( processedArgCount > 0 )
291
+ for ( int argIndex = 0 ; argIndex < processedArgCount ; argIndex ++ )
246
292
{
247
- processedArgs = args
248
- . Take ( processedArgCount )
249
- . Select ( MapToHostType )
250
- . ToArray ( )
251
- ;
252
- }
253
- else
254
- {
255
- processedArgs = new object [ 0 ] ;
293
+ OriginalValue arg = args . GetAt ( argIndex ) ;
294
+ processedArgs [ argIndex ] = MapToHostType ( arg ) ;
256
295
}
257
296
258
297
return processedArgs ;
@@ -345,6 +384,43 @@ private WrapperException WrapJsException(OriginalException originalException)
345
384
return wrapperException ;
346
385
}
347
386
387
+ private void OnConsoleWrite ( OriginalContext context , string type , in OriginalArguments args )
388
+ {
389
+ int argCount = args . Length ;
390
+ var processedArgs = new object [ argCount ] ;
391
+
392
+ for ( int argIndex = 0 ; argIndex < argCount ; argIndex ++ )
393
+ {
394
+ OriginalValue arg = args . GetAt ( argIndex ) ;
395
+ object processedArg = MapToHostType ( arg ) ;
396
+
397
+ if ( processedArg is OriginalValue )
398
+ {
399
+ if ( arg . IsFunction )
400
+ {
401
+ var jsFunction = ( OriginalFunction ) arg ;
402
+ processedArg = string . Format ( "[Function: {0}]" , jsFunction . name ) ;
403
+ }
404
+ else if ( arg . IsSymbol )
405
+ {
406
+ processedArg = string . Format ( "Symbol({0})" , arg . ToString ( ) ) ;
407
+ }
408
+ else if ( arg . IsObject || arg . IsArray )
409
+ {
410
+ processedArg = OriginalJsonObject . Stringify ( arg ) ;
411
+ }
412
+ else
413
+ {
414
+ processedArg = arg . ToString ( ) ;
415
+ }
416
+ }
417
+
418
+ processedArgs [ argIndex ] = processedArg ;
419
+ }
420
+
421
+ _consoleCallback ? . Invoke ( type , processedArgs ) ;
422
+ }
423
+
348
424
#endregion
349
425
350
426
/// <summary>
@@ -688,6 +764,12 @@ public override void Dispose()
688
764
{
689
765
if ( _jsContext != null )
690
766
{
767
+ if ( _consoleCallback != null )
768
+ {
769
+ _jsContext . ConsoleEvent -= OnConsoleWrite ;
770
+ _consoleCallback = null ;
771
+ }
772
+
691
773
_jsContext . Dispose ( ) ;
692
774
_jsContext = null ;
693
775
}
0 commit comments