1111using OriginalClrProxy = YantraJS . Core . Clr . ClrProxy ;
1212using OriginalClrType = YantraJS . Core . Clr . ClrType ;
1313using OriginalContext = YantraJS . Core . JSContext ;
14+ using OriginalDate = YantraJS . Core . JSDate ;
1415using OriginalException = YantraJS . Core . JSException ;
1516using OriginalFunction = YantraJS . Core . JSFunction ;
17+ using OriginalJsonObject = YantraJS . Core . JSJSON ;
1618using OriginalTypeConverter = YantraJS . Utils . TypeConverter ;
1719using OriginalUndefined = YantraJS . Core . JSUndefined ;
1820using OriginalValue = YantraJS . Core . JSValue ;
@@ -59,6 +61,11 @@ public sealed class YantraJsEngine : JsEngineBase
5961 /// </summary>
6062 private OriginalContext _jsContext ;
6163
64+ /// <summary>
65+ /// JS debugging console callback
66+ /// </summary>
67+ private YantraJsConsoleCallback _consoleCallback ;
68+
6269 /// <summary>
6370 /// Synchronizer of code execution
6471 /// </summary>
@@ -75,11 +82,35 @@ public sealed class YantraJsEngine : JsEngineBase
7582 /// Constructs an instance of adapter for the Yantra JS engine
7683 /// </summary>
7784 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 )
7893 {
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 )
80111 {
81- ClrMemberNamingConvention = OriginalClrMemberNamingConvention . Declared
82- } ;
112+ throw JsErrorHelpers . WrapEngineLoadException ( e , EngineName , EngineVersion , true ) ;
113+ }
83114 }
84115
85116
@@ -139,6 +170,21 @@ private static object MapToHostType(OriginalValue value)
139170 {
140171 result = value . ToString ( ) ;
141172 }
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+ }
142188 else
143189 {
144190 result = value ;
@@ -201,7 +247,7 @@ private static OriginalFunction CreateEmbeddedFunction(Delegate del)
201247 {
202248 MethodInfo method = del . GetMethodInfo ( ) ;
203249 ParameterInfo [ ] parameters = method . GetParameters ( ) ;
204- object [ ] processedArgs = GetHostDelegateArguments ( args . ToArray ( ) , parameters . Length ) ;
250+ object [ ] processedArgs = GetHostDelegateArguments ( args , parameters . Length ) ;
205251
206252 ReflectionHelpers . FixArgumentTypes ( ref processedArgs , parameters ) ;
207253
@@ -231,28 +277,21 @@ private static OriginalFunction CreateEmbeddedFunction(Delegate del)
231277 return originalFunction ;
232278 }
233279
234- private static object [ ] GetHostDelegateArguments ( OriginalValue [ ] args , int maxArgCount )
280+ private static object [ ] GetHostDelegateArguments ( in OriginalArguments args , int maxArgCount )
235281 {
236- if ( args == null )
282+ int argCount = args . Length ;
283+ if ( argCount == 0 )
237284 {
238- throw new ArgumentNullException ( nameof ( args ) ) ;
285+ return new object [ 0 ] ;
239286 }
240287
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 ] ;
244290
245- if ( processedArgCount > 0 )
291+ for ( int argIndex = 0 ; argIndex < processedArgCount ; argIndex ++ )
246292 {
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 ) ;
256295 }
257296
258297 return processedArgs ;
@@ -345,6 +384,43 @@ private WrapperException WrapJsException(OriginalException originalException)
345384 return wrapperException ;
346385 }
347386
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+
348424 #endregion
349425
350426 /// <summary>
@@ -688,6 +764,12 @@ public override void Dispose()
688764 {
689765 if ( _jsContext != null )
690766 {
767+ if ( _consoleCallback != null )
768+ {
769+ _jsContext . ConsoleEvent -= OnConsoleWrite ;
770+ _consoleCallback = null ;
771+ }
772+
691773 _jsContext . Dispose ( ) ;
692774 _jsContext = null ;
693775 }
0 commit comments