Git Product home page Git Product logo

Comments (5)

paulbartrum avatar paulbartrum commented on July 30, 2024

Yep, that's definitely wrong, later declarations should override earlier ones.

I tried running this code in the latest version (v3.2.6):

static void Main(string[] args)
{
    var engine = new ScriptEngine();
    engine.SetGlobalValue("console", new Jurassic.Library.FirebugConsole(engine));
    engine.Execute(@"
        function main() {
            console.log('Script A');
        }
        main();");
    engine.Evaluate(@"
        function main() {
            console.log('Script B');
        }
        main();");
}

And it worked as expected. Likely it was fixed when I did a big overhaul of how scoping works a couple years back.

Note that this code prints "Script B" twice, and that's also correct (declarations are processed before code):

static void Main(string[] args)
{
    var engine = new ScriptEngine();
    engine.SetGlobalValue("console", new Jurassic.Library.FirebugConsole(engine));
    engine.Execute(@"
        function main() {
            console.log('Script A');
        }
        main();
        function main() {
            console.log('Script B');
        }
        main();");
}

from jurassic.

lsim avatar lsim commented on July 30, 2024

Thanks for your speedy reply (as always)!

I somehow thought it would be more work testing it out on 3.2.6. I should have given it a go.

I played around with your code and I'm seeing the same thing you're seeing.

Good to know that it's fixed in 3.2.6. We'll have to put some effort into getting the upgrade under way.

from jurassic.

paulbartrum avatar paulbartrum commented on July 30, 2024

If you don't want to update you may be able to clear out the value of "main" in between the two executions, e.g. something like engine.SetGlobalValue("main", Undefined.Value);. I haven't tried it but I guess that would fix it(?)

from jurassic.

lsim avatar lsim commented on July 30, 2024

Our scripts are entirely dynamic, and we do have some cases where we rely on variables declared in one script being accessed from others, so scope wiping between runs will break things.

I have started the upgrade. Looks doable for now.

from jurassic.

paulbartrum avatar paulbartrum commented on July 30, 2024

The code I pasted would only touch the value of "main" and nothing else (so not really "scope wiping"), but if the name of the function is dynamic then yeah that'd be problematic.

from jurassic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.