Skip to content

Commit 04ff06d

Browse files
committed
Variable priority
1 parent 4d1fee6 commit 04ff06d

7 files changed

Lines changed: 52 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- RuleScripted Classes.
1212
- RuleScriptedClassUtil.
1313
- RuleScriptedClass interface.
14+
- `this` in script.
1415
- Script Properties.
1516
- `hasErrorHandler` variable to RuleScript and RuleScriptInterp.
1617
- `errorHandler` variable to RuleScript and RuleScriptInterp.

rulescript/RuleScriptInterp.hx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package rulescript;
22

33
import hscript.Expr;
44
import rulescript.RuleScriptProperty.Property;
5+
import rulescript.scriptedClass.RuleScriptedClass;
56

67
using rulescript.Tools;
78

@@ -52,6 +53,9 @@ class RuleScriptInterp extends hscript.Interp
5253

5354
override function resolve(id:String):Dynamic
5455
{
56+
if (id == 'this')
57+
return this;
58+
5559
if (id == 'super' && superInstance != null)
5660
return superInstance;
5761

@@ -440,9 +444,25 @@ class RuleScriptInterp extends hscript.Interp
440444
*/
441445
override function get(o:Dynamic, f:String):Dynamic
442446
{
447+
if (o == this)
448+
{
449+
if (variables.exists(f))
450+
return getScriptProp(variables.get(f));
451+
else
452+
o = superInstance;
453+
}
454+
443455
var prop:Dynamic = super.get(o, f);
456+
444457
if (prop != null)
445-
return prop;
458+
return getScriptProp(prop);
459+
460+
if (o is RuleScriptedClass)
461+
{
462+
var cl:RuleScriptedClass = cast(o, RuleScriptedClass);
463+
if (cl.variableExists(f))
464+
return getScriptProp(cl.getVariable(f));
465+
}
446466

447467
for (cl in usings)
448468
{

rulescript/Tools.hx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ class Tools
5656

5757
#if hscriptPos
5858
inline public static function getExpr(e:Expr):ExprDef
59-
{
6059
return e.e;
61-
}
6260
#else
6361
inline public static function getExpr(e:Expr):Expr
6462
return e;
@@ -94,6 +92,16 @@ class Tools
9492
case DUsing(path):
9593
pushExpr(EUsing(path));
9694
case DClass(c):
95+
c.fields.sort((f1:FieldDecl, f2:FieldDecl) ->
96+
{
97+
return switch [f1.kind.match(KVar(_)), f2.kind.match(KVar(_))]
98+
{
99+
case [true, true], [false, false]: 0;
100+
case [true, false]: -1;
101+
case [false, true]: 1;
102+
};
103+
});
104+
97105
for (field in c.fields)
98106
{
99107
switch (field.kind)

rulescript/parsers/HxParser.hx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,16 @@ class HScriptParserPlus extends hscript.Parser
675675
var part:Dynamic = parts[currentPart++];
676676
if (part is String)
677677
part = mk(EConst(CString(cast part)));
678+
else
679+
{
680+
switch (Tools.getExpr(part))
681+
{
682+
case EConst(c):
683+
default:
684+
part = mk(EParent(part));
685+
}
686+
}
687+
678688
if (e == null)
679689
e = part;
680690
else
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package rulescript.scriptedClass;
22

33
@:autoBuild(rulescript.macro.RuleScriptedClass.build())
4-
interface RuleScriptedClass {}
4+
interface RuleScriptedClass
5+
{
6+
function variableExists(name:String):Bool;
7+
function getVariable(name:String):Dynamic;
8+
function setVariable(name:String, value:Dynamic):Dynamic;
9+
}

test/scripts/haxe/ScriptedClass.rhx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package;
22

33
class ScriptedClass extends test.ScriptedClassTest
44
{
5+
public var variable1(default,default) = 'variable1: Hello World';
6+
57
public function new(customArg:Int,arg1:String)
68
{
79
trace('Constructor.pre: $customArg, $arg1');

test/scripts/haxe/importTest/ScriptImportTest.rhx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ScriptImportTest{
66
function main(){
77
trace(Script.helloWorld());
88

9-
new ScriptedClass(1,'ScriptImportTest');
9+
var scriptedClass = new ScriptedClass(1,'ScriptImportTest');
10+
trace(scriptedClass.variable1);
1011
}
1112
}

0 commit comments

Comments
 (0)