Skip to content

Commit 104ab65

Browse files
author
Zing
committed
refactor(js引擎): 替换goja_nodejs为自定义require模块并支持os文件系统访问
1 parent e7fe39c commit 104ab65

10 files changed

Lines changed: 56 additions & 34 deletions

File tree

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ require (
1414
require (
1515
filippo.io/edwards25519 v1.1.0 // indirect
1616
github.com/dlclark/regexp2 v1.11.4 // indirect
17-
github.com/dop251/goja_nodejs v0.0.0-20260212111938-1f56ff5bcf14 // indirect
1817
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
1918
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
2019
golang.org/x/text v0.16.0 // indirect
20+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
2121
)
22-
23-
replace github.com/Dasongzi1366/AutoGo => ./AutoGo

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yA
66
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
77
github.com/dop251/goja v0.0.0-20260311135729-065cd970411c h1:OcLmPfx1T1RmZVHHFwWMPaZDdRf0DBMZOFMVWJa7Pdk=
88
github.com/dop251/goja v0.0.0-20260311135729-065cd970411c/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
9-
github.com/dop251/goja_nodejs v0.0.0-20260212111938-1f56ff5bcf14 h1:3U8dTgyNBhEQ/GVw0jZW5q+93Zw2gAZPRWhJ9TwV3rM=
10-
github.com/dop251/goja_nodejs v0.0.0-20260212111938-1f56ff5bcf14/go.mod h1:Tb7Xxye4LX7cT3i8YLvmPMGCV92IOi4CDZvm/V8ylc0=
11-
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
12-
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
139
github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q=
1410
github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
1511
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
1612
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
17-
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
18-
github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg=
1913
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
2014
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
2115
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
@@ -24,8 +18,6 @@ github.com/makiuchi-d/gozxing v0.1.1 h1:xxqijhoedi+/lZlhINteGbywIrewVdVv2wl9r5O9
2418
github.com/makiuchi-d/gozxing v0.1.1/go.mod h1:eRIHbOjX7QWxLIDJoQuMLhuXg9LAuw6znsUtRkNw9DU=
2519
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
2620
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
27-
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
28-
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
2921
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
3022
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
3123
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=

js_engine/.DS_Store

0 Bytes
Binary file not shown.

js_engine/define/.DS_Store

0 Bytes
Binary file not shown.

js_engine/js_engine.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"time"
1212

1313
"github.com/ZingYao/autogo_scriptengine/js_engine/model"
14+
"github.com/ZingYao/autogo_scriptengine/js_engine/model/require"
1415

1516
"github.com/dop251/goja"
16-
noderequire "github.com/dop251/goja_nodejs/require"
1717
)
1818

1919
// EngineState 引擎状态
@@ -97,9 +97,19 @@ func (e *JSEngine) init() {
9797

9898
// registerNodeJS 注册 Node.js 能力
9999
func (e *JSEngine) registerNodeJS() {
100-
// 注册 Node.js require 功能
101-
registry := new(noderequire.Registry)
102-
registry.Enable(e.vm)
100+
// 使用自定义的 require 模块
101+
var requireModule *require.RequireModule
102+
103+
if e.config.FileSystem != nil {
104+
// 如果配置了自定义文件系统,使用 fs.FS
105+
requireModule = require.NewRequireModule(e.vm, e.config.FileSystem)
106+
} else {
107+
// 否则使用 os 包直接访问文件系统(支持绝对路径)
108+
requireModule = require.NewRequireModuleWithOS(e.vm)
109+
}
110+
111+
// 注册 require 模块
112+
requireModule.Register()
103113

104114
// 注册 console 模块
105115
consoleObj := e.vm.NewObject()
@@ -112,10 +122,6 @@ func (e *JSEngine) registerNodeJS() {
112122
// 注册 process 模块
113123
processObj := e.vm.NewObject()
114124
e.vm.Set("process", processObj)
115-
116-
// 注册 __dirname 和 __filename
117-
e.vm.Set("__dirname", ".")
118-
e.vm.Set("__filename", "script.js")
119125
}
120126

121127
// Start 启动引擎
@@ -429,17 +435,12 @@ func (e *JSEngine) executeStringOnce(script string, dir ...string) error {
429435
return fmt.Errorf("JavaScript engine not initialized")
430436
}
431437

432-
// 如果配置了文件系统且 __dirname 未设置,设置 __dirname
433-
if e.config.FileSystem != nil {
434-
currentDir := e.vm.Get("__dirname")
435-
if currentDir == goja.Undefined() || currentDir.String() == "" {
436-
__dirname := "scripts"
437-
if len(dir) > 0 && dir[0] != "" {
438-
__dirname = dir[0]
439-
}
440-
e.vm.Set("__dirname", __dirname)
441-
}
438+
// 设置 __dirname(每次执行都更新,确保使用正确的工作目录)
439+
__dirname := "scripts"
440+
if len(dir) > 0 && dir[0] != "" {
441+
__dirname = dir[0]
442442
}
443+
e.vm.Set("__dirname", __dirname)
443444

444445
// 注册特殊的 process.exit 函数,用于控制退出动作
445446
e.registerExitControl()

js_engine/model/.DS_Store

0 Bytes
Binary file not shown.

js_engine/model/require/require_inject.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package require
33
import (
44
"fmt"
55
"io/fs"
6+
"os"
67
"path/filepath"
78
"strings"
89
"sync"
@@ -17,6 +18,7 @@ type RequireModule struct {
1718
mu sync.RWMutex
1819
fileSys fs.FS
1920
initialDir string // 初始目录,用于主脚本
21+
useOS bool // 是否使用 os 包直接访问文件系统(支持绝对路径)
2022
}
2123

2224
// NewRequireModule 创建新的 require 模块
@@ -26,6 +28,17 @@ func NewRequireModule(vm *goja.Runtime, fileSys fs.FS) *RequireModule {
2628
cache: make(map[string]goja.Value),
2729
fileSys: fileSys,
2830
initialDir: "scripts", // 默认初始目录
31+
useOS: false,
32+
}
33+
}
34+
35+
// NewRequireModuleWithOS 创建支持直接文件系统访问的 require 模块
36+
func NewRequireModuleWithOS(vm *goja.Runtime) *RequireModule {
37+
return &RequireModule{
38+
vm: vm,
39+
cache: make(map[string]goja.Value),
40+
initialDir: "scripts",
41+
useOS: true,
2942
}
3043
}
3144

@@ -135,35 +148,53 @@ func (r *RequireModule) resolveModule(modulePath string) (string, error) {
135148
// resolveExtension 解析文件扩展名
136149
func (r *RequireModule) resolveExtension(path string) (string, error) {
137150
// 尝试直接读取
138-
if _, err := fs.Stat(r.fileSys, path); err == nil {
151+
if r.fileExists(path) {
139152
return path, nil
140153
}
141154

142155
// 尝试添加 .js 扩展名
143156
jsPath := path + ".js"
144-
if _, err := fs.Stat(r.fileSys, jsPath); err == nil {
157+
if r.fileExists(jsPath) {
145158
return jsPath, nil
146159
}
147160

148161
// 尝试添加 .json 扩展名
149162
jsonPath := path + ".json"
150-
if _, err := fs.Stat(r.fileSys, jsonPath); err == nil {
163+
if r.fileExists(jsonPath) {
151164
return jsonPath, nil
152165
}
153166

154167
// 尝试 index.js
155168
indexPath := filepath.Join(path, "index.js")
156-
if _, err := fs.Stat(r.fileSys, indexPath); err == nil {
169+
if r.fileExists(indexPath) {
157170
return indexPath, nil
158171
}
159172

160173
return "", fmt.Errorf("module not found: %s", path)
161174
}
162175

176+
// fileExists 检查文件是否存在
177+
func (r *RequireModule) fileExists(path string) bool {
178+
if r.useOS {
179+
_, err := os.Stat(path)
180+
return err == nil
181+
}
182+
_, err := fs.Stat(r.fileSys, path)
183+
return err == nil
184+
}
185+
186+
// readFile 读取文件内容
187+
func (r *RequireModule) readFile(path string) ([]byte, error) {
188+
if r.useOS {
189+
return os.ReadFile(path)
190+
}
191+
return fs.ReadFile(r.fileSys, path)
192+
}
193+
163194
// loadModule 加载模块
164195
func (r *RequireModule) loadModule(path string) (goja.Value, error) {
165196
// 读取文件内容
166-
content, err := fs.ReadFile(r.fileSys, path)
197+
content, err := r.readFile(path)
167198
if err != nil {
168199
return nil, fmt.Errorf("failed to read file: %v", err)
169200
}

lua_engine/.DS_Store

0 Bytes
Binary file not shown.

lua_engine/define/.DS_Store

0 Bytes
Binary file not shown.

lua_engine/model/.DS_Store

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)