Skip to content

Commit

Permalink
Add a back door to opt out of the packed array argument checking via …
Browse files Browse the repository at this point in the history
…an attribute.

Make JSILc return a non-zero exit code if non-fatal errors occurred during translation.
  • Loading branch information
kg committed Mar 23, 2013
1 parent c1cb3ee commit df09acc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Compiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ static void Main (string[] arguments) {
Console.Error.WriteLine("// No assemblies specified to translate. Exiting.");
}

int totalFailureCount = 0;

foreach (var buildGroup in buildGroups) {
var config = buildGroup.BaseConfiguration;
var variables = buildGroup.BaseVariables;
Expand Down Expand Up @@ -541,6 +543,8 @@ static void Main (string[] arguments) {
EmitLog(outputDir, localConfig, filename, outputs, ignoredMethods);

buildGroup.Profile.WriteOutputs(localVariables, outputs, outputDir, Path.GetFileName(filename) + ".");

totalFailureCount += translator.Failures.Count;
}
}
}
Expand All @@ -549,6 +553,8 @@ static void Main (string[] arguments) {
Console.Error.WriteLine("// Press the any key to continue.");
Console.ReadKey();
}

Environment.ExitCode = totalFailureCount;
}

static void EmitLog (
Expand Down
8 changes: 8 additions & 0 deletions JSIL/AssemblyTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public MethodToAnalyze (MethodInfo mi) {
public readonly FunctionCache FunctionCache;
public readonly AssemblyManifest Manifest;

public readonly List<Exception> Failures = new List<Exception>();

public event Action<string> AssemblyLoaded;
public event Action<string> ProxyAssemblyLoaded;

Expand Down Expand Up @@ -1447,6 +1449,8 @@ internal JSFunctionExpression TranslateMethodExpression (
optimizer.Optimize(context, ilb);
}
} catch (Exception exception) {
Failures.Add(exception);

if (CouldNotDecompileMethod != null)
CouldNotDecompileMethod(bodyDef.FullName, exception);

Expand Down Expand Up @@ -1480,6 +1484,8 @@ internal JSFunctionExpression TranslateMethodExpression (
try {
body = translator.Translate();
} catch (Exception exc) {
Failures.Add(exc);

if (CouldNotDecompileMethod != null)
CouldNotDecompileMethod(bodyDef.FullName, exc);

Expand Down Expand Up @@ -1863,6 +1869,7 @@ where NeedsStaticConstructor(f.FieldType)
defaultValue = translator.TranslateNode(ile.Arguments[0]);
} catch (Exception ex) {
WarningFormat("Warning: failed to translate default value for static field '{0}': {1}", targetField, ex);

continue;
}

Expand All @@ -1887,6 +1894,7 @@ where NeedsStaticConstructor(f.FieldType)
} catch (Exception ex) {
// This may fail because we didn't do a full translation.
WarningFormat("Warning: failed to translate default value for static field '{0}': {1}", targetField, ex);

continue;
}

Expand Down
3 changes: 3 additions & 0 deletions JSIL/PackedStructArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static TypeReference MakePackedArrayType (TypeReference arrayType, TypeRe
}

public static void CheckInvocationSafety (MethodInfo method, JSExpression[] argumentValues, TypeSystem typeSystem) {
if (method.Metadata.HasAttribute("JSIL.Meta.JSAllowPackedArrayArgumentsAttribute"))
return;

TypeReference temp;
string[] argumentNames = GetPackedArrayArgumentNames(method, out temp);

Expand Down
4 changes: 4 additions & 0 deletions Meta/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,8 @@ public JSPackedArrayArgumentsAttribute (params string[] argumentNames) {
ArgumentNames = argumentNames;
}
}

[AttributeUsage(AttributeTargets.Method)]
public class JSAllowPackedArrayArgumentsAttribute : Attribute {
}
}
2 changes: 2 additions & 0 deletions Meta/PackedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static class TypedArrayExtensionMethods {
/// If the specified array is backed by a typed array, returns its backing array buffer.
/// </summary>
[JSReplacement("JSIL.GetArrayBuffer($array)")]
[JSAllowPackedArrayArguments]
[JSIsPure]
public static dynamic GetArrayBuffer<T> (this T[] array)
where T : struct {
Expand All @@ -46,6 +47,7 @@ public static class PackedArrayExtensionMethods {
/// If the specified array is a packed array, returns its backing typed array.
/// </summary>
[JSReplacement("JSIL.GetBackingTypedArray($array)")]
[JSAllowPackedArrayArguments]
[JSIsPure]
public static byte[] GetBackingTypedArray<T> (this T[] array)
where T : struct {
Expand Down
27 changes: 27 additions & 0 deletions Tests/FailingTestCases/PassNormalArrayAsPackedArrayArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using JSIL.Meta;

public struct IntFloatPair {
public int Int;
public float Float;

public override string ToString () {
return String.Format("Int={0:0000}, Float={1:000.000}", Int, Float);
}
}

public static class Program {
public static IntFloatPair[] PackedArray = new IntFloatPair[2];

public static unsafe void Main (string[] args) {
FillPackedArray(PackedArray);

foreach (var item in PackedArray)
Console.WriteLine(item);
}

[JSPackedArrayArguments("array")]
public static void FillPackedArray (IntFloatPair[] array) {
Console.WriteLine(array.Length);
}
}

0 comments on commit df09acc

Please sign in to comment.