Skip to content

Commit 31882f1

Browse files
committed
added Checker.allowGlobalTypes + Interp.allowTypeResolve()
1 parent 4fdb4e0 commit 31882f1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

hscript/Checker.hx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ class Checker {
462462
public var allowUntypedMeta : Bool;
463463
public var allowPrivateAccess : Bool;
464464
public var allowNew : Bool;
465+
public var allowGlobalTypes : Bool;
465466

466467
public function new( ?types ) {
467468
if( types == null ) types = new CheckerTypes();
@@ -1125,7 +1126,7 @@ class Checker {
11251126
return null;
11261127
}
11271128
var acc = mk(null,e);
1128-
var impl = resolveGlobal(a.impl.name,acc,Value,false);
1129+
var impl = resolveGlobal(a.impl.name,acc,null/* skip resolveGlobal */,false);
11291130
if( impl == null )
11301131
return null;
11311132
var t = checkField(cf,a,pl,forWrite,e);
@@ -1241,7 +1242,7 @@ class Checker {
12411242
var ft = getField(ot, p.f, p.e, p == path[path.length-1] ? forWrite : false);
12421243
if( ft == null ) {
12431244
switch( ot ) {
1244-
case TInst(c, _) if( c.name == "#Std" ):
1245+
case TInst(c, _) if( c.name == "#Std" && allowGlobalTypes ):
12451246
// these two methods are extern in HL and we must provide
12461247
// some stubs so they both type and execute
12471248
switch( p.f ) {
@@ -1378,7 +1379,7 @@ class Checker {
13781379
return TDynamic;
13791380
default:
13801381
#if hscriptPos
1381-
var wt = switch( withType ) { case WithType(t): follow(t); default: null; };
1382+
var wt = switch( withType ) { case null: null; case WithType(t): follow(t); default: null; };
13821383
switch( wt ) {
13831384
case null:
13841385
// enum constructor resolution
@@ -1400,7 +1401,7 @@ class Checker {
14001401
return wt;
14011402
default:
14021403
}
1403-
// this variable resolution
1404+
// this variable resolution : if we have a this, we can access it
14041405
var g = locals.get("this");
14051406
if( g == null ) g = globals.get("this");
14061407
if( g != null ) {
@@ -1440,7 +1441,7 @@ class Checker {
14401441
break;
14411442
}
14421443
}
1443-
if( !t.match(TUnresolved(_)) ) {
1444+
if( !t.match(TUnresolved(_)) && (allowGlobalTypes || withType == null) ) {
14441445
var acc = getTypeAccess(t, expr);
14451446
if( acc != null ) {
14461447
expr.e = acc;
@@ -1460,7 +1461,14 @@ class Checker {
14601461
}
14611462

14621463
function getTypeAccess( t : TType, expr : Expr, ?field : String ) : ExprDef {
1463-
return null;
1464+
var path = switch( t ) {
1465+
case TInst(c,_): c.name;
1466+
case TEnum(e,_): e.name;
1467+
default: return null;
1468+
}
1469+
var e : hscript.Expr.ExprDef = ECall(mk(EIdent("$resolve"),expr),[mk(EConst(CString(path)),expr)]);
1470+
if( field != null ) e = EField(mk(e,expr),field);
1471+
return e;
14641472
}
14651473

14661474
function unifyCallParams( args : Array<{ name : String, opt : Bool, t : TType }>, params : Array<Expr>, pos : Expr ) {

hscript/Interp.hx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ class Interp {
5252
initOps();
5353
}
5454

55+
public function allowTypeResolve() {
56+
variables.set("$resolve", resolveType);
57+
}
58+
59+
function resolveType( path : String ) : Dynamic {
60+
var c = std.Type.resolveClass(path);
61+
if( c != null ) return c;
62+
var e = std.Type.resolveEnum(path);
63+
if( e != null ) return e;
64+
throw "Invalid type "+path;
65+
}
66+
5567
private function resetVariables(){
5668
variables = new Map<String,Dynamic>();
5769
variables.set("null",null);

hscript/LiveClass.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ class LiveClassRuntime {
235235
var chk = new hscript.Checker(LiveClass.getTypes());
236236
chk.allowNew = true;
237237
chk.allowPrivateAccess = true;
238+
chk.allowGlobalTypes = true;
238239
chk.setGlobal("this", type);
239240
for( v in newVars )
240241
chk.setGlobal(v.name, v.type);
@@ -256,6 +257,7 @@ class LiveClassRuntime {
256257
var interp : LiveClassInterp = obj.__interp_inst;
257258
if( interp == null ) {
258259
interp = new LiveClassInterp();
260+
interp.allowTypeResolve();
259261
interp.variables.set("this", obj);
260262
obj.__interp_inst = interp;
261263
}

0 commit comments

Comments
 (0)