summaryrefslogtreecommitdiff
path: root/Lib/javascript/jsc
diff options
context:
space:
mode:
authorNickolay Shmyrev <nshmyrev@gmail.com>2017-01-12 15:53:44 +0100
committerNickolay Shmyrev <nshmyrev@gmail.com>2017-01-14 21:10:48 +0100
commitcf7811b17890af6da7174e4e5a65ca38fc55c0e2 (patch)
treea12b0b9bcb590a0b1331985fada085162f0ecc84 /Lib/javascript/jsc
parenta699944c9561bbe0a16b7505d8432347b830decf (diff)
downloadswig-cf7811b17890af6da7174e4e5a65ca38fc55c0e2.tar.gz
Proper array typemaps in Javascript
https://github.com/swig/swig/issues/865
Diffstat (limited to 'Lib/javascript/jsc')
-rw-r--r--Lib/javascript/jsc/arrays_javascript.i87
1 files changed, 29 insertions, 58 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);
-}