Skip to content

Commit

Permalink
Fix marshalling of arrays with immutable elements (at least for most …
Browse files Browse the repository at this point in the history
…cases)
  • Loading branch information
Jakob Ovrum committed Sep 1, 2014
1 parent 0647c5c commit ff8dfe5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
9 changes: 5 additions & 4 deletions luad/conversions/arrays.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ void pushArray(T)(lua_State* L, ref T arr) if (isArray!T)
}
}

// TODO: do the immutable/const initialization *properly*
T getArray(T)(lua_State* L, int idx) if (isArray!T)
{
alias ElementType!T ElemType;
alias ElemType = ElementType!T;
auto len = lua_objlen(L, idx);

static if(isStaticArray!T)
{
if(len != T.length)
luaL_error(L, "Incorrect number of array elements: %d, expected: %d", len, T.length);

T arr;
Unqual!ElemType[T.length] arr;
}
else
auto arr = new ElemType[len];
auto arr = new Unqual!ElemType[len];

foreach(i; 0 .. len)
{
Expand All @@ -44,7 +45,7 @@ T getArray(T)(lua_State* L, int idx) if (isArray!T)
arr[i] = popValue!ElemType(L);
}

return arr;
return cast(T)arr;
}

void fillStaticArray(T)(lua_State* L, ref T arr) if(isStaticArray!T)
Expand Down
17 changes: 11 additions & 6 deletions luad/stack.d
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,18 @@ unittest

assert(lua_gettop(L) == 0, "bad popValue semantics for primitives");

// arrays
immutable int[] arr = [1, 2, 3];
pushValue(L, arr);
assert(lua_istable(L, -1) && popValue!(typeof(arr))(L) == arr);

//void arrays
immutable void[] iarr = "foobar";
pushValue(L, iarr);
assert(lua_isstring(L, -1) && popValue!(typeof(iarr))(L) == "foobar");
immutable void[] voidiarr = "foobar";
pushValue(L, voidiarr);
assert(lua_isstring(L, -1) && popValue!(typeof(voidiarr))(L) == "foobar");

void[] arr ="baz".dup;
pushValue(L, arr);
void[] voidarr ="baz".dup;
pushValue(L, voidarr);
assert(lua_isstring(L, -1) && popValue!(void[])(L) == "baz");

//popStack
Expand All @@ -626,4 +631,4 @@ unittest
assert(stack[1].type == LuaType.String);
assert(stack[2].type == LuaType.Number);
assert(stack[3].type == LuaType.Boolean);
}
}

1 comment on commit ff8dfe5

@TurkeyMan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment, these sorts of issues are dealt with very extensively in my further patches.
I realise this one is self-contained to the array code, which is fine, but just so you know, the struct/class patch deals with mutability extensively (actually, I need to push some updates to that patch).
If you make many of these sorts of fixes now, you're probably just making lots of merge conflicts for me.

Please sign in to comment.