Proper array typemaps in Javascript

https://github.com/swig/swig/issues/865
This commit is contained in:
Nickolay Shmyrev 2017-01-12 15:53:44 +01:00
commit cf7811b178
9 changed files with 115 additions and 125 deletions

View file

@ -0,0 +1,3 @@
SRCS =
include $(SRCDIR)../example.mk

View file

@ -0,0 +1,9 @@
{
"targets": [
{
"target_name": "example",
"sources": [ "example_wrap.cxx" ],
"include_dirs": ["$srcdir"]
}
]
}

View file

@ -0,0 +1,31 @@
/* File : example.i */
%module example
%include <arrays_javascript.i>
%apply int[] {int *data1}
%apply int[3] {int data2[3]}
%apply int[4] {int data3[4]}
%inline %{
int sum1(int *data1, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += data1[i];
}
return sum;
}
int sum2(int data2[3]) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += data2[i];
}
return sum;
}
int data3[4] = {1, 2, 3, 4};
%}

View file

@ -0,0 +1 @@
module.exports = require("build/Release/example");

View file

@ -0,0 +1,7 @@
var example = require("example");
var arr = [1, 2, 3, 4, 5];
console.log(example.sum1(arr, arr.length));
console.log(example.sum2(arr));
console.log(example.data3)

View file

@ -1,3 +1,4 @@
array
class
constant
enum

View file

@ -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)) {}
%typemap(in, fragment="SWIG_JSCGetIntProperty") int[], int[ANY]
(int length = 0, JSObjectRef array, JSValueRef jsvalue, int i = 0, int res = 0, $*1_ltype temp) {
%define JAVASCRIPT_ARRAYS_IN_DECL(NAME, CTYPE, ANY, ANYLENGTH)
%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;
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)
$1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length);
JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutInt", int)
JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutNumber", double)
// Get each element from array
for (i = 0; i < length; i++)
{
jsvalue = JSObjectGetPropertyAtIndex(context, array, i, NULL);
// 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);
}

View file

@ -21,105 +21,72 @@
* 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)) {}
%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) {
%define JAVASCRIPT_ARRAYS_IN_DECL(NAME, CTYPE, ANY, ANYLENGTH)
%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);
// Get each element from array
for (i = 0; i < length; i++)
for (int i = 0; i < length; i++)
{
jsvalue = array->Get(i);
v8::Local<v8::Value> jsvalue = array->Get(i);
$*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
{
SWIG_exception_fail(SWIG_ERROR, "$input is not JSObjectRef");
SWIG_exception_fail(SWIG_ERROR, "$input is not an array");
}
}
%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;
v8::Local<v8::Array> array = v8::Array::New(length);
%enddef
for (i = 0; i < length; i++)
%define JAVASCRIPT_ARRAYS_OUT_DECL(NAME, CTYPE)
%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++)
{
array->Set(i, SWIG_From(int)($1[i]));
array->Set(i, SWIG_From(CTYPE)($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);
%enddef
length = $1_dim0;
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)
$1 = ($*1_ltype *)malloc(sizeof($*1_ltype) * length);
JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutInt", int)
JAVASCRIPT_ARRAYS_OUT_DECL("SWIG_JSCOutNumber", double)
// 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;
}
}
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;
v8::Local<v8::Array> array = v8::Array::New(length);
for (i = 0; i < length; i++)
{
array->Set(i, SWIG_From(double)($1[i]));
}
$result = array;
}

View file

@ -56,7 +56,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)
@ -69,7 +69,7 @@ typedef v8::PropertyCallbackInfo<v8::Value> SwigV8PropertyCallbackInfo;
#define SWIGV8_UNDEFINED() v8::Undefined()
#define SWIGV8_NULL() v8::Null()
#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)
@ -608,7 +608,7 @@ v8::Handle<v8::Value> SWIGV8_AppendOutput(v8::Local<v8::Value> result, v8::Handl
SWIGV8_HANDLESCOPE_ESC();
if (result->IsUndefined()) {
result = SWIGV8_ARRAY_NEW();
result = SWIGV8_ARRAY_NEW(0);
}
#if (V8_MAJOR_VERSION-0) < 4 && (SWIG_V8_VERSION < 0x031903)
v8::Handle<v8::Array> arr = v8::Handle<v8::Array>::Cast(result);