jswrap
' Program
The jswrap
program is a tool that helps implementing C functions
in JavaScript.
jswrap
Program
The jswrap
program is invoked as:
jswrap
options... file
The jswrap
program processes the command line options
and according to them and the default values, it converts the input file
file to the corresponding `.h' and `.c' files. The
options can be one of more of the following command line options:
-g
--debug
-h file
--header file
-n
--no-error-handler
-o
--output file
-r
--reentrant
JSInterpPtr
'
argument to all C functions it generates.
-V
--version
jswrap
program.
--help
jswrap
program.
The definition file contains the function definitions and their implementation in JavaScript. The function definitions are normal JavaScript function definitions but they are extended with the type information. The type information is used to generate the C header files and the glue code that is used in the function call. The definition file can also contain normal JavaScript comments. The comments are ignored and they are not copied to the generated C header and implementation files.
The syntax of the function definition is:
function [return_type] function_name(
argument_type... argument[, ...])
{ JavaScript code implementing the function. }
Where:
void
function
returning no value.
^[A-Za-z_][A-Za-z_0-9]*
'.
The type specifiers specify the native C and JavaScript type that is used for the argument or for the return value. The following type are supported:
cstring
'\0'
terminated C-string. In the C, this is presented as
`char *
'. In the JavaScript, this is a normal string.
double
double
' floating
point number.
int
long
' integer.
string
unsigned
char *
, unsigned int
' pair. In the JavaScript, this is a normal
string. Note! Because the type's presentation in C is two
types, this value can't be used as a return value of a function.
The following example shows how the types are converted to the corresponding C header file. The input file `types.jsw' is as follows:
function types (cstring vcstring, double vdouble, int vint, string vstring) { }
The resulting C header file `types.h' contains the following
definitions for the function types
:
void types ( char *vcstring, double vdouble, long vint, unsigned char *vstring, unsigned int vstring_len );
The passing type specifiers specify how the argument is passed to the function. The following specifies are supported:
in
out
undefined
. When the control returns from the
implementation of the function, the argument's current value is returned
to the calling C function.
In the C, the output and input-output arguments are presented as
pointers to the variables, containing the values.
inout
The following example shows show the output and input-output arguments are presented in the C header file. The input file `pass.jsw' is as follows:
function pass (out cstring vcstring, out double vdouble, inout int vint, inout string vstring) { }
The resulting C header file `pass.h' contains the following
definitions for the function pass
:
void pass ( char **vcstring, double *vdouble, long *vint, unsigned char **vstring, unsigned int *vstring_len );
The life scope specifiers specify the liveness of the value, passed in an argument. The following specifiers are supported:
static
cstring
and
string
types.
The specifier don't have any affect for the generated C header file.
function hello (cstring user) { System.stdout.writeln ("Hello, " + user + "!"); } function int max_sum (int a, int b, int out sum) { sum = a + b; return a > b ? a : b; }
$ jswrap sample.jsw
/* This file is automatically generated from `hello.jsw' by jswrap. */ #ifndef HELLO_H #define HELLO_H void hello ( char *user ); int max_sum ( int a, int b, int *sum ); #endif /* not HELLO_H */
#include <js.h> #include "hello.h" JSInterpPtr jswrap_interp; int main (int argc, char *argv[]) { int a, b, max, sum; jswrap_interp = js_create_interp (NULL); hello ("World"); a = 5; b = 7; max = max_sum (a, b, &sum); printf ("%d + %d = %d, max(%d, %d) = %d\n", a, b, sum, a, b, max); return 0; }
Go to the first, previous, next, last section, table of contents.