Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a common 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

Also, a JavaScript file with the global: VARIABLE_X = 100;

These values will always need to be the same.

Instead of having to change them in both places (.h and .js), I need a common file where both can get this value, thus only needing to be changed in one place.

How can I create a common file between JavaScript and C/C++........ for a #define/constant ?

Thank You.

like image 328
T.T.T. Avatar asked Nov 29 '10 22:11

T.T.T.


3 Answers

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

Just stick a rule in your makefile that regenerates constants.h if constants.js has been changed.

like image 64
Anon. Avatar answered Oct 13 '22 20:10

Anon.


What build system are you using? Build system is a very good place for this type of stuff.

like image 30
Šimon Tóth Avatar answered Oct 13 '22 22:10

Šimon Tóth


This method uses a separate file for each data type, but allows you to include specially formatted javascript files in a C/C++ header.

You can #define macros in C/C++ to change identifiers as needed. For javascript the obvious candidate is var since it is only needed in javascript ... it can be redefined as necessary to any c type. Normally I wouldn't recommend using new Array() in javascript, but since C/C++ use {...} to initialize arrays while javascript uses [...], it is a necessary evil that allows you to wrap them.

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: On further thought you can do more than just keep your constants in sync. You can even write code that can run as both javascript and C using a little known difference between the two languages. In C you can use a '\' which continues on the next line, thus allowing us to use *\newline/ to end a comment in C (compile with -Wno-comment to ignore warnings in Clang), but since js doesn't follow the same rules, it will keep going as a comment until it finds an actual '*/' token.

/* 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
/**/
like image 24
technosaurus Avatar answered Oct 13 '22 20:10

technosaurus