jurassic icon indicating copy to clipboard operation
jurassic copied to clipboard

Make global function belong to window object

Open Gdocal opened this issue 8 years ago • 7 comments
trafficstars

Hello. I have code:

function SetError(_if_gp) {
    window.last_error = _if_gp;
}
window.SetError("err#1");

This code works fine in any browser, because all global objects treated as winows properties. Is there anyway to reproduce same behaviour in jurassic library?

I already created new class which represents Window object and which interhited from ObjectInstance

Gdocal avatar Jun 07 '17 18:06 Gdocal

there is my solution:

public class Window : ObjectInstance
 {
       .......
        protected override object GetMissingPropertyValue(string propertyName)
        {
            var has= _engine.HasGlobalValue(propertyName);
            if (has)
            {
                var v= _engine.GetGlobalValue(propertyName);
                return v;
            }
             ....
       }
}

Gdocal avatar Jun 07 '17 18:06 Gdocal

Could you not just use this?

scriptEngine.SetGlobalValue("window", scriptEngine.Global);

paulbartrum avatar Jun 07 '17 23:06 paulbartrum

@Gdocal I did what @paulbartrum has suggested. In navigators, the global object is the window object. Since the global object is created by the engine, I had to define all the methods/properties of the "window" by myself.

maitredede avatar Jul 03 '17 01:07 maitredede

Hey i saw you asking this on Jint also, with which engine you think this is accomplished easier?

KardoseR avatar Jul 05 '17 15:07 KardoseR

@KardoseR Both engines can do almost the same things. The principles are the same, they only differ in the code you write to apply them. I am working with both engines, and let developpers choose which one to choose.

maitredede avatar Jul 10 '17 00:07 maitredede

@MaitreDede hi are you still working on this? which engine you are using the most as of now?

KardoseR avatar Jan 10 '18 00:01 KardoseR

Hi @KardoseR like I said earlier, I used the simple shortcut scriptEngine.SetGlobalValue("window", scriptEngine.Global);. My project is in standby mode since a few month, but I coded the ability to choose implementation of javascript engine (Jurassic or Jint) using dependency injection.

maitredede avatar Jan 10 '18 01:01 maitredede