neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
467 stars 76 forks source link

Expception: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'. #111

Closed NotoriousRebel closed 4 years ago

NotoriousRebel commented 4 years ago

NeoLua Version: NeoLua 1.3.11 Example to reproduce:

              using Lua lua = new Lua();
               dynamic dg = lua.CreateEnvironment<LuaGlobal>();
                var source = @"
                local url = ""https://github.com/totovr/C-Sharp/blob/master/HelloWorld/HelloWorld/bin/Debug/HelloWorld.exe?raw=true"";
                local cmd = { };
                local sys = clr.System;
                local client = sys.Net.WebClient(); 
                print(url);
                local ms : byte = sys.IO.MemoryStream(client:DownloadData(url));
                print(ms)
                return 1;
                "
                var chunk = lua.CompileChunk(source, "test.lua", new LuaCompileOptions() { DebugEngine = null });
                 Console.WriteLine(dg.dochunk(chunk));

Expception: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'. StackTrace: -- internal --

I have tried removing the byte next to local ms, replacing it with System.Byte, replacing it with System.Byte[], adding the keyword new before sys.IO.MemoryStream... Not sure what is going wrong.

neolithos commented 4 years ago

This line looks strange.

  1. DownloadData returns a array of values, they are passed to an overloaded constructors -> this sometimes tricky -> use named signatures to be sure
  2. The return value of the constructor MemoryStream is a class not a byte -> : byte or change it to System.IO.MemoryStream
  3. What do you try to print?
    local ms = sys.IO.MemoryStream( buffer = client:DownloadData(url) );
NotoriousRebel commented 4 years ago

Thank you for the quick response, printing was just for debug purposes. I have it working now I removed the signatures and turned client:DownloadData(URL) into a variable :)