37 return base.HasProperty(propName, caller);
65 public bool Cancel {
get;
private set; }
76 if(propName ==
"Cancel")
80 return base.HasProperty(propName, caller);
85 if (propName ==
"Cancel")
157 return new BadArray(attributes.ToList());
174 else if(args.Length == 2)
191 case "GetAttributes":
198 return base.HasProperty(propName, caller);
208 case "GetAttributes":
231 return "MemberInfo: " +
m_Name;
259 private readonly Dictionary<string, BadObject[]>
m_Attributes =
new Dictionary<string, BadObject[]>();
272 private readonly Dictionary<Type, object>
m_SingletonCache =
new Dictionary<Type, object>();
309 m_ScopeVariables.OnChangedProperty +=
OnChanged;
333 m_ScopeVariables.OnChangedProperty +=
OnChanged;
349 bool useVisibility =
false) : this(
428 if (args[0] is not IBadString name)
430 throw new BadRuntimeException(
"Expected Name in Scope Constructor");
433 return CreateScope(c, name.Value);
466 foreach (Action finalizer
in m_Finalizers)
471 m_Finalizers.Clear();
482 Parent.SetRegisteredApi(api);
487 if (!m_RegisteredApis.Contains(api))
489 m_RegisteredApis.Add(api);
505 m_Finalizers.Add(finalizer);
532 public void AddSingleton<T>(T instance) where T :
class
534 if (instance ==
null)
539 m_SingletonCache.Add(typeof(T), instance);
547 public T? GetSingleton<T>()
551 return Parent.GetSingleton<T>();
554 if (m_SingletonCache.TryGetValue(typeof(T), out
object? value))
569 public T GetSingleton<T>(
bool createNew) where T :
new()
573 return Parent.GetSingleton<T>(createNew);
576 if (m_SingletonCache.ContainsKey(typeof(T)))
578 return (T)m_SingletonCache[typeof(T)];
583 throw new Exception(
"Singleton not found");
587 m_SingletonCache[typeof(T)] = v;
612 foreach (KeyValuePair<Type, object> kvp
in ctx.Scope.GetRootScope().m_SingletonCache)
614 s.m_SingletonCache.Add(kvp.Key, kvp.Value);
635 return GetStackTrace(
this);
646 List<BadScope> stack =
new List<BadScope>();
648 while (current !=
null)
655 current = current.m_Caller ?? current.
Parent;
658 return string.Join(
"\n", stack.Select(s => s.Name));
669 ~(BadScopeFlags.CaptureReturn |
670 BadScopeFlags.CaptureBreak |
671 BadScopeFlags.CaptureContinue |
709 Parent?.SetContinue();
719 return m_Exports ?? Null;
724 if (m_Exports !=
null)
741 Parent.AddExport(key, value);
745 if (m_Exports ==
null)
750 m_Exports.SetProperty(key, value);
770 Parent?.SetReturnValue(value);
780 return m_ScopeVariables;
798 BadScope sc =
new BadScope(
this, caller, name, flags, useVisibility ?? m_UseVisibility)
800 ClassObject = ClassObject,
801 FunctionObject = FunctionObject,
808 if (ClassObject ==
null)
812 if (m_Attributes.TryGetValue(name, out var attributes))
831 if (ClassObject ==
null)
835 if (m_Attributes.TryGetValue(name, out var attributes))
863 if (ClassObject ==
null)
867 foreach (var kvp
in m_Attributes)
887 if (HasLocal(name, caller.
Scope,
false))
891 Action<BadObject, BadPropertyInfo?>? setter =
null;
892 if (setAccessor !=
null)
894 setter = (value, pi) =>
898 foreach (
BadObject o
in setCtx.Execute(setAccessor))
904 m_ScopeVariables.PropertyInfos.Add(name,
new BadPropertyInfo(type, setter ==
null));
908 BadExecutionContext? getCtx = new BadExecutionContext(caller.Scope.CreateChild($
"get {name}", caller.Scope, null));
909 BadObject? get = Null;
910 foreach (BadObject o in getCtx.Execute(getAccessor))
914 return get.Dereference();
918 m_Attributes[name] = attributes;
931 if (HasLocal(name, caller ??
this,
false))
936 m_Attributes[name] = attributes ?? [];
937 m_ScopeVariables.GetProperty(name,
false, caller ??
this).Set(value, info,
true);
948 if (HasLocal(name,
this))
950 return m_ScopeVariables.GetPropertyInfo(name);
958 return Parent!.GetVariableInfo(name);
968 char first = propName[0];
969 char second = propName.Length > 1 ? propName[1] :
'\0';
992 while (current !=
null)
1005 current = current.
Parent;
1020 if (m_UseVisibility)
1024 if ((GetPropertyVisibility(name) & vis) == 0)
1030 if (HasLocal(name, caller))
1032 return m_ScopeVariables.GetProperty(name, caller);
1040 return Parent!.GetVariable(name, caller);
1051 return GetVariable(name,
this);
1063 return !useExtensions ? m_ScopeVariables.InnerTable.ContainsKey(name) : m_ScopeVariables.HasProperty(name, caller);
1074 return HasLocal(name, caller) || Parent !=
null && Parent.HasVariable(name, caller);
1081 return HasVariable(propName, caller ??
this) ? GetVariable(propName, caller ??
this) : base.GetProperty(propName, caller);
1087 return HasVariable(propName, caller ??
this) || base.HasProperty(propName, caller);
1096 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.
readonly BadObject m_OldValue
override string ToSafeString(List< BadObject > done)
readonly BadMemberInfo m_Member
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
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...
BadMemberChangeEvent(BadObject mInstance, BadMemberInfo mMember, BadObject mOldValue, BadObject mNewValue)
readonly BadObject m_Instance
readonly BadObject m_NewValue
BadMemberChangedEvent(BadObject mInstance, BadMemberInfo mMember, BadObject mOldValue, BadObject mNewValue)
override BadClassPrototype GetPrototype()
override BadClassPrototype GetPrototype()
readonly BadObjectReference m_CancelReference
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...
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
BadMemberChangingEvent(BadObject mInstance, BadMemberInfo mMember, BadObject mOldValue, BadObject mNewValue)
BadObject SetValue(BadExecutionContext ctx, BadObject[] args)
static BadClassPrototype Prototype
readonly BadObjectReference m_MemberTypeReference
readonly BadObjectReference m_NameReference
readonly BadObjectReference m_GetValueReference
BadObject GetValue(BadExecutionContext ctx, BadObject[] args)
override BadClassPrototype GetPrototype()
BadMemberInfo(string name, BadScope scope)
readonly BadObjectReference m_GetAttributesReference
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 BadObjectReference m_SetValueReference
override string ToSafeString(List< BadObject > done)
BadObject GetAttributes(BadExecutionContext ctx, BadObject[] args)
readonly BadObjectReference m_IsReadonlyReference
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
readonly BadScope m_Scope
static readonly BadNativeClassPrototype< BadMemberInfo > s_Prototype
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.
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(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.
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.
Interop Function taking an array of arguments.
Implements a Dynamic List/Array for the BadScript Language.
static BadClassPrototype Prototype
The Prototype for the BadScript Array.
The Base Class for all BadScript Objects.
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
static readonly BadObject Null
The Null Value for the BadScript Language.
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
void Set(BadObject obj, BadPropertyInfo? info=null, bool noChangeEvent=false)
Sets the Referenced Object to a new Value.
Stores Meta Information about a Property.
bool IsReadOnly
Indicates if this property is read only.
BadClassPrototype? Type
The (optional) Type used for typechecking if a value gets assigned to this property.
Implements a Table Structure for the BadScript Language.
void SetChangeInterceptor(Func< string, BadObject, BadObject, bool >? interceptor)
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Implements a function that can be called from the script.
Provides function parameter info.
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 MemberChangedEventArgs
static readonly BadInterfacePrototype MemberChangingEventArgs
static readonly BadInterfacePrototype ChangeAttribute
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
static readonly BadInterfacePrototype InitializeAttribute
static readonly BadInterfacePrototype ChangedAttribute
Implements a Native Class Prototype.
Implements the Interface for Native Boolean.
Implements the Interface for Native Strings.
Contains the Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes 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.