diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2021-03-06 11:08:58 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2021-03-06 11:08:58 +0000 |
commit | 513ea736d3155989886b92e8a4bb1ee24e40a223 (patch) | |
tree | e00c9bc304aafdfe7acffd5251c84e7fccc6a053 /Lib/javascript | |
parent | d007f35e05ee27ad1c454fe351bd9fc73df24065 (diff) | |
parent | 0b2d5ffa6ed4a7058518177cd3b3d7aa2ff6e357 (diff) | |
download | swig-513ea736d3155989886b92e8a4bb1ee24e40a223.tar.gz |
Merge branch 'array_fix'
* array_fix:
add javascript_arays.i fix to changes file
Convert javascript_arrays.i example into testcase
Update .gitignore for java doxygen output
Javascript code formatting corrections
Fix Javascript arrays for modern node versions
Fix for new SWIGV8_ARRAY_NEW definition
Proper array typemaps in Javascript
Diffstat (limited to 'Lib/javascript')
-rw-r--r-- | Lib/javascript/jsc/arrays_javascript.i | 87 | ||||
-rw-r--r-- | Lib/javascript/v8/arrays_javascript.i | 109 | ||||
-rw-r--r-- | Lib/javascript/v8/javascriptcomplex.swg | 18 | ||||
-rw-r--r-- | Lib/javascript/v8/javascriptrun.swg | 8 |
4 files changed, 77 insertions, 145 deletions
diff --git a/Lib/javascript/jsc/arrays_javascript.i b/Lib/javascript/jsc/arrays_javascript.i index b9199d86b..713b7ef23 100644 --- a/Lib/javascript/jsc/arrays_javascript.i +++ b/Lib/javascript/jsc/arrays_javascript.i @@ -21,34 +21,39 @@ * fs = example.FiddleSticks; * ----------------------------------------------------------------------------- */ -%fragment("SWIG_JSCGetIntProperty", "header", fragment=SWIG_AsVal_frag(int)) {} + +%fragment("SWIG_JSCGetIntProperty", "header", fragment=SWIG_AsVal_frag(int)) {} %fragment("SWIG_JSCGetNumberProperty", "header", fragment=SWIG_AsVal_frag(double)) {} +%fragment("SWIG_JSCOutInt", "header", fragment=SWIG_From_frag(int)) {} +%fragment("SWIG_JSCOutNumber", "header", fragment=SWIG_From_frag(double)) {} + +%define JAVASCRIPT_ARRAYS_IN_DECL(NAME, CTYPE, ANY, ANYLENGTH) -%typemap(in, fragment="SWIG_JSCGetIntProperty") int[], int[ANY] - (int length = 0, JSObjectRef array, JSValueRef jsvalue, int i = 0, int res = 0, $*1_ltype temp) { +%typemap(in, fragment=NAME) CTYPE[ANY] { if (JSValueIsObject(context, $input)) { + int i; // Convert into Array - array = JSValueToObject(context, $input, NULL); + JSObjectRef array = JSValueToObject(context, $input, NULL); - length = $1_dim0; + int length = ANYLENGTH; $1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length); // Get each element from array for (i = 0; i < length; i++) { - jsvalue = JSObjectGetPropertyAtIndex(context, array, i, NULL); + JSValueRef jsvalue = JSObjectGetPropertyAtIndex(context, array, i, NULL); + $*1_ltype temp; // Get primitive value from JSObject - res = SWIG_AsVal(int)(jsvalue, &temp); + int res = SWIG_AsVal(CTYPE)(jsvalue, &temp); if (!SWIG_IsOK(res)) { SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double"); } arg$argnum[i] = temp; } - } else { @@ -56,68 +61,34 @@ } } -%typemap(freearg) int[], int[ANY] { +%typemap(freearg) CTYPE[ANY] { free($1); } -%typemap(out, fragment=SWIG_From_frag(int)) int[], int[ANY] (int length = 0, int i = 0) -{ - length = $1_dim0; +%enddef + +%define JAVASCRIPT_ARRAYS_OUT_DECL(NAME, CTYPE) + +%typemap(out, fragment=NAME) CTYPE[ANY] { + int length = $1_dim0; JSValueRef values[length]; + int i; for (i = 0; i < length; i++) { - values[i] = SWIG_From(int)($1[i]); + values[i] = SWIG_From(CTYPE)($1[i]); } $result = JSObjectMakeArray(context, length, values, NULL); } -%typemap(in, fragment="SWIG_JSCGetNumberProperty") double[], double[ANY] - (int length = 0, JSObjectRef array, JSValueRef jsvalue, int i = 0, int res = 0, $*1_ltype temp) { - if (JSValueIsObject(context, $input)) - { - // Convert into Array - array = JSValueToObject(context, $input, NULL); +%enddef - length = $1_dim0; - - $1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length); +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetIntProperty", int, , SWIGJSC_ArrayLength(context, array)) +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetIntProperty", int, ANY, $1_dim0) +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetNumberProperty", double, , SWIGJSC_ArrayLength(context, array)) +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetNumberProperty", double, ANY, $1_dim0) - // Get each element from array - for (i = 0; i < length; i++) - { - jsvalue = JSObjectGetPropertyAtIndex(context, array, i, NULL); +JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutInt", int) +JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutNumber", double) - // Get primitive value from JSObject - res = SWIG_AsVal(double)(jsvalue, &temp); - if (!SWIG_IsOK(res)) - { - SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double"); - } - arg$argnum[i] = temp; - } - - } - else - { - SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef"); - } -} - -%typemap(freearg) double[], double[ANY] { - free($1); -} - -%typemap(out, fragment=SWIG_From_frag(double)) double[], double[ANY] (int length = 0, int i = 0) -{ - length = $1_dim0; - JSValueRef values[length]; - - for (i = 0; i < length; i++) - { - values[i] = SWIG_From(double)($1[i]); - } - - $result = JSObjectMakeArray(context, length, values, NULL); -} diff --git a/Lib/javascript/v8/arrays_javascript.i b/Lib/javascript/v8/arrays_javascript.i index 22b50be8f..6dc7e4b9b 100644 --- a/Lib/javascript/v8/arrays_javascript.i +++ b/Lib/javascript/v8/arrays_javascript.i @@ -21,105 +21,66 @@ * fs = example.FiddleSticks; * ----------------------------------------------------------------------------- */ -%fragment("SWIG_JSCGetIntProperty", "header", fragment=SWIG_AsVal_frag(int)) {} + +%fragment("SWIG_JSCGetIntProperty", "header", fragment=SWIG_AsVal_frag(int)) {} %fragment("SWIG_JSCGetNumberProperty", "header", fragment=SWIG_AsVal_frag(double)) {} +%fragment("SWIG_JSCOutInt", "header", fragment=SWIG_From_frag(int)) {} +%fragment("SWIG_JSCOutNumber", "header", fragment=SWIG_From_frag(double)) {} + +%define JAVASCRIPT_ARRAYS_IN_DECL(NAME, CTYPE, ANY, ANYLENGTH) -%typemap(in, fragment="SWIG_JSCGetIntProperty") int[], int[ANY] - (int length = 0, v8::Local<v8::Array> array, v8::Local<v8::Value> jsvalue, int i = 0, int res = 0, $*1_ltype temp) { - if ($input->IsArray()) - { +%typemap(in, fragment=NAME) CTYPE[ANY] { + if ($input->IsArray()) { // Convert into Array - array = v8::Local<v8::Array>::Cast($input); + v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast($input); - length = $1_dim0; + int length = ANYLENGTH; - $1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length); + $1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length); // Get each element from array - for (i = 0; i < length; i++) - { - jsvalue = array->Get(i); + for (int i = 0; i < length; i++) { + v8::Local<v8::Value> jsvalue = SWIGV8_ARRAY_GET(array, i); + $*1_ltype temp; // Get primitive value from JSObject - res = SWIG_AsVal(int)(jsvalue, &temp); - if (!SWIG_IsOK(res)) - { + int res = SWIG_AsVal(CTYPE)(jsvalue, &temp); + if (!SWIG_IsOK(res)) { SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double"); } arg$argnum[i] = temp; } - - } - else - { - SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef"); + } else { + SWIG_exception_fail(SWIG_ERROR, "$input is not an array"); } } -%typemap(freearg) int[], int[ANY] { - free($1); +%typemap(freearg) CTYPE[ANY] { + free($1); } -%typemap(out, fragment=SWIG_From_frag(int)) int[], int[ANY] (int length = 0, int i = 0) -{ - length = $1_dim0; - v8::Local<v8::Array> array = v8::Array::New(length); - - for (i = 0; i < length; i++) - { - array->Set(i, SWIG_From(int)($1[i])); - } - - - $result = array; -} - -%typemap(in, fragment="SWIG_JSCGetNumberProperty") double[], double[ANY] - (int length = 0, v8::Local<v8::Array> array, v8::Local<v8::Value> jsvalue, int i = 0, int res = 0, $*1_ltype temp) { - if ($input->IsArray()) - { - // Convert into Array - array = v8::Local<v8::Array>::Cast($input); - - length = $1_dim0; +%enddef - $1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length); +%define JAVASCRIPT_ARRAYS_OUT_DECL(NAME, CTYPE) - // Get each element from array - for (i = 0; i < length; i++) - { - jsvalue = array->Get(i); - - // Get primitive value from JSObject - res = SWIG_AsVal(double)(jsvalue, &temp); - if (!SWIG_IsOK(res)) - { - SWIG_exception_fail(SWIG_ERROR, "Failed to convert $input to double"); - } - arg$argnum[i] = temp; - } +%typemap(out, fragment=NAME) CTYPE[ANY] { + int length = $1_dim0; + v8::Local<v8::Array> array = SWIGV8_ARRAY_NEW(length); + for (int i = 0; i < length; i++) { + SWIGV8_ARRAY_SET(array, i, SWIG_From(CTYPE)($1[i])); } - else - { - SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef"); - } -} -%typemap(freearg) double[], double[ANY] { - free($1); + $result = array; } -%typemap(out, fragment=SWIG_From_frag(double)) double[], double[ANY] (int length = 0, int i = 0) -{ - length = $1_dim0; - v8::Local<v8::Array> array = v8::Array::New(length); +%enddef - for (i = 0; i < length; i++) - { - array->Set(i, SWIG_From(double)($1[i])); - } +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetIntProperty", int, , array->Length()) +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetIntProperty", int, ANY, $1_dim0) +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetNumberProperty", double, , array->Length()) +JAVASCRIPT_ARRAYS_IN_DECL("SWIG_JSCGetNumberProperty", double, ANY, $1_dim0) +JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutInt", int) +JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutNumber", double) - $result = array; -} diff --git a/Lib/javascript/v8/javascriptcomplex.swg b/Lib/javascript/v8/javascriptcomplex.swg index b73d5e2dd..7b3c5547e 100644 --- a/Lib/javascript/v8/javascriptcomplex.swg +++ b/Lib/javascript/v8/javascriptcomplex.swg @@ -17,7 +17,7 @@ SWIG_From_dec(Type)(%ifcplusplus(const Type&, Type) c) { SWIGV8_HANDLESCOPE_ESC(); - v8::Local<v8::Array> vals = SWIGV8_ARRAY_NEW(); + v8::Local<v8::Array> vals = SWIGV8_ARRAY_NEW(0); SWIGV8_ARRAY_SET(vals, 0, SWIG_From(double)(Real(c))); SWIGV8_ARRAY_SET(vals, 1, SWIG_From(double)(Imag(c))); @@ -39,23 +39,23 @@ SWIG_AsVal_dec(Type) (SWIGV8_VALUE o, Type* val) if (o->IsArray()) { SWIGV8_ARRAY array = SWIGV8_ARRAY::Cast(o); - if(array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2]."); + if (array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2]."); double re, im; int res; res = SWIG_AsVal(double)(SWIGV8_ARRAY_GET(array, 0), &re); - if(!SWIG_IsOK(res)) { + if (!SWIG_IsOK(res)) { return SWIG_TypeError; } res = SWIG_AsVal(double)(SWIGV8_ARRAY_GET(array, 1), &im); - if(!SWIG_IsOK(res)) { + if (!SWIG_IsOK(res)) { return SWIG_TypeError; } if (val) *val = Constructor(re, im); return SWIG_OK; - } else if(o->IsNumber()){ + } else if (o->IsNumber()) { double d; int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d)); if (SWIG_IsOK(res)) { @@ -81,17 +81,17 @@ SWIG_AsVal_dec(Type) (SWIGV8_VALUE o, Type* val) if (o->IsArray()) { SWIGV8_ARRAY array = SWIGV8_ARRAY::Cast(o); - if(array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2]."); + if (array->Length() != 2) SWIG_Error(SWIG_TypeError, "Illegal argument for complex: must be array[2]."); double re, im; int res; res = SWIG_AsVal(double)(SWIGV8_ARRAY_GET(array, 0), &re); - if(!SWIG_IsOK(res)) { + if (!SWIG_IsOK(res)) { return SWIG_TypeError; } res = SWIG_AsVal(double)(SWIGV8_ARRAY_GET(array, 1), &im); - if(!SWIG_IsOK(res)) { + if (!SWIG_IsOK(res)) { return SWIG_TypeError; } @@ -102,7 +102,7 @@ SWIG_AsVal_dec(Type) (SWIGV8_VALUE o, Type* val) } else { return SWIG_OverflowError; } - } else if(o->IsNumber()){ + } else if (o->IsNumber()) { float re; int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re)); if (SWIG_IsOK(res)) { diff --git a/Lib/javascript/v8/javascriptrun.swg b/Lib/javascript/v8/javascriptrun.swg index bab259c03..642908a37 100644 --- a/Lib/javascript/v8/javascriptrun.swg +++ b/Lib/javascript/v8/javascriptrun.swg @@ -71,7 +71,7 @@ typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo; #endif #if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x032318) -#define SWIGV8_ARRAY_NEW() v8::Array::New() +#define SWIGV8_ARRAY_NEW(size) v8::Array::New(size) #define SWIGV8_BOOLEAN_NEW(bool) v8::Boolean::New(bool) #define SWIGV8_EXTERNAL_NEW(val) v8::External::New(val) #define SWIGV8_FUNCTEMPLATE_NEW(func) v8::FunctionTemplate::New(func) @@ -91,7 +91,7 @@ typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo; #define SWIGV8_ARRAY_GET(array, index) (array)->Get(index) #define SWIGV8_ARRAY_SET(array, index, value) (array)->Set(index, value) #else -#define SWIGV8_ARRAY_NEW() v8::Array::New(v8::Isolate::GetCurrent()) +#define SWIGV8_ARRAY_NEW(size) v8::Array::New(v8::Isolate::GetCurrent(), size) #define SWIGV8_BOOLEAN_NEW(bool) v8::Boolean::New(v8::Isolate::GetCurrent(), bool) #define SWIGV8_EXTERNAL_NEW(val) v8::External::New(v8::Isolate::GetCurrent(), val) #define SWIGV8_FUNCTEMPLATE_NEW(func) v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), func) @@ -703,9 +703,9 @@ SWIGV8_VALUE SWIGV8_AppendOutput(SWIGV8_VALUE result, SWIGV8_VALUE obj) { SWIGV8_HANDLESCOPE_ESC(); if (result->IsUndefined()) { - result = SWIGV8_ARRAY_NEW(); + result = SWIGV8_ARRAY_NEW(0); } else if (!result->IsArray()) { - SWIGV8_ARRAY tmparr = SWIGV8_ARRAY_NEW(); + SWIGV8_ARRAY tmparr = SWIGV8_ARRAY_NEW(0); SWIGV8_ARRAY_SET(tmparr, 0, result); result = tmparr; } |