Comments (2)
Sorry for the delay.
That's a valid observation. Since only one context may be active at a time (per thread), a probable solution would be to use a thread-local stack instead of each context tracking their predecessor.
EDIT: Although that does not guarantee soundness either, but I believe I have a different solution.
from chakracore-rs.
I've pondered more about a potential solution and each have their own pros and cons
-
Use a stack-based context logic and associate each guard with an ID:
Context ID Context1 1234 Context2 5234 Context3 1236 Whenever a guard is dropped, it checks whether it is currently active, and if so pops itself from the
thread-local stack and activates its predecessor. Otherwise, if not currently active, it just removes
itself from the stack by its ID. This would also require a reentrant mutex for each runtime (unless this validation is deferred to ChakraCore itself).Pros Cons Simple API Requires reentrant mutex for the runtime - Introduces guard inconsistencies* *A guard may not ensure that the context it references is actually active.
let guard1 = context1.make_current()?; let guard2 = context2.make_current()?; // This property would still be associated with `context2` let property = Property::new(&guard1, "foo");
-
Take advantage of the runtime being
Send + Sync
, and expose a&mut self
for activating a context (where only one context can be activate at a time).This approach would rely on the type system to ensure that the runtime is only used from a single thread at a time, whilst also preventing guard inconsistencies.
Pros Cons No guard inconsistencies Arguably a more cumbersome API No reentrant mutex - let guard1 = runtime.activate_context(&context1); // The following would not compile since `runtime` is already mutably borrowed let guard2 = runtime.activate_context(&context2);
The downside is of course that the runtime must be passed around whenever a context is used.
NOTE: An extension to this method may also be to allow nested guards:
let guard2 = guard1.switch(&context2); // The following would not compile since `guard1` is mutably borrowed during the `switch` let property = Property::new(&guard1, "foo");
It may also be worth mentioning that none of these options would solve scenarios where values are intermixed from different contexts:
let object1 = Object::new(&guard1);
let guard2 = guard1.switch(&context2);
// `object1` is associated with `context1`, whilst a value from `context2` is assigned.
object1.set(&guard2, &property, Number::new(&guard2, 10));
This may be solved by each value tracking its own associated context, but I'm not sure it's worth the overhead.
I'd love some input if anyone is interested or has any ideas. I will continue to ponder but I believe using runtime + &mut
may be the cleanest and most robust solution.
from chakracore-rs.
Related Issues (20)
- Value::to_json should return Result HOT 1
- Exception source, line, and column info
- Implement symbol type
- Implement typed arrays
- Implement data views
- Implement external properties data
- Cloning Context/Value fails w/o ContextGuard HOT 2
- Having trouble building on macOS HOT 8
- Can't build to target musl on Ubuntu HOT 2
- lib directory does not exist on Windows custom
- JsCopyString expected 5 parameters HOT 5
- How to load a js file? HOT 3
- How to pass function callbacks? HOT 2
- access violation with call_with_this HOT 9
- Compilation problem HOT 3
- Segfault when dropping Context
- Only one thread can access the same context at a given time HOT 15
- Build from binaries? HOT 2
- 0.2.0 does not build HOT 1
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 chakracore-rs.