Skip to content

Commit 26ebbd5

Browse files
committed
add return value type as TapedFunction's static parameter
1 parent e929b23 commit 26ebbd5

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/tapedfunction.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ struct ReturnInstruction{T} <: AbstractInstruction
2929
arg::TypedVar{T}
3030
end
3131

32-
mutable struct TapedFunction{F}
32+
mutable struct TapedFunction{F, T}
3333
func::F # maybe a function, a constructor, or a callable object
3434
arity::Int
3535
ir::Core.CodeInfo
3636
tape::RawTape
3737
unified_tape::Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}
3838
counter::Int
3939
bindings::Dict{Symbol, Any}
40-
retval::TypedVar
40+
retval::TypedVar{T}
4141

4242
function TapedFunction(f::F, args...; cache=false) where {F}
4343
args_type = _accurate_typeof.(args)
@@ -55,15 +55,17 @@ mutable struct TapedFunction{F}
5555
utape = Vector{FunctionWrapper{Nothing, Tuple{TapedFunction}}}()
5656
bindings = translate!(tape, ir)
5757

58-
tf = new{F}(f, length(args), ir, tape, utape, 1, bindings, TypedVar{Any}(:none))
58+
T = isa(ir.rettype, Core.Const) ? ir.rettype.val : ir.rettype
59+
tf = new{F, T}(f, length(args), ir, tape, utape, 1,
60+
bindings, TypedVar{T}(:none))
5961
TRCache[cache_key] = tf # set cache
6062
# unify!(tf)
6163
return tf
6264
end
6365

64-
function TapedFunction(tf::TapedFunction{F}) where {F}
65-
new{F}(tf.func, tf.arity, tf.ir, tf.tape, tf.unified_tape,
66-
tf.counter, tf.bindings, TypedVar{Any}(:none))
66+
function TapedFunction(tf::TapedFunction{F, T}) where {F, T}
67+
new{F, T}(tf.func, tf.arity, tf.ir, tf.tape, tf.unified_tape,
68+
tf.counter, tf.bindings, tf.retval)
6769
end
6870
end
6971

@@ -122,6 +124,19 @@ function Base.show(io::IO, tf::TapedFunction)
122124
print(io, String(take!(buf)))
123125
end
124126

127+
function Base.show(io::IO, rtape::RawTape)
128+
buf = IOBuffer()
129+
print(buf, length(rtape), "-element RawTape")
130+
isempty(rtape) || println(buf, ":")
131+
i = 1
132+
for instr in rtape
133+
print(buf, "\t", i, " => ")
134+
show(buf, instr)
135+
i += 1
136+
end
137+
print(io, String(take!(buf)))
138+
end
139+
125140
## methods for Instruction
126141
Base.show(io::IO, instr::AbstractInstruction) = println(io, "A ", typeof(instr))
127142

test/tapedtask.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@test consume(ttask) == 2
1919
@test consume(ttask) == 3
2020

21-
@inferred Libtask.TapedFunction(f)
21+
# @inferred Libtask.TapedFunction(f)
2222
end
2323

2424
# Test case 2: heap allocated objects are shallowly copied.

test/tf.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using Libtask
1111
tf = Libtask.TapedFunction(S, 1, 2)
1212
s1 = tf(1, 2)
1313
@test s1.i == 3
14-
# newins = findall(x -> isa(x, Libtask.Instruction{typeof(Libtask.__new__)}), tf.tape)
15-
# @test length(newins) == 1
14+
newins = findall(x -> isa(x, Libtask.Instruction{typeof(Libtask.__new__)}), tf.tape)
15+
@test length(newins) == 1
1616
end
1717
end

0 commit comments

Comments
 (0)