How to create a shared file between JavaScript and C / C ++ for a constant?

I have a C ++ program with the following in the header file: #define VARIABLE_X 100

In addition, a global JavaScript file: VARIABLE_X = 100;

These values ​​should always be the same.

Instead of changing them in both places (.h and .js), I need a common file in which both can get this value, so it only needs to be changed in one place.

How to create a shared file between JavaScript and C / C ++ ........ for # define / constant?

Thank.

+5
source share
4 answers

You have the actual script -driven build process, right?

makefile, constants.h, constants.js .

+5

? - .

+3

, C/++ javascript.

#define C/++ . javascript var, javascript... c. new Array() javascript, C/++ {...} , javascript [...], , .

c-header.h

//constant integers
#define var const int
#include "int-consts.js"
#undef var

//constant strings
#define var const char*
#include "string-consts.js"
#undef var

//arrays of strings
#define var const char**
#define new (const char*[])
#define Array(...)  {__VA_ARGS__}
#include "string-arrays.js"
#undef new
#undef var

//and so forth for other types

int-consts.js

var swapMagicOffset = 4086;

string-consts.js

var swapMagic = "SWAP-SPACE";
var swapMagic2 = "SWAPSPACE2";

string-arrays.js

var cars = new Array("Ford", "Chevy", "Dodge");

Edit: , . , javascript, C, . C '\', , *\newline/ C ( -Wno-comment Clang), js , , * *.

/* C comment ends with the '/' on next line but js comment is open  *\
/ //BEGIN C Block
#define function int
/* This ends the original js comment, but we add an opening '/*' for C  */

/*Most compilers can build K&R style C with parameters like this:*/
function volume(x,y,z)/**\
/int x,y,z;/**/{return x*y*z;}

/**\
/
#undef function
/**/
+1

. : , Javascript Javascript.

Something simple:

 printf("VARIABLE_X = %d;\n", VARIABLE_X);

If this becomes a maintenance problem, you can write a simple function to generate both the .h file and the .js file from some common format.

0
source

All Articles