Comments (5)
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.
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.
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.
@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.
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)
- Store object using SharedDynamicObject HOT 1
- Implement async/await syntax HOT 19
- Functions defined in js eventually be released safely? HOT 1
- Question:Why JSON.parse of goja doesn't use json.Unmarshal in the golang standard library? HOT 1
- Buffer is not defined HOT 2
- new Date().toString() returns shortened timezone HOT 2
- AssertFunction fails for object.function_name HOT 2
- Wrapping map in a type breaks toValue after 6c0d9883792e42e6da320ac03ae0d6c1b0cd7530 HOT 2
- Error parsing typescript 5 compiler code with goja
- A problem with AsyncContextTracker HOT 5
- Can I do `sleep` and can I generate random numbers? HOT 1
- `AsyncContextTracker.Resumed` not called if returning from an async function HOT 9
- Modifying objects in javascript fails silently HOT 5
- Object not being added to array as expected HOT 7
- Stack incorrect after recursive RunProgram calls HOT 2
- Modify nodes and export new JS code HOT 1
- Access `stack` of `Exception`
- Recursive RunProgram calls eventually cause variable corruption HOT 6
- Error executing functions in parallel HOT 1
- Resulting in higher and higher CPU consumption 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 goja.