Skip to content

Commit ca0599b

Browse files
committed
capture variables for inline Custom Classes
1 parent d055203 commit ca0599b

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

hscript/Interp.hx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ class Interp {
879879
usings: [for(u in this.usings) u.name],
880880
pkg: null,
881881
ogInterp: this,
882-
usePublicVars: true
882+
isInline: true
883883
};
884884

885885
registerCustomClass(customClassDecl, null);
@@ -1182,7 +1182,7 @@ class Interp {
11821182
me.depth = depth;
11831183
return r;
11841184
};
1185-
var f = Reflect.makeVarArgs(f);
1185+
var f:Function = Reflect.makeVarArgs(f);
11861186
if (name != null) {
11871187
if (depth == 0) {
11881188
// global function

hscript/customclass/CustomClass.hx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
4040

4141
public var __allowSetGet:Bool = false;
4242

43+
private var isInline:Bool = false;
44+
private var ogVariables:Map<String, Dynamic>;
45+
4346
public function new(__class:CustomClassDecl, args:Array<Dynamic>, ?extendFieldDecl:Map<String, Dynamic>, ?ogInterp:Interp, ?callNew:Bool = true) {
4447
this.__class = __class;
4548
this.interp = new Interp(this);
@@ -50,7 +53,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
5053
interp.allowStaticVariables = ogInterp.allowStaticVariables;
5154
interp.staticVariables = ogInterp.staticVariables;
5255
// todo: make it so you can use variables from the same scope as where the class was defined
53-
if(__class.usePublicVars) {
56+
if(__class.isInline != null && __class.isInline) {
57+
isInline = __class.isInline;
58+
ogVariables = ogInterp.variables;
5459
interp.allowPublicVariables = __class.staticInterp.allowPublicVariables;
5560
interp.publicVariables = __class.staticInterp.publicVariables;
5661
}
@@ -131,15 +136,25 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
131136
if (args == null)
132137
args = [];
133138

134-
if(__class.superClassDecl is CustomClassDecl)
139+
// TODO: disallow copy for super Custom Class
140+
if(__class.superClassDecl is CustomClassDecl)
135141
superClass = new CustomClass(__class.superClassDecl, args, _cachedFieldDecls, this.interp);
136142
else {
137143
if (_cachedSuperFields != null) {
138144
Reflect.setField(__class.superClassDecl, "__cachedFields", _cachedSuperFields); // Static field
139145
}
146+
147+
var disallowCopy = Type.getInstanceFields(__class.superClassDecl);
148+
140149
superClass = Type.createInstance(__class.superClassDecl, args);
141150
superClass.__customClass = this;
142-
superClass.__real_fields = Type.getInstanceFields(__class.superClassDecl);
151+
superClass.__real_fields = disallowCopy;
152+
153+
if(isInline) {
154+
for(s => v in ogVariables)
155+
if(!disallowCopy.contains(s))
156+
interp.variables.set(s, v);
157+
}
143158
}
144159
/*
145160
var extendString = new Printer().typeToString(__class.classDecl.extend);

hscript/customclass/CustomClassDecl.hx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
1515
public var usings:Array<String>;
1616
public var pkg:Null<Array<String>> = null;
1717
public var ogInterp:Null<Interp> = null;
18-
public var usePublicVars:Null<Bool> = null;
18+
public var isInline:Null<Bool> = null;
1919

2020
public var staticInterp:Interp = new Interp();
2121

@@ -27,27 +27,26 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
2727

2828
public var __allowSetGet:Bool = true;
2929

30-
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, ?pkg:Array<String>, ?ogInterp:Interp, ?usePublicVars:Bool) {
30+
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, ?pkg:Array<String>, ?ogInterp:Interp, ?isInline:Bool) {
3131
this.classDecl = classDecl;
3232
this.imports = imports;
3333
this.usings = usings;
3434
this.pkg = pkg;
3535
this.ogInterp = ogInterp;
36-
this.usePublicVars = usePublicVars;
36+
this.isInline = isInline;
3737

3838
if(ogInterp != null) {
3939
staticInterp.importFailedCallback = ogInterp.importFailedCallback;
4040
staticInterp.errorHandler = ogInterp.errorHandler;
4141
staticInterp.allowStaticVariables = ogInterp.allowStaticVariables;
4242
staticInterp.staticVariables = ogInterp.staticVariables;
4343

44-
if(usePublicVars != null && usePublicVars) {
44+
if(isInline != null && isInline) {
4545
// uses public variables from the same scope as where the class was defined
46+
staticInterp.variables = ogInterp.variables;
4647
staticInterp.allowPublicVariables = ogInterp.allowPublicVariables;
4748
staticInterp.publicVariables = ogInterp.publicVariables;
4849
}
49-
50-
// TODO: use normal variables, not just public
5150
}
5251

5352
cacheImports();
@@ -64,6 +63,8 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
6463
var importedClass = imp.fullPath;
6564
var importAlias = imp.as;
6665

66+
if(this.staticInterp.variables.exists(imp.name)) continue; // class is already imported
67+
6768
if (Interp.customClassExist(importedClass) && this.staticInterp.importFailedCallback != null) {
6869
this.staticInterp.importFailedCallback(importedClass.split("."), importAlias);
6970
continue;

0 commit comments

Comments
 (0)