Skip to content

Commit

Permalink
New JsAPI (sq#532). Tests updated, removed depricated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
iskiselev committed Dec 9, 2015
1 parent 7b08c67 commit 5838f04
Show file tree
Hide file tree
Showing 19 changed files with 649 additions and 112 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(function JsFunction$Members() {
var $, $thisType;
var $T00 = function () {
return ($T00 = JSIL.Memoize($asm00.JSIL.JsObject))();
};

function JsFunction__ctor() {
$T00().prototype._ctor.call(this);
};

JSIL.MakeType({
BaseType: $asm00.TypeRef("JSIL.JsObject"),
Name: "JSIL.JsObject+JsFunction",
IsPublic: false,
IsReferenceType: true,
MaximumConstructorArguments: 0,
}, function ($ib) {
$ = $ib;

$.Method({ Static: false, Public: false }, ".ctor",
JSIL.MethodSignature.Void,
JsFunction__ctor
);


return function (newThisType) { $thisType = newThisType; };
});

})();
34 changes: 34 additions & 0 deletions JSIL.Libraries/Includes/Bootstrap/Dynamic/Classes/JSIL.JsObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function JsObject$Members() {
var $, $thisType;

function JsObject__ctor() {
};

function JsObject_Global(key) {
return JSIL.GlobalNamespace[key];
};

JSIL.MakeType({
BaseType: $asm01.TypeRef("System.Object"),
Name: "JSIL.JsObject",
IsPublic: true,
IsReferenceType: true,
MaximumConstructorArguments: 0,
}, function ($ib) {
$ = $ib;

$.Method({ Static: false, Public: false }, ".ctor",
JSIL.MethodSignature.Void,
JsObject__ctor
);

$.Method({ Static: true, Public: true }, "Global",
new JSIL.MethodSignature($.Type, [$.String]),
JsObject_Global
);


return function (newThisType) { $thisType = newThisType; };
});

})();
20 changes: 20 additions & 0 deletions JSIL.Libraries/Includes/Bootstrap/Dynamic/Classes/JSIL.Services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function Services$Members() {
var $, $thisType;

function Services_Get(serviceName, throwIfMissing) {
return JSIL.Host.getService(serviceName, !throwIfMissing);
};

JSIL.MakeStaticClass("JSIL.Services", true, [], function ($ib) {
$ = $ib;

$.Method({ Static: true, Public: true }, "Get",
new JSIL.MethodSignature($asm00.TypeRef("JSIL.JsObject"), [$.String, $.Boolean]),
Services_Get
);


return function (newThisType) { $thisType = newThisType; };
});

})();
4 changes: 4 additions & 0 deletions JSIL.Libraries/Includes/Bootstrap/Dynamic/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ JSIL.DeclareNamespace("Microsoft.CSharp.RuntimeBinder");
JSIL.DeclareNamespace("System.Linq");
JSIL.DeclareNamespace("System.Linq.Expressions");

//? include("Classes/JSIL.JsObject.js"); writeln();
//? include("Classes/JSIL.JsObject.JSFunction.js"); writeln();
//? include("Classes/JSIL.JSObjectHelper.js"); writeln();

//? include("Classes/System.Runtime.CompilerServices.CallSite.js"); writeln();
//? include("Classes/System.Runtime.CompilerServices.CallSiteBinder.js"); writeln();
//? include("Classes/Microsoft.CSharp.RuntimeBinder.Binder.js"); writeln();
Expand Down
4 changes: 4 additions & 0 deletions JSIL.Libraries/JSIL.Libraries.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<ItemGroup>
<Content Include="Includes\Bootstrap\Core\Helpers\JSIL.MakeIConvertibleMethods.js" />
<Content Include="Includes\Bootstrap\Core\Interfaces\System.IConvertible.js" />
<Content Include="Includes\Bootstrap\Dynamic\Classes\JSIL.Services.js" />
<Content Include="Includes\Bootstrap\Dynamic\Classes\JSIL.JSObjectHelper.js" />
<Content Include="Includes\Bootstrap\Dynamic\Classes\JSIL.JsObject.JSFunction.js" />
<Content Include="Includes\Bootstrap\Dynamic\Classes\JSIL.JsObject.js" />
<Content Include="Includes\Bootstrap\Dynamic\Classes\Microsoft.CSharp.RuntimeBinder.Binder.js" />
<Content Include="Includes\Bootstrap\Dynamic\Enums\Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags.js" />
<Content Include="Includes\Bootstrap\Dynamic\Enums\System.Linq.Expressions.ExpressionType.js" />
Expand Down
65 changes: 23 additions & 42 deletions JSIL/ILBlockTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,61 +848,42 @@ JSExpression[] arguments
JSExpression commaFirstClause = null;
IDictionary<string, JSExpression> argumentsDict = null;

if (arguments.Length > 1) {
var argumentsExpression = arguments[1];
var argumentsArray = argumentsExpression as JSNewArrayExpression;

if (method == null || method.Method.Parameters[1].ParameterType is GenericParameter) {
// This call was made dynamically or through generic version of method, so the parameters are not an array.

argumentsDict = new Dictionary<string, JSExpression>();

for (var i = 0; i < (arguments.Length - 1); i++)
argumentsDict.Add(String.Format("{0}", i), arguments[i + 1]);
} else if (argumentsArray == null) {
// The array is static so we need to pull elements out of it after assigning it a name.
// FIXME: Only handles up to 40 elements.
var argumentsExpressionType = argumentsExpression.GetActualType(TypeSystem);
var temporaryVariable = MakeTemporaryVariable(argumentsExpressionType);
var temporaryAssignment = new JSBinaryOperatorExpression(JSOperator.Assignment, temporaryVariable, argumentsExpression, argumentsExpressionType);

commaFirstClause = temporaryAssignment;

argumentsDict = new Dictionary<string, JSExpression>();

for (var i = 0; i < 40; i++)
argumentsDict.Add(String.Format("{0}", i), new JSIndexerExpression(temporaryVariable, JSLiteral.New(i)));
} else {
var argumentsArrayExpression = argumentsArray.SizeOrArrayInitializer as JSArrayExpression;

if (argumentsArrayExpression == null)
throw new NotImplementedException("Literal array must have values");

argumentsDict = new Dictionary<string, JSExpression>();

int i = 0;
foreach (var value in argumentsArrayExpression.Values) {
argumentsDict.Add(String.Format("{0}", i), value);
if (arguments.Length > 1)
{
argumentsDict = new Dictionary<string, JSExpression>();

i += 1;
}
}
for (var i = 0; i < (arguments.Length - 1); i++)
argumentsDict.Add(String.Format("{0}", i), arguments[i + 1]);
}

var verbatimLiteral = new JSVerbatimLiteral(
methodName, expression.Value, argumentsDict
);

if (commaFirstClause != null)
return new JSCommaExpression(commaFirstClause, verbatimLiteral);
else
return verbatimLiteral;
return verbatimLiteral;
} else {
throw new NotImplementedException("Verbatim method not implemented: " + methodName);
}
break;
}

case "JSIL.JSObject":
{
if (methodName == "Global")
{
var expression = arguments[0] as JSStringLiteral;
if (expression != null)
return new JSDotExpression(
JSIL.GlobalNamespace, new JSStringIdentifier(expression.Value, TypeSystem.Object, true)
);
else
return new JSIndexerExpression(
JSIL.GlobalNamespace, arguments[0], TypeSystem.Object
);
}
break;
}

case "JSIL.JSGlobal": {
if (methodName == "get_Item") {
var expression = arguments[0] as JSStringLiteral;
Expand Down
2 changes: 1 addition & 1 deletion Meta
Submodule Meta updated 4 files
+12 −12 Builtins.cs
+247 −0 JSObject.cs
+1 −0 Meta.csproj
+27 −182 Verbatim.cs
Binary file removed Tests/BinaryTestCases/DynamicComplex.exe
Binary file not shown.
18 changes: 0 additions & 18 deletions Tests/FailingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,5 @@ public void FailingTestCases (object[] parameters) {
protected IEnumerable<TestCaseData> FailingTestCasesSource () {
return FolderTestSource("FailingTestCases", MakeDefaultProvider(), new AssemblyCache());
}

[Test]
public void VerbatimDynamic()
{
try
{
var js = GetJavascript(
@"SpecialTestCases\Issue548.cs",
"{\"obj1\":\"{}\"}"
);
}
catch (Exception)
{
return;
}

Assert.Fail("Test passed when it should have failed");
}
}
}
13 changes: 0 additions & 13 deletions Tests/MetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,19 +450,6 @@ public void ComplexDynamics () {
}
}

// Mono compiles this to different IL.
[Test]
public void ComplexDynamicsMonoBinary () {
try {
GetJavascript(
@"BinaryTestCases\DynamicComplex.exe"
);

Assert.Fail("Translated JS ran successfully");
} catch (JavaScriptEvaluatorException jse) {
Assert.IsTrue(jse.ToString().Contains("TypeError: realTarget is undefined"), jse.ToString());
}
}

// Mono generates really weird control flow for this
[Test]
Expand Down
7 changes: 4 additions & 3 deletions Tests/SpecialTestCases/Eval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

public static class Program {
public static int Foo () {
var result = Builtins.Eval("2") ?? 1;

return (int)result;
if (Builtins.IsJavascript) {
return Builtins.Eval("2").AssumeType<int>();
}
return 1;
}

public static void Main (string[] args) {
Expand Down
23 changes: 13 additions & 10 deletions Tests/SpecialTestCases/IndexBuiltinByName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@

public static class Program {
public static void Main (string[] args) {
const string pri = "pri";
string nt = "nt";
if (Builtins.IsJavascript)
{
const string pri = "pri";
string nt = "nt";

var p = Builtins.Global[pri + nt] as dynamic;
if (p != null)
p("printed");
var p = Builtins.Global[pri + nt] as dynamic;
if (p != null)
p("printed");

if (Builtins.Local["p"] != null)
(Builtins.Local["p"] as dynamic)("printed again");
if (Builtins.Local["p"] != null)
(Builtins.Local["p"] as dynamic)("printed again");

var q = Builtins.Global["quit"] as dynamic;
if (q != null)
q();
var q = Builtins.Global["quit"] as dynamic;
if (q != null)
q();
}

Console.WriteLine("test");
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/SpecialTestCases/Verbatim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using JSIL;

public static class Program {
public static int Add (int a, int b) {
return Verbatim.Expression<int>("a + b");
public static int Add (int a, int b)
{
return Builtins.IsJavascript ? Verbatim.Expression("a + b").AssumeType<int>() : 0;
}

public static void Main (string[] args) {
Console.WriteLine(Add(1, 3));
Verbatim.Expression("return");
if (Builtins.IsJavascript)
Verbatim.Expression("return");
Console.WriteLine(2);
}
}
2 changes: 1 addition & 1 deletion Tests/SpecialTestCases/VerbatimThis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
public static class Program {
public class CustomType {
public CustomType () {
Console.WriteLine("{0}", Builtins.This ?? "");
Console.WriteLine("{0}", Builtins.This);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SpecialTestCases/VerbatimVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static void Main (string[] args) {
var a = 2;
var b = 5;
Console.WriteLine(Verbatim.Expression("$0 + $1", a, b));
int i = Verbatim.Expression<int>("$0 + $1", a, b);
int i = Verbatim.Expression("$0 + $1", a, b).AssumeType<int>();
Console.WriteLine(i);
}
}
12 changes: 0 additions & 12 deletions Tests/SpecialTestCases/VerbatimVariablesExistingArray.cs

This file was deleted.

2 changes: 0 additions & 2 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@
<None Include="InterfaceTestCases\InterfaceOverloads2.cs" />
<None Include="PerformanceTestCases\Sieve.cs" />
<None Include="SpecialTestCases\VerbatimVariables.cs" />
<None Include="SpecialTestCases\VerbatimVariablesExistingArray.cs" />
<None Include="ReflectionTestCases\Issue274_1.cs" />
<None Include="ReflectionTestCases\Issue274_2.cs" />
<None Include="PerformanceTestCases\FuseePackedVertices.cs" />
Expand Down Expand Up @@ -621,7 +620,6 @@
<None Include="TestCases\EnumIfStatement.cs" />
<None Include="TestCases\DelegateResultWithConstraints.cs" />
<None Include="TestCases\GenericNestedTypeConstructedInParentStaticConstructor.cs" />
<None Include="BinaryTestCases\DynamicComplex.exe" />
<None Include="TestCases\FailsOnMono\Events.vb" />
<None Include="TestCases\GenericMethodThisReference.cs" />
<None Include="TestCases\StringEmpty.cs" />
Expand Down
13 changes: 7 additions & 6 deletions Tests/VerbatimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void VerbatimIsEmittedRawInGeneratedJavascript () {

[Test]
public void BuiltinsThisEvaluatesToJSThis () {
GenericTest(
GetJavascript(
@"SpecialTestCases\VerbatimThis.cs",
"", "Program+CustomType"
"Program+CustomType"
);
}

Expand Down Expand Up @@ -72,11 +72,12 @@ public void VerbatimVariables () {
}

[Test]
public void VerbatimVariablesExistingArray () {
public void VerbatimDynamic()
{
var js = GetJavascript(
@"SpecialTestCases\VerbatimVariablesExistingArray.cs",
"hello\r\n7"
);
@"SpecialTestCases\Issue548.cs",
"{\"obj1\":\"{}\"}"
);
}
}
}

0 comments on commit 5838f04

Please sign in to comment.