@@ -192,16 +192,6 @@ public JsValue MapToScriptType(object value)
192
192
}
193
193
}
194
194
195
- /// <summary>
196
- /// Makes a mapping of array items from the host type to a script type
197
- /// </summary>
198
- /// <param name="args">The source array</param>
199
- /// <returns>The mapped array</returns>
200
- public JsValue [ ] MapToScriptType ( object [ ] args )
201
- {
202
- return args . Select ( MapToScriptType ) . ToArray ( ) ;
203
- }
204
-
205
195
/// <summary>
206
196
/// Makes a mapping of value from the script type to a host type
207
197
/// </summary>
@@ -257,16 +247,6 @@ public object MapToHostType(JsValue value)
257
247
return result ;
258
248
}
259
249
260
- /// <summary>
261
- /// Makes a mapping of array items from the script type to a host type
262
- /// </summary>
263
- /// <param name="args">The source array</param>
264
- /// <returns>The mapped array</returns>
265
- public object [ ] MapToHostType ( JsValue [ ] args )
266
- {
267
- return args . Select ( MapToHostType ) . ToArray ( ) ;
268
- }
269
-
270
250
private EmbeddedObject CreateEmbeddedObjectOrFunction ( object obj )
271
251
{
272
252
var del = obj as Delegate ;
@@ -298,13 +278,13 @@ private EmbeddedObject CreateEmbeddedFunction(Delegate del)
298
278
{
299
279
JsNativeFunction nativeFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
300
280
{
301
- object [ ] processedArgs = GetHostItemMemberArguments ( args ) ;
302
281
#if NET40
303
282
MethodInfo method = del . Method ;
304
283
#else
305
284
MethodInfo method = del . GetMethodInfo ( ) ;
306
285
#endif
307
286
ParameterInfo [ ] parameters = method . GetParameters ( ) ;
287
+ object [ ] processedArgs = GetHostItemMemberArguments ( args , parameters . Length ) ;
308
288
309
289
ReflectionHelpers . FixArgumentTypes ( ref processedArgs , parameters ) ;
310
290
@@ -316,6 +296,7 @@ private EmbeddedObject CreateEmbeddedFunction(Delegate del)
316
296
}
317
297
catch ( Exception e )
318
298
{
299
+ JsValue undefinedValue = JsValue . Undefined ;
319
300
Exception exception = UnwrapException ( e ) ;
320
301
var wrapperException = exception as WrapperException ;
321
302
JsValue errorValue = wrapperException != null ?
@@ -326,7 +307,7 @@ private EmbeddedObject CreateEmbeddedFunction(Delegate del)
326
307
;
327
308
JsContext . SetException ( errorValue ) ;
328
309
329
- return JsValue . Undefined ;
310
+ return undefinedValue ;
330
311
}
331
312
332
313
JsValue resultValue = MapToScriptType ( result ) ;
@@ -397,10 +378,12 @@ private EmbeddedType CreateEmbeddedType(Type type)
397
378
return resultValue ;
398
379
}
399
380
381
+ JsValue undefinedValue = JsValue . Undefined ;
382
+
400
383
if ( constructors . Length == 0 )
401
384
{
402
385
CreateAndSetError ( string . Format ( Strings . Runtime_HostTypeConstructorNotFound , typeName ) ) ;
403
- return JsValue . Undefined ;
386
+ return undefinedValue ;
404
387
}
405
388
406
389
var bestFitConstructor = ( ConstructorInfo ) ReflectionHelpers . GetBestFitMethod (
@@ -409,7 +392,7 @@ private EmbeddedType CreateEmbeddedType(Type type)
409
392
{
410
393
CreateAndSetReferenceError ( string . Format (
411
394
Strings . Runtime_SuitableConstructorOfHostTypeNotFound , typeName ) ) ;
412
- return JsValue . Undefined ;
395
+ return undefinedValue ;
413
396
}
414
397
415
398
ReflectionHelpers . FixArgumentTypes ( ref processedArgs , bestFitConstructor . GetParameters ( ) ) ;
@@ -430,7 +413,7 @@ private EmbeddedType CreateEmbeddedType(Type type)
430
413
;
431
414
JsContext . SetException ( errorValue ) ;
432
415
433
- return JsValue . Undefined ;
416
+ return undefinedValue ;
434
417
}
435
418
436
419
resultValue = MapToScriptType ( result ) ;
@@ -502,11 +485,13 @@ private void ProjectFields(EmbeddedItem externalItem)
502
485
503
486
JsNativeFunction nativeGetFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
504
487
{
488
+ JsValue undefinedValue = JsValue . Undefined ;
489
+
505
490
if ( instance && obj == null )
506
491
{
507
492
CreateAndSetTypeError ( string . Format (
508
493
Strings . Runtime_InvalidThisContextForHostObjectField , fieldName ) ) ;
509
- return JsValue . Undefined ;
494
+ return undefinedValue ;
510
495
}
511
496
512
497
object result ;
@@ -538,7 +523,7 @@ private void ProjectFields(EmbeddedItem externalItem)
538
523
}
539
524
JsContext . SetException ( errorValue ) ;
540
525
541
- return JsValue . Undefined ;
526
+ return undefinedValue ;
542
527
}
543
528
544
529
JsValue resultValue = MapToScriptType ( result ) ;
@@ -552,11 +537,13 @@ private void ProjectFields(EmbeddedItem externalItem)
552
537
553
538
JsNativeFunction nativeSetFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
554
539
{
540
+ JsValue undefinedValue = JsValue . Undefined ;
541
+
555
542
if ( instance && obj == null )
556
543
{
557
544
CreateAndSetTypeError ( string . Format (
558
545
Strings . Runtime_InvalidThisContextForHostObjectField , fieldName ) ) ;
559
- return JsValue . Undefined ;
546
+ return undefinedValue ;
560
547
}
561
548
562
549
object value = MapToHostType ( args [ 1 ] ) ;
@@ -589,10 +576,10 @@ private void ProjectFields(EmbeddedItem externalItem)
589
576
}
590
577
JsContext . SetException ( errorValue ) ;
591
578
592
- return JsValue . Undefined ;
579
+ return undefinedValue ;
593
580
}
594
581
595
- return JsValue . Undefined ;
582
+ return undefinedValue ;
596
583
} ;
597
584
nativeFunctions . Add ( nativeSetFunction ) ;
598
585
@@ -626,11 +613,13 @@ private void ProjectProperties(EmbeddedItem externalItem)
626
613
{
627
614
JsNativeFunction nativeGetFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
628
615
{
616
+ JsValue undefinedValue = JsValue . Undefined ;
617
+
629
618
if ( instance && obj == null )
630
619
{
631
620
CreateAndSetTypeError ( string . Format (
632
621
Strings . Runtime_InvalidThisContextForHostObjectProperty , propertyName ) ) ;
633
- return JsValue . Undefined ;
622
+ return undefinedValue ;
634
623
}
635
624
636
625
object result ;
@@ -662,7 +651,7 @@ private void ProjectProperties(EmbeddedItem externalItem)
662
651
}
663
652
JsContext . SetException ( errorValue ) ;
664
653
665
- return JsValue . Undefined ;
654
+ return undefinedValue ;
666
655
}
667
656
668
657
JsValue resultValue = MapToScriptType ( result ) ;
@@ -679,11 +668,13 @@ private void ProjectProperties(EmbeddedItem externalItem)
679
668
{
680
669
JsNativeFunction nativeSetFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
681
670
{
671
+ JsValue undefinedValue = JsValue . Undefined ;
672
+
682
673
if ( instance && obj == null )
683
674
{
684
675
CreateAndSetTypeError ( string . Format (
685
676
Strings . Runtime_InvalidThisContextForHostObjectProperty , propertyName ) ) ;
686
- return JsValue . Undefined ;
677
+ return undefinedValue ;
687
678
}
688
679
689
680
object value = MapToHostType ( args [ 1 ] ) ;
@@ -716,10 +707,10 @@ private void ProjectProperties(EmbeddedItem externalItem)
716
707
}
717
708
JsContext . SetException ( errorValue ) ;
718
709
719
- return JsValue . Undefined ;
710
+ return undefinedValue ;
720
711
}
721
712
722
- return JsValue . Undefined ;
713
+ return undefinedValue ;
723
714
} ;
724
715
nativeFunctions . Add ( nativeSetFunction ) ;
725
716
@@ -752,11 +743,13 @@ private void ProjectMethods(EmbeddedItem externalItem)
752
743
753
744
JsNativeFunction nativeFunction = ( callee , isConstructCall , args , argCount , callbackData ) =>
754
745
{
746
+ JsValue undefinedValue = JsValue . Undefined ;
747
+
755
748
if ( instance && obj == null )
756
749
{
757
750
CreateAndSetTypeError ( string . Format (
758
751
Strings . Runtime_InvalidThisContextForHostObjectMethod , methodName ) ) ;
759
- return JsValue . Undefined ;
752
+ return undefinedValue ;
760
753
}
761
754
762
755
object [ ] processedArgs = GetHostItemMemberArguments ( args ) ;
@@ -767,7 +760,7 @@ private void ProjectMethods(EmbeddedItem externalItem)
767
760
{
768
761
CreateAndSetReferenceError ( string . Format (
769
762
Strings . Runtime_SuitableMethodOfHostObjectNotFound , methodName ) ) ;
770
- return JsValue . Undefined ;
763
+ return undefinedValue ;
771
764
}
772
765
773
766
ReflectionHelpers . FixArgumentTypes ( ref processedArgs , bestFitMethod . GetParameters ( ) ) ;
@@ -801,7 +794,7 @@ private void ProjectMethods(EmbeddedItem externalItem)
801
794
}
802
795
JsContext . SetException ( errorValue ) ;
803
796
804
- return JsValue . Undefined ;
797
+ return undefinedValue ;
805
798
}
806
799
807
800
JsValue resultValue = MapToScriptType ( result ) ;
@@ -815,11 +808,35 @@ private void ProjectMethods(EmbeddedItem externalItem)
815
808
}
816
809
}
817
810
818
- [ MethodImpl ( ( MethodImplOptions ) 256 /* AggressiveInlining */ ) ]
819
- private object [ ] GetHostItemMemberArguments ( JsValue [ ] args )
811
+ private object [ ] GetHostItemMemberArguments ( JsValue [ ] args , int maxArgCount = - 1 )
820
812
{
821
- object [ ] processedArgs = args . Length > 1 ?
822
- MapToHostType ( args . Skip ( 1 ) . ToArray ( ) ) : new object [ 0 ] ;
813
+ if ( args == null )
814
+ {
815
+ throw new ArgumentNullException ( nameof ( args ) ) ;
816
+ }
817
+
818
+ int argCount = args . Length ;
819
+ const int skippedArgCount = 1 ;
820
+ int processedArgCount = argCount >= skippedArgCount ? argCount - skippedArgCount : 0 ;
821
+ if ( maxArgCount >= 0 && processedArgCount > maxArgCount )
822
+ {
823
+ processedArgCount = maxArgCount ;
824
+ }
825
+ object [ ] processedArgs ;
826
+
827
+ if ( processedArgCount > 0 )
828
+ {
829
+ processedArgs = args
830
+ . Skip ( skippedArgCount )
831
+ . Take ( processedArgCount )
832
+ . Select ( MapToHostType )
833
+ . ToArray ( )
834
+ ;
835
+ }
836
+ else
837
+ {
838
+ processedArgs = new object [ 0 ] ;
839
+ }
823
840
824
841
return processedArgs ;
825
842
}
0 commit comments