I'm trying to get started with JSIL. I've followed the directions as far as I understand. I have a very basic C# dummy project with the code:
namespace TestLib
{
public class MagicType
{
public int Multiply(int x, int y)
{ // test instance method
return x * y;
}
public static int Add(int x, int y)
{// test static int method
return x + y;
}
}
}
I've compiled this with jsilc
, and created a website that hosts this along with the jsil scripts. My html initializes this:
<script type="text/javascript">
var jsilConfig = {
libraryRoot: '/js/jsil/',
scriptRoot: '/js/testlib/',
manifestRoot: '/js/testlib/',
manifests: ['TestLib.dll'] // gets as far as Loading '/js/testlib/TestLib.dll.manifest.js'...
// which is 200 / OK
};
var asm = null;
var runMain = function () { // doesn't get invoked
console.log('> main');
asm = JSIL.GetAssembly("TestLib", true); // (executed outside method) returns a stub with no content
console.log('< main');
};
</script>
<script src="/js/jsil/jsil.js"></script>
but... I can't access the library. The console output indicates that it is loading:
Loading '/js/testlib/TestLib.dll.manifest.js'...
which is a 200 OK. However, I can't access it. If I run:
var asm = JSIL.GetAssembly("TestLib", true);
then I get back a stub to the library, but it doesn't have anything in it. There's a asm.$typesByName
, but that is an empty object. What I want to do (to see it work) is to call the Multiply
and Add
methods.
So: what am I missing? my intent is to host a transpiled library that I can access through js, which is as I understand it: possible. I just can't make it work. I have uploaded my entire test project here: https://github.com/mgravell/jsilfun
The missing piece is that JSIL's browser layer performs loading in two stages: There's the bootstrapper, which you've loaded successfully and has loaded your manifests, and then the browser scaffold, which sets up the DOM interfaces and actually executes the assemblies you've loaded. This occurs in two stages since so many things are asynchronous and you might have a reason to load additional assemblies on your own or do other work before actually running all the code.
The browser scaffold exposes a function called onLoad
(yeah, sorry) that you can call to perform the second stage. The examples all do this with <body onload="onLoad()">
or something similar, but you can call it any way you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With