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,20 @@
1
+ //go:build !cgo
2
+
3
+ package engine
4
+
5
+ // ffi_nocgo.go: CGO_ENABLED=0 (AOT 静态构建) 下的 FFI 桩。
6
+ // LoadLibrary/dlopen 依赖 cgo, 静态构建中不可用; 这里保持 engine 包
7
+ // 可编译 —— FFI 系统调用在运行时返回明确错误, 而非构建期链接失败。
8
+ // 其余能力 (VM/网络/音频/画布) 不受影响。
9
+
10
+ func (vm *vmState) ffiOpen(path string) uint64 { return 0 }
11
+
12
+ func (vm *vmState) ffiSym(handle uint64, name string) uint64 { return 0 }
13
+
14
+ func (vm *vmState) ffiCall(fnID, argbuf, nargs uint64,
15
+ wantFloat bool) (uint64, string) {
16
+ return 0, "FFI unavailable: built without cgo (static AOT build); " +
17
+ "use the dynamic-library runtime for ffi_*"
18
+ }
19
+
20
+ func (vm *vmState) ffiClose(handle uint64) uint64 { return mask64 }
@@ -0,0 +1,69 @@
1
+ package engine
2
+
3
+ // ffi_registry.go: FFI 句柄注册表 (平台无关部分)。
4
+ //
5
+ // dlopen/dlsym 的原生指针用递增整数句柄暴露给 CIN 程序: 指针不在内存镜像
6
+ // 里搬运, go vet 的 unsafeptr 检查也保持安静。
7
+
8
+ import (
9
+ "sync"
10
+ "unsafe"
11
+ )
12
+
13
+ var ffiRegistry = struct {
14
+ sync.Mutex
15
+ next uint64
16
+ libs map[uint64]unsafe.Pointer // dlopen 句柄
17
+ fns map[uint64]unsafe.Pointer // dlsym 函数指针
18
+ }{
19
+ next: 1,
20
+ libs: map[uint64]unsafe.Pointer{},
21
+ fns: map[uint64]unsafe.Pointer{},
22
+ }
23
+
24
+ // ffiLibGet 取出 dlopen 句柄 (不存在返回 false)。
25
+ func ffiLibGet(handle uint64) (unsafe.Pointer, bool) {
26
+ ffiRegistry.Lock()
27
+ defer ffiRegistry.Unlock()
28
+ lib, ok := ffiRegistry.libs[handle]
29
+ return lib, ok
30
+ }
31
+
32
+ // ffiLibPut 登记句柄, 返回暴露给 CIN 的整数 id。
33
+ func ffiLibPut(p unsafe.Pointer) uint64 {
34
+ ffiRegistry.Lock()
35
+ defer ffiRegistry.Unlock()
36
+ id := ffiRegistry.next
37
+ ffiRegistry.next++
38
+ ffiRegistry.libs[id] = p
39
+ return id
40
+ }
41
+
42
+ // ffiFnGet 取出函数指针。
43
+ func ffiFnGet(fnID uint64) (unsafe.Pointer, bool) {
44
+ ffiRegistry.Lock()
45
+ defer ffiRegistry.Unlock()
46
+ fp, ok := ffiRegistry.fns[fnID]
47
+ return fp, ok
48
+ }
49
+
50
+ // ffiFnPut 登记函数指针, 返回暴露给 CIN 的整数 id。
51
+ func ffiFnPut(p unsafe.Pointer) uint64 {
52
+ ffiRegistry.Lock()
53
+ defer ffiRegistry.Unlock()
54
+ id := ffiRegistry.next
55
+ ffiRegistry.next++
56
+ ffiRegistry.fns[id] = p
57
+ return id
58
+ }
59
+
60
+ // ffiLibDel 注销 dlopen 句柄 (返回是否存在)。
61
+ func ffiLibDel(handle uint64) (unsafe.Pointer, bool) {
62
+ ffiRegistry.Lock()
63
+ defer ffiRegistry.Unlock()
64
+ lib, ok := ffiRegistry.libs[handle]
65
+ if ok {
66
+ delete(ffiRegistry.libs, handle)
67
+ }
68
+ return lib, ok
69
+ }
@@ -0,0 +1,109 @@
1
+ //go:build !windows && cgo
2
+
3
+ package engine
4
+
5
+ // ffi_unix.go: 动态库加载与调用 (Linux/Termux/macOS: dlopen/dlsym)。
6
+ // 语义见 ffi_windows.go 顶部注释。
7
+
8
+ import (
9
+ "encoding/binary"
10
+ "fmt"
11
+ "unsafe"
12
+ )
13
+
14
+ /*
15
+ #cgo linux LDFLAGS: -ldl
16
+ #include <dlfcn.h>
17
+ #include <stdlib.h>
18
+ #include <string.h>
19
+ typedef long long (*ffi_fn8)(long long, long long, long long, long long,
20
+ long long, long long, long long, long long);
21
+ typedef double (*ffi_fnf8)(double, double, double, double,
22
+ double, double, double, double);
23
+ static void* ffi_open(const char* path) {
24
+ return dlopen(path, RTLD_NOW | RTLD_LOCAL);
25
+ }
26
+ static void* ffi_sym(void* h, const char* name) {
27
+ return dlsym(h, name);
28
+ }
29
+ static void ffi_close(void* h) { dlclose(h); }
30
+ static long long ffi_call8(void* f, long long* a) {
31
+ return ((ffi_fn8)f)(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
32
+ }
33
+ // 浮点调用: a[] 存 IEEE754 位模式, memcpy 到 double 数组保持位模式,
34
+ // 按 double 传参 —— SysV ABI 下浮点参数走 XMM0-XMM7 (整型声明走
35
+ // RDI/RSI/..., 目标函数从 XMM 读到垃圾)。
36
+ static double ffi_callf8(void* f, long long* a) {
37
+ double d[8];
38
+ memcpy(d, a, sizeof d);
39
+ return ((ffi_fnf8)f)(d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]);
40
+ }
41
+ */
42
+ import "C"
43
+
44
+ func (vm *vmState) ffiOpen(path string) uint64 {
45
+ if path == "" {
46
+ return 0
47
+ }
48
+ cpath := C.CString(path)
49
+ defer C.free(unsafe.Pointer(cpath))
50
+ h := C.ffi_open(cpath)
51
+ if h == nil {
52
+ return 0
53
+ }
54
+ return ffiLibPut(h)
55
+ }
56
+
57
+ func (vm *vmState) ffiSym(handle uint64, name string) uint64 {
58
+ if name == "" {
59
+ return 0
60
+ }
61
+ lib, ok := ffiLibGet(handle)
62
+ if !ok {
63
+ return 0
64
+ }
65
+ cname := C.CString(name)
66
+ defer C.free(unsafe.Pointer(cname))
67
+ fp := C.ffi_sym(lib, cname)
68
+ if fp == nil {
69
+ return 0
70
+ }
71
+ return ffiFnPut(fp)
72
+ }
73
+
74
+ // ffiCall 执行 ffi_call / ffi_callf。返回值: 整数调用为 int64;
75
+ // 浮点调用为 IEEE754 位模式。
76
+ func (vm *vmState) ffiCall(fnID, argbuf, nargs uint64, wantFloat bool) (uint64, string) {
77
+ if nargs > 8 {
78
+ return 0, fmt.Sprintf("FFI call: too many arguments (%d, max 8)", nargs)
79
+ }
80
+ fp, ok := ffiFnGet(fnID)
81
+ if !ok {
82
+ return 0, "FFI call: invalid function handle"
83
+ }
84
+ var args [8]C.longlong
85
+ if nargs > 0 {
86
+ if e := vm.checkAddr(argbuf, int(nargs)*8); e != "" {
87
+ return 0, "FFI call: " + e
88
+ }
89
+ for i := uint64(0); i < nargs; i++ {
90
+ args[i] = C.longlong(int64(binary.LittleEndian.Uint64(
91
+ vm.mem[argbuf+i*8 : argbuf+i*8+8])))
92
+ }
93
+ }
94
+ if wantFloat {
95
+ r := C.ffi_callf8(fp, &args[0])
96
+ return fToBits(float64(r)), ""
97
+ }
98
+ r := C.ffi_call8(fp, &args[0])
99
+ return uint64(int64(r)) & mask64, ""
100
+ }
101
+
102
+ func (vm *vmState) ffiClose(handle uint64) uint64 {
103
+ lib, ok := ffiLibDel(handle)
104
+ if !ok {
105
+ return mask64
106
+ }
107
+ C.ffi_close(lib)
108
+ return 0
109
+ }
@@ -0,0 +1,117 @@
1
+ //go:build windows && cgo
2
+
3
+ package engine
4
+
5
+ // ffi_windows.go: 动态库加载与调用 (Windows: LoadLibrary / GetProcAddress)。
6
+ //
7
+ // 语义 (SYS 140-144):
8
+ // - dlopen(path) -> 句柄 (0 失败)
9
+ // - dlsym(handle,name) -> 函数句柄 (0 失败)
10
+ // - ffi_call(fn, argbuf, nargs) -> long long (int64)
11
+ // - ffi_callf(fn, argbuf, nargs) -> double (按 IEEE754 位模式存 X0)
12
+ // - lib_close(handle) -> 0 成功 / -1
13
+ //
14
+ // 调用约定: 最多 8 个 int64 参数, 从 vm 内存 argbuf 顺序读取; 浮点参数由
15
+ // CIN 侧以位模式预先写入 argbuf。
16
+
17
+ import (
18
+ "encoding/binary"
19
+ "fmt"
20
+ "unsafe"
21
+ )
22
+
23
+ /*
24
+ #include <windows.h>
25
+ #include <stdlib.h>
26
+ #include <string.h>
27
+ typedef long long (*ffi_fn8)(long long, long long, long long, long long,
28
+ long long, long long, long long, long long);
29
+ typedef double (*ffi_fnf8)(double, double, double, double,
30
+ double, double, double, double);
31
+ static void* ffi_open(const char* path) {
32
+ return (void*)LoadLibraryA(path);
33
+ }
34
+ static void* ffi_sym(void* h, const char* name) {
35
+ return (void*)GetProcAddress((HMODULE)h, name);
36
+ }
37
+ static void ffi_close(void* h) { FreeLibrary((HMODULE)h); }
38
+ static long long ffi_call8(void* f, long long* a) {
39
+ return ((ffi_fn8)f)(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
40
+ }
41
+ // 浮点调用: a[] 存的是 IEEE754 位模式 (int64 槽), memcpy 到 double 数组
42
+ // 保持位模式不变, 再按 double 类型传参 —— Windows x64 ABI 下前 4 个浮点
43
+ // 参数走 XMM0-XMM3 (整型声明会走 RCX/RDX/..., 目标函数从 XMM 读到垃圾)。
44
+ static double ffi_callf8(void* f, long long* a) {
45
+ double d[8];
46
+ memcpy(d, a, sizeof d);
47
+ return ((ffi_fnf8)f)(d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]);
48
+ }
49
+ */
50
+ import "C"
51
+
52
+ func (vm *vmState) ffiOpen(path string) uint64 {
53
+ if path == "" {
54
+ return 0
55
+ }
56
+ cpath := C.CString(path)
57
+ defer C.free(unsafe.Pointer(cpath))
58
+ h := C.ffi_open(cpath)
59
+ if h == nil {
60
+ return 0
61
+ }
62
+ return ffiLibPut(h)
63
+ }
64
+
65
+ func (vm *vmState) ffiSym(handle uint64, name string) uint64 {
66
+ if name == "" {
67
+ return 0
68
+ }
69
+ lib, ok := ffiLibGet(handle)
70
+ if !ok {
71
+ return 0
72
+ }
73
+ cname := C.CString(name)
74
+ defer C.free(unsafe.Pointer(cname))
75
+ fp := C.ffi_sym(lib, cname)
76
+ if fp == nil {
77
+ return 0
78
+ }
79
+ return ffiFnPut(fp)
80
+ }
81
+
82
+ // ffiCall 执行 ffi_call / ffi_callf。返回值: 整数调用为 int64;
83
+ // 浮点调用为 IEEE754 位模式。
84
+ func (vm *vmState) ffiCall(fnID, argbuf, nargs uint64, wantFloat bool) (uint64, string) {
85
+ if nargs > 8 {
86
+ return 0, fmt.Sprintf("FFI call: too many arguments (%d, max 8)", nargs)
87
+ }
88
+ fp, ok := ffiFnGet(fnID)
89
+ if !ok {
90
+ return 0, "FFI call: invalid function handle"
91
+ }
92
+ var args [8]C.longlong
93
+ if nargs > 0 {
94
+ if e := vm.checkAddr(argbuf, int(nargs)*8); e != "" {
95
+ return 0, "FFI call: " + e
96
+ }
97
+ for i := uint64(0); i < nargs; i++ {
98
+ args[i] = C.longlong(int64(binary.LittleEndian.Uint64(
99
+ vm.mem[argbuf+i*8 : argbuf+i*8+8])))
100
+ }
101
+ }
102
+ if wantFloat {
103
+ r := C.ffi_callf8(fp, &args[0])
104
+ return fToBits(float64(r)), ""
105
+ }
106
+ r := C.ffi_call8(fp, &args[0])
107
+ return uint64(int64(r)) & mask64, ""
108
+ }
109
+
110
+ func (vm *vmState) ffiClose(handle uint64) uint64 {
111
+ lib, ok := ffiLibDel(handle)
112
+ if !ok {
113
+ return mask64
114
+ }
115
+ C.ffi_close(lib)
116
+ return 0
117
+ }
@@ -0,0 +1,209 @@
1
+ package engine
2
+
3
+ import (
4
+ "image"
5
+ "sync"
6
+ )
7
+
8
+ var (
9
+ guiMu sync.Mutex
10
+ guiOpen bool
11
+ guiClosedF bool // 用户已请求关闭 (点 X / Alt+F4 / WM_DELETE_WINDOW)
12
+ guiW, guiH int
13
+ guiMouseX int
14
+ guiMouseY int
15
+ guiMouseBtn int // bit0 左 / bit1 右 / bit2 中 (当前按住)
16
+ guiBuffer *image.RGBA
17
+ guiForcedOff bool // 测试钩子: 禁止真实窗口 (不触碰显示服务)
18
+ )
19
+
20
+ const guiMaxDim = 4096 // 单边尺寸上限 (防一个 gui_new 吃掉全部内存)
21
+
22
+ // guiNew SYS 119: gui_new(w, h, title) -> 0 / -1。
23
+ func (vm *vmState) guiNew(w, h uint64, title string) uint64 {
24
+ if guiForcedOff {
25
+ return mask64
26
+ }
27
+ cw, ch := clampCoord(w), clampCoord(h)
28
+ if cw < 1 {
29
+ cw = 1
30
+ }
31
+ if ch < 1 {
32
+ ch = 1
33
+ }
34
+ if cw > guiMaxDim {
35
+ cw = guiMaxDim
36
+ }
37
+ if ch > guiMaxDim {
38
+ ch = guiMaxDim
39
+ }
40
+ buf := image.NewRGBA(image.Rect(0, 0, cw, ch))
41
+ for i := range buf.Pix { // 白色背景 (与 canvas() 一致)
42
+ buf.Pix[i] = 255
43
+ }
44
+ if !guiPlatformOpen(cw, ch, title, buf) {
45
+ return mask64
46
+ }
47
+ guiMu.Lock()
48
+ guiOpen = true
49
+ guiClosedF = false
50
+ guiW, guiH = cw, ch
51
+ guiMouseX, guiMouseY, guiMouseBtn = 0, 0, 0
52
+ guiBuffer = buf
53
+ guiMu.Unlock()
54
+ curCanvas = buf // 画布内建直接画进窗口后备缓冲
55
+ return 0
56
+ }
57
+
58
+ // guiUpdate SYS 120: gui_update() -> 0 (处理事件 + 呈现) / -1 (无窗口)。
59
+ func (vm *vmState) guiUpdate() uint64 {
60
+ guiMu.Lock()
61
+ open, buf := guiOpen, guiBuffer
62
+ guiMu.Unlock()
63
+ if !open || buf == nil {
64
+ return mask64
65
+ }
66
+ if !guiPlatformUpdate(buf) {
67
+ return mask64
68
+ }
69
+ return 0
70
+ }
71
+
72
+ // guiClose SYS 121: gui_close() -> 0 (销毁窗口; 画布保留可 save_png)。
73
+ func (vm *vmState) guiClose() uint64 {
74
+ guiMu.Lock()
75
+ open := guiOpen
76
+ guiMu.Unlock()
77
+ if open {
78
+ guiPlatformClose()
79
+ }
80
+ guiMu.Lock()
81
+ guiOpen = false
82
+ guiMu.Unlock()
83
+ return 0
84
+ }
85
+
86
+ // guiClosedQ SYS 122: gui_closed() -> 1 用户请求关闭 / 0。
87
+ func (vm *vmState) guiClosedQ() uint64 {
88
+ guiMu.Lock()
89
+ defer guiMu.Unlock()
90
+ if guiOpen && guiClosedF {
91
+ return 1
92
+ }
93
+ return 0
94
+ }
95
+
96
+ // guiActiveQ SYS 128: gui_active() -> 1 窗口已打开 / 0。
97
+ func (vm *vmState) guiActiveQ() uint64 {
98
+ guiMu.Lock()
99
+ defer guiMu.Unlock()
100
+ if guiOpen {
101
+ return 1
102
+ }
103
+ return 0
104
+ }
105
+
106
+ func (vm *vmState) mouseX() uint64 {
107
+ guiMu.Lock()
108
+ defer guiMu.Unlock()
109
+ if !guiOpen {
110
+ return mask64
111
+ }
112
+ return uint64(guiMouseX) & mask31
113
+ }
114
+
115
+ func (vm *vmState) mouseY() uint64 {
116
+ guiMu.Lock()
117
+ defer guiMu.Unlock()
118
+ if !guiOpen {
119
+ return mask64
120
+ }
121
+ return uint64(guiMouseY) & mask31
122
+ }
123
+
124
+ func (vm *vmState) mouseBtn() uint64 {
125
+ guiMu.Lock()
126
+ defer guiMu.Unlock()
127
+ if !guiOpen {
128
+ return mask64
129
+ }
130
+ return uint64(guiMouseBtn)
131
+ }
132
+
133
+ func guiMarkClosed() {
134
+ guiMu.Lock()
135
+ guiClosedF = true
136
+ guiMu.Unlock()
137
+ }
138
+
139
+ // guiWindowOpen 是否已打开 GUI 窗口 (行输入据此决定是否走键码队列)。
140
+ func guiWindowOpen() bool {
141
+ guiMu.Lock()
142
+ defer guiMu.Unlock()
143
+ return guiOpen
144
+ }
145
+
146
+ // guiUserClosed 用户已请求关闭且窗口仍标记为打开: 行输入应立即返回 EOF,
147
+ // 避免窗口关闭后 input/input_str 永久阻塞在键码轮询上。
148
+ func guiUserClosed() bool {
149
+ guiMu.Lock()
150
+ defer guiMu.Unlock()
151
+ return guiOpen && guiClosedF
152
+ }
153
+
154
+ // vkbShow SYS 158: 弹出 Windows 屏幕/触摸键盘 -> 0 成功 / -1 不可用。
155
+ func (vm *vmState) vkbShow() uint64 {
156
+ if guiVkbShow() {
157
+ return 0
158
+ }
159
+ return mask64
160
+ }
161
+
162
+ // vkbHide SYS 159: 收起 Windows 屏幕/触摸键盘 -> 0 成功 / -1 不可用。
163
+ func (vm *vmState) vkbHide() uint64 {
164
+ if guiVkbHide() {
165
+ return 0
166
+ }
167
+ return mask64
168
+ }
169
+
170
+ func guiInjectKey(code uint64) {
171
+ keyMu.Lock()
172
+ keyQueue = append(keyQueue, code)
173
+ keyMu.Unlock()
174
+ }
175
+
176
+ func guiSetMouse(x, y int, btnMask int) {
177
+ guiMu.Lock()
178
+ guiMouseX, guiMouseY = x, y
179
+ if btnMask >= 0 {
180
+ guiMouseBtn = btnMask
181
+ }
182
+ guiMu.Unlock()
183
+ }
184
+
185
+ func guiMouseButtonBit(bit int, down bool) int {
186
+ guiMu.Lock()
187
+ defer guiMu.Unlock()
188
+ if down {
189
+ guiMouseBtn |= bit
190
+ } else {
191
+ guiMouseBtn &^= bit
192
+ }
193
+ return guiMouseBtn
194
+ }
195
+
196
+ // guiSnapshot 复制后备缓冲像素快照 (呈现线程读, VM 线程写, 防撕裂)。
197
+ func guiSnapshot(buf *image.RGBA) []byte {
198
+ guiMu.Lock()
199
+ defer guiMu.Unlock()
200
+ if !guiOpen || buf != guiBuffer || buf == nil {
201
+ return nil
202
+ }
203
+ pix := make([]byte, len(buf.Pix))
204
+ copy(pix, buf.Pix)
205
+ return pix
206
+ }
207
+
208
+ // guiMask31 限制负坐标按补码回绕后仍为非负 (mouse_x 返回值)。
209
+ const mask31 = uint64(1<<31) - 1
@@ -0,0 +1,13 @@
1
+ //go:build !windows && !linux
2
+
3
+ package engine
4
+
5
+ import "image"
6
+
7
+ func guiPlatformOpen(w, h int, title string, buf *image.RGBA) bool { return false }
8
+
9
+ func guiPlatformUpdate(buf *image.RGBA) bool { return false }
10
+
11
+ func guiPlatformClose() {}
12
+
13
+ // 虚拟键盘实现见 vkb_stub.go (其余平台优雅失败)。
@@ -0,0 +1,148 @@
1
+ package engine
2
+
3
+ import (
4
+ "image"
5
+ "testing"
6
+ )
7
+
8
+ // resetGuiState 恢复 GUI 全局状态到"从未打开" (隔离用例间影响)。
9
+ func resetGuiState() {
10
+ guiMu.Lock()
11
+ guiOpen = false
12
+ guiClosedF = false
13
+ guiW, guiH = 0, 0
14
+ guiMouseX, guiMouseY, guiMouseBtn = 0, 0, 0
15
+ guiBuffer = nil
16
+ guiForcedOff = true // 默认禁真实窗口, 单测不触碰显示服务
17
+ guiMu.Unlock()
18
+ }
19
+
20
+ func TestGuiNewForcedOffGraceful(t *testing.T) {
21
+ resetGuiState()
22
+ defer resetGuiState()
23
+ vm := newHostVM(4096)
24
+ // 非窗口环境 (管道 / 不支持平台 / 测试钩子): gui_new 返回 -1, 不报错
25
+ if got := vm.guiNew(320, 240, "t"); got != mask64 {
26
+ t.Fatalf("guiForcedOff 下 gui_new 应返回 -1, 实际 %d", got)
27
+ }
28
+ if got := vm.guiActiveQ(); got != 0 {
29
+ t.Fatalf("gui_active 应为 0, 实际 %d", got)
30
+ }
31
+ // 后续操作全部优雅失败
32
+ if got := vm.guiUpdate(); got != mask64 {
33
+ t.Fatalf("无窗口 gui_update 应返回 -1, 实际 %d", got)
34
+ }
35
+ if got := vm.guiClose(); got != 0 {
36
+ t.Fatalf("gui_close 恒返回 0, 实际 %d", got)
37
+ }
38
+ if got := vm.guiClosedQ(); got != 0 {
39
+ t.Fatalf("gui_closed 应为 0, 实际 %d", got)
40
+ }
41
+ if got := vm.mouseX(); got != mask64 {
42
+ t.Fatalf("无窗口 mouse_x 应返回 -1, 实际 %d", got)
43
+ }
44
+ if got := vm.mouseY(); got != mask64 {
45
+ t.Fatalf("无窗口 mouse_y 应返回 -1, 实际 %d", got)
46
+ }
47
+ if got := vm.mouseBtn(); got != mask64 {
48
+ t.Fatalf("无窗口 mouse_button 应返回 -1, 实际 %d", got)
49
+ }
50
+ }
51
+
52
+ // openFakeWindow 直接置全局状态模拟"窗口已打开" (不依赖平台后端)。
53
+ func openFakeWindow(w, h int) *image.RGBA {
54
+ buf := image.NewRGBA(image.Rect(0, 0, w, h))
55
+ guiMu.Lock()
56
+ guiOpen = true
57
+ guiClosedF = false
58
+ guiW, guiH = w, h
59
+ guiMouseX, guiMouseY, guiMouseBtn = 0, 0, 0
60
+ guiBuffer = buf
61
+ guiMu.Unlock()
62
+ return buf
63
+ }
64
+
65
+ func TestGuiMouseAndStateQueries(t *testing.T) {
66
+ resetGuiState()
67
+ defer resetGuiState()
68
+ vm := newHostVM(4096)
69
+ buf := openFakeWindow(80, 60)
70
+
71
+ if got := vm.guiActiveQ(); got != 1 {
72
+ t.Fatalf("gui_active 应为 1, 实际 %d", got)
73
+ }
74
+ if got := vm.guiClosedQ(); got != 0 {
75
+ t.Fatalf("未请求关闭时 gui_closed 应为 0, 实际 %d", got)
76
+ }
77
+ guiSetMouse(10, 20, -1)
78
+ guiMouseButtonBit(1, true) // 左键按下
79
+ if got := vm.mouseX(); got != 10 {
80
+ t.Fatalf("mouse_x 应为 10, 实际 %d", got)
81
+ }
82
+ if got := vm.mouseY(); got != 20 {
83
+ t.Fatalf("mouse_y 应为 20, 实际 %d", got)
84
+ }
85
+ if got := vm.mouseBtn(); got != 1 {
86
+ t.Fatalf("mouse_button 应为 1 (左键), 实际 %d", got)
87
+ }
88
+ guiMouseButtonBit(4, true) // 再按中键
89
+ if got := vm.mouseBtn(); got != 5 {
90
+ t.Fatalf("mouse_button 应为 5 (左|中), 实际 %d", got)
91
+ }
92
+ guiMouseButtonBit(1, false) // 左键抬起
93
+ if got := vm.mouseBtn(); got != 4 {
94
+ t.Fatalf("mouse_button 应为 4 (中键), 实际 %d", got)
95
+ }
96
+ // 用户请求关闭 (点 X / Alt+F4)
97
+ guiMarkClosed()
98
+ if got := vm.guiClosedQ(); got != 1 {
99
+ t.Fatalf("guiMarkClosed 后 gui_closed 应为 1, 实际 %d", got)
100
+ }
101
+ _ = buf
102
+ }
103
+
104
+ func TestGuiSnapshotCopiesPixels(t *testing.T) {
105
+ resetGuiState()
106
+ defer resetGuiState()
107
+ buf := openFakeWindow(2, 1)
108
+ buf.Pix[0], buf.Pix[1], buf.Pix[2], buf.Pix[3] = 1, 2, 3, 4
109
+
110
+ snap := guiSnapshot(buf)
111
+ if snap == nil {
112
+ t.Fatal("窗口打开时 guiSnapshot 应返回像素副本")
113
+ }
114
+ if len(snap) != len(buf.Pix) {
115
+ t.Fatalf("快照长度应与缓冲一致, 实际 %d vs %d", len(snap), len(buf.Pix))
116
+ }
117
+ if snap[0] != 1 || snap[1] != 2 || snap[2] != 3 || snap[3] != 4 {
118
+ t.Fatalf("快照内容不符: %v", snap[:4])
119
+ }
120
+ // 改原缓冲不影响已有快照 (防撕裂)
121
+ buf.Pix[0] = 255
122
+ if snap[0] != 1 {
123
+ t.Fatal("快照应是独立副本, 不随后备缓冲变化")
124
+ }
125
+ // 未匹配的缓冲 / 关闭状态返回 nil
126
+ if guiSnapshot(image.NewRGBA(image.Rect(0, 0, 1, 1))) != nil {
127
+ t.Fatal("非当前窗口缓冲的快照应返回 nil")
128
+ }
129
+ guiMu.Lock()
130
+ guiOpen = false
131
+ guiMu.Unlock()
132
+ if guiSnapshot(buf) != nil {
133
+ t.Fatal("窗口关闭后快照应返回 nil")
134
+ }
135
+ }
136
+
137
+ func TestGuiInjectKeySharesKeyboardQueue(t *testing.T) {
138
+ resetGuiState()
139
+ defer resetKeyState()
140
+ // GUI 窗口按键注入与终端共用同一键码队列 (get_key 可读)
141
+ guiInjectKey('A')
142
+ guiInjectKey(1003) // keyLeft
143
+ keyMu.Lock()
144
+ defer keyMu.Unlock()
145
+ if len(keyQueue) != 2 || keyQueue[0] != 'A' || keyQueue[1] != 1003 {
146
+ t.Fatalf("guiInjectKey 应进入键盘队列, 实际 %v", keyQueue)
147
+ }
148
+ }