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

 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.
 
BadArray GetMemberInfos ()
 
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.
 
IEnumerable< BadScopeGetStackTraceEnumerable ()
 
BadScope GetFirstTracableOrRoot ()
 
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 (DateTimeOffset d)
 
static implicit operator BadObject (TimeSpan t)
 
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 IEnumerable< BadScopeGetStackTraceEnumerable (BadScope scope)
 
static BadScopeFlags ClearCaptures (BadScopeFlags flags)
 Clears all Capture Flags from the given Flags.
 

Private Attributes

readonly Dictionary< string, BadObject[]> m_Attributes = new Dictionary<string, BadObject[]>()
 
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< 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 15 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 68 of file BadScope.cs.

72 {
73 Name = name;
74 Flags = flags;
75 m_Caller = caller;
76 m_Provider = provider;
78 m_ScopeVariables.OnChangedProperty += OnChanged;
79 }
readonly BadTable m_ScopeVariables
The Scope Variables.
Definition BadScope.cs:37
BadScopeFlags Flags
The Scope Flags.
Definition BadScope.cs:162
BadScope? m_Caller
The Caller of the Current Scope.
Definition BadScope.cs:52
readonly BadInteropExtensionProvider m_Provider
The Extension Provider.
Definition BadScope.cs:27
string Name
The Name of the Scope (for Debugging)
Definition BadScope.cs:157
bool OnChange(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:643
void OnChanged(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:611
void SetChangeInterceptor(Func< string, BadObject, BadObject, bool >? interceptor)
Definition BadTable.cs:93

◆ 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 89 of file BadScope.cs.

94 {
95 Name = name;
96 Flags = flags;
97 m_Caller = caller;
98 m_ScopeVariables = locals;
99 m_Provider = provider;
101 m_ScopeVariables.OnChangedProperty += OnChanged;
102 }

◆ 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 112 of file BadScope.cs.

116 : this(name,
117 parent.Provider,
118 caller,
119 ClearCaptures(parent.Flags) | flags
120 )
121 {
122 m_UseVisibility = useVisibility;
123 Parent = parent;
124 }
readonly bool m_UseVisibility
Indicates if the Scope uses the visibility subsystem.
Definition BadScope.cs:47
BadScope? Parent
The Parent Scope.
Definition BadScope.cs:152
static BadScopeFlags ClearCaptures(BadScopeFlags flags)
Clears all Capture Flags from the given Flags.
Definition BadScope.cs:472

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 543 of file BadScope.cs.

544 {
545 if (Parent != null)
546 {
547 Parent.AddExport(key, value);
548 }
549 else
550 {
551 if (m_Exports == null)
552 {
553 m_Exports = new BadTable();
554 }
555
556 m_Exports.SetProperty(key, value);
557 }
558 }
void AddExport(string key, BadObject value)
Sets an exported key value pair in the scope.
Definition BadScope.cs:543
BadObject? m_Exports
Contains the exported variables of the scope.
Definition BadScope.cs:57
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 282 of file BadScope.cs.

283 {
284 if (m_IsDisposed)
285 {
286 throw BadRuntimeException.Create(this, "Scope is already disposed");
287 }
288
289 m_Finalizers.Add(finalizer);
290 }
readonly List< Action > m_Finalizers
The Finalizer List of the Scope.
Definition BadScope.cs:22
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 316 of file BadScope.cs.

316 : class
317 {
318 if (instance == null)
319 {
320 throw new BadRuntimeException("Cannot add null as singleton");
321 }
322
323 m_SingletonCache.Add(typeof(T), instance);
324 }
readonly Dictionary< Type, object > m_SingletonCache
The Singleton Cache.
Definition BadScope.cs:42

◆ 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 472 of file BadScope.cs.

473 {
474 return flags &
475 ~(BadScopeFlags.CaptureReturn |
476 BadScopeFlags.CaptureBreak |
477 BadScopeFlags.CaptureContinue |
478 BadScopeFlags.CaptureThrow);
479 }
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 598 of file BadScope.cs.

602 {
603 BadScope sc = new BadScope(this, caller, name, flags, useVisibility ?? m_UseVisibility)
604 {
606 };
607
608 return sc;
609 }
BadScope(string name, BadInteropExtensionProvider provider, BadScope? caller=null, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a new Scope.
Definition BadScope.cs:68
BadClass? ClassObject
The Class Object of the Scope.
Definition BadScope.cs:141
BadFunction? FunctionObject
The Function Object of the Scope.
Definition BadScope.cs:146

◆ 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 392 of file BadScope.cs.

393 {
394 BadScope s = locals != null
395 ? new BadScope(name, ctx.Scope.Provider, locals)
396 : new BadScope(name, ctx.Scope.Provider);
397
398 foreach (KeyValuePair<Type, object> kvp in ctx.Scope.GetRootScope()
399 .m_SingletonCache)
400 {
401 s.m_SingletonCache.Add(kvp.Key, kvp.Value);
402 }
403
404 return s;
405 }
BadInteropExtensionProvider Provider
The Extension Provider.
Definition BadScope.cs:136

◆ DefineProperty()

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

Definition at line 714 of file BadScope.cs.

720 {
721 if (HasLocal(name, caller.Scope, false))
722 {
723 throw new BadRuntimeException($"Property {name} is already defined");
724 }
725
726 Action<BadObject, BadSourcePosition?, BadPropertyInfo?>? setter = null;
727
728 if (setAccessor != null)
729 {
730 setter = (value, p, pi) =>
731 {
732 BadExecutionContext? setCtx =
733 new BadExecutionContext(caller.Scope.CreateChild($"set {name}", caller.Scope, null));
734
735 setCtx.Scope.DefineVariable("value",
736 value,
737 setCtx.Scope,
739 );
740
741 foreach (BadObject o in setCtx.Execute(setAccessor))
742 {
743 //Execute
744 }
745 };
746 }
747
748 m_ScopeVariables.PropertyInfos.Add(name, new BadPropertyInfo(type, setter == null));
749
751 BadObjectReference.Make($"property {name}",
752 (p) =>
753 {
754 BadExecutionContext? getCtx =
755 new BadExecutionContext(caller.Scope
756 .CreateChild($"get {name}",
757 caller.Scope,
758 null
759 )
760 );
761 BadObject? get = Null;
762
763 foreach (BadObject o in getCtx.Execute(getAccessor))
764 {
765 get = o;
766 }
767
768 return get.Dereference(p);
769 },
770 setter
771 )
772 );
773
774 m_Attributes[name] = attributes;
775 }
readonly Dictionary< string, BadObject[]> m_Attributes
Definition BadScope.cs:17
bool HasLocal(string name, BadScope caller, bool useExtensions=true)
returns true if the specified variable is defined in the current scope
Definition BadScope.cs:924
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< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? 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:59
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Definition BadTable.cs:54
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 785 of file BadScope.cs.

790 {
791 if (HasLocal(name, caller ?? this, false))
792 {
793 throw new BadRuntimeException($"Variable {name} is already defined");
794 }
795
796 m_Attributes[name] = attributes ?? [];
797
798 m_ScopeVariables.GetProperty(name, false, caller ?? this)
799 .Set(value, null, info, true);
800 }
void Set(BadObject obj, BadSourcePosition? position, 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:141

◆ Dispose()

void BadScript2.Runtime.BadScope.Dispose ( )

Disposes the Scope and calls all finalizers.

Definition at line 232 of file BadScope.cs.

233 {
234 if (m_IsDisposed)
235 {
236 return;
237 }
238
239 m_IsDisposed = true;
240
241 foreach (Action finalizer in m_Finalizers)
242 {
243 finalizer();
244 }
245
246 m_Finalizers.Clear();
247 }

◆ GetExports()

BadObject BadScript2.Runtime.BadScope.GetExports ( )

Returns the exported key value pairs of the scope.

Returns
BadTable with all exported variables

Definition at line 523 of file BadScope.cs.

524 {
525 return m_Exports ?? Null;
526 }
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28

◆ GetFirstTracableOrRoot()

BadScope BadScript2.Runtime.BadScope.GetFirstTracableOrRoot ( )

◆ GetMemberInfos()

BadArray BadScript2.Runtime.BadScope.GetMemberInfos ( )

Definition at line 251 of file BadScope.cs.

252 {
253 return new BadArray(m_ScopeVariables.InnerTable.Keys.Select(x => (BadObject)new BadMemberInfo(x, this))
254 .ToList()
255 );
256 }
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 944 of file BadScope.cs.

945 {
946 return HasVariable(propName, caller ?? this)
947 ? GetVariable(propName, caller ?? this)
948 : base.GetProperty(propName, caller);
949 }
BadObjectReference GetVariable(string name, BadScope caller)
Returns the variable reference of the specified variable.
Definition BadScope.cs:879
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:937
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadScope.cs:944

◆ 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 828 of file BadScope.cs.

829 {
830 char first = propName[0];
831 char second = propName.Length > 1 ? propName[1] : '\0';
832
833 return second switch
834 {
835 '_' => first == '_' ? BadPropertyVisibility.Private : BadPropertyVisibility.Public,
836 _ => first == '_' ? BadPropertyVisibility.Protected : BadPropertyVisibility.Public,
837 };
838 }
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 380 of file BadScope.cs.

381 {
382 return Prototype;
383 }
static BadClassPrototype Prototype
A Class Prototype for the Scope.
Definition BadScope.cs:188

◆ GetRootScope()

BadScope BadScript2.Runtime.BadScope.GetRootScope ( )

Returns the Root Scope of the Scope.

Returns
The Root Scope

Definition at line 305 of file BadScope.cs.

306 {
307 return Parent?.GetRootScope() ?? this;
308 }
BadScope GetRootScope()
Returns the Root Scope of the Scope.
Definition BadScope.cs:305

◆ 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 331 of file BadScope.cs.

332 {
333 if (Parent != null)
334 {
335 return Parent.GetSingleton<T>();
336 }
337
338 if (m_SingletonCache.TryGetValue(typeof(T), out object? value))
339 {
340 return (T)value;
341 }
342
343 return default;
344 }

◆ 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 353 of file BadScope.cs.

353 : new()
354 {
355 if (Parent != null)
356 {
357 return Parent.GetSingleton<T>(createNew);
358 }
359
360 if (m_SingletonCache.ContainsKey(typeof(T)))
361 {
362 return (T)m_SingletonCache[typeof(T)];
363 }
364
365 if (!createNew)
366 {
367 throw new Exception("Singleton not found");
368 }
369
370 T v = new T();
371 m_SingletonCache[typeof(T)] = v;
372
373 return v;
374 }

◆ GetStackTrace() [1/2]

string BadScript2.Runtime.BadScope.GetStackTrace ( )

Returns the Stack Trace of the Current scope.

Returns
Stack Trace

Definition at line 420 of file BadScope.cs.

421 {
422 return GetStackTrace(this);
423 }
string GetStackTrace()
Returns the Stack Trace of the Current scope.
Definition BadScope.cs:420

◆ 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 430 of file BadScope.cs.

431 {
432 BadScope? current = scope;
433 List<BadScope> stack = new List<BadScope>();
434
435 while (current != null)
436 {
437 if (current.CountInStackTrace)
438 {
439 stack.Add(current);
440 }
441
442 current = current.m_Caller ?? current.Parent;
443 }
444
445 return string.Join("\n", stack.Select(s => s.Name));
446 }

◆ GetStackTraceEnumerable() [1/2]

IEnumerable< BadScope > BadScript2.Runtime.BadScope.GetStackTraceEnumerable ( )

◆ GetStackTraceEnumerable() [2/2]

static IEnumerable< BadScope > BadScript2.Runtime.BadScope.GetStackTraceEnumerable ( BadScope  scope)
staticprivate

Definition at line 448 of file BadScope.cs.

449 {
450 BadScope? current = scope;
451
452 while (current != null)
453 {
454 if (current.CountInStackTrace)
455 {
456 yield return current;
457 }
458
459 current = current.m_Caller ?? current.Parent;
460 }
461 }

◆ GetTable()

BadTable BadScript2.Runtime.BadScope.GetTable ( )

Returns the Variable Table of the current scope.

Returns
BadTable with all local variables

Definition at line 584 of file BadScope.cs.

585 {
586 return m_ScopeVariables;
587 }

◆ 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 912 of file BadScope.cs.

913 {
914 return GetVariable(name, this);
915 }

◆ 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 879 of file BadScope.cs.

880 {
881 if (m_UseVisibility)
882 {
884 ? BadPropertyVisibility.All
885 : BadPropertyVisibility.Public;
886
887 if ((GetPropertyVisibility(name) & vis) == 0)
888 {
889 throw BadRuntimeException.Create(caller, $"Variable '{name}' is not visible to {caller}");
890 }
891 }
892
893 if (HasLocal(name, caller))
894 {
895 return m_ScopeVariables.GetProperty(name, caller);
896 }
897
898 if (Parent == null)
899 {
900 throw BadRuntimeException.Create(caller, $"Variable '{name}' is not defined");
901 }
902
903 return Parent!.GetVariable(name, caller);
904 }
bool IsVisibleParentOf(BadScope scope)
Returns true if the specified scope is visible to the current scope.
Definition BadScope.cs:845
static BadPropertyVisibility GetPropertyVisibility(string propName)
Returns the visibility of the specified property.
Definition BadScope.cs:828

◆ 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 808 of file BadScope.cs.

809 {
810 if (HasLocal(name, this))
811 {
813 }
814
815 if (Parent == null)
816 {
817 throw new BadRuntimeException($"Variable '{name}' is not defined");
818 }
819
820 return Parent!.GetVariableInfo(name);
821 }
BadPropertyInfo GetVariableInfo(string name)
Returns the variable info of the specified variable.
Definition BadScope.cs:808
BadPropertyInfo GetPropertyInfo(string propName)
Returns Property Information for a given Key.
Definition BadTable.cs:110

◆ 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 924 of file BadScope.cs.

925 {
926 return !useExtensions
927 ? m_ScopeVariables.InnerTable.ContainsKey(name)
928 : m_ScopeVariables.HasProperty(name, caller);
929 }
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:128

◆ 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 952 of file BadScope.cs.

953 {
954 return HasVariable(propName, caller ?? this) || base.HasProperty(propName, caller);
955 }

◆ 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 937 of file BadScope.cs.

938 {
939 return HasLocal(name, caller) || (Parent != null && Parent.HasVariable(name, caller));
940 }

◆ InitializeAttributes()

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

Definition at line 684 of file BadScope.cs.

685 {
686 if (ClassObject == null)
687 {
688 throw new BadRuntimeException("Scope is not a class scope");
689 }
690
691 foreach (KeyValuePair<string, BadObject[]> kvp in m_Attributes)
692 {
693 BadMemberInfo? member = new BadMemberInfo(kvp.Key, this);
694
695 foreach (BadClass attribute in kvp.Value.OfType<BadClass>())
696 {
698 {
699 BadFunction? invoke = (BadFunction)attribute.GetProperty("Initialize")
700 .Dereference(null);
701
702 foreach (BadObject o in invoke.Invoke(new BadObject[] { ClassObject!, member },
703 new BadExecutionContext(this)
704 ))
705 {
706 //Execute
707 yield return o;
708 }
709 }
710 }
711 }
712 }
Implements a function that can be called from the script.
IEnumerable< BadObject > Invoke(BadObject[] args, BadExecutionContext caller)
Invokes the function with the specified arguments.
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:120
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 845 of file BadScope.cs.

846 {
847 if (scope == this)
848 {
849 return true;
850 }
851
852 BadScope? current = scope.Parent;
853
854 while (current != null)
855 {
856 if (!current.m_UseVisibility)
857 {
858 return false;
859 }
860
861 if (current == this)
862 {
863 return true;
864 }
865
866 current = current.Parent;
867 }
868
869 return false;
870 }

◆ OnChange()

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

Definition at line 643 of file BadScope.cs.

644 {
645 if (ClassObject == null)
646 {
647 return false;
648 }
649
650 if (m_Attributes.TryGetValue(name, out BadObject[]? attributes))
651 {
652 BadMemberInfo? member = new BadMemberInfo(name, this);
653
654 foreach (BadClass attribute in attributes.OfType<BadClass>())
655 {
657 {
658 BadFunction? invoke = (BadFunction)attribute.GetProperty("OnChange")
659 .Dereference(null);
660
661 BadMemberChangingEvent? eventObj =
662 new BadMemberChangingEvent(ClassObject!, member, oldValue, newValue);
663 BadObject? obj = Null;
664
665 foreach (BadObject o in invoke.Invoke(new BadObject[] { eventObj },
666 new BadExecutionContext(this)
667 ))
668 {
669 //Execute
670 obj = o;
671 }
672
673 if (eventObj.Cancel)
674 {
675 return true;
676 }
677 }
678 }
679 }
680
681 return false;
682 }

◆ OnChanged()

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

Definition at line 611 of file BadScope.cs.

612 {
613 if (ClassObject == null)
614 {
615 return;
616 }
617
618 if (m_Attributes.TryGetValue(name, out BadObject[]? attributes))
619 {
620 BadMemberInfo? member = new BadMemberInfo(name, this);
621
622 foreach (BadClass attribute in attributes.OfType<BadClass>())
623 {
625 {
626 BadFunction? invoke = (BadFunction)attribute.GetProperty("OnChanged")
627 .Dereference(null);
628
629 BadMemberChangedEvent? eventObj =
630 new BadMemberChangedEvent(ClassObject!, member, oldValue, newValue);
631
632 foreach (BadObject o in invoke.Invoke(new BadObject[] { eventObj },
633 new BadExecutionContext(this)
634 ))
635 {
636 //Execute
637 }
638 }
639 }
640 }
641 }

◆ 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 485 of file BadScope.cs.

486 {
487 if ((Flags & BadScopeFlags.AllowBreak) == 0)
488 {
489 throw new BadRuntimeException("Break not allowed in this scope");
490 }
491
492 IsBreak = true;
493
494 if ((Flags & BadScopeFlags.CaptureBreak) == 0)
495 {
496 Parent?.SetBreak();
497 }
498 }
bool IsBreak
Is true if the Break Keyword was set.
Definition BadScope.cs:172
void SetBreak()
Sets the break keyword inside this scope.
Definition BadScope.cs:485

◆ SetCaller()

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

Sets the Caller of the Scope.

Parameters
callerThe Caller

Definition at line 296 of file BadScope.cs.

297 {
298 m_Caller = caller;
299 }

◆ 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 504 of file BadScope.cs.

505 {
506 if ((Flags & BadScopeFlags.AllowContinue) == 0)
507 {
508 throw new BadRuntimeException("Continue not allowed in this scope");
509 }
510
511 IsContinue = true;
512
513 if ((Flags & BadScopeFlags.CaptureContinue) == 0)
514 {
516 }
517 }
bool IsContinue
Is true if the Continue Keyword was set.
Definition BadScope.cs:177
void SetContinue()
Sets the continue keyword inside this scope.
Definition BadScope.cs:504

◆ SetExports()

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

Definition at line 528 of file BadScope.cs.

529 {
530 if (m_Exports != null)
531 {
532 throw BadRuntimeException.Create(ctx.Scope, "Exports are already set");
533 }
534
535 m_Exports = exports;
536 }

◆ SetFlags()

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

Sets the Scope Flags.

Parameters
flagsScope Flags

Definition at line 411 of file BadScope.cs.

412 {
413 Flags = flags;
414 }

◆ SetRegisteredApi()

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

Registers an API.

Parameters
api

Definition at line 262 of file BadScope.cs.

263 {
264 if (Parent != null)
265 {
267
268 return;
269 }
270
271 if (!m_RegisteredApis.Contains(api))
272 {
273 m_RegisteredApis.Add(api);
274 }
275 }
readonly List< string > m_RegisteredApis
A List of Registered APIs.
Definition BadScope.cs:32
void SetRegisteredApi(string api)
Registers an API.
Definition BadScope.cs:262

◆ 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 565 of file BadScope.cs.

566 {
567 if ((Flags & BadScopeFlags.AllowReturn) == 0)
568 {
569 throw new BadRuntimeException("Return not allowed in this scope");
570 }
571
572 ReturnValue = value;
573
574 if ((Flags & BadScopeFlags.CaptureReturn) == 0)
575 {
576 Parent?.SetReturnValue(value);
577 }
578 }
BadObject? ReturnValue
The Return value of the scope.
Definition BadScope.cs:182
void SetReturnValue(BadObject? value)
Sets the Return value of this scope.
Definition BadScope.cs:565

◆ ToSafeString()

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

Definition at line 959 of file BadScope.cs.

960 {
961 done.Add(this);
962
963 return m_ScopeVariables.ToSafeString(done);
964 }
override string ToSafeString(List< BadObject > done)
Definition BadTable.cs:262

Member Data Documentation

◆ m_Attributes

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

Definition at line 17 of file BadScope.cs.

◆ m_Caller

BadScope? BadScript2.Runtime.BadScope.m_Caller
private

The Caller of the Current Scope.

Definition at line 52 of file BadScope.cs.

◆ m_Exports

BadObject? BadScript2.Runtime.BadScope.m_Exports
private

Contains the exported variables of the scope.

Definition at line 57 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 22 of file BadScope.cs.

◆ m_IsDisposed

bool BadScript2.Runtime.BadScope.m_IsDisposed
private

Definition at line 59 of file BadScope.cs.

◆ m_Provider

readonly BadInteropExtensionProvider BadScript2.Runtime.BadScope.m_Provider
private

The Extension Provider.

Definition at line 27 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 32 of file BadScope.cs.

◆ m_ScopeVariables

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

The Scope Variables.

Definition at line 37 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 42 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 47 of file BadScope.cs.

Property Documentation

◆ Attributes

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

Definition at line 126 of file BadScope.cs.

◆ ClassObject

BadClass? BadScript2.Runtime.BadScope.ClassObject
getset

The Class Object of the Scope.

Definition at line 141 of file BadScope.cs.

141{ get; internal set; }

◆ CountInStackTrace

bool BadScript2.Runtime.BadScope.CountInStackTrace
getprivate

Indicates if the Scope should count towards the Stack Trace.

Definition at line 167 of file BadScope.cs.

◆ Flags

BadScopeFlags BadScript2.Runtime.BadScope.Flags
getprivate set

The Scope Flags.

Definition at line 162 of file BadScope.cs.

162{ get; private set; }

◆ FunctionObject

BadFunction? BadScript2.Runtime.BadScope.FunctionObject
getset

The Function Object of the Scope.

Definition at line 146 of file BadScope.cs.

146{ get; internal set; }

◆ IsBreak

bool BadScript2.Runtime.BadScope.IsBreak
getprivate set

Is true if the Break Keyword was set.

Definition at line 172 of file BadScope.cs.

172{ get; private set; }

◆ IsContinue

bool BadScript2.Runtime.BadScope.IsContinue
getprivate set

Is true if the Continue Keyword was set.

Definition at line 177 of file BadScope.cs.

177{ get; private set; }

◆ Name

string BadScript2.Runtime.BadScope.Name
get

The Name of the Scope (for Debugging)

Definition at line 157 of file BadScope.cs.

157{ get; }

◆ Parent

BadScope? BadScript2.Runtime.BadScope.Parent
get

The Parent Scope.

Definition at line 152 of file BadScope.cs.

152{ get; }

◆ Prototype

BadClassPrototype BadScript2.Runtime.BadScope.Prototype
staticget

A Class Prototype for the Scope.

Definition at line 188 of file BadScope.cs.

◆ Provider

BadInteropExtensionProvider BadScript2.Runtime.BadScope.Provider
get

The Extension Provider.

Definition at line 136 of file BadScope.cs.

◆ RegisteredApis

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

A List of Registered APIs.

Definition at line 131 of file BadScope.cs.

◆ ReturnValue

BadObject? BadScript2.Runtime.BadScope.ReturnValue
getprivate set

The Return value of the scope.

Definition at line 182 of file BadScope.cs.

182{ get; private set; }

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