补充
This commit is contained in:
290
Test/DecoratorTest/App/main.py
Normal file
290
Test/DecoratorTest/App/main.py
Normal file
@@ -0,0 +1,290 @@
|
||||
import t
|
||||
from stdio import printf
|
||||
|
||||
# ============================================================
|
||||
# 自定义装饰器处理函数
|
||||
# 调用约定 (v4):
|
||||
# i32 decor_name(i8* ctx, i8* func_name, i32 phase,
|
||||
# i8* args_ptr, i8* ret_ptr, i8* decor_args_ptr)
|
||||
#
|
||||
# ctx: 栈帧局部上下文(32字节),每次调用独立,线程/递归安全
|
||||
# args_ptr: 可读写,装饰器可修改函数入参
|
||||
# ret_ptr: 后置阶段可读写,装饰器可修改返回值
|
||||
# 返回值(前置阶段):
|
||||
# 0 = 跳过原函数调用
|
||||
# 1 = 正常调用一次
|
||||
# N = 循环调用 N 次
|
||||
# ============================================================
|
||||
|
||||
# @log 装饰器:打印函数进入/退出,返回 1(正常调用一次)
|
||||
def log(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt,
|
||||
args_ptr: t.CVoid | t.CPtr,
|
||||
ret_ptr: t.CVoid | t.CPtr,
|
||||
decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport:
|
||||
if phase == 0:
|
||||
printf("[LOG] >> %s enter\n", func_name)
|
||||
return 1
|
||||
else:
|
||||
printf("[LOG] << %s exit\n", func_name)
|
||||
return 0
|
||||
|
||||
# @trace 装饰器:详细追踪
|
||||
def trace(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt,
|
||||
args_ptr: t.CVoid | t.CPtr,
|
||||
ret_ptr: t.CVoid | t.CPtr,
|
||||
decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport:
|
||||
if phase == 0:
|
||||
printf("[TRACE] calling %s...\n", func_name)
|
||||
return 1
|
||||
else:
|
||||
printf("[TRACE] %s returned\n", func_name)
|
||||
return 0
|
||||
|
||||
# @timing 装饰器:使用全局变量记录计时
|
||||
# 注意:理想情况下应使用 ctx 栈帧局部上下文替代全局变量,
|
||||
# 但 Viper 当前前端尚不支持 i8* 到 i32* 的指针转型,
|
||||
# 因此暂时使用全局变量演示计时逻辑
|
||||
_timing_tmp: t.CInt = 0
|
||||
|
||||
def timing(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt,
|
||||
args_ptr: t.CVoid | t.CPtr,
|
||||
ret_ptr: t.CVoid | t.CPtr,
|
||||
decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport:
|
||||
global _timing_tmp
|
||||
if phase == 0:
|
||||
_timing_tmp = 100
|
||||
printf("[TIMING] %s start at t=%d\n", func_name, _timing_tmp)
|
||||
return 1
|
||||
else:
|
||||
elapsed: t.CInt = 105 - _timing_tmp
|
||||
printf("[TIMING] %s took %d ms\n", func_name, elapsed)
|
||||
return 0
|
||||
|
||||
# @repeat 装饰器(带参数,流程劫持):循环调用原函数 N 次
|
||||
def repeat(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt,
|
||||
args_ptr: t.CVoid | t.CPtr,
|
||||
ret_ptr: t.CVoid | t.CPtr,
|
||||
decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport:
|
||||
if phase == 0:
|
||||
printf("[REPEAT] %s will run 3 times\n", func_name)
|
||||
return 3
|
||||
else:
|
||||
printf("[REPEAT] %s all iterations done\n", func_name)
|
||||
return 0
|
||||
|
||||
# @skip 装饰器:跳过原函数调用(流程劫持,返回 0)
|
||||
_skip_count: t.CInt = 0
|
||||
|
||||
def skip(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt,
|
||||
args_ptr: t.CVoid | t.CPtr,
|
||||
ret_ptr: t.CVoid | t.CPtr,
|
||||
decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport:
|
||||
global _skip_count
|
||||
if phase == 0:
|
||||
_skip_count = _skip_count + 1
|
||||
printf("[SKIP] %s skipped! (call #%d)\n", func_name, _skip_count)
|
||||
return 0
|
||||
else:
|
||||
printf("[SKIP] %s post (ret_ptr=%p)\n", func_name, ret_ptr)
|
||||
return 0
|
||||
|
||||
# @count_calls 装饰器:统计函数被调用次数(递归保护测试用)
|
||||
_call_count: t.CInt = 0
|
||||
|
||||
def count_calls(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt,
|
||||
args_ptr: t.CVoid | t.CPtr,
|
||||
ret_ptr: t.CVoid | t.CPtr,
|
||||
decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport:
|
||||
global _call_count
|
||||
if phase == 0:
|
||||
_call_count = _call_count + 1
|
||||
printf("[COUNT] %s call #%d\n", func_name, _call_count)
|
||||
return 1
|
||||
else:
|
||||
printf("[COUNT] %s done (total calls: %d)\n", func_name, _call_count)
|
||||
return 0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 1:单个装饰器 @log
|
||||
# ============================================================
|
||||
|
||||
@log
|
||||
def add(a: t.CInt, b: t.CInt) -> t.CInt | t.CExport:
|
||||
return a + b
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 2:链式装饰器 @log @trace => log(trace(f))
|
||||
# 执行顺序:log_pre → trace_pre → f → trace_post → log_post
|
||||
# ============================================================
|
||||
|
||||
@log
|
||||
@trace
|
||||
def multiply(a: t.CInt, b: t.CInt) -> t.CInt | t.CExport:
|
||||
return a * b
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 3:装饰器修饰 void 返回函数
|
||||
# ============================================================
|
||||
|
||||
@log
|
||||
def greet(name: str) -> t.CVoid | t.CExport:
|
||||
printf("Hello, %s!\n", name)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 4:装饰器修饰无参数函数
|
||||
# ============================================================
|
||||
|
||||
@log
|
||||
def get_value() -> t.CInt | t.CExport:
|
||||
return 42
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 5:@timing 装饰器
|
||||
# ============================================================
|
||||
|
||||
@timing
|
||||
def compute(x: t.CInt) -> t.CInt | t.CExport:
|
||||
return x * x + 1
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 6:带参数装饰器 @repeat(3) — 流程劫持,循环调用 3 次
|
||||
# ============================================================
|
||||
|
||||
@repeat(3)
|
||||
def echo(msg: str) -> t.CVoid | t.CExport:
|
||||
printf(" echo: %s\n", msg)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 7:链式 @timing @log
|
||||
# ============================================================
|
||||
|
||||
@timing
|
||||
@log
|
||||
def power(base: t.CInt, exp: t.CInt) -> t.CInt | t.CExport:
|
||||
result: t.CInt = 1
|
||||
i: t.CInt = 0
|
||||
while i < exp:
|
||||
result = result * base
|
||||
i = i + 1
|
||||
return result
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 8:@skip 装饰器 — 流程劫持,跳过原函数调用
|
||||
# ============================================================
|
||||
|
||||
@skip
|
||||
def dangerous() -> t.CInt | t.CExport:
|
||||
printf("This should NOT be printed!\n")
|
||||
return 999
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 9:@repeat(3) 修饰有返回值的函数
|
||||
# ============================================================
|
||||
|
||||
@repeat(3)
|
||||
def accumulate(x: t.CInt) -> t.CInt | t.CExport:
|
||||
printf(" accumulate(%d)\n", x)
|
||||
return x * 10
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 10:递归装饰保护 — @count_calls 修饰递归函数
|
||||
# v4 递归保护:最外层 wrapper 检查全局标志位,
|
||||
# 递归调用时跳过所有装饰逻辑,仅最外层调用触发装饰器
|
||||
# ============================================================
|
||||
|
||||
@count_calls
|
||||
def factorial(n: t.CInt) -> t.CInt | t.CExport:
|
||||
if n <= 1:
|
||||
return 1
|
||||
return n * factorial(n - 1)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 11:@log 修饰递归函数 — 验证递归保护
|
||||
# 递归保护确保只有最外层调用打印 LOG,内部递归不触发
|
||||
# ============================================================
|
||||
|
||||
@log
|
||||
def fib(n: t.CInt) -> t.CInt | t.CExport:
|
||||
if n <= 1:
|
||||
return n
|
||||
return fib(n - 1) + fib(n - 2)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 主函数
|
||||
# ============================================================
|
||||
|
||||
def main() -> t.CInt | t.CExport:
|
||||
printf("=== Decorator Test v4 ===\n\n")
|
||||
|
||||
# 测试 1:单个 @log
|
||||
printf("--- Test 1: single @log ---\n")
|
||||
r1: t.CInt = add(3, 4)
|
||||
printf("add(3,4) = %d\n\n", r1)
|
||||
|
||||
# 测试 2:链式 @log @trace
|
||||
printf("--- Test 2: chained @log @trace ---\n")
|
||||
r2: t.CInt = multiply(5, 6)
|
||||
printf("multiply(5,6) = %d\n\n", r2)
|
||||
|
||||
# 测试 3:void 返回
|
||||
printf("--- Test 3: @log on void function ---\n")
|
||||
greet("Viper")
|
||||
printf("\n")
|
||||
|
||||
# 测试 4:无参数
|
||||
printf("--- Test 4: @log on no-arg function ---\n")
|
||||
r4: t.CInt = get_value()
|
||||
printf("get_value() = %d\n\n", r4)
|
||||
|
||||
# 测试 5:@timing
|
||||
printf("--- Test 5: @timing ---\n")
|
||||
r5: t.CInt = compute(7)
|
||||
printf("compute(7) = %d\n\n", r5)
|
||||
|
||||
# 测试 6:@repeat(3) — 流程劫持,循环 3 次
|
||||
printf("--- Test 6: @repeat(3) flow hijack ---\n")
|
||||
echo("hello")
|
||||
printf("\n")
|
||||
|
||||
# 测试 7:链式 @timing @log
|
||||
printf("--- Test 7: chained @timing @log ---\n")
|
||||
r7: t.CInt = power(2, 10)
|
||||
printf("power(2,10) = %d\n\n", r7)
|
||||
|
||||
# 测试 8:@skip — 跳过原函数调用
|
||||
printf("--- Test 8: @skip flow hijack ---\n")
|
||||
r8: t.CInt = dangerous()
|
||||
printf("dangerous() = %d (should be 0, skipped)\n\n", r8)
|
||||
|
||||
# 测试 9:@repeat(3) 有返回值
|
||||
printf("--- Test 9: @repeat(3) with return value ---\n")
|
||||
r9: t.CInt = accumulate(5)
|
||||
printf("accumulate(5) = %d (last iteration result)\n\n", r9)
|
||||
|
||||
# 测试 10:递归装饰保护 — @count_calls
|
||||
printf("--- Test 10: recursive decoration protection ---\n")
|
||||
_call_count = 0
|
||||
r10: t.CInt = factorial(5)
|
||||
printf("factorial(5) = %d (should be 120)\n", r10)
|
||||
printf(" decorator call count = %d (should be 1, not 5)\n\n", _call_count)
|
||||
|
||||
# 测试 11:@log 递归保护 — fib
|
||||
printf("--- Test 11: @log on recursive fib ---\n")
|
||||
r11: t.CInt = fib(6)
|
||||
printf("fib(6) = %d (should be 8)\n", r11)
|
||||
printf(" (should see only 1 LOG enter/exit pair)\n\n")
|
||||
|
||||
printf("=== All decorator tests done ===\n")
|
||||
return 0
|
||||
1
Test/DecoratorTest/output/577f9e1aa32b17f8.deps.json
Normal file
1
Test/DecoratorTest/output/577f9e1aa32b17f8.deps.json
Normal file
@@ -0,0 +1 @@
|
||||
{"stdio": "73edbcf76e32d00b"}
|
||||
28
Test/DecoratorTest/project.json
Normal file
28
Test/DecoratorTest/project.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "DecoratorTest",
|
||||
"version": "1.0.0",
|
||||
"source_dir": "./App",
|
||||
"temp_dir": "./temp",
|
||||
"output_dir": "./output",
|
||||
"compiler": {
|
||||
"cmd": "llc",
|
||||
"flags": ["-filetype=obj", "-relocation-model=pic"]
|
||||
},
|
||||
"linker": {
|
||||
"cmd": "clang++",
|
||||
"flags": ["-Wl,--allow-multiple-definition", "-Wl,--unresolved-symbols=ignore-in-object-files", "-lmsvcrt", "-lucrt", "-lpthread", "-lmingwex", "-lkernel32", "-luser32", "-latomic"],
|
||||
"output": "DecoratorTest.exe"
|
||||
},
|
||||
"includes": [
|
||||
"../../includes"
|
||||
],
|
||||
"target": {
|
||||
"triple": "x86_64-pc-windows-gnu",
|
||||
"datalayout": "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
|
||||
},
|
||||
"options": {
|
||||
"slice_level": 3,
|
||||
"target": "llvm",
|
||||
"strict_mode": true
|
||||
}
|
||||
}
|
||||
68
Test/DecoratorTest/temp/577f9e1aa32b17f8.pyi
Normal file
68
Test/DecoratorTest/temp/577f9e1aa32b17f8.pyi
Normal file
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
Auto-generated Python stub file from main.py
|
||||
Module: main
|
||||
"""
|
||||
|
||||
import c
|
||||
|
||||
|
||||
import t
|
||||
from stdio import printf
|
||||
|
||||
def log(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt, args_ptr: t.CVoid | t.CPtr, ret_ptr: t.CVoid | t.CPtr, decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport: pass
|
||||
|
||||
def trace(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt, args_ptr: t.CVoid | t.CPtr, ret_ptr: t.CVoid | t.CPtr, decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport: pass
|
||||
|
||||
|
||||
_timing_tmp: t.CExtern | t.CInt
|
||||
|
||||
def timing(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt, args_ptr: t.CVoid | t.CPtr, ret_ptr: t.CVoid | t.CPtr, decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport: pass
|
||||
|
||||
def repeat(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt, args_ptr: t.CVoid | t.CPtr, ret_ptr: t.CVoid | t.CPtr, decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport: pass
|
||||
|
||||
|
||||
_skip_count: t.CExtern | t.CInt
|
||||
|
||||
def skip(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt, args_ptr: t.CVoid | t.CPtr, ret_ptr: t.CVoid | t.CPtr, decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport: pass
|
||||
|
||||
|
||||
_call_count: t.CExtern | t.CInt
|
||||
|
||||
def count_calls(ctx: t.CVoid | t.CPtr, func_name: str, phase: t.CInt, args_ptr: t.CVoid | t.CPtr, ret_ptr: t.CVoid | t.CPtr, decor_args_ptr: t.CVoid | t.CPtr) -> t.CInt | t.CExport: pass
|
||||
|
||||
@log
|
||||
def add(a: t.CInt, b: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
@log
|
||||
@trace
|
||||
def multiply(a: t.CInt, b: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
@log
|
||||
def greet(name: str) -> t.CVoid | t.CExport: pass
|
||||
|
||||
@log
|
||||
def get_value() -> t.CInt | t.CExport: pass
|
||||
|
||||
@timing
|
||||
def compute(x: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
@repeat(3)
|
||||
def echo(msg: str) -> t.CVoid | t.CExport: pass
|
||||
|
||||
@timing
|
||||
@log
|
||||
def power(base: t.CInt, exp: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
@skip
|
||||
def dangerous() -> t.CInt | t.CExport: pass
|
||||
|
||||
@repeat(3)
|
||||
def accumulate(x: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
@count_calls
|
||||
def factorial(n: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
@log
|
||||
def fib(n: t.CInt) -> t.CInt | t.CExport: pass
|
||||
|
||||
def main() -> t.CInt | t.CExport: pass
|
||||
28
Test/DecoratorTest/temp/73edbcf76e32d00b.pyi
Normal file
28
Test/DecoratorTest/temp/73edbcf76e32d00b.pyi
Normal file
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Auto-generated Python stub file from stdio.py
|
||||
Module: stdio
|
||||
"""
|
||||
|
||||
|
||||
import t, c
|
||||
|
||||
def printf(fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def fprintf(stream: t.CVoid | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def sprintf(buf: t.CChar | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def snprintf(buf: t.CChar | t.CPtr, size: t.CSizeT, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def puts(s: t.CChar | t.CConst | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def fputs(s: t.CChar | t.CConst | t.CPtr, stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
def fgets(buf: t.CChar | t.CPtr, size: t.CInt, stream: t.CVoid | t.CPtr) -> t.CChar | t.CPtr | t.CExtern | t.CExport: pass
|
||||
|
||||
def fflush(stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
|
||||
|
||||
|
||||
stdin: t.CExtern | t.CVoid | t.CPtr
|
||||
stdout: t.CExtern | t.CVoid | t.CPtr
|
||||
stderr: t.CExtern | t.CVoid | t.CPtr
|
||||
2
Test/DecoratorTest/temp/_sha1_map.txt
Normal file
2
Test/DecoratorTest/temp/_sha1_map.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
577f9e1aa32b17f8:main.py
|
||||
73edbcf76e32d00b:includes/stdio.py
|
||||
Reference in New Issue
Block a user