"Type" for Custom Classes - Part II
- Move the superClass instantiation to CustomClassDecl - Imported classes available for Static Functions Warning: untested code.
This commit is contained in:
@@ -23,16 +23,12 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
|
||||
|
||||
public var superClass:Dynamic;
|
||||
public var superConstructor(default, null):Dynamic;
|
||||
|
||||
// TODO: move this to CustomClassDecl
|
||||
public var className(get, never):String;
|
||||
private function get_className():String {
|
||||
// TODO: simplify this
|
||||
var name = "";
|
||||
if (__class.pkg != null) {
|
||||
name += __class.pkg.join(".");
|
||||
}
|
||||
name += __class.classDecl.name;
|
||||
return name;
|
||||
var pkg = __class.pkg != null ? '${__class.pkg.join(".")}.' : "";
|
||||
var name = __class.classDecl.name;
|
||||
return '$pkg$name';
|
||||
}
|
||||
|
||||
private var __class:CustomClassDecl;
|
||||
@@ -128,6 +124,17 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
|
||||
if (args == null)
|
||||
args = [];
|
||||
|
||||
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
|
||||
}
|
||||
superClass = Type.createInstance(__class.superClassDecl, args);
|
||||
superClass.__customClass = this;
|
||||
superClass.__real_fields = Type.getInstanceFields(__class.superClassDecl);
|
||||
}
|
||||
/*
|
||||
var extendString = new Printer().typeToString(__class.classDecl.extend);
|
||||
if (__class.pkg != null && extendString.indexOf(".") == -1) {
|
||||
extendString = __class.pkg.join(".") + "." + extendString;
|
||||
@@ -149,6 +156,7 @@ class CustomClass implements IHScriptCustomAccessBehaviour {
|
||||
superClass.__customClass = this;
|
||||
superClass.__real_fields = Type.getInstanceFields(c);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
function buildImports() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package hscript.customclass;
|
||||
|
||||
import hscript.proxy.ProxyType;
|
||||
import hscript.customclass.utils.FunctionUtils;
|
||||
import haxe.Constraints.Function;
|
||||
import hscript.Expr.FieldDecl;
|
||||
@@ -13,23 +14,63 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
|
||||
public var imports:Map<String, CustomClassImport>;
|
||||
public var usings:Array<String>;
|
||||
public var pkg:Null<Array<String>> = null;
|
||||
public var ogInterp:Interp = null;
|
||||
|
||||
public var staticInterp:Interp = new Interp();
|
||||
|
||||
public var superClassDecl(default, null):Dynamic = null; //This holds the super class reference.
|
||||
|
||||
var _cachedStaticFields:Map<String, FieldDecl> = [];
|
||||
var _cachedStaticFunctions:Map<String, FunctionDecl> = [];
|
||||
var _cachedStaticVariables:Map<String, VarDecl> = [];
|
||||
|
||||
public var __allowSetGet:Bool = true;
|
||||
|
||||
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, pkg:Null<Array<String>>) {
|
||||
public function new(classDecl:Expr.ClassDecl, imports:Map<String, CustomClassImport>, usings:Array<String>, pkg:Null<Array<String>>, ogInterp:Interp = null) {
|
||||
this.classDecl = classDecl;
|
||||
this.imports = imports;
|
||||
this.usings = usings;
|
||||
this.pkg = pkg;
|
||||
this.ogInterp = ogInterp;
|
||||
|
||||
if(ogInterp != null && ogInterp.importFailedCallback != null && ogInterp.errorHandler != null) {
|
||||
staticInterp.importFailedCallback = ogInterp.importFailedCallback;
|
||||
staticInterp.errorHandler = ogInterp.errorHandler;
|
||||
staticInterp.allowStaticVariables = ogInterp.allowStaticVariables;
|
||||
staticInterp.staticVariables = ogInterp.staticVariables;
|
||||
}
|
||||
|
||||
cacheImports();
|
||||
cacheFields();
|
||||
processUsings();
|
||||
if(classDecl.extend != null)
|
||||
buildSuperClass();
|
||||
}
|
||||
|
||||
function cacheImports() {
|
||||
// This will make imported classes available for Static Functions
|
||||
for(i => imp in imports) {
|
||||
var importedClass = imp.fullPath;
|
||||
var importAlias = imp.as;
|
||||
|
||||
if (Interp.customClassExist(importedClass) && this.staticInterp.importFailedCallback != null) {
|
||||
this.staticInterp.importFailedCallback(importedClass.split("."), importAlias);
|
||||
continue;
|
||||
}
|
||||
|
||||
#if hscriptPos
|
||||
var e:Expr = {
|
||||
e: ExprDef.EImport(importedClass, importAlias),
|
||||
pmin: 0,
|
||||
pmax: 0,
|
||||
origin: this.className,
|
||||
line: i
|
||||
};
|
||||
#else
|
||||
var e = Expr.EImport(importedClass, importAlias);
|
||||
#end
|
||||
this.staticInterp.expr(e);
|
||||
}
|
||||
}
|
||||
|
||||
function cacheFields() {
|
||||
@@ -69,6 +110,17 @@ class CustomClassDecl implements IHScriptCustomAccessBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
function buildSuperClass() {
|
||||
var extendString = new Printer().typeToString(classDecl.extend);
|
||||
if (this.pkg != null && extendString.indexOf(".") == -1) {
|
||||
extendString = this.pkg.join(".") + "." + extendString;
|
||||
}
|
||||
|
||||
superClassDecl = ProxyType.resolveClass(extendString);
|
||||
if(superClassDecl == null)
|
||||
staticInterp.error(ECustom("could not resolve super class: " + extendString));
|
||||
}
|
||||
|
||||
public function callFunction(name:String, ?args:Array<Dynamic>):Dynamic {
|
||||
var func:Function = getFunction(name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user