diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index 0f635d6d540717..3cd2e08be106da 100644 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -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: "/", } } diff --git a/src/internal/filepathlite/path_js_wasm.go b/src/internal/filepathlite/path_js_wasm.go new file mode 100644 index 00000000000000..b8bee68ab07af6 --- /dev/null +++ b/src/internal/filepathlite/path_js_wasm.go @@ -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) +} diff --git a/src/internal/filepathlite/path_unix.go b/src/internal/filepathlite/path_unix.go index e31f1ae74f7479..2c5ee8b9d41912 100644 --- a/src/internal/filepathlite/path_unix.go +++ b/src/internal/filepathlite/path_unix.go @@ -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