Skip to content

Commit

Permalink
Fixing JsInline issues. Adding test and support for [JsInline] on pro…
Browse files Browse the repository at this point in the history
…perty getters. Renaming JsScript to Global and using a static class here so that users do not have to derive from JsScript. Now access to the Window or Document properties are thru the Global static class. Perhaps there is a better way to do this
  • Loading branch information
Frank Laub committed Jan 5, 2010
1 parent af76db3 commit ffaaa35
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/DotWeb.Client/DotWeb.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<Compile Include="Dom\Css\Style.cs" />
<Compile Include="Dom\Window.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="JsScript.cs" />
<Compile Include="Global.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotWeb.System\DotWeb.System.csproj">
Expand Down
10 changes: 5 additions & 5 deletions src/DotWeb.Client/JsScript.cs → src/DotWeb.Client/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@

namespace DotWeb.Client
{
public class JsScript
public static class Global
{
public extern Window Window {
[JsCode("return $wnd;")]
public static extern Window Window {
[JsInline("$wnd")]
get;
}

public extern HtmlDocument Document {
[JsCode("return $doc;")]
public static extern HtmlDocument Document {
[JsInline("$doc")]
get;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/DotWeb.Hosting.Bridge/JsFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ private string GenerateFunctionBody(MethodBase method) {
}

private string GenerateInlineBody(MethodBase method, string format) {
if (method.IsSpecialName) {
if (method.Name.StartsWith("get_")) {
return "return " + format + ";";
}
}

var parameters = method.GetParameters();
var args = parameters.Select(x => x.Name).ToArray();
return string.Format(format, args) + ";";
Expand Down
8 changes: 8 additions & 0 deletions src/DotWeb.Translator/Generator/JavaScript/JsPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ public string VisitReturn(CodeLengthReference expr) {
}

public string VisitReturn(CodePropertyReference exp) {
// deal with [JsInline]
var method = exp.Method.Reference.Resolve();
string jsInline = AttributeHelper.GetJsInline(method);
if (jsInline != null) {
Debug.Assert(exp.ReferenceType == CodePropertyReference.RefType.Get);
return jsInline;
}

if (exp.IsFieldLike()) {
return string.Format("{0}.{1}", Print(exp.TargetObject), EncodeName(exp.Property.Name));
}
Expand Down
5 changes: 4 additions & 1 deletion src/DotWeb.Translator/TranslationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private CodeMethodMember Parse(MethodDefinition method) {

if (!method.HasBody || method.Body.Instructions.Count == 0) {
string msg = string.Format(
"{0}\nA method marked extern must either have [JsCode] or be declared in a type derived from JsObject.",
"{0}\nA method marked extern must either have [JsCode], [JsInline], or be declared in a type derived from JsObject.",
method
);
throw new MissingMethodException(msg);
Expand Down Expand Up @@ -184,6 +184,9 @@ private bool IsEmittable(TypeDefinition type) {
}

private bool IsEmittable(MethodDefinition method) {
if (AttributeHelper.GetJsInline(method) != null)
return false;

var type = method.DeclaringType;
return IsEmittable(type);
}
Expand Down
4 changes: 2 additions & 2 deletions test/DotWeb.Hosting.Test.Script/JsBridgeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ public DelegateWrapperTest() {
}
}

public class CastInterfaceTest : JsScript
public class CastInterfaceTest
{
public CastInterfaceTest() {
var window = Window;
var window = Global.Window;
var doc = window.document;
var element = doc.getElementById("box");
var box = (HtmlDivElement)element;
Expand Down
6 changes: 6 additions & 0 deletions test/DotWeb.Hosting.Test.Script/JsFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//
using DotWeb.Client;
using System.DotWeb;
using DotWeb.Client.Dom;

namespace DotWeb.Hosting.Test.Script
{
Expand All @@ -34,6 +35,11 @@ public void TestJsCode() { }
[JsInline("alert({0})")]
public void TestJsInline(string arg) { }

public extern Window Window {
[JsInline("$wnd")]
get;
}

public void TestNoArgs() { }
public void TestOneArg(int x) { }
public void TestTwoArgs(int x, int y) { }
Expand Down
8 changes: 4 additions & 4 deletions test/DotWeb.Hosting.Test/JsBridgeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2009, Frank Laub
 // Copyright 2009, Frank Laub
//
// This file is part of DotWeb.
//
Expand Down Expand Up @@ -252,7 +252,7 @@ public void TestObjectWrapper() {
public void TestCastInterface() {
var loadType = this.asm.GetType("DotWeb.Hosting.Test.Script.CastInterfaceTest");
var asmClient = Assembly.Load("Hosted-DotWeb.Client");
var jsScriptType = asmClient.GetType("DotWeb.Client.JsScript");
var globalType = asmClient.GetType("DotWeb.Client.Global");
var windowType = asmClient.GetType("DotWeb.Client.Dom.Window");
var documentType = asmClient.GetType("DotWeb.Client.Dom.Document");
var htmlElementType = asmClient.GetType("DotWeb.Client.Dom.Html.HtmlElement");
Expand All @@ -265,8 +265,8 @@ public void TestCastInterface() {

//var element = Window.document.getElementById("box");

//var window = Window;
var window = session.DefineFunctionMessage(jsScriptType.GetMethod("get_Window"));
//var window = Global.Window;
var window = session.DefineFunctionMessage(globalType.GetMethod("get_Window"));
session.InvokeFunctionMessage(window.Name, 0);
var windowId = ++remoteId;
session.OnReturnMessage(false, JsValueType.JsObject, windowId);
Expand Down
5 changes: 5 additions & 0 deletions test/DotWeb.Hosting.Test/JsFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public void TestJsInline() {
"__DotWeb_Hosting_Test_Script_CrashTestDummy$TestJsInline",
"arg",
"alert(arg);");
RunTest(
"get_Window",
"__DotWeb_Hosting_Test_Script_CrashTestDummy$get_Window",
"",
"return $wnd;");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
$Namespace('DotWeb.Client');

DotWeb.Client.JsScript = function() {
};

DotWeb.Client.JsScript.prototype.get_Window = function() {
return $wnd;
};

$Namespace('H8');
$Namespace('H8');

H8.DecorationTests = function() {
this.$super.constructor();
};
H8.DecorationTests.$extend(DotWeb.Client.JsScript);

H8.DecorationTests.prototype.box_OnMouseOver = function(evt /*DotWeb.Client.Dom.Events.MouseEvent*/) {
};

H8.DecorationTests.prototype.TestCastInterface = function() {
var element = this.get_Window().document.getElementById("box");
var element = $doc.getElementById("box");
var box = /*(DotWeb.Client.Dom.Html.HtmlDivElement)*/element;
box.onmouseover = $Delegate(this, this.box_OnMouseOver);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ System.Console.WriteLine = function(value /*System.Object*/) {
console.log(value);
};

$Namespace('DotWeb.Client');

DotWeb.Client.JsScript = function() {
};

$Namespace('H8');

H8.DecorationTests = function() {
this.$super.constructor();
};
H8.DecorationTests.$extend(DotWeb.Client.JsScript);

H8.DecorationTests.prototype.TestJsAnonymous = function() {
var __g__initLocal0 = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
H8.DecorationTests.prototype.TestJsInline = function() {
jQuery("*");
H8.DecorationTests.TakeJQuery(jQuery("#id"));
System.Console.WriteLine($doc.getElementById("id"));
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,8 @@ System.Console.Write = function(value /*System.Int32*/) {
console.log(value);
};

$Namespace('DotWeb.Client');

DotWeb.Client.JsScript = function() {
};

H8.DecorationTests = function() {
this.$super.constructor();
};
H8.DecorationTests.$extend(DotWeb.Client.JsScript);

H8.DecorationTests.prototype.TestJsIntrinsic = function() {
var __g__initLocal3 = new H8.IntrinsicClass().$ctor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,10 @@ System.Console.WriteLine = function(value /*System.Object*/) {
console.log(value);
};

$Namespace('DotWeb.Client');

DotWeb.Client.JsScript = function() {
};

$Namespace('H8');

H8.DecorationTests = function() {
this.$super.constructor();
};
H8.DecorationTests.$extend(DotWeb.Client.JsScript);

H8.DecorationTests.prototype.TestJsNamespace = function() {
var __g__initLocal5 = new DefaultNamespaceTest().$ctor();
Expand Down
5 changes: 3 additions & 2 deletions test/DotWeb.Translator.Test/Script/DecorationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FooNamespaceTest
public int Value { get; set; }
}

public class DecorationTests : JsScript
public class DecorationTests
{
[JsCode("alert(arg);")]
public void TestJsCode(string arg) {
Expand All @@ -95,6 +95,7 @@ public void TestJsCode(string arg) {
public void TestJsInline() {
JQuery("*");
TakeJQuery(JQuery("#id"));
Console.WriteLine(Global.Document.getElementById("id"));
}

public void TestJsAnonymous() {
Expand Down Expand Up @@ -152,7 +153,7 @@ public void TestJsNamespace() {
}

public void TestCastInterface() {
var element = Window.document.getElementById("box");
var element = Global.Document.getElementById("box");
var box = (HtmlDivElement)element;
box.onmouseover = box_OnMouseOver;
}
Expand Down
2 changes: 1 addition & 1 deletion test/DotWeb.Weaver.Test.Script/NativeCallbackTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class NativeObject : JsObject
public extern void NativeCall();
}

public class NativeCallback : JsScript
public class NativeCallback
{
public NativeCallback() {
Console.Write("Hi");
Expand Down
2 changes: 1 addition & 1 deletion test/DotWeb.Weaver.Test.Script/TypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace DotWeb.Weaver.Test.Script
{
class TypeTest : JsScript
class TypeTest
{
public int field;
public int Property { get; set; }
Expand Down

0 comments on commit ffaaa35

Please sign in to comment.