Giter Club home page Giter Club logo

Comments (3)

paulbartrum avatar paulbartrum commented on July 21, 2024

I was thinking that promise execution would happen at the ScriptEngine level, i.e. at the end of a Execute, Evaluate or ExecuteFile. That avoids any problems with reentrancy and should be faster too (FunctionInstance.Call/CallLateBound is called a lot).

from jurassic.

maitredede avatar maitredede commented on July 21, 2024

Hi,

In my case, I have used a Promise polyfill. It relies on javascript methods "setImmediate" (or "setTimeout" with value of 0). But the function are evaluated in an other sequence. With your sample, instead of having "1 3 2" you would have "1 2 3", and the "then" functions are executed on the next loop iteration.

So to use Promise (and other stuff), I have done my own execution loop. First script execution executes the full script, and "while I have pending functions in the stack, tries to execute it". By "try to execute", I mean that a pending promise not resolved is stacked, but not executable (so kept on stack).
In the same maner, I have "setTimeout" and "cancelTimeout" (no need for setInterval yet).

This way, I expose Promises to do threaded operations (WinForms long initialization with UI libs, file loading...) and then, the promise "resolve" is called from other thread, but the "then" functions are called in main loop.

It is not the best solution, but we can say it works like in browsers.

from jurassic.

kpreisser avatar kpreisser commented on July 21, 2024

Hi @paulbartrum,
thanks for your reply!

Running the scheduled Promise handlers at the ScriptEngine level is also what I first thought. However, when I want to run callbacks on the script later (after Execute has finished), I use the FunctionInstance supplied by the script code and then simply use FunctionInstance.Call(...). But in this case, the promise handlers would still need to be called before this Call returns if this FunctionInstance resolves a promise.

(To clarify, before I run the Script code with ScriptEngine.Execute(...), I set a global API object with .NET methods where the Script can register events by supplying a callback, and the .NET method takes a FunctionInstance as parameter, which I save and then later .Call() to call back the script's handler. Is this the correct way?)

@maitredede Thanks! In our application we also provide e.g. a setInterval method to call back a FunctionInstance in a regular interval, and also to register for some internal events that can occur on any time. We also plan to provide an API that uses a Promise for long-running operations (e.g. reading a file), where we store the PromiseInstance internal and later call its resolve method, which then should call back the onFulfilled handlers. (The Callbacks will be called from a different thread than the one which called ScriptEngine.Execute(), but we ensure through locking that only one thread at a time calls a script function.)

Currently, we also use a Promise polyfill that uses an internal setImmediate function, which stores the FunctionInstances and invokes them as soon as either the ScriptEngine.Execute() (initialization) or the FunctionInstance.Call() (callback) returns.

Thank you!

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.