Git Product home page Git Product logo

Comments (8)

ry avatar ry commented on May 19, 2024

Ideally sending from multiple goroutines would work, but I haven't tested it. It might need a lock.

Killing a worker could be done with a call to v8::Isolate::TerminateExecution(). Do you need it?

from v8worker.

emicklei avatar emicklei commented on May 19, 2024

I will setup such a test (goroutine) as well.

Yes, I need to be able to terminate a worker to protect my program against Javascript errors that result in endless loops or other disasters. My application can load and run arbitrary Javascript code and typically during development of that code, mistakes are made. I would like my application to recover from that without restart.

from v8worker.

ry avatar ry commented on May 19, 2024

@emicklei I spent a little time trying to get kill working. It doesn't work yet. But maybe you can make more progress with this? https://gist.github.com/ry/1f0656fe15c229c0aa45

from v8worker.

emicklei avatar emicklei commented on May 19, 2024

Thank you for working on this request. I will look at it as well.

from v8worker.

emicklei avatar emicklei commented on May 19, 2024

not related but probably of interest to you, I created a fork in which I added another builtin function for synchronous message exchange. I realize that this is "against" your original clean design of message passing but I needed it to replace my existing otto based solution. I got this extension working now but it also needs more testing.

from v8worker.

ry avatar ry commented on May 19, 2024

Why do you need that? I'm not against adding something like that if there's a necessary use case. It's a call from js that send a message to go and returns the response?

from v8worker.

emicklei avatar emicklei commented on May 19, 2024

In Javascript, I expose several objects that represent an api written in Go. At the time of its design, most api operations were synchronous and could be mapped to function calls of their Go counterpart. These operations include state access of a large object structure (physics engine).

I have considered changing my api design but that would obviously break all existing programs. So instead, I was looking for a construct that could emulate the behavior using send and recv. With the current implementation, I don't see how I can send a message and wait for the receive to happen and return the value encoded by the message. (even Promises require callback functions).

With this additional request-response feature, I can use these values for transport

type MessageSend struct {
    Receiver  string        `json:"receiver" `
    Method    string        `json:"method" `
    Arguments []interface{} `json:"args" `
}

which can be dispatched on both sides. For example utils.now() is translated to a MessageSend with receiver utils, method now. On the Go side, I have registered a Module value by the name of utils and have a Dispatch(MessageSend) function that performs a function lookup (case) and invocation. A similar mechanism is implemented in Javascript for calling from Go (currently without scoping).

$recv(function(msg) {
    var obj = JSON.parse(msg);
    this[obj.method].apply(this, obj.args)
});

The response of a Go Dispatch(msg) will be an interface{} value encoded in JSON before returning to v8worker and to the caller in Javascript.
Because the flow is now synchronous, when an error happens on the Go side, I can report that to the developers too (using the console Module) complete with stack information (file:line).

There are many other details to this story but this is basically how I intend to use the package.

from v8worker.

emicklei avatar emicklei commented on May 19, 2024

( off topic ) fyi, I just published a package implementing this idea. https://godoc.org/github.com/emicklei/v8dispatcher

from v8worker.

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.