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");
226#region IDisposable Members
240 foreach (Action finalizer
in m_Finalizers)
245 m_Finalizers.Clear();
265 Parent.SetRegisteredApi(api);
270 if (!m_RegisteredApis.Contains(api))
272 m_RegisteredApis.Add(api);
288 m_Finalizers.Add(finalizer);
315 public void AddSingleton<T>(T instance) where T :
class
317 if (instance ==
null)
322 m_SingletonCache.Add(typeof(T), instance);
330 public T? GetSingleton<T>()
334 return Parent.GetSingleton<T>();
337 if (m_SingletonCache.TryGetValue(typeof(T), out
object? value))
352 public T GetSingleton<T>(
bool createNew) where T :
new()
356 return Parent.GetSingleton<T>(createNew);
359 if (m_SingletonCache.ContainsKey(typeof(T)))
361 return (T)m_SingletonCache[typeof(T)];
366 throw new Exception(
"Singleton not found");
370 m_SingletonCache[typeof(T)] = v;
397 foreach (KeyValuePair<Type, object> kvp
in ctx.Scope.GetRootScope()
400 s.m_SingletonCache.Add(kvp.Key, kvp.Value);
421 return GetStackTrace(
this);
432 List<BadScope> stack =
new List<BadScope>();
434 while (current !=
null)
441 current = current.m_Caller ?? current.
Parent;
444 return string.Join(
"\n", stack.Select(s => s.Name));
451 while (current !=
null)
455 yield
return current;
458 current = current.m_Caller ?? current.
Parent;
474 ~(BadScopeFlags.CaptureReturn |
475 BadScopeFlags.CaptureBreak |
476 BadScopeFlags.CaptureContinue |
514 Parent?.SetContinue();
524 return m_Exports ??
Null;
529 if (m_Exports !=
null)
546 Parent.AddExport(key, value);
550 if (m_Exports ==
null)
555 m_Exports.SetProperty(key, value);
575 Parent?.SetReturnValue(value);
585 return m_ScopeVariables;
602 BadScope sc =
new BadScope(
this, caller, name, flags, useVisibility ?? m_UseVisibility)
604 ClassObject = ClassObject, FunctionObject = FunctionObject,
612 if (ClassObject ==
null)
617 if (m_Attributes.TryGetValue(name, out
BadObject[]? attributes))
644 if (ClassObject ==
null)
649 if (m_Attributes.TryGetValue(name, out
BadObject[]? attributes))
685 if (ClassObject ==
null)
690 foreach (KeyValuePair<
string,
BadObject[]> kvp
in m_Attributes)
720 if (HasLocal(name, caller.
Scope,
false))
725 Action<BadObject, BadSourcePosition?, BadPropertyInfo?>? setter =
null;
727 if (setAccessor !=
null)
729 setter = (value, p, pi) =>
740 foreach (
BadObject o
in setCtx.Execute(setAccessor))
747 m_ScopeVariables.PropertyInfos.Add(name,
new BadPropertyInfo(type, setter ==
null));
749 m_ScopeVariables.InnerTable.Add(name,
753 BadExecutionContext? getCtx =
754 new BadExecutionContext(caller.Scope
755 .CreateChild($
"get {name}",
760 BadObject? get = Null;
762 foreach (BadObject o in getCtx.Execute(getAccessor))
767 return get.Dereference(p);
773 m_Attributes[name] = attributes;
790 if (HasLocal(name, caller ??
this,
false))
795 m_Attributes[name] = attributes ?? [];
797 m_ScopeVariables.GetProperty(name,
false, caller ??
this)
798 .Set(value,
null, info,
true);
809 if (HasLocal(name,
this))
811 return m_ScopeVariables.GetPropertyInfo(name);
819 return Parent!.GetVariableInfo(name);
829 char first = propName[0];
830 char second = propName.Length > 1 ? propName[1] :
'\0';
853 while (current !=
null)
883 ? BadPropertyVisibility.All
886 if ((GetPropertyVisibility(name) & vis) == 0)
892 if (HasLocal(name, caller))
894 return m_ScopeVariables.GetProperty(name, caller);
902 return Parent!.GetVariable(name, caller);
913 return GetVariable(name,
this);
925 return !useExtensions
926 ? m_ScopeVariables.InnerTable.ContainsKey(name)
927 : m_ScopeVariables.HasProperty(name, caller);
938 return HasLocal(name, caller) || (Parent !=
null && Parent.HasVariable(name, caller));
945 return HasVariable(propName, caller ??
this)
946 ? GetVariable(propName, caller ??
this)
947 : base.GetProperty(propName, caller);
953 return HasVariable(propName, caller ??
this) || base.HasProperty(propName, caller);
962 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.