Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions test/core/test_nested_structs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <stdio.h>
#include "emscripten.h"

struct base {
int x;
float y;
union {
int a;
float b;
};
char c;
};

struct hashtableentry {
int key;
base data;
};

struct hashset {
typedef hashtableentry entry;
struct chain { entry elem; chain *next; };
// struct chainchunk { chain chains[100]; chainchunk *next; };
};

struct hashtable : hashset {
hashtable() {
base b;
entry e;
chain c;
printf("*%zu,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld*\n",
sizeof(base),
long(&b.x) - long(&b),
long(&b.y) - long(&b),
long(&b.a) - long(&b),
long(&b.b) - long(&b),
long(&b.c) - long(&b),
sizeof(hashtableentry),
long(&e.key) - long(&e),
long(&e.data) - long(&e),
long(&e.data.x) - long(&e),
long(&e.data.y) - long(&e),
long(&e.data.a) - long(&e),
long(&e.data.b) - long(&e),
long(&e.data.c) - long(&e),
sizeof(hashset::chain),
long(&c.elem) - long(&c),
long(&c.next) - long(&c),
long(&c.elem.key) - long(&c),
long(&c.elem.data) - long(&c),
long(&c.elem.data.x) - long(&c),
long(&c.elem.data.y) - long(&c),
long(&c.elem.data.a) - long(&c),
long(&c.elem.data.b) - long(&c),
long(&c.elem.data.c) - long(&c)
);
}
};

struct B { char buffer[62]; int last; char laster; char laster2; };

struct Bits {
unsigned short A : 1;
unsigned short B : 1;
unsigned short C : 1;
unsigned short D : 1;
unsigned short x1 : 1;
unsigned short x2 : 1;
unsigned short x3 : 1;
unsigned short x4 : 1;
};

int main() {
hashtable t;

// Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next
// one is aligned properly. Also handle char; char; etc. properly.
B b;
printf("*%ld,%ld,%ld,%ld,%ld,%ld,%ld,%zu*\n", long(&b.buffer) - long(&b),
long(&b.buffer[0]) - long(&b),
long(&b.buffer[1]) - long(&b),
long(&b.buffer[2]) - long(&b),
long(&b.last) - long(&b),
long(&b.laster) - long(&b),
long(&b.laster2) - long(&b),
sizeof(B));

// Part 3 - bitfields, and small structures
printf("*%zu*\n", sizeof(Bits));
return 0;
}
96 changes: 2 additions & 94 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2877,105 +2877,13 @@ def test_legacy_stack_deps(self):
}''')
self.do_runf('main.c', cflags=['--js-library=lib.js'])

def test_nestedstructs(self):
src = r'''
#include <stdio.h>
#include "emscripten.h"

struct base {
int x;
float y;
union {
int a;
float b;
};
char c;
};

struct hashtableentry {
int key;
base data;
};

struct hashset {
typedef hashtableentry entry;
struct chain { entry elem; chain *next; };
// struct chainchunk { chain chains[100]; chainchunk *next; };
};

struct hashtable : hashset {
hashtable() {
base b;
entry e;
chain c;
printf("*%zu,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld*\n",
sizeof(base),
long(&b.x) - long(&b),
long(&b.y) - long(&b),
long(&b.a) - long(&b),
long(&b.b) - long(&b),
long(&b.c) - long(&b),
sizeof(hashtableentry),
long(&e.key) - long(&e),
long(&e.data) - long(&e),
long(&e.data.x) - long(&e),
long(&e.data.y) - long(&e),
long(&e.data.a) - long(&e),
long(&e.data.b) - long(&e),
long(&e.data.c) - long(&e),
sizeof(hashset::chain),
long(&c.elem) - long(&c),
long(&c.next) - long(&c),
long(&c.elem.key) - long(&c),
long(&c.elem.data) - long(&c),
long(&c.elem.data.x) - long(&c),
long(&c.elem.data.y) - long(&c),
long(&c.elem.data.a) - long(&c),
long(&c.elem.data.b) - long(&c),
long(&c.elem.data.c) - long(&c)
);
}
};

struct B { char buffer[62]; int last; char laster; char laster2; };

struct Bits {
unsigned short A : 1;
unsigned short B : 1;
unsigned short C : 1;
unsigned short D : 1;
unsigned short x1 : 1;
unsigned short x2 : 1;
unsigned short x3 : 1;
unsigned short x4 : 1;
};

int main() {
hashtable t;

// Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next
// one is aligned properly. Also handle char; char; etc. properly.
B b;
printf("*%ld,%ld,%ld,%ld,%ld,%ld,%ld,%zu*\n", long(&b.buffer) - long(&b),
long(&b.buffer[0]) - long(&b),
long(&b.buffer[1]) - long(&b),
long(&b.buffer[2]) - long(&b),
long(&b.last) - long(&b),
long(&b.laster) - long(&b),
long(&b.laster2) - long(&b),
sizeof(B));

// Part 3 - bitfields, and small structures
printf("*%zu*\n", sizeof(Bits));
return 0;
}
'''
def test_nested_structs(self):
# Bloated memory; same layout as C/C++
if self.is_wasm64():
expected = '*16,0,4,8,8,12|20,0,4,4,8,12,12,16|32,0,24,0,4,4,8,12,12,16*\n*0,0,1,2,64,68,69,72*\n*2*'
else:
expected = '*16,0,4,8,8,12|20,0,4,4,8,12,12,16|24,0,20,0,4,4,8,12,12,16*\n*0,0,1,2,64,68,69,72*\n*2*'
self.do_run(src, expected)
self.do_runf('core/test_nested_structs.cpp', expected)

def prep_dlfcn_main(self, libs=None):
if libs is None:
Expand Down