Eric Mandel
2017-09-06 19:33:01 UTC
In v1.37.20, I don't seem to be able to use Module.ccall to invoke a
varargs routine, even if I specify the exact number of arguments for the
given invocation. Should this work?
Consider a cvals() routine that just prints out the double precision
varargs until the stop marker is found (or until we have gone too far):
int nmax = 4;
int cvals(double a, ...){
int i=0;
double b;
va_list args;
// declared value
va_start(args, a);
fprintf(stdout, "in cvals: [declared: %f]\n", a);
while( 1 ){
// get next double precision value
b = va_arg(args, double);
// stop if we reached the end marker or go beyond max
if( b < 0 ){
fprintf(stdout, " found end marker\n");
break;
} else if( i > nmax ){
fprintf(stdout, " went past max args (BAD): %d\n", nmax);
break;
} else {
fprintf(stdout, " vararg %d: %f\n", i, b);
i++;
}
}
va_end(args);
return i;
}
The expected result when calling this directly in C, e.g.:
cval(100.0, 1.01, 2.02, 3.03, -1.0)
is:
in cvals: [declared: 100.000000]
vararg 0: 1.010000
vararg 1: 2.020000
vararg 2: 3.030000
found end marker
Using Module.ccall with a specific number of args gives a bogus result:
Module.ccall("cvals", "null", ["number", "number", "number", "number",
"number"], [100.0, 1.01, 2.02, 3.03, -1.0])
in cvals: [declared: 100.000000]
vararg 0: 0.000000
vararg 1: 0.000000
vararg 2: 0.000000
vararg 3: 0.000000
vararg 4: 0.000000
went past max args (BAD): 4
I see from a previous post (Binding varargs function, 9/15/13) that cwrap
does not (yet) support varargs. But should ccall with a specific number of
args work? If not, are there any suggested work-arounds, short of re-coding
the C varargs routine?
Thanks!
Eric
varargs routine, even if I specify the exact number of arguments for the
given invocation. Should this work?
Consider a cvals() routine that just prints out the double precision
varargs until the stop marker is found (or until we have gone too far):
int nmax = 4;
int cvals(double a, ...){
int i=0;
double b;
va_list args;
// declared value
va_start(args, a);
fprintf(stdout, "in cvals: [declared: %f]\n", a);
while( 1 ){
// get next double precision value
b = va_arg(args, double);
// stop if we reached the end marker or go beyond max
if( b < 0 ){
fprintf(stdout, " found end marker\n");
break;
} else if( i > nmax ){
fprintf(stdout, " went past max args (BAD): %d\n", nmax);
break;
} else {
fprintf(stdout, " vararg %d: %f\n", i, b);
i++;
}
}
va_end(args);
return i;
}
The expected result when calling this directly in C, e.g.:
cval(100.0, 1.01, 2.02, 3.03, -1.0)
is:
in cvals: [declared: 100.000000]
vararg 0: 1.010000
vararg 1: 2.020000
vararg 2: 3.030000
found end marker
Using Module.ccall with a specific number of args gives a bogus result:
Module.ccall("cvals", "null", ["number", "number", "number", "number",
"number"], [100.0, 1.01, 2.02, 3.03, -1.0])
in cvals: [declared: 100.000000]
vararg 0: 0.000000
vararg 1: 0.000000
vararg 2: 0.000000
vararg 3: 0.000000
vararg 4: 0.000000
went past max args (BAD): 4
I see from a previous post (Binding varargs function, 9/15/13) that cwrap
does not (yet) support varargs. But should ccall with a specific number of
args work? If not, are there any suggested work-arounds, short of re-coding
the C varargs routine?
Thanks!
Eric
--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.