BadScript 2
Loading...
Searching...
No Matches
BadScript2.Runtime.BadScope Class Reference

Implements the Scope for the Script Engine. More...

Inheritance diagram for BadScript2.Runtime.BadScope:
BadScript2.Runtime.Objects.BadObject

Public Member Functions

BadArray GetMemberInfos ()
 
 BadScope (string name, BadInteropExtensionProvider provider, BadScope? caller=null, BadScopeFlags flags=BadScopeFlags.RootScope)
 Creates a new Scope.
 
 BadScope (string name, BadInteropExtensionProvider provider, BadTable locals, BadScope? caller=null, BadScopeFlags flags=BadScopeFlags.RootScope)
 Creates a new Scope.
 
void Dispose ()
 Disposes the Scope and calls all finalizers.
 
void AddFinalizer (Action finalizer)
 Adds a Finalizer to the Scope.
 
void SetCaller (BadScope? caller)
 Sets the Caller of the Scope.
 
BadScope GetRootScope ()
 Returns the Root Scope of the Scope.
 
void AddSingleton< T > (T instance)
 Adds a Singleton to the Scope.
 
T? GetSingleton< T > ()
 Gets a Singleton from the Scope.
 
GetSingleton< T > (bool createNew)
 Gets a Singleton from the Scope.
 
override BadClassPrototype GetPrototype ()
 Returns the Class Prototype for the Scope.
 
void SetFlags (BadScopeFlags flags)
 Sets the Scope Flags.
 
string GetStackTrace ()
 Returns the Stack Trace of the Current scope.
 
void SetBreak ()
 Sets the break keyword inside this scope.
 
void SetContinue ()
 Sets the continue keyword inside this scope.
 
BadObject GetExports ()
 Returns the exported key value pairs of the scope.
 
void SetExports (BadExecutionContext ctx, BadObject exports)
 
void AddExport (string key, BadObject value)
 Sets an exported key value pair in the scope.
 
void SetReturnValue (BadObject? value)
 Sets the Return value of this scope.
 
BadTable GetTable ()
 Returns the Variable Table of the current scope.
 
BadScope CreateChild (string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
 Creates a subscope of the current scope.
 
void DefineProperty (string name, BadClassPrototype type, BadExpression getAccessor, BadExpression? setAccessor, BadExecutionContext caller, BadObject[] attributes)
 
void DefineVariable (string name, BadObject value, BadScope? caller=null, BadPropertyInfo? info=null, BadObject[]? attributes=null)
 Defines a new Variable in the current scope.
 
BadPropertyInfo GetVariableInfo (string name)
 Returns the variable info of the specified variable.
 
bool IsVisibleParentOf (BadScope scope)
 Returns true if the specified scope is visible to the current scope.
 
BadObjectReference GetVariable (string name, BadScope caller)
 Returns the variable reference of the specified variable.
 
BadObjectReference GetVariable (string name)
 Returns a variable reference of the specified variable.
 
bool HasLocal (string name, BadScope caller, bool useExtensions=true)
 returns true if the specified variable is defined in the current scope
 
bool HasVariable (string name, BadScope caller)
 returns true if the specified variable is defined in the current scope or any parent scope
 
override BadObjectReference GetProperty (string propName, BadScope? caller=null)
 Returns a Reference to the Property with the given Name.
Parameters
propNameThe Property Name
callerThe caller Scope
Returns
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 Instance.
Parameters
propNameThe Property Name
callerThe caller Scope
Returns
True if the Property or an Extension with that name exists

 
override string ToSafeString (List< BadObject > done)
 
- Public Member Functions inherited from BadScript2.Runtime.Objects.BadObject
BadClassPrototype GetPrototype ()
 Returns the Prototype of this Object.
 
string ToSafeString (List< BadObject > done)
 Returns a String Representation of this Object. This function is recursion proof and supports circular references.
 
override string ToString ()
 Returns a String Representation of this Object.
 

Static Public Member Functions

static BadPropertyVisibility GetPropertyVisibility (string propName)
 Returns the visibility of the specified property.
 
- Static Public Member Functions inherited from BadScript2.Runtime.Objects.BadObject
static bool CanWrap (object? o)
 Returns true if the given object cam be wrapped.
 
static BadObject Wrap< T > (T obj, bool allowNative=true)
 Wraps the given object into a BadObject Instance.
 
static implicit operator BadObject (bool b)
 Implicit Converstion from Boolean to BadObject.
 
static implicit operator BadObject (BadNullable< bool > b)
 Converts the given object to a BadObject Instance.
 
static implicit operator BadObject (decimal d)
 Implicit Converstion from Number to BadObject.
 
static implicit operator BadObject (BadNullable< decimal > b)
 Converts the given object to a BadObject Instance.
 
static implicit operator BadObject (string s)
 Implicit Converstion from String to BadObject.
 
static implicit operator BadObject (BadNullable< string > b)
 Converts the given object to a BadObject Instance.
 

Package Functions

void SetRegisteredApi (string api)
 Registers an API.
 
void OnChanged (string name, BadObject oldValue, BadObject newValue)
 
bool OnChange (string name, BadObject oldValue, BadObject newValue)
 
IEnumerable< BadObjectInitializeAttributes ()
 

Properties

IReadOnlyDictionary< string, BadObject[]> Attributes [get]
 
IReadOnlyCollection< string > RegisteredApis [get]
 A List of Registered APIs.
 
BadInteropExtensionProvider Provider [get]
 The Extension Provider.
 
BadClassClassObject [get, set]
 The Class Object of the Scope.
 
BadFunctionFunctionObject [get, set]
 The Function Object of the Scope.
 
BadScopeParent [get]
 The Parent Scope.
 
string Name [get]
 The Name of the Scope (for Debugging)
 
BadScopeFlags Flags [get, private set]
 The Scope Flags.
 
bool CountInStackTrace [get]
 Indicates if the Scope should count towards the Stack Trace.
 
bool IsBreak [get, private set]
 Is true if the Break Keyword was set.
 
bool IsContinue [get, private set]
 Is true if the Continue Keyword was set.
 
BadObjectReturnValue [get, private set]
 The Return value of the scope.
 
static BadClassPrototype Prototype [get]
 A Class Prototype for the Scope.
 

Private Member Functions

 BadScope (BadScope parent, BadScope? caller, string name, BadScopeFlags flags=BadScopeFlags.RootScope, bool useVisibility=false)
 Creates a new Scope.
 

Static Private Member Functions

static BadScope CreateScope (BadExecutionContext ctx, string name, BadTable? locals=null)
 Creates a Root Scope with the given name.
 
static string GetStackTrace (BadScope scope)
 Returns the Stack Trace of the given Scope.
 
static BadScopeFlags ClearCaptures (BadScopeFlags flags)
 Clears all Capture Flags from the given Flags.
 

Private Attributes

readonly List< Action > m_Finalizers = new List<Action>()
 The Finalizer List of the Scope.
 
readonly BadInteropExtensionProvider m_Provider
 The Extension Provider.
 
readonly List< string > m_RegisteredApis = new List<string>()
 A List of Registered APIs.
 
readonly BadTable m_ScopeVariables = new BadTable()
 The Scope Variables.
 
readonly Dictionary< string, BadObject[]> m_Attributes = new Dictionary<string, BadObject[]>()
 
readonly Dictionary< Type, object > m_SingletonCache = new Dictionary<Type, object>()
 The Singleton Cache.
 
readonly bool m_UseVisibility
 Indicates if the Scope uses the visibility subsystem.
 
BadScopem_Caller
 The Caller of the Current Scope.
 
BadObjectm_Exports
 Contains the exported variables of the scope.
 
bool m_IsDisposed
 

Additional Inherited Members

- Static Public Attributes inherited from BadScript2.Runtime.Objects.BadObject
static readonly BadObject Null = new BadNullObject()
 The Null Value for the BadScript Language.
 
static readonly BadObject True = new BadBoolean(true)
 The True Value for the BadScript Language.
 
static readonly BadObject False = new BadBoolean(false)
 The False Value for the BadScript Language.
 

Detailed Description

Implements the Scope for the Script Engine.

Definition at line 237 of file BadScope.cs.

Constructor & Destructor Documentation

◆ BadScope() [1/3]

BadScript2.Runtime.BadScope.BadScope ( string  name,
BadInteropExtensionProvider  provider,
BadScope caller = null,
BadScopeFlags  flags = BadScopeFlags::RootScope 
)

Creates a new Scope.

Parameters
nameThe Name of the Scope
providerThe Extension Provider of the Runtime
callerThe Caller of the Scope
flagsThe Flags of the Scope

Definition at line 298 of file BadScope.cs.

303 {
304 Name = name;
305 Flags = flags;
306 m_Caller = caller;
307 m_Provider = provider;
309 m_ScopeVariables.OnChangedProperty += OnChanged;
310 }
readonly BadTable m_ScopeVariables
The Scope Variables.
Definition BadScope.cs:257
BadScopeFlags Flags
The Scope Flags.
Definition BadScope.cs:394
BadScope? m_Caller
The Caller of the Current Scope.
Definition BadScope.cs:282
readonly BadInteropExtensionProvider m_Provider
The Extension Provider.
Definition BadScope.cs:247
string Name
The Name of the Scope (for Debugging)
Definition BadScope.cs:389
bool OnChange(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:829
void OnChanged(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:806
void SetChangeInterceptor(Func< string, BadObject, BadObject, bool >? interceptor)
Definition BadTable.cs:19

◆ BadScope() [2/3]

BadScript2.Runtime.BadScope.BadScope ( string  name,
BadInteropExtensionProvider  provider,
BadTable  locals,
BadScope caller = null,
BadScopeFlags  flags = BadScopeFlags::RootScope 
)

Creates a new Scope.

Parameters
nameThe Name of the Scope
providerThe Extension Provider of the Runtime
localsThe Local Variables
callerThe Caller of the Scope
flagsThe Flags of the Scope

Definition at line 320 of file BadScope.cs.

326 {
327 Name = name;
328 Flags = flags;
329 m_Caller = caller;
330 m_ScopeVariables = locals;
331 m_Provider = provider;
333 m_ScopeVariables.OnChangedProperty += OnChanged;
334 }

◆ BadScope() [3/3]

BadScript2.Runtime.BadScope.BadScope ( BadScope  parent,
BadScope caller,
string  name,
BadScopeFlags  flags = BadScopeFlags::RootScope,
bool  useVisibility = false 
)
private

Creates a new Scope.

Parameters
parentThe Parent Scope
callerThe Caller of the Scope
nameThe Name of the Scope
flagsThe Flags of the Scope
useVisibilityDoes the Scope use the Visibility Subsystem

Definition at line 344 of file BadScope.cs.

349 : this(
350 name,
351 parent.Provider,
352 caller,
353 ClearCaptures(parent.Flags) | flags
354 )
355 {
356 m_UseVisibility = useVisibility;
357 Parent = parent;
358 }
readonly bool m_UseVisibility
Indicates if the Scope uses the visibility subsystem.
Definition BadScope.cs:277
BadScope? Parent
The Parent Scope.
Definition BadScope.cs:384
static BadScopeFlags ClearCaptures(BadScopeFlags flags)
Clears all Capture Flags from the given Flags.
Definition BadScope.cs:666

Member Function Documentation

◆ AddExport()

void BadScript2.Runtime.BadScope.AddExport ( string  key,
BadObject  value 
)

Sets an exported key value pair in the scope.

Parameters
keyThe Key
valueThe Value

Definition at line 737 of file BadScope.cs.

738 {
739 if (Parent != null)
740 {
741 Parent.AddExport(key, value);
742 }
743 else
744 {
745 if (m_Exports == null)
746 {
747 m_Exports = new BadTable();
748 }
749
750 m_Exports.SetProperty(key, value);
751 }
752 }
void AddExport(string key, BadObject value)
Sets an exported key value pair in the scope.
Definition BadScope.cs:737
BadObject? m_Exports
Contains the exported variables of the scope.
Definition BadScope.cs:287
Implements a Table Structure for the BadScript Language.
Definition BadTable.cs:14

◆ AddFinalizer()

void BadScript2.Runtime.BadScope.AddFinalizer ( Action  finalizer)

Adds a Finalizer to the Scope.

Parameters
finalizerThe Finalizer
Exceptions
BadRuntimeExceptionGets raised if the Scope is already disposed

Definition at line 498 of file BadScope.cs.

499 {
500 if (m_IsDisposed)
501 {
502 throw BadRuntimeException.Create(this, "Scope is already disposed");
503 }
504
505 m_Finalizers.Add(finalizer);
506 }
readonly List< Action > m_Finalizers
The Finalizer List of the Scope.
Definition BadScope.cs:242
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.

◆ AddSingleton< T >()

void BadScript2.Runtime.BadScope.AddSingleton< T > ( instance)

Adds a Singleton to the Scope.

Parameters
instance
Template Parameters
T
Exceptions
BadRuntimeException
Type Constraints
T :class 

Definition at line 532 of file BadScope.cs.

532 : class
533 {
534 if (instance == null)
535 {
536 throw new BadRuntimeException("Cannot add null as singleton");
537 }
538
539 m_SingletonCache.Add(typeof(T), instance);
540 }
readonly Dictionary< Type, object > m_SingletonCache
The Singleton Cache.
Definition BadScope.cs:272

◆ ClearCaptures()

static BadScopeFlags BadScript2.Runtime.BadScope.ClearCaptures ( BadScopeFlags  flags)
staticprivate

Clears all Capture Flags from the given Flags.

Parameters
flagsThe Flags to be cleared
Returns
Cleared Flags

Definition at line 666 of file BadScope.cs.

667 {
668 return flags &
669 ~(BadScopeFlags.CaptureReturn |
670 BadScopeFlags.CaptureBreak |
671 BadScopeFlags.CaptureContinue |
672 BadScopeFlags.CaptureThrow);
673 }
BadScopeFlags
Defines Different Behaviours for the Current Scope.

◆ CreateChild()

BadScope BadScript2.Runtime.BadScope.CreateChild ( string  name,
BadScope caller,
bool?  useVisibility,
BadScopeFlags  flags = BadScopeFlags::RootScope 
)

Creates a subscope of the current scope.

Parameters
nameScope Name
callerThe Caller
useVisibilitySpecifies if the scope is part of a class structure(if visibility flags are used)
flagsScope Flags
Returns
New BadScope Instance

Definition at line 792 of file BadScope.cs.

797 {
798 BadScope sc = new BadScope(this, caller, name, flags, useVisibility ?? m_UseVisibility)
799 {
802 };
803
804 return sc;
805 }
BadScope(string name, BadInteropExtensionProvider provider, BadScope? caller=null, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a new Scope.
Definition BadScope.cs:298
BadClass? ClassObject
The Class Object of the Scope.
Definition BadScope.cs:373
BadFunction? FunctionObject
The Function Object of the Scope.
Definition BadScope.cs:378

◆ CreateScope()

static BadScope BadScript2.Runtime.BadScope.CreateScope ( BadExecutionContext  ctx,
string  name,
BadTable locals = null 
)
staticprivate

Creates a Root Scope with the given name.

Parameters
ctxThe Calling Context
nameScope Name
localsThe Local Variables of the new Scope
Returns
New Scope Instance

Definition at line 608 of file BadScope.cs.

609 {
610 BadScope s = locals != null ? new BadScope(name, ctx.Scope.Provider, locals) : new BadScope(name, ctx.Scope.Provider);
611
612 foreach (KeyValuePair<Type, object> kvp in ctx.Scope.GetRootScope().m_SingletonCache)
613 {
614 s.m_SingletonCache.Add(kvp.Key, kvp.Value);
615 }
616
617 return s;
618 }
BadInteropExtensionProvider Provider
The Extension Provider.
Definition BadScope.cs:368

◆ DefineProperty()

void BadScript2.Runtime.BadScope.DefineProperty ( string  name,
BadClassPrototype  type,
BadExpression  getAccessor,
BadExpression setAccessor,
BadExecutionContext  caller,
BadObject[]  attributes 
)

Definition at line 885 of file BadScope.cs.

886 {
887 if (HasLocal(name, caller.Scope, false))
888 {
889 throw new BadRuntimeException($"Property {name} is already defined");
890 }
891 Action<BadObject, BadPropertyInfo?>? setter = null;
892 if (setAccessor != null)
893 {
894 setter = (value, pi) =>
895 {
896 BadExecutionContext? setCtx = new BadExecutionContext(caller.Scope.CreateChild($"set {name}", caller.Scope, null));
897 setCtx.Scope.DefineVariable("value", value, setCtx.Scope, new BadPropertyInfo(BadAnyPrototype.Instance, true));
898 foreach (BadObject o in setCtx.Execute(setAccessor))
899 {
900 //Execute
901 }
902 };
903 }
904 m_ScopeVariables.PropertyInfos.Add(name, new BadPropertyInfo(type, setter == null));
905 m_ScopeVariables.InnerTable.Add(name, BadObjectReference.Make($"property {name}",
906 () =>
907 {
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))
911 {
912 get = o;
913 }
914 return get.Dereference();
915 },
916 setter));
917
918 m_Attributes[name] = attributes;
919 }
readonly Dictionary< string, BadObject[]> m_Attributes
Definition BadScope.cs:259
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
Definition BadScope.cs:1061
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
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.
Stores Meta Information about a Property.
Dictionary< string, BadPropertyInfo > PropertyInfos
A Table of additional property information.
Definition BadTable.cs:65
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Definition BadTable.cs:60
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.

◆ DefineVariable()

void BadScript2.Runtime.BadScope.DefineVariable ( string  name,
BadObject  value,
BadScope caller = null,
BadPropertyInfo info = null,
BadObject?[]  attributes = null 
)

Defines a new Variable in the current scope.

Parameters
nameVariable Name
valueVariable Value
callerThe Caller of the Scope
infoVariable Info
Exceptions
BadRuntimeExceptionGets raised if the specified variable is already defined.

Definition at line 929 of file BadScope.cs.

930 {
931 if (HasLocal(name, caller ?? this, false))
932 {
933 throw new BadRuntimeException($"Variable {name} is already defined");
934 }
935
936 m_Attributes[name] = attributes ?? [];
937 m_ScopeVariables.GetProperty(name, false, caller ?? this).Set(value, info, true);
938 }
void Set(BadObject obj, BadPropertyInfo? info=null, bool noChangeEvent=false)
Sets the Referenced Object to a new Value.
BadObjectReference GetProperty(string propName, bool useExtensions, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadTable.cs:142

◆ Dispose()

void BadScript2.Runtime.BadScope.Dispose ( )

Disposes the Scope and calls all finalizers.

Definition at line 458 of file BadScope.cs.

459 {
460 if (m_IsDisposed)
461 {
462 return;
463 }
464
465 m_IsDisposed = true;
466 foreach (Action finalizer in m_Finalizers)
467 {
468 finalizer();
469 }
470
471 m_Finalizers.Clear();
472 }

◆ GetExports()

BadObject BadScript2.Runtime.BadScope.GetExports ( )

Returns the exported key value pairs of the scope.

Returns
BadTable with all exported variables

Definition at line 717 of file BadScope.cs.

718 {
719 return m_Exports ?? Null;
720 }
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28

◆ GetMemberInfos()

BadArray BadScript2.Runtime.BadScope.GetMemberInfos ( )

Definition at line 263 of file BadScope.cs.

264 {
265 return new BadArray(
266 m_ScopeVariables.InnerTable.Keys.Select(x => (BadObject)new BadMemberInfo(x, this)).ToList());
267 }
Implements a Dynamic List/Array for the BadScript Language.
Definition BadArray.cs:17

◆ GetProperty()

override BadObjectReference BadScript2.Runtime.BadScope.GetProperty ( string  propName,
BadScope caller = null 
)
virtual

Returns a Reference to the Property with the given Name.

Parameters
propNameThe Property Name
callerThe caller Scope
Returns
The Property Reference

Reimplemented from BadScript2.Runtime.Objects.BadObject.

Definition at line 1079 of file BadScope.cs.

1080 {
1081 return HasVariable(propName, caller ?? this) ? GetVariable(propName, caller ?? this) : base.GetProperty(propName, caller);
1082 }
BadObjectReference GetVariable(string name, BadScope caller)
Returns the variable reference of the specified variable.
Definition BadScope.cs:1018
bool HasVariable(string name, BadScope caller)
returns true if the specified variable is defined in the current scope or any parent scope
Definition BadScope.cs:1072
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadScope.cs:1079

◆ GetPropertyVisibility()

static BadPropertyVisibility BadScript2.Runtime.BadScope.GetPropertyVisibility ( string  propName)
static

Returns the visibility of the specified property.

Parameters
propNameThe Property Name
Returns
The Visibility of the Property

Definition at line 966 of file BadScope.cs.

967 {
968 char first = propName[0];
969 char second = propName.Length > 1 ? propName[1] : '\0';
970
971 return second switch
972 {
973 '_' => first == '_' ? BadPropertyVisibility.Private : BadPropertyVisibility.Public,
974 _ => first == '_' ? BadPropertyVisibility.Protected : BadPropertyVisibility.Public,
975 };
976 }
BadPropertyVisibility
The Visibility of a Property.

◆ GetPrototype()

override BadClassPrototype BadScript2.Runtime.BadScope.GetPrototype ( )

Returns the Class Prototype for the Scope.

Returns
BadClassPrototype

Definition at line 596 of file BadScope.cs.

597 {
598 return Prototype;
599 }
static BadClassPrototype Prototype
A Class Prototype for the Scope.
Definition BadScope.cs:420

◆ GetRootScope()

BadScope BadScript2.Runtime.BadScope.GetRootScope ( )

Returns the Root Scope of the Scope.

Returns
The Root Scope

Definition at line 521 of file BadScope.cs.

522 {
523 return Parent?.GetRootScope() ?? this;
524 }
BadScope GetRootScope()
Returns the Root Scope of the Scope.
Definition BadScope.cs:521

◆ GetSingleton< T >() [1/2]

T? BadScript2.Runtime.BadScope.GetSingleton< T > ( )

Gets a Singleton from the Scope.

Template Parameters
TType of the Singleton
Returns
The Singleton

Definition at line 547 of file BadScope.cs.

548 {
549 if (Parent != null)
550 {
551 return Parent.GetSingleton<T>();
552 }
553
554 if (m_SingletonCache.TryGetValue(typeof(T), out object? value))
555 {
556 return (T)value;
557 }
558
559 return default;
560 }

◆ GetSingleton< T >() [2/2]

T BadScript2.Runtime.BadScope.GetSingleton< T > ( bool  createNew)

Gets a Singleton from the Scope.

Parameters
createNewShould a new instance be created if the singleton does not exist
Template Parameters
TType of the Singleton
Returns
The Singleton
Exceptions
ExceptionGets raised if the singleton does not exist and createNew is false
Type Constraints
T :new() 

Definition at line 569 of file BadScope.cs.

569 : new()
570 {
571 if (Parent != null)
572 {
573 return Parent.GetSingleton<T>(createNew);
574 }
575
576 if (m_SingletonCache.ContainsKey(typeof(T)))
577 {
578 return (T)m_SingletonCache[typeof(T)];
579 }
580
581 if (!createNew)
582 {
583 throw new Exception("Singleton not found");
584 }
585
586 T v = new T();
587 m_SingletonCache[typeof(T)] = v;
588
589 return v;
590 }

◆ GetStackTrace() [1/2]

string BadScript2.Runtime.BadScope.GetStackTrace ( )

Returns the Stack Trace of the Current scope.

Returns
Stack Trace

Definition at line 633 of file BadScope.cs.

634 {
635 return GetStackTrace(this);
636 }
string GetStackTrace()
Returns the Stack Trace of the Current scope.
Definition BadScope.cs:633

◆ GetStackTrace() [2/2]

static string BadScript2.Runtime.BadScope.GetStackTrace ( BadScope  scope)
staticprivate

Returns the Stack Trace of the given Scope.

Parameters
scopeThe Scope
Returns
Stack Trace

Definition at line 643 of file BadScope.cs.

644 {
645 BadScope? current = scope;
646 List<BadScope> stack = new List<BadScope>();
647
648 while (current != null)
649 {
650 if (current.CountInStackTrace)
651 {
652 stack.Add(current);
653 }
654
655 current = current.m_Caller ?? current.Parent;
656 }
657
658 return string.Join("\n", stack.Select(s => s.Name));
659 }

◆ GetTable()

BadTable BadScript2.Runtime.BadScope.GetTable ( )

Returns the Variable Table of the current scope.

Returns
BadTable with all local variables

Definition at line 778 of file BadScope.cs.

779 {
780 return m_ScopeVariables;
781 }

◆ GetVariable() [1/2]

BadObjectReference BadScript2.Runtime.BadScope.GetVariable ( string  name)

Returns a variable reference of the specified variable.

Parameters
nameVariable Name
Returns
Variable Reference
Exceptions
BadRuntimeExceptionGets raised if the variable can not be found

Definition at line 1049 of file BadScope.cs.

1050 {
1051 return GetVariable(name, this);
1052 }

◆ GetVariable() [2/2]

BadObjectReference BadScript2.Runtime.BadScope.GetVariable ( string  name,
BadScope  caller 
)

Returns the variable reference of the specified variable.

Parameters
nameVariable Name
callerThe Calling Scope
Returns
Variable Reference
Exceptions
BadRuntimeExceptionGets raised if the variable can not be found or is not visible

Definition at line 1018 of file BadScope.cs.

1019 {
1020 if (m_UseVisibility)
1021 {
1022 BadPropertyVisibility vis = IsVisibleParentOf(caller) ? BadPropertyVisibility.All : BadPropertyVisibility.Public;
1023
1024 if ((GetPropertyVisibility(name) & vis) == 0)
1025 {
1026 throw BadRuntimeException.Create(caller, $"Variable '{name}' is not visible to {caller}");
1027 }
1028 }
1029
1030 if (HasLocal(name, caller))
1031 {
1032 return m_ScopeVariables.GetProperty(name, caller);
1033 }
1034
1035 if (Parent == null)
1036 {
1037 throw BadRuntimeException.Create(caller, $"Variable '{name}' is not defined");
1038 }
1039
1040 return Parent!.GetVariable(name, caller);
1041 }
bool IsVisibleParentOf(BadScope scope)
Returns true if the specified scope is visible to the current scope.
Definition BadScope.cs:983
static BadPropertyVisibility GetPropertyVisibility(string propName)
Returns the visibility of the specified property.
Definition BadScope.cs:966

◆ GetVariableInfo()

BadPropertyInfo BadScript2.Runtime.BadScope.GetVariableInfo ( string  name)

Returns the variable info of the specified variable.

Parameters
nameVariable Name
Returns
Variable Info
Exceptions
BadRuntimeExceptionGets raised if the variable can not be found

Definition at line 946 of file BadScope.cs.

947 {
948 if (HasLocal(name, this))
949 {
951 }
952
953 if (Parent == null)
954 {
955 throw new BadRuntimeException($"Variable '{name}' is not defined");
956 }
957
958 return Parent!.GetVariableInfo(name);
959 }
BadPropertyInfo GetVariableInfo(string name)
Returns the variable info of the specified variable.
Definition BadScope.cs:946
BadPropertyInfo GetPropertyInfo(string propName)
Returns Property Information for a given Key.
Definition BadTable.cs:111

◆ HasLocal()

bool BadScript2.Runtime.BadScope.HasLocal ( string  name,
BadScope  caller,
bool  useExtensions = true 
)

returns true if the specified variable is defined in the current scope

Parameters
nameThe Name
callerThe Caller of the Scope
useExtensionsShould the Extension Subsystem be searched for the property
Returns
true if the variable is defined

Definition at line 1061 of file BadScope.cs.

1062 {
1063 return !useExtensions ? m_ScopeVariables.InnerTable.ContainsKey(name) : m_ScopeVariables.HasProperty(name, caller);
1064 }
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...
Definition BadTable.cs:129

◆ HasProperty()

override bool BadScript2.Runtime.BadScope.HasProperty ( string  propName,
BadScope caller = null 
)
virtual

Returns true if the object contains a given property or there exists an extension for the current Instance.

Parameters
propNameThe Property Name
callerThe caller Scope
Returns
True if the Property or an Extension with that name exists

Reimplemented from BadScript2.Runtime.Objects.BadObject.

Definition at line 1085 of file BadScope.cs.

1086 {
1087 return HasVariable(propName, caller ?? this) || base.HasProperty(propName, caller);
1088 }

◆ HasVariable()

bool BadScript2.Runtime.BadScope.HasVariable ( string  name,
BadScope  caller 
)

returns true if the specified variable is defined in the current scope or any parent scope

Parameters
nameThe Name
callerThe Caller of the Scope
Returns
true if the variable is defined

Definition at line 1072 of file BadScope.cs.

1073 {
1074 return HasLocal(name, caller) || Parent != null && Parent.HasVariable(name, caller);
1075 }

◆ InitializeAttributes()

IEnumerable< BadObject > BadScript2.Runtime.BadScope.InitializeAttributes ( )
package

Definition at line 861 of file BadScope.cs.

862 {
863 if (ClassObject == null)
864 {
865 throw new BadRuntimeException("Scope is not a class scope");
866 }
867 foreach (var kvp in m_Attributes)
868 {
869 var member = new BadMemberInfo(kvp.Key, this);
870 foreach (BadClass attribute in kvp.Value.OfType<BadClass>())
871 {
873 {
874 var invoke = (BadFunction)attribute.GetProperty("Initialize").Dereference();
875 foreach (BadObject o in invoke.Invoke(new BadObject[] { ClassObject!, member }, new BadExecutionContext(this)))
876 {
877 //Execute
878 yield return o;
879 }
880 }
881 }
882 }
883 }
Implements a function that can be called from the script.
Implements a Type Instance in the BadScript Language.
Definition BadClass.cs:11
BadObjectReference GetProperty(string propName, BadPropertyVisibility visibility, BadScope? caller=null)
Gets a property from this class or any of its base classes.
Definition BadClass.cs:116
bool InheritsFrom(BadClassPrototype proto)
Returns true if the given object is an instance of the specified prototype.
Definition BadClass.cs:60
Helper Class that Builds a Native Class from a Prototype.
static readonly BadInterfacePrototype InitializeAttribute

◆ IsVisibleParentOf()

bool BadScript2.Runtime.BadScope.IsVisibleParentOf ( BadScope  scope)

Returns true if the specified scope is visible to the current scope.

Parameters
scopeThe Scope
Returns
true if the scope is visible

Definition at line 983 of file BadScope.cs.

984 {
985 if (scope == this)
986 {
987 return true;
988 }
989
990 BadScope? current = scope.Parent;
991
992 while (current != null)
993 {
994 if (!current.m_UseVisibility)
995 {
996 return false;
997 }
998
999 if (current == this)
1000 {
1001 return true;
1002 }
1003
1004
1005 current = current.Parent;
1006 }
1007
1008 return false;
1009 }

◆ OnChange()

bool BadScript2.Runtime.BadScope.OnChange ( string  name,
BadObject  oldValue,
BadObject  newValue 
)
package

Definition at line 829 of file BadScope.cs.

830 {
831 if (ClassObject == null)
832 {
833 return false;
834 }
835 if (m_Attributes.TryGetValue(name, out var attributes))
836 {
837 var member = new BadMemberInfo(name, this);
838 foreach (BadClass attribute in attributes.OfType<BadClass>())
839 {
841 {
842 var invoke = (BadFunction)attribute.GetProperty("OnChange").Dereference();
843 var eventObj = new BadMemberChangingEvent(ClassObject!, member, oldValue, newValue);
844 var obj = BadObject.Null;
845 foreach (BadObject o in invoke.Invoke(new BadObject[] { eventObj }, new BadExecutionContext(this)))
846 {
847 //Execute
848 obj = o;
849 }
850
851 if (eventObj.Cancel)
852 {
853 return true;
854 }
855 }
856 }
857 }
858
859 return false;
860 }

◆ OnChanged()

void BadScript2.Runtime.BadScope.OnChanged ( string  name,
BadObject  oldValue,
BadObject  newValue 
)
package

Definition at line 806 of file BadScope.cs.

807 {
808 if (ClassObject == null)
809 {
810 return;
811 }
812 if (m_Attributes.TryGetValue(name, out var attributes))
813 {
814 var member = new BadMemberInfo(name, this);
815 foreach (BadClass attribute in attributes.OfType<BadClass>())
816 {
818 {
819 var invoke = (BadFunction)attribute.GetProperty("OnChanged").Dereference();
820 var eventObj = new BadMemberChangedEvent(ClassObject!, member, oldValue, newValue);
821 foreach (BadObject o in invoke.Invoke(new BadObject[] { eventObj }, new BadExecutionContext(this)))
822 {
823 //Execute
824 }
825 }
826 }
827 }
828 }

◆ SetBreak()

void BadScript2.Runtime.BadScope.SetBreak ( )

Sets the break keyword inside this scope.

Exceptions
BadRuntimeExceptionGets raised if the current scope does not allow the Break Keyword

Definition at line 679 of file BadScope.cs.

680 {
681 if ((Flags & BadScopeFlags.AllowBreak) == 0)
682 {
683 throw new BadRuntimeException("Break not allowed in this scope");
684 }
685
686 IsBreak = true;
687
688 if ((Flags & BadScopeFlags.CaptureBreak) == 0)
689 {
690 Parent?.SetBreak();
691 }
692 }
bool IsBreak
Is true if the Break Keyword was set.
Definition BadScope.cs:404
void SetBreak()
Sets the break keyword inside this scope.
Definition BadScope.cs:679

◆ SetCaller()

void BadScript2.Runtime.BadScope.SetCaller ( BadScope caller)

Sets the Caller of the Scope.

Parameters
callerThe Caller

Definition at line 512 of file BadScope.cs.

513 {
514 m_Caller = caller;
515 }

◆ SetContinue()

void BadScript2.Runtime.BadScope.SetContinue ( )

Sets the continue keyword inside this scope.

Exceptions
BadRuntimeExceptionGets raised if the current scope does not allow the continue Keyword

Definition at line 698 of file BadScope.cs.

699 {
700 if ((Flags & BadScopeFlags.AllowContinue) == 0)
701 {
702 throw new BadRuntimeException("Continue not allowed in this scope");
703 }
704
705 IsContinue = true;
706
707 if ((Flags & BadScopeFlags.CaptureContinue) == 0)
708 {
710 }
711 }
bool IsContinue
Is true if the Continue Keyword was set.
Definition BadScope.cs:409
void SetContinue()
Sets the continue keyword inside this scope.
Definition BadScope.cs:698

◆ SetExports()

void BadScript2.Runtime.BadScope.SetExports ( BadExecutionContext  ctx,
BadObject  exports 
)

Definition at line 722 of file BadScope.cs.

723 {
724 if (m_Exports != null)
725 {
726 throw BadRuntimeException.Create(ctx.Scope, "Exports are already set");
727 }
728
729 m_Exports = exports;
730 }

◆ SetFlags()

void BadScript2.Runtime.BadScope.SetFlags ( BadScopeFlags  flags)

Sets the Scope Flags.

Parameters
flagsScope Flags

Definition at line 624 of file BadScope.cs.

625 {
626 Flags = flags;
627 }

◆ SetRegisteredApi()

void BadScript2.Runtime.BadScope.SetRegisteredApi ( string  api)
package

Registers an API.

Parameters
api

Definition at line 478 of file BadScope.cs.

479 {
480 if (Parent != null)
481 {
483
484 return;
485 }
486
487 if (!m_RegisteredApis.Contains(api))
488 {
489 m_RegisteredApis.Add(api);
490 }
491 }
readonly List< string > m_RegisteredApis
A List of Registered APIs.
Definition BadScope.cs:252
void SetRegisteredApi(string api)
Registers an API.
Definition BadScope.cs:478

◆ SetReturnValue()

void BadScript2.Runtime.BadScope.SetReturnValue ( BadObject value)

Sets the Return value of this scope.

Parameters
valueThe Return Value
Exceptions
BadRuntimeExceptionGets Raised if the Scope does not allow returning

Definition at line 759 of file BadScope.cs.

760 {
761 if ((Flags & BadScopeFlags.AllowReturn) == 0)
762 {
763 throw new BadRuntimeException("Return not allowed in this scope");
764 }
765
766 ReturnValue = value;
767
768 if ((Flags & BadScopeFlags.CaptureReturn) == 0)
769 {
770 Parent?.SetReturnValue(value);
771 }
772 }
BadObject? ReturnValue
The Return value of the scope.
Definition BadScope.cs:414
void SetReturnValue(BadObject? value)
Sets the Return value of this scope.
Definition BadScope.cs:759

◆ ToSafeString()

override string BadScript2.Runtime.BadScope.ToSafeString ( List< BadObject done)

Definition at line 1092 of file BadScope.cs.

1093 {
1094 done.Add(this);
1095
1096 return m_ScopeVariables.ToSafeString(done);
1097 }
override string ToSafeString(List< BadObject > done)
Definition BadTable.cs:240

Member Data Documentation

◆ m_Attributes

readonly Dictionary<string, BadObject[]> BadScript2.Runtime.BadScope.m_Attributes = new Dictionary<string, BadObject[]>()
private

Definition at line 259 of file BadScope.cs.

◆ m_Caller

BadScope? BadScript2.Runtime.BadScope.m_Caller
private

The Caller of the Current Scope.

Definition at line 282 of file BadScope.cs.

◆ m_Exports

BadObject? BadScript2.Runtime.BadScope.m_Exports
private

Contains the exported variables of the scope.

Definition at line 287 of file BadScope.cs.

◆ m_Finalizers

readonly List<Action> BadScript2.Runtime.BadScope.m_Finalizers = new List<Action>()
private

The Finalizer List of the Scope.

Definition at line 242 of file BadScope.cs.

◆ m_IsDisposed

bool BadScript2.Runtime.BadScope.m_IsDisposed
private

Definition at line 289 of file BadScope.cs.

◆ m_Provider

readonly BadInteropExtensionProvider BadScript2.Runtime.BadScope.m_Provider
private

The Extension Provider.

Definition at line 247 of file BadScope.cs.

◆ m_RegisteredApis

readonly List<string> BadScript2.Runtime.BadScope.m_RegisteredApis = new List<string>()
private

A List of Registered APIs.

Definition at line 252 of file BadScope.cs.

◆ m_ScopeVariables

readonly BadTable BadScript2.Runtime.BadScope.m_ScopeVariables = new BadTable()
private

The Scope Variables.

Definition at line 257 of file BadScope.cs.

◆ m_SingletonCache

readonly Dictionary<Type, object> BadScript2.Runtime.BadScope.m_SingletonCache = new Dictionary<Type, object>()
private

The Singleton Cache.

Definition at line 272 of file BadScope.cs.

◆ m_UseVisibility

readonly bool BadScript2.Runtime.BadScope.m_UseVisibility
private

Indicates if the Scope uses the visibility subsystem.

Definition at line 277 of file BadScope.cs.

Property Documentation

◆ Attributes

IReadOnlyDictionary<string, BadObject[]> BadScript2.Runtime.BadScope.Attributes
get

Definition at line 261 of file BadScope.cs.

◆ ClassObject

BadClass? BadScript2.Runtime.BadScope.ClassObject
getset

The Class Object of the Scope.

Definition at line 373 of file BadScope.cs.

373{ get; internal set; }

◆ CountInStackTrace

bool BadScript2.Runtime.BadScope.CountInStackTrace
getprivate

Indicates if the Scope should count towards the Stack Trace.

Definition at line 399 of file BadScope.cs.

◆ Flags

BadScopeFlags BadScript2.Runtime.BadScope.Flags
getprivate set

The Scope Flags.

Definition at line 394 of file BadScope.cs.

394{ get; private set; }

◆ FunctionObject

BadFunction? BadScript2.Runtime.BadScope.FunctionObject
getset

The Function Object of the Scope.

Definition at line 378 of file BadScope.cs.

378{ get; internal set; }

◆ IsBreak

bool BadScript2.Runtime.BadScope.IsBreak
getprivate set

Is true if the Break Keyword was set.

Definition at line 404 of file BadScope.cs.

404{ get; private set; }

◆ IsContinue

bool BadScript2.Runtime.BadScope.IsContinue
getprivate set

Is true if the Continue Keyword was set.

Definition at line 409 of file BadScope.cs.

409{ get; private set; }

◆ Name

string BadScript2.Runtime.BadScope.Name
get

The Name of the Scope (for Debugging)

Definition at line 389 of file BadScope.cs.

389{ get; }

◆ Parent

BadScope? BadScript2.Runtime.BadScope.Parent
get

The Parent Scope.

Definition at line 384 of file BadScope.cs.

384{ get; }

◆ Prototype

BadClassPrototype BadScript2.Runtime.BadScope.Prototype
staticget

A Class Prototype for the Scope.

Definition at line 420 of file BadScope.cs.

◆ Provider

BadInteropExtensionProvider BadScript2.Runtime.BadScope.Provider
get

The Extension Provider.

Definition at line 368 of file BadScope.cs.

◆ RegisteredApis

IReadOnlyCollection<string> BadScript2.Runtime.BadScope.RegisteredApis
get

A List of Registered APIs.

Definition at line 363 of file BadScope.cs.

◆ ReturnValue

BadObject? BadScript2.Runtime.BadScope.ReturnValue
getprivate set

The Return value of the scope.

Definition at line 414 of file BadScope.cs.

414{ get; private set; }

The documentation for this class was generated from the following file: