Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Reworked shared variables. (see [#36](https://github.com/Kriptel/RuleScript/pull/36))
- Wildcard import.
- ScriptedModule.
- Shared variables between interps. (see [#29](https://github.com/Kriptel/RuleScript/pull/29))
Expand Down
4 changes: 1 addition & 3 deletions docs/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

- `types` - Stores all previously `defined` types.

- `publicVariables` - **Public** variables, each entry in this array is added using the meta `@:contextValue('public')`, or the keyword `public`.

- `staticVariables` - **Static** variables, each entry in this array is added using the meta `@:contextValue('static')`, or the keyword `static`.
- `variables` - **Public** & **Static** variables, each entry in this array is added using the meta `@:contextValue('public')`, or the keyword `public`.

---

Expand Down
32 changes: 15 additions & 17 deletions rulescript/Context.hx
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package rulescript;

import rulescript.scriptedClass.RuleScriptedClass.ScriptedClass;
import rulescript.types.ScriptedAbstract;
import rulescript.types.ScriptedType;
import rulescript.scriptedClass.RuleScriptedClass.ScriptedClass;
import rulescript.types.context.EVariableModifiers;
import rulescript.types.context.EVariableDeclarations;

/**
* The Context stores types to preserve repeated imports,
* eliminating the need to compile script types or search for native ones.
*
* All script types automatically inherit the Context.
*/
class Context
{
class Context {
public var types:Map<String, Dynamic> = [];
public var variables:Map<String, ContextVariable> = [];

public var publicVariables:Map<String, Dynamic> = [];
public var staticVariables:Map<String, Dynamic> = [];
@:deprecated public var publicVariables(default, never):Map<String, Dynamic> = [];
@:deprecated public var staticVariables(default, never):Map<String, Dynamic> = [];

public function new() {}

public function reset():Void
{
public function reset():Void {
types = [];

resetVariables();
}

public function resetVariables():Void
{
publicVariables = []; // yuhuh -orbl
staticVariables = [];
public function resetVariables():Void {
variables = []; // yuhuh -orbl
}

public function resolveType(path:String):Dynamic
{
public function resolveType(path:String):Dynamic {
if (types.exists(path))
return types[path];
else
{
else {
final t:Dynamic = Tools.resolveType(path, this);

if (t is ScriptedType)
switch (cast(t, ScriptedType).__rulescript_type)
{
switch (cast(t, ScriptedType).__rulescript_type) {
case CLASS:
@:privateAccess cast(t, ScriptedClass).interp.access.context = this;
case ABSTRACT:
Expand All @@ -54,3 +50,5 @@ class Context
};
}
}

typedef ContextVariable = {modifier:EVariableModifiers, declaration:EVariableDeclarations, value:Dynamic, ?parent:String};
46 changes: 27 additions & 19 deletions rulescript/interps/RuleScriptInterp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import rulescript.types.ScriptedEnum;
import rulescript.types.ScriptedType;
import rulescript.types.ScriptedTypeUtil;
import rulescript.types.ScriptedTypedef;
import rulescript.types.context.EVariableModifiers;
import rulescript.types.context.EVariableDeclarations;

using rulescript.Tools;

Expand Down Expand Up @@ -96,13 +98,8 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
v = get(superInstance, id);

// SHARED VARIABLES
if (v == null && context != null)
{
if (context.staticVariables.exists(id))
v = context.staticVariables.get(id);
if (context.publicVariables.exists(id))
v = context.publicVariables.get(id);
}
if (v == null && context != null && context.variables.exists(id))
v = context.variables.get(id).value;

if (v == null)
error(EUnknownVariable(id)); // fixes - orbl
Expand Down Expand Up @@ -151,10 +148,12 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
{
if (superInstance != null && (superFields.contains(name) || superFields.contains('set_' + name)))
Reflect.setProperty(superInstance, name, v);
else if (context != null && context.staticVariables.exists(name))
context.staticVariables.set(name, v);
else if (context != null && context.publicVariables.exists(name))
context.publicVariables.set(name, v);
else if (context != null && context.variables.exists(name)) {
final cv:rulescript.Context.ContextVariable = context.variables.get(name);
if (cv.declaration == FINAL && cv.parent != scriptName)
error(ECustom('$name: Unable to override final variable defined by another script.'));
else context.variables.set(name, {modifier: cv.modifier, declaration:cv.declaration, value: v, parent: scriptName});
}
else
{
var lastValue = variables.get(name);
Expand Down Expand Up @@ -242,7 +241,7 @@ class RuleScriptInterp extends hscript.Interp implements IInterp

if ((!locals.exists(id) && !variables.exists(id))
&& (!superFields.contains(id) && !superFields.contains('get_$id'))
&& (context == null || !context.staticVariables.exists(id) && !context.publicVariables.exists(id)))
&& (context == null || !context.variables.exists(id)))
{
final typePath:String = path.join('.');

Expand Down Expand Up @@ -278,28 +277,37 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
return switch (n)
{
case ':contextValue' if (context != null):
var isFunction:Bool = false;

var isFunction:Bool = false, isFinal:Bool = false;
final n:Null<String> = switch (Tools.getExpr(e))
{
case EFunction(_, _, n):
isFunction = true;
n;
case EProp(n, _), EVar(n, _): n;
case EProp(n, _): n;
case EVar(n, _,_,_,f): isFinal = f; n;
default: null;
};

final isStatic:Bool = args.length > 0 && args[0].getExpr().match(EIdent('static'));

if (isFunction && depth == 0)
{
return (isStatic ? context.staticVariables : context.publicVariables)[n] = this.expr(e);
return context.variables[n] = {
value: this.expr(e),
declaration: isFinal ? EVariableDeclarations.FINAL : EVariableDeclarations.VAR,
modifier: isStatic ? EVariableModifiers.STATIC : EVariableModifiers.PUBLIC,
parent: scriptName
};
}
else if (depth == 0)
{
this.expr(e);

(isStatic ? context.staticVariables : context.publicVariables).set(n, resolve(n));
context.variables.set(n, {
value: resolve(n),
declaration: isFinal ? EVariableDeclarations.FINAL : EVariableDeclarations.VAR,
modifier: isStatic ? EVariableModifiers.STATIC : EVariableModifiers.PUBLIC,
parent: scriptName
});
}
else
{
Expand All @@ -314,7 +322,7 @@ class RuleScriptInterp extends hscript.Interp implements IInterp
case EVar(n, _, e, global, _):
if (global)
{
if (context == null || (!context.staticVariables.exists(n) && !context.publicVariables.exists(n)))
if (context == null || !context.variables.exists(n))
variables.set(n, (e == null) ? null : this.expr(e));
}
else
Expand Down
6 changes: 6 additions & 0 deletions rulescript/types/context/EVariableDeclarations.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package rulescript.types.context;

enum EVariableDeclarations {
FINAL;
VAR;
}
6 changes: 6 additions & 0 deletions rulescript/types/context/EVariableModifiers.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package rulescript.types.context;

enum EVariableModifiers {
PUBLIC;
STATIC;
}