aboutsummaryrefslogtreecommitdiff
path: root/src/include/emscripten.h
blob: 958bef980907ff792db0304a28cfe9cbb7270704 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * 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
//
// NOTE: As of now, ES_SIZEOF is not needed when using QUANTUM_SIZE
//       of 4. We will use the same offsets as C/C++ does in that case.
//       ES_SIZEOF is useful if QUANTUM_SIZE is 1.
//
// 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|V instead of sizeof when using
// Emscripten. Use ES_SIZEOF for types, ES_SIZEOV for values.
//
// Note that there is no way for Emscripten to know if you used
// ES_SIZEOF properly, or if you did and and you used sizeof.
// No warning will be shown if you do not use it.
//
// 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*)0)
  template<class T>
  int es_sizeov(T* x) { return es_sizeof((T*)0); }
  #define ES_SIZEOV(V) es_sizeof(V)

  // Undefine normal archs, so asm is not attempted
  #define __EMSCRIPTEN__
  #undef __i386__
  #undef __x86_64__
#else
  #define ES_SIZEOF(T) sizeof(T)
#endif

// Interface to the underlying JS engine. This function will
// eval() the given script.
extern void emscripten_run_script(const char *script);