kevingadd/JSIL — Issue #43: System.Threading.Thread initialization in JSIL.Bootrstrap.js
Author: matra774
Created: 2011-12-04 13:35:32+00:00
State: open

JSIL.Bootrstrap.js contains a JS implementation of System.Threading.Thread, but it is not using the Jsil.ImplementExternals, which makes the following test case fail with "The external method 'get_CurrentThread' of type 'System.Threading.Thread' has not been implemented."

static IEnumerable GiveMeSomeInts()
{
yield return 1;
yield return 2;
yield return 3;
}

static void Main(string[] args)
{

int sum = 0;
foreach (var anInt in GiveMeSomeInts())
{
sum += anInt;
}

Assert(6, sum, "foreach/yield");
}

The fix is something along the following:

JSIL.ImplementExternals(
"System.Threading.Thread", false, {
_cctor2 : function () {
// This type already has a cctor, so we add a second one.
System.Threading.Thread._currentThread = new System.Threading.Thread();
},
get_CurrentThread : function () {
return System.Threading.Thread._currentThread;
}
}
);

JSIL.ImplementExternals(
"System.Threading.Thread", true, {
_ctor : function () { },
get_ManagedThreadId : function () { return 0; } // matra774 added
}
);

Please note, that get_ManagedThreadId aslo need to be implemented (it can return 0 as long the JavaScipt is running on single thread, which is the case in today's browser).

The C# compiler generates code similar to this:

this.$initialThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;