17 private readonly Dictionary<string, BadObject[]>
m_Attributes =
new Dictionary<string, BadObject[]>();
42 private readonly Dictionary<Type, object>
m_SingletonCache =
new Dictionary<Type, object>();
78 m_ScopeVariables.OnChangedProperty +=
OnChanged;
101 m_ScopeVariables.OnChangedProperty +=
OnChanged;
116 bool useVisibility =
false) : this(name,
195 return CreateScope(c,
"SCOPE");
227#region IDisposable Members
241 foreach (Action finalizer
in m_Finalizers)
246 m_Finalizers.Clear();
266 Parent.SetRegisteredApi(api);
271 if (!m_RegisteredApis.Contains(api))
273 m_RegisteredApis.Add(api);
289 m_Finalizers.Add(finalizer);
316 public void AddSingleton<T>(T instance) where T :
class
318 if (instance ==
null)
323 m_SingletonCache.Add(typeof(T), instance);
331 public T? GetSingleton<T>()
335 return Parent.GetSingleton<T>();
338 if (m_SingletonCache.TryGetValue(typeof(T), out
object? value))
353 public T GetSingleton<T>(
bool createNew) where T :
new()
357 return Parent.GetSingleton<T>(createNew);
360 if (m_SingletonCache.ContainsKey(typeof(T)))
362 return (T)m_SingletonCache[typeof(T)];
367 throw new Exception(
"Singleton not found");
371 m_SingletonCache[typeof(T)] = v;
398 foreach (KeyValuePair<Type, object> kvp
in ctx.Scope.GetRootScope()
401 s.m_SingletonCache.Add(kvp.Key, kvp.Value);
422 return GetStackTrace(
this);
433 List<BadScope> stack =
new List<BadScope>();
435 while (current !=
null)
442 current = current.m_Caller ?? current.
Parent;
445 return string.Join(
"\n", stack.Select(s => s.Name));
452 while (current !=
null)
456 yield
return current;
459 current = current.m_Caller ?? current.
Parent;
475 ~(BadScopeFlags.CaptureReturn |
476 BadScopeFlags.CaptureBreak |
477 BadScopeFlags.CaptureContinue |
515 Parent?.SetContinue();
525 return m_Exports ??
Null;
530 if (m_Exports !=
null)
547 Parent.AddExport(key, value);
551 if (m_Exports ==
null)
556 m_Exports.SetProperty(key, value);
576 Parent?.SetReturnValue(value);
586 return m_ScopeVariables;
603 BadScope sc =
new BadScope(
this, caller, name, flags, useVisibility ?? m_UseVisibility)
605 ClassObject = ClassObject, FunctionObject = FunctionObject,
613 if (ClassObject ==
null)
618 if (m_Attributes.TryGetValue(name, out
BadObject[]? attributes))
645 if (ClassObject ==
null)
650 if (m_Attributes.TryGetValue(name, out
BadObject[]? attributes))
686 if (ClassObject ==
null)
691 foreach (KeyValuePair<
string,
BadObject[]> kvp
in m_Attributes)
721 if (HasLocal(name, caller.
Scope,
false))
726 Action<BadObject, BadSourcePosition?, BadPropertyInfo?>? setter =
null;
728 if (setAccessor !=
null)
730 setter = (value, p, pi) =>
741 foreach (
BadObject o
in setCtx.Execute(setAccessor))
748 m_ScopeVariables.PropertyInfos.Add(name,
new BadPropertyInfo(type, setter ==
null));
750 m_ScopeVariables.InnerTable.Add(name,
754 BadExecutionContext? getCtx =
755 new BadExecutionContext(caller.Scope
756 .CreateChild($
"get {name}",
761 BadObject? get = Null;
763 foreach (BadObject o in getCtx.Execute(getAccessor))
768 return get.Dereference(p);
774 m_Attributes[name] = attributes;
791 if (HasLocal(name, caller ??
this,
false))
796 m_Attributes[name] = attributes ?? [];
798 m_ScopeVariables.GetProperty(name,
false, caller ??
this)
799 .Set(value,
null, info,
true);
810 if (HasLocal(name,
this))
812 return m_ScopeVariables.GetPropertyInfo(name);
820 return Parent!.GetVariableInfo(name);
830 char first = propName[0];
831 char second = propName.Length > 1 ? propName[1] :
'\0';
854 while (current !=
null)
884 ? BadPropertyVisibility.All
887 if ((GetPropertyVisibility(name) & vis) == 0)
893 if (HasLocal(name, caller))
895 return m_ScopeVariables.GetProperty(name, caller);
903 return Parent!.GetVariable(name, caller);
914 return GetVariable(name,
this);
926 return !useExtensions
927 ? m_ScopeVariables.InnerTable.ContainsKey(name)
928 : m_ScopeVariables.HasProperty(name, caller);
939 return HasLocal(name, caller) || (Parent !=
null && Parent.HasVariable(name, caller));
946 return HasVariable(propName, caller ??
this)
947 ? GetVariable(propName, caller ??
this)
948 : base.GetProperty(propName, caller);
954 return HasVariable(propName, caller ??
this) || base.HasProperty(propName, caller);
963 return m_ScopeVariables.ToSafeString(done);
Base Implementation for all Expressions used inside the Script.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
BadScope(string name, BadInteropExtensionProvider provider, BadTable locals, BadScope? caller=null, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a new Scope.
readonly BadTable m_ScopeVariables
The Scope Variables.
BadScope GetRootScope()
Returns the Root Scope of the Scope.
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
BadScopeFlags Flags
The Scope Flags.
BadInteropExtensionProvider Provider
The Extension Provider.
BadScope? m_Caller
The Caller of the Current Scope.
readonly Dictionary< string, BadObject[]> m_Attributes
bool IsContinue
Is true if the Continue Keyword was set.
readonly Dictionary< Type, object > m_SingletonCache
The Singleton Cache.
BadObjectReference GetVariable(string name, BadScope caller)
Returns the variable reference of the specified variable.
BadTable GetTable()
Returns the Variable Table of the current scope.
readonly List< string > m_RegisteredApis
A List of Registered APIs.
override BadClassPrototype GetPrototype()
Returns the Class Prototype for the Scope.
bool HasVariable(string name, BadScope caller)
returns true if the specified variable is defined in the current scope or any parent scope
IEnumerable< BadObject > InitializeAttributes()
readonly bool m_UseVisibility
Indicates if the Scope uses the visibility subsystem.
void DefineProperty(string name, BadClassPrototype type, BadExpression getAccessor, BadExpression? setAccessor, BadExecutionContext caller, BadObject[] attributes)
bool IsVisibleParentOf(BadScope scope)
Returns true if the specified scope is visible to the current scope.
void SetContinue()
Sets the continue keyword inside this scope.
bool IsBreak
Is true if the Break Keyword was set.
string GetStackTrace()
Returns the Stack Trace of the Current scope.
static IEnumerable< BadScope > GetStackTraceEnumerable(BadScope scope)
BadScope? Parent
The Parent Scope.
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
override bool HasProperty(string propName, BadScope? caller=null)
Returns true if the object contains a given property or there exists an extension for the current Ins...
readonly BadInteropExtensionProvider m_Provider
The Extension Provider.
string Name
The Name of the Scope (for Debugging)
IReadOnlyCollection< string > RegisteredApis
A List of Registered APIs.
BadScope GetFirstTracableOrRoot()
BadScope(string name, BadInteropExtensionProvider provider, BadScope? caller=null, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a new Scope.
IReadOnlyDictionary< string, BadObject[]> Attributes
static BadScope CreateScope(BadExecutionContext ctx, string name, BadTable? locals=null)
Creates a Root Scope with the given name.
BadPropertyInfo GetVariableInfo(string name)
Returns the variable info of the specified variable.
bool OnChange(string name, BadObject oldValue, BadObject newValue)
BadClass? ClassObject
The Class Object of the Scope.
static BadClassPrototype Prototype
A Class Prototype for the Scope.
static string GetStackTrace(BadScope scope)
Returns the Stack Trace of the given Scope.
void Dispose()
Disposes the Scope and calls all finalizers.
void SetExports(BadExecutionContext ctx, BadObject exports)
void SetRegisteredApi(string api)
Registers an API.
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
void AddExport(string key, BadObject value)
Sets an exported key value pair in the scope.
readonly List< Action > m_Finalizers
The Finalizer List of the Scope.
BadObject? ReturnValue
The Return value of the scope.
void SetReturnValue(BadObject? value)
Sets the Return value of this scope.
BadObject? m_Exports
Contains the exported variables of the scope.
bool CountInStackTrace
Indicates if the Scope should count towards the Stack Trace.
override string ToSafeString(List< BadObject > done)
static BadScopeFlags ClearCaptures(BadScopeFlags flags)
Clears all Capture Flags from the given Flags.
void SetCaller(BadScope? caller)
Sets the Caller of the Scope.
void SetBreak()
Sets the break keyword inside this scope.
BadObjectReference GetVariable(string name)
Returns a variable reference of the specified variable.
void AddFinalizer(Action finalizer)
Adds a Finalizer to the Scope.
static BadPropertyVisibility GetPropertyVisibility(string propName)
Returns the visibility of the specified property.
BadObject GetExports()
Returns the exported key value pairs of the scope.
IEnumerable< BadScope > GetStackTraceEnumerable()
void DefineVariable(string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
Defines a new Variable in the current scope.
BadArray GetMemberInfos()
void OnChanged(string name, BadObject oldValue, BadObject newValue)
void SetFlags(BadScopeFlags flags)
Sets the Scope Flags.
BadScope(BadScope parent, BadScope? caller, string name, BadScopeFlags flags=BadScopeFlags.RootScope, bool useVisibility=false)
Creates a new Scope.
BadFunction? FunctionObject
The Function Object of the Scope.
Gets thrown by the runtime.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implements a Dynamic List/Array for the BadScript Language.
The Base Class for all BadScript Objects.
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? delete=null)
Creates a new Reference Object.
Stores Meta Information about a Property.
Implements a Table Structure for the BadScript Language.
void SetChangeInterceptor(Func< string, BadObject, BadObject, bool >? interceptor)
Implements a function that can be called from the script.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Type Instance in the BadScript Language.
BadObjectReference GetProperty(string propName, BadPropertyVisibility visibility, BadScope? caller=null)
Gets a property from this class or any of its base classes.
bool InheritsFrom(BadClassPrototype proto)
Returns true if the given object is an instance of the specified prototype.
Implements a Class Prototype for the BadScript Language.
Helper Class that Builds a Native Class from a Prototype.
static readonly BadInterfacePrototype ChangeAttribute
static readonly BadInterfacePrototype InitializeAttribute
static readonly BadInterfacePrototype ChangedAttribute
Implements a Native Class Prototype.
Implements the Interface for Native Strings.
Contains Shared Data Structures and Functionality.
Contains the Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Contains Runtime Type Objects.
BadPropertyVisibility
The Visibility of a Property.
Contains the Runtime Objects.
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.