I have some c++ files with functions that I am trying to compile to wasm. For some reason, even though the files get compiled with exported functions to wasm by emscripten, I keep on getting 'TypeError: ___ is not a function' when I try to use the functions.
Here's the 'bridge.cpp' file that contains all of the functions I want to export:
/*
* Bridge from JS into the cellmap computation code in Wasm
*/
#include <gameoflife.h>
#include <constants.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
/**
* A struct that manages our instantiation of
* the game of life
*/
typedef struct {
cellmap *cm; /** holds the instance of the cellmap */
unsigned char *cells; /** holds the cell vals for current iteration */
} GOL_Instance;
/**
* Constructor for the game of life engine
*/
GOL_Instance *
GOL_Instance_new() {
GOL_Instance *gol;
gol = (GOL_Instance *)malloc(sizeof *gol);
if (gol) {
memset(gol, 0, sizeof *gol);
}
gol->cm = new_cellmap();
return gol;
}
/**
* Method to destroy the gol instance when the user is done
*/
void
GOL_Instance_destroy(GOL_Instance *gol) {
gol->cm->~cellmap();
delete[] gol->cells;
free(gol);
}
/**
* Method to initialize the gol instance
*/
void
GOL_Init(GOL_Instance *gol) {
gol->cm->init();
gol->cells = gol->cm->return_cells();
}
/**
* Method to run the gol instance by one step
*/
void
GOL_Step(GOL_Instance *gol) {
gol->cm->next_generation();
gol->cells = gol->cm->return_cells();
}
/**
* Method to get the cell values
*/
void
GOL_get_values(GOL_Instance *gol, int8_t *arr) {
unsigned char *c = gol->cells;
unsigned int total_cells = cellmap_width * cellmap_height;
for (unsigned int i = 0; i < total_cells; i++) {
unsigned int curr_ind = i * 8;
int8_t curr = (int8_t) *(c+i) & 0x01;
*(arr + curr_ind) = curr;
}
}
Then I am using emscripten to compile the code:
em++ -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 \
-Igameoflife/include -Os -DNDEBUG \
-s EXPORTED_FUNCTIONS="['_GOL_Instance_new', '_GOL_Instance_destroy', '_GOL_Init', '_GOL_Step', '_GOL_get_values']" \
-o gol.js gameoflife/src/cellmap.cpp bridge.cpp
And this command gives me the files 'gol.js' and 'gol.wasm' which seem to compile without any errors. However, when in my javascript I try to actually use the given functionality (note: I'm using the functions in an SVG file which is why my 'href' tags are under a different namespace):
<script xlink:href="gol.js"></script>
<script><![CDATA[
var wasm_loaded = false;
var gol_instance;
const cols = 96;
const rows = 96;
var cellArr = new Int8Array(cols * rows);
const box_side = 10; // boxes are 10 pixels accross
Module.onRuntimeInitialized = function() {
wasm_loaded = true;
gol_instance = Module._GOL_Instance_new();
}
.
.
.
Then in the console output from chrome when I host the page locally I get wasm streaming compile failed: TypeError: Module._GOL_Instance_new is not a function
and javascript crashes.
I've tried compiling with '-s EXPORT_ALL=1' and still the function can't be found. Also, it seems that the 'gol.wasm' file is uncharacteristically small (only 12 characters). I'm not sure if this could be indicative of the problem but I thought it might be relative.