Skip to content

Commit 28a6197

Browse files
committed
getter and setter stuff
1 parent 0fbdf27 commit 28a6197

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

polymod/hscript/_internal/PolymodAbstractScriptClass.hx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ abstract PolymodAbstractScriptClass(PolymodScriptClass) from PolymodScriptClass
4747
{
4848
var v = this.findVar(name);
4949

50+
switch (v.get) {
51+
case "get":
52+
return this.callFunction('get_$name');
53+
54+
case "null":
55+
throw "Invalid access to field " + name;
56+
return null;
57+
}
58+
5059
var varValue:Dynamic = null;
5160
if (this._interp.variables.exists(name) == false)
5261
{
@@ -117,6 +126,17 @@ abstract PolymodAbstractScriptClass(PolymodScriptClass) from PolymodScriptClass
117126
case _:
118127
if (this.findVar(name) != null)
119128
{
129+
var decl = this.findVar(name);
130+
switch (decl.set) {
131+
case "set":
132+
this.callFunction('set_$name', [value]);
133+
return value;
134+
135+
case "never" | "null":
136+
throw "Invalid access to field " + name;
137+
return value;
138+
}
139+
120140
this._interp.variables.set(name, value);
121141
return value;
122142
}

polymod/hscript/_internal/PolymodInterpEx.hx

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ class PolymodInterpEx extends Interp
186186
return v;
187187
}
188188
}
189+
190+
@:privateAccess
191+
{
192+
if (_proxy != null)
193+
{
194+
var decl = _proxy.findVar(id);
195+
var v = expr(e2);
196+
switch (decl?.set)
197+
{
198+
case "set":
199+
var out = _proxy.callFunction('set_$id', [v]);
200+
return (out == null) ? v : out;
201+
202+
case "never":
203+
errorEx(EInvalidAccess(id));
204+
return null;
205+
}
206+
}
207+
}
189208
case EField(e0, id):
190209
// Make sure setting superclass fields works when using this.
191210
// Also ensures property functions are accounted for.
@@ -213,6 +232,76 @@ class PolymodInterpEx extends Interp
213232
return super.assign(e1, e2);
214233
}
215234

235+
override function increment(e:Expr, prefix:Bool, delta:Int)
236+
{
237+
switch (Tools.expr(e))
238+
{
239+
case EIdent(id):
240+
@:privateAccess
241+
{
242+
if (_proxy != null)
243+
{
244+
var decl = _proxy.findVar(id);
245+
if (decl != null)
246+
{
247+
var v = switch (decl.get)
248+
{
249+
case "get": _proxy.callFunction('get_$id');
250+
default: expr(decl.expr);
251+
}
252+
253+
if (prefix)
254+
v += delta;
255+
256+
switch(decl.set)
257+
{
258+
case "set":
259+
_proxy.callFunction('set_$id', [prefix ? v : (v += delta)]);
260+
return prefix ? v : (v += delta);
261+
case "never":
262+
errorEx(EInvalidAccess(id));
263+
return prefix ? v : (v += delta);
264+
}
265+
}
266+
}
267+
}
268+
default:
269+
}
270+
271+
return super.increment(e, prefix, delta);
272+
}
273+
274+
override function evalAssignOp(op:String, fop:(Dynamic->Dynamic)->Dynamic, e1:Expr, e2:Expr)
275+
{
276+
switch (Tools.expr(e1))
277+
{
278+
case EIdent(id):
279+
@:privateAccess
280+
{
281+
if (_proxy != null)
282+
{
283+
var decl = _proxy.findVar(id);
284+
if (decl != null)
285+
{
286+
var v = fop(expr(e1),expr(e2));
287+
288+
switch(decl.set)
289+
{
290+
case "set":
291+
_proxy.callFunction('set_$id', [v]);
292+
return v;
293+
case "never":
294+
errorEx(EInvalidAccess(id));
295+
return v;
296+
}
297+
}
298+
}
299+
}
300+
default:
301+
}
302+
return super.evalAssignOp(op, fop, e1, e2);
303+
}
304+
216305
public override function expr(e:Expr):Dynamic
217306
{
218307
// Override to provide some fixes, falling back to super.expr() when not needed.
@@ -226,6 +315,20 @@ class PolymodInterpEx extends Interp
226315
#end
227316
// These overrides are used to handle specific cases where problems occur.
228317

318+
case EIdent(id):
319+
@:privateAccess
320+
{
321+
if (_proxy != null)
322+
{
323+
var decl = _proxy.findVar(id);
324+
switch (decl?.get)
325+
{
326+
case "get":
327+
return _proxy.callFunction('get_$id');
328+
}
329+
}
330+
}
331+
229332
case EVar(n, _, e): // Fix to ensure local variables are committed properly.
230333
declared.push({n: n, old: locals.get(n)});
231334
var result = (e == null) ? null : expr(e);

0 commit comments

Comments
 (0)