capture variables for inline Custom Classes
This commit is contained in:
+2
-2
@@ -879,7 +879,7 @@ class Interp {
|
||||
usings: [for(u in this.usings) u.name],
|
||||
pkg: null,
|
||||
ogInterp: this,
|
||||
usePublicVars: true
|
||||
isInline: true
|
||||
};
|
||||
|
||||
registerCustomClass(customClassDecl, null);
|
||||
@@ -1182,7 +1182,7 @@ class Interp {
|
||||
me.depth = depth;
|
||||
return r;
|
||||
};
|
||||
var f = Reflect.makeVarArgs(f);
|
||||
var f:Function = Reflect.makeVarArgs(f);
|
||||
if (name != null) {
|
||||
if (depth == 0) {
|
||||
// global function
|
||||
|
||||
@@ -40,6 +40,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
|
||||
|
||||
public var __allowSetGet:Bool = false;
|
||||
|
||||
private var isInline:Bool = false;
|
||||
private var ogVariables:Map<String, Dynamic>;
|
||||
|
||||
public function new(__class:CustomClassDecl, args:Array<Dynamic>, ?extendFieldDecl:Map<String, Dynamic>, ?ogInterp:Interp, ?callNew:Bool = true) {
|
||||
this.__class = __class;
|
||||
this.interp = new Interp(this);
|
||||
@@ -50,7 +53,9 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
|
||||
interp.allowStaticVariables = ogInterp.allowStaticVariables;
|
||||
interp.staticVariables = ogInterp.staticVariables;
|
||||
// todo: make it so you can use variables from the same scope as where the class was defined
|
||||
if(__class.usePublicVars) {
|
||||
if(__class.isInline != null && __class.isInline) {
|
||||
isInline = __class.isInline;
|
||||
ogVariables = ogInterp.variables;
|
||||
interp.allowPublicVariables = __class.staticInterp.allowPublicVariables;
|
||||
interp.publicVariables = __class.staticInterp.publicVariables;
|
||||
}
|
||||
@@ -131,15 +136,25 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
|
||||
if (args == null)
|
||||
args = [];
|
||||
|
||||
if(__class.superClassDecl is CustomClassDecl)
|
||||
// TODO: disallow copy for super Custom Class
|
||||
if(__class.superClassDecl is CustomClassDecl)
|
||||
superClass = new CustomClass(__class.superClassDecl, args, _cachedFieldDecls, this.interp);
|
||||
else {
|
||||
if (_cachedSuperFields != null) {
|
||||
Reflect.setField(__class.superClassDecl, "__cachedFields", _cachedSuperFields); // Static field
|
||||
}
|
||||
|
||||
var disallowCopy = Type.getInstanceFields(__class.superClassDecl);
|
||||
|
||||
superClass = Type.createInstance(__class.superClassDecl, args);
|
||||
superClass.__customClass = this;
|
||||
superClass.__real_fields = Type.getInstanceFields(__class.superClassDecl);
|
||||
superClass.__real_fields = disallowCopy;
|
||||
|
||||
if(isInline) {
|
||||
for(s => v in ogVariables)
|
||||
if(!disallowCopy.contains(s))
|
||||
interp.variables.set(s, v);
|
||||
}
|
||||
}
|
||||
/*
|
||||
var extendString = new Printer().typeToString(__class.classDecl.extend);
|
||||
|
||||
@@ -15,7 +15,7 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
|
||||
public var usings:Array<String>;
|
||||
public var pkg:Null<Array<String>> = null;
|
||||
public var ogInterp:Null<Interp> = null;
|
||||
public var usePublicVars:Null<Bool> = null;
|
||||
public var isInline:Null<Bool> = null;
|
||||
|
||||
public var staticInterp:Interp = new Interp();
|
||||
|
||||
@@ -27,13 +27,13 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
|
||||
|
||||
public var __allowSetGet:Bool = true;
|
||||
|
||||
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, ?pkg:Array<String>, ?ogInterp:Interp, ?usePublicVars:Bool) {
|
||||
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, ?pkg:Array<String>, ?ogInterp:Interp, ?isInline:Bool) {
|
||||
this.classDecl = classDecl;
|
||||
this.imports = imports;
|
||||
this.usings = usings;
|
||||
this.pkg = pkg;
|
||||
this.ogInterp = ogInterp;
|
||||
this.usePublicVars = usePublicVars;
|
||||
this.isInline = isInline;
|
||||
|
||||
if(ogInterp != null) {
|
||||
staticInterp.importFailedCallback = ogInterp.importFailedCallback;
|
||||
@@ -41,13 +41,12 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
|
||||
staticInterp.allowStaticVariables = ogInterp.allowStaticVariables;
|
||||
staticInterp.staticVariables = ogInterp.staticVariables;
|
||||
|
||||
if(usePublicVars != null && usePublicVars) {
|
||||
if(isInline != null && isInline) {
|
||||
// uses public variables from the same scope as where the class was defined
|
||||
staticInterp.variables = ogInterp.variables;
|
||||
staticInterp.allowPublicVariables = ogInterp.allowPublicVariables;
|
||||
staticInterp.publicVariables = ogInterp.publicVariables;
|
||||
}
|
||||
|
||||
// TODO: use normal variables, not just public
|
||||
}
|
||||
|
||||
cacheImports();
|
||||
@@ -64,6 +63,8 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
|
||||
var importedClass = imp.fullPath;
|
||||
var importAlias = imp.as;
|
||||
|
||||
if(this.staticInterp.variables.exists(imp.name)) continue; // class is already imported
|
||||
|
||||
if (Interp.customClassExist(importedClass) && this.staticInterp.importFailedCallback != null) {
|
||||
this.staticInterp.importFailedCallback(importedClass.split("."), importAlias);
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user