Skip to content

Commit

Permalink
Fix issues due to removal of syscall/js.Wrapper from std lib
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnStarich committed Apr 21, 2023
1 parent 8f1adc2 commit 93d62a8
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 50 deletions.
15 changes: 8 additions & 7 deletions cmd/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ import (
)

// editorJSFunc is a JS function that opens on a JS element and returns a JS object with the following spec:
// {
// getContents() string
// setContents(string)
// getCursorIndex() int
// setCursorIndex(int)
// }
//
// {
// getContents() string
// setContents(string)
// getCursorIndex() int
// setCursorIndex(int)
// }
type editorJSFunc js.Value

func (e editorJSFunc) New(elem *dom.Element) ide.Editor {
editor := &jsEditor{
titleChan: make(chan string, 1),
}
editor.elem = js.Value(e).Invoke(elem, js.FuncOf(editor.onEdit))
editor.elem = js.Value(e).Invoke(elem.JSValue(), js.FuncOf(editor.onEdit))
return editor
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/editor/ide/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const (
)

// runGoProcess optimizes runProcess by skipping the wait time for listing PATH directories on startup
func (w *window) runGoProcess(console TaskConsole, args ...string) promise.Promise {
func (w *window) runGoProcess(console TaskConsole, args ...string) promise.JS {
return w.runRawProcess(console, goBinaryPath, "go", args...)
}

func (w *window) runProcess(console TaskConsole, name string, args ...string) promise.Promise {
func (w *window) runProcess(console TaskConsole, name string, args ...string) promise.JS {
return w.runRawProcess(console, name, name, args...)
}

func (w *window) runRawProcess(console TaskConsole, fullPath, name string, args ...string) promise.Promise {
func (w *window) runRawProcess(console TaskConsole, fullPath, name string, args ...string) promise.JS {
resolve, reject, prom := promise.New()
go func() {
var success bool
Expand Down Expand Up @@ -70,6 +70,6 @@ func (w *window) startProcess(console TaskConsole, rawPath, name string, args ..

func (w *window) runPlayground(console TaskConsole) {
w.runGoProcess(console, "build", "-v", ".").Then(func(_ interface{}) interface{} {
return w.runProcess(console, "./playground")
return w.runProcess(console, "./playground").JSValue()
})
}
2 changes: 1 addition & 1 deletion cmd/editor/ide/dropdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newDropdown(attachTo, content *dom.Element) *dropdown {
attached: attachTo,
}
dom.GetDocument().AddEventListener("click", func(event js.Value) {
if !event.Call("composedPath").Call("includes", drop).Bool() {
if !event.Call("composedPath").Call("includes", drop.JSValue()).Bool() {
drop.Close()
}
})
Expand Down
6 changes: 3 additions & 3 deletions cmd/editor/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type terminal struct {

func (b *terminalBuilder) New(elem *dom.Element, rawName, name string, args ...string) (ide.Console, error) {
term := &terminal{
xterm: b.newXTermFunc.Invoke(elem),
xterm: b.newXTermFunc.Invoke(elem.JSValue()),
titleChan: make(chan string, 1),
}
go func() {
Expand Down Expand Up @@ -113,7 +113,7 @@ func (t *terminal) Close() error {
}
t.closed = true
const colorRed = "\033[1;31m"
t.xterm.Call("write", idbblob.FromBlob(blob.NewBytes([]byte("\n\r"+colorRed+"[exited]\n\r"))))
t.xterm.Call("write", idbblob.FromBlob(blob.NewBytes([]byte("\n\r"+colorRed+"[exited]\n\r"))).JSValue())
var err error
for _, closer := range t.closables {
cErr := closer()
Expand All @@ -131,7 +131,7 @@ func (t *terminal) readOutputPipes(r io.Reader) {
_, err := r.Read(buf)
switch err {
case nil:
t.xterm.Call("write", idbblob.FromBlob(blob.NewBytes(buf)))
t.xterm.Call("write", idbblob.FromBlob(blob.NewBytes(buf)).JSValue())
case io.EOF:
t.Close()
return
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/hack-pad/safejs v0.1.0 // indirect
github.com/hack-pad/safejs v0.1.1 // indirect
github.com/matryer/is v1.4.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-tty v0.0.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
golang.org/x/sys v0.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/sh/v3 v3.3.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/hack-pad/hush v0.1.0 h1:lm/iUaRpVsKkpbN6U9wf45arVnCXzTqsMG1jyihIgkI=
github.com/hack-pad/hush v0.1.0/go.mod h1:NqjEIfyA2YtlnEPlI/1K3tNuyXGByWFadPxPlGrDPms=
github.com/hack-pad/safejs v0.1.0 h1:qPS6vjreAqh2amUqj4WNG1zIw7qlRQJ9K10eDKMCnE8=
github.com/hack-pad/safejs v0.1.0/go.mod h1:HdS+bKF1NrE72VoXZeWzxFOVQVUSqZJAG0xNCnb+Tio=
github.com/hack-pad/safejs v0.1.1 h1:d5qPO0iQ7h2oVtpzGnLExE+Wn9AtytxIfltcS2b9KD8=
github.com/hack-pad/safejs v0.1.1/go.mod h1:HdS+bKF1NrE72VoXZeWzxFOVQVUSqZJAG0xNCnb+Tio=
github.com/johnstarich/go/datasize v0.0.1 h1:Hjswen8gwmO7trXtQ8Xl8NUOQAKKm1wvus3/xK5eHGY=
github.com/johnstarich/go/datasize v0.0.1/go.mod h1:4eHLMGz7Q5uCmZeS9rZdahvAih1QmBg1EW3bBXTJpi4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -53,9 +55,16 @@ github.com/rogpeppe/go-internal v1.7.0 h1:3qqXGV8nn7GJT65debw77Dzrx9sfWYgP0DDo7x
github.com/rogpeppe/go-internal v1.7.0/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
Expand Down Expand Up @@ -96,6 +105,9 @@ gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/editorconfig v0.2.0 h1:XL+7ys6ls/RKrkUNFQvEwIvNHh+JKx8Mj1pUV5wQxQE=
mvdan.cc/editorconfig v0.2.0/go.mod h1:lvnnD3BNdBYkhq+B4uBuFFKatfp02eB6HixDvEz91C0=
mvdan.cc/sh/v3 v3.3.0 h1:ujzElMnry63f4I5sjPFxzo6xia+gwsHZM0yyauuyZ6k=
Expand Down
2 changes: 1 addition & 1 deletion install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func installFunc(this js.Value, args []js.Value) interface{} {
}
resolve(nil)
}()
return prom
return prom.JSValue()
}

func install(args []js.Value) error {
Expand Down
9 changes: 4 additions & 5 deletions internal/interop/error_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ import (
"github.com/pkg/errors"
)

func WrapAsJSError(err error, message string) error {
func WrapAsJSError(err error, message string) js.Value {
return wrapAsJSError(err, message)
}

func wrapAsJSError(err error, message string, args ...js.Value) error {
func wrapAsJSError(err error, message string, args ...js.Value) js.Value {
if err == nil {
return nil
return js.Null()
}

errMessage := errors.Wrap(err, message).Error()
for _, arg := range args {
errMessage += fmt.Sprintf("\n%v", arg)
}

val := js.ValueOf(map[string]interface{}{
return js.ValueOf(map[string]interface{}{
"message": js.ValueOf(errMessage),
"code": js.ValueOf(mapToErrNo(err, errMessage)),
})
return js.Error{Value: val}
}
4 changes: 2 additions & 2 deletions internal/interop/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func setFuncHandler(name string, fn interface{}, args []js.Value) (returnedVal i
}()

ret, err = fn(args)
err = wrapAsJSError(err, name, args...)
ret = append([]interface{}{err}, ret...)
errValue := wrapAsJSError(err, name, args...)
ret = append([]interface{}{errValue}, ret...)
callback.Invoke(ret...)
}()
return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/js/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ func destroyMount(this js.Value, args []js.Value) interface{} {
resolve, reject, prom := promise.New()
mountPath := args[0].String()
go func() {
err := interop.WrapAsJSError(fs.DestroyMount(mountPath), "destroyMount")
if err != nil {
reject(err)
jsErr := interop.WrapAsJSError(fs.DestroyMount(mountPath), "destroyMount")
if jsErr.Type() != js.TypeNull {
reject(jsErr)
} else {
resolve(nil)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/js/fs/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func openSync(args []js.Value) (interface{}, error) {

p := process.Current()
fd, err := p.Files().Open(path, flags, mode)
return fd, err
return fd.JSValue(), err
}
4 changes: 2 additions & 2 deletions internal/js/fs/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func overlayIndexedDB(this js.Value, args []js.Value) interface{} {
resolve(nil)
}
}()
return prom
return prom.JSValue()
}

func OverlayIndexedDB(args []js.Value) (err error) {
Expand Down Expand Up @@ -77,7 +77,7 @@ func overlayTarGzip(this js.Value, args []js.Value) interface{} {
resolve(nil)
}
}()
return prom
return prom.JSValue()
}

func OverlayTarGzip(args []js.Value) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/js/fs/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func pipeSync(args []js.Value) (interface{}, error) {
}
p := process.Current()
fds := p.Files().Pipe()
return []interface{}{fds[0], fds[1]}, nil
return []interface{}{fds[0].JSValue(), fds[1].JSValue()}, nil
}
8 changes: 4 additions & 4 deletions internal/js/fs/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ var (
})
)

func jsStat(info os.FileInfo) interface{} {
func jsStat(info os.FileInfo) js.Value {
if info == nil {
return nil
return js.Null()
}
const blockSize = 4096 // TODO find useful value for blksize
modTime := info.ModTime().UnixNano() / 1e6
return map[string]interface{}{
return js.ValueOf(map[string]interface{}{
"dev": 0,
"ino": 0,
"mode": jsMode(info.Mode()),
Expand All @@ -64,7 +64,7 @@ func jsStat(info os.FileInfo) interface{} {
"isFile": jsBoolFunc(info.Mode().IsRegular()),
"isSocket": funcFalse,
"isSymbolicLink": jsBoolFunc(info.Mode()&os.ModeSymlink == os.ModeSymlink),
}
})
}

var modeBitTranslation = map[os.FileMode]uint32{
Expand Down
2 changes: 1 addition & 1 deletion internal/js/fs/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func write(args []js.Value) ([]interface{}, error) {

p := process.Current()
n, err := p.Files().Write(fd, buffer, offset, length, position)
return []interface{}{n, buffer}, err
return []interface{}{n, buffer.JSValue()}, err
}
8 changes: 4 additions & 4 deletions internal/js/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func Init() {
interop.SetFunc(jsProcess, "getgid", getegid)
interop.SetFunc(jsProcess, "getegid", getegid)
interop.SetFunc(jsProcess, "getgroups", getgroups)
jsProcess.Set("pid", currentProcess.PID())
jsProcess.Set("ppid", currentProcess.ParentPID())
jsProcess.Set("pid", currentProcess.PID().JSValue())
jsProcess.Set("ppid", currentProcess.ParentPID().JSValue())
interop.SetFunc(jsProcess, "umask", umask)
interop.SetFunc(jsProcess, "cwd", cwd)
interop.SetFunc(jsProcess, "chdir", chdir)
Expand All @@ -42,8 +42,8 @@ func Init() {
}

func switchedContext(pid, ppid process.PID) {
jsProcess.Set("pid", pid)
jsProcess.Set("ppid", ppid)
jsProcess.Set("pid", pid.JSValue())
jsProcess.Set("ppid", ppid.JSValue())
}

func Dump() interface{} {
Expand Down
10 changes: 7 additions & 3 deletions internal/js/process/spawn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ func spawn(args []js.Value) (interface{}, error) {
return Spawn(command, argv, procAttr)
}

func Spawn(command string, args []string, attr *process.ProcAttr) (process.Process, error) {
type jsWrapper interface {
JSValue() js.Value
}

func Spawn(command string, args []string, attr *process.ProcAttr) (js.Value, error) {
p, err := process.New(command, args, attr)
if err != nil {
return p, err
return js.Value{}, err
}
return p, p.Start()
return p.(jsWrapper).JSValue(), p.Start()
}

func parseProcAttr(defaultCommand string, value js.Value) (argv0 string, attr *process.ProcAttr) {
Expand Down
6 changes: 3 additions & 3 deletions internal/js/process/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func waitSync(args []js.Value) (interface{}, error) {
pid := process.PID(args[0].Int())
waitStatus := new(syscall.WaitStatus)
wpid, err := Wait(pid, waitStatus, 0, nil)
return map[string]interface{}{
"pid": wpid,
return js.ValueOf(map[string]interface{}{
"pid": wpid.JSValue(),
"exitCode": waitStatus.ExitStatus(),
}, err
}), err
}

func Wait(pid process.PID, wstatus *syscall.WaitStatus, options int, rusage *syscall.Rusage) (wpid process.PID, err error) {
Expand Down
14 changes: 12 additions & 2 deletions internal/log/js_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package log

import (
"fmt"
"syscall/js"

"github.com/hack-pad/hackpad/internal/global"
"github.com/hack-pad/safejs"
)

var (
console = js.Global().Get("console")
console = safejs.MustGetGlobal("console")
)

const logLevelKey = "logLevel"
Expand Down Expand Up @@ -54,7 +56,15 @@ func logJSValues(kind consoleType, args ...interface{}) int {
if kind < logLevel {
return 0
}
console.Call(kind.String(), args...)
var jsArgs []interface{}
for _, arg := range args {
jsArg, err := safejs.ValueOf(arg)
if err != nil {
jsArg = safejs.Safe(js.ValueOf(fmt.Sprintf("LOGERR(%s: %T %+v)", err, arg, arg)))
}
jsArgs = append(jsArgs, jsArg)
}
console.Call(kind.String(), jsArgs...)
return 0
}

Expand Down
4 changes: 2 additions & 2 deletions internal/process/process_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var (

func (p *process) JSValue() js.Value {
return js.ValueOf(map[string]interface{}{
"pid": p.pid,
"ppid": p.parentPID,
"pid": p.pid.JSValue(),
"ppid": p.parentPID.JSValue(),
"error": interop.WrapAsJSError(p.err, "spawn"),
})
}
Expand Down

0 comments on commit 93d62a8

Please sign in to comment.