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

package/js/v8: add console.error, console.warn, setTimeout, setInterval, clearTimeout and clearInterval. #233

Merged
merged 1 commit into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/timewasted/go-accept-headers v0.0.0-20130320203746-c78f304b1b09
github.com/xlab/treeprint v1.1.0
go.kuoruan.net/v8go-polyfills v0.5.0
go.kuoruan.net/v8go-polyfills v0.5.1-0.20220727011656-c74c5b408ebd
golang.org/x/mod v0.5.1
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/tools v0.1.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ github.com/xlab/treeprint v1.1.0 h1:G/1DjNkPpfZCFt9CSh6b5/nY4VimlbHF3Rh4obvtzDk=
github.com/xlab/treeprint v1.1.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
go.kuoruan.net/v8go-polyfills v0.5.0 h1:wd2WxsFIXWK/FcrpITw6BOo8Rn24xMmd4qoHofgg8hc=
go.kuoruan.net/v8go-polyfills v0.5.0/go.mod h1:egHzK8RIHR7dPOYzhnRsomClFTVmYCtvhTWqec4JXaY=
go.kuoruan.net/v8go-polyfills v0.5.1-0.20220727011656-c74c5b408ebd h1:lMfOO39WTD+CxBPmqZvLdISrLVsEjgNfWoV4viBt15M=
go.kuoruan.net/v8go-polyfills v0.5.1-0.20220727011656-c74c5b408ebd/go.mod h1:egHzK8RIHR7dPOYzhnRsomClFTVmYCtvhTWqec4JXaY=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down
18 changes: 15 additions & 3 deletions package/js/v8/v8.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package v8

import (
"os"

"github.com/livebud/bud/package/js"
"go.kuoruan.net/v8go-polyfills/console"
"go.kuoruan.net/v8go-polyfills/fetch"
"go.kuoruan.net/v8go-polyfills/timers"
"go.kuoruan.net/v8go-polyfills/url"
"rogchap.com/v8go"
)
Expand All @@ -28,6 +31,12 @@ func load() (*v8go.Isolate, *v8go.Context, error) {
isolate.Dispose()
return nil, nil, err
}
// setTimeout/setInterval support
if err := timers.InjectTo(isolate, global); err != nil {
isolate.TerminateExecution()
isolate.Dispose()
return nil, nil, err
}
// Create the context
context := v8go.NewContext(isolate, global)
// URL support
Expand All @@ -37,9 +46,12 @@ func load() (*v8go.Isolate, *v8go.Context, error) {
isolate.Dispose()
return nil, nil, err
}
// Console support
// TODO: this dependency looks like it can be improved
if err := console.InjectTo(context); err != nil {
// Console log, warn, error support
if err := console.InjectMultipleTo(context,
console.NewConsole(console.WithOutput(os.Stderr), console.WithMethodName("error")),
console.NewConsole(console.WithOutput(os.Stderr), console.WithMethodName("warn")),
console.NewConsole(console.WithOutput(os.Stdout), console.WithMethodName("log")),
); err != nil {
context.Close()
isolate.TerminateExecution()
isolate.Dispose()
Expand Down
35 changes: 34 additions & 1 deletion package/js/v8/v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@ func TestEval(t *testing.T) {
is.Equal("10", result)
}

func TestConsole(t *testing.T) {
func TestConsoleLog(t *testing.T) {
is := is.New(t)
_, err := v8.Eval("TestConsole.js", `console.log("a", 3, { hi: "world" })`)
is.NoErr(err)
}

func TestConsoleWarn(t *testing.T) {
is := is.New(t)
_, err := v8.Eval("TestConsole.js", `console.warn("a", 3, { hi: "world" })`)
is.NoErr(err)
}

func TestConsoleError(t *testing.T) {
is := is.New(t)
_, err := v8.Eval("TestConsole.js", `console.error("a", 3, { hi: "world" })`)
is.NoErr(err)
}

func TestFetch(t *testing.T) {
is := is.New(t)
res, err := v8.Eval("TestFetch.js", `fetch("http://google.com").then(res => res.status)`)
Expand All @@ -43,3 +55,24 @@ func TestURL(t *testing.T) {
is.NoErr(err)
is.Equal(res, "google.com")
}

func TestURLSearchParams(t *testing.T) {
is := is.New(t)
res, err := v8.Eval("TestURLSearchParams.js", `(new URLSearchParams('?a=1')).get('a')`)
is.NoErr(err)
is.Equal(res, "1")
}

func TestSetClearInterval(t *testing.T) {
is := is.New(t)
res, err := v8.Eval("TestSetClearInterval.js", `let id = setInterval(() => clearInterval(id), 500)`)
is.NoErr(err)
is.Equal(res, "undefined")
}

func TestSetClearTimeout(t *testing.T) {
is := is.New(t)
res, err := v8.Eval("TestSetClearTimeout.js", `let id = setTimeout(() => clearTimeout(id), 500)`)
is.NoErr(err)
is.Equal(res, "undefined")
}