Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua string include zero bytes cannot pass to function receive byte[] parameter #9

Closed
xophiix opened this issue Mar 19, 2015 · 2 comments

Comments

@xophiix
Copy link

xophiix commented Mar 19, 2015

in C#

void foo(byte[]) {
}

// binding code is
MyType self=(MyType)checkSelf(l);
System.Byte[] a1;
checkType(l,2,out a1);
self.foo(a1);

it doesn't work because no string to byte[] conversion allowed directly.

so I have to write my own MonoPInvoke function like:

[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static public int foo(IntPtr l) {
    MyType self=(MyType)LuaObject.checkSelf(l);
    int bytesLen;
    IntPtr ptr = LuaDLL.lua_tolstring(l, 2, out bytesLen);
    byte[] bytes = new byte[bytesLen];
    for (int i = 0; i < bytesLen; ++i) {
        bytes[i] = (byte)Marshal.ReadByte(ptr, i);
    }
    self.foo(bytes);
}

If the engine can include this kind of conversion code, it will be better.

I think maybe I should fork slua and send some pull request :)

@pangweiwei
Copy link
Owner

I've written function that convert byte[] to lua string, but some user prefer keeping as ud. So i don't make a decision how to pass byte[] , you can do it yourself.

@pangweiwei
Copy link
Owner

I think keeping as ud , and supply a function that convert ud to str is better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants