Skip to content

Commit 000ef2b

Browse files
author
KDr2
committed
refactor new instruction, add test cases
1 parent d14936b commit 000ef2b

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

src/tapedfunction.jl

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ mutable struct Instruction{F} <: AbstractInstruction
1818
tape::Tape
1919
end
2020

21-
22-
"""
23-
NewInstruction
24-
25-
A `NewInstruction` stands for a `new` operator, which only appears in
26-
an inner constructor. Its represtation in IRCode is not a function call,
27-
so we need a new intruction type to represent it on tapes.
28-
"""
29-
mutable struct NewInstruction <: AbstractInstruction
30-
input::Tuple
31-
output
32-
tape::Tape
33-
end
34-
3521
Tape() = Tape(Vector{AbstractInstruction}(), 1, nothing)
3622
Tape(owner) = Tape(Vector{AbstractInstruction}(), 1, owner)
3723
MacroTools.@forward Tape.tape Base.iterate, Base.length
@@ -92,7 +78,7 @@ function Base.show(io::IO, tp::Tape)
9278
end
9379

9480
function (instr::Instruction{F})() where F
95-
# Catch run-time exceptions / errors.
81+
# catch run-time exceptions / errors.
9682
try
9783
output = instr.fun(map(val, instr.input)...)
9884
instr.output.val = output
@@ -102,9 +88,9 @@ function (instr::Instruction{F})() where F
10288
end
10389
end
10490

105-
106-
function (instr::NewInstruction)()
107-
# Catch run-time exceptions / errors.
91+
function _new end
92+
function (instr::Instruction{typeof(_new)})()
93+
# catch run-time exceptions / errors.
10894
try
10995
expr = Expr(:new, map(val, instr.input)...)
11096
output = eval(expr)
@@ -147,15 +133,15 @@ function run_and_record!(tape::Tape, f, args...)
147133
return output
148134
end
149135

150-
function run_and_record_new!(tape::Tape, args...)
136+
function run_and_record!(tape::Tape, ::typeof(_new), args...)
151137
output = try
152138
expr = Expr(:new, map(val, args)...)
153139
box(eval(expr))
154140
catch e
155141
@warn e
156142
Box{Any}(nothing)
157143
end
158-
ins = NewInstruction(args, output, tape)
144+
ins = Instruction(_new, args, output, tape)
159145
push!(tape, ins)
160146
return output
161147
end
@@ -233,7 +219,7 @@ function intercept(ir; recorder=:run_and_record!)
233219
ir[x] = IRTools.xcall(@__MODULE__, recorder, tape, new_args...)
234220
elseif Meta.isexpr(st.expr, :new)
235221
args = st.expr.args
236-
ir[x] = IRTools.xcall(@__MODULE__, :run_and_record_new!, tape, args...)
222+
ir[x] = IRTools.xcall(@__MODULE__, recorder, tape, _new, args...)
237223
else
238224
@warn "Unknown IR code: " st
239225
end

src/tapedtask.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,6 @@ function Base.copy(x::Instruction, on_tape::Tape, roster::Dict{UInt64, Any})
212212
Instruction(x.fun, input, output, on_tape)
213213
end
214214

215-
function Base.copy(x::NewInstruction, on_tape::Tape, roster::Dict{UInt64, Any})
216-
input = map(x.input) do ob
217-
copy_box(ob, roster)
218-
end
219-
output = copy_box(x.output, roster)
220-
NewInstruction(input, output, on_tape)
221-
end
222-
223215
function Base.copy(t::Tape, roster::Dict{UInt64, Any}; partial=true)
224216
old_data = t.tape
225217
len = partial ? length(old_data) - t.counter + 1 : length(old_data)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Libtask
22
using Test
33

4+
include("tf.jl")
45
include("ctask.jl")
56
include("tarray.jl")
67
include("tref.jl")

test/tf.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Libtask
2+
3+
@testset "tapedfunction" begin
4+
# Test case 1: stack allocated objects are deep copied.
5+
@testset "Instruction{typeof(_new)}" begin
6+
mutable struct S
7+
i::Int
8+
S(x, y) = new(x + y)
9+
end
10+
11+
tf = Libtask.TapedFunction(S)
12+
s1 = tf(1, 2)
13+
@test s1.i == 3
14+
newins = findall(x -> isa(x, Libtask.Instruction{typeof(Libtask._new)}), tf.tape.tape)
15+
@test length(newins) == 1
16+
end
17+
end

0 commit comments

Comments
 (0)