@@ -241,7 +241,7 @@ private JsValue MapToScriptType(object value)
241241 switch ( typeCode )
242242 {
243243 case TypeCode . Boolean :
244- return JsValue . FromBoolean ( ( bool ) value ) ;
244+ return ( bool ) value ? JsValue . True : JsValue . False ;
245245
246246 case TypeCode . SByte :
247247 case TypeCode . Byte :
@@ -335,6 +335,50 @@ private object[] MapToHostType(JsValue[] args)
335335 return args . Select ( MapToHostType ) . ToArray ( ) ;
336336 }
337337
338+ /// <summary>
339+ /// Adds a reference to the value
340+ /// </summary>
341+ /// <param name="value">The value</param>
342+ private static void AddReferenceToValue ( JsValue value )
343+ {
344+ if ( CanHaveReferences ( value ) )
345+ {
346+ value . AddRef ( ) ;
347+ }
348+ }
349+
350+ /// <summary>
351+ /// Removes a reference to the value
352+ /// </summary>
353+ /// <param name="value">The value</param>
354+ private static void RemoveReferenceToValue ( JsValue value )
355+ {
356+ if ( CanHaveReferences ( value ) )
357+ {
358+ value . Release ( ) ;
359+ }
360+ }
361+
362+ /// <summary>
363+ /// Checks whether the value can have references
364+ /// </summary>
365+ /// <param name="value">The value</param>
366+ /// <returns>Result of check (true - may have; false - may not have)</returns>
367+ private static bool CanHaveReferences ( JsValue value )
368+ {
369+ JsValueType valueType = value . ValueType ;
370+
371+ switch ( valueType )
372+ {
373+ case JsValueType . Null :
374+ case JsValueType . Undefined :
375+ case JsValueType . Boolean :
376+ return false ;
377+ default :
378+ return true ;
379+ }
380+ }
381+
338382 private JsValue FromObject ( object value )
339383 {
340384 var del = value as Delegate ;
@@ -951,11 +995,30 @@ protected override object InnerCallFunction(string functionName, params object[]
951995 string . Format ( CoreStrings . Runtime_FunctionNotExist , functionName ) ) ;
952996 }
953997
954- var processedArgs = MapToScriptType ( args ) ;
955- var allProcessedArgs = new [ ] { globalObj } . Concat ( processedArgs ) . ToArray ( ) ;
956-
998+ JsValue resultValue ;
957999 JsValue functionValue = globalObj . GetProperty ( functionId ) ;
958- JsValue resultValue = functionValue . CallFunction ( allProcessedArgs ) ;
1000+
1001+ if ( args . Length > 0 )
1002+ {
1003+ JsValue [ ] processedArgs = MapToScriptType ( args ) ;
1004+
1005+ foreach ( JsValue processedArg in processedArgs )
1006+ {
1007+ AddReferenceToValue ( processedArg ) ;
1008+ }
1009+
1010+ JsValue [ ] allProcessedArgs = new [ ] { globalObj } . Concat ( processedArgs ) . ToArray ( ) ;
1011+ resultValue = functionValue . CallFunction ( allProcessedArgs ) ;
1012+
1013+ foreach ( JsValue processedArg in processedArgs )
1014+ {
1015+ RemoveReferenceToValue ( processedArg ) ;
1016+ }
1017+ }
1018+ else
1019+ {
1020+ resultValue = functionValue . CallFunction ( globalObj ) ;
1021+ }
9591022
9601023 return MapToHostType ( resultValue ) ;
9611024 } ) ;
0 commit comments