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

Better example code #39

Open
markusbkk opened this issue Nov 25, 2022 · 2 comments
Open

Better example code #39

markusbkk opened this issue Nov 25, 2022 · 2 comments

Comments

@markusbkk
Copy link

markusbkk commented Nov 25, 2022

The provided example is not very concise or easy to understand (it actually doesn't even provide output, nor log an error in case the module can't be found).

Would be great if we could come up with some better examples.
I'm reworking the original example provided in the README RN.

Alas, I've yet to figure out how to actually invoke console.log.
The comments say it should be activated by default but this code fails

package main

import (
	"fmt"

	"github.com/dop251/goja"
	"github.com/dop251/goja_nodejs/eventloop"
)

func main() {
	runtime := goja.New()
	loop := eventloop.NewEventLoop()
	// req := registry.Enable(runtime)

	loop.Run(func(vm *goja.Runtime) {
		fmt.Println(runtime.RunString(`
        console.log("Hello World")
        `))
    })
}

Error logged @ stdout: <nil> ReferenceError: console is not defined at <eval>:2:9(0)

@dop251
Copy link
Owner

dop251 commented Nov 26, 2022

The code fails because you're running it in an 'empty' runtime that you have created, rather than the loop runtime which is provided as the argument to the function you're running in the loop, i.e. it should be

func main() {
	loop := eventloop.NewEventLoop()

	loop.Run(func(vm *goja.Runtime) {
                vm.RunString(`
                console.log("Hello World")
               `)
        })
}

@markusbkk
Copy link
Author

The code fails because you're running it in an 'empty' runtime that you have created, rather than the loop runtime which is provided as the argument to the function you're running in the loop, i.e. it should be

func main() {
	loop := eventloop.NewEventLoop()

	loop.Run(func(vm *goja.Runtime) {
                vm.RunString(`
                console.log("Hello World")
               `)
        })
}

Oh. You're right. I didn't even see that. Thank you!

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