Skip to content

internal/filepathlite: use host node.js function to parse path in wasm #68831

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion misc/wasm/wasm_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@
globalThis.path = {
resolve(...pathSegments) {
return pathSegments.join("/");
}
},
isAbsolute(path) {
return path[0] === "/";
},
parse(path) {
return {
root: "/"
}
},
normalize(path) {
return path;
},
sep: "/",
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/internal/filepathlite/path_js_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build js && wasm

package filepathlite

import "syscall/js"

func IsPathSeparator(c uint8) bool {
return c == '\\' || c == '/'
}

var jsPath = js.Global().Get("path")

var (
Separator = jsPath.Get("sep").String()[0]
)

func isLocal(path string) bool {
return !jsPath.Call("isAbsolute", path).Bool()
}

func localize(path string) (string, error) {
return jsPath.Call("normalize", path).String(), nil
}

func IsAbs(path string) bool {
return jsPath.Call("isAbsolute", path).Bool()
}

func volumeNameLen(path string) int {
length := jsPath.Call("parse", path).Get("root").Get("length").Int()
return max(0, length-1)
}
2 changes: 1 addition & 1 deletion src/internal/filepathlite/path_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build unix || (js && wasm) || wasip1
//go:build unix || wasip1

package filepathlite

Expand Down