Skip to content

Commit 5f83234

Browse files
committed
Merge pull request livecode#2958 from peter-b/js/lcb-extensions
libscript: Further tweaks to alignment for foreign invoke storage
2 parents ead494c + 6881828 commit 5f83234

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

libscript/src/script-instance.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool __MCScriptHandlerDescribe(void *context, MCStringRef &r_desc);
4747

4848
////////////////////////////////////////////////////////////////////////////////
4949

50-
template <size_t WORD, size_t LENGTH>
50+
template <typename WORD, size_t LENGTH>
5151
class __MCScriptStackStorage
5252
{
5353
public:
@@ -59,23 +59,35 @@ class __MCScriptStackStorage
5959
void *Allocate(size_t p_request)
6060
{
6161
/* Ensure the amount allocated is aligned to the word size */
62-
size_t t_amount = (~(WORD-1)) & ((WORD-1) + p_request);
62+
size_t t_amount = (~(sizeof(WORD)-1)) & ((sizeof(WORD)-1) + p_request);
63+
MCAssert(t_amount % sizeof(WORD) == 0);
64+
MCAssert(t_amount >= p_request);
6365

6466
/* Ensure there's enough space left in the storage */
65-
MCAssert(m_used + t_amount < (WORD * LENGTH));
67+
MCAssert(m_used + t_amount < sizeof(m_storage));
6668

6769
/* Create and return the pointer, updating the current offset */
68-
void *t_ptr = &m_storage[m_used];
70+
MCAssert(m_used % sizeof(WORD) == 0);
71+
void *t_ptr = &m_storage[m_used / sizeof(WORD)];
6972

7073
m_used += t_amount;
7174

7275
return t_ptr;
7376
}
7477
private:
75-
uint8_t m_storage[WORD * LENGTH];
78+
WORD m_storage[LENGTH];
7679
size_t m_used;
7780
};
7881

82+
#if defined(__EMSCRIPTEN__)
83+
/* On emscripten, we require double alignment */
84+
typedef __MCScriptStackStorage<double,32> MCScriptStackStorage;
85+
86+
#else
87+
typedef __MCScriptStackStorage<void *,32> MCScriptStackStorage;
88+
89+
#endif
90+
7991
////////////////////////////////////////////////////////////////////////////////
8092

8193
// This is the module of the most recent LCB stack frame on the current thread's
@@ -1219,7 +1231,7 @@ static bool MCScriptPerformForeignInvoke(MCScriptFrame*& x_frame, MCScriptInstan
12191231
void *t_args[16];
12201232
ffi_type *t_arg_types[16];
12211233
bool t_arg_new[16];
1222-
__MCScriptStackStorage<sizeof(void *),32> t_invoke_storage;
1234+
MCScriptStackStorage t_invoke_storage;
12231235

12241236
uindex_t t_arg_index;
12251237
t_arg_index = 0;

0 commit comments

Comments
 (0)