No announcement yet.

Custom external javascript class structure

  • Filter
  • Time
  • Show
Clear All
new posts

    Custom external javascript class structure


    I'm trying to use as a transpiler to generate javascript code calling external functions which have a complicated structure ( for Screeps game if interested). E.g. for example a common javascript call would be

    Now I can generate this via Script.Call("Game.spawns.spawn1.createCreep",...) but this can only use static name spawn1. I'm trying to create a parallel class structure in c# for the external library so that a call to Game.Spawns("spawn1").createCreep(...) would dynamically generate the call above in JS.

    I've been first toying with the idea of using Script.Write(str) with str = prefix() + ".createCreep" and prefix is a function to construct the Game.spawns.spawn1 string but Script.Write seems to only work with very simple inlines and in any case prefix() can be only resolved at runtime so that doesn't seem like the way to proceed.

    I've looked around the forum and saw some notice of extension methods, does that sound like it could be applicable to above?

    In a sense, it seems what I really would like is to extend to generate customised javascript, is that plausible? But the javascript is not fully static so it starts to look more of a dynamic recompilation rather than static transpilation so wanted to hear some thoughts on whether that's even possible or I'd be better off writing my own solution instead of heavily readapting
    Last edited by Daniil; 2015-08-19 @ 06:38 AM. Reason: Formatted with [CODE] and [VAR] tags

    Hello zedr0n,

    Welcome to Bridge!

    Can you post a couple code samples demonstrating how you would like to configure the C#, and what you would like the JavaScript to look like? This might help us understand your requirements.

    Edit: Looks like fabricio.murta beat me to the first response.
    Last edited by geoffrey.mcgill; 2015-08-19 @ 12:14 AM.



      For some reason, fabricio.murta's post doesn't appear. I am reposting it here taking from the email notification:

      Hello zedr0n! Welcome to Bridge.NET forums!

      I read from your description: A framework to support the Screeps Game javascript library.

      Like we have Bridge.jQuery and Bridge.Bootstrap framework supports and are on the way to do Bridge.AngularJS, this really looks like what you need to have working.

      It may sound complicated to start with but might get very handy as you progress with a project using your JavaScript library. I'll share some points, and you can try and implement those mappings in your specific project. Once you have a good set of classes mapping correctly to your library, we would love to turn it into an officially supported framework for Bridge.

      In fact, you can do this yourself right now, as Bridge is open source. There would be a learning curve to get thru, of course. If you are interested in directions of making the framework support per se, just let us know, I already have some information I can share to help you get started (before we have a full-fledged knowledgebase article about it).

      Now, to what you would need in general terms:
      - As you are representing classes on Screeps Game library, you'll want C# representations/stubs for the classes
      - As some code sequence might not follow the JavaScript syntax in some manner, a mapping Template might be used
      - As you already have the classes defined in Screeps Game lib, you won't want Bridge.NET to output them and override/conflict with the lib, so you need to instruct Bridge to Ignore it when code is being transcribed from C# to JS.

      So, for your example you would need a class like this:
      namespace Game
          [Ignore] // important! this will make your class C#-only
          public static class Spawns
              [Template("Game.spawns.{ spawnName:raw}.createCreep({name})")]
              public static void CreateCreep(string spawnName, string name)
      So when you, in C#:
      Game.Spawns.CreateCreep("spawn1", "myCreep");
      You will get in JavaScript:
      There should be better approaches to refer to the 'spawn1' name you pointed, this is just a generic example showcasing some possibilities.

      And this is the overall guidelines. Much can come once this is done, and this is to some extent the spirit of framework support we are to implement on Bridge.NET.

      As for the extension methods, it makes more sense when you already have something stubbed on Bridge and want to extend its functionality. For example, if you already have the Element class to denote HTML5 elements but the specific library adds, say, an specific attribute it needs, then you can create an extension to get/set/del this attribute to any class inheriting from Element (any HTML5 tag) so you can implement support for your library's functionality. If we don't have a class that represents the game, there's nothing to extend then, so a blank stub class must be written.

      I hope that helps. Whichever way you are interested in progressing (a full-fledged framework support or just individual stub classes for your project), count on us to help you make it come true!
      Last edited by Daniil; 2015-08-19 @ 06:36 AM.


        Thanks for the detailed response, that sounds quite flexible and I'll try to tinker with it and come back with questions if or rather when I run into more complications.

        Actually, I do have one already. Do you have some more info on Template format string syntax? Can I use member variables? ( can these only be static? ) Are arrays possible? E.g. I would want something like:

        public void createCreep(string[] props, string name)
        [Template("{ prefix : raw}.createCreep({props}, {name})")]
        private void _createCreep(string prefix, string[] props, string name)
        public string prefix()
          return basePrefix+name+".";

        string[] props = { "WORK", "MOVE", "CARRY" };
        Spawn spawn = new Spawn("Game.spawns.","spawn1"); // the first is base prefix dynamically generated by parent classes in real code
        should output JS
        Game.spawns.spawn1.createCreep( [WORK, MOVE, CARRY], 'Worker1')
        Last edited by zedr0n; 2015-08-19 @ 07:39 AM.


          Hello again zedr0n! Glad you liked the approach.

          About the example, there are some situations where you expand variables but, basically, when they are constants.

          In the sample you suggested, what you are going to have in JavaScript is something similar to:
          var props = [ "WORK", "MOVE", "CARRY" ];
          Game.spawns.spawn1(props, "Worker1");
          I believe this would work just as same as you wanted. I am not really sure with the [InlineConst] property will give you a good alternative, as a const... is a const, and you would not be able to just freely determine what you want to pass to the method.

          InlineConst just makes it so, suppose it decorates the props definition, whenever you refer it on the C# code, its -value- will be output insted of the js-equivalent variable, thus resulting in Game.spawns.spawn1([ "WORK", "MOVE", "CARRY" ]);.

          As for documentation on the Template attribute, I'll try to push forward the publication of an article documenting every supported attribute in Bridge. As of now, you can see many examples of its usage on Bridge.NET public sources. This is one example on the Byte class.

          Meanwhile, while the browseable samples above don't help, you can ask us for suggestions on how to attain a given mapping. Providing an simplified example of the code you want to run would help a lot to ensure the results can be attained in the replies we can provide you with.


            That's fine. As long as i can use the member variables of the class ( e.g. prefix = Game.spawns.spawn1 in this case ) it should be enough for me. I was mostly wondering if dynamic runtime variables are going to be processed correctly by the mapping template. I.e. the Spawn object might have been created in some other place with different name, e.g. spawn5 so theoretically the code would need to be executed to determine the token after Game.spawns. when generating JS output
            Last edited by zedr0n; 2015-08-19 @ 09:42 PM.


              Hello zedr0n! Really sorry for taking so long to get back to you!

              I'm not quite sure I got it but, looks like Game.spawns.spawn1 will be a 'spawn1' instance of the Game.spawns, right? As if you did var spawn1 = new Game.Spawns.SpawnType();. If you had a full html+js (yet simple, just demonstrating the usage so we can understand in a shorter time), maybe we could try with you approaches to better support. There are a lot of questions on my head... For example, if the Game.spawns space is from the javascript framework (and not something you defined in your specific javascript code), if there are a fixed amount of spawns defined (spawn1..4, for example), or if you really needed once to instantiate that spawn1... What you needed in your JavaScript side in order to have the spawn defined. In other words, what should Bridge.NET emit as JavaScript and what Bridge should keep just on the C# side (to allow strongly typed references+intellisense in C# side).

              On a side subject, you asked about documentation on the [Template()] attribute, I pointed you to some files using it and told you about a KnowledgeBase article we were willing to release soon. It still needs some improvement on their examples and formatting, but is pretty complete (new attributes could have been added in the past few days, and some are subject to be added soon). The article is this: Bridge.NET Attributes Reference.

              I hope this helps, and thanks again for your interest in Bridge.NET. Your game project seems very interesting and we're looking forward to help you get great results with it!


                No problem, I've done a bit more work on this and it seems to be possible to get it working so thanks for all your help. It's not my game project unfortunately, I'm just writing a kind of c# client framework for my own use as there's an API for uploading your own javascript so it seems to fit quite well.

                For now i'm waiting for strict mode support ( as the game runs all its JS code in strict ) and the latest fixes to document usage I've seen on the branch before delving into it further.

                In the meantime I've tried to integrate with Awesomium.NET to have pure C# cross-platform Bootstrap GUI for applications which seems to be working OK but trying to make the interaction between the two more seamless, I will share some code if anything interesting comes out of it.


                  I've tried Screeps now (played the tutorial). One question you already answered: "...there's an API for uploading your own javascript...", and this itself raises another question about interoperability.

                  Bridge needs bridge.js to work. It acts as a 'CLR wrapper' and is required in any usage of bridge-built JavaScript. As I saw, you would need to define a module and add bridge.js to it, or include bridge.js from outside, as all javascript is imported and run by the screeps server (if I got it correctly). I believe including bridge.js would be a problem, although the referencing to the Game.creeps and Game.spawns classes should not be a problem (once including bridge.js is sorted out).

                  Well, if you can just upload bridge.js and make it load before the other bridge scripts, then you should get it working fine.

                  Let us know when you try it more with bridge.

                  p.s.: looks awesome! If/once you get anything else to share about it, please don't hesitate. I just ask you, if you don't mind, opening a new thread so we keep different discussions on different threads. :)


                    Screeps supports JS modules so I've managed to make it load bridge.js but unfortunately got into a few issues because of strict JS mode - opened another thread about that in feature requests before.


                      Just for cross references - a link to a feature request forum thread regarding the strict JavaScript mode.