Skip to content

Commit

Permalink
If a JSReplacement method is also JSIsPure, treat it as constant if a…
Browse files Browse the repository at this point in the history
…ll its arguments are constant.

Mark the Math proxies as pure.
  • Loading branch information
kg committed Mar 23, 2013
1 parent 3d598e5 commit 6aa69bc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
13 changes: 12 additions & 1 deletion JSIL/AST/JSLiteralTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,16 @@ public class JSVerbatimLiteral : JSLiteral {
public readonly TypeReference Type;
public readonly string Expression;
public readonly IDictionary<string, JSExpression> Variables;
public readonly bool IsConstantIfArgumentsAre;

public JSVerbatimLiteral (MethodReference originalMethod, string expression, IDictionary<string, JSExpression> variables, TypeReference type = null)
public JSVerbatimLiteral (MethodReference originalMethod, string expression, IDictionary<string, JSExpression> variables, TypeReference type = null, bool isConstantIfArgumentsAre = false)
: base(GetValues(variables)) {

OriginalMethod = originalMethod;
Type = type;
Expression = expression;
Variables = variables;
IsConstantIfArgumentsAre = isConstantIfArgumentsAre;
}

protected static JSExpression[] GetValues (IDictionary<string, JSExpression> variables) {
Expand All @@ -316,6 +318,15 @@ public override object Literal {
get { return Expression; }
}

public override bool IsConstant {
get {
if (!IsConstantIfArgumentsAre)
return false;
else
return Variables.Values.All((v) => v.IsConstant);
}
}

public override TypeReference GetActualType (TypeSystem typeSystem) {
if (Type != null)
return Type;
Expand Down
4 changes: 3 additions & 1 deletion JSIL/ILBlockTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,10 @@ TypeReference resultType
argsDict.Add(kvp.Name, kvp.Value);
}

var isConstantIfArgumentsAre = methodInfo.Metadata.HasAttribute("JSIL.Meta.JSIsPure");

return new JSVerbatimLiteral(
method, (string)parms[0].Value, argsDict, resultType
method, (string)parms[0].Value, argsDict, resultType, isConstantIfArgumentsAre
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions Meta/PackedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public LinkedTypeAttribute (Type type) {

public unsafe interface IPackedArray<T> {
[JSResultIsNew]
[JSIsPure]
T Get (int index);
[JSResultIsNew]
void* GetReference (int index);
[JSEscapingArguments("value")]
[JSMutatedArguments()]
void Set (int index, T value);
[JSIsPure]
int Length { get; }
}

Expand All @@ -28,6 +31,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)")]
[JSIsPure]
public static dynamic GetArrayBuffer<T> (this T[] array)
where T : struct {

Expand All @@ -40,6 +44,7 @@ public static class PackedArrayExtensionMethods {
/// If the specified array is a packed array, returns its backing typed array.
/// </summary>
[JSReplacement("JSIL.GetBackingTypedArray($array)")]
[JSIsPure]
public static byte[] GetBackingTypedArray<T> (this T[] array)
where T : struct {

Expand Down
17 changes: 17 additions & 0 deletions Proxies/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,86 +21,103 @@ public static AnyType Max (params AnyType[] arguments) {
}

[JSReplacement("Math.abs($value)")]
[JSIsPure]
public static AnyType Abs (AnyType value) {
throw new InvalidOperationException();
}

[JSReplacement("Math.sqrt($d)")]
[JSIsPure]
public static double Sqrt (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.cos($d)")]
[JSIsPure]
public static double Cos (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.sin($d)")]
[JSIsPure]
public static double Sin (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.acos($d)")]
[JSIsPure]
public static double Acos (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.asin($d)")]
[JSIsPure]
public static double Asin (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.tan($d)")]
[JSIsPure]
public static double Tan (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.atan($d)")]
[JSIsPure]
public static double Atan (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.atan2($y, $x)")]
[JSIsPure]
public static double Atan2 (double y, double x) {
throw new InvalidOperationException();
}

[JSReplacement("Math.log($d)")]
[JSIsPure]
public static double Log (double d) {
throw new InvalidOperationException();
}

[JSReplacement("(Math.log($a) / Math.log($newBase))")]
[JSIsPure]
public static double Log (double a, double newBase) {
throw new InvalidOperationException();
}

[JSReplacement("(Math.log($d) / Math.LN10)")]
[JSIsPure]
public static double Log10 (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.round($d)")]
[JSIsPure]
public static double Round (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.floor($d)")]
[JSIsPure]
public static double Floor (double d) {
throw new InvalidOperationException();
}

[JSReplacement("($d | 0)")]
[JSIsPure]
public static double Truncate (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.ceil($d)")]
[JSIsPure]
public static double Ceiling (double d) {
throw new InvalidOperationException();
}

[JSReplacement("Math.pow($base, $exponent)")]
[JSIsPure]
public static AnyType Pow (AnyType @base, AnyType exponent) {
throw new InvalidOperationException();
}
Expand Down
1 change: 1 addition & 0 deletions build_demos.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ title Building Pathtracer
bin\JSILc "Examples\SimplePathtracer.sln"
title Building WebGL
bin\JSILc "Examples\WebGL\WebGL.sln"
bin\JSILc "Examples\WebGL_Vertex_Structs\WebGL_Vertex_Structs.sln"
title Building Tetris
bin\JSILc "Examples\ThirdParty\Tetris\tetris.sln" "jsil.org\demos\Tetris\Tetris.jsilconfig" --platform=x86 --configuration=Debug
title Building Platformer Starter Kit
Expand Down

0 comments on commit 6aa69bc

Please sign in to comment.