From 4b64becbbb6fa86afe3aa00ce89ac26dafad45ab Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 1 Mar 2021 14:20:18 +0000 Subject: [PATCH] OUTPUT typemaps on methods that don't return void SWIGJSC_ValueIsArray could be implemented by JSValueIsArray in later versions of Javascript webkit, similar fix to previous commits for v8. Enhance testing of OUTPUT typemaps to test more than one output. --- .../javascript/li_typemaps_runme.js | 3 ++- Examples/test-suite/li_typemaps.i | 6 ++++- Examples/test-suite/lua/li_typemaps_runme.lua | 4 +-- .../test-suite/perl5/li_typemaps_runme.pl | 5 ++-- Lib/javascript/jsc/javascriptrun.swg | 26 +++++++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Examples/test-suite/javascript/li_typemaps_runme.js b/Examples/test-suite/javascript/li_typemaps_runme.js index 987606030..c0b1b43b0 100644 --- a/Examples/test-suite/javascript/li_typemaps_runme.js +++ b/Examples/test-suite/javascript/li_typemaps_runme.js @@ -10,7 +10,7 @@ function check_array(a, b) { if (a.length != b.length) throw new Error("Array length mismatch " + a.length + " " + b.length) if (!a.every(function(element, index) { return element === b[index]; })) - throw new Error("Arrays don't match a: " + a + " b:" + b) + throw new Error("Arrays don't match a:" + a + " b:" + b) } // Check double INPUT typemaps @@ -47,3 +47,4 @@ check_array(li_typemaps.inoutr_int2(1,2), [1, 2]) fi = li_typemaps.out_foo(10) check(fi[0].a, 10) check(fi[1], 20) +check(fi[2], 30) diff --git a/Examples/test-suite/li_typemaps.i b/Examples/test-suite/li_typemaps.i index a53c1c74a..d508c1c84 100644 --- a/Examples/test-suite/li_typemaps.i +++ b/Examples/test-suite/li_typemaps.i @@ -2,6 +2,7 @@ %include "typemaps.i" +%apply int *OUTPUT { int *OUTPUT2 }; %apply int &INOUT { int &INOUT2 }; %newobject out_foo; %inline %{ @@ -51,10 +52,13 @@ void out_longlong(long long x, long long *OUTPUT) { *OUTPUT = x; } void out_ulonglong(unsigned long long x, unsigned long long *OUTPUT) { *OUTPUT = x; } /* Tests a returning a wrapped pointer and an output argument */ -struct Foo *out_foo(int a, int *OUTPUT) { +struct Foo *out_foo(int a, int *OUTPUT, int *OUTPUT2) { struct Foo *f = new struct Foo(); f->a = a; *OUTPUT = a * 2; + struct Foo *f2 = new struct Foo(); + f2->a = a; + *OUTPUT2 = a * 3; return f; } diff --git a/Examples/test-suite/lua/li_typemaps_runme.lua b/Examples/test-suite/lua/li_typemaps_runme.lua index 7456d8245..342634a5b 100644 --- a/Examples/test-suite/lua/li_typemaps_runme.lua +++ b/Examples/test-suite/lua/li_typemaps_runme.lua @@ -38,5 +38,5 @@ assert(li_typemaps.inoutr_bool(false)==false) a,b=li_typemaps.inoutr_int2(1,2) assert(a==1 and b==2) -f,i=li_typemaps.out_foo(10) -assert(f.a==10 and i==20) +f,i,i2=li_typemaps.out_foo(10) +assert(f.a==10 and i==20 and i2==30) diff --git a/Examples/test-suite/perl5/li_typemaps_runme.pl b/Examples/test-suite/perl5/li_typemaps_runme.pl index a573b89a0..2755862a2 100644 --- a/Examples/test-suite/perl5/li_typemaps_runme.pl +++ b/Examples/test-suite/perl5/li_typemaps_runme.pl @@ -1,7 +1,7 @@ #!/usr/bin/perl use strict; use warnings; -use Test::More tests => 415; +use Test::More tests => 416; BEGIN { use_ok('li_typemaps') } require_ok('li_typemaps'); @@ -75,10 +75,11 @@ SKIP: { batch('ulonglong', $c); } -my($foo, $int) = li_typemaps::out_foo(10); +my($foo, $int, $int2) = li_typemaps::out_foo(10); isa_ok($foo, 'li_typemaps::Foo'); is($foo->{a}, 10); is($int, 20); +is($int2, 30); my($a, $b) = li_typemaps::inoutr_int2(13, 31); is($a, 13); diff --git a/Lib/javascript/jsc/javascriptrun.swg b/Lib/javascript/jsc/javascriptrun.swg index 4a8fc5be5..26c440244 100644 --- a/Lib/javascript/jsc/javascriptrun.swg +++ b/Lib/javascript/jsc/javascriptrun.swg @@ -317,6 +317,30 @@ unsigned int SWIGJSC_ArrayLength(JSContextRef context, JSObjectRef arr) { } } +SWIGRUNTIME +bool SWIGJSC_ValueIsArray(JSContextRef context, JSValueRef value) { + if (JSValueIsObject(context, value)) { + static JSStringRef ArrayString = NULL; + static JSStringRef isArrayString = NULL; + JSObjectRef array = NULL; + JSObjectRef isArray = NULL; + JSValueRef retval = NULL; + + if (!ArrayString) + ArrayString = JSStringCreateWithUTF8CString("Array"); + if (!isArrayString) + isArrayString = JSStringCreateWithUTF8CString("isArray"); + + array = (JSObjectRef)JSObjectGetProperty(context, JSContextGetGlobalObject(context), ArrayString, NULL); + isArray = (JSObjectRef)JSObjectGetProperty(context, array, isArrayString, NULL); + retval = JSObjectCallAsFunction(context, isArray, NULL, 1, &value, NULL); + + if (JSValueIsBoolean(context, retval)) + return JSValueToBoolean(context, retval); + } + return false; +} + SWIGRUNTIME JSValueRef SWIGJSC_AppendOutput(JSContextRef context, JSValueRef value, JSValueRef obj) { JSObjectRef arr; @@ -324,6 +348,8 @@ JSValueRef SWIGJSC_AppendOutput(JSContextRef context, JSValueRef value, JSValueR if (JSValueIsUndefined(context, value)) { arr = JSObjectMakeArray(context, 0, 0, 0); + } else if (!SWIGJSC_ValueIsArray(context, value)) { + arr = JSObjectMakeArray(context, 1, &value, 0); } else { arr = JSValueToObject(context, value, 0); }