Add complex support to v8 module.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/oliverb-javascript-v8@13825 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Oliver Buchtala 2012-09-08 01:15:29 +00:00
commit 8b7a9fec25

View file

@ -12,13 +12,15 @@
%fragment(SWIG_From_frag(Type),"header", %fragment(SWIG_From_frag(Type),"header",
fragment=SWIG_From_frag(double)) fragment=SWIG_From_frag(double))
{ {
SWIGINTERNINLINE JSObjectRef SWIGINTERNINLINE v8::Handle<v8::Value>
SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c) SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c)
{ {
JSValueRef vals[2]; v8::HandleScope scope;
vals[0] = SWIG_From(double)(Real(c)); v8::Local<v8::Array> vals = v8::Array::New(2);
vals[1] = SWIG_From(double)(Imag(c));
return JSObjectMakeArray(context, 2, vals, NULL); vals->Set(0, SWIG_From(double)(Real(c)));
vals->Set(1, SWIG_From(double)(Imag(c)));
return scope.Close(vals);
} }
} }
%enddef %enddef
@ -29,42 +31,30 @@ SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c)
fragment=SWIG_AsVal_frag(double)) fragment=SWIG_AsVal_frag(double))
{ {
SWIGINTERN int SWIGINTERN int
SWIG_AsVal_dec(Type) (JSValueRef o, Type* val) SWIG_AsVal_dec(Type) (v8::Handle<v8::Value> o, Type* val)
{ {
if (JSValueIsObject(context, o)) { v8::HandleScope scope;
JSObjectRef array;
JSValueRef exception, js_re, js_im; if (o->IsArray()) {
v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(o);
if(array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2].");
double re, im; double re, im;
int res; int res;
exception = 0; res = SWIG_AsVal(double)(array->Get(0), &re);
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)) { if(!SWIG_IsOK(res)) {
return SWIG_TypeError; return SWIG_TypeError;
} }
res = SWIG_AsVal(double)(js_im, &im); res = SWIG_AsVal(double)(array->Get(1), &im);
if(!SWIG_IsOK(res)) { if(!SWIG_IsOK(res)) {
return SWIG_TypeError; return SWIG_TypeError;
} }
if (val) *val = Constructor(re, im); if (val) *val = Constructor(re, im);
return SWIG_OK; return SWIG_OK;
} else { } else if(o->IsNumber()){
double d; double d;
int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d)); int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
if (SWIG_IsOK(res)) { if (SWIG_IsOK(res)) {
@ -83,35 +73,23 @@ SWIG_AsVal_dec(Type) (JSValueRef o, Type* val)
%fragment(SWIG_AsVal_frag(Type),"header", %fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsVal_frag(float)) { fragment=SWIG_AsVal_frag(float)) {
SWIGINTERN int SWIGINTERN int
SWIG_AsVal_dec(Type)(JSValueRef o, Type *val) SWIG_AsVal_dec(Type) (v8::Handle<v8::Value> o, Type* val)
{ {
if (JSValueIsObject(context, o)) { v8::HandleScope scope;
JSObjectRef array;
JSValueRef exception, js_re, js_im; if (o->IsArray()) {
v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(o);
if(array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2].");
double re, im; double re, im;
int res; int res;
exception = 0; res = SWIG_AsVal(double)(array->Get(0), &re);
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)) { if(!SWIG_IsOK(res)) {
return SWIG_TypeError; return SWIG_TypeError;
} }
res = SWIG_AsVal(double)(js_im, &im); res = SWIG_AsVal(double)(array->Get(1), &im);
if(!SWIG_IsOK(res)) { if(!SWIG_IsOK(res)) {
return SWIG_TypeError; return SWIG_TypeError;
} }
@ -123,7 +101,7 @@ SWIG_AsVal_dec(Type)(JSValueRef o, Type *val)
} else { } else {
return SWIG_OverflowError; return SWIG_OverflowError;
} }
} else { } else if(o->IsNumber()){
float re; float re;
int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re)); int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
if (SWIG_IsOK(res)) { if (SWIG_IsOK(res)) {
@ -134,7 +112,6 @@ SWIG_AsVal_dec(Type)(JSValueRef o, Type *val)
return SWIG_TypeError; return SWIG_TypeError;
} }
} }
%swig_fromcplx_conv(Type, Real, Imag); %swig_fromcplx_conv(Type, Real, Imag);
%enddef %enddef