Skip to content

Commit 543bec1

Browse files
committed
Merge pull request #206 from kennytm/dss-extras
Changes to Pickler/Serializer/Reflection for DSS
2 parents 9a52191 + dcbe5bb commit 543bec1

File tree

12 files changed

+431
-32
lines changed

12 files changed

+431
-32
lines changed

lib/compiler/NewAssembler.oz

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,23 +431,13 @@ define
431431
end
432432

433433
meth LookForXRegsInPattern(Pattern)
434-
proc {Loop Qs}
435-
case Qs
436-
of nil then
437-
skip
438-
[] _#_#patmatcapture(Idx)#Qr then
439-
if Idx >= @xRegCount then
440-
xRegCount := Idx+1
441-
end
442-
{Loop Qr}
443-
[] _#_#_#Qr then
444-
{Loop Qr}
445-
end
446-
end
447-
448-
Qs = {BootSerializer.serialize {BootSerializer.new} [Pattern#_]}
434+
Qs = {BootSerializer.extractByLabels Pattern r(patmatcapture:_)}
449435
in
450-
{Loop Qs}
436+
{ForAll Qs proc {$ _#patmatcapture(Idx)}
437+
if Idx >= @xRegCount then
438+
xRegCount := Idx + 1
439+
end
440+
end}
451441
end
452442

453443
meth output($)

lib/main/sys/Pickle.oz

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export
4444

4545
Pack
4646
PackWithCells
47+
PackWithReplacements
4748
Unpack
4849

4950
define
@@ -54,7 +55,9 @@ define
5455
%% Save and its variants
5556
%%
5657

57-
Save = BootPickle.save
58+
fun {Save URL}
59+
{BootPickle.save URL nil}
60+
end
5861

5962
proc {SaveCompressed Value FileName Level}
6063
{Save Value FileName}
@@ -89,7 +92,22 @@ define
8992
%% Pack and its variants
9093
%%
9194

92-
Pack = BootPickle.pack
95+
%%% {Pack Object} = BS
96+
%%%
97+
%%% Serialize an object into a ByteString. May throw an exception if the
98+
%%% object contains non-serializable components (e.g. cells, unbound variables)
99+
fun {Pack Value}
100+
{BootPickle.pack Value nil}
101+
end
102+
103+
%%% {PackWithReplacements Object [From1#To1 From2#To2 ...]} = BS
104+
%%%
105+
%%% Serialize an object into a ByteString with some components temporarily
106+
%%% replaced. If the object contains any of FromN, they will all be replaced
107+
%%% by ToN before the serialization starts. Deserializing the byte string
108+
%%% will only give back ToN, not FromN. The Object itself will stay the same
109+
%%% after calling PackWithReplacements.
110+
PackWithReplacements = BootPickle.pack
93111

94112
fun {PackWithCells Value}
95113
{Pack Value}

platform-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(BASE_FUNCTORS
1313
#"finalize.oz" "gc.oz"
1414
"state.oz" "thread.oz"
1515
"vm.oz"
16+
"reflection.oz" "serializer.oz"
1617
)
1718
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/base")
1819
foreach(FUNCTOR ${BASE_FUNCTORS})

platform-test/base/pickle.oz

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323

2424
functor
2525
import
26-
BootPickle(pack:Pack unpack:Unpack) at 'x-oz://boot/Pickle'
27-
%Pickle(pack:Pack unpack:Unpack)
26+
%BootPickle(pack:Pack unpack:Unpack) at 'x-oz://boot/Pickle'
27+
Pickle(pack:Pack unpack:Unpack packWithReplacements:PackWithReplacements)
2828
BootName(newUnique:NewUniqueName newNamed:NewNamedName) at 'x-oz://boot/Name'
29+
System
2930
export
3031
Return
3132
define
@@ -38,12 +39,15 @@ define
3839
end
3940
end
4041

42+
BadThread
43+
4144
Nogoods=
4245
[{NewCell 1}
4346
{NewPort _}
4447
_
4548
{New BaseObject noop}
46-
thread {Delay 2000} 42 end
49+
thread BadThread = {Thread.this} for _ in 1..200 do {Delay 10} end 42 end
50+
BadThread
4751
]
4852

4953
NN={NewName}
@@ -110,6 +114,31 @@ define
110114
end keys:[pickle])
111115
packBad(proc {$}
112116
{All Nogoods fun {$ X} {TryPack X}==[X] end} = true
117+
{Thread.terminate BadThread}
113118
end keys:[pickle])
119+
120+
packWithReplacements(
121+
proc {$}
122+
A = {NewCell 1}
123+
B = {NewCell 1}
124+
C = A
125+
S U
126+
Replacements = [A#a B#b]
127+
Obj = [fun {$} A#B#c end A B C d A]
128+
in
129+
S = {PackWithReplacements Obj Replacements}
130+
U = {Unpack S}
131+
[a b a d a] = U.2
132+
%a#b#c = {U.1}
133+
%% ^ TODO cannot test this on local machine, the unpickler
134+
%% will recognize the anonymous function as a global node
135+
%% and reuse the existing method in this VM.
136+
[A B C d A] = Obj.2
137+
A#B#c = {Obj.1}
138+
true = {IsCell A}
139+
true = {IsCell B}
140+
true = (A \= B)
141+
end
142+
)
114143
])
115144
end

platform-test/base/reflection.oz

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
%%% Copyright © 2014, Université catholique de Louvain
2+
%%% All rights reserved.
3+
%%%
4+
%%% Redistribution and use in source and binary forms, with or without
5+
%%% modification, are permitted provided that the following conditions are met:
6+
%%%
7+
%%% * Redistributions of source code must retain the above copyright notice,
8+
%%% this list of conditions and the following disclaimer.
9+
%%% * Redistributions in binary form must reproduce the above copyright notice,
10+
%%% this list of conditions and the following disclaimer in the documentation
11+
%%% and/or other materials provided with the distribution.
12+
%%%
13+
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
%%% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
%%% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
%%% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
%%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
%%% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
%%% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
%%% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
%%% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
%%% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23+
%%% POSSIBILITY OF SUCH DAMAGE.
24+
25+
functor
26+
27+
import
28+
Reflection at 'x-oz://boot/Reflection'
29+
BootName at 'x-oz://boot/Name'
30+
31+
export
32+
Return
33+
34+
define
35+
Return = reflection([
36+
become(
37+
keys: [reflection become]
38+
1: proc {$}
39+
P = {NewCell 1}
40+
R = {NewArray 0 1 _}
41+
Q = P
42+
in
43+
true = (P == Q)
44+
true = (P \= R)
45+
true = (Q \= R)
46+
true = {IsCell P}
47+
true = {IsCell Q}
48+
true = {IsArray R}
49+
{Reflection.become P R}
50+
true = (P == Q)
51+
true = (P == R)
52+
true = (Q == R)
53+
false = {IsCell P}
54+
false = {IsCell Q}
55+
true = {IsArray P}
56+
true = {IsArray Q}
57+
true = {IsArray R}
58+
end
59+
)
60+
61+
getStructuralBehavior(
62+
keys: [reflection getStructuralBehavior]
63+
1: proc {$}
64+
P
65+
class C from BaseObject end
66+
in
67+
for A#B in [
68+
value#1
69+
value#x
70+
value#unit
71+
value#true
72+
value#false
73+
value#1.5
74+
value#~0.3
75+
value#nil
76+
value#{VirtualString.toCompactString "12345"}
77+
value#{VirtualByteString.toCompactByteString [0 1 2 3 4]}
78+
value#{BootName.newUnique test}
79+
structural#"string"
80+
structural#[1 2 3 4]
81+
structural#(1#2)
82+
structural#(r(a:1 b:2))
83+
token#{NewCell 1}
84+
token#{NewArray 0 1 _}
85+
token#C
86+
token#{New C noop}
87+
token#{NewName}
88+
token#{NewLock}
89+
token#{NewPort _}
90+
token#proc {$} skip end
91+
token#{NewWeakDictionary _}
92+
token#{NewDictionary}
93+
token#{NewChunk x}
94+
variable#_
95+
variable#P
96+
variable#(!!P)
97+
] do
98+
A = {Reflection.getStructuralBehavior B}
99+
end
100+
end
101+
)
102+
103+
reflectiveVariable(
104+
keys: [reflection reflectiveVariable]
105+
1: proc {$}
106+
S = _
107+
V = {Reflection.newReflectiveVariable S}
108+
Q = V
109+
A B C
110+
in
111+
thread
112+
true = (Q == 5)
113+
end
114+
thread
115+
{Delay 100}
116+
V = 5
117+
true = (V == 5)
118+
end
119+
120+
variable = {Reflection.getStructuralBehavior V}
121+
122+
A|B = S
123+
markNeeded = A.1 % <- probably we don't need to do anything with `markNeeded`?
124+
A.2 = unit
125+
C|_ = B
126+
bind(5) = C.1
127+
C.2 = unit
128+
129+
variable = {Reflection.getStructuralBehavior V}
130+
{Reflection.bindReflectiveVariable V 5}
131+
value = {Reflection.getStructuralBehavior V}
132+
end
133+
)
134+
135+
reflectiveEntity(
136+
keys: [reflection reflectiveEntity]
137+
1: proc {$}
138+
S
139+
E = {Reflection.newReflectiveEntity S}
140+
G
141+
in
142+
thread
143+
123 = @E
144+
456 = {Get E 678}
145+
G = 789
146+
end
147+
148+
token = {Reflection.getStructuralBehavior E}
149+
150+
(isCell(true)#unit)|(access(123)#unit)|(arrayGet(678 456)#unit)|_ = S
151+
152+
{Wait G}
153+
789 = G
154+
end
155+
)
156+
157+
throwExceptionEntity(
158+
keys: [reflection throwExceptionEntity]
159+
1: proc {$}
160+
SE
161+
E = {Reflection.newReflectiveEntity SE}
162+
EException
163+
in
164+
thread
165+
try
166+
{Put E 1 2}
167+
EException = unit
168+
catch X then
169+
EException = X
170+
end
171+
end
172+
173+
(_#{Value.failed someException})|_ = SE
174+
true = (EException == someException)
175+
end
176+
)
177+
178+
throwExceptionVariable(
179+
keys: [reflection throwExceptionVariable]
180+
1: proc {$}
181+
SV
182+
V = {Reflection.newReflectiveVariable SV}
183+
VException
184+
ExceptionToRaise = {Exception.failure something}
185+
in
186+
thread
187+
try
188+
V = 4
189+
VException = unit
190+
catch X then
191+
VException = X
192+
end
193+
end
194+
195+
(_#{Value.failed ExceptionToRaise})|_ = SV
196+
true = (VException == ExceptionToRaise)
197+
end
198+
)
199+
])
200+
end

0 commit comments

Comments
 (0)