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

TypeError: The method call is ambiguous between the following methods… #134

Open
Taritsyn opened this issue Jun 25, 2018 · 0 comments
Open

Comments

@Taritsyn
Copy link
Contributor

Hello, Paul!

If you run the following console application by using the latest version of Jurassic (e0d652b):

using System;
using System.Text;

using Jurassic;

namespace TestJurassicInterop
{
	class Program
	{
		static void Main(string[] args)
		{
			var fileManager = new FakeFileManager();
			var base64Encoder = new Base64Encoder();

			var engine = new ScriptEngine
			{
				EnableExposedClrTypes = true
			};
			engine.SetGlobalValue("fileManager", fileManager);
			engine.SetGlobalValue("base64Encoder", base64Encoder);

			string result = engine.Evaluate<string>(
				"base64Encoder.EncodeToBase64(fileManager.ReadBinaryFile('0.gif'))");
			Console.WriteLine("result = {0}", result);
		}

		/// <summary>
		/// Fake file manager
		/// </summary>
		internal sealed class FakeFileManager
		{
			/// <summary>
			/// Opens a binary file, reads all content of the file, and then closes the file
			/// </summary>
			/// <param name="path">The file to open for reading</param>
			/// <returns>The byte array containing all content of the file</returns>
			public byte[] ReadBinaryFile(string path)
			{
				if (path == null)
				{
					throw new ArgumentNullException("path");
				}

				var bytes = new byte[] { 71, 73, 70, 56, 57, 97, 1, 0 };

				return bytes;
			}
		}

		/// <summary>
		/// Base64 encoder
		/// </summary>
		internal sealed class Base64Encoder
		{
			/// <summary>
			/// Encodes a text content to Base64
			/// </summary>
			/// <param name="value">Text content</param>
			/// <returns>Base64 encoded content</returns>
			public string EncodeToBase64(string value)
			{
				if (value == null)
				{
					throw new ArgumentNullException("value");
				}

				byte[] bytes = Encoding.GetEncoding(0).GetBytes(value);
				string encodedValue = Convert.ToBase64String(bytes);

				return encodedValue;
			}

			/// <summary>
			/// Encodes a binary content to Base64
			/// </summary>
			/// <param name="value">Binary content</param>
			/// <returns>Base64 encoded content</returns>
			public string EncodeToBase64(byte[] value)
			{
				if (value == null)
				{
					throw new ArgumentNullException("value");
				}

				string encodedValue = Convert.ToBase64String(value);

				return encodedValue;
			}
		}
	}
}

Then following error occurs:

Unhandled Exception: Jurassic.JavaScriptException: TypeError: The method call is ambiguous between the following methods: System.String EncodeToBase64(System.String), System.String EncodeToBase64(Byte[])
   at Jurassic.Compiler.BinderUtilities.ResolveOverloads(RuntimeMethodHandle[] methodHandles, ScriptEngine engine, Object thisValue, Object[] arguments)
   at binder_for_TestJurassicInterop.Program+Base64Encoder.EncodeToBase64(ScriptEngine , Object , Object[] )
   at Jurassic.Compiler.Binder.Call(ScriptEngine engine, Object thisObject, Object[] arguments)
   at Jurassic.Library.ClrFunction.CallLateBound(Object thisObject, Object[] arguments)
   at Jurassic.Library.FunctionInstance.CallWithStackTrace(String path, String function, Int32 line, Object thisObject, Object[] argumentValues)
   at eval(ScriptEngine , Scope , Object )
   at Jurassic.Compiler.EvalMethodGenerator.Execute(ScriptEngine engine)
   at Jurassic.ScriptEngine.Evaluate(ScriptSource source)
   at TestJurassicInterop.Program.Main(String[] args) in c:\temp\TestJurassicInterop\TestJurassicInterop\Program.cs:line 22

Similar code worked for many years until changes were made to CLR interop (5b83b3b).

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

1 participant