Type conversion - from C to JavaScript

There are basically three contexts for type conversion from C to JavaScript:

  1. Both the C type and desired JavaScript type are known. This is the case when values are passed by pointer to a C function, changed by the C function, and converted back to JavaScript.
  2. The C type is known, but the desired JavaScript type is undefined. This is the case for function return values, reading struct members, reading global variables or converting arguments to callback functions
  3. The desired JavaScript type is known, but the C type is undefined. This is the case for conversion of Pointers.


Desired JavaScript type
Undefined
null
Pointer
struct_N
Cfunction
String
Boolean
Int
Number
Array
C type
Undefined

Pointer 4
Pointer 4
struct_N 4
Cfunction 4
String
Boolean
Int
Number
Array 5
char
Int




String Boolean Int Number
short
Int




String Boolean Int Number
bool 1
Boolean





Boolean Int Number
int
Int





Boolean Int Number
float/double
Number





Boolean Int Number
valist
Pointer 4








Array 5
struct N struct_N 6 struct_N 6 struct_N 6 struct_N 6
any [n] 2
Array 5
Array 5 Array 5 Array 5 Array 5 Array 5 Array 5 Array 5 Array 5 Array 5
char *
Pointer 4 Pointer 4 Pointer 4

String



Array 5
short *
Pointer 4 Pointer 4 Pointer 4

String



Array 5
bool *
Pointer 4 Pointer 4 Pointer 4





Array 5
int *
Pointer 4 Pointer 4 Pointer 4





Array 5
float/double *
Pointer 4 Pointer 4 Pointer 4





Array 5
valist *
Pointer 4 Pointer 4 Pointer 4





Array 5
struct N *
struct_N 6
struct_N 6 struct_N 6 struct_N 4





void *
Pointer 4 Pointer 4 Pointer 4 struct_N 4
Cfunction 4
Pointer 4




function *
Cfunction 4
Cfunction 4
Cfunction 4

Cfunction 4





any ** 3
Pointer 4
Pointer 4
Pointer 4






Array 5

1 char, short or int types which have been typedeffed into a type whose name ends in "bool", "Bool", "BOOL", "boolean", "Boolean" or "BOOLEAN".
2 The element type can be any of the C types
3 The dereferenced type can be any of the C types
4 If the C pointer is NULL, the returned JS value will be null
5 Each element is converted according to the rules that apply to its type. If the array pointer has changed by the C function, its contents are not dereferenced. Instead, the new pointer is returned as a Pointer.
6 If no prototype named struct_N exists, (where N is the name of the C struct) a Pointer is returned instead. If the C pointer is NULL, the returned JS value will be null.