/// <summary>
/// __call metafunction of type references.
/// Searches for and calls a constructor for the type.
/// Returns nil if the constructor is not found or if the arguments are invalid.
/// Throws an error if the constructor generates an exception.
/// </summary>
int classCall(lua.State L)
{
using (luanet.entercfunction(L, interpreter))
{
var klass = _checktyperef(L);
var validConstructor = new MethodCache();
lua.remove(L, 1);
var constructors = klass.UnderlyingSystemType.GetConstructors();
foreach (ConstructorInfo constructor in constructors)
{
if (translator.matchParameters(L, constructor, ref validConstructor))
{
if (!translator.memberIsAllowed(constructor))
{
if (constructors.Length == 1)
{
return(luaL.error(L, "constructor call failed (access denied)"));
}
continue;
}
object result;
try { result = constructor.Invoke(validConstructor.args); }
catch (TargetInvocationException ex) { return(translator.throwError(L, luaclr.verifyex(ex.InnerException))); }
catch (Exception ex) { return(luaL.error(L, "constructor call failed (" + ex.Message + ")")); }
translator.push(L, result);
return(1);
}
}
return(luaL.error(L, "constructor arguments do not match (" + klass.UnderlyingSystemType + ")"));
}
}