Skip to content

Interacting with code

kripken edited this page Dec 18, 2011 · 24 revisions

Interacting with Compiled Code

You can interact in various ways with the compiled code:

  • Functions in the original source become JS functions, so you can call them. C++ functions will be name-mangled, though - C functions are much more convenient. If you want to call functions, consider defining them with extern "C" { .. } in your C++ code.
  • The types of the parameters you pass to functions need to make sense. Integers and floating point values can be passed as is. Aside from those, there are pointers, which are simply integers in the generated code.
  • Strings in JavaScript must be converted to pointers for compiled code, the relevant functions are Pointer_stringify which given a pointer returns a JavaScript string, and the other direction can be accomplished by allocate(intArrayFromString(someString), 'i8', ALLOC_STACK) which will convert a JavaScript string someString to a pointer. Note that conversion to a pointer allocates memory (that's the call to allocate there).
  • You can access memory using getValue(ptr, type) and setValue(ptr, value, type). The first argument is a pointer, a number representing a memory address. type must be an LLVM IR type, one of i8,i16,i32,i64,float,double or a pointer type like i8*.
  • There are various other convenience functions, see preamble.js (that file will be included with the generated code).
  • The bindings generator can generate convenient JS classes from C++ headers. See the bindings test in the test runner, and the ammo.js project (the main consumer of that tool)
  • For filesystem-related manners, see the Filesystem Guide.
Clone this wiki locally