Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 16 additions & 35 deletions rulescript/Tools.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,14 +11,11 @@ import rulescript.types.ScriptedTypeUtil;
import rulescript.types.Typedefs;
#end

#if hl
@:build(rulescript.macro.CallMethodMacro.build())
#end
class Tools
{
public static function parseTypePath(typePath:String):TypePath
public static function parseTypePath(typePath:String):ImportPath
{
return new TypePath(typePath);
return new ImportPath(typePath);
}

inline public static function startsWithLowerCase(s:String):Bool
Expand All@@ -40,14 +37,14 @@ class Tools
return _printer.typeToString(type);
}

@:noCompletion public static function usingFunction(?o:Dynamic, f:Function, ?a1:Dynamic, ?a2:Dynamic, ?a3:Dynamic, ?a4:Dynamic, ?a5:Dynamic, ?a6:Dynamic,
?a7:Dynamic, ?a8:Dynamic)
@:noCompletion public static function usingFunction(?o:Dynamic, f:Function, ?args:Array<Dynamic>)
{
#if interp
var args:Array<Dynamic> = [o, a1, a2, a3, a4, a5, a6, a7, a8];
var i:Int = 8;
var args:Array<Dynamic> = [o].concat(args);
var i:Int = 0;
i = args.length;

while (i >= 0)
while (i != 1)
{
if (args[i] == null)
args.pop();
Expand All@@ -57,9 +54,9 @@ class Tools
}
return Reflect.callMethod(o, f, args);
#elseif hl
return __hl_callMethod(f, [o, a1, a2, a3, a4, a5, a6, a7, a8]);
return __hl_callMethod(f, [o].concat(args));
#else
return Reflect.callMethod(o, f, [o, a1, a2, a3, a4, a5, a6, a7, a8]);
return Reflect.callMethod(o, f, [o].concat(args));
#end
}

Expand DownExpand Up@@ -393,20 +390,6 @@ class Tools
{
case 0:
() -> f([]);
case 1:
Tools.callMethod1.bind(f, _);
case 2:
Tools.callMethod2.bind(f, _, _);
case 3:
Tools.callMethod3.bind(f, _, _, _);
case 4:
Tools.callMethod4.bind(f, _, _, _, _);
case 5, 6:
Tools.callMethod6.bind(f, _, _, _, _, _, _);
case 7, 8:
Tools.callMethod8.bind(f, _, _, _, _, _, _, _, _);
case 9, 10, 11, 12:
Tools.callMethod12.bind(f, _, _, _, _, _, _, _, _, _, _, _, _);
default:
Reflect.makeVarArgs(f);
}
Expand All@@ -419,10 +402,6 @@ class Tools
throw "Invalid function " + func;

final need = ft.getArgsCount();

if (need > 8)
return rulescript.macro.CallMethodMacro.__hl_callMethod();

final args:hl.types.ArrayDyn = cast args;

final count = args.length;
Expand DownExpand Up@@ -482,7 +461,7 @@ enum EnumPattern
#end

@:forward
abstract TypePath(_TypePath)
abstract ImportPath(_ImportPath)
{
public var typeName(get, never):String;

Expand All@@ -506,7 +485,8 @@ abstract TypePath(_TypePath)
pack: pack,
name: name,
sub: typeName,
fullPath: typePath
fullPath: typePath,
cls: Type.resolveClass(typePath)
}
}

Expand All@@ -529,9 +509,9 @@ abstract TypePath(_TypePath)
return this.sub ?? this.name;
}

public static function create(pack:Array<String>, name:String, sub:String):TypePath
public static function create(pack:Array<String>, name:String, sub:String):ImportPath
{
return new TypePath(createString(pack, name, sub));
return new ImportPath(createString(pack, name, sub));
}

public static function createString(pack:Array<String>, name:String, ?sub:String):String
Expand DownExpand Up@@ -560,10 +540,11 @@ abstract TypePath(_TypePath)
}
}

private typedef _TypePath =
private typedef _ImportPath =
{
var pack:Array<String>;
var name:String;
var ?sub:String;
var ?cls:Class<Dynamic>;
var fullPath:String;
}
58 changes: 38 additions & 20 deletions rulescript/interps/NeoInterp.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@ import haxe.Constraints.IMap;
import haxe.Exception;
import hscript.Expr;
import rulescript.RuleScript.IInterp;
import rulescript.Tools.TypePath;
import rulescript.Tools.ImportPath;
import rulescript.Tools.getScriptProp;
import rulescript.interps.neo.NeoCompiler;
import rulescript.interps.neo.NeoInterpAccess;
Expand DownExpand Up@@ -38,7 +38,8 @@ class NeoInterp implements IInterp

public var variables:Map<String, Dynamic>;

public var usings:Array<Dynamic>;
public var usings:Map<String, ImportPath>;
public var usingsCache:Map<String, Array<Dynamic>->Dynamic>;

var bytes:Array<NeoByte>;
var pos:Int = 0;
Expand DownExpand Up@@ -74,6 +75,7 @@ class NeoInterp implements IInterp
}));

usings = [];
usingsCache = [];

bytes = [];

Expand DownExpand Up@@ -263,8 +265,11 @@ class NeoInterp implements IInterp
v;

case CALL:
final f:Dynamic = getValue(command());
final prevCommand:Null<NeoByte> = command();
final f:Dynamic = getValue(prevCommand);
final str:String = stringBuffer[current()];

trace(f, prevCommand, str);
if (f == null)
error(ENullAccess);

Expand All@@ -273,6 +278,14 @@ class NeoInterp implements IInterp
getValue(command())
];

if (prevCommand == FIELD && usingsCache.exists(str))
{
trace(str, f, args);
setValue(usingsCache[str]([f].concat(args)));
trace(dyn);
return DYNAMIC;
}

setValue(call(f, args));

DYNAMIC;
Expand DownExpand Up@@ -538,14 +551,14 @@ class NeoInterp implements IInterp
{
if (!variables.exists(typeName))
{
final type:Dynamic = resolveType(TypePath.createString(path.split('.'), typeName));
final type:Dynamic = resolveType(ImportPath.createString(path.split('.'), typeName));
variables.set(typeName, type);
}
}
}
else
{
var name:String = alias ?? func ?? TypePath.getTypeName(path);
var name:String = alias ?? func ?? ImportPath.getTypeName(path);
var type:Dynamic = resolveType(path);

final value = if (func != null)
Expand All@@ -563,8 +576,25 @@ class NeoInterp implements IInterp
final path:String = nextString();
final type:Dynamic = resolveType(path);

if (!usings.contains(type))
usings.push(type);
trace(path, type);
if (!usings.exists(path))
{
usings.set(path, new ImportPath(path));

// Obtain the using's function fields
final fields = Type.getClassFields(usings.get(path).cls);
if (fields.length > 0)
{
for (fld in fields)
{
final field:Dynamic = Reflect.getProperty(usings.get(path).cls, fld);
if (!Reflect.isFunction(field)) continue;

final func:Dynamic = function(params:Array<Dynamic>) return Tools.usingFunction(usings.get(path).cls, field, params);
usingsCache.set(fld, func);
}
}
}

VOID;
case FUNCTION:
Expand DownExpand Up@@ -723,7 +753,6 @@ class NeoInterp implements IInterp
#else
final result:Dynamic = Reflect.callMethod(null, f, args);
#end

return result;
}

Expand DownExpand Up@@ -768,20 +797,9 @@ class NeoInterp implements IInterp
}

var prop:Dynamic = Reflect.getProperty(o, f);

if (prop != null)
return getScriptProp(prop);

if (usings.length > 0)
{
for (cl in usings)
{
var prop:Dynamic = Reflect.getProperty(cl, f);
if (prop != null)
return Tools.usingFunction.bind(o, prop, _, _, _, _, _, _, _, _);
}
}


return null;
}

Expand Down
58 changes: 35 additions & 23 deletions rulescript/interps/RuleScriptInterp.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,8 +27,11 @@ class RuleScriptInterp extends hscript.Interp implements IInterp

public var access:RuleScriptAccess;

public var imports:Map<String, Dynamic> = [];
public var usings:Map<String, Dynamic> = [];
public var imports:Map<String, ImportPath> = [];
public var importsCache:Map<String, Dynamic> = [];

public var usings:Map<String, ImportPath> = [];
public var usingsCache:Map<String, Array<Dynamic>->Dynamic> = [];

public var superInstance(default, set):Dynamic;

Expand DownExpand Up@@ -57,6 +60,7 @@ class RuleScriptInterp extends hscript.Interp implements IInterp

imports = [];
usings = [];
usingsCache = [];
typePaths = [];
}

Expand DownExpand Up@@ -349,8 +353,8 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
else
t;

imports.set(name, value);

imports.set(name, new ImportPath(path));
importsCache.set(name, value);
variables.set(name, value);
}
else
Expand All@@ -359,8 +363,9 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
{
if (!variables.exists(typeName))
{
final type:Dynamic = resolveType(TypePath.createString(path.split('.'), typeName));
imports.set(typeName, type);
final type:Dynamic = resolveType(ImportPath.createString(path.split('.'), typeName));
imports.set(typeName, new ImportPath(path));
importsCache.set(typeName, type);
variables.set(typeName, type);
}
}
Expand All@@ -371,7 +376,22 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
if (t == null)
error(ECustom('Type not found : $path'));

usings.set(path, t);
usings.set(path, new ImportPath(path));

// Obtain the using's function fields
final fields = Type.getClassFields(usings.get(path).cls);
if (fields.length > 0)
{
for (fld in fields)
{
final field:Dynamic = Reflect.getProperty(usings.get(path).cls, fld);
if (!Reflect.isFunction(field)) continue;

final func:Dynamic = function(params:Array<Dynamic>) return Tools.usingFunction(usings.get(path).cls, field, params);
usingsCache.set(fld, func);
}
}

case ETypeVarPath(path):
return resolveTypeOrValue(path);
case EMeta(n, args, e):
Expand DownExpand Up@@ -773,17 +793,9 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
}

var prop:Dynamic = super.get(o, f);

if (prop != null)
return getScriptProp(prop);

for (cl in usings)
{
var prop:Dynamic = Reflect.getProperty(cl, f);
if (prop != null)
return Tools.usingFunction.bind(o, prop, _, _, _, _, _, _, _, _);
}

return null;
}

Expand DownExpand Up@@ -829,21 +841,21 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
if (f == superInstance)
return call(o, resolve('__super_new'), args);

#if hl
final result:Dynamic = Tools.__hl_callMethod(f, args);
#else
final result:Dynamic = super.call(o, f, args);
#end

isSuperCall = false;

return result;
}

override function fcall(o:Dynamic, f:String, args:Array<Dynamic>):Dynamic
{
return call(o, ((o == superInstance
&& (locals.exists('__super_$f') || variables.exists('__super_$f'))) ? (resolve('__super_$f')) : get(o, f)), args);
var func = ((o == superInstance
&& (locals.exists('__super_$f') || variables.exists('__super_$f'))) ? (resolve('__super_$f')) : get(o, f));
if (func != null)
return call(o, func, args);

if (usingsCache.exists(f))
return usingsCache[f]([o].concat(args));
return null;
}

override function cnew(cl:String, args:Array<Dynamic>):Dynamic
Expand Down
6 changes: 3 additions & 3 deletions rulescript/macro/AbstractMacro.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,7 @@ package rulescript.macro;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.ExprTools;
import rulescript.Tools.TypePath;
import rulescript.Tools.ImportPath;
import sys.FileSystem;
import sys.io.File;

Expand DownExpand Up@@ -38,7 +38,7 @@ class AbstractMacro
for (abstractType in abstractsList)
{
if (!ignoreList.contains('-#$abstractType'))
buildAbstract(new TypePath(abstractType));
buildAbstract(new ImportPath(abstractType));
}
];

Expand DownExpand Up@@ -369,7 +369,7 @@ class AbstractMacro
return tokens;
}

static function buildAbstract(typePath:TypePath):Expr
static function buildAbstract(typePath:ImportPath):Expr
{
var type = switch (Context.getType(typePath.fullPath))
{
Expand Down
Loading