Skip to content

Commit

Permalink
Mannux sample works again.
Browse files Browse the repository at this point in the history
When there is only one overload of a method with a given argument count, use fast dispatch instead of signature-based dispatch.
Fix some copy-and-paste errors from skeletons.
  • Loading branch information
kg committed Apr 11, 2012
1 parent b0f6509 commit 184f044
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 421 deletions.
19 changes: 19 additions & 0 deletions JSIL/JavascriptAstEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,9 +1144,28 @@ public void VisitNode (JSInvocationExpression invocation) {
Output.RPar();
};

// If there's only one overload with this argument count, we don't need to use
// the expensive overloaded method dispatch path.
if (isOverloaded) {
MethodSignatureSet mss;
if (method.DeclaringType.MethodSignatures.TryGet(method.Name, out mss)) {
int argCount = method.Parameters.Length;
int overloadCount = 0;

foreach (var signature in mss.Signatures) {
if (signature.ParameterCount == argCount)
overloadCount += 1;
}

if (overloadCount < 2)
isOverloaded = false;
}
}

var oldInvoking = ReferenceContext.InvokingMethod;
try {
if (isOverloaded) {

var methodName = Util.EscapeIdentifier(method.GetName(true), EscapingMode.MemberIdentifier);

Output.MethodSignature(jsm.Reference, method.Signature, ReferenceContext);
Expand Down
6 changes: 6 additions & 0 deletions JSIL/MethodSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public MethodSignatureSet () {
);
}

public IEnumerable<MethodSignature> Signatures {
get {
return Counts.Keys;
}
}

public void Add (MethodSignature signature) {
var count = Counts.GetOrCreate(
signature, () => new Count()
Expand Down
6 changes: 6 additions & 0 deletions JSIL/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ public void Clear () {
States.Clear();
}

public IEnumerable<TKey> Keys {
get {
return Storage.Keys;
}
}

public bool MightContainKey (TKey key) {
return Storage.ContainsKey(key) || States.ContainsKey(key);
}
Expand Down
58 changes: 34 additions & 24 deletions Libraries/JSIL.Bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ JSIL.ImplementExternals(
);

$.Method({Static:false, Public:true }, ".ctor",
(new JSIL.MethodSignature(null, [$.String, $asms[5].TypeRef("System.Exception")], [])),
(new JSIL.MethodSignature(null, [$.String, mscorlib.TypeRef("System.Exception")], [])),
function _ctor (message, innerException) {
this._message = message;
this._innerException = innerException;
}
);

$.Method({Static:false, Public:true }, "get_InnerException",
(new JSIL.MethodSignature($asms[5].TypeRef("System.Exception"), [], [])),
(new JSIL.MethodSignature(mscorlib.TypeRef("System.Exception"), [], [])),
function get_InnerException () {
return this._innerException;
}
Expand Down Expand Up @@ -762,13 +762,10 @@ JSIL.MakeClass("System.Object", "System.Threading.Thread", true, [], function ($
$.Property({Public: true , Static: true }, "ManagedThreadId");
});

$jsilcore.$ListExternals = function ($, isArrayList) {
$jsilcore.$ListExternals = function ($, T, isArrayList) {
var mscorlib = JSIL.GetCorlib();

var T;
if (isArrayList) {
T = $.Object;
} else {
if (typeof (T) === "undefined") {
T = new JSIL.GenericParameter("T", "System.Collections.Generic.List`1");
}

Expand Down Expand Up @@ -993,16 +990,25 @@ $jsilcore.$ListExternals = function ($, isArrayList) {
indexOfImpl
);

$.Method({Static:false, Public:true }, "Remove",
new JSIL.MethodSignature(mscorlib.TypeRef("System.Boolean"), [T], []),
function (item) {
var index = this._items.indexOf(item);
if (index === -1)
return false;
var removeImpl = function (item) {
var index = this._items.indexOf(item);
if (index === -1)
return false;

this.RemoveAt(index);
}
);
return this.RemoveAt(index);
};

if (isArrayList) {
$.Method({Static:false, Public:true }, "Remove",
new JSIL.MethodSignature(null, [T], []),
removeImpl
);
} else {
$.Method({Static:false, Public:true }, "Remove",
new JSIL.MethodSignature(mscorlib.TypeRef("System.Boolean"), [T], []),
removeImpl
);
}

$.Method({Static:false, Public:true }, "RemoveAll",
new JSIL.MethodSignature(mscorlib.TypeRef("System.Int32"), [mscorlib.TypeRef("System.Predicate`1", [T])], []),
Expand All @@ -1024,6 +1030,7 @@ $jsilcore.$ListExternals = function ($, isArrayList) {
function (index) {
this._items.splice(index, 1);
this._size -= 1;
return true;
}
);

Expand Down Expand Up @@ -1064,11 +1071,11 @@ $jsilcore.$ListExternals = function ($, isArrayList) {
};

JSIL.ImplementExternals("System.Collections.Generic.List`1", function ($) {
$jsilcore.$ListExternals($, false);
$jsilcore.$ListExternals($);
});

$jsilcore.$ArrayListExternals = function ($) {
$jsilcore.$ListExternals($, true);
$jsilcore.$ListExternals($, $.Object, true);

var mscorlib = JSIL.GetCorlib();
var toArrayImpl = function () {
Expand All @@ -1090,7 +1097,7 @@ $jsilcore.$ArrayListExternals = function ($) {
JSIL.ImplementExternals("System.Collections.ArrayList", $jsilcore.$ArrayListExternals);

$jsilcore.$CollectionExternals = function ($) {
$jsilcore.$ListExternals($, false);
$jsilcore.$ListExternals($);

var mscorlib = JSIL.GetCorlib();

Expand All @@ -1114,7 +1121,7 @@ $jsilcore.$CollectionExternals = function ($) {
JSIL.ImplementExternals("System.Collections.ObjectModel.Collection`1", $jsilcore.$CollectionExternals);

$jsilcore.$ReadOnlyCollectionExternals = function ($) {
$jsilcore.$ListExternals($, false);
$jsilcore.$ListExternals($);

var mscorlib = JSIL.GetCorlib();

Expand Down Expand Up @@ -1905,6 +1912,8 @@ JSIL.MakeStruct("System.ValueType", "System.TimeSpan", true, [], function ($) {
});

JSIL.ImplementExternals("System.Collections.Generic.Dictionary`2", function ($) {
var mscorlib = JSIL.GetCorlib();

$.RawMethod(false, "$getHash", function (key) {
if ((typeof (key) !== "undefined") && (key !== null) && (typeof (key.GetHashCode) === "function") && (key.GetHashCode.__IsPlaceholder__ !== true)) {
return key.GetHashCode();
Expand Down Expand Up @@ -2025,7 +2034,7 @@ JSIL.ImplementExternals("System.Collections.Generic.Dictionary`2", function ($)
);

$.Method({Static:false, Public:true }, "get_Keys",
(new JSIL.MethodSignature($asms[5].TypeRef("System.Collections.Generic.Dictionary`2/KeyCollection", [new JSIL.GenericParameter("TKey", "System.Collections.Generic.Dictionary`2"), new JSIL.GenericParameter("TValue", "System.Collections.Generic.Dictionary`2")]), [], [])),
(new JSIL.MethodSignature(mscorlib.TypeRef("System.Collections.Generic.Dictionary`2/KeyCollection", [new JSIL.GenericParameter("TKey", "System.Collections.Generic.Dictionary`2"), new JSIL.GenericParameter("TValue", "System.Collections.Generic.Dictionary`2")]), [], [])),
function get_Keys () {
if (this.tKeysEnumerator === null) {
this.tKeysEnumerator = JSIL.ArrayEnumerator.Of(this.TKey);
Expand All @@ -2051,7 +2060,7 @@ JSIL.ImplementExternals("System.Collections.Generic.Dictionary`2", function ($)
);

$.Method({Static:false, Public:true }, "get_Values",
(new JSIL.MethodSignature($asms[5].TypeRef("System.Collections.Generic.Dictionary`2/ValueCollection", [new JSIL.GenericParameter("TKey", "System.Collections.Generic.Dictionary`2"), new JSIL.GenericParameter("TValue", "System.Collections.Generic.Dictionary`2")]), [], [])),
(new JSIL.MethodSignature(mscorlib.TypeRef("System.Collections.Generic.Dictionary`2/ValueCollection", [new JSIL.GenericParameter("TKey", "System.Collections.Generic.Dictionary`2"), new JSIL.GenericParameter("TValue", "System.Collections.Generic.Dictionary`2")]), [], [])),
function get_Values () {
if (this.tValuesEnumerator === null) {
this.tValuesEnumerator = JSIL.ArrayEnumerator.Of(this.TValue);
Expand Down Expand Up @@ -2714,7 +2723,7 @@ JSIL.ImplementExternals("System.Diagnostics.StackTrace", function ($) {
);

$.Method({Static:false, Public:true }, "GetFrame",
(new JSIL.MethodSignature($asms[5].TypeRef("System.Diagnostics.StackFrame"), [$.Int32], [])),
(new JSIL.MethodSignature(mscorlib.TypeRef("System.Diagnostics.StackFrame"), [$.Int32], [])),
function GetFrame (index) {
// FIXME
return new System.Diagnostics.StackFrame();
Expand All @@ -2724,6 +2733,7 @@ JSIL.ImplementExternals("System.Diagnostics.StackTrace", function ($) {
});

JSIL.ImplementExternals("System.Diagnostics.StackFrame", function ($) {
var mscorlib = JSIL.GetCorlib();

$.Method({Static:false, Public:true }, ".ctor",
(new JSIL.MethodSignature(null, [], [])),
Expand All @@ -2733,7 +2743,7 @@ JSIL.ImplementExternals("System.Diagnostics.StackFrame", function ($) {
);

$.Method({Static:false, Public:true }, "GetMethod",
(new JSIL.MethodSignature($asms[5].TypeRef("System.Reflection.MethodBase"), [], [])),
(new JSIL.MethodSignature(mscorlib.TypeRef("System.Reflection.MethodBase"), [], [])),
function GetMethod () {
// FIXME
return new System.Reflection.MethodBase();
Expand Down
22 changes: 13 additions & 9 deletions Libraries/JSIL.Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3382,35 +3382,35 @@ JSIL.InterfaceBuilder.prototype.Method = function (_descriptor, methodName, sign
});
};

JSIL.InterfaceBuilder.prototype.InheritDefaultConstructor = function () {
JSIL.InterfaceBuilder.prototype.InheritBaseMethod = function (name) {
var signature = new JSIL.MethodSignature(null, [], []);
var descriptor = this.ParseDescriptor({Public: true, Static: false}, ".ctor", signature);
var descriptor = this.ParseDescriptor({Public: true, Static: false}, name, signature);

var mangledName = signature.GetKey(descriptor.EscapedName);

var fn = [null];

fn[0] = function () {
var proto = Object.getPrototypeOf(this);
var baseCtor;
var baseMethod;

while (true) {
baseCtor = proto[mangledName];
if (baseCtor === fn[0])
baseMethod = proto[mangledName];
if (baseMethod === fn[0])
proto = Object.getPrototypeOf(proto);
else
break;
}

if (typeof (baseCtor) === "function")
baseCtor.call(this);
if (typeof (baseMethod) === "function")
baseMethod.apply(this, arguments);
else
JSIL.Host.warning("InheritDefaultConstructor() used but no default constructor was found to inherit!");
JSIL.Host.warning("InheritBaseMethod() used but no method was found to inherit!");
};

JSIL.SetValueProperty(fn[0], "toString",
function () {
return "<Inherited Default Constructor>";
return "<Inherited " + name + ">";
}
);

Expand All @@ -3423,6 +3423,10 @@ JSIL.InterfaceBuilder.prototype.InheritDefaultConstructor = function () {
});
};

JSIL.InterfaceBuilder.prototype.InheritDefaultConstructor = function () {
this.InheritBaseMethod(".ctor");
};

JSIL.InterfaceBuilder.prototype.ImplementInterfaces = function (/* ...interfacesToImplement */) {
var interfaces = this.typeObject.__Interfaces__;
if (typeof (interfaces) === "undefined")
Expand Down
23 changes: 9 additions & 14 deletions Libraries/JSIL.XNACore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var $asms = new JSIL.AssemblyCollection({
1: "Microsoft.Xna.Framework.Game",
3: "Microsoft.Xna.Framework.Graphics",
5: "mscorlib",
11: "System.Drawing",
15: "System.Windows.Forms",
});

$jsilxna.nextImageId = 0;
Expand Down Expand Up @@ -1875,15 +1877,6 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Vector4", function ($) {
});
});

JSIL.ImplementExternals("Microsoft.Xna.Framework.Matrix", true, {
xScale: 1,
yScale: 1,
zScale: 1,
xTranslation: 0,
yTranslation: 0,
zTranslation: 0
});

JSIL.ImplementExternals("Microsoft.Xna.Framework.Matrix", function ($) {
$.Method({
Static: true,
Expand Down Expand Up @@ -2407,10 +2400,7 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Input.KeyboardState", function
});

JSIL.ImplementExternals("Microsoft.Xna.Framework.Input.Mouse", function ($) {
$.Method({
Static: false,
Public: true
}, "GetState", new JSIL.MethodSignature(null, [], []), function (playerIndex) {
var getStateImpl = function (playerIndex) {
var buttons = JSIL.Host.getHeldButtons();
var position = JSIL.Host.getMousePosition();

Expand All @@ -2425,7 +2415,12 @@ JSIL.ImplementExternals("Microsoft.Xna.Framework.Input.Mouse", function ($) {
(buttons.indexOf(1) >= 0) ? pressed : released,
released, released
);
});
};

$.Method({Static:true , Public:true }, "GetState",
(new JSIL.MethodSignature($asms[0].TypeRef("Microsoft.Xna.Framework.Input.MouseState"), [], [])),
getStateImpl
);
});

JSIL.ImplementExternals("Microsoft.Xna.Framework.Input.MouseState", function ($) {
Expand Down
Loading

0 comments on commit 184f044

Please sign in to comment.