From: <ne...@us...> - 2012-08-20 18:35:01
|
Revision: 13693 http://swig.svn.sourceforge.net/swig/?rev=13693&view=rev Author: neha1 Date: 2012-08-20 18:34:55 +0000 (Mon, 20 Aug 2012) Log Message: ----------- Add support for complex types. Note: in javascript there is no builtin type for complex values. Instead, a number array of length 2 is mapped to complex. Added Paths: ----------- branches/gsoc2012-javascript/Lib/javascript/jsc/ccomplex.i branches/gsoc2012-javascript/Lib/javascript/jsc/complex.i branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcomplex.swg branches/gsoc2012-javascript/Lib/javascript/jsc/std_complex.i Added: branches/gsoc2012-javascript/Lib/javascript/jsc/ccomplex.i =================================================================== --- branches/gsoc2012-javascript/Lib/javascript/jsc/ccomplex.i (rev 0) +++ branches/gsoc2012-javascript/Lib/javascript/jsc/ccomplex.i 2012-08-20 18:34:55 UTC (rev 13693) @@ -0,0 +1,26 @@ +/* ----------------------------------------------------------------------------- + * ccomplex.i + * + * C complex typemaps + * ISO C99: 7.3 Complex arithmetic <complex.h> + * ----------------------------------------------------------------------------- */ + + +%include <javscriptcomplex.swg> + +%{ +#include <complex.h> +%} + + +/* C complex constructor */ +#define CCplxConst(r, i) ((r) + I*(i)) + +%swig_cplxflt_convn(float complex, CCplxConst, creal, cimag); +%swig_cplxdbl_convn(double complex, CCplxConst, creal, cimag); +%swig_cplxdbl_convn(complex, CCplxConst, creal, cimag); + +/* declaring the typemaps */ +%typemaps_primitive(SWIG_TYPECHECK_CPLXFLT, float complex); +%typemaps_primitive(SWIG_TYPECHECK_CPLXDBL, double complex); +%typemaps_primitive(SWIG_TYPECHECK_CPLXDBL, complex); Added: branches/gsoc2012-javascript/Lib/javascript/jsc/complex.i =================================================================== --- branches/gsoc2012-javascript/Lib/javascript/jsc/complex.i (rev 0) +++ branches/gsoc2012-javascript/Lib/javascript/jsc/complex.i 2012-08-20 18:34:55 UTC (rev 13693) @@ -0,0 +1,6 @@ +#ifdef __cplusplus +%include <std_complex.i> +#else +%include <ccomplex.i> +#endif + Added: branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcomplex.swg =================================================================== --- branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcomplex.swg (rev 0) +++ branches/gsoc2012-javascript/Lib/javascript/jsc/javascriptcomplex.swg 2012-08-20 18:34:55 UTC (rev 13693) @@ -0,0 +1,146 @@ +/* + 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 concret examples. +*/ + +/* the common from converter */ +%define %swig_fromcplx_conv(Type, Real, Imag) +%fragment(SWIG_From_frag(Type),"header", + fragment=SWIG_From_frag(double)) +{ +SWIGINTERNINLINE JSObjectRef +SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c) +{ + JSValueRef vals[2]; + vals[0] = SWIG_From(double)(Real(c)); + vals[1] = SWIG_From(double)(Imag(c)); + return JSObjectMakeArray(context, 2, vals, NULL); +} +} +%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) (JSValueRef o, Type* val) +{ + if (JSValueIsObject(context, o)) { + JSObjectRef array; + JSValueRef exception, js_re, js_im; + double re, im; + int res; + + exception = 0; + res = 0; + + array = JSValueToObject(context, o, &exception); + if(exception != 0) + return SWIG_TypeError; + + js_re = JSObjectGetPropertyAtIndex(context, array, 0, &exception); + if(exception != 0) + return SWIG_TypeError; + + js_im = JSObjectGetPropertyAtIndex(context, array, 1, &exception); + if(exception != 0) + return SWIG_TypeError; + + res = SWIG_AsVal(double)(js_re, &re); + if(!SWIG_IsOK(res)) { + return SWIG_TypeError; + } + + res = SWIG_AsVal(double)(js_im, &im); + if(!SWIG_IsOK(res)) { + return SWIG_TypeError; + } + + if (val) *val = Constructor(re, im); + return SWIG_OK; + } else { + double d; + int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d)); + if (SWIG_IsOK(res)) { + if (val) *val = Constructor(d, 0.0); + return res; + } + } + 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)(JSValueRef o, Type *val) +{ + if (JSValueIsObject(context, o)) { + JSObjectRef array; + JSValueRef exception, js_re, js_im; + double re, im; + int res; + + exception = 0; + res = 0; + + array = JSValueToObject(context, o, &exception); + if(exception != 0) + return SWIG_TypeError; + + js_re = JSObjectGetPropertyAtIndex(context, array, 0, &exception); + if(exception != 0) + return SWIG_TypeError; + + js_im = JSObjectGetPropertyAtIndex(context, array, 1, &exception); + if(exception != 0) + return SWIG_TypeError; + + res = SWIG_AsVal(double)(js_re, &re); + if(!SWIG_IsOK(res)) { + return SWIG_TypeError; + } + + res = SWIG_AsVal(double)(js_im, &im); + if(!SWIG_IsOK(res)) { + return SWIG_TypeError; + } + + 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 { + float re; + int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re)); + if (SWIG_IsOK(res)) { + if (val) *val = Constructor(re, 0.0); + return res; + } + } + 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) Added: branches/gsoc2012-javascript/Lib/javascript/jsc/std_complex.i =================================================================== --- branches/gsoc2012-javascript/Lib/javascript/jsc/std_complex.i (rev 0) +++ branches/gsoc2012-javascript/Lib/javascript/jsc/std_complex.i 2012-08-20 18:34:55 UTC (rev 13693) @@ -0,0 +1,19 @@ +/* + * STD C++ complex typemaps + */ + +%include <javascriptcomplex.swg> + +%{ +#include <complex> +%} + +/* defining the complex as/from converters */ + +%swig_cplxdbl_convn(std::complex<double>, std::complex<double>, std::real, std::imag) +%swig_cplxflt_convn(std::complex<float>, std::complex<float>, std::real, std::imag) + +/* defining the typemaps */ + +%typemaps_primitive(%checkcode(CPLXDBL), std::complex<double>); +%typemaps_primitive(%checkcode(CPLXFLT), std::complex<float>); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |