11 #ifndef EMP_JS_UTILS_H 12 #define EMP_JS_UTILS_H 18 #include "../base/assert.h" 19 #include "../base/vector.h" 20 #include "../base/array.h" 46 std::map<std::string, std::string> map_type_names;
47 map_type_names[
typeid(int8_t).name()] =
"i8";
48 map_type_names[
typeid(int16_t).name()] =
"i16";
49 map_type_names[
typeid(int32_t).name()] =
"i32";
50 map_type_names[
typeid(int64_t).name()] =
"i64";
51 map_type_names[
typeid(float).name()] =
"float";
52 map_type_names[
typeid(double).name()] =
"double";
53 map_type_names[
typeid(int8_t*).name()] =
"i8*";
54 map_type_names[
typeid(int16_t*).name()] =
"i16*";
55 map_type_names[
typeid(int32_t*).name()] =
"i32*";
56 map_type_names[
typeid(int64_t*).name()] =
"i64*";
57 map_type_names[
typeid(
float*).name()] =
"float*";
58 map_type_names[
typeid(
double*).name()] =
"double*";
59 map_type_names[
typeid(
void*).name()] =
"*";
60 map_type_names[
typeid(std::string).name()] =
"string";
62 return map_type_names;
77 template<
typename C,
class =
typename C::value_type>
78 typename std::enable_if<std::is_pod<typename C::value_type>::value,
void>::type
81 using T =
typename C::value_type;
84 emp_assert((map_type_names.find(
typeid(T).name()) != map_type_names.end()));
85 int type_size =
sizeof(T);
87 std::string type_string = map_type_names[
typeid(T).name()];
90 if (recursive_el.
size() == 0) {
91 EM_ASM({emp_i.__incoming_array = [];});
95 var curr_array = emp_i.__incoming_array;
99 while (curr_array.length > 0) {
100 var next_index = getValue($4+(depth*4),
"i32");
102 curr_array = curr_array[next_index];
106 for (i=0; i<$1; i++) {
107 curr_array.push(getValue($0+(i*$2), Pointer_stringify($3)));
109 }, &values[0], values.size(), type_size, type_string.c_str(), recursive_el.data());
113 template<
typename C,
class =
typename C::value_type>
114 typename std::enable_if<std::is_same<typename C::value_type, std::string>::value,
void>::type
118 if (recursive_el.
size() == 0) {
119 EM_ASM({emp_i.__incoming_array = [];});
123 emp_i.__curr_array = emp_i.__incoming_array;
127 while (emp_i.__curr_array.length > 0) {
128 var next_index = getValue($0+(depth*4),
"i32");
130 emp_i.__curr_array = emp_i.__curr_array[next_index];
132 }, recursive_el.data());
135 for (
auto val : values) {
138 emp_i.__curr_array.push(Pointer_stringify($0));
142 EM_ASM({
delete emp_i.__curr_array;});
146 template<
typename C,
class =
typename C::value_type>
147 typename std::enable_if<C::value_type::n_fields != -1,
void>::type
152 if (recursive_el.
size() == 0) {
153 EM_ASM({emp_i.__incoming_array = [];});
158 var curr_array = emp_i.__incoming_array;
162 while (curr_array.length > 0) {
163 var next_index = getValue($1+(depth*4),
"i32");
165 curr_array = curr_array[next_index];
169 for (i=0; i<$0; i++) {
171 curr_array.push(new_obj);
173 }, values.size(), recursive_el.data());
175 for (std::size_t j = 0; j<values.size(); j++) {
176 for (std::size_t i = 0; i<values[j].var_names.size(); i++) {
179 std::string var_name = values[j].var_names[i];
180 std::string type_string = map_type_names[values[j].var_types[i].name()];
182 emp_assert((map_type_names.find(values[j].var_types[i].name())
183 != map_type_names.end()), values[j].var_types[i].name());
187 var curr_array = emp_i.__incoming_array;
191 while (curr_array[0].length > 0) {
192 var next_index = getValue($4+(depth*4),
"i32");
194 curr_array = curr_array[next_index];
197 if (Pointer_stringify($1) ==
"string") {
198 curr_array[$3][Pointer_stringify($2)] = Pointer_stringify($0);
200 curr_array[$3][Pointer_stringify($2)] = getValue($0, Pointer_stringify($1));
202 }, values[j].pointers[i], type_string.c_str(), var_name.c_str(),
203 j, recursive_el.data());
211 template<
typename C,
class =
typename C::value_type>
220 template<std::
size_t SIZE1, std::
size_t SIZE2,
typename T>
225 if (recursive_el.
size() == 0) {
226 EM_ASM({emp_i.__incoming_array = [];});
231 var curr_array = emp_i.__incoming_array;
233 while (curr_array.length > 0) {
234 var next_index = getValue($0+(depth*4),
"i32");
236 curr_array = curr_array[next_index];
238 for (i=0; i<$1; i++) {
241 }, recursive_el.data(), values.
size());
245 for (std::size_t i = 0; i<values.size(); i++) {
259 if (recursive_el.
size() == 0) {
260 EM_ASM({emp_i.__incoming_array = [];});
265 var curr_array = emp_i.__incoming_array;
267 while (curr_array.length > 0) {
268 var next_index = getValue($0+(depth*4),
"i32");
270 curr_array = curr_array[next_index];
272 for (i=0; i<$1; i++) {
275 }, recursive_el.data(), values.
size());
279 for (std::size_t i = 0; i<values.size(); i++) {
298 template <std::
size_t SIZE,
typename T>
303 emp_assert((map_type_names.find(
typeid(T).name()) != map_type_names.end()),
typeid(T).name());
304 int type_size =
sizeof(T);
306 std::string type_string = map_type_names[
typeid(T).name()];
309 emp_assert(arr.
size() == EM_ASM_INT_V({
return emp_i.__outgoing_array.length}),
310 arr.
size(), EM_ASM_INT_V({
return emp_i.__outgoing_array.length}));
313 T * buffer = (T*) EM_ASM_INT({
314 var buffer = Module._malloc(emp_i.__outgoing_array.length*$0);
316 for (i=0; i<emp_i.__outgoing_array.length; i++) {
317 setValue(buffer+(i*$0), emp_i.__outgoing_array[i], Pointer_stringify($1));
321 }, type_size, type_string.c_str());
324 for (std::size_t i=0; i<arr.
size(); i++) {
325 arr[i] = *(buffer + i);
333 template <
typename T>
338 emp_assert((map_type_names.find(
typeid(T).name()) != map_type_names.end()),
typeid(T).name());
339 int type_size =
sizeof(T);
341 std::string type_string = map_type_names[
typeid(T).name()];
344 T * buffer = (T*) EM_ASM_INT({
345 var buffer = Module._malloc(emp_i.__outgoing_array.length*$0);
347 for (i=0; i<emp_i.__outgoing_array.length; i++) {
348 setValue(buffer+(i*$0), emp_i.__outgoing_array[i], Pointer_stringify($1));
352 }, type_size, type_string.c_str());
355 for (
int i=0; i < EM_ASM_INT_V({
return emp_i.__outgoing_array.length}); i++) {
399 template <std::
size_t SIZE>
402 emp_assert(arr.
size() == EM_ASM_INT_V({
return emp_i.__outgoing_array.length}));
404 char * buffer = (
char *) EM_ASM_INT_V({
407 var buffer = Module._malloc(emp_i.__outgoing_array.length*2);
409 for (i=0; i<emp_i.__outgoing_array.length; i++) {
410 writeStringToMemory(emp_i.__outgoing_array[i], buffer+(i*2));
416 for (
size_t i=0; i<arr.
size(); i++) {
417 arr[i] = *(buffer + i*2);
428 char * buffer = (
char *) EM_ASM_INT_V({
431 var buffer = Module._malloc(emp_i.__outgoing_array.length*2);
433 for (i=0; i<emp_i.__outgoing_array.length; i++) {
434 writeStringToMemory(emp_i.__outgoing_array[i], buffer+(i*2));
440 for (
int i=0; i<EM_ASM_INT_V({
return emp_i.__outgoing_array.length}); i++) {
448 template <std::
size_t SIZE>
451 emp_assert(arr.
size() == EM_ASM_INT_V({
return emp_i.__outgoing_array.length}));
453 char * buffer = (
char *) EM_ASM_INT_V({
456 for (i=0; i<emp_i.__outgoing_array.length; i++) {
457 arr_size += emp_i.__outgoing_array[i].length + 1;
460 var buffer = Module._malloc(arr_size);
463 var cumulative_size = 0;
464 for (i=0; i<emp_i.__outgoing_array.length; i++) {
465 writeStringToMemory(emp_i.__outgoing_array[i], buffer + (cumulative_size));
466 cumulative_size += emp_i.__outgoing_array[i].length + 1;
473 int cumulative_size = 0;
474 for (
size_t i=0; i<arr.
size(); i++) {
476 arr[i] = std::string(buffer + cumulative_size);
477 cumulative_size += arr[i].
size() + 1;
486 char * buffer = (
char *) EM_ASM_INT_V({
489 for (i=0; i<emp_i.__outgoing_array.length; i++) {
490 arr_size += emp_i.__outgoing_array[i].length + 1;
493 var buffer = Module._malloc(arr_size);
496 var cumulative_size = 0;
497 for (i=0; i<emp_i.__outgoing_array.length; i++) {
498 writeStringToMemory(emp_i.__outgoing_array[i], buffer + (cumulative_size));
499 cumulative_size += emp_i.__outgoing_array[i].length + 1;
506 int cumulative_size = 0;
507 for (
int i=0; i<EM_ASM_INT_V({
return emp_i.__outgoing_array.length}); i++) {
509 arr.
push_back(std::string(buffer + cumulative_size));
510 cumulative_size += arr[(size_t)i].size() + 1;
517 template <std::
size_t SIZE, std::
size_t SIZE2,
typename T>
520 emp_assert(arr.
size() == EM_ASM_INT_V({
return emp_i.__outgoing_array.length}));
524 EM_ASM({emp_i.__temp_array = [emp_i.__outgoing_array];});
528 EM_ASM({emp_i.__temp_array.push(emp_i.__outgoing_array);});
531 for (
size_t i = 0; i < arr.
size(); i++) {
533 emp_i.__outgoing_array = emp_i.__temp_array[emp_i.__temp_array.length - 1][$0];
539 if (recurse == 0) { EM_ASM({emp_i.__temp_array = [];}); }
540 else { EM_ASM({emp_i.__temp_array.pop();}); }
544 template <
typename T>
548 int size = EM_ASM_INT_V({
return emp_i.__outgoing_array.length});
552 emp_i.__temp_array = [emp_i.__outgoing_array];
553 console.log(emp_i.__outgoing_array);
558 EM_ASM({emp_i.__temp_array.push(emp_i.__outgoing_array);});
561 for (
int i = 0; i < size; i++) {
563 emp_i.__outgoing_array = emp_i.__temp_array[emp_i.__temp_array.length - 1][$0];
565 while ((
int)arr.
size() <= i) {
573 EM_ASM({emp_i.__temp_array = [];});
575 EM_ASM({emp_i.__temp_array.pop();});
Define Initialize() and other functions to set up Empirical to build Emscripten projects.
void push_back(PB_Ts &&...args)
Definition: vector.h:189
size_t size() const
Definition: vector.h:151
void pass_array_to_cpp(emp::array< T, SIZE > &arr, bool recurse=false)
Definition: js_utils.h:299
void pass_vector_to_cpp(emp::vector< T > &arr, bool recurse=false)
Same as pass_array_to_cpp, but lets you store values in a vector instead.
Definition: js_utils.h:334
std::map< std::string, std::string > get_type_to_string_map()
Definition: js_utils.h:41
constexpr size_t size() const
Definition: array.h:137
If we are in emscripten, make sure to include the header.
Definition: array.h:37
#define emp_assert(...)
Definition: assert.h:199
void pass_array_to_javascript(C values)
Definition: js_utils.h:212