Skip to content

Commit ba27fbe

Browse files
committed
fix: use host node.js function to parse path in wasm
1 parent d363534 commit ba27fbe

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

misc/wasm/wasm_exec.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,25 @@
7373
}
7474
}
7575

76-
if (!globalThis.path) {
77-
globalThis.path = {
78-
resolve(...pathSegments) {
79-
return pathSegments.join("/");
80-
}
81-
}
82-
}
76+
if (!globalThis.path) {
77+
globalThis.path = {
78+
resolve(...pathSegments) {
79+
return pathSegments.join("/");
80+
},
81+
isAbsolute(path) {
82+
return path[0] === "/";
83+
},
84+
parse(path) {
85+
return {
86+
root: "/"
87+
}
88+
},
89+
normalize(path) {
90+
return path;
91+
},
92+
sep: "/",
93+
}
94+
}
8395

8496
if (!globalThis.crypto) {
8597
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build js && wasm
6+
7+
package filepathlite
8+
9+
import "syscall/js"
10+
11+
func IsPathSeparator(c uint8) bool {
12+
return c == '\\' || c == '/'
13+
}
14+
15+
var jsPath = js.Global().Get("path")
16+
17+
var (
18+
Separator = jsPath.Get("sep").String()[0]
19+
)
20+
21+
func isLocal(path string) bool {
22+
return !jsPath.Call("isAbsolute", path).Bool()
23+
}
24+
25+
func localize(path string) (string, error) {
26+
return jsPath.Call("normalize", path).String(), nil
27+
}
28+
29+
func IsAbs(path string) bool {
30+
return jsPath.Call("isAbsolute", path).Bool()
31+
}
32+
33+
func volumeNameLen(path string) int {
34+
length := jsPath.Call("parse", path).Get("root").Get("length").Int()
35+
return max(0, length-1)
36+
}

src/internal/filepathlite/path_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build unix || (js && wasm) || wasip1
5+
//go:build unix || wasip1
66

77
package filepathlite
88

0 commit comments

Comments
 (0)