Comments (3)
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.
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.
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)
- Return value of CompiledScript HOT 5
- 3.2.1 backward compatibility issues HOT 4
- PropertyAccessorValue not visible HOT 1
- How to catch CLR Exception? HOT 7
- System.ArgumentNullException: Value cannot be null. (Parameter 'value') after update from 3.1.0 to 3.2.2 HOT 3
- JSONObject.Parse() fails when number is in scientific notation HOT 4
- ScriptEngine.EnableDebugging HOT 3
- Template of literals is slow
- JSON.stringify([].length) errors HOT 2
- JSONObject.Parse consumes more and more memory. Possible memory leak? HOT 11
- Scoping and CompiledScript.Execute vs ScriptEngine.Evaluate HOT 5
- Catching script errors at compile time HOT 2
- Currently JSONObject.Stringify returns object instead of string after upgrading from 3.1.0 to 3.2.6 HOT 3
- ScriptEngine.GetGlobalValue HOT 1
- Bug in Evaluate: Loop and variable handling HOT 2
- PM - Customer side scripting for Channels HOT 1
- Date.UTC() doesn't interpret two-digit years correctly HOT 1
- Possibility to create 'sessions' HOT 4
- Linux compability ? HOT 4
- Is it possible to go from JS to DLL and then to C# source? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jurassic.