Skip to content

Commit e23573e

Browse files
committed
check statics for write & private access. added @:privateAccess support
1 parent 22b3f59 commit e23573e

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

hscript/Checker.hx

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef CClass = {> CNamedType,
5555
var ?isInterface : Bool;
5656
var fields : Map<String,CField>;
5757
var statics : Map<String,CField>;
58+
var staticClass : TType;
5859
}
5960

6061
typedef CField = {
@@ -70,6 +71,7 @@ typedef CField = {
7071

7172
typedef CEnum = {> CNamedType,
7273
var constructors : Array<{ name : String, ?args : Array<{ name : String, opt : Bool, t : TType }> }>;
74+
var enumClass : TType;
7375
}
7476

7577
typedef CTypedef = {> CNamedType,
@@ -129,6 +131,7 @@ class CheckerTypes {
129131
fields : [],
130132
statics : [],
131133
params : [],
134+
staticClass : null,
132135
};
133136
types.set(name, CTClass(ct));
134137
return ct;
@@ -183,6 +186,7 @@ class CheckerTypes {
183186
params : [],
184187
fields : new Map(),
185188
statics : new Map(),
189+
staticClass: null,
186190
};
187191
addMeta(c,cl);
188192
if( c.isInterface )
@@ -218,9 +222,20 @@ class CheckerTypes {
218222
cl.fields.set(f.name, f);
219223
}
220224
}
225+
var stc : CClass = {
226+
name : "#"+cl.name,
227+
fields : [],
228+
statics: [],
229+
params: [],
230+
staticClass : null,
231+
};
232+
cl.staticClass = TInst(stc,[]);
221233
for( f in c.statics ) {
222234
var f = addField(f);
223-
if( f != null ) cl.statics.set(f.name, f);
235+
if( f != null ) {
236+
cl.statics.set(f.name, f);
237+
stc.fields.set(f.name, f);
238+
}
224239
}
225240
localParams = null;
226241
});
@@ -231,6 +246,7 @@ class CheckerTypes {
231246
name : e.path,
232247
params : [],
233248
constructors: [],
249+
enumClass: null,
234250
};
235251
addMeta(e,en);
236252
for( p in e.params )
@@ -240,6 +256,8 @@ class CheckerTypes {
240256
for( c in e.constructors )
241257
en.constructors.push({ name : c.name, args : c.args == null ? null : [for( a in c.args ) { name : a.name, opt : a.opt, t : makeXmlType(a.t) }] });
242258
localParams = null;
259+
var ent = TEnum(en,en.params);
260+
en.enumClass = TType({ name : "#"+en.name, params : [], t : TAnon([for( f in en.constructors ) { name : f.name, t : f.args == null ? ent : TFun(f.args,ent), opt : false }]) },[]);
243261
});
244262
types.set(en.name, CTEnum(en));
245263
case TTypedecl(t):
@@ -393,6 +411,7 @@ class Checker {
393411
public var allowReturn : Null<TType>;
394412
public var allowGlobalsDefine : Bool;
395413
public var allowUntypedMeta : Bool;
414+
public var allowPrivateAccess : Bool;
396415

397416
public function new( ?types ) {
398417
if( types == null ) types = new CheckerTypes();
@@ -866,7 +885,7 @@ class Checker {
866885
var at = apply(arg.t, s.params, [for( _ in s.params ) makeMono()]);
867886
at = apply(at,a.params,args);
868887
var acc = mk(null,e);
869-
if( tryUnify(to,at) && resolveGlobal(a.impl.name,acc,Value) != null ) {
888+
if( tryUnify(to,at) && resolveGlobal(a.impl.name,acc,Value,false) != null ) {
870889
e.e = ECall(mk(EField(acc,s.name),e),[mk(e.e,e)]);
871890
return true;
872891
}
@@ -1045,7 +1064,7 @@ class Checker {
10451064
return null;
10461065
}
10471066
var acc = mk(null,e);
1048-
var impl = resolveGlobal(a.impl.name,acc,Value);
1067+
var impl = resolveGlobal(a.impl.name,acc,Value,false);
10491068
if( impl == null )
10501069
return null;
10511070
var t = checkField(cf,a,pl,forWrite,e);
@@ -1158,7 +1177,7 @@ class Checker {
11581177
path.shift();
11591178
return readPath(l,path,forWrite);
11601179
}
1161-
var t = resolveGlobal(root.f,root.e,path.length == 1 ? withType : Value);
1180+
var t = resolveGlobal(root.f,root.e,path.length == 1 ? withType : Value, forWrite && path.length == 1);
11621181
if( t != null ) {
11631182
path.shift();
11641183
return readPath(t,path,forWrite);
@@ -1167,7 +1186,7 @@ class Checker {
11671186
while( path.length > 1 ) {
11681187
var name = [for( p in path ) p.f].join(".");
11691188
var union = punion(path[0].e,path[path.length-1].e);
1170-
var t = resolveGlobal(name, union, Value);
1189+
var t = resolveGlobal(name, union, Value, forWrite && fields.length == 0);
11711190
if( t != null ) {
11721191
#if hscriptPos
11731192
if( union.e != null ) path[path.length-1].e.e = union.e;
@@ -1188,7 +1207,7 @@ class Checker {
11881207
return false;
11891208
}
11901209

1191-
function resolveGlobal( name : String, expr : Expr, withType : WithType ) : TType {
1210+
function resolveGlobal( name : String, expr : Expr, withType : WithType, forWrite : Bool ) : TType {
11921211
var g = globals.get(name);
11931212
if( g != null ) {
11941213
return switch( g ) {
@@ -1257,7 +1276,7 @@ class Checker {
12571276
var acc = getTypeAccess(g, expr, name);
12581277
if( acc != null ) {
12591278
expr.e = acc;
1260-
return apply(f.t, f.params, [for( a in f.params ) makeMono()]);
1279+
return checkField(f,c,[for( a in f.params ) makeMono()], forWrite, expr);
12611280
}
12621281
}
12631282
default:
@@ -1270,10 +1289,10 @@ class Checker {
12701289
if( acc != null ) {
12711290
expr.e = acc;
12721291
switch( t ) {
1273-
case TInst(c,_):
1274-
return TType({ name : "#"+c.name, params : [], t : TAnon([for( f in c.statics ) { name : f.name, t : f.t, opt : false }]) },[]);
1292+
case TInst(c,_) if( c.staticClass != null ):
1293+
return c.staticClass;
12751294
case TEnum(e,_):
1276-
return TType({ name : "#"+e.name, params : [], t : TAnon([for( f in e.constructors ) { name : f.name, t : f.args == null ? t : TFun(f.args,t), opt : false }]) },[]);
1295+
return e.enumClass;
12771296
default:
12781297
throw "assert";
12791298
}
@@ -1691,6 +1710,13 @@ class Checker {
16911710
function checkMeta( m : String, args : Array<Expr>, next : Expr, expr : Expr, withType ) {
16921711
if( m == ":untyped" && allowUntypedMeta )
16931712
return makeMono();
1713+
if( m == ":privateAccess" && allowPrivateAccess ) {
1714+
var prev = checkPrivate;
1715+
checkPrivate = false;
1716+
var t = typeExpr(next, withType);
1717+
checkPrivate = prev;
1718+
return t;
1719+
}
16941720
return typeExpr(next, withType);
16951721
}
16961722

0 commit comments

Comments
 (0)