From ba27fbed08ab8381429e992edfba56aa5c76eefc Mon Sep 17 00:00:00 2001 From: Zxilly Date: Sun, 11 Aug 2024 03:41:34 +0800 Subject: [PATCH 1/2] fix: use host node.js function to parse path in wasm --- misc/wasm/wasm_exec.js | 26 +++++++++++----- src/internal/filepathlite/path_js_wasm.go | 36 +++++++++++++++++++++++ src/internal/filepathlite/path_unix.go | 2 +- 3 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 src/internal/filepathlite/path_js_wasm.go diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index 0f635d6d540717..6531701c62643a 100644 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -73,13 +73,25 @@ } } - if (!globalThis.path) { - globalThis.path = { - resolve(...pathSegments) { - return pathSegments.join("/"); - } - } - } + if (!globalThis.path) { + globalThis.path = { + resolve(...pathSegments) { + return pathSegments.join("/"); + }, + isAbsolute(path) { + return path[0] === "/"; + }, + parse(path) { + return { + root: "/" + } + }, + normalize(path) { + return path; + }, + sep: "/", + } + } if (!globalThis.crypto) { throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)"); 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 From f490e6de8f2374047c5e0d091174d8dce2742780 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Sun, 11 Aug 2024 03:58:10 +0800 Subject: [PATCH 2/2] fix style --- misc/wasm/wasm_exec.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index 6531701c62643a..3cd2e08be106da 100644 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -73,25 +73,25 @@ } } - if (!globalThis.path) { - globalThis.path = { - resolve(...pathSegments) { - return pathSegments.join("/"); - }, - isAbsolute(path) { - return path[0] === "/"; - }, - parse(path) { - return { - root: "/" - } - }, - normalize(path) { - return path; - }, + if (!globalThis.path) { + globalThis.path = { + resolve(...pathSegments) { + return pathSegments.join("/"); + }, + isAbsolute(path) { + return path[0] === "/"; + }, + parse(path) { + return { + root: "/" + } + }, + normalize(path) { + return path; + }, sep: "/", - } - } + } + } if (!globalThis.crypto) { throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");