@@ -172,7 +172,7 @@ private EdgeJsValue MapToScriptType(object value)
172172 switch ( typeCode )
173173 {
174174 case TypeCode . Boolean :
175- return EdgeJsValue . FromBoolean ( ( bool ) value ) ;
175+ return ( bool ) value ? EdgeJsValue . True : EdgeJsValue . False ;
176176
177177 case TypeCode . SByte :
178178 case TypeCode . Byte :
@@ -282,6 +282,50 @@ private object[] MapToHostType(EdgeJsValue[] args)
282282 {
283283 return args . Select ( MapToHostType ) . ToArray ( ) ;
284284 }
285+
286+ /// <summary>
287+ /// Adds a reference to the value
288+ /// </summary>
289+ /// <param name="value">The value</param>
290+ private static void AddReferenceToValue ( EdgeJsValue value )
291+ {
292+ if ( CanHaveReferences ( value ) )
293+ {
294+ value . AddRef ( ) ;
295+ }
296+ }
297+
298+ /// <summary>
299+ /// Removes a reference to the value
300+ /// </summary>
301+ /// <param name="value">The value</param>
302+ private static void RemoveReferenceToValue ( EdgeJsValue value )
303+ {
304+ if ( CanHaveReferences ( value ) )
305+ {
306+ value . Release ( ) ;
307+ }
308+ }
309+
310+ /// <summary>
311+ /// Checks whether the value can have references
312+ /// </summary>
313+ /// <param name="value">The value</param>
314+ /// <returns>Result of check (true - may have; false - may not have)</returns>
315+ private static bool CanHaveReferences ( EdgeJsValue value )
316+ {
317+ JsValueType valueType = value . ValueType ;
318+
319+ switch ( valueType )
320+ {
321+ case JsValueType . Null :
322+ case JsValueType . Undefined :
323+ case JsValueType . Boolean :
324+ return false ;
325+ default :
326+ return true ;
327+ }
328+ }
285329#if NETSTANDARD1_3
286330
287331 private EdgeJsValue FromObject ( object value )
@@ -945,11 +989,30 @@ public override object CallFunction(string functionName, params object[] args)
945989 string . Format ( CommonStrings . Runtime_FunctionNotExist , functionName ) ) ;
946990 }
947991
948- var processedArgs = MapToScriptType ( args ) ;
949- var allProcessedArgs = new [ ] { globalObj } . Concat ( processedArgs ) . ToArray ( ) ;
950-
992+ EdgeJsValue resultValue ;
951993 EdgeJsValue functionValue = globalObj . GetProperty ( functionId ) ;
952- EdgeJsValue resultValue = functionValue . CallFunction ( allProcessedArgs ) ;
994+
995+ if ( args . Length > 0 )
996+ {
997+ EdgeJsValue [ ] processedArgs = MapToScriptType ( args ) ;
998+
999+ foreach ( EdgeJsValue processedArg in processedArgs )
1000+ {
1001+ AddReferenceToValue ( processedArg ) ;
1002+ }
1003+
1004+ EdgeJsValue [ ] allProcessedArgs = new [ ] { globalObj } . Concat ( processedArgs ) . ToArray ( ) ;
1005+ resultValue = functionValue . CallFunction ( allProcessedArgs ) ;
1006+
1007+ foreach ( EdgeJsValue processedArg in processedArgs )
1008+ {
1009+ RemoveReferenceToValue ( processedArg ) ;
1010+ }
1011+ }
1012+ else
1013+ {
1014+ resultValue = functionValue . CallFunction ( globalObj ) ;
1015+ }
9531016
9541017 return MapToHostType ( resultValue ) ;
9551018 } ) ;
0 commit comments