Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a basic JSIL library (from C#) in a web page?

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

like image 305
Marc Gravell Avatar asked May 26 '17 19:05

Marc Gravell


1 Answers

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.

like image 64
Katelyn Gadd Avatar answered Oct 23 '22 00:10

Katelyn Gadd