File: javascriptcomplex.swg

package info (click to toggle)
swig 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,980 kB
  • sloc: cpp: 54,284; ansic: 29,022; java: 17,595; python: 12,734; cs: 10,421; ruby: 7,263; yacc: 6,501; makefile: 5,860; javascript: 5,538; sh: 5,422; perl: 4,246; php: 3,733; ml: 2,198; tcl: 2,015; lisp: 1,448; xml: 115
file content (120 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download | duplicates (2)
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
/*
  Defines the As/From converters for double/float complex, you need to
  provide complex Type, the Name you want to use in the converters,
  the complex Constructor method, and the Real and Imag complex
  accessor methods.

  See the std_complex.i and ccomplex.i for concrete examples.
*/

/* the common from converter */
%define %swig_fromcplx_conv(Type, Real, Imag)
%fragment(SWIG_From_frag(Type),"header",
          fragment=SWIG_From_frag(double))
{
SWIGINTERNINLINE Napi::Value
#if defined(__cplusplus)
SWIG_From_dec(Type)(const Type& c)
#else
SWIG_From_dec(Type)(Type c)
#endif
{
  Napi::Array vals = Napi::Array::New(0);

  vals.Set(0, SWIG_From(double)(Real(c)));
  vals.Set(1, SWIG_From(double)(Imag(c)));
  return vals;
}
}
%enddef

/* the double case */
%define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
	  fragment=SWIG_AsVal_frag(double))
{
SWIGINTERN int
SWIG_AsVal_dec(Type) (Napi::Value o, Type* val)
{
  if (o.IsArray()) {
    Napi::Array array = Napi::Array::Cast(o);
    
    if (array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2].");
    double re, im;
    Napi::Value r;

    r = array.Get(0);
    if (!r.IsNumber()) {
      return SWIG_TypeError;
    }
    re = r.ToNumber().DoubleValue();
    
    r = array.Get(1);
    if (!r.IsNumber()) {
      return SWIG_TypeError;
    }
    im = r.ToNumber().DoubleValue();
    
    if (val) *val = Constructor(re, im);
    return SWIG_OK;
  } else if (o.IsNumber()) {
    double d = o.ToNumber().DoubleValue();
    if (val) *val = Constructor(d, 0.0);
    return SWIG_OK;
  }
  return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef

/* the float case */
%define %swig_cplxflt_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
          fragment=SWIG_AsVal_frag(float)) {
SWIGINTERN int
SWIG_AsVal_dec(Type) (Napi::Value o, Type* val)
{
  if (o.IsArray()) {
    Napi::Array array = o.As<Napi::Array>();
    
    if (array.Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2].");
    double re, im;
    Napi::Value r;
    
    r = array.Get(0);
    if (!r.IsNumber()) {
      return SWIG_TypeError;
    }
    re = r.ToNumber().DoubleValue();
    
    r = array.Get(1);
    if (!r.IsNumber()) {
      return SWIG_TypeError;
    }
    im = r.ToNumber().DoubleValue();
    
    if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) {
      if (val) *val = Constructor(%numeric_cast(re, float),
                                  %numeric_cast(im, float));
      return SWIG_OK;
    } else {
      return SWIG_OverflowError;
    }    
  } else if (o.IsNumber()) {
    float f = static_cast<float>(o.ToNumber().DoubleValue());
    if (val) *val = Constructor(f, 0.0);
    return SWIG_OK;
  }
  return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef

#define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
%swig_cplxflt_conv(Type, Constructor, Real, Imag)


#define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
%swig_cplxdbl_conv(Type, Constructor, Real, Imag)