Comments (4)
Thanks for giving this thought. It's one of those things that never came up in my usage but I knew it'd be important in many other cases. I'll help give this some careful study over the next few days (sorry, just a bit busy here) but please feel free to keep iterating and to continue updating this issue!
from ducc.
First, I want to say that I love Ducc! It's the Duktape library I didn't know I was waiting for, and it was really easy for me to pick up and use
Anyway, for motivation on this issue: I'm trying to implement a CommonJS/NodeJS-style module system in Duktape. Duktape has an official answer on modules, but I feel like the recommended approach is flawed (their official implementation literally wraps the module code in (function(require, exports, module) { /* module code */ })(...)
). I think running each module in a separate context and using a shared heap for the module is a more promising avenue.
I'd be willing to take a stab at implementing this in a PR, but I think there's multiple different ways to tackle this from an API perspective (all of which have trade-offs). A few possible solutions I've thought about:
- Add a lifetime to
Ducc
, add newducc.thread()
/ducc.thread_new_global()
instance methods that use temporary lifetimes. This would unfortunately be a breaking change since existing code would have to be updated to useDucc<'static>
throughout. - Add a new
Heap
type, updateDucc
with anRc<Heap>
field.Ducc::new()
stays the same butducc.thread()
just clonesRc<Heap>
. This should be a non-breaking change, but using reference counting is not zero-cost. - Add a
ducc.with_thread()
method that takes a closure which would take&Ducc
. I think this would feel a bit hacky, but dealing with a closure should make it easier to implement without significantly impacting the rest of the library.
I also don't know much about the safety implications of duk_push_thread
so it's hard for me to say if there are any potential unsoundness issues with any particular approach.
from ducc.
I pushed up a WIP/proof-of-concept branch with a basic implementation for this issue: wip/duktape-threading. Unfortunately it segfaults in one of the tests I added so there's still some work to do before it's actually useable!
I based it on the with_thread
style I mentioned above. It seems to work well enough for my use case, but I'm not convinced it's the best API interface
from ducc.
Minor update: I fixed the segfault (it happened because I hadn't set udata
or anymap
in the newly-created thread context). So, the branch now works at least. I'll leave it at that for now, but do let me know if you have any thoughts/suggestions/proposals and I can work on prototyping it out! I can make a PR when I have a bit more certainty on what the final form of the API should look like
from ducc.
Related Issues (11)
- Undefined symbols for architecture x86_64: "DUK_VARARGS" HOT 6
- Store Ducc values inside userdata HOT 4
- Implement accessors (duk_def_prop) HOT 3
- Idea: object (and function?) handle
- Support for modules? HOT 2
- Can we mutate userdata entry inside a JS invoked function? HOT 4
- Use `DUK_USE_DATE_NOW_WINDOWS` flag for Windows 7 support HOT 5
- Add support for arm HOT 2
- Warnings When Compiling on Stable
- Unhandled fatal error causes stack overflow 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 ducc.