Skip to content

Instantly share code, notes, and snippets.

@Kugelschieber
Created January 25, 2023 15:35
Show Gist options
  • Save Kugelschieber/96cc218235444b45595e46fb49fc533b to your computer and use it in GitHub Desktop.
Save Kugelschieber/96cc218235444b45595e46fb49fc533b to your computer and use it in GitHub Desktop.
Otto JS Test in Go
package main
import (
"log"
"time"
"github.com/robertkrimen/otto"
)
const (
script = `
var messages = ["We're doing something", "horrible", "here!"];
for (var i = 0; i < messages.length; i++) {
console.log(messages[i]);
}
while (true) {}
`
)
func execUnsafe(script string) {
defer func() {
if r := recover(); r != nil {
log.Println(r)
}
}()
vm := otto.New()
vm.Interrupt = make(chan func(), 1)
go func() {
<-time.After(time.Second * 2)
vm.Interrupt <- func() {
panic("timeout")
}
}()
if _, err := vm.Run(script); err != nil {
log.Println(err)
}
}
func main() {
execUnsafe(script)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment