fan-1.0 /src/sys/dotnet/fan/sys/Method.cs

Language C# Lines 595
MD5 Hash 7c376faaa7519bf0984dd7faa2cbf033 Estimated Cost $11,464 (why?)
Repository https://bitbucket.org/bedlaczech/fan-1.0 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   27 Sep 06  Andy Frank  Creation
//

using System;
using System.Reflection;
using System.Text;
using Fanx.Fcode;
using Fanx.Util;

namespace Fan.Sys
{
  /// <summary>
  /// Method is an invocable operation on a Type.
  /// </summary>
  public class Method : Slot
  {

  //////////////////////////////////////////////////////////////////////////
  // C# Constructor
  //////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Constructor used by Type.reflect.
    /// </summary>
    public Method(Type parent, string name, int flags, Facets facets, int lineNum, Type returns, Type inheritedReturns, List pars)
     : this(parent, name, flags, facets, lineNum, returns, inheritedReturns, pars, null)
    {
    }

    /// <summary>
    /// Constructor used by GenericType and we are given the generic
    /// method that is being parameterized.
    /// </summary>
    public Method(Type parent, string name, int flags, Facets facets, int lineNum, Type returns, Type inheritedReturns, List pars, Method generic)
      : base(parent, name, flags, facets, lineNum)
    {
      List fparams = pars.ro();
      if ((flags & (FConst.Static|FConst.Ctor)) == 0)
      {
        object[] temp = new object[pars.sz()+1];
        temp[0] = new Param("this", parent, 0);
        pars.copyInto(temp, 1, pars.sz());
        fparams = new List(Sys.ParamType, temp);
      }

      this.m_func = new MethodFunc(this, returns, fparams);
      this.m_params = pars;
      this.m_inheritedReturns = inheritedReturns;
      this.m_mask = (generic != null) ? 0 : toMask(parent, returns, pars);
      this.m_generic = generic;
    }

    /// <summary>
    /// Compute if the method signature contains generic parameter types.
    /// </summary>
    private static int toMask(Type parent, Type returns, List pars)
    {
      // we only use generics in Sys
      if (parent.pod() != Sys.m_sysPod) return 0;

      int p = returns.isGenericParameter() ? 1 : 0;
      for (int i=0; i<pars.sz(); ++i)
        p |= ((Param)pars.get(i)).m_type.isGenericParameter() ? 1 : 0;

      int mask = 0;
      if (p != 0) mask |= GENERIC;
      return mask;
    }

  //////////////////////////////////////////////////////////////////////////
  // Methods
  //////////////////////////////////////////////////////////////////////////

    public override Type @typeof() { return Sys.MethodType; }

    public Type returns() { return m_func.returns(); }

    public Type inheritedReturns() { return m_inheritedReturns; }

    public List @params() { return m_params.ro(); }

    public Func func() { return m_func; }

    public override string signature()
    {
      StringBuilder s = new StringBuilder();
      s.Append(m_func.returns()).Append(' ').Append(m_name).Append('(');
      for (int i=0; i<m_params.sz(); ++i)
      {
        if (i > 0) s.Append(", ");
        Param p = (Param)m_params.get(i);
        s.Append(p.m_type).Append(' ').Append(p.m_name);
      }
      s.Append(')');
      return s.ToString();
    }

    public override object trap(string name, List args)
    {
      // private undocumented access
      if (name == "inheritedReturnType")
        return m_inheritedReturns;
      else
        return base.trap(name, args);
    }

  //////////////////////////////////////////////////////////////////////////
  // Generics
  //////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Return if this method contains generic parameters in it's signature.
    /// </summary>
    public bool isGenericMethod()
    {
      return (m_mask & GENERIC) != 0;
    }

    /// <summary>
    /// Return if this method is the parameterization of a generic method,
    /// with all the generic parameters filled in with real types.
    /// </summary>
    public bool isGenericInstance()
    {
      return m_generic != null;
    }

    /// <summary>
    /// If isGenericInstance is true, then return the generic method which
    /// this method instantiates.  The generic method may be used to access
    /// the actual signatures used in the Java code (via getRawType).  If
    /// this method is not a generic instance, return null.
    /// </summary>
    public Method getGenericMethod()
    {
      return m_generic;
    }

  //////////////////////////////////////////////////////////////////////////
  // Call Conveniences
  //////////////////////////////////////////////////////////////////////////

    public object callList(List args) { return m_func.callList(args); }
    public object callOn(object target, List args) { return m_func.callOn(target, args); }
    public object call() { return m_func.call(); }
    public object call(object a) { return m_func.call(a); }
    public object call(object a, object b) { return m_func.call(a,b); }
    public object call(object a, object b, object c) { return m_func.call(a,b,c); }
    public object call(object a, object b, object c, object d) { return m_func.call(a,b,c,d); }
    public object call(object a, object b, object c, object d, object e) { return m_func.call(a,b,c,d,e); }
    public object call(object a, object b, object c, object d, object e, object f) { return m_func.call(a,b,c,d,e,f); }
    public object call(object a, object b, object c, object d, object e, object f, object g) { return m_func.call(a,b,c,d,e,f,g); }
    public object call(object a, object b, object c, object d, object e, object f, object g, object h) { return m_func.call(a,b,c,d,e,f,g,h); }

  //////////////////////////////////////////////////////////////////////////
  // MethodFunc
  //////////////////////////////////////////////////////////////////////////

    internal class MethodFunc : Func
    {
      internal MethodFunc(Method method, Type returns, List pars)
      {
        this.m         = method;
        this.m_returns = returns;
        this.m_params  = pars;
      }

      private Method m;
      private Type m_returns;
      private List m_params;

      public override Type returns() { return m_returns; }

      public override long arity() { return m_params.size(); }

      public override List @params() { return m_params; }

      public override Method method() { return m; }

      public override bool isImmutable() { return true; }

      public override object callList(List args)
      {
        int argsSize = args == null ? 0 : args.sz();

        bool isStatic = _isStatic();
        int p = checkArgs(argsSize, isStatic, false);
        object[] a = new object[p];

        if (isStatic)
        {
          if (args != null && a.Length > 0) args.toArray(a, 0, a.Length);
          return m.invoke(null, a);
        }
        else
        {
          object i = args.get(0);
          if (a.Length > 0) args.toArray(a, 1, a.Length);
          return m.invoke(i, a);
        }
      }

      public override object callOn(object target, List args)
      {
        int argsSize = args == null ? 0 : args.sz();
        bool dotnetStatic = _isStatic();
        bool fanStatic = ((m.m_flags & (FConst.Static|FConst.Ctor)) != 0);

        if (dotnetStatic && !fanStatic)
        {
          // if Java static doesn't match Fantom static, then this is
          // a FanXXX method which we need to call as Java static
          int p = checkArgs(argsSize, false, true);
          object[] a = new object[p+1];
          a[0] = target;
          if (args != null && a.Length > 0) args.copyInto(a, 1, a.Length-1);
          return m.invoke(null, a);
        }
        else
        {
          // we don't include target as part of arguments
          int p = checkArgs(argsSize, dotnetStatic, true);
          object[] a = new object[p];
          if (args != null && a.Length > 0) args.toArray(a, 0, a.Length);
          return m.invoke(target, a);
        }
      }

      public override object call()
      {
        bool isStatic = _isStatic();
        checkArgs(0, isStatic, false);
        return m.invoke(null, noArgs);
      }

      public override object call(object a)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(1, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(2, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b, object c)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(3, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          if (p > 2) args[2] = c;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          if (p > 1) args[1] = c;
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b, object c, object d)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(4, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          if (p > 2) args[2] = c;
          if (p > 3) args[3] = d;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          if (p > 1) args[1] = c;
          if (p > 2) args[2] = d;
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b, object c, object d, object e)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(5, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          if (p > 2) args[2] = c;
          if (p > 3) args[3] = d;
          if (p > 4) args[4] = e;
          return m.invoke(null, args);
        }


        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          if (p > 1) args[1] = c;
          if (p > 2) args[2] = d;
          if (p > 3) args[3] = e;
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b, object c, object d, object e, object f)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(6, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          if (p > 2) args[2] = c;
          if (p > 3) args[3] = d;
          if (p > 4) args[4] = e;
          if (p > 5) args[5] = f;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          if (p > 1) args[1] = c;
          if (p > 2) args[2] = d;
          if (p > 3) args[3] = e;
          if (p > 4) args[4] = f;
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b, object c, object d, object e, object f, object g)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(7, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          if (p > 2) args[2] = c;
          if (p > 3) args[3] = d;
          if (p > 4) args[4] = e;
          if (p > 5) args[5] = f;
          if (p > 6) args[6] = g;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          if (p > 1) args[1] = c;
          if (p > 2) args[2] = d;
          if (p > 3) args[3] = e;
          if (p > 4) args[4] = f;
          if (p > 5) args[5] = g;
          return m.invoke(a, args);
        }
      }

      public override object call(object a, object b, object c, object d, object e, object f, object g, object h)
      {
        bool isStatic = _isStatic();
        int p = checkArgs(8, isStatic, false);
        object[] args;
        if (isStatic)
        {
          args = new object[p];
          if (p > 0) args[0] = a;
          if (p > 1) args[1] = b;
          if (p > 2) args[2] = c;
          if (p > 3) args[3] = d;
          if (p > 4) args[4] = e;
          if (p > 5) args[5] = f;
          if (p > 6) args[6] = g;
          if (p > 7) args[7] = h;
          return m.invoke(null, args);
        }
        else
        {
          args = new object[p];
          if (p > 0) args[0] = b;
          if (p > 1) args[1] = c;
          if (p > 2) args[2] = d;
          if (p > 3) args[3] = e;
          if (p > 4) args[4] = f;
          if (p > 5) args[5] = g;
          if (p > 6) args[6] = h;
          return m.invoke(a, args);
        }
      }

      private bool _isStatic()
      {
        try
        {
          // ensure parent has finished emitting so that reflect is populated
          m.m_parent.finish();

          // return if .NET method(s) is static
          return m.m_reflect[0].IsStatic;
        }
        catch (Exception)
        {
          throw Err.make("Method not mapped to System.Reflection correctly " + m.qname()).val;
        }
      }

      private int checkArgs(int args, bool isStatic, bool isCallOn)
      {
        // compuate min/max parameters - reflect contains all the method versions
        // with full pars at index zero, and full defaults at reflect.Length-1
        int max = m_params.sz();
        if (!isStatic) max--;
        int min = max-m.m_reflect.Length+1;

        // do checking
        if (isStatic || isCallOn)
        {
          if (args < min) throw ArgErr.make("Too few arguments: " + args + " < " + min+".."+max).val;
        }
        else
        {
          if (args < min+1) throw ArgErr.make("Too few arguments: " + args + " < instance+" + min+".."+max).val;
          args--;
        }

        // return size of arguments to pass to java method
        return args <= max ? args : max;
      }
    }

  //////////////////////////////////////////////////////////////////////////
  // Reflection
  //////////////////////////////////////////////////////////////////////////

    public int minParams()
    {
      if (m_minParams < 0)
      {
        int min = 0;
        for (; min<m_params.sz(); ++min)
          if (((Param)m_params.get(min)).hasDefault()) break;
        m_minParams = min;
      }
      return m_minParams;
    }

    private bool isInstance() { return (m_flags & (FConst.Static|FConst.Ctor)) == 0; }

    internal object invoke(object instance, object[] args)
    {
      if (m_reflect == null) m_parent.finish();

      try
      {
        // zero index is full signature up to using max defaults
        int index = m_params.sz()-args.Length;
        if (m_parent.dotnetRepr() && isInstance()) index++;
        if (index < 0) index = 0;
        MethodInfo m = m_reflect[index];

        //System.Console.WriteLine(">>> " + m_reflect.Length + "/" + index);
        //System.Console.WriteLine(m_reflect[index]);
        //System.Console.WriteLine("---");
        //for (int i=0; i<m_reflect.Length; i++)
        //  System.Console.WriteLine(m_reflect[i]);

        // TODO - not sure how this should work entirely yet, but we need
        // to be responsible for "boxing" Fantom wrappers and primitives

        // box the parameters
        ParameterInfo[] pars = m.GetParameters();
        for (int i=0; i<args.Length; i++)
        {
          System.Type pt = pars[i].ParameterType;
          if (pt == boolPrimitive && args[i] is Fan.Sys.Boolean)
          {
            args[i] = (args[i] as Fan.Sys.Boolean).booleanValue();
          }
          else if (pt == doublePrimitive && args[i] is Fan.Sys.Double)
          {
            args[i] = (args[i] as Fan.Sys.Double).doubleValue();
          }
          else if (pt == longPrimitive && args[i] is Fan.Sys.Long)
          {
            args[i] = (args[i] as Fan.Sys.Long).longValue();
          }
        }

        // invoke method
        object ret = m.Invoke(instance, args);

        // box the return value
        return FanUtil.box(ret);
      }
      catch (ArgumentException e)
      {
        throw ArgErr.make(e).val;
      }
      catch (TargetInvocationException e)
      {
        Err err = Err.make(e.InnerException);
        err.m_stack = e.InnerException.StackTrace;
        throw err.val;
      }
      catch (Exception e)
      {
        if (m_reflect == null)
          throw Err.make("Method not mapped to System.Reflection.MethodInfo correctly " + m_qname).val;

        /*
        System.Console.WriteLine("ERROR:      " + signature());
        System.Console.WriteLine("  instance: " + instance);
        System.Console.WriteLine("  args:     " + (args == null ? "null" : ""+args.Length));
        for (int i=0; args != null && i<args.Length; ++i)
          System.Console.WriteLine("    args[" + i + "] = " + args[i]);
        Err.dumpStack(e);
        */

        throw Err.make("Cannot call '" + this + "': " + e).val;
      }
    }

    private static System.Type boolPrimitive = System.Type.GetType("System.Boolean");
    private static System.Type doublePrimitive = System.Type.GetType("System.Double");
    private static System.Type longPrimitive = System.Type.GetType("System.Int64");

  //////////////////////////////////////////////////////////////////////////
  // Fields
  //////////////////////////////////////////////////////////////////////////

    internal static readonly int GENERIC = 0x01;  // is this a generic method
    internal static readonly object[] noArgs = new object[0];

    internal Func m_func;
    internal List m_params;           // might be different from func.params is instance method
    internal Type m_inheritedReturns; // for covariance
    internal int m_mask;
    internal Method m_generic;
    internal MethodInfo[] m_reflect;
    private int m_minParams = -1;

  }
}
Back to Top