Created
January 25, 2023 15:35
-
-
Save Kugelschieber/96cc218235444b45595e46fb49fc533b to your computer and use it in GitHub Desktop.
Otto JS Test in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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