aboutsummaryrefslogtreecommitdiff
path: root/src/include/emscripten.h
blob: 99cd40e37218cf2d98095a86a8781f7baeae2f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * This file contains a few useful things for compiling C/C++ code
 * with Emscripten, an LLVM-to-JavaScript compiler.
 *
 * The code can be used permissively under the MIT license.
 *
 * http://emscripten.org
 */

// ES_SIZEOF
//
// A 'safe' sizeof operator. Sadly llvm-gcc calculates sizeof's
// and stores them hardcoded as raw values, unlike say offsets
// within a structure which it nicely details using getelementptr.
//
// You should always use ES_SIZEOF instead of sizeof when using
// Emscripten.
//
// Note that this only works with types, not values.
//
// Sadly
//        #define ES_SIZEOF(x) int(&((x*)(NULL))[1])
// does not work, since the compiler parses and hard-codes the
// value. So we need to trick it with a function call.
#ifdef EMSCRIPTEN
  template<class T>
  int es_sizeof(T* x) { return int(&x[1]); }
  #define ES_SIZEOF(T) es_sizeof((T*)NULL)
#else
  #define ES_SIZEOF(T) sizeof(T)
#endif