codecin 7.22.10__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +1057 -0
  3. codecin/assembler.py +619 -0
  4. codecin/cbackend.py +3024 -0
  5. codecin/cin.py +4265 -0
  6. codecin/cli.py +670 -0
  7. codecin/codecin_native.dll +0 -0
  8. codecin/config.py +50 -0
  9. codecin/console.py +183 -0
  10. codecin/cpu.py +312 -0
  11. codecin/crom.py +449 -0
  12. codecin/cvm.py +708 -0
  13. codecin/disasm.py +149 -0
  14. codecin/environment.py +176 -0
  15. codecin/errors.py +54 -0
  16. codecin/isa.py +560 -0
  17. codecin/lib/array/pic.toml +10 -0
  18. codecin/lib/array/src/array.cin +103 -0
  19. codecin/lib/bigint/pic.toml +10 -0
  20. codecin/lib/bigint/src/bigint.cin +589 -0
  21. codecin/lib/bits/pic.toml +10 -0
  22. codecin/lib/bits/src/bits.cin +143 -0
  23. codecin/lib/bitset/pic.toml +10 -0
  24. codecin/lib/bitset/src/bitset.cin +374 -0
  25. codecin/lib/codec/pic.toml +10 -0
  26. codecin/lib/codec/src/codec.cin +563 -0
  27. codecin/lib/combin/pic.toml +10 -0
  28. codecin/lib/combin/src/combin.cin +414 -0
  29. codecin/lib/conv/pic.toml +10 -0
  30. codecin/lib/conv/src/conv.cin +151 -0
  31. codecin/lib/cppstd/pic.toml +10 -0
  32. codecin/lib/cppstd/src/cppstd.cin +418 -0
  33. codecin/lib/cstd/pic.toml +10 -0
  34. codecin/lib/cstd/src/cstd.cin +424 -0
  35. codecin/lib/csv/pic.toml +10 -0
  36. codecin/lib/csv/src/csv.cin +405 -0
  37. codecin/lib/dp/pic.toml +10 -0
  38. codecin/lib/dp/src/dp.cin +325 -0
  39. codecin/lib/ffi/pic.toml +10 -0
  40. codecin/lib/ffi/src/ffi.cin +163 -0
  41. codecin/lib/fmt/pic.toml +10 -0
  42. codecin/lib/fmt/src/fmt.cin +325 -0
  43. codecin/lib/frac/pic.toml +10 -0
  44. codecin/lib/frac/src/frac.cin +252 -0
  45. codecin/lib/gostd/pic.toml +10 -0
  46. codecin/lib/gostd/src/gostd.cin +415 -0
  47. codecin/lib/graph/pic.toml +10 -0
  48. codecin/lib/graph/src/graph.cin +367 -0
  49. codecin/lib/gui/pic.toml +10 -0
  50. codecin/lib/gui/src/gui.cin +218 -0
  51. codecin/lib/hash/pic.toml +10 -0
  52. codecin/lib/hash/src/hash.cin +67 -0
  53. codecin/lib/heap/pic.toml +10 -0
  54. codecin/lib/heap/src/heap.cin +199 -0
  55. codecin/lib/io/pic.toml +10 -0
  56. codecin/lib/io/src/io.cin +141 -0
  57. codecin/lib/json/pic.toml +10 -0
  58. codecin/lib/json/src/json.cin +84 -0
  59. codecin/lib/key/pic.toml +10 -0
  60. codecin/lib/key/src/key.cin +78 -0
  61. codecin/lib/math/pic.toml +10 -0
  62. codecin/lib/math/src/math.cin +50 -0
  63. codecin/lib/matrix/pic.toml +10 -0
  64. codecin/lib/matrix/src/matrix.cin +155 -0
  65. codecin/lib/net/pic.toml +10 -0
  66. codecin/lib/net/src/net.cin +171 -0
  67. codecin/lib/path/pic.toml +10 -0
  68. codecin/lib/path/src/path.cin +372 -0
  69. codecin/lib/queue/pic.toml +10 -0
  70. codecin/lib/queue/src/queue.cin +112 -0
  71. codecin/lib/rand/pic.toml +10 -0
  72. codecin/lib/rand/src/rand.cin +46 -0
  73. codecin/lib/set/pic.toml +10 -0
  74. codecin/lib/set/src/set.cin +269 -0
  75. codecin/lib/sort/pic.toml +10 -0
  76. codecin/lib/sort/src/sort.cin +93 -0
  77. codecin/lib/stat/pic.toml +10 -0
  78. codecin/lib/stat/src/stat.cin +138 -0
  79. codecin/lib/str/pic.toml +10 -0
  80. codecin/lib/str/src/str.cin +54 -0
  81. codecin/lib/termux/pic.toml +10 -0
  82. codecin/lib/termux/src/termux.cin +118 -0
  83. codecin/lib/test/pic.toml +10 -0
  84. codecin/lib/test/src/test.cin +74 -0
  85. codecin/lib/text/pic.toml +10 -0
  86. codecin/lib/text/src/text.cin +323 -0
  87. codecin/lib/time/pic.toml +10 -0
  88. codecin/lib/time/src/time.cin +54 -0
  89. codecin/lib/token/pic.toml +10 -0
  90. codecin/lib/token/src/token.cin +183 -0
  91. codecin/lib/tree/pic.toml +10 -0
  92. codecin/lib/tree/src/tree.cin +256 -0
  93. codecin/lib/unionfind/pic.toml +10 -0
  94. codecin/lib/unionfind/src/unionfind.cin +115 -0
  95. codecin/lib/validate/pic.toml +10 -0
  96. codecin/lib/validate/src/validate.cin +148 -0
  97. codecin/lib/vec/pic.toml +10 -0
  98. codecin/lib/vec/src/vec.cin +88 -0
  99. codecin/logger.py +145 -0
  100. codecin/memory.py +306 -0
  101. codecin/mkn.py +916 -0
  102. codecin/native/aot/aot.go +114 -0
  103. codecin/native/aot/stub_main.go.txt +23 -0
  104. codecin/native/build.ps1 +33 -0
  105. codecin/native/build.sh +48 -0
  106. codecin/native/cmd/cincc/main.go +66 -0
  107. codecin/native/compiler/codegen.go +2238 -0
  108. codecin/native/compiler/compile_test.go +1069 -0
  109. codecin/native/compiler/parser.go +1331 -0
  110. codecin/native/compiler/syscalls.go +166 -0
  111. codecin/native/compiler/tokenizer.go +394 -0
  112. codecin/native/compiler/types.go +217 -0
  113. codecin/native/engine/args.go +37 -0
  114. codecin/native/engine/audio.go +402 -0
  115. codecin/native/engine/audio_other.go +105 -0
  116. codecin/native/engine/audio_test.go +279 -0
  117. codecin/native/engine/audio_windows.go +190 -0
  118. codecin/native/engine/canvas.go +271 -0
  119. codecin/native/engine/console_other.go +9 -0
  120. codecin/native/engine/console_windows.go +43 -0
  121. codecin/native/engine/crom.go +86 -0
  122. codecin/native/engine/encode.go +93 -0
  123. codecin/native/engine/engine_test.go +198 -0
  124. codecin/native/engine/ffi_nocgo.go +20 -0
  125. codecin/native/engine/ffi_registry.go +69 -0
  126. codecin/native/engine/ffi_unix.go +109 -0
  127. codecin/native/engine/ffi_windows.go +117 -0
  128. codecin/native/engine/gui.go +209 -0
  129. codecin/native/engine/gui_stub.go +13 -0
  130. codecin/native/engine/gui_test.go +148 -0
  131. codecin/native/engine/gui_windows.go +604 -0
  132. codecin/native/engine/gui_x11.go +723 -0
  133. codecin/native/engine/host_test.go +139 -0
  134. codecin/native/engine/hostcrypto.go +30 -0
  135. codecin/native/engine/hostdesktop.go +173 -0
  136. codecin/native/engine/hostnet.go +127 -0
  137. codecin/native/engine/isa_gen.go +689 -0
  138. codecin/native/engine/keyboard.go +432 -0
  139. codecin/native/engine/keyboard_other.go +94 -0
  140. codecin/native/engine/keyboard_stub.go +17 -0
  141. codecin/native/engine/keyboard_termios_darwin.go +9 -0
  142. codecin/native/engine/keyboard_termios_linux.go +9 -0
  143. codecin/native/engine/keyboard_test.go +246 -0
  144. codecin/native/engine/keyboard_windows.go +208 -0
  145. codecin/native/engine/keyboard_windows_test.go +123 -0
  146. codecin/native/engine/lineedit.go +262 -0
  147. codecin/native/engine/lineedit_test.go +105 -0
  148. codecin/native/engine/lineint_unix.go +17 -0
  149. codecin/native/engine/lineint_windows.go +19 -0
  150. codecin/native/engine/mem_other.go +37 -0
  151. codecin/native/engine/mem_windows.go +33 -0
  152. codecin/native/engine/native.go +640 -0
  153. codecin/native/engine/netsock.go +321 -0
  154. codecin/native/engine/ops_ext.go +515 -0
  155. codecin/native/engine/stdin.go +72 -0
  156. codecin/native/engine/stdin_other.go +9 -0
  157. codecin/native/engine/stdin_windows.go +60 -0
  158. codecin/native/engine/system.go +313 -0
  159. codecin/native/engine/termux.go +180 -0
  160. codecin/native/engine/version_gen.go +7 -0
  161. codecin/native/engine/vkb_darwin.go +62 -0
  162. codecin/native/engine/vkb_linux.go +95 -0
  163. codecin/native/engine/vkb_stub.go +11 -0
  164. codecin/native/engine/vm.go +1677 -0
  165. codecin/native/go.mod +3 -0
  166. codecin/native/ir/ir.go +48 -0
  167. codecin/native/main.go +448 -0
  168. codecin/native/mc/aarch64.go +165 -0
  169. codecin/native/mc/aarch64_test.go +30 -0
  170. codecin/native/mc/elf.go +88 -0
  171. codecin/native/mc/print_test.go +52 -0
  172. codecin/native/mc/x86_64.go +1262 -0
  173. codecin/native/mc/x86_64_test.go +102 -0
  174. codecin/native.py +432 -0
  175. codecin/native_codegen.py +987 -0
  176. codecin/nativegen.py +786 -0
  177. codecin/object.py +539 -0
  178. codecin/optimizer.py +236 -0
  179. codecin/packaging/__init__.py +46 -0
  180. codecin/packaging/archive.py +177 -0
  181. codecin/packaging/model.py +519 -0
  182. codecin/packaging/spec.py +193 -0
  183. codecin/packaging/store.py +372 -0
  184. codecin/packaging/toml.py +541 -0
  185. codecin/packaging/vcs.py +215 -0
  186. codecin/pic.py +1917 -0
  187. codecin/pic_api.py +374 -0
  188. codecin/pic_login.py +519 -0
  189. codecin/pic_registry.py +515 -0
  190. codecin/registers.py +130 -0
  191. codecin/sdgcc.py +493 -0
  192. codecin/stats.py +230 -0
  193. codecin/stdlib.py +138 -0
  194. codecin/toolchain.py +396 -0
  195. codecin/toolkit.py +26 -0
  196. codecin/version.py +260 -0
  197. codecin-7.22.10.dist-info/METADATA +1077 -0
  198. codecin-7.22.10.dist-info/RECORD +202 -0
  199. codecin-7.22.10.dist-info/WHEEL +5 -0
  200. codecin-7.22.10.dist-info/entry_points.txt +5 -0
  201. codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
  202. codecin-7.22.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,139 @@
1
+ package engine
2
+
3
+ import (
4
+ "path/filepath"
5
+ "strings"
6
+ "testing"
7
+ )
8
+
9
+ // 宿主能力 (SYS 80..115) 的引擎侧单元测试: 只覆盖确定性、无副作用或副作用
10
+ // 可完全控制的调用 (哈希/编码/路径/内存信息)。涉及真实系统交互 (剪贴板、
11
+ // 通知、网络、Termux) 的调用在 Python 侧测试中以门禁方式覆盖。
12
+
13
+ // newHostVM 构造一个可分配堆字符串的最小 vmState。
14
+ func newHostVM(size int) *vmState {
15
+ mem := make([]byte, size)
16
+ return &vmState{
17
+ mem: mem,
18
+ sp: uint64(size - 8),
19
+ heapPtr: uint64(size / 2),
20
+ }
21
+ }
22
+
23
+ // readHostString 读取 x0 指向的 NUL 结尾字符串。
24
+ func readHostString(vm *vmState, ptr uint64) string {
25
+ return vm.readCString(ptr)
26
+ }
27
+
28
+ func TestHostSHA256AndBase64(t *testing.T) {
29
+ vm := newHostVM(4096)
30
+
31
+ got := readHostString(vm, vm.sha256Hex("abc"))
32
+ want := "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
33
+ if got != want {
34
+ t.Fatalf("sha256(abc) = %q, 期望 %q", got, want)
35
+ }
36
+
37
+ enc := readHostString(vm, vm.base64Encode("hello"))
38
+ if enc != "aGVsbG8=" {
39
+ t.Fatalf("base64_encode(hello) = %q", enc)
40
+ }
41
+ dec := readHostString(vm, vm.base64Decode("aGVsbG8="))
42
+ if dec != "hello" {
43
+ t.Fatalf("base64_decode 往返失败: %q", dec)
44
+ }
45
+
46
+ // 非法 Base64 -> 空串 (不报错、不 panic)
47
+ if bad := readHostString(vm, vm.base64Decode("!!!")); bad != "" {
48
+ t.Fatalf("非法 Base64 应返回空串, 实际 %q", bad)
49
+ }
50
+
51
+ // UTF-8 字节参与哈希 (中文 3 字节)
52
+ if n := len(readHostString(vm, vm.sha256Hex("中"))); n != 64 {
53
+ t.Fatalf("sha256 十六进制长度应为 64, 实际 %d", n)
54
+ }
55
+ }
56
+
57
+ func TestHostPathHelpers(t *testing.T) {
58
+ vm := newHostVM(4096)
59
+
60
+ joined := readHostString(vm, vm.pathJoin("a", "b.txt"))
61
+ if joined != filepath.Join("a", "b.txt") {
62
+ t.Fatalf("path_join = %q", joined)
63
+ }
64
+ if base := readHostString(vm, vm.pathBasename(joined)); base != "b.txt" {
65
+ t.Fatalf("path_basename = %q", base)
66
+ }
67
+ if dir := readHostString(vm, vm.pathDirname(joined)); dir != "a" {
68
+ t.Fatalf("path_dirname = %q", dir)
69
+ }
70
+ if abs := readHostString(vm, vm.pathAbs(".")); abs == "" {
71
+ t.Fatal("path_abs 返回空串")
72
+ }
73
+ if tmp := readHostString(vm, vm.tempDir()); tmp == "" {
74
+ t.Fatal("temp_dir 返回空串")
75
+ }
76
+ }
77
+
78
+ func TestHostSystemInfo(t *testing.T) {
79
+ vm := newHostVM(4096)
80
+
81
+ if n := vm.cpuCount(); n == 0 {
82
+ t.Fatal("cpu_count 返回 0")
83
+ }
84
+ if arch := readHostString(vm, vm.archName()); arch == "" {
85
+ t.Fatal("arch_name 返回空串")
86
+ }
87
+ info := readHostString(vm, vm.memInfo())
88
+ if !strings.Contains(info, `"total_kb":`) ||
89
+ !strings.Contains(info, `"free_kb":`) {
90
+ t.Fatalf("mem_info 结构不符: %q", info)
91
+ }
92
+
93
+ // 时间: 毫秒时间戳应为正数
94
+ if ms := vm.timeMs(); ms < 1_600_000_000_000 {
95
+ t.Fatalf("time_ms 异常: %d", ms)
96
+ }
97
+ // is_android 只允许 0/1
98
+ if a := vm.isAndroid(); a != 0 && a != 1 {
99
+ t.Fatalf("is_android 应返回 0/1, 实际 %d", a)
100
+ }
101
+ // sleep_ms(0) 不得报错
102
+ if code := vm.sleepMs(0); code != 0 {
103
+ t.Fatalf("sleep_ms(0) = %d", code)
104
+ }
105
+ }
106
+
107
+ func TestHostFileHelpers(t *testing.T) {
108
+ dir := t.TempDir()
109
+ vm := newHostVM(8192)
110
+
111
+ src := filepath.Join(dir, "a.txt")
112
+ dst := filepath.Join(dir, "b.txt")
113
+ moved := filepath.Join(dir, "c.txt")
114
+ if code := vm.fileWrite(src, "hello"); code != 0 {
115
+ t.Fatalf("file_write = %d", code)
116
+ }
117
+ if code := vm.fileCopy(src, dst); code != 0 {
118
+ t.Fatalf("file_copy = %d", code)
119
+ }
120
+ if code := vm.fileMove(dst, moved); code != 0 {
121
+ t.Fatalf("file_move = %d", code)
122
+ }
123
+ if code := vm.fileSize(moved); code != 5 {
124
+ t.Fatalf("file_size = %d, 期望 5", code)
125
+ }
126
+ if code := vm.fileMtime(moved); code <= 0 {
127
+ t.Fatalf("file_mtime = %d, 期望正数", code)
128
+ }
129
+ if code := vm.isDir(dir); code != 1 {
130
+ t.Fatalf("is_dir(目录) = %d", code)
131
+ }
132
+ if code := vm.isDir(moved); code != 0 {
133
+ t.Fatalf("is_dir(文件) = %d", code)
134
+ }
135
+ // 失败路径: 不存在的文件
136
+ if code := vm.fileMtime(filepath.Join(dir, "nope")); code != mask64 {
137
+ t.Fatalf("file_mtime(不存在) = %d, 期望 -1", code)
138
+ }
139
+ }
@@ -0,0 +1,30 @@
1
+ package engine
2
+
3
+ import (
4
+ "crypto/sha256"
5
+ "encoding/base64"
6
+ "encoding/hex"
7
+ "strings"
8
+ )
9
+
10
+ // 编码与哈希 (纯标准库, Windows/Linux/Android 行为一致)。
11
+
12
+ // sha256Hex 返回 UTF-8 字节的 SHA-256 十六进制摘要 (小写)。
13
+ func (vm *vmState) sha256Hex(s string) uint64 {
14
+ sum := sha256.Sum256([]byte(s))
15
+ return vm.hs(hex.EncodeToString(sum[:]))
16
+ }
17
+
18
+ // base64Encode 对 UTF-8 字节做标准 Base64 编码 (带 '=' 填充)。
19
+ func (vm *vmState) base64Encode(s string) uint64 {
20
+ return vm.hs(base64.StdEncoding.EncodeToString([]byte(s)))
21
+ }
22
+
23
+ // base64Decode 解码标准 Base64; 非法输入返回空串。
24
+ func (vm *vmState) base64Decode(s string) uint64 {
25
+ raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(s))
26
+ if err != nil {
27
+ return vm.empty()
28
+ }
29
+ return vm.hs(string(raw))
30
+ }
@@ -0,0 +1,173 @@
1
+ package engine
2
+
3
+ import (
4
+ "os/exec"
5
+ "runtime"
6
+ "strings"
7
+ )
8
+
9
+ // 桌面/移动端集成: 剪贴板、系统通知、打开 URL、Android Intent。
10
+ //
11
+ // 分发顺序: Termux (Android) -> 平台原生命令 (Windows / macOS / Linux)。
12
+ // 每条命令都先 LookPath, 缺失即优雅失败 (返回 -1 / 空串), 不 panic、不阻塞。
13
+ // 平台命令行在两个平台上保持同一份实现, 因此行为可预期且可测试。
14
+
15
+ // runCapture 执行命令 (可选 stdin), 返回 (stdout, 0/-1)。
16
+ func runCapture(stdin string, name string, args ...string) (string, uint64) {
17
+ path, err := exec.LookPath(name)
18
+ if err != nil {
19
+ return "", mask64
20
+ }
21
+ cmd := exec.Command(path, args...)
22
+ if stdin != "" {
23
+ cmd.Stdin = strings.NewReader(stdin)
24
+ }
25
+ out, err := cmd.Output()
26
+ if err != nil {
27
+ return string(out), mask64
28
+ }
29
+ return string(out), 0
30
+ }
31
+
32
+ // ---------------- 剪贴板 ----------------
33
+
34
+ func clipboardRead() (string, uint64) {
35
+ if TermuxAvailable() {
36
+ if out, code := termuxRun("termux-clipboard-get"); code == 0 {
37
+ return out, 0
38
+ }
39
+ }
40
+ switch runtime.GOOS {
41
+ case "windows":
42
+ return runCapture("", "powershell", "-NoProfile", "-Command",
43
+ "Get-Clipboard -Raw")
44
+ case "darwin":
45
+ return runCapture("", "pbpaste")
46
+ default: // linux / android
47
+ candidates := [][]string{
48
+ {"wl-paste", "--no-newline"},
49
+ {"xclip", "-selection", "clipboard", "-o"},
50
+ {"xsel", "-b"},
51
+ }
52
+ for _, cand := range candidates {
53
+ if _, err := exec.LookPath(cand[0]); err != nil {
54
+ continue
55
+ }
56
+ if out, code := runCapture("", cand[0], cand[1:]...); code == 0 {
57
+ return out, 0
58
+ }
59
+ }
60
+ }
61
+ return "", mask64
62
+ }
63
+
64
+ func clipboardWrite(text string) uint64 {
65
+ if TermuxAvailable() {
66
+ if _, code := termuxRun("termux-clipboard-set", text); code == 0 {
67
+ return 0
68
+ }
69
+ }
70
+ switch runtime.GOOS {
71
+ case "windows":
72
+ _, code := runCapture(text, "cmd", "/c", "clip")
73
+ return code
74
+ case "darwin":
75
+ _, code := runCapture(text, "pbcopy")
76
+ return code
77
+ default: // linux / android
78
+ candidates := [][]string{
79
+ {"wl-copy"},
80
+ {"xclip", "-selection", "clipboard", "-i"},
81
+ {"xsel", "-b", "-i"},
82
+ }
83
+ for _, cand := range candidates {
84
+ if _, err := exec.LookPath(cand[0]); err != nil {
85
+ continue
86
+ }
87
+ if _, code := runCapture(text, cand[0], cand[1:]...); code == 0 {
88
+ return 0
89
+ }
90
+ }
91
+ }
92
+ return mask64
93
+ }
94
+
95
+ func (vm *vmState) clipboardGet() uint64 {
96
+ out, code := clipboardRead()
97
+ if code != 0 {
98
+ return vm.empty()
99
+ }
100
+ return vm.hs(strings.TrimRight(out, "\r\n"))
101
+ }
102
+
103
+ func (vm *vmState) clipboardSet(text string) uint64 {
104
+ return clipboardWrite(text)
105
+ }
106
+
107
+ // ---------------- 系统通知 ----------------
108
+
109
+ func (vm *vmState) notify(title, body string) uint64 {
110
+ if TermuxAvailable() {
111
+ if _, code := termuxRun("termux-notification",
112
+ "--title", title, "--content", body); code == 0 {
113
+ return 0
114
+ }
115
+ }
116
+ switch runtime.GOOS {
117
+ case "windows":
118
+ // Wscript.Shell.Popup: 10 秒后自动消失, 无需额外模块。
119
+ script := "(New-Object -ComObject Wscript.Shell).Popup('" +
120
+ psSingleQuote(body) + "',10,'" + psSingleQuote(title) + "',64)"
121
+ _, code := runCapture("", "powershell", "-NoProfile", "-Command", script)
122
+ return code
123
+ case "darwin":
124
+ script := "display notification \"" + osaDoubleQuote(body) +
125
+ "\" with title \"" + osaDoubleQuote(title) + "\""
126
+ _, code := runCapture("", "osascript", "-e", script)
127
+ return code
128
+ default: // linux / android
129
+ if _, err := exec.LookPath("notify-send"); err == nil {
130
+ _, code := runCapture("", "notify-send", title, body)
131
+ return code
132
+ }
133
+ }
134
+ return mask64
135
+ }
136
+
137
+ // psSingleQuote 转义 PowerShell 单引号字符串内的单引号。
138
+ func psSingleQuote(s string) string {
139
+ return strings.ReplaceAll(s, "'", "''")
140
+ }
141
+
142
+ // osaDoubleQuote 转义 AppleScript 双引号字符串内的引号与反斜杠。
143
+ func osaDoubleQuote(s string) string {
144
+ s = strings.ReplaceAll(s, "\\", "\\\\")
145
+ return strings.ReplaceAll(s, "\"", "\\\"")
146
+ }
147
+
148
+ // ---------------- 打开 URL / 默认程序 ----------------
149
+
150
+ func (vm *vmState) openURL(url string) uint64 {
151
+ if TermuxAvailable() {
152
+ if _, code := termuxRun("termux-open-url", url); code == 0 {
153
+ return 0
154
+ }
155
+ }
156
+ switch runtime.GOOS {
157
+ case "windows":
158
+ // start 的第一个参数是窗口标题, 必须给空串
159
+ _, code := runCapture("", "cmd", "/c", "start", "", url)
160
+ return code
161
+ case "darwin":
162
+ _, code := runCapture("", "open", url)
163
+ return code
164
+ case "android":
165
+ return vm.androidIntent("android.intent.action.VIEW", url)
166
+ default: // linux
167
+ if _, err := exec.LookPath("xdg-open"); err == nil {
168
+ _, code := runCapture("", "xdg-open", url)
169
+ return code
170
+ }
171
+ }
172
+ return mask64
173
+ }
@@ -0,0 +1,127 @@
1
+ package engine
2
+
3
+ import (
4
+ "io"
5
+ "net/http"
6
+ "os"
7
+ "strings"
8
+ "time"
9
+ )
10
+
11
+ // 网络能力 (Go 标准库 net/http; Windows/Linux/Android 通用)。
12
+ // 所有调用都带超时与大小上限, 失败时返回空串 / -1, 不 panic。
13
+ const (
14
+ httpTimeout = 15 * time.Second
15
+ maxHTTPBodyBytes = 8 << 20 // 8 MiB: 单次 http_get/http_post 返回上限
16
+ maxDownloadBytes = 256 << 20 // 256 MiB: download 落盘上限
17
+ )
18
+
19
+ var httpClient = &http.Client{Timeout: httpTimeout}
20
+
21
+ // readBody 读取响应体 (带上限); 非 2xx 状态码由调用方决定处理方式。
22
+ func readBody(resp *http.Response) (string, bool) {
23
+ defer resp.Body.Close()
24
+ body, err := io.ReadAll(io.LimitReader(resp.Body, maxHTTPBodyBytes))
25
+ if err != nil {
26
+ return "", false
27
+ }
28
+ return string(body), true
29
+ }
30
+
31
+ // httpGet 发起 GET 请求, 返回响应体 (失败为空串)。
32
+ // 状态码同样记录到 lastHTTPSt (与 http_req 一致, 供 http_code 查询)。
33
+ func (vm *vmState) httpGet(url string) uint64 {
34
+ vm.lastHTTPSt = -1
35
+ resp, err := httpClient.Get(url)
36
+ if err != nil {
37
+ return vm.empty()
38
+ }
39
+ vm.lastHTTPSt = int64(resp.StatusCode)
40
+ body, ok := readBody(resp)
41
+ if !ok {
42
+ return vm.empty()
43
+ }
44
+ return vm.hs(body)
45
+ }
46
+
47
+ // httpPost 发起 POST (text/plain; charset=utf-8), 返回响应体 (失败为空串)。
48
+ func (vm *vmState) httpPost(url, body string) uint64 {
49
+ vm.lastHTTPSt = -1
50
+ resp, err := httpClient.Post(url, "text/plain; charset=utf-8",
51
+ strings.NewReader(body))
52
+ if err != nil {
53
+ return vm.empty()
54
+ }
55
+ vm.lastHTTPSt = int64(resp.StatusCode)
56
+ out, ok := readBody(resp)
57
+ if !ok {
58
+ return vm.empty()
59
+ }
60
+ return vm.hs(out)
61
+ }
62
+
63
+ // download 下载 url 到本地文件; 0 成功 / -1 失败 (含非 2xx 状态码)。
64
+ func (vm *vmState) download(url, path string) uint64 {
65
+ resp, err := httpClient.Get(url)
66
+ if err != nil {
67
+ return mask64
68
+ }
69
+ defer resp.Body.Close()
70
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
71
+ return mask64
72
+ }
73
+ f, err := os.Create(path)
74
+ if err != nil {
75
+ return mask64
76
+ }
77
+ defer f.Close()
78
+ if _, err := io.Copy(f, io.LimitReader(resp.Body, maxDownloadBytes)); err != nil {
79
+ return mask64
80
+ }
81
+ return 0
82
+ }
83
+
84
+ // httpReq 发起任意方法的 HTTP 请求 (SYS 145)。
85
+ // headers 为 '\n' 分隔的 "Key: Value" 行 (可空); body 可空。
86
+ // 返回响应体 (新堆字符串; 失败为空串), 状态码记录到 lastHTTPSt 供
87
+ // SYS 146 (http_code) 查询; 请求失败时状态码为 -1。
88
+ func (vm *vmState) httpReq(method, url, headers, body string) uint64 {
89
+ vm.lastHTTPSt = -1
90
+ if method == "" {
91
+ method = http.MethodGet
92
+ }
93
+ var rd io.Reader
94
+ if body != "" {
95
+ rd = strings.NewReader(body)
96
+ }
97
+ req, err := http.NewRequest(method, url, rd)
98
+ if err != nil {
99
+ return vm.empty()
100
+ }
101
+ for _, line := range strings.Split(headers, "\n") {
102
+ line = strings.TrimSpace(line)
103
+ if line == "" {
104
+ continue
105
+ }
106
+ k, v, ok := strings.Cut(line, ":")
107
+ if !ok {
108
+ continue
109
+ }
110
+ k = strings.TrimSpace(k)
111
+ v = strings.TrimSpace(v)
112
+ if k == "" || strings.EqualFold(k, "Host") {
113
+ continue
114
+ }
115
+ req.Header.Set(k, v)
116
+ }
117
+ resp, err := httpClient.Do(req)
118
+ if err != nil {
119
+ return vm.empty()
120
+ }
121
+ vm.lastHTTPSt = int64(resp.StatusCode)
122
+ out, ok := readBody(resp)
123
+ if !ok {
124
+ return vm.empty()
125
+ }
126
+ return vm.hs(out)
127
+ }