Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant pass javascript functions between runtimes. #21

Closed
silven opened this issue Jun 4, 2013 · 3 comments
Closed

Cant pass javascript functions between runtimes. #21

silven opened this issue Jun 4, 2013 · 3 comments

Comments

@silven
Copy link

silven commented Jun 4, 2013

Hello,

I've got a situation where I want multiple Otto runtimes and would like to pass functions between them to allow the runtimes to interact. But code like this doesn't work:

package main

import (
    "fmt"
    "github.com/robertkrimen/otto"
)

func main() {
    o1 := otto.New()
    o1.Run(`
        console.log("HI")
        exports = {}
        exports.tomte = function() {
            console.log("Tomte!")
            return 7
        }
    `)
    value, err := o1.Get("exports")
    if err != nil {
        fmt.Println("ERROR:", err)
        return
    }
    o2 := otto.New()
    o2.Set("exp", value)
    value, err = o2.Run(`
        console.log(exp.tomte)
    `)
    if err != nil {
        fmt.Println("ERROR:", err)
        return
    }
    fmt.Println("Bye bye Otto!")
}

Doesn't make the function in the first runtime to be visible in the second. I try passing the function itself, and in Go it has the "type" [function] but the second runtime can't run the code.

Is it at all possible to make functions travel across runtimes or how can I achieve interaction between different segments of independent code? Or some sort of observer pattern and event trigger system.

@robertkrimen
Copy link
Owner

Well, that's interesting, and kind of weird.

If you had this code in one runtime:

     var abc = 0;
     var fn = function() {
            abc += 1;
     }

What does it mean when you export fn to a different runtime and call it there? Does it increment abc in the original runtime? Does it try to increment abc in the new runtime? (edit: It should increment abc in the original runtime... probably.)

Maybe you can better explain what you're trying to do?

I'm not sure why the code is not running, I don't explicitly check for cross-runtime Object migration, though I probably should.

@silven
Copy link
Author

silven commented Jun 5, 2013

I was trying to create a network of independent pieces of code, occasionally interacting with each other. My hope being that if I could export functions, that would affect code in it's origin runtime, I could define and extend the situation in pure javascript, without having to give hooks for every single function. This to create observer patterns and event triggering mechanics.

I hope that is enough of an explanation.

@robertkrimen
Copy link
Owner

Thanks for the report, this was actually a pretty substantial issue with the toValue method. Basically, the reflection stuff was treating a Value as a struct, turning it into a sort of nonsensical Object in the JavaScript runtime.

Your original code should work fine now.

Technically, I don't think there is anything wrong with an Object existing in more than one runtime at the same time. It would be costly to try and prevent anyway (deep introspection of every value passed to toValue, etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants