Compare commits
98
Commits
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
/hscript.swf
|
/hscript.swf
|
||||||
/release.zip
|
/release.zip
|
||||||
|
dump/*
|
||||||
tests/bin/*
|
tests/bin/*
|
||||||
@@ -1,16 +1,28 @@
|
|||||||
hscript-improved
|
hscript-improved
|
||||||
=======
|
=======
|
||||||
|
|
||||||
|
- [Features](docs/FEATURES.md)
|
||||||
|
|
||||||
How to install
|
How to install
|
||||||
```
|
```
|
||||||
haxelib git hscript-improved https://github.com/FNF-CNE-Devs/hscript-improved.git
|
haxelib git hscript-improved https://github.com/CodenameCrew/hscript-improved.git
|
||||||
```
|
```
|
||||||
|
|
||||||
To enable custom classes support you have to do this in project.xml
|
To enable custom classes support you have to do this in project.xml
|
||||||
|
|
||||||
```xml
|
```xml
|
||||||
<define name="CUSTOM_CLASSES" />
|
<define name="CUSTOM_CLASSES" />
|
||||||
```
|
```
|
||||||
Warning: custom classes are sometimes broken, would like help to fix them. You can only override functions from the current class, not from the extended part, like you cant override update in FlxText because FlxText doesnt have a update function overriden
|
|
||||||
|
or set this in build.hxml
|
||||||
|
|
||||||
|
```hxml
|
||||||
|
-D CUSTOM_CLASSES
|
||||||
|
```
|
||||||
|
|
||||||
|
Current Custom Class Limitations :
|
||||||
|
|
||||||
|
- You cannot create custom classes that extends a typed class (those ones that has `<T>`), this will be implemented in the future.
|
||||||
|
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
# FEATURES
|
||||||
|
|
||||||
|
- Custom Classes
|
||||||
|
- Final Classes
|
||||||
|
- Static Classes
|
||||||
|
- Allow for type check (`is`)
|
||||||
|
- Allow extending other custom classes
|
||||||
|
- Enums
|
||||||
|
|
||||||
|
```haxe
|
||||||
|
enum TypeValue {
|
||||||
|
NUMBER(n:Int);
|
||||||
|
DECIMAL(d:Float, ?p:Int);
|
||||||
|
CHARACTER(s:String);
|
||||||
|
BOOLEAN(b:Bool);
|
||||||
|
}
|
||||||
|
|
||||||
|
var type = TypeValue.DECIMAL(10.1234, 2);
|
||||||
|
// You need to type the full enum field for each case
|
||||||
|
// i.e. you can't type the enum field directly (limitation for now)
|
||||||
|
switch(type) {
|
||||||
|
case TypeValue.NUMBER(number):
|
||||||
|
trace("number: " + number);
|
||||||
|
case TypeValue.DECIMAL(decimal, precision):
|
||||||
|
if(precision != null)
|
||||||
|
trace("decimal: " + decimal + " | rounded decimal: " + roundDecimal(decimal, precision));
|
||||||
|
else
|
||||||
|
trace("decimal: " + decimal);
|
||||||
|
case TypeValue.CHARACTER(char):
|
||||||
|
trace("character: " + char);
|
||||||
|
default:
|
||||||
|
trace("unknown type");
|
||||||
|
}
|
||||||
|
|
||||||
|
function roundDecimal(Value:Float, Precision:Int) {
|
||||||
|
var mult:Float = Math.pow(10, Precision);
|
||||||
|
return Math.fround(Value * mult) / mult;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- Enum matching with arguments for switch statements (for real and scripted enums)
|
||||||
|
- Property Fields (`(get, set)` variables)
|
||||||
|
|
||||||
|
```haxe
|
||||||
|
public var myvar(get, set):Int;
|
||||||
|
var _myvar:Int = 10;
|
||||||
|
|
||||||
|
function get_myvar():Int {
|
||||||
|
return _myvar;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_myvar(val:Int):Int {
|
||||||
|
if(val > 10) return _myvar = val;
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- `@:isVar` metadata support
|
||||||
|
|
||||||
|
- Static extension (`using`)
|
||||||
|
|
||||||
|
```haxe
|
||||||
|
using StringTools;
|
||||||
|
|
||||||
|
class IntExtender {
|
||||||
|
static public function triple(i:Int) {
|
||||||
|
return i * 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// need to create/import the custom class
|
||||||
|
// before setting the extension (limitation for now)
|
||||||
|
using IntExtender;
|
||||||
|
|
||||||
|
var str = " Hello World! ";
|
||||||
|
trace(str.trim()); // "Hello World!"
|
||||||
|
trace(12.triple()); // 36
|
||||||
|
```
|
||||||
|
|
||||||
|
- Support for real and custom classes
|
||||||
|
- Misc.
|
||||||
|
- Allow using type parameters for creating objects (i.e. `var a = new TypedObject<Int>();`)
|
||||||
|
- Allow `package` declaration. Ignored by the interpreter.
|
||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
--macro keep('IntIterator')
|
--macro keep('IntIterator')
|
||||||
--macro hscript.macros.UsingHandler.init()
|
--macro hscript.macros.AbstractHandler.init()
|
||||||
--macro hscript.macros.ClassExtendMacro.init()
|
--macro hscript.macros.ClassExtendMacro.init()
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "hscript",
|
"name": "hscript-improved",
|
||||||
"url": "https://github.com/HaxeFoundation/hscript",
|
"url": "https://github.com/HaxeFoundation/hscript",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "Haxe Script is a scripting engine for a subset of the Haxe language",
|
"description": "Haxe Script is a scripting engine for a subset of the Haxe language",
|
||||||
|
|||||||
+536
-536
File diff suppressed because it is too large
Load Diff
+12
-4
@@ -124,10 +124,18 @@ class Bytes {
|
|||||||
#end
|
#end
|
||||||
bout.addByte(Type.enumIndex(e));
|
bout.addByte(Type.enumIndex(e));
|
||||||
switch( e ) {
|
switch( e ) {
|
||||||
|
case EPackage(n):
|
||||||
|
// TODO
|
||||||
case EImport(c):
|
case EImport(c):
|
||||||
// TODO
|
// TODO
|
||||||
case EClass(_, _, _, _):
|
case EClass(_, _, _, _):
|
||||||
// TODO
|
// TODO
|
||||||
|
case EEnum(en):
|
||||||
|
// TODO
|
||||||
|
case ECast(e, _):
|
||||||
|
// TODO
|
||||||
|
case ERegex(e, f):
|
||||||
|
// TODO
|
||||||
case EConst(c):
|
case EConst(c):
|
||||||
doEncodeConst(c);
|
doEncodeConst(c);
|
||||||
case EIdent(v):
|
case EIdent(v):
|
||||||
@@ -148,11 +156,11 @@ class Bytes {
|
|||||||
doEncode(e);
|
doEncode(e);
|
||||||
doEncodeString(f);
|
doEncodeString(f);
|
||||||
case EBinop(op,e1,e2):
|
case EBinop(op,e1,e2):
|
||||||
doEncodeString(op);
|
doEncodeString(op.toString());
|
||||||
doEncode(e1);
|
doEncode(e1);
|
||||||
doEncode(e2);
|
doEncode(e2);
|
||||||
case EUnop(op,prefix,e):
|
case EUnop(op,prefix,e):
|
||||||
doEncodeString(op);
|
doEncodeString(op.toString()); // maybe doEncodeInt
|
||||||
bout.addByte(prefix?1:0);
|
bout.addByte(prefix?1:0);
|
||||||
doEncode(e);
|
doEncode(e);
|
||||||
case ECall(e,el):
|
case ECall(e,el):
|
||||||
@@ -270,11 +278,11 @@ class Bytes {
|
|||||||
var e = doDecode();
|
var e = doDecode();
|
||||||
EField(e,doDecodeString());
|
EField(e,doDecodeString());
|
||||||
case 6:
|
case 6:
|
||||||
var op = doDecodeString();
|
var op = Binop.fromString(doDecodeString());
|
||||||
var e1 = doDecode();
|
var e1 = doDecode();
|
||||||
EBinop(op,e1,doDecode());
|
EBinop(op,e1,doDecode());
|
||||||
case 7:
|
case 7:
|
||||||
var op = doDecodeString();
|
var op = Unop.fromString(doDecodeString());
|
||||||
var prefix = bin.get(pin++) != 0;
|
var prefix = bin.get(pin++) != 0;
|
||||||
EUnop(op,prefix,doDecode());
|
EUnop(op,prefix,doDecode());
|
||||||
case 8:
|
case 8:
|
||||||
|
|||||||
+33
-16
@@ -422,6 +422,9 @@ class Checker {
|
|||||||
return makeType(t,e);
|
return makeType(t,e);
|
||||||
case CTOpt(t):
|
case CTOpt(t):
|
||||||
return makeType(t,e);
|
return makeType(t,e);
|
||||||
|
case CTExpr(_):
|
||||||
|
error("Unsupported expr type parameter", e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1070,10 +1073,10 @@ class Checker {
|
|||||||
case EUnop(op, _, e):
|
case EUnop(op, _, e):
|
||||||
var et = typeExpr(e, Value);
|
var et = typeExpr(e, Value);
|
||||||
switch( op ) {
|
switch( op ) {
|
||||||
case "++", "--", "-":
|
case OpIncrement | OpDecrement | OpNeg:
|
||||||
unify(et,TInt,e);
|
unify(et,TInt,e);
|
||||||
return et;
|
return et;
|
||||||
case "!":
|
case OpNot:
|
||||||
unify(et,TBool,e);
|
unify(et,TBool,e);
|
||||||
return et;
|
return et;
|
||||||
default:
|
default:
|
||||||
@@ -1110,11 +1113,11 @@ class Checker {
|
|||||||
return TVoid;
|
return TVoid;
|
||||||
case EBinop(op, e1, e2):
|
case EBinop(op, e1, e2):
|
||||||
switch( op ) {
|
switch( op ) {
|
||||||
case "&", "|", "^", ">>", ">>>", "<<":
|
case OpAnd, OpOr, OpXor, OpShr, OpUshr, OpShl:
|
||||||
typeExprWith(e1,TInt);
|
typeExprWith(e1,TInt);
|
||||||
typeExprWith(e2,TInt);
|
typeExprWith(e2,TInt);
|
||||||
return TInt;
|
return TInt;
|
||||||
case "=":
|
case OpAssign:
|
||||||
if( allowDefine ) {
|
if( allowDefine ) {
|
||||||
switch( edef(e1) ) {
|
switch( edef(e1) ) {
|
||||||
case EIdent(i) if( !locals.exists(i) && !globals.exists(i) ):
|
case EIdent(i) if( !locals.exists(i) && !globals.exists(i) ):
|
||||||
@@ -1130,7 +1133,7 @@ class Checker {
|
|||||||
}
|
}
|
||||||
typeExprWith(e2,vt);
|
typeExprWith(e2,vt);
|
||||||
return vt;
|
return vt;
|
||||||
case "+":
|
case OpAdd:
|
||||||
var t1 = typeExpr(e1,WithType(TInt));
|
var t1 = typeExpr(e1,WithType(TInt));
|
||||||
var t2 = typeExpr(e2,WithType(t1));
|
var t2 = typeExpr(e2,WithType(t1));
|
||||||
tryUnify(t1,t2);
|
tryUnify(t1,t2);
|
||||||
@@ -1147,14 +1150,14 @@ class Checker {
|
|||||||
unify(t1, TFloat, e1);
|
unify(t1, TFloat, e1);
|
||||||
unify(t2, TFloat, e2);
|
unify(t2, TFloat, e2);
|
||||||
}
|
}
|
||||||
case "-", "*", "/", "%":
|
case OpSub, OpMult, OpDiv, OpMod:
|
||||||
var t1 = typeExpr(e1,WithType(TInt));
|
var t1 = typeExpr(e1,WithType(TInt));
|
||||||
var t2 = typeExpr(e2,WithType(t1));
|
var t2 = typeExpr(e2,WithType(t1));
|
||||||
if( !tryUnify(t1,t2) )
|
if( !tryUnify(t1,t2) )
|
||||||
unify(t2,t1,e2);
|
unify(t2,t1,e2);
|
||||||
switch( [follow(t1), follow(t2)]) {
|
switch( [follow(t1), follow(t2)]) {
|
||||||
case [TInt, TInt]:
|
case [TInt, TInt]:
|
||||||
if( op == "/" ) return TFloat;
|
if( op == OpDiv ) return TFloat;
|
||||||
return TInt;
|
return TInt;
|
||||||
case [TFloat|TDynamic, TInt|TDynamic], [TInt|TDynamic, TFloat|TDynamic], [TFloat, TFloat]:
|
case [TFloat|TDynamic, TInt|TDynamic], [TInt|TDynamic, TFloat|TDynamic], [TFloat, TFloat]:
|
||||||
return TFloat;
|
return TFloat;
|
||||||
@@ -1162,21 +1165,21 @@ class Checker {
|
|||||||
unify(t1, TFloat, e1);
|
unify(t1, TFloat, e1);
|
||||||
unify(t2, TFloat, e2);
|
unify(t2, TFloat, e2);
|
||||||
}
|
}
|
||||||
case "&&", "||":
|
case OpBoolAnd, OpBoolOr:
|
||||||
typeExprWith(e1,TBool);
|
typeExprWith(e1,TBool);
|
||||||
typeExprWith(e2,TBool);
|
typeExprWith(e2,TBool);
|
||||||
return TBool;
|
return TBool;
|
||||||
case "...":
|
case OpInterval:
|
||||||
typeExprWith(e1,TInt);
|
typeExprWith(e1,TInt);
|
||||||
typeExprWith(e2,TInt);
|
typeExprWith(e2,TInt);
|
||||||
return makeIterator(TInt);
|
return makeIterator(TInt);
|
||||||
case "==", "!=":
|
case OpEq, OpNeq:
|
||||||
var t1 = typeExpr(e1,Value);
|
var t1 = typeExpr(e1,Value);
|
||||||
var t2 = typeExpr(e2,WithType(t1));
|
var t2 = typeExpr(e2,WithType(t1));
|
||||||
if( !tryUnify(t1,t2) )
|
if( !tryUnify(t1,t2) )
|
||||||
unify(t2,t1,e2);
|
unify(t2,t1,e2);
|
||||||
return TBool;
|
return TBool;
|
||||||
case ">", "<", ">=", "<=":
|
case OpGt, OpLt, OpGte, OpLte:
|
||||||
var t1 = typeExpr(e1,Value);
|
var t1 = typeExpr(e1,Value);
|
||||||
var t2 = typeExpr(e2,WithType(t1));
|
var t2 = typeExpr(e2,WithType(t1));
|
||||||
if( !tryUnify(t1,t2) )
|
if( !tryUnify(t1,t2) )
|
||||||
@@ -1187,12 +1190,26 @@ class Checker {
|
|||||||
error("Cannot compare "+typeStr(t1), expr);
|
error("Cannot compare "+typeStr(t1), expr);
|
||||||
}
|
}
|
||||||
return TBool;
|
return TBool;
|
||||||
|
case OpAddAssign, OpSubAssign, OpMultAssign, OpDivAssign, OpModAssign, OpAndAssign, OpOrAssign, OpXorAssign, OpShlAssign, OpShrAssign, OpUshrAssign, OpNcoalAssign:
|
||||||
|
var baseOp = switch(op) {
|
||||||
|
case OpAddAssign: OpAdd;
|
||||||
|
case OpSubAssign: OpSub;
|
||||||
|
case OpMultAssign: OpMult;
|
||||||
|
case OpDivAssign: OpDiv;
|
||||||
|
case OpModAssign: OpMod;
|
||||||
|
case OpAndAssign: OpAnd;
|
||||||
|
case OpOrAssign: OpOr;
|
||||||
|
case OpXorAssign: OpXor;
|
||||||
|
case OpShlAssign: OpShl;
|
||||||
|
case OpShrAssign: OpShr;
|
||||||
|
case OpUshrAssign: OpUshr;
|
||||||
|
case OpNcoalAssign: OpNcoal;
|
||||||
|
default: op;
|
||||||
|
};
|
||||||
|
var t = typeExpr(mk(EBinop(baseOp,e1,e2),expr),withType);
|
||||||
|
return typeExpr(mk(EBinop(OpAssign,e1,e2),expr), withType);
|
||||||
default:
|
default:
|
||||||
if( op.charCodeAt(op.length-1) == "=".code ) {
|
error("Unsupported operation "+op.toString(), expr);
|
||||||
var t = typeExpr(mk(EBinop(op.substr(0,op.length-1),e1,e2),expr),withType);
|
|
||||||
return typeExpr(mk(EBinop("=",e1,e2),expr), withType);
|
|
||||||
}
|
|
||||||
error("Unsupported operation "+op, expr);
|
|
||||||
}
|
}
|
||||||
case ETry(etry, v, et, ecatch):
|
case ETry(etry, v, et, ecatch):
|
||||||
var vt = typeExpr(etry, withType);
|
var vt = typeExpr(etry, withType);
|
||||||
|
|||||||
@@ -0,0 +1,359 @@
|
|||||||
|
package hscript;
|
||||||
|
|
||||||
|
import hscript.utils.UnsafeReflect;
|
||||||
|
import haxe.Constraints.Function;
|
||||||
|
|
||||||
|
using Lambda;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Custom Class core.
|
||||||
|
*
|
||||||
|
* Provides handlers for custom classes.
|
||||||
|
*
|
||||||
|
* @author Jamextreme140
|
||||||
|
*/
|
||||||
|
@:access(hscript.CustomClassHandler)
|
||||||
|
class CustomClass implements IHScriptCustomClassBehaviour {
|
||||||
|
public var className(get, never):String;
|
||||||
|
|
||||||
|
private function get_className():String
|
||||||
|
return __class.name;
|
||||||
|
|
||||||
|
public var __interp:Interp;
|
||||||
|
public var __real_fields:Array<String> = []; // UNUSED
|
||||||
|
public var __class__fields:Array<String> = []; // Declared fields
|
||||||
|
|
||||||
|
public var __allowSetGet:Bool = true;
|
||||||
|
|
||||||
|
var __class:CustomClassHandler;
|
||||||
|
var __superClass:IHScriptCustomClassBehaviour;
|
||||||
|
var __upperClass:IHScriptCustomClassBehaviour;
|
||||||
|
var __constructor:Function;
|
||||||
|
|
||||||
|
var __overrideFields:Array<String> = [];
|
||||||
|
var __cachedFieldSet:Map<String, Dynamic> = null;
|
||||||
|
var initializing:Bool = false;
|
||||||
|
|
||||||
|
public function new(__class:CustomClassHandler, ?args:Array<Dynamic>, ?cachedFieldSet:Map<String, Dynamic>) {
|
||||||
|
this.__class = __class;
|
||||||
|
|
||||||
|
__interp = new Interp();
|
||||||
|
__interp.errorHandler = __class.__interp.errorHandler;
|
||||||
|
__interp.importFailedCallback = __class.__interp.importFailedCallback;
|
||||||
|
|
||||||
|
// __interp.variables = __class.staticInterp.variables;
|
||||||
|
@:privateAccess __interp.usingHandler.usingEntries = __class.ogInterp.usingHandler.usingEntries;
|
||||||
|
__interp.publicVariables = __class.ogInterp.publicVariables;
|
||||||
|
__interp.staticVariables = __class.ogInterp.staticVariables;
|
||||||
|
__interp.customClasses = __class.ogInterp.customClasses;
|
||||||
|
|
||||||
|
for(f => v in __class.__interp.variables) {
|
||||||
|
if(f == 'new') continue;
|
||||||
|
if (!__interp.variables.exists(f))
|
||||||
|
__interp.variables.set(f, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (f in __class.fields) {
|
||||||
|
switch (Tools.expr(f)) {
|
||||||
|
case EVar(n): __class__fields.push(n);
|
||||||
|
case EFunction(_, _, n, _, _, _, isOverride):
|
||||||
|
if(isOverride) __overrideFields.push(n);
|
||||||
|
__class__fields.push(n);
|
||||||
|
default: continue;
|
||||||
|
}
|
||||||
|
@:privateAccess __interp.exprReturn(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
__interp.scriptObject = this;
|
||||||
|
|
||||||
|
initializing = true;
|
||||||
|
|
||||||
|
if(cachedFieldSet != null)
|
||||||
|
for(f => v in cachedFieldSet)
|
||||||
|
this.hset(f, v);
|
||||||
|
|
||||||
|
if (hasField('new')) {
|
||||||
|
buildConstructor();
|
||||||
|
call('new', args);
|
||||||
|
|
||||||
|
if(__cachedFieldSet != null) {
|
||||||
|
__cachedFieldSet.clear();
|
||||||
|
__cachedFieldSet = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.__superClass == null && __class.extend != null)
|
||||||
|
__interp.error(ECustom("super() not called"));
|
||||||
|
} else if (__class.extend != null) {
|
||||||
|
buildSuperClass(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
initializing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cacheFieldSet(name:String, val:Dynamic) {
|
||||||
|
if(!initializing) return;
|
||||||
|
if(__cachedFieldSet == null) __cachedFieldSet = [];
|
||||||
|
__cachedFieldSet.set(name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildConstructor() {
|
||||||
|
__constructor = Reflect.makeVarArgs(buildSuperClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildSuperClass(?args:Array<Dynamic>) {
|
||||||
|
if (args == null)
|
||||||
|
args = [];
|
||||||
|
|
||||||
|
if (__class.cl == null) {
|
||||||
|
__interp.error(ECustom('Current class does not have a super'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (__class.cl is CustomClassHandler) {
|
||||||
|
var customClass = new CustomClass(__class.cl, args, __cachedFieldSet);
|
||||||
|
if(__overrideFields.length > 0) {
|
||||||
|
for (field in __overrideFields) {
|
||||||
|
var func = __interp.variables.get(field);
|
||||||
|
customClass.overrideField(field, func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
customClass.__upperClass = this;
|
||||||
|
__superClass = customClass;
|
||||||
|
@:privateAccess __interp.__instanceFields = __interp.__instanceFields.concat(getSuperFields());
|
||||||
|
} else {
|
||||||
|
if(__cachedFieldSet != null)
|
||||||
|
UnsafeReflect.setField(__class.cl, "__cachedFieldSet", __cachedFieldSet);
|
||||||
|
|
||||||
|
__superClass = Type.createInstance(__class.cl, args);
|
||||||
|
var disallowCopy:Array<String> = {
|
||||||
|
var fieldMap:Map<String, String> = []; // Prevent duplicate values
|
||||||
|
for(f in Reflect.fields(__superClass).concat(Type.getInstanceFields(Type.getClass(__superClass))))
|
||||||
|
fieldMap.set(f, f);
|
||||||
|
|
||||||
|
fieldMap.array();
|
||||||
|
}
|
||||||
|
this.__real_fields = disallowCopy;
|
||||||
|
@:privateAccess __interp.__instanceFields = __interp.__instanceFields.concat(disallowCopy);
|
||||||
|
__superClass.__real_fields = this.__real_fields;
|
||||||
|
__superClass.__class__fields = this.__class__fields;
|
||||||
|
__superClass.__interp = this.__interp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function call(name:String, ?args:Array<Dynamic>, ?toSuper:Bool = false):Dynamic {
|
||||||
|
var superFnName:Null<String> = toSuper ? '_HX_SUPER__$name' : null;
|
||||||
|
var fn:Dynamic = {
|
||||||
|
if(toSuper && __interp.variables.exists(superFnName)) {
|
||||||
|
__interp.variables.get(superFnName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
__interp.variables.get(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (fn != null && Reflect.isFunction(fn))
|
||||||
|
return UnsafeReflect.callMethodUnsafe(null, fn, (args == null) ? [] : args);
|
||||||
|
|
||||||
|
// If not found in current class, try parent class recursively
|
||||||
|
if (__superClass != null && __superClass is CustomClass)
|
||||||
|
return cast(__superClass, CustomClass).call(name, args, toSuper);
|
||||||
|
|
||||||
|
__interp.error(ECustom('$name doesn\'t exists or is not a function'));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasField(name:String) {
|
||||||
|
return __class__fields.contains(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasStaticField(name:String):Bool {
|
||||||
|
return __class.hasField(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getField(name:String, allowProperty:Bool = true):Dynamic {
|
||||||
|
var f = __interp.variables.get(name);
|
||||||
|
if (f != null && allowProperty && f is Property) {
|
||||||
|
var prop:Property = cast f;
|
||||||
|
//prop.__allowSetGet = this.__allowSetGet;
|
||||||
|
var r = prop.get(!__allowSetGet);
|
||||||
|
//prop.__allowSetGet = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setField(name:String, val:Dynamic):Dynamic {
|
||||||
|
var f = getField(name, false);
|
||||||
|
if (f != null && f is Property) {
|
||||||
|
var prop:Property = cast f;
|
||||||
|
//prop.__allowSetGet = this.__allowSetGet;
|
||||||
|
var r = prop.set(val, !__allowSetGet);
|
||||||
|
//prop.__allowSetGet = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
__interp.variables.set(name, val);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overrides (replaces) the declared function.
|
||||||
|
* @param name
|
||||||
|
* @param func
|
||||||
|
*/
|
||||||
|
function overrideField(name:String, func:Function) {
|
||||||
|
var f = getField(name, false);
|
||||||
|
if(f != null && Reflect.isFunction(f)) {
|
||||||
|
__interp.variables.set(name, func);
|
||||||
|
__interp.variables.set('_HX_SUPER__$name', f);
|
||||||
|
}
|
||||||
|
else if(__superClass != null && __superClass is CustomClass) {
|
||||||
|
cast(__superClass, CustomClass).overrideField(name, func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function superHasField(name:String) {
|
||||||
|
if (__superClass == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var realFieldExists = __superClass.__real_fields != null && __superClass.__real_fields.contains(name);
|
||||||
|
var classFieldExists = __superClass.__class__fields != null && __superClass.__class__fields.contains(name);
|
||||||
|
|
||||||
|
if(!realFieldExists && !classFieldExists && __superClass is CustomClass)
|
||||||
|
return cast(__superClass, CustomClass).superHasField(name);
|
||||||
|
|
||||||
|
return realFieldExists || classFieldExists;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSuperFields():Array<String> {
|
||||||
|
if(__superClass == null) return [];
|
||||||
|
|
||||||
|
var classFields:Map<String, String> = []; // Prevents duplicated values
|
||||||
|
var cls:Null<IHScriptCustomClassBehaviour> = __superClass;
|
||||||
|
|
||||||
|
while (cls != null) {
|
||||||
|
for(fieldSet in [cls.__class__fields, cls.__real_fields])
|
||||||
|
for(f in fieldSet)
|
||||||
|
classFields.set(f, f);
|
||||||
|
var next:IHScriptCustomClassBehaviour = null;
|
||||||
|
if(cls is CustomClass)
|
||||||
|
next = cast(cls, CustomClass).__superClass;
|
||||||
|
if (next == null)
|
||||||
|
break;
|
||||||
|
cls = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [for(f in classFields) f];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hget(name:String):Dynamic {
|
||||||
|
switch (name) {
|
||||||
|
case 'superClass': return __superClass;
|
||||||
|
case 'superConstructor': return __constructor;
|
||||||
|
default:
|
||||||
|
if (hasField(name))
|
||||||
|
return getField(name);
|
||||||
|
|
||||||
|
if (hasStaticField(name)) {
|
||||||
|
__interp.error(ECustom('The field ${name} should be accessed in a static way.'));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (__superClass != null) {
|
||||||
|
if (superHasField(name)) {
|
||||||
|
__superClass.__allowSetGet = this.__allowSetGet;
|
||||||
|
return __superClass.hget(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw "field '"
|
||||||
|
+ name
|
||||||
|
+ "' does not exist in custom class '"
|
||||||
|
+ this.className
|
||||||
|
+ "'"
|
||||||
|
+ (__superClass != null ? "' or super class '" + Type.getClassName(Type.getClass(this.__superClass)) + "'" : "");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hset(name:String, val:Dynamic):Dynamic {
|
||||||
|
if (hasField(name))
|
||||||
|
return setField(name, val);
|
||||||
|
|
||||||
|
if (hasStaticField(name)) {
|
||||||
|
__interp.error(ECustom('The field ${name} should be accessed in a static way.'));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (__superClass != null) {
|
||||||
|
if (superHasField(name)) {
|
||||||
|
__superClass.__allowSetGet = this.__allowSetGet;
|
||||||
|
return __superClass.hset(name, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(__class.extend != null && initializing) {
|
||||||
|
cacheFieldSet(name, val);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw "field '"
|
||||||
|
+ name
|
||||||
|
+ "' does not exist in custom class '"
|
||||||
|
+ this.className
|
||||||
|
+ "'"
|
||||||
|
+ (__superClass != null ? "' or super class '" + Type.getClassName(Type.getClass(this.__superClass)) + "'" : "");
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// UNUSED
|
||||||
|
public function __callGetter(name:String):Dynamic {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __callSetter(name:String, val:Dynamic):Dynamic {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the real superClass if the Custom Class
|
||||||
|
* extends another Custom Class, and so on until
|
||||||
|
* it reaches a real class, otherwise it will
|
||||||
|
* return the last fetched Custom Class
|
||||||
|
* @return Null<Dynamic>
|
||||||
|
*/
|
||||||
|
public function getSuperclass():IHScriptCustomClassBehaviour {
|
||||||
|
if(__superClass == null) return null;
|
||||||
|
|
||||||
|
var cls:Null<IHScriptCustomClassBehaviour> = __superClass;
|
||||||
|
|
||||||
|
// Check if the superClass is another custom class,
|
||||||
|
// so it will find for a real class, otherwise
|
||||||
|
// returns the last super CustomClass parent.
|
||||||
|
while (cls != null && cls is CustomClass) {
|
||||||
|
var next = cast(cls, CustomClass).__superClass;
|
||||||
|
if (next == null)
|
||||||
|
break; // Return the Custom Class itself
|
||||||
|
cls = next;
|
||||||
|
}
|
||||||
|
return cls is CustomClass ? this : cls;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: scripted safe cast for custom classes
|
||||||
|
public function getUpperclass():IHScriptCustomClassBehaviour {
|
||||||
|
if(__upperClass == null) return this;
|
||||||
|
|
||||||
|
var cls:CustomClass = cast __upperClass;
|
||||||
|
|
||||||
|
while (cls != null) {
|
||||||
|
var prev:CustomClass = cast cls.__upperClass;
|
||||||
|
if(prev == null)
|
||||||
|
break;
|
||||||
|
cls = prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cls;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: scriptable "toString" function
|
||||||
|
public function toString():String
|
||||||
|
return className;
|
||||||
|
}
|
||||||
+119
-172
@@ -1,151 +1,147 @@
|
|||||||
package hscript;
|
package hscript;
|
||||||
|
|
||||||
import hscript.Interp.DeclaredVar;
|
/**
|
||||||
import hscript.utils.UnsafeReflect;
|
* Provides handlers for static custom class fields and instantiation.
|
||||||
using StringTools;
|
*/
|
||||||
|
@:access(hscript.Property)
|
||||||
class CustomClassHandler implements IHScriptCustomConstructor {
|
class CustomClassHandler implements IHScriptCustomConstructor implements IHScriptCustomAccessBehaviour{
|
||||||
public static var staticHandler = new StaticHandler();
|
|
||||||
|
|
||||||
public var ogInterp:Interp;
|
public var ogInterp:Interp;
|
||||||
public var name:String;
|
public var name:String;
|
||||||
public var fields:Array<Expr>;
|
public var fields:Array<Expr>;
|
||||||
public var extend:String;
|
public var extend:Null<String>;
|
||||||
public var interfaces:Array<String>;
|
public var interfaces:Array<String>;
|
||||||
|
public final isFinal:Bool;
|
||||||
|
|
||||||
public var cl:Class<Dynamic>;
|
public var cl:Dynamic;
|
||||||
|
|
||||||
public function new(ogInterp:Interp, name:String, fields:Array<Expr>, ?extend:String, ?interfaces:Array<String>) {
|
private var __interp:Interp;
|
||||||
|
private var __staticFields:Array<String> = [];
|
||||||
|
|
||||||
|
public var __allowSetGet:Bool = true;
|
||||||
|
|
||||||
|
public function new(ogInterp:Interp, name:String, fields:Array<Expr>, ?extend:String, ?interfaces:Array<String>, ?isFinal:Bool) {
|
||||||
this.ogInterp = ogInterp;
|
this.ogInterp = ogInterp;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.fields = fields;
|
this.fields = fields;
|
||||||
this.extend = extend;
|
this.extend = extend;
|
||||||
this.interfaces = interfaces;
|
this.interfaces = interfaces;
|
||||||
|
this.isFinal = isFinal != null ? isFinal : false;
|
||||||
|
|
||||||
this.cl = extend == null ? CustomTemplateClass : Type.resolveClass('${extend}_HSX');
|
if(extend != null) {
|
||||||
if(cl == null)
|
if(ogInterp.customClasses.exists(extend)) {
|
||||||
ogInterp.error(EInvalidClass(extend));
|
var customCls:CustomClassHandler = ogInterp.customClasses.get(extend);
|
||||||
|
if(customCls.isFinal)
|
||||||
|
ogInterp.error(ECustom('Cannot extend a final class'));
|
||||||
|
this.cl = customCls;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
this.cl = Type.resolveClass('${extend}_HSX');
|
||||||
|
|
||||||
|
if(cl == null)
|
||||||
|
ogInterp.error(EInvalidClass(extend));
|
||||||
|
}
|
||||||
|
|
||||||
|
initStatic();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hnew(args:Array<Dynamic>):Dynamic {
|
@:access(hscript.Interp)
|
||||||
// TODO: clean this up, it sucks, i hate it
|
function initStatic() {
|
||||||
// TODO: make static vars work correctly
|
__interp = new Interp();
|
||||||
var interp = new Interp();
|
__interp.errorHandler = ogInterp.errorHandler;
|
||||||
|
__interp.importFailedCallback = ogInterp.importFailedCallback;
|
||||||
|
|
||||||
interp.errorHandler = ogInterp.errorHandler;
|
//__interp.variables = ogInterp.variables;
|
||||||
|
__interp.usingHandler.usingEntries = ogInterp.usingHandler.usingEntries;
|
||||||
|
__interp.publicVariables = ogInterp.publicVariables;
|
||||||
|
__interp.staticVariables = ogInterp.staticVariables;
|
||||||
|
__interp.customClasses = ogInterp.customClasses;
|
||||||
|
|
||||||
var _class:IHScriptCustomClassBehaviour = Type.createInstance(cl, args);
|
for(f => v in ogInterp.variables)
|
||||||
|
if(!__interp.variables.exists(f))
|
||||||
|
__interp.variables.set(f, v);
|
||||||
|
|
||||||
//var __capturedLocals = ogInterp.duplicate(ogInterp.locals);
|
for(i => e in fields.copy()) {
|
||||||
//var capturedLocals:Map<String, DeclaredVar> = [];
|
var isValid:Bool = false;
|
||||||
//for(k=>e in __capturedLocals)
|
var staticField:Bool = false;
|
||||||
// if (e != null && e.depth <= 0)
|
var fieldName:String = null;
|
||||||
// capturedLocals.set(k, e);
|
switch (Tools.expr(e)) {
|
||||||
|
case EVar(n, _, _, _, isStatic) | EFunction(_, _, n, _, _, isStatic):
|
||||||
var disallowCopy = Type.getInstanceFields(cl);
|
isValid = true;
|
||||||
|
staticField = isStatic;
|
||||||
_class.__real_fields = disallowCopy;
|
fieldName = n;
|
||||||
|
default:
|
||||||
// todo: make it so you can use variables from the same scope as where the class was defined
|
|
||||||
|
|
||||||
//for (key => value in capturedLocals) {
|
|
||||||
// if(!disallowCopy.contains(key)) {
|
|
||||||
// interp.locals.set(key, {r: value, depth: -1});
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
for (key => value in ogInterp.variables) {
|
|
||||||
if(!disallowCopy.contains(key)) {
|
|
||||||
interp.variables.set(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(key => value in ogInterp.customClasses) {
|
|
||||||
if(!disallowCopy.contains(key)) {
|
|
||||||
interp.customClasses.set(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// todo: clone static vars, but make it so setting it only sets it on the class
|
|
||||||
// todo: clone public vars
|
|
||||||
|
|
||||||
//trace("Before: " + [for(key => value in interp.variables) key]);
|
|
||||||
|
|
||||||
interp.variables.set("super", staticHandler);
|
|
||||||
|
|
||||||
var comparisonMap:Map<String, Dynamic> = [];
|
|
||||||
for(key => value in interp.variables) {
|
|
||||||
comparisonMap.set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
_class.__custom__variables = interp.variables;
|
|
||||||
|
|
||||||
//trace(fields);
|
|
||||||
for(expr in fields) {
|
|
||||||
@:privateAccess
|
|
||||||
interp.exprReturn(expr);
|
|
||||||
}
|
|
||||||
|
|
||||||
//trace("After: " + [for(key => value in interp.variables) key]);
|
|
||||||
|
|
||||||
// get only variables that were not set before
|
|
||||||
var classVariables = [
|
|
||||||
for(key => value in interp.variables)
|
|
||||||
if(!comparisonMap.exists(key) || comparisonMap[key] != value)
|
|
||||||
key => value
|
|
||||||
];
|
|
||||||
//for(variable => value in classVariables) {
|
|
||||||
// if(variable == "this" || variable == "super" || variable == "new") continue;
|
|
||||||
// @:privateAccess
|
|
||||||
// if(!interp.__instanceFields.contains(variable)) {
|
|
||||||
// interp.__instanceFields.push(variable);
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
_class.__class__fields = [for(key => value in classVariables) key];
|
|
||||||
|
|
||||||
//trace(_class.__class__fields);
|
|
||||||
//@:privateAccess
|
|
||||||
//trace(interp.__instanceFields);
|
|
||||||
|
|
||||||
_class.__interp = interp;
|
|
||||||
_class.__allowSetGet = false;
|
|
||||||
interp.scriptObject = _class;
|
|
||||||
|
|
||||||
for(variable => value in interp.variables) {
|
|
||||||
if(variable == "this" || variable == "super" || variable == "new") continue;
|
|
||||||
|
|
||||||
if(variable.startsWith("set_") || variable.startsWith("get_")) {
|
|
||||||
_class.__allowSetGet = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var newFunc = interp.variables.get("new");
|
|
||||||
if(newFunc != null) {
|
|
||||||
var comparisonMap:Map<String, Dynamic> = [];
|
|
||||||
for(key => value in interp.variables) {
|
|
||||||
comparisonMap.set(key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UnsafeReflect.callMethodUnsafe(null, newFunc, args);
|
if(staticField && isValid) {
|
||||||
|
__interp.exprReturn(e);
|
||||||
// get only variables that were not set before
|
__staticFields.push(fieldName);
|
||||||
var classVariables = [
|
fields.remove(e);
|
||||||
for(key => value in interp.variables)
|
|
||||||
if(!comparisonMap.exists(key) || comparisonMap[key] != value)
|
|
||||||
key => value
|
|
||||||
];
|
|
||||||
for(variable => value in classVariables) {
|
|
||||||
if(variable == "this" || variable == "super" || variable == "new") continue;
|
|
||||||
@:privateAccess
|
|
||||||
if(!interp.__instanceFields.contains(variable)) {
|
|
||||||
interp.__instanceFields.push(variable);
|
|
||||||
}
|
|
||||||
if(!_class.__class__fields.contains(variable)) {
|
|
||||||
_class.__class__fields.push(variable);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hnew(args:Array<Dynamic>):Dynamic
|
||||||
|
return new CustomClass(this, args);
|
||||||
|
|
||||||
return _class;
|
@:allow(hscript.Interp)
|
||||||
|
inline function hasField(name:String) {
|
||||||
|
return __staticFields.contains(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getField(name:String, allowProperty:Bool = true):Dynamic {
|
||||||
|
var f = __interp.variables.get(name);
|
||||||
|
if(f is Property && allowProperty) {
|
||||||
|
var prop:Property = cast f;
|
||||||
|
//prop.__allowSetGet = this.__allowSetGet;
|
||||||
|
var r = prop.get(!__allowSetGet);
|
||||||
|
//prop.__allowSetGet = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setField(name:String, val:Dynamic):Dynamic {
|
||||||
|
var f = getField(name, false);
|
||||||
|
if(f is Property) {
|
||||||
|
var prop:Property = cast f;
|
||||||
|
//prop.__allowSetGet = this.__allowSetGet;
|
||||||
|
var r = prop.set(val, !__allowSetGet);
|
||||||
|
//prop.__allowSetGet = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
__interp.variables.set(name, val);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hget(name:String):Dynamic {
|
||||||
|
if(name == 'new') {
|
||||||
|
return Reflect.makeVarArgs(function(args:Array<Dynamic>):Dynamic {
|
||||||
|
return inline this.hnew(args);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if(hasField(name)) {
|
||||||
|
return getField(name);
|
||||||
|
}
|
||||||
|
throw "field '"+ name+ "' does not exist in class '"+ this.name+ "'";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hset(name:String, val:Dynamic):Dynamic {
|
||||||
|
if(hasField(name))
|
||||||
|
return setField(name, val);
|
||||||
|
|
||||||
|
throw "field '"+ name+ "' does not exist in class '"+ this.name+ "'";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// UNUSED
|
||||||
|
public function __callGetter(name:String):Dynamic {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __callSetter(name:String, val:Dynamic):Dynamic {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toString():String {
|
public function toString():String {
|
||||||
@@ -153,51 +149,6 @@ class CustomClassHandler implements IHScriptCustomConstructor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CustomTemplateClass implements IHScriptCustomClassBehaviour implements IHScriptCustomAccessBehaviour {
|
|
||||||
public var __interp:Interp;
|
|
||||||
public var __allowSetGet:Bool = true;
|
|
||||||
public var __custom__variables:Map<String, Dynamic>;
|
|
||||||
public var __real_fields:Array<String>;
|
|
||||||
public var __class__fields:Array<String>;
|
|
||||||
|
|
||||||
public function hset(name:String, val:Dynamic):Dynamic {
|
|
||||||
if(__allowSetGet && __custom__variables.exists("set_" + name))
|
|
||||||
return __callSetter(name, val);
|
|
||||||
if (__custom__variables.exists(name)) {
|
|
||||||
__custom__variables.set(name, val);
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
if(__real_fields.contains(name)) {
|
|
||||||
UnsafeReflect.setProperty(this, name, val);
|
|
||||||
return UnsafeReflect.field(this, name);
|
|
||||||
}
|
|
||||||
__custom__variables.set(name, val);
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
public function hget(name:String):Dynamic {
|
|
||||||
if(__allowSetGet && __custom__variables.exists("get_" + name))
|
|
||||||
return __callGetter(name);
|
|
||||||
if (__custom__variables.exists(name))
|
|
||||||
return __custom__variables.get(name);
|
|
||||||
|
|
||||||
return UnsafeReflect.getProperty(this, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __callGetter(name:String):Dynamic {
|
|
||||||
__allowSetGet = false;
|
|
||||||
var v = __custom__variables.get("get_" + name)();
|
|
||||||
__allowSetGet = true;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __callSetter(name:String, val:Dynamic):Dynamic {
|
|
||||||
__allowSetGet = false;
|
|
||||||
var v = __custom__variables.get("set_" + name)(val);
|
|
||||||
__allowSetGet = true;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is for backwards compatibility with old hscript-improved, since some scripts use it
|
* This is for backwards compatibility with old hscript-improved, since some scripts use it
|
||||||
@@ -236,7 +187,3 @@ class TemplateClass implements IHScriptCustomBehaviour implements IHScriptCustom
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final class StaticHandler {
|
|
||||||
public function new() {}
|
|
||||||
}
|
|
||||||
+403
-218
@@ -1,218 +1,403 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C)2008-2017 Haxe Foundation
|
* Copyright (C)2008-2017 Haxe Foundation
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
* to deal in the Software without restriction, including without limitation
|
* to deal in the Software without restriction, including without limitation
|
||||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
* and/or sell copies of the Software, and to permit persons to whom the
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
* Software is furnished to do so, subject to the following conditions:
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
* DEALINGS IN THE SOFTWARE.
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package hscript;
|
package hscript;
|
||||||
|
|
||||||
typedef Int8 = #if cpp cpp.Int8 #elseif java java.Int8 #elseif cs cs.Int8 #else Int #end;
|
typedef Int8 = #if cpp cpp.Int8 #elseif java java.Int8 #elseif cs cs.Int8 #else Int #end;
|
||||||
typedef Int16 = #if cpp cpp.Int16 #elseif java java.Int16 #elseif cs cs.Int16 #else Int #end;
|
typedef Int16 = #if cpp cpp.Int16 #elseif java java.Int16 #elseif cs cs.Int16 #else Int #end;
|
||||||
typedef Int32 = #if cpp cpp.Int32 #else Int #end;
|
typedef Int32 = #if cpp cpp.Int32 #else Int #end;
|
||||||
typedef Int64 = #if cpp cpp.Int64 #elseif java java.Int64 #elseif cs cs.Int64 #else Int #end;
|
typedef Int64 = #if cpp cpp.Int64 #elseif java java.Int64 #elseif cs cs.Int64 #else Int #end;
|
||||||
|
|
||||||
typedef UInt8 = #if cpp cpp.UInt8 #elseif cs cs.UInt8 #else Int #end;
|
typedef UInt8 = #if cpp cpp.UInt8 #elseif cs cs.UInt8 #else Int #end;
|
||||||
typedef UInt16 = #if cpp cpp.UInt16 #elseif cs cs.UInt16 #else Int #end;
|
typedef UInt16 = #if cpp cpp.UInt16 #elseif cs cs.UInt16 #else Int #end;
|
||||||
typedef UInt32 = #if cpp cpp.UInt32 #else Int #end;
|
|
||||||
typedef UInt64 = #if cpp cpp.UInt64 #else Int #end;
|
enum abstract Binop(Int) from Int to Int {
|
||||||
|
var OpAdd = 0;
|
||||||
enum Const {
|
var OpSub = 1;
|
||||||
CInt( v : Int );
|
var OpMult = 2;
|
||||||
CFloat( f : Float );
|
var OpDiv = 3;
|
||||||
CString( s : String );
|
var OpMod = 4;
|
||||||
}
|
var OpAnd = 5;
|
||||||
|
var OpOr = 6;
|
||||||
#if hscriptPos
|
var OpXor = 7;
|
||||||
@:structInit
|
var OpShl = 8;
|
||||||
final class Expr {
|
var OpShr = 9;
|
||||||
public var e : ExprDef;
|
var OpUshr = 10;
|
||||||
public var pmin : Int;
|
var OpEq = 11;
|
||||||
public var pmax : Int;
|
var OpNeq = 12;
|
||||||
public var origin : String;
|
var OpGte = 13;
|
||||||
public var line : Int;
|
var OpLte = 14;
|
||||||
}
|
var OpGt = 15;
|
||||||
enum ExprDef {
|
var OpLt = 16;
|
||||||
#else
|
var OpBoolOr = 17;
|
||||||
typedef ExprDef = Expr;
|
var OpBoolAnd = 18;
|
||||||
enum Expr {
|
var OpIs = 19;
|
||||||
#end
|
var OpAssign = 20;
|
||||||
EConst( c : Const );
|
var OpNcoal = 21;
|
||||||
EIdent( v : String );
|
var OpInterval = 22;
|
||||||
EVar( n : String, ?t : CType, ?e : Expr, ?isPublic : Bool, ?isStatic : Bool, ?isPrivate : Bool, ?isFinal : Bool, ?isInline : Bool );
|
var OpArrow = 23;
|
||||||
EParent( e : Expr );
|
var OpAddAssign = 24;
|
||||||
EBlock( e : Array<Expr> );
|
var OpSubAssign = 25;
|
||||||
EField( e : Expr, f : String , ?safe : Bool );
|
var OpMultAssign = 26;
|
||||||
EBinop( op : String, e1 : Expr, e2 : Expr );
|
var OpDivAssign = 27;
|
||||||
EUnop( op : String, prefix : Bool, e : Expr );
|
var OpModAssign = 28;
|
||||||
ECall( e : Expr, params : Array<Expr> );
|
var OpAndAssign = 29;
|
||||||
EIf( cond : Expr, e1 : Expr, ?e2 : Expr );
|
var OpOrAssign = 30;
|
||||||
EWhile( cond : Expr, e : Expr );
|
var OpXorAssign = 31;
|
||||||
EFor( v : String, it : Expr, e : Expr, ?ithv: String);
|
var OpShlAssign = 32;
|
||||||
EBreak;
|
var OpShrAssign = 33;
|
||||||
EContinue;
|
var OpUshrAssign = 34;
|
||||||
EFunction( args : Array<Argument>, e : Expr, ?name : String, ?ret : CType, ?isPublic : Bool, ?isStatic : Bool, ?isOverride : Bool, ?isPrivate : Bool, ?isFinal : Bool, ?isInline : Bool );
|
var OpNcoalAssign = 35;
|
||||||
EReturn( ?e : Expr );
|
var OpArrowFn = 36;
|
||||||
EArray( e : Expr, index : Expr );
|
|
||||||
EArrayDecl( e : Array<Expr>, ?wantedType: CType );
|
public static inline function fromString(s:String):Binop {
|
||||||
ENew( cl : String, params : Array<Expr> );
|
return switch(s) {
|
||||||
EThrow( e : Expr );
|
case "+": OpAdd;
|
||||||
ETry( e : Expr, v : String, t : Null<CType>, ecatch : Expr );
|
case "-": OpSub;
|
||||||
EObject( fl : Array<ObjectField> );
|
case "*": OpMult;
|
||||||
ETernary( cond : Expr, e1 : Expr, e2 : Expr );
|
case "/": OpDiv;
|
||||||
ESwitch( e : Expr, cases : Array<SwitchCase>, ?defaultExpr : Expr );
|
case "%": OpMod;
|
||||||
EDoWhile( cond : Expr, e : Expr);
|
case "&": OpAnd;
|
||||||
EMeta( name : String, args : Array<Expr>, e : Expr );
|
case "|": OpOr;
|
||||||
ECheckType( e : Expr, t : CType );
|
case "^": OpXor;
|
||||||
|
case "<<": OpShl;
|
||||||
EImport( c : String, ?asname:String );
|
case ">>": OpShr;
|
||||||
EClass( name:String, fields:Array<Expr>, ?extend:String, interfaces:Array<String>, ?isFinal:Bool, ?isPrivate:Bool );
|
case ">>>": OpUshr;
|
||||||
}
|
case "==": OpEq;
|
||||||
|
case "!=": OpNeq;
|
||||||
@:structInit
|
case ">=": OpGte;
|
||||||
final class ObjectField {
|
case "<=": OpLte;
|
||||||
public var name : String;
|
case ">": OpGt;
|
||||||
public var e : Expr;
|
case "<": OpLt;
|
||||||
}
|
case "||": OpBoolOr;
|
||||||
|
case "&&": OpBoolAnd;
|
||||||
@:structInit
|
case "is": OpIs;
|
||||||
final class SwitchCase {
|
case "=": OpAssign;
|
||||||
public var values : Array<Expr>;
|
case "??": OpNcoal;
|
||||||
public var expr : Expr;
|
case "...": OpInterval;
|
||||||
}
|
case "->": OpArrow;
|
||||||
|
case "=>": OpArrowFn;
|
||||||
@:structInit
|
case "+=": OpAddAssign;
|
||||||
final class Argument {
|
case "-=": OpSubAssign;
|
||||||
public var name : String;
|
case "*=": OpMultAssign;
|
||||||
public var t : CType;
|
case "/=": OpDivAssign;
|
||||||
public var opt : Bool;
|
case "%=": OpModAssign;
|
||||||
public var value : Expr;
|
case "&=": OpAndAssign;
|
||||||
}
|
case "|=": OpOrAssign;
|
||||||
|
case "^=": OpXorAssign;
|
||||||
@:structInit
|
case "<<=": OpShlAssign;
|
||||||
final class MetadataEntry {
|
case ">>=": OpShrAssign;
|
||||||
public var name : String;
|
case ">>>=": OpUshrAssign;
|
||||||
public var params : Array<Expr>;
|
case _ if (s == "??" + "="): OpNcoalAssign;
|
||||||
}
|
default: -1;
|
||||||
|
}
|
||||||
typedef Metadata = Array<MetadataEntry>;
|
}
|
||||||
|
|
||||||
enum CType {
|
public inline function toString():String {
|
||||||
CTPath( path : Array<String>, ?params : Array<CType> );
|
return switch(this) {
|
||||||
CTFun( args : Array<CType>, ret : CType );
|
case OpAdd: "+";
|
||||||
CTAnon( fields : Array<{ name : String, t : CType, ?meta : Metadata }> );
|
case OpSub: "-";
|
||||||
CTParent( t : CType );
|
case OpMult: "*";
|
||||||
CTOpt( t : CType );
|
case OpDiv: "/";
|
||||||
CTNamed( n : String, t : CType );
|
case OpMod: "%";
|
||||||
}
|
case OpAnd: "&";
|
||||||
|
case OpOr: "|";
|
||||||
#if hscriptPos
|
case OpXor: "^";
|
||||||
class Error {
|
case OpShl: "<<";
|
||||||
public var e : ErrorDef;
|
case OpShr: ">>";
|
||||||
public var pmin : Int;
|
case OpUshr: ">>>";
|
||||||
public var pmax : Int;
|
case OpEq: "==";
|
||||||
public var origin : String;
|
case OpNeq: "!=";
|
||||||
public var line : Int;
|
case OpGte: ">=";
|
||||||
public function new(e, pmin, pmax, origin, line) {
|
case OpLte: "<=";
|
||||||
this.e = e;
|
case OpGt: ">";
|
||||||
this.pmin = pmin;
|
case OpLt: "<";
|
||||||
this.pmax = pmax;
|
case OpBoolOr: "||";
|
||||||
this.origin = origin;
|
case OpBoolAnd: "&&";
|
||||||
this.line = line;
|
case OpIs: "is";
|
||||||
}
|
case OpAssign: "=";
|
||||||
public function toString(): String {
|
case OpNcoal: "??";
|
||||||
return Printer.errorToString(this);
|
case OpInterval: "...";
|
||||||
}
|
case OpArrow: "->";
|
||||||
}
|
case OpArrowFn: "=>";
|
||||||
enum ErrorDef {
|
case OpAddAssign: "+=";
|
||||||
#else
|
case OpSubAssign: "-=";
|
||||||
enum Error {
|
case OpMultAssign: "*=";
|
||||||
#end
|
case OpDivAssign: "/=";
|
||||||
EInvalidChar( c : Int );
|
case OpModAssign: "%=";
|
||||||
EUnexpected( s : String );
|
case OpAndAssign: "&=";
|
||||||
EUnterminatedString;
|
case OpOrAssign: "|=";
|
||||||
EUnterminatedComment;
|
case OpXorAssign: "^=";
|
||||||
EInvalidPreprocessor( msg : String );
|
case OpShlAssign: "<<=";
|
||||||
EUnknownVariable( v : String );
|
case OpShrAssign: ">>=";
|
||||||
EInvalidIterator( v : String );
|
case OpUshrAssign: ">>>=";
|
||||||
EInvalidOp( op : String );
|
case OpNcoalAssign: "??" + "=";
|
||||||
EInvalidAccess( f : String );
|
default: "?";
|
||||||
ECustom( msg : String );
|
}
|
||||||
EInvalidClass( className : String);
|
}
|
||||||
EAlreadyExistingClass( className : String);
|
}
|
||||||
}
|
|
||||||
|
enum abstract Unop(Int) from Int to Int {
|
||||||
|
var OpNot = 0;
|
||||||
enum ModuleDecl {
|
var OpNeg = 1;
|
||||||
DPackage( path : Array<String> );
|
var OpIncrement = 2;
|
||||||
DImport( path : Array<String>, ?everything : Bool );
|
var OpDecrement = 3;
|
||||||
DClass( c : ClassDecl );
|
var OpNegBits = 4;
|
||||||
DTypedef( c : TypeDecl );
|
|
||||||
}
|
public static inline function fromString(s:String):Unop {
|
||||||
|
return switch(s) {
|
||||||
typedef ModuleType = {
|
case "!": OpNot;
|
||||||
var name : String;
|
case "-": OpNeg;
|
||||||
var params : {}; // TODO : not yet parsed
|
case "++": OpIncrement;
|
||||||
var meta : Metadata;
|
case "--": OpDecrement;
|
||||||
var isPrivate : Bool;
|
case "~": OpNegBits;
|
||||||
}
|
default: -1;
|
||||||
|
}
|
||||||
typedef ClassDecl = {> ModuleType,
|
}
|
||||||
var extend : Null<CType>;
|
|
||||||
var implement : Array<CType>;
|
public inline function toString():String {
|
||||||
var fields : Array<FieldDecl>;
|
return switch(this) {
|
||||||
var isExtern : Bool;
|
case OpNot: "!";
|
||||||
}
|
case OpNeg: "-";
|
||||||
|
case OpIncrement: "++";
|
||||||
typedef TypeDecl = {> ModuleType,
|
case OpDecrement: "--";
|
||||||
var t : CType;
|
case OpNegBits: "~";
|
||||||
}
|
default: "?";
|
||||||
|
}
|
||||||
typedef FieldDecl = {
|
}
|
||||||
var name : String;
|
}
|
||||||
var meta : Metadata;
|
|
||||||
var kind : FieldKind;
|
typedef UInt32 = #if cpp cpp.UInt32 #else Int #end;
|
||||||
var access : Array<FieldAccess>;
|
typedef UInt64 = #if cpp cpp.UInt64 #else Int #end;
|
||||||
}
|
|
||||||
|
enum Const {
|
||||||
enum abstract FieldAccess(UInt8) {
|
CInt( v : Int );
|
||||||
var APublic;
|
CFloat( f : Float );
|
||||||
var APrivate;
|
CString( s : String, ?i : Bool );
|
||||||
var AInline;
|
}
|
||||||
var AOverride;
|
|
||||||
var AStatic;
|
#if hscriptPos
|
||||||
var AMacro;
|
@:structInit
|
||||||
}
|
final class Expr {
|
||||||
|
public var e : ExprDef;
|
||||||
enum FieldKind {
|
public var pmin : Int;
|
||||||
KFunction( f : FunctionDecl );
|
public var pmax : Int;
|
||||||
KVar( v : VarDecl );
|
public var origin : String;
|
||||||
}
|
public var line : Int;
|
||||||
|
}
|
||||||
@:structInit
|
enum ExprDef {
|
||||||
final class FunctionDecl {
|
#else
|
||||||
public var args : Array<Argument>;
|
typedef ExprDef = Expr;
|
||||||
public var body : Expr;
|
enum Expr {
|
||||||
public var ret : Null<CType>;
|
#end
|
||||||
}
|
EConst( c : Const );
|
||||||
|
EIdent( v : String );
|
||||||
typedef VarDecl = {
|
EVar( n : String, ?t : CType, ?e : Expr, ?isPublic : Bool, ?isStatic : Bool, ?isPrivate : Bool, ?isFinal : Bool, ?isInline : Bool, ?get : FieldPropertyAccess, ?set : FieldPropertyAccess, ?isVar:Bool );
|
||||||
var get : Null<String>;
|
EParent( e : Expr );
|
||||||
var set : Null<String>;
|
EBlock( e : Array<Expr> );
|
||||||
var expr : Null<Expr>;
|
EField( e : Expr, f : String , ?safe : Bool );
|
||||||
var type : Null<CType>;
|
EBinop( op : Binop, e1 : Expr, e2 : Expr );
|
||||||
}
|
EUnop( op : Unop, prefix : Bool, e : Expr );
|
||||||
|
ECall( e : Expr, params : Array<Expr> );
|
||||||
|
EIf( cond : Expr, e1 : Expr, ?e2 : Expr );
|
||||||
|
EWhile( cond : Expr, e : Expr );
|
||||||
|
EFor( v : String, it : Expr, e : Expr, ?ithv: String);
|
||||||
|
EBreak;
|
||||||
|
EContinue;
|
||||||
|
EFunction( args : Array<Argument>, e : Expr, ?name : String, ?ret : CType, ?isPublic : Bool, ?isStatic : Bool, ?isOverride : Bool, ?isPrivate : Bool, ?isFinal : Bool, ?isInline : Bool );
|
||||||
|
EReturn( ?e : Expr );
|
||||||
|
EArray( e : Expr, index : Expr );
|
||||||
|
EArrayDecl( e : Array<Expr>, ?wantedType: CType );
|
||||||
|
ENew( cl : String, params : Array<Expr>, ?paramType:Array<CType> );
|
||||||
|
EThrow( e : Expr );
|
||||||
|
ETry( e : Expr, v : String, t : Null<CType>, ecatch : Expr );
|
||||||
|
EObject( fl : Array<ObjectField> );
|
||||||
|
ETernary( cond : Expr, e1 : Expr, e2 : Expr );
|
||||||
|
ESwitch( e : Expr, cases : Array<SwitchCase>, ?defaultExpr : Expr );
|
||||||
|
EDoWhile( cond : Expr, e : Expr);
|
||||||
|
EMeta( name : String, args : Array<Expr>, e : Expr );
|
||||||
|
ECheckType( e : Expr, t : CType );
|
||||||
|
|
||||||
|
EPackage( ?n:String );
|
||||||
|
EImport( c : String, ?asname:String, ?isUsing:Bool );
|
||||||
|
EClass( name:String, fields:Array<Expr>, ?extend:String, interfaces:Array<String>, ?isFinal:Bool, ?isPrivate:Bool );
|
||||||
|
EEnum( en:EnumDecl, ?isAbstract:Bool );
|
||||||
|
ECast(e:Expr, ?t:CType);
|
||||||
|
ERegex(e:String, flags:String);
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class ObjectField {
|
||||||
|
public var name : String;
|
||||||
|
public var e : Expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class SwitchCase {
|
||||||
|
public var values : Array<Expr>;
|
||||||
|
public var expr : Expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class Argument {
|
||||||
|
public var name : String;
|
||||||
|
public var t : CType;
|
||||||
|
public var opt : Bool;
|
||||||
|
public var value : Expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class MetadataEntry {
|
||||||
|
public var name : String;
|
||||||
|
public var params : Array<Expr>;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef Metadata = Array<MetadataEntry>;
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class EnumDecl {
|
||||||
|
public var name : String;
|
||||||
|
public var fields : Array<EnumField>;
|
||||||
|
public var underlyingType : Null<CType>;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class EnumField {
|
||||||
|
public var name : String;
|
||||||
|
public var args : Array<Argument>;
|
||||||
|
public var value : Null<Expr>;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum CType {
|
||||||
|
CTPath( path : Array<String>, ?params : Array<CType> );
|
||||||
|
CTFun( args : Array<CType>, ret : CType );
|
||||||
|
CTAnon( fields : Array<{ name : String, t : CType, ?meta : Metadata }> );
|
||||||
|
CTParent( t : CType );
|
||||||
|
CTOpt( t : CType );
|
||||||
|
CTNamed( n : String, t : CType );
|
||||||
|
CTExpr( e : Expr ); // for type parameters only
|
||||||
|
}
|
||||||
|
|
||||||
|
#if hscriptPos
|
||||||
|
class Error {
|
||||||
|
public var e : ErrorDef;
|
||||||
|
public var pmin : Int;
|
||||||
|
public var pmax : Int;
|
||||||
|
public var origin : String;
|
||||||
|
public var line : Int;
|
||||||
|
public function new(e, pmin, pmax, origin, line) {
|
||||||
|
this.e = e;
|
||||||
|
this.pmin = pmin;
|
||||||
|
this.pmax = pmax;
|
||||||
|
this.origin = origin;
|
||||||
|
this.line = line;
|
||||||
|
}
|
||||||
|
public function toString(): String {
|
||||||
|
return Printer.errorToString(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enum ErrorDef {
|
||||||
|
#else
|
||||||
|
enum Error {
|
||||||
|
#end
|
||||||
|
EInvalidChar( c : Int );
|
||||||
|
EUnexpected( s : String );
|
||||||
|
EUnterminatedString;
|
||||||
|
EUnterminatedComment;
|
||||||
|
EInvalidPreprocessor( msg : String );
|
||||||
|
EUnknownVariable( v : String );
|
||||||
|
EInvalidIterator( v : String );
|
||||||
|
EInvalidOp( op : String );
|
||||||
|
EInvalidAccess( f : String );
|
||||||
|
ECustom( msg : String );
|
||||||
|
EInvalidClass( className : String);
|
||||||
|
EAlreadyExistingClass( className : String);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum ModuleDecl {
|
||||||
|
DPackage( path : Array<String> );
|
||||||
|
DImport( path : Array<String>, ?everything : Bool );
|
||||||
|
DClass( c : ClassDecl );
|
||||||
|
DTypedef( c : TypeDecl );
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef ModuleType = {
|
||||||
|
var name : String;
|
||||||
|
var params : {}; // TODO : not yet parsed
|
||||||
|
var meta : Metadata;
|
||||||
|
var isPrivate : Bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef ClassDecl = {> ModuleType,
|
||||||
|
var extend : Null<CType>;
|
||||||
|
var implement : Array<CType>;
|
||||||
|
var fields : Array<FieldDecl>;
|
||||||
|
var isExtern : Bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef TypeDecl = {> ModuleType,
|
||||||
|
var t : CType;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef FieldDecl = {
|
||||||
|
var name : String;
|
||||||
|
var meta : Metadata;
|
||||||
|
var kind : FieldKind;
|
||||||
|
var access : Array<FieldAccess>;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum abstract FieldAccess(UInt8) {
|
||||||
|
var APublic;
|
||||||
|
var APrivate;
|
||||||
|
var AInline;
|
||||||
|
var AOverride;
|
||||||
|
var AStatic;
|
||||||
|
var AMacro;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum abstract FieldPropertyAccess(UInt8) {
|
||||||
|
var ADefault;
|
||||||
|
var ANull;
|
||||||
|
var AGet;
|
||||||
|
var ASet;
|
||||||
|
var ADynamic;
|
||||||
|
var ANever;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum FieldKind {
|
||||||
|
KFunction( f : FunctionDecl );
|
||||||
|
KVar( v : VarDecl );
|
||||||
|
}
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
final class FunctionDecl {
|
||||||
|
public var args : Array<Argument>;
|
||||||
|
public var body : Expr;
|
||||||
|
public var ret : Null<CType>;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef VarDecl = {
|
||||||
|
var get : Null<String>;
|
||||||
|
var set : Null<String>;
|
||||||
|
var expr : Null<Expr>;
|
||||||
|
var type : Null<CType>;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,182 @@
|
|||||||
|
package hscript;
|
||||||
|
|
||||||
|
import hscript.utils.UnsafeReflect;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper class for enums, both for real and scripted.
|
||||||
|
* Use EnumTools and EnumValueTools for enum operations.
|
||||||
|
*/
|
||||||
|
@:structInit
|
||||||
|
class HEnum implements IHScriptCustomBehaviour {
|
||||||
|
var enumValues(default, null) = {};
|
||||||
|
|
||||||
|
public function setEnum(name:String, enumValue:Dynamic):Void {
|
||||||
|
UnsafeReflect.setField(enumValues, name, enumValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEnum(name:String):Null<Dynamic> {
|
||||||
|
if (UnsafeReflect.hasField(enumValues, name))
|
||||||
|
return UnsafeReflect.field(enumValues, name);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getEnumValues():Dynamic {
|
||||||
|
return enumValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hget(name:String):Dynamic {
|
||||||
|
return getEnum(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hset(name:String, val:Dynamic):Dynamic {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@:nullSafety
|
||||||
|
@:structInit
|
||||||
|
class HEnumValue {
|
||||||
|
public var enumName:String;
|
||||||
|
public var fieldName:String;
|
||||||
|
public var index:Int;
|
||||||
|
public var args:Array<Dynamic>;
|
||||||
|
|
||||||
|
public function toString():String {
|
||||||
|
return '$enumName.$fieldName${args.length > 0 ? '(${[for (a in args) a].join(", ")})' : ''}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public inline function getEnumName():String
|
||||||
|
return this.enumName;
|
||||||
|
|
||||||
|
public inline function getConstructorArgs():Array<Dynamic>
|
||||||
|
return this.args;
|
||||||
|
|
||||||
|
public function compare(other:HEnumValue):Bool {
|
||||||
|
if (enumName != other.enumName || fieldName != other.fieldName)
|
||||||
|
return false;
|
||||||
|
if (args.length == 0 && other.args.length == 0)
|
||||||
|
return true;
|
||||||
|
if (args.length == 0 || other.args.length == 0)
|
||||||
|
return false;
|
||||||
|
if (args.length != other.args.length)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (i in 0...args.length)
|
||||||
|
if (args[i] != other.args[i])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@:nullSafety
|
||||||
|
class EnumTools {
|
||||||
|
public static function getConstructors(e:Dynamic):Array<String> {
|
||||||
|
if(Std.isOfType(e, HEnum)) {
|
||||||
|
var henum:HEnum = cast e;
|
||||||
|
return UnsafeReflect.fields(henum.getEnumValues());
|
||||||
|
}
|
||||||
|
return Type.getEnumConstructs(cast e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function createByName(e:Dynamic, constr:String, ?params:Array<Dynamic>):Dynamic {
|
||||||
|
if(Std.isOfType(e, HEnum)) {
|
||||||
|
var henum:HEnum = cast e;
|
||||||
|
var constructor = henum.getEnum(constr);
|
||||||
|
if(constructor == null)
|
||||||
|
throw 'Constructor $constr not found in enum';
|
||||||
|
if(Std.isOfType(constructor, HEnumValue))
|
||||||
|
return constructor;
|
||||||
|
if(Reflect.isFunction(constructor))
|
||||||
|
return constructor(params == null ? [] : params);
|
||||||
|
throw 'Invalid constructor type';
|
||||||
|
}
|
||||||
|
return Type.createEnum(cast e, constr, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function createByIndex(e:Dynamic, index:Int, ?params:Array<Dynamic>):Dynamic {
|
||||||
|
if(Std.isOfType(e, HEnum)) {
|
||||||
|
var constructors = getConstructors(e);
|
||||||
|
if(index < 0 || index >= constructors.length)
|
||||||
|
throw 'Index $index out of bounds for enum';
|
||||||
|
return createByName(e, constructors[index], params);
|
||||||
|
}
|
||||||
|
return Type.createEnumIndex(cast e, index, params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@:nullSafety
|
||||||
|
class EnumValueTools {
|
||||||
|
public static function getType(e:Dynamic):Null<String> {
|
||||||
|
if(Std.isOfType(e, HEnumValue)) {
|
||||||
|
var hv:HEnumValue = cast e;
|
||||||
|
return hv.enumName;
|
||||||
|
}
|
||||||
|
var en = Type.getEnum(e);
|
||||||
|
if(en != null)
|
||||||
|
return Type.getEnumName(en);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getName(e:Dynamic):String {
|
||||||
|
if(Std.isOfType(e, HEnumValue)) {
|
||||||
|
var hv:HEnumValue = cast e;
|
||||||
|
return hv.fieldName;
|
||||||
|
}
|
||||||
|
return Type.enumConstructor(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getParameters(e:Dynamic):Array<Dynamic> {
|
||||||
|
if(Std.isOfType(e, HEnumValue)) {
|
||||||
|
var hv:HEnumValue = cast e;
|
||||||
|
return hv.args.copy();
|
||||||
|
}
|
||||||
|
return Type.enumParameters(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getIndex(e:Dynamic):Int {
|
||||||
|
if(Std.isOfType(e, HEnumValue)) {
|
||||||
|
var hv:HEnumValue = cast e;
|
||||||
|
return hv.index;
|
||||||
|
}
|
||||||
|
return Type.enumIndex(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function equals(a:Dynamic, b:Dynamic):Bool {
|
||||||
|
if(Std.isOfType(a, HEnumValue) && Std.isOfType(b, HEnumValue)) {
|
||||||
|
var hva:HEnumValue = cast a;
|
||||||
|
var hvb:HEnumValue = cast b;
|
||||||
|
return hva.compare(hvb);
|
||||||
|
}
|
||||||
|
return Type.enumEq(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function match(e:Dynamic, pattern:Dynamic):Bool {
|
||||||
|
if(Std.isOfType(e, HEnumValue)) {
|
||||||
|
var hv:HEnumValue = cast e;
|
||||||
|
if(Std.isOfType(pattern, HEnumValue)) {
|
||||||
|
var hp:HEnumValue = cast pattern;
|
||||||
|
if(hv.enumName != hp.enumName || hv.fieldName != hp.fieldName)
|
||||||
|
return false;
|
||||||
|
if(hp.args.length == 0)
|
||||||
|
return true;
|
||||||
|
if(hv.args.length != hp.args.length)
|
||||||
|
return false;
|
||||||
|
for(i in 0...hp.args.length) {
|
||||||
|
var pa = hp.args[i];
|
||||||
|
if(pa != null && hv.args[i] != pa)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(Reflect.isObject(pattern)) {
|
||||||
|
var patternName = UnsafeReflect.hasField(pattern, "name") ? UnsafeReflect.field(pattern, "name") : null;
|
||||||
|
if(patternName != null && patternName != hv.fieldName)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Type.enumEq(e, pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package hscript;
|
||||||
|
|
||||||
|
// Soon...
|
||||||
|
interface IHScriptAbstractBehaviour extends IHScriptCustomBehaviour {
|
||||||
|
public var hasOp:Bool;
|
||||||
|
public var hasArr:Bool;
|
||||||
|
// @:op(A * B), @:op(A++), etc...
|
||||||
|
public function hop(kind:String, a:Dynamic, ?b:Dynamic):Dynamic;
|
||||||
|
|
||||||
|
public function harrayget(key:Dynamic):Dynamic;
|
||||||
|
public function harrayset(key:Dynamic, val:Dynamic):Dynamic;
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package hscript;
|
package hscript;
|
||||||
|
|
||||||
interface IHScriptCustomAccessBehaviour {
|
/**
|
||||||
|
* Same Interface as IHScriptCustomBehaviour but for Property.
|
||||||
|
*/
|
||||||
|
interface IHScriptCustomAccessBehaviour extends IHScriptCustomBehaviour {
|
||||||
var __allowSetGet:Bool;
|
var __allowSetGet:Bool;
|
||||||
|
|
||||||
public function hset(name:String, val:Dynamic):Dynamic;
|
|
||||||
public function hget(name:String):Dynamic;
|
|
||||||
|
|
||||||
public function __callGetter(name:String):Dynamic;
|
public function __callGetter(name:String):Dynamic;
|
||||||
public function __callSetter(name:String, val:Dynamic):Dynamic;
|
public function __callSetter(name:String, val:Dynamic):Dynamic;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,23 @@
|
|||||||
package hscript;
|
package hscript;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special Interface for handling field access behaviour.
|
||||||
|
* Basically works like the operator overload `@:op(a.b)`
|
||||||
|
* for an abstract.
|
||||||
|
*/
|
||||||
interface IHScriptCustomBehaviour {
|
interface IHScriptCustomBehaviour {
|
||||||
|
/**
|
||||||
|
* Field Write Access
|
||||||
|
* @param name - Field Name
|
||||||
|
* @param val - Value to assign
|
||||||
|
* @return Dynamic - The assigned value
|
||||||
|
*/
|
||||||
public function hset(name:String, val:Dynamic):Dynamic;
|
public function hset(name:String, val:Dynamic):Dynamic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Field Read Access
|
||||||
|
* @param name - Field Name
|
||||||
|
* @return Dynamic - The returned field
|
||||||
|
*/
|
||||||
public function hget(name:String):Dynamic;
|
public function hget(name:String):Dynamic;
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,11 @@
|
|||||||
package hscript;
|
package hscript;
|
||||||
|
|
||||||
interface IHScriptCustomClassBehaviour {
|
/**
|
||||||
|
* Special Interface to make a class usable for Custom Classes.
|
||||||
|
*/
|
||||||
|
interface IHScriptCustomClassBehaviour extends IHScriptCustomAccessBehaviour{
|
||||||
public var __interp:Interp;
|
public var __interp:Interp;
|
||||||
public var __custom__variables:Map<String, Dynamic>;
|
|
||||||
public var __allowSetGet:Bool;
|
|
||||||
public var __real_fields:Array<String>;
|
public var __real_fields:Array<String>;
|
||||||
public var __class__fields:Array<String>;
|
public var __class__fields:Array<String>;
|
||||||
|
|
||||||
public function hset(name:String, val:Dynamic):Dynamic;
|
|
||||||
public function hget(name:String):Dynamic;
|
|
||||||
|
|
||||||
public function __callGetter(name:String):Dynamic;
|
|
||||||
|
|
||||||
public function __callSetter(name:String, val:Dynamic):Dynamic;
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package hscript;
|
package hscript;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special Interface for handling new instances of an object.
|
||||||
|
*/
|
||||||
interface IHScriptCustomConstructor {
|
interface IHScriptCustomConstructor {
|
||||||
public function hnew(args:Array<Dynamic>):Dynamic;
|
public function hnew(args:Array<Dynamic>):Dynamic;
|
||||||
}
|
}
|
||||||
+852
-325
File diff suppressed because it is too large
Load Diff
+7
-4
@@ -127,6 +127,8 @@ class Macro {
|
|||||||
tf.push( { name : f.name, meta : meta, doc : null, access : [], kind : FVar(convertType(f.t), null), pos : p } );
|
tf.push( { name : f.name, meta : meta, doc : null, access : [], kind : FVar(convertType(f.t), null), pos : p } );
|
||||||
}
|
}
|
||||||
TAnonymous(tf);
|
TAnonymous(tf);
|
||||||
|
case CTExpr(_):
|
||||||
|
throw "assert";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,12 +151,13 @@ class Macro {
|
|||||||
case EField(e, f):
|
case EField(e, f):
|
||||||
EField(convert(e), f);
|
EField(convert(e), f);
|
||||||
case EBinop(op, e1, e2):
|
case EBinop(op, e1, e2):
|
||||||
var b = binops.get(op);
|
var b = binops.get(op.toString());
|
||||||
if( b == null ) throw EInvalidOp(op);
|
if( b == null ) throw EInvalidOp(op.toString());
|
||||||
EBinop(b, convert(e1), convert(e2));
|
EBinop(b, convert(e1), convert(e2));
|
||||||
case EUnop(op, prefix, e):
|
case EUnop(op, prefix, e):
|
||||||
var u = unops.get(op);
|
var opStr = op.toString();
|
||||||
if( u == null ) throw EInvalidOp(op);
|
var u = unops.get(opStr);
|
||||||
|
if( u == null ) throw EInvalidOp(opStr);
|
||||||
EUnop(u, !prefix, convert(e));
|
EUnop(u, !prefix, convert(e));
|
||||||
case ECall(e, params):
|
case ECall(e, params):
|
||||||
ECall(convert(e), map(params, convert));
|
ECall(convert(e), map(params, convert));
|
||||||
|
|||||||
+585
-171
File diff suppressed because it is too large
Load Diff
+494
-377
@@ -1,377 +1,494 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C)2008-2017 Haxe Foundation
|
* Copyright (C)2008-2017 Haxe Foundation
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
* to deal in the Software without restriction, including without limitation
|
* to deal in the Software without restriction, including without limitation
|
||||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
* and/or sell copies of the Software, and to permit persons to whom the
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
* Software is furnished to do so, subject to the following conditions:
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
* DEALINGS IN THE SOFTWARE.
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package hscript;
|
package hscript;
|
||||||
import hscript.Expr;
|
import hscript.Expr;
|
||||||
|
|
||||||
class Printer {
|
class Printer {
|
||||||
|
|
||||||
var buf : StringBuf;
|
var buf : StringBuf;
|
||||||
var tabs : String;
|
var tabs : String;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function exprToString( e : Expr ):String {
|
public function exprToString( e : Expr ):String {
|
||||||
buf = new StringBuf();
|
buf = new StringBuf();
|
||||||
tabs = "";
|
tabs = "";
|
||||||
expr(e);
|
expr(e);
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function typeToString( t : CType ):String {
|
public function typeToString( t : CType ):String {
|
||||||
buf = new StringBuf();
|
buf = new StringBuf();
|
||||||
tabs = "";
|
tabs = "";
|
||||||
type(t);
|
type(t);
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline function add<T>(s:T):Void buf.add(s);
|
inline function add<T>(s:T):Void buf.add(s);
|
||||||
|
|
||||||
function type( t : CType ):Void {
|
function type( t : CType ):Void {
|
||||||
switch( t ) {
|
switch( t ) {
|
||||||
case CTOpt(t):
|
case CTOpt(t):
|
||||||
add('?');
|
add('?');
|
||||||
type(t);
|
type(t);
|
||||||
case CTPath(path, params):
|
case CTPath(path, params):
|
||||||
add(path.join("."));
|
add(path.join("."));
|
||||||
if( params != null ) {
|
if( params != null ) {
|
||||||
add("<");
|
add("<");
|
||||||
var first = true;
|
var first = true;
|
||||||
for( p in params ) {
|
for( p in params ) {
|
||||||
if( first ) first = false else add(", ");
|
if( first ) first = false else add(", ");
|
||||||
type(p);
|
type(p);
|
||||||
}
|
}
|
||||||
add(">");
|
add(">");
|
||||||
}
|
}
|
||||||
case CTNamed(name, t):
|
case CTNamed(name, t):
|
||||||
add(name);
|
add(name);
|
||||||
add(':');
|
add(':');
|
||||||
type(t);
|
type(t);
|
||||||
case CTFun(args, ret) if (Lambda.exists(args, function (a) return a.match(CTNamed(_, _)))):
|
case CTFun(args, ret) if (Lambda.exists(args, function (a) return a.match(CTNamed(_, _)))):
|
||||||
add('(');
|
add('(');
|
||||||
for (a in args)
|
for (a in args)
|
||||||
switch a {
|
switch a {
|
||||||
case CTNamed(_, _): type(a);
|
case CTNamed(_, _): type(a);
|
||||||
default: type(CTNamed('_', a));
|
default: type(CTNamed('_', a));
|
||||||
}
|
}
|
||||||
add(')->');
|
add(')->');
|
||||||
type(ret);
|
type(ret);
|
||||||
case CTFun(args, ret):
|
case CTFun(args, ret):
|
||||||
if( args.length == 0 )
|
if( args.length == 0 )
|
||||||
add("Void -> ");
|
add("Void -> ");
|
||||||
else {
|
else {
|
||||||
for( a in args ) {
|
for( a in args ) {
|
||||||
type(a);
|
type(a);
|
||||||
add(" -> ");
|
add(" -> ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
type(ret);
|
type(ret);
|
||||||
case CTAnon(fields):
|
case CTAnon(fields):
|
||||||
add("{");
|
add("{");
|
||||||
var first = true;
|
var first = true;
|
||||||
for( f in fields ) {
|
for( f in fields ) {
|
||||||
if( first ) { first = false; add(" "); } else add(", ");
|
if( first ) { first = false; add(" "); } else add(", ");
|
||||||
add(f.name + " : ");
|
add(f.name + " : ");
|
||||||
type(f.t);
|
type(f.t);
|
||||||
}
|
}
|
||||||
add(first ? "}" : " }");
|
add(first ? "}" : " }");
|
||||||
case CTParent(t):
|
case CTParent(t):
|
||||||
add("(");
|
add("(");
|
||||||
type(t);
|
type(t);
|
||||||
add(")");
|
add(")");
|
||||||
}
|
case CTExpr(e):
|
||||||
}
|
expr(e);
|
||||||
|
}
|
||||||
function addType( t : CType ):Void {
|
}
|
||||||
if( t != null ) {
|
|
||||||
add(" : ");
|
function addType( t : CType ):Void {
|
||||||
type(t);
|
if( t != null ) {
|
||||||
}
|
add(" : ");
|
||||||
}
|
type(t);
|
||||||
|
}
|
||||||
function expr( e : Expr ):Void {
|
}
|
||||||
if( e == null ) {
|
|
||||||
add("??NULL??");
|
function expr( e : Expr ):Void {
|
||||||
return;
|
if( e == null ) {
|
||||||
}
|
add("??NULL??");
|
||||||
switch( #if hscriptPos e.e #else e #end ) {
|
return;
|
||||||
case EImport(c, n):
|
}
|
||||||
add("import " + c);
|
switch(Tools.expr(e)) {
|
||||||
if(n != null)
|
case EPackage(n):
|
||||||
add(' as $n');
|
add('package');
|
||||||
case EClass(name, fields, extend, interfaces):
|
if(n != null)
|
||||||
add('class $name');
|
add(' $n');
|
||||||
if (extend != null)
|
add(';\n');
|
||||||
add(' extends $extend');
|
case EImport(c, n, u):
|
||||||
for(_interface in interfaces) {
|
add('${u ? 'using' : 'import'} $c');
|
||||||
add(' implements $_interface');
|
if(n != null)
|
||||||
}
|
add(' as $n');
|
||||||
add(' {\n');
|
case EClass(name, fields, extend, interfaces, fnal):
|
||||||
tabs += "\t";
|
var isFinal = fnal != null && fnal;
|
||||||
//for(field in fields) {
|
if(isFinal)
|
||||||
// expr(field);
|
add('final ');
|
||||||
//}
|
add('class $name');
|
||||||
|
if (extend != null)
|
||||||
tabs = tabs.substr(1);
|
add(' extends $extend');
|
||||||
add("}");
|
for(_interface in interfaces) {
|
||||||
case EConst(c):
|
add(' implements $_interface');
|
||||||
switch( c ) {
|
}
|
||||||
case CInt(i): add(i);
|
tabs += "\t";
|
||||||
case CFloat(f): add(f);
|
add(" {\n");
|
||||||
case CString(s): add('"'); add(s.split('"').join('\\"').split("\n").join("\\n").split("\r").join("\\r").split("\t").join("\\t")); add('"');
|
for( e in fields ) {
|
||||||
}
|
add(tabs);
|
||||||
case EIdent(v):
|
expr(e);
|
||||||
add(v);
|
//add(";\n");
|
||||||
case EVar(n, t, e): // TODO: static, public, override
|
}
|
||||||
add("var " + n);
|
//for(field in fields) {
|
||||||
addType(t);
|
// expr(field);
|
||||||
if( e != null ) {
|
//}
|
||||||
add(" = ");
|
|
||||||
expr(e);
|
tabs = tabs.substr(1);
|
||||||
}
|
add("}");
|
||||||
case EParent(e):
|
case EEnum(en, isAbstract):
|
||||||
add("("); expr(e); add(")");
|
if(isAbstract) {
|
||||||
case EBlock(el):
|
add('enum abstract ${en.name}(');
|
||||||
if( el.length == 0 ) {
|
if(en.underlyingType != null)
|
||||||
add("{}");
|
type(en.underlyingType);
|
||||||
} else {
|
else
|
||||||
tabs += "\t";
|
add('Int');
|
||||||
add("{\n");
|
add(')');
|
||||||
for( e in el ) {
|
if(en.fields.length == 0) {
|
||||||
add(tabs);
|
add(' {}');
|
||||||
expr(e);
|
return;
|
||||||
add(";\n");
|
}
|
||||||
}
|
tabs += "\t";
|
||||||
tabs = tabs.substr(1);
|
add(" {\n");
|
||||||
add("}");
|
for(e in en.fields) {
|
||||||
}
|
add(tabs);
|
||||||
case EField(e, f, s):
|
add(e.name);
|
||||||
expr(e);
|
if(e.value != null) {
|
||||||
add((s == true ? "?." : ".") + f);
|
add(" = ");
|
||||||
case EBinop(op, e1, e2):
|
expr(e.value);
|
||||||
expr(e1);
|
}
|
||||||
add(" " + op + " ");
|
add(";\n");
|
||||||
expr(e2);
|
}
|
||||||
case EUnop(op, pre, e):
|
tabs = tabs.substr(1);
|
||||||
if( pre ) {
|
add("}");
|
||||||
add(op);
|
} else {
|
||||||
expr(e);
|
add('enum ${en.name}');
|
||||||
} else {
|
if(en.fields.length == 0) {
|
||||||
expr(e);
|
add(' {}');
|
||||||
add(op);
|
return;
|
||||||
}
|
}
|
||||||
case ECall(e, args):
|
tabs += "\t";
|
||||||
if( e == null )
|
add(" {\n");
|
||||||
expr(e);
|
for(e in en.fields) {
|
||||||
else switch( #if hscriptPos e.e #else e #end ) {
|
add(tabs);
|
||||||
case EField(_), EIdent(_), EConst(_):
|
add(e.name);
|
||||||
expr(e);
|
if(e.args.length > 0) {
|
||||||
default:
|
add("(");
|
||||||
add("(");
|
var first = true;
|
||||||
expr(e);
|
for( a in e.args ) {
|
||||||
add(")");
|
if( first ) first = false else add(", ");
|
||||||
}
|
if( a.opt ) add("?");
|
||||||
add("(");
|
add(a.name);
|
||||||
var first = true;
|
addType(a.t);
|
||||||
for( a in args ) {
|
}
|
||||||
if( first ) first = false else add(", ");
|
add(')');
|
||||||
expr(a);
|
}
|
||||||
}
|
add(";\n");
|
||||||
add(")");
|
}
|
||||||
case EIf(cond,e1,e2):
|
tabs = tabs.substr(1);
|
||||||
add("if( ");
|
add("}");
|
||||||
expr(cond);
|
}
|
||||||
add(" ) ");
|
case ECast(e, t):
|
||||||
expr(e1);
|
var safe = t != null;
|
||||||
if( e2 != null ) {
|
add("cast ");
|
||||||
add(" else ");
|
if(safe) add("(");
|
||||||
expr(e2);
|
expr(e);
|
||||||
}
|
if(safe) {
|
||||||
case EWhile(cond,e):
|
add(", ");
|
||||||
add("while( ");
|
addType(t);
|
||||||
expr(cond);
|
add(")");
|
||||||
add(" ) ");
|
}
|
||||||
expr(e);
|
case ERegex(e, f):
|
||||||
case EDoWhile(cond,e):
|
add('~/$e/$f');
|
||||||
add("do ");
|
add(';\n');
|
||||||
expr(e);
|
case EConst(c):
|
||||||
add(" while ( ");
|
switch( c ) {
|
||||||
expr(cond);
|
case CInt(i): add(i);
|
||||||
add(" )");
|
case CFloat(f): add(f);
|
||||||
case EFor(v, it, e, ithv):
|
case CString(s): add('"'); add(s.split('"').join('\\"').split("\n").join("\\n").split("\r").join("\\r").split("\t").join("\\t")); add('"');
|
||||||
if(ithv != null)
|
}
|
||||||
add("for( "+ithv+" => "+v+" in ");
|
case EIdent(v):
|
||||||
else
|
add(v);
|
||||||
add("for( "+v+" in ");
|
case EVar(n, t, e, p, s, pr, isFinal, isInline, get, set, _):
|
||||||
expr(it);
|
if(p) add("public ");
|
||||||
add(" ) ");
|
else if(pr) add("private ");
|
||||||
expr(e);
|
if(s) add("static ");
|
||||||
case EBreak:
|
if(isInline) add("inline ");
|
||||||
add("break");
|
if(isFinal) add("final " + n);
|
||||||
case EContinue:
|
else add("var " + n);
|
||||||
add("continue");
|
|
||||||
case EFunction(params, e, name, ret): // TODO: static, public, override
|
if(get != null || set != null) {
|
||||||
add("function");
|
add("(");
|
||||||
if( name != null )
|
switch(get) {
|
||||||
add(" " + name);
|
case ADefault: add("default, ");
|
||||||
add("(");
|
case ANull: add("null, ");
|
||||||
var first = true;
|
case AGet: add("get, ");
|
||||||
for( a in params ) {
|
case ADynamic: add("dynamic, ");
|
||||||
if( first ) first = false else add(", ");
|
case ANever: add("never, ");
|
||||||
if( a.opt ) add("?");
|
default:
|
||||||
add(a.name);
|
}
|
||||||
addType(a.t);
|
switch(set) {
|
||||||
}
|
case ADefault: add("default");
|
||||||
add(")");
|
case ANull: add("null");
|
||||||
addType(ret);
|
case ASet: add("set");
|
||||||
add(" ");
|
case ADynamic: add("dynamic");
|
||||||
expr(e);
|
case ANever: add("never");
|
||||||
case EReturn(e):
|
default:
|
||||||
add("return");
|
}
|
||||||
if( e != null ) {
|
add(")");
|
||||||
add(" ");
|
}
|
||||||
expr(e);
|
|
||||||
}
|
addType(t);
|
||||||
case EArray(e,index):
|
if( e != null ) {
|
||||||
expr(e);
|
add(" = ");
|
||||||
add("[");
|
expr(e);
|
||||||
expr(index);
|
}
|
||||||
add("]");
|
case EParent(e):
|
||||||
case EArrayDecl(el, _):
|
add("("); expr(e); add(")");
|
||||||
add("[");
|
case EBlock(el):
|
||||||
var first = true;
|
if( el.length == 0 ) {
|
||||||
for( e in el ) {
|
add("{}");
|
||||||
if( first ) first = false else add(", ");
|
} else {
|
||||||
expr(e);
|
tabs += "\t";
|
||||||
}
|
add("{\n");
|
||||||
add("]");
|
for( e in el ) {
|
||||||
case ENew(cl, args):
|
add(tabs);
|
||||||
add("new " + cl + "(");
|
expr(e);
|
||||||
var first = true;
|
add(";\n");
|
||||||
for( e in args ) {
|
}
|
||||||
if( first ) first = false else add(", ");
|
tabs = tabs.substr(1);
|
||||||
expr(e);
|
add("}");
|
||||||
}
|
}
|
||||||
add(")");
|
case EField(e, f, s):
|
||||||
case EThrow(e):
|
expr(e);
|
||||||
add("throw ");
|
add((s == true ? "?." : ".") + f);
|
||||||
expr(e);
|
case EBinop(op, e1, e2):
|
||||||
case ETry(e, v, t, ecatch):
|
expr(e1);
|
||||||
add("try ");
|
add(" " + op.toString() + " ");
|
||||||
expr(e);
|
expr(e2);
|
||||||
add(" catch( " + v);
|
case EUnop(op, pre, e):
|
||||||
addType(t);
|
if( pre ) {
|
||||||
add(") ");
|
add(op);
|
||||||
expr(ecatch);
|
expr(e);
|
||||||
case EObject(fl):
|
} else {
|
||||||
if( fl.length == 0 ) {
|
expr(e);
|
||||||
add("{}");
|
add(op);
|
||||||
} else {
|
}
|
||||||
tabs += "\t";
|
case ECall(e, args):
|
||||||
add("{\n");
|
if( e == null )
|
||||||
for( f in fl ) {
|
expr(e);
|
||||||
add(tabs);
|
else switch( Tools.expr(e)) {
|
||||||
add(f.name+" : ");
|
case EField(_), EIdent(_), EConst(_):
|
||||||
expr(f.e);
|
expr(e);
|
||||||
add(",\n");
|
default:
|
||||||
}
|
add("(");
|
||||||
tabs = tabs.substr(1);
|
expr(e);
|
||||||
add("}");
|
add(")");
|
||||||
}
|
}
|
||||||
case ETernary(c,e1,e2):
|
add("(");
|
||||||
expr(c);
|
var first = true;
|
||||||
add(" ? ");
|
for( a in args ) {
|
||||||
expr(e1);
|
if( first ) first = false else add(", ");
|
||||||
add(" : ");
|
expr(a);
|
||||||
expr(e2);
|
}
|
||||||
case ESwitch(e, cases, def):
|
add(")");
|
||||||
add("switch( ");
|
case EIf(cond,e1,e2):
|
||||||
expr(e);
|
add("if( ");
|
||||||
add(") {");
|
expr(cond);
|
||||||
for( c in cases ) {
|
add(" ) ");
|
||||||
add("case ");
|
expr(e1);
|
||||||
var first = true;
|
if( e2 != null ) {
|
||||||
for( v in c.values ) {
|
add(" else ");
|
||||||
if( first ) first = false else add(", ");
|
expr(e2);
|
||||||
expr(v);
|
}
|
||||||
}
|
case EWhile(cond,e):
|
||||||
add(": ");
|
add("while( ");
|
||||||
expr(c.expr);
|
expr(cond);
|
||||||
add(";\n");
|
add(" ) ");
|
||||||
}
|
expr(e);
|
||||||
if( def != null ) {
|
case EDoWhile(cond,e):
|
||||||
add("default: ");
|
add("do ");
|
||||||
expr(def);
|
expr(e);
|
||||||
add(";\n");
|
add(" while ( ");
|
||||||
}
|
expr(cond);
|
||||||
add("}");
|
add(" )");
|
||||||
case EMeta(name, args, e):
|
case EFor(v, it, e, ithv):
|
||||||
add("@");
|
if(ithv != null)
|
||||||
add(name);
|
add("for( "+ithv+" => "+v+" in ");
|
||||||
if( args != null && args.length > 0 ) {
|
else
|
||||||
add("(");
|
add("for( "+v+" in ");
|
||||||
var first = true;
|
expr(it);
|
||||||
for( a in args ) {
|
add(" ) ");
|
||||||
if( first ) first = false else add(", ");
|
expr(e);
|
||||||
expr(e);
|
case EBreak:
|
||||||
}
|
add("break");
|
||||||
add(")");
|
case EContinue:
|
||||||
}
|
add("continue");
|
||||||
add(" ");
|
case EFunction(params, e, name, ret): // TODO: static, public, override
|
||||||
expr(e);
|
add("function");
|
||||||
case ECheckType(e, t):
|
if( name != null )
|
||||||
add("(");
|
add(" " + name);
|
||||||
expr(e);
|
add("(");
|
||||||
add(" : ");
|
var first = true;
|
||||||
addType(t);
|
for( a in params ) {
|
||||||
add(")");
|
if( first ) first = false else add(", ");
|
||||||
}
|
if( a.opt ) add("?");
|
||||||
}
|
add(a.name);
|
||||||
|
addType(a.t);
|
||||||
public static function toString( e : Expr ):String {
|
}
|
||||||
return new Printer().exprToString(e);
|
add(")");
|
||||||
}
|
addType(ret);
|
||||||
|
add(" ");
|
||||||
public static function errorToString( e : Expr.Error ):String {
|
expr(e);
|
||||||
var message = switch( #if hscriptPos e.e #else e #end ) {
|
case EReturn(e):
|
||||||
case EInvalidChar(c): "Invalid character: '"+(StringTools.isEof(c) ? "EOF (End Of File)" : String.fromCharCode(c))+"' ("+c+")";
|
add("return");
|
||||||
case EUnexpected(s): "Unexpected token: \""+s+"\"";
|
if( e != null ) {
|
||||||
case EUnterminatedString: "Unterminated string";
|
add(" ");
|
||||||
case EUnterminatedComment: "Unterminated comment";
|
expr(e);
|
||||||
case EInvalidPreprocessor(str): "Invalid preprocessor (" + str + ")";
|
}
|
||||||
case EUnknownVariable(v): "Unknown variable: "+v;
|
case EArray(e,index):
|
||||||
case EInvalidIterator(v): "Invalid iterator: "+v;
|
expr(e);
|
||||||
case EInvalidOp(op): "Invalid operator: "+op;
|
add("[");
|
||||||
case EInvalidAccess(f): "Invalid access to field " + f;
|
expr(index);
|
||||||
case ECustom(msg): msg;
|
add("]");
|
||||||
case EInvalidClass(cla): "Invalid class: " + cla + " was not found.";
|
case EArrayDecl(el, _):
|
||||||
case EAlreadyExistingClass(cla): 'Custom Class named $cla already exists.';
|
add("[");
|
||||||
};
|
var first = true;
|
||||||
#if hscriptPos
|
for( e in el ) {
|
||||||
return e.origin + ":" + e.line + ": " + message;
|
if( first ) first = false else add(", ");
|
||||||
#else
|
expr(e);
|
||||||
return message;
|
}
|
||||||
#end
|
add("]");
|
||||||
}
|
case ENew(cl, args, params):
|
||||||
|
add("new " + cl);
|
||||||
|
if(params != null) {
|
||||||
}
|
add("<");
|
||||||
|
var first = true;
|
||||||
|
for( p in params ) {
|
||||||
|
if( first ) first = false else add(", ");
|
||||||
|
type(p);
|
||||||
|
}
|
||||||
|
add(">");
|
||||||
|
}
|
||||||
|
add("(");
|
||||||
|
var first = true;
|
||||||
|
for( e in args ) {
|
||||||
|
if( first ) first = false else add(", ");
|
||||||
|
expr(e);
|
||||||
|
}
|
||||||
|
add(")");
|
||||||
|
case EThrow(e):
|
||||||
|
add("throw ");
|
||||||
|
expr(e);
|
||||||
|
case ETry(e, v, t, ecatch):
|
||||||
|
add("try ");
|
||||||
|
expr(e);
|
||||||
|
add(" catch( " + v);
|
||||||
|
addType(t);
|
||||||
|
add(") ");
|
||||||
|
expr(ecatch);
|
||||||
|
case EObject(fl):
|
||||||
|
if( fl.length == 0 ) {
|
||||||
|
add("{}");
|
||||||
|
} else {
|
||||||
|
tabs += "\t";
|
||||||
|
add("{\n");
|
||||||
|
for( f in fl ) {
|
||||||
|
add(tabs);
|
||||||
|
add(f.name+" : ");
|
||||||
|
expr(f.e);
|
||||||
|
add(",\n");
|
||||||
|
}
|
||||||
|
tabs = tabs.substr(1);
|
||||||
|
add("}");
|
||||||
|
}
|
||||||
|
case ETernary(c,e1,e2):
|
||||||
|
expr(c);
|
||||||
|
add(" ? ");
|
||||||
|
expr(e1);
|
||||||
|
add(" : ");
|
||||||
|
expr(e2);
|
||||||
|
case ESwitch(e, cases, def):
|
||||||
|
add("switch( ");
|
||||||
|
expr(e);
|
||||||
|
add(") {");
|
||||||
|
for( c in cases ) {
|
||||||
|
add("case ");
|
||||||
|
var first = true;
|
||||||
|
for( v in c.values ) {
|
||||||
|
if( first ) first = false else add(", ");
|
||||||
|
expr(v);
|
||||||
|
}
|
||||||
|
add(": ");
|
||||||
|
expr(c.expr);
|
||||||
|
add(";\n");
|
||||||
|
}
|
||||||
|
if( def != null ) {
|
||||||
|
add("default: ");
|
||||||
|
expr(def);
|
||||||
|
add(";\n");
|
||||||
|
}
|
||||||
|
add("}");
|
||||||
|
case EMeta(name, args, e):
|
||||||
|
add("@");
|
||||||
|
add(name);
|
||||||
|
if( args != null && args.length > 0 ) {
|
||||||
|
add("(");
|
||||||
|
var first = true;
|
||||||
|
for( a in args ) {
|
||||||
|
if( first ) first = false else add(", ");
|
||||||
|
expr(e);
|
||||||
|
}
|
||||||
|
add(")");
|
||||||
|
}
|
||||||
|
add(" ");
|
||||||
|
expr(e);
|
||||||
|
case ECheckType(e, t):
|
||||||
|
add("(");
|
||||||
|
expr(e);
|
||||||
|
add(" : ");
|
||||||
|
addType(t);
|
||||||
|
add(")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function toString( e : Expr ):String {
|
||||||
|
return new Printer().exprToString(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function errorToString( e : Expr.Error ):String {
|
||||||
|
var message = switch( #if hscriptPos e.e #else e #end ) {
|
||||||
|
case EInvalidChar(c): "Invalid character: '"+(StringTools.isEof(c) ? "EOF (End Of File)" : String.fromCharCode(c))+"' ("+c+")";
|
||||||
|
case EUnexpected(s): "Unexpected token: \""+s+"\"";
|
||||||
|
case EUnterminatedString: "Unterminated string";
|
||||||
|
case EUnterminatedComment: "Unterminated comment";
|
||||||
|
case EInvalidPreprocessor(str): "Invalid preprocessor (" + str + ")";
|
||||||
|
case EUnknownVariable(v): "Unknown variable: "+v;
|
||||||
|
case EInvalidIterator(v): "Invalid iterator: "+v;
|
||||||
|
case EInvalidOp(op): "Invalid operator: "+op;
|
||||||
|
case EInvalidAccess(f): "Invalid access to field " + f;
|
||||||
|
case ECustom(msg): msg;
|
||||||
|
case EInvalidClass(cla): "Invalid class: " + cla + " was not found.";
|
||||||
|
case EAlreadyExistingClass(cla): 'Custom Class named $cla already exists.';
|
||||||
|
};
|
||||||
|
#if hscriptPos
|
||||||
|
return e.origin + ":" + e.line + ": " + message;
|
||||||
|
#else
|
||||||
|
return message;
|
||||||
|
#end
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,179 @@
|
|||||||
|
package hscript;
|
||||||
|
|
||||||
|
import hscript.utils.UnsafeReflect;
|
||||||
|
import hscript.Interp;
|
||||||
|
import hscript.Expr.FieldPropertyAccess;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special variable that handles 'getter/setter' function calls
|
||||||
|
* depending of the read/write access combination.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* ```haxe
|
||||||
|
* public var myvar(get, set):Int;
|
||||||
|
* var _myvar:Int = 10;
|
||||||
|
*
|
||||||
|
* function get_myvar():Int {
|
||||||
|
* return _myvar;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* function set_myvar(val:Int):Int {
|
||||||
|
* if(val > 10) return _myvar = val;
|
||||||
|
* return val;
|
||||||
|
* }
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @see https://haxe.org/manual/class-field-property.html
|
||||||
|
*/
|
||||||
|
@:access(hscript.Interp)
|
||||||
|
class Property {
|
||||||
|
private static inline var GET = 'get_';
|
||||||
|
private static inline var SET = 'set_';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the attached field.
|
||||||
|
*/
|
||||||
|
public final name:String;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current value. If isn't initialized, it's always `null`.
|
||||||
|
*/
|
||||||
|
public var r:Dynamic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The getter property kind
|
||||||
|
*/
|
||||||
|
public final getter:FieldPropertyAccess;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The setter property kind
|
||||||
|
*/
|
||||||
|
public final setter:FieldPropertyAccess;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the field is declared as static.
|
||||||
|
*/
|
||||||
|
public var isStatic(get, never):Bool;
|
||||||
|
function get_isStatic() {
|
||||||
|
return __isStatic && interp.allowStaticVariables;
|
||||||
|
}
|
||||||
|
|
||||||
|
var isVar:Bool;
|
||||||
|
var interp:Interp;
|
||||||
|
|
||||||
|
@:allow(hscript.Interp)
|
||||||
|
private var getterFunc(get, never):String;
|
||||||
|
private inline function get_getterFunc():String {
|
||||||
|
return '$GET$name';
|
||||||
|
}
|
||||||
|
|
||||||
|
@:allow(hscript.Interp)
|
||||||
|
private var setterFunc(get, never):String;
|
||||||
|
private inline function get_setterFunc():String {
|
||||||
|
return '$SET$name';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function new(name:String, r:Dynamic, getter:FieldPropertyAccess, setter:FieldPropertyAccess, isVar:Bool, isStatic:Bool, interp:Interp) {
|
||||||
|
this.name = name;
|
||||||
|
this.r = r;
|
||||||
|
this.getter = getter;
|
||||||
|
this.setter = setter;
|
||||||
|
this.isVar = isVar;
|
||||||
|
this.__isStatic = isStatic;
|
||||||
|
this.interp = interp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal flags to gain access to the current field value (if isn't a property field)
|
||||||
|
var __allowReadAccess:Bool = false;
|
||||||
|
var __allowWriteAccess:Bool = false;
|
||||||
|
// Internal flag to gain access if the field is accessed with @:bypassAccessor
|
||||||
|
var __allowSetGet:Bool = true;
|
||||||
|
|
||||||
|
final __isStatic:Bool = false;
|
||||||
|
|
||||||
|
public function get(isBypassAccessor:Bool) {
|
||||||
|
if(isBypassAccessor) __allowSetGet = false;
|
||||||
|
var r:Dynamic = callGetter();
|
||||||
|
if(isBypassAccessor) __allowSetGet = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(value:Dynamic, isBypassAccessor:Bool) {
|
||||||
|
if(isBypassAccessor) __allowSetGet = false;
|
||||||
|
var r:Dynamic = callSetter(value);
|
||||||
|
if(isBypassAccessor) __allowSetGet = true;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function callGetter():Dynamic {
|
||||||
|
switch (getter) {
|
||||||
|
case AGet | ADynamic:
|
||||||
|
var fName:String = getterFunc;
|
||||||
|
if (!__allowReadAccess && __allowSetGet) {
|
||||||
|
if (varExists(fName)) {
|
||||||
|
return callAccessor(fName);
|
||||||
|
} else
|
||||||
|
interp.error(ECustom('Method $fName required by property $name is missing'));
|
||||||
|
} else {
|
||||||
|
if ((setter == ADefault || setter == ANull) || isVar) {
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
interp.error(ECustom('Field $name cannot be accessed because it is not a real variable${interp.isBypassAccessor ? '. Add @:isVar to enable it' : ''}'));
|
||||||
|
}
|
||||||
|
case ANever:
|
||||||
|
interp.error(ECustom('This expression cannot be accessed for reading'));
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function callSetter(val:Dynamic):Dynamic {
|
||||||
|
switch (setter) {
|
||||||
|
case ASet | ADynamic:
|
||||||
|
var fName:String = setterFunc;
|
||||||
|
if (!__allowWriteAccess && __allowSetGet) {
|
||||||
|
if (varExists(fName))
|
||||||
|
return callAccessor(fName, val);
|
||||||
|
else
|
||||||
|
interp.error(ECustom('Method $fName required by property $name is missing'));
|
||||||
|
} else {
|
||||||
|
if ((getter == ADefault || getter == ANull) || isVar) {
|
||||||
|
return r = val;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
interp.error(ECustom('Field $name cannot be accessed because it is not a real variable${interp.isBypassAccessor ? '. Add @:isVar to enable it' : ''}'));
|
||||||
|
}
|
||||||
|
case ANever:
|
||||||
|
interp.error(ECustom('This expression cannot be accessed for writing'));
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
return r = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function callAccessor(f:String, ?value:Dynamic):Dynamic {
|
||||||
|
var fn = isStatic ? interp.staticVariables.get(f) : interp.variables.get(f);
|
||||||
|
var rt:Dynamic = null;
|
||||||
|
var isWrite:Bool = value != null;
|
||||||
|
if (fn != null && Reflect.isFunction(fn)) {
|
||||||
|
if (isWrite) __allowWriteAccess = true;
|
||||||
|
else __allowReadAccess = true;
|
||||||
|
|
||||||
|
rt = UnsafeReflect.callMethodUnsafe(null, fn, isWrite ? [value] : []);
|
||||||
|
|
||||||
|
if (isWrite) __allowWriteAccess = false;
|
||||||
|
else __allowReadAccess = false;
|
||||||
|
|
||||||
|
return rt;
|
||||||
|
} else
|
||||||
|
interp.error(ECustom('Method $f required by property ${f.substr(3)} is missing'));
|
||||||
|
|
||||||
|
return rt;
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline function varExists(n:String) {
|
||||||
|
return isStatic ? interp.staticVariables.exists(n) : interp.variables.exists(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
+143
-113
@@ -1,114 +1,144 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C)2008-2017 Haxe Foundation
|
* Copyright (C)2008-2017 Haxe Foundation
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
* to deal in the Software without restriction, including without limitation
|
* to deal in the Software without restriction, including without limitation
|
||||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
* and/or sell copies of the Software, and to permit persons to whom the
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
* Software is furnished to do so, subject to the following conditions:
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
*
|
*
|
||||||
* The above copyright notice and this permission notice shall be included in
|
* The above copyright notice and this permission notice shall be included in
|
||||||
* all copies or substantial portions of the Software.
|
* all copies or substantial portions of the Software.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
* DEALINGS IN THE SOFTWARE.
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
package hscript;
|
package hscript;
|
||||||
import hscript.Expr;
|
import hscript.Expr;
|
||||||
|
|
||||||
class Tools {
|
class Tools {
|
||||||
|
|
||||||
public static function iter( e : Expr, f : Expr -> Void ):Void {
|
public static function iter( e : Expr, f : Expr -> Void ):Void {
|
||||||
switch( expr(e) ) {
|
switch( expr(e) ) {
|
||||||
case EConst(_), EIdent(_):
|
case EConst(_), EIdent(_):
|
||||||
case EImport(c): f(e);
|
case EImport(c): f(e);
|
||||||
case EClass(_, e, _, _): for( a in e ) f(a);
|
case EClass(_, e, _, _): for( a in e ) f(a);
|
||||||
case EVar(_, _, e): if( e != null ) f(e);
|
case EVar(_, _, e): if( e != null ) f(e);
|
||||||
case EParent(e): f(e);
|
case EParent(e): f(e);
|
||||||
case EBlock(el): for( e in el ) f(e);
|
case EBlock(el): for( e in el ) f(e);
|
||||||
case EField(e, _): f(e);
|
case EField(e, _): f(e);
|
||||||
case EBinop(_, e1, e2): f(e1); f(e2);
|
case EBinop(_, e1, e2): f(e1); f(e2);
|
||||||
case EUnop(_, _, e): f(e);
|
case EUnop(_, _, e): f(e);
|
||||||
case ECall(e, args): f(e); for( a in args ) f(a);
|
case ECall(e, args): f(e); for( a in args ) f(a);
|
||||||
case EIf(c, e1, e2): f(c); f(e1); if( e2 != null ) f(e2);
|
case EIf(c, e1, e2): f(c); f(e1); if( e2 != null ) f(e2);
|
||||||
case EWhile(c, e): f(c); f(e);
|
case EWhile(c, e): f(c); f(e);
|
||||||
case EDoWhile(c, e): f(c); f(e);
|
case EDoWhile(c, e): f(c); f(e);
|
||||||
case EFor(_, it, e): f(it); f(e);
|
case EFor(_, it, e): f(it); f(e);
|
||||||
case EBreak,EContinue:
|
case EBreak,EContinue:
|
||||||
case EFunction(_, e, _, _): f(e);
|
case EFunction(_, e, _, _): f(e);
|
||||||
case EReturn(e): if( e != null ) f(e);
|
case EReturn(e): if( e != null ) f(e);
|
||||||
case EArray(e, i): f(e); f(i);
|
case EArray(e, i): f(e); f(i);
|
||||||
case EArrayDecl(el): for( e in el ) f(e);
|
case EArrayDecl(el): for( e in el ) f(e);
|
||||||
case ENew(_,el): for( e in el ) f(e);
|
case ENew(_,el): for( e in el ) f(e);
|
||||||
case EThrow(e): f(e);
|
case EThrow(e): f(e);
|
||||||
case ETry(e, _, _, c): f(e); f(c);
|
case ETry(e, _, _, c): f(e); f(c);
|
||||||
case EObject(fl): for( fi in fl ) f(fi.e);
|
case EObject(fl): for( fi in fl ) f(fi.e);
|
||||||
case ETernary(c, e1, e2): f(c); f(e1); f(e2);
|
case ETernary(c, e1, e2): f(c); f(e1); f(e2);
|
||||||
case ESwitch(e, cases, def):
|
case ESwitch(e, cases, def):
|
||||||
f(e);
|
f(e);
|
||||||
for( c in cases ) {
|
for( c in cases ) {
|
||||||
for( v in c.values ) f(v);
|
for( v in c.values ) f(v);
|
||||||
f(c.expr);
|
f(c.expr);
|
||||||
}
|
}
|
||||||
if( def != null ) f(def);
|
if( def != null ) f(def);
|
||||||
case EMeta(name, args, e): if( args != null ) for( a in args ) f(a); f(e);
|
case EMeta(name, args, e): if( args != null ) for( a in args ) f(a); f(e);
|
||||||
case ECheckType(e,_): f(e);
|
case ECheckType(e,_): f(e);
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function map( e : Expr, f : Expr -> Expr ):Expr {
|
public static function map( e : Expr, f : Expr -> Expr ):Expr {
|
||||||
var edef = switch( expr(e) ) {
|
var edef = switch( expr(e) ) {
|
||||||
case EConst(_), EIdent(_), EBreak, EContinue: expr(e);
|
case EConst(_), EIdent(_), EBreak, EContinue: expr(e);
|
||||||
case EVar(n, t, e, isPublic, isStatic, isPrivate): EVar(n, t, if( e != null ) f(e) else null, isPublic, isStatic, isPrivate);
|
case EVar(n, t, e, isPublic, isStatic, isPrivate): EVar(n, t, if( e != null ) f(e) else null, isPublic, isStatic, isPrivate);
|
||||||
case EParent(e): EParent(f(e));
|
case EParent(e): EParent(f(e));
|
||||||
case EBlock(el): EBlock([for( e in el ) f(e)]);
|
case EBlock(el): EBlock([for( e in el ) f(e)]);
|
||||||
case EField(e, fi): EField(f(e),fi);
|
case EField(e, fi): EField(f(e),fi);
|
||||||
case EBinop(op, e1, e2): EBinop(op, f(e1), f(e2));
|
case EBinop(op, e1, e2): EBinop(op, f(e1), f(e2));
|
||||||
case EUnop(op, pre, e): EUnop(op, pre, f(e));
|
case EUnop(op, pre, e): EUnop(op, pre, f(e));
|
||||||
case ECall(e, args): ECall(f(e),[for( a in args ) f(a)]);
|
case ECall(e, args): ECall(f(e),[for( a in args ) f(a)]);
|
||||||
case EIf(c, e1, e2): EIf(f(c),f(e1),if( e2 != null ) f(e2) else null);
|
case EIf(c, e1, e2): EIf(f(c),f(e1),if( e2 != null ) f(e2) else null);
|
||||||
case EWhile(c, e): EWhile(f(c),f(e));
|
case EWhile(c, e): EWhile(f(c),f(e));
|
||||||
case EDoWhile(c, e): EDoWhile(f(c),f(e));
|
case EDoWhile(c, e): EDoWhile(f(c),f(e));
|
||||||
case EFor(v, it, e): EFor(v, f(it), f(e));
|
case EFor(v, it, e): EFor(v, f(it), f(e));
|
||||||
case EFunction(args, e, name, t, isPublic, isStatic, isOverride, isPrivate): EFunction(args, f(e), name, t, isPublic, isStatic, isOverride, isPrivate);
|
case EFunction(args, e, name, t, isPublic, isStatic, isOverride, isPrivate): EFunction(args, f(e), name, t, isPublic, isStatic, isOverride, isPrivate);
|
||||||
case EReturn(e): EReturn(if( e != null ) f(e) else null);
|
case EReturn(e): EReturn(if( e != null ) f(e) else null);
|
||||||
case EArray(e, i): EArray(f(e),f(i));
|
case EArray(e, i): EArray(f(e),f(i));
|
||||||
case EArrayDecl(el): EArrayDecl([for( e in el ) f(e)]);
|
case EArrayDecl(el): EArrayDecl([for( e in el ) f(e)]);
|
||||||
case ENew(cl,el): ENew(cl,[for( e in el ) f(e)]);
|
case ENew(cl,el): ENew(cl,[for( e in el ) f(e)]);
|
||||||
case EThrow(e): EThrow(f(e));
|
case EThrow(e): EThrow(f(e));
|
||||||
case ETry(e, v, t, c): ETry(f(e), v, t, f(c));
|
case ETry(e, v, t, c): ETry(f(e), v, t, f(c));
|
||||||
case EObject(fl): EObject([for( fi in fl ) { name : fi.name, e : f(fi.e) }]);
|
case EObject(fl): EObject([for( fi in fl ) { name : fi.name, e : f(fi.e) }]);
|
||||||
case ETernary(c, e1, e2): ETernary(f(c), f(e1), f(e2));
|
case ETernary(c, e1, e2): ETernary(f(c), f(e1), f(e2));
|
||||||
case ESwitch(e, cases, def): ESwitch(f(e), [for( c in cases ) { values : [for( v in c.values ) f(v)], expr : f(c.expr) } ], def == null ? null : f(def));
|
case ESwitch(e, cases, def): ESwitch(f(e), [for( c in cases ) { values : [for( v in c.values ) f(v)], expr : f(c.expr) } ], def == null ? null : f(def));
|
||||||
case EMeta(name, args, e): EMeta(name, args == null ? null : [for( a in args ) f(a)], f(e));
|
case EMeta(name, args, e): EMeta(name, args == null ? null : [for( a in args ) f(a)], f(e));
|
||||||
case ECheckType(e,t): ECheckType(f(e), t);
|
case ECheckType(e,t): ECheckType(f(e), t);
|
||||||
case EImport(c): EImport(c);
|
case EImport(c): EImport(c);
|
||||||
case EClass(name, el, extend, interfaces): EClass(name, [for( e in el ) f(e)], extend, interfaces);
|
case EClass(name, el, extend, interfaces): EClass(name, [for( e in el ) f(e)], extend, interfaces);
|
||||||
}
|
default: expr(e);
|
||||||
return mk(edef, e);
|
}
|
||||||
}
|
return mk(edef, e);
|
||||||
|
}
|
||||||
public static inline function expr( e : Expr ) : ExprDef {
|
|
||||||
#if hscriptPos
|
public static inline function expr( e : Expr ) : ExprDef {
|
||||||
return e.e;
|
#if hscriptPos
|
||||||
#else
|
return e.e;
|
||||||
return e;
|
#else
|
||||||
#end
|
return e;
|
||||||
}
|
#end
|
||||||
|
}
|
||||||
public static inline function mk( e : ExprDef, p : Expr ):Expr {
|
|
||||||
#if hscriptPos
|
public static inline function mk( e : ExprDef, p : Expr ):Expr {
|
||||||
return { e : e, pmin : p.pmin, pmax : p.pmax, origin : p.origin, line : p.line };
|
#if hscriptPos
|
||||||
#else
|
return { e : e, pmin : p.pmin, pmax : p.pmax, origin : p.origin, line : p.line };
|
||||||
return e;
|
#else
|
||||||
#end
|
return e;
|
||||||
}
|
#end
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DO NOT USE INLINE ON THIS FUNCTION
|
||||||
|
**/
|
||||||
|
public static function argCount(func: haxe.Constraints.Function): Int {
|
||||||
|
// https://github.com/pisayesiwsi/hscript-iris/blob/dev/crowplexus/hscript/Tools.hx#L206
|
||||||
|
#if cpp
|
||||||
|
return untyped __cpp__("{0}->__ArgCount()", func);
|
||||||
|
#elseif js
|
||||||
|
return untyped js.Syntax.code("{0}.length", func);
|
||||||
|
#elseif hl
|
||||||
|
var ft = hl.Type.getDynamic(func);
|
||||||
|
if (ft.kind != HFun)
|
||||||
|
return -1;
|
||||||
|
return ft.getArgsCount();
|
||||||
|
#else
|
||||||
|
return -1;
|
||||||
|
#end
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isUppercase(s:String) {
|
||||||
|
if(s.length == 0) return false;
|
||||||
|
var c:Int = StringTools.fastCodeAt(s, 0);
|
||||||
|
if(StringTools.isEof(c)) return false; // Just in case :3
|
||||||
|
return c >= 65 && c <= 90; // A-Z
|
||||||
|
}
|
||||||
|
|
||||||
|
public static inline function isCustomAbstract(obj:Dynamic):Bool
|
||||||
|
return obj != null && obj is IHScriptAbstractBehaviour;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,14 +10,16 @@ import haxe.macro.Compiler;
|
|||||||
|
|
||||||
using StringTools;
|
using StringTools;
|
||||||
|
|
||||||
class UsingHandler {
|
class AbstractHandler {
|
||||||
public static function init() {
|
public static function init() {
|
||||||
|
#if HSCRIPT_ABSTRACT_SUPPORT
|
||||||
#if !display
|
#if !display
|
||||||
if(Context.defined("display")) return;
|
if(Context.defined("display")) return;
|
||||||
for(apply in Config.ALLOWED_ABSTRACT_AND_ENUM) {
|
for(apply in Config.ALLOWED_ABSTRACT_AND_ENUM) {
|
||||||
Compiler.addGlobalMetadata(apply, '@:build(hscript.macros.UsingHandler.build())');
|
Compiler.addGlobalMetadata(apply, '@:build(hscript.macros.AbstractHandler.build())');
|
||||||
}
|
}
|
||||||
#end
|
#end
|
||||||
|
#end
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function build():Array<Field> {
|
public static function build():Array<Field> {
|
||||||
@@ -10,10 +10,10 @@ import haxe.macro.Type.FieldKind;
|
|||||||
import haxe.macro.Type.ClassField;
|
import haxe.macro.Type.ClassField;
|
||||||
import haxe.macro.Type.VarAccess;
|
import haxe.macro.Type.VarAccess;
|
||||||
import haxe.macro.*;
|
import haxe.macro.*;
|
||||||
import Sys;
|
|
||||||
|
|
||||||
using StringTools;
|
using StringTools;
|
||||||
|
|
||||||
|
// BIG TODO: make typed classes scriptable
|
||||||
class ClassExtendMacro {
|
class ClassExtendMacro {
|
||||||
public static inline final FUNC_PREFIX = "_HX_SUPER__";
|
public static inline final FUNC_PREFIX = "_HX_SUPER__";
|
||||||
public static inline final CLASS_SUFFIX = "_HSX";
|
public static inline final CLASS_SUFFIX = "_HSX";
|
||||||
@@ -29,6 +29,7 @@ class ClassExtendMacro {
|
|||||||
for(apply in Config.ALLOWED_CUSTOM_CLASSES) {
|
for(apply in Config.ALLOWED_CUSTOM_CLASSES) {
|
||||||
Compiler.addGlobalMetadata(apply, "@:build(hscript.macros.ClassExtendMacro.build())");
|
Compiler.addGlobalMetadata(apply, "@:build(hscript.macros.ClassExtendMacro.build())");
|
||||||
}
|
}
|
||||||
|
//Context.onAfterTyping(buildTyped);
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
}
|
}
|
||||||
@@ -61,6 +62,7 @@ class ClassExtendMacro {
|
|||||||
if(key == "away3d.tools.commands.Weld") return fields; // Error: Unknown identifier
|
if(key == "away3d.tools.commands.Weld") return fields; // Error: Unknown identifier
|
||||||
if(fkey == "hscript.CustomClassHandler.TemplateClass") return fields; // Error: Redefined
|
if(fkey == "hscript.CustomClassHandler.TemplateClass") return fields; // Error: Redefined
|
||||||
if(fkey == "hscript.CustomClassHandler.CustomTemplateClass") return fields; // Error: Redefined
|
if(fkey == "hscript.CustomClassHandler.CustomTemplateClass") return fields; // Error: Redefined
|
||||||
|
if(fkey == "hscript.CustomClass") return fields; // Error: Redefined
|
||||||
if(key == "sys.thread.EventLoop") return fields; // Error: cant override force inlined
|
if(key == "sys.thread.EventLoop") return fields; // Error: cant override force inlined
|
||||||
if(Config.DISALLOW_CUSTOM_CLASSES.contains(cl.module) || Config.DISALLOW_CUSTOM_CLASSES.contains(fkey)) return fields;
|
if(Config.DISALLOW_CUSTOM_CLASSES.contains(cl.module) || Config.DISALLOW_CUSTOM_CLASSES.contains(fkey)) return fields;
|
||||||
if(cl.module.contains("_")) return fields; // Weird issue, sorry
|
if(cl.module.contains("_")) return fields; // Weird issue, sorry
|
||||||
@@ -211,6 +213,15 @@ class ClassExtendMacro {
|
|||||||
continue;
|
continue;
|
||||||
if (f.name == "new") {
|
if (f.name == "new") {
|
||||||
hasNew = true;
|
hasNew = true;
|
||||||
|
switch (f.kind) {
|
||||||
|
case FFun(fn):
|
||||||
|
var constructor:Field = buildConstructor(fn.args);
|
||||||
|
|
||||||
|
shadowClass.fields.push(constructor);
|
||||||
|
definedFields.push(f.name);
|
||||||
|
default:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (f.name.startsWith(FUNC_PREFIX))
|
if (f.name.startsWith(FUNC_PREFIX))
|
||||||
@@ -246,27 +257,24 @@ class ClassExtendMacro {
|
|||||||
overrideExpr = macro {
|
overrideExpr = macro {
|
||||||
var name:String = $v{name};
|
var name:String = $v{name};
|
||||||
|
|
||||||
if (__custom__variables != null) {
|
if (__interp != null && __class__fields.contains(name)) {
|
||||||
if(__custom__variables.exists(name)) {
|
var v:Dynamic = null;
|
||||||
var v:Dynamic = null;
|
if (Reflect.isFunction(v = __interp.variables.get(name))) {
|
||||||
if (Reflect.isFunction(v = __custom__variables.get(name))) {
|
return v($a{arguments});
|
||||||
return v($a{arguments});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.$name($a{arguments});
|
return super.$name($a{arguments});
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
overrideExpr = macro {
|
overrideExpr = macro {
|
||||||
var name:String = $v{name};
|
var name:String = $v{name};
|
||||||
|
|
||||||
if (__custom__variables != null) {
|
if (__interp != null && __class__fields.contains(name)) {
|
||||||
if(__custom__variables.exists(name)) {
|
var v:Dynamic = null;
|
||||||
var v:Dynamic = null;
|
if (Reflect.isFunction(v = __interp.variables.get(name))) {
|
||||||
if (Reflect.isFunction(v = __custom__variables.get(name))) {
|
v($a{arguments});
|
||||||
v($a{arguments});
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.$name($a{arguments});
|
super.$name($a{arguments});
|
||||||
@@ -331,7 +339,6 @@ class ClassExtendMacro {
|
|||||||
pack: cl.pack.copy(),
|
pack: cl.pack.copy(),
|
||||||
name: cl.name
|
name: cl.name
|
||||||
}, [
|
}, [
|
||||||
{name: "IHScriptCustomAccessBehaviour", pack: ["hscript"]},
|
|
||||||
{name: "IHScriptCustomClassBehaviour", pack: ["hscript"]}
|
{name: "IHScriptCustomClassBehaviour", pack: ["hscript"]}
|
||||||
], false, true, false);
|
], false, true, false);
|
||||||
shadowClass.name = '${cl.name}$CLASS_SUFFIX';
|
shadowClass.name = '${cl.name}$CLASS_SUFFIX';
|
||||||
@@ -341,20 +348,27 @@ class ClassExtendMacro {
|
|||||||
|
|
||||||
// Adding hscript getters and setters
|
// Adding hscript getters and setters
|
||||||
|
|
||||||
|
shadowClass.fields.push({
|
||||||
|
name: "__cachedFieldSet",
|
||||||
|
pos: Context.currentPos(),
|
||||||
|
kind: FVar(macro: Map<String, Dynamic>),
|
||||||
|
access: [APublic, AStatic]
|
||||||
|
});
|
||||||
|
|
||||||
shadowClass.fields.push({
|
shadowClass.fields.push({
|
||||||
name: "__interp",
|
name: "__interp",
|
||||||
pos: Context.currentPos(),
|
pos: Context.currentPos(),
|
||||||
kind: FVar(macro: hscript.Interp),
|
kind: FVar(macro: hscript.Interp),
|
||||||
access: [APublic]
|
access: [APublic]
|
||||||
});
|
});
|
||||||
|
/*
|
||||||
shadowClass.fields.push({
|
shadowClass.fields.push({
|
||||||
name: "__custom__variables",
|
name: "__custom__variables",
|
||||||
pos: Context.currentPos(),
|
pos: Context.currentPos(),
|
||||||
kind: FVar(macro: Map<String, Dynamic>),
|
kind: FVar(macro: Map<String, Dynamic>),
|
||||||
access: [APublic]
|
access: [APublic]
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
shadowClass.fields.push({
|
shadowClass.fields.push({
|
||||||
name: "__allowSetGet",
|
name: "__allowSetGet",
|
||||||
pos: Context.currentPos(),
|
pos: Context.currentPos(),
|
||||||
@@ -383,10 +397,7 @@ class ClassExtendMacro {
|
|||||||
ret: macro: Dynamic,
|
ret: macro: Dynamic,
|
||||||
params: [],
|
params: [],
|
||||||
expr: macro {
|
expr: macro {
|
||||||
__allowSetGet = false;
|
return null;
|
||||||
var v = __custom__variables.get("get_" + name)();
|
|
||||||
__allowSetGet = true;
|
|
||||||
return v;
|
|
||||||
},
|
},
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
@@ -407,10 +418,7 @@ class ClassExtendMacro {
|
|||||||
ret: macro: Dynamic,
|
ret: macro: Dynamic,
|
||||||
params: [],
|
params: [],
|
||||||
expr: macro {
|
expr: macro {
|
||||||
__allowSetGet = false;
|
return null;
|
||||||
var v = __custom__variables.get("set_" + name)(val);
|
|
||||||
__allowSetGet = true;
|
|
||||||
return v;
|
|
||||||
},
|
},
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
@@ -472,30 +480,81 @@ class ClassExtendMacro {
|
|||||||
|
|
||||||
var hgetField = if(hasHgetInSuper) {
|
var hgetField = if(hasHgetInSuper) {
|
||||||
macro {
|
macro {
|
||||||
if(__allowSetGet && __custom__variables.exists("get_" + name))
|
if (__interp != null) {
|
||||||
return __callGetter(name);
|
if(__class__fields.contains(name)) {
|
||||||
if (__custom__variables.exists(name))
|
var v:Dynamic = __interp.variables.get(name);
|
||||||
return __custom__variables.get(name);
|
if(v != null && v is hscript.Property)
|
||||||
|
return cast(v, hscript.Property).callGetter(name);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
else @:privateAccess {
|
||||||
|
var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass;
|
||||||
|
while(cls != null) {
|
||||||
|
if(cls.hasField(name))
|
||||||
|
return cls.getField(name);
|
||||||
|
|
||||||
|
var prev:hscript.CustomClass = cast cls.__upperClass;
|
||||||
|
if(prev == null)
|
||||||
|
break;
|
||||||
|
cls = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return super.hget(name);
|
return super.hget(name);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
macro {
|
macro {
|
||||||
if(__allowSetGet && __custom__variables.exists("get_" + name))
|
if (__interp != null) {
|
||||||
return __callGetter(name);
|
if(__class__fields.contains(name)) {
|
||||||
if (__custom__variables.exists(name))
|
var v:Dynamic = __interp.variables.get(name);
|
||||||
return __custom__variables.get(name);
|
var ba:Bool = @:privateAccess __interp.isBypassAccessor;
|
||||||
|
if(v != null && v is hscript.Property)
|
||||||
|
return cast(v, hscript.Property).get(ba);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
else @:privateAccess {
|
||||||
|
var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass;
|
||||||
|
while(cls != null) {
|
||||||
|
if(cls.hasField(name))
|
||||||
|
return cls.getField(name);
|
||||||
|
|
||||||
|
var prev:hscript.CustomClass = cast cls.__upperClass;
|
||||||
|
if(prev == null)
|
||||||
|
break;
|
||||||
|
cls = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return UnsafeReflect.getProperty(this, name);
|
return UnsafeReflect.getProperty(this, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var hsetField = if(hasHsetInSuper) {
|
var hsetField = if(hasHsetInSuper) {
|
||||||
macro {
|
macro {
|
||||||
if(__allowSetGet && __custom__variables.exists("set_" + name))
|
if (__interp != null) {
|
||||||
return __callSetter(name, val);
|
if(__class__fields.contains(name)) {
|
||||||
if (__custom__variables.exists(name)) {
|
var v:Dynamic = __interp.variables.get(name);
|
||||||
__custom__variables.set(name, val);
|
if(v != null && v is hscript.Property)
|
||||||
return val;
|
return cast(v, hscript.Property).callSetter(name, val);
|
||||||
|
__interp.variables.set(name, val);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
else @:privateAccess {
|
||||||
|
var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass;
|
||||||
|
while(cls != null) {
|
||||||
|
if(cls.hasField(name))
|
||||||
|
return cls.setField(name, val);
|
||||||
|
|
||||||
|
var prev:hscript.CustomClass = cast cls.__upperClass;
|
||||||
|
if(prev == null)
|
||||||
|
break;
|
||||||
|
cls = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(__real_fields.contains(name)) {
|
if(__real_fields.contains(name)) {
|
||||||
UnsafeReflect.setProperty(this, name, val);
|
UnsafeReflect.setProperty(this, name, val);
|
||||||
return UnsafeReflect.field(this, name);
|
return UnsafeReflect.field(this, name);
|
||||||
@@ -504,17 +563,34 @@ class ClassExtendMacro {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
macro {
|
macro {
|
||||||
if(__allowSetGet && __custom__variables.exists("set_" + name))
|
if (__interp != null) {
|
||||||
return __callSetter(name, val);
|
if(__class__fields.contains(name)) {
|
||||||
if (__custom__variables.exists(name)) {
|
var v:Dynamic = __interp.variables.get(name);
|
||||||
__custom__variables.set(name, val);
|
var ba:Bool = @:privateAccess __interp.isBypassAccessor;
|
||||||
return val;
|
if(v != null && v is hscript.Property)
|
||||||
|
return cast(v, hscript.Property).set(val, ba);
|
||||||
|
__interp.variables.set(name, val);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
else @:privateAccess {
|
||||||
|
var cls:hscript.CustomClass = cast __interp.__customClass.__upperClass;
|
||||||
|
while(cls != null) {
|
||||||
|
if(cls.hasField(name))
|
||||||
|
return cls.setField(name, val);
|
||||||
|
|
||||||
|
var prev:hscript.CustomClass = cast cls.__upperClass;
|
||||||
|
if(prev == null)
|
||||||
|
break;
|
||||||
|
cls = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(__real_fields.contains(name)) {
|
if(__real_fields.contains(name)) {
|
||||||
UnsafeReflect.setProperty(this, name, val);
|
UnsafeReflect.setProperty(this, name, val);
|
||||||
return UnsafeReflect.field(this, name);
|
return UnsafeReflect.field(this, name);
|
||||||
}
|
}
|
||||||
__custom__variables.set(name, val);
|
//__custom__variables.set(name, val);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -579,6 +655,51 @@ class ClassExtendMacro {
|
|||||||
|
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function buildConstructor(constArgs:Array<FunctionArg>):Field {
|
||||||
|
var superCallArgs:Array<Expr> = [for (arg in constArgs) macro $i{arg.name}];
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: 'new',
|
||||||
|
access: [APublic],
|
||||||
|
pos: Context.currentPos(),
|
||||||
|
kind: FFun({
|
||||||
|
args: constArgs,
|
||||||
|
expr: macro {
|
||||||
|
// Call the super constructor with appropriate args
|
||||||
|
super($a{superCallArgs});
|
||||||
|
|
||||||
|
if(__cachedFieldSet != null) {
|
||||||
|
for(k => v in __cachedFieldSet) {
|
||||||
|
Reflect.setProperty(this, k, v);
|
||||||
|
}
|
||||||
|
__cachedFieldSet.clear();
|
||||||
|
__cachedFieldSet = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static function buildTyped(modules:Array<haxe.macro.Type.ModuleType>) {
|
||||||
|
for(m in modules) {
|
||||||
|
switch(m) {
|
||||||
|
case TClassDecl(c):
|
||||||
|
var cl = c.get();
|
||||||
|
if (cl.isAbstract || cl.isExtern || cl.isFinal || cl.isInterface)
|
||||||
|
continue;
|
||||||
|
if (cl.params.length == 0)
|
||||||
|
continue;
|
||||||
|
if (!cl.name.endsWith("_Impl_") && !cl.name.endsWith(CLASS_SUFFIX) && !cl.name.endsWith("_HSC"))
|
||||||
|
buildTypedClass(cl);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static function buildTypedClass(cl:ClassType) {}
|
||||||
|
|
||||||
|
static function buildShadowClass(cl:ClassType) {}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
class ClassExtendMacro {
|
class ClassExtendMacro {
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package hscript.utils;
|
||||||
|
|
||||||
|
@:structInit
|
||||||
|
class UsingEntry {
|
||||||
|
public var call:Dynamic->String->Array<Dynamic>->Dynamic;
|
||||||
|
public var fields:Array<String>;
|
||||||
|
|
||||||
|
public function hasField(name:String) {
|
||||||
|
return fields.contains(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special class that handles static extension function calls.
|
||||||
|
*
|
||||||
|
* A static extension allows pseudo-extending
|
||||||
|
* existing types without modifying their source.
|
||||||
|
* In Haxe this is achieved by declaring a static method with a first argument
|
||||||
|
* of the extending type and then bringing the defining class into context through `using`.
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
* ```haxe
|
||||||
|
* class IntExtender {
|
||||||
|
* static public function triple(i:Int) {
|
||||||
|
* return i * 3;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* using IntExtender;
|
||||||
|
*
|
||||||
|
* trace(12.triple()); // 36
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @see https://haxe.org/manual/lf-static-extension.html
|
||||||
|
*/
|
||||||
|
class UsingHandler {
|
||||||
|
// Predefined static extension classes
|
||||||
|
public static final defaultExtension:Map<String, UsingEntry> = [
|
||||||
|
"StringTools" => { // https://github.com/pisayesiwsi/hscript-iris/blob/dev/crowplexus/iris/Iris.hx#L45
|
||||||
|
fields: Type.getClassFields(StringTools),
|
||||||
|
call: function(o:Dynamic, f:String, args:Array<Dynamic>):Dynamic {
|
||||||
|
if (f == "isEof") // has @:noUsing
|
||||||
|
return null;
|
||||||
|
return switch (Type.typeof(o)) {
|
||||||
|
case TInt if (f == 'hex'):
|
||||||
|
StringTools.hex(o, args[0]);
|
||||||
|
case TClass(String):
|
||||||
|
var field = UnsafeReflect.field(StringTools, f);
|
||||||
|
if (UnsafeReflect.isFunction(field)) UnsafeReflect.callMethodUnsafe(StringTools, field, [o].concat(args)); else null;
|
||||||
|
default:
|
||||||
|
null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Lambda" => { // https://github.com/pisayesiwsi/hscript-iris/blob/dev/crowplexus/iris/Iris.hx#L62
|
||||||
|
fields: Type.getClassFields(Lambda),
|
||||||
|
call: function(o:Dynamic, f:String, args:Array<Dynamic>):Dynamic {
|
||||||
|
if (o != null && o.iterator != null) {
|
||||||
|
var field = UnsafeReflect.field(Lambda, f);
|
||||||
|
if (UnsafeReflect.isFunction(field)) {
|
||||||
|
return UnsafeReflect.callMethodUnsafe(Lambda, field, [o].concat(args));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
@:allow(hscript.CustomClass)
|
||||||
|
@:allow(hscript.CustomClassHandler)
|
||||||
|
public var usingEntries(default, null):Map<String, UsingEntry> = [];
|
||||||
|
|
||||||
|
public function new() {}
|
||||||
|
|
||||||
|
public function registerEntry(name:String, entry:Dynamic->String->Array<Dynamic>->Dynamic, fields:Array<String>) {
|
||||||
|
usingEntries.set(name, {call: entry, fields: fields});
|
||||||
|
}
|
||||||
|
|
||||||
|
public inline function entryExists(name:String):Bool {
|
||||||
|
return usingEntries.exists(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user