Code Monkey home page Code Monkey logo

Comments (4)

SkylerLipthay avatar SkylerLipthay commented on August 18, 2024 1

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.

kylewlacy avatar kylewlacy commented on August 18, 2024

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:

  1. Add a lifetime to Ducc, add new ducc.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 use Ducc<'static> throughout.
  2. Add a new Heap type, update Ducc with an Rc<Heap> field. Ducc::new() stays the same but ducc.thread() just clones Rc<Heap>. This should be a non-breaking change, but using reference counting is not zero-cost.
  3. 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.

kylewlacy avatar kylewlacy commented on August 18, 2024

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.

kylewlacy avatar kylewlacy commented on August 18, 2024

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)

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.