Giter Club home page Giter Club logo

Comments (5)

dop251 avatar dop251 commented on July 17, 2024

I did not implement this functionality because I think it's fundamentally flawed as there is no way in Go to clone any object or determine that the object cannot be cloned (and return an error if it's the case).

While it's possible to clone js values, what should we do with imported ones? We could create another go wrapper which would reference the same object by it would mean some objects are copied and others are referenced.

You can access global object properties with runtime.Get()/Set().

Let me know exactly what you're after and I may be able to help.

from goja.

liclac avatar liclac commented on July 17, 2024

What I want to so is essentially to perform initialization once, then run the same script across multiple goroutines. I could simply do the initialization once for each goroutine and use SetRandSource() to ensure initialization is deterministic, but I really just want a quick and dirty way to provide cheap, identical environments. A Keys() function to use with Get() perhaps?

from goja.

dop251 avatar dop251 commented on July 17, 2024

It would still not work properly without cloning.

As far as I understand that you need to have a bunch of workers running code with the same environment in parallel. The best option would be having initialisation code as a method that takes *Runtime as a parameter. Any libraries can (and should) be pre-compiled, a *Program instance does not depend on a *Runtime and is thread-safe so RunProgram() is like RunString() only faster.

Depending on your application it could be worth considering using sync.Pool so that you don't need to do initialisation each time.

Without seeing the code or understanding the task I can't really advice more. Feel free to ask if you have any further questions.

from goja.

angadn avatar angadn commented on July 17, 2024

@dop251 Thank you for the explanation on this thread. I am trying to achieve something similar and using a sync.Pool of *goja.Runtime objects with a bunch of Set functions in the scope. The interesting problem I am coming across is passing a bunch of args using goja.Value to an asserted goja.Callable returns incorrect values!

function a(_0, _1) { return _0 * _1 }

Suppose I sync.Pool#Get an instance with this program and call it as so callable(goja.Undefined(), NUM1, NUM2), on the reused instances I get incorrect responses instead of NUM1 * NUM2!

Is this a known issue and if so, how do I get around it? The additional *goja.Runtime instances are raking up a lot of memory for me and also slowing down execution as a result. Thanks for your time!

from goja.

angadn avatar angadn commented on July 17, 2024

Quick update on this: I am unable to reproduce this outside of my application code and must conclude that it is an application issue. Apologies!

from goja.

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.