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:642
void OnChanged(string name, BadObject oldValue, BadObject newValue)
Definition BadScope.cs:610
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:471

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

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

282 {
283 if (m_IsDisposed)
284 {
285 throw BadRuntimeException.Create(this, "Scope is already disposed");
286 }
287
288 m_Finalizers.Add(finalizer);
289 }
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 315 of file BadScope.cs.

315 : class
316 {
317 if (instance == null)
318 {
319 throw new BadRuntimeException("Cannot add null as singleton");
320 }
321
322 m_SingletonCache.Add(typeof(T), instance);
323 }
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 471 of file BadScope.cs.

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

601 {
602 BadScope sc = new BadScope(this, caller, name, flags, useVisibility ?? m_UseVisibility)
603 {
605 };
606
607 return sc;
608 }
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 391 of file BadScope.cs.

392 {
393 BadScope s = locals != null
394 ? new BadScope(name, ctx.Scope.Provider, locals)
395 : new BadScope(name, ctx.Scope.Provider);
396
397 foreach (KeyValuePair<Type, object> kvp in ctx.Scope.GetRootScope()
398 .m_SingletonCache)
399 {
400 s.m_SingletonCache.Add(kvp.Key, kvp.Value);
401 }
402
403 return s;
404 }
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 713 of file BadScope.cs.

719 {
720 if (HasLocal(name, caller.Scope, false))
721 {
722 throw new BadRuntimeException($"Property {name} is already defined");
723 }
724
725 Action<BadObject, BadSourcePosition?, BadPropertyInfo?>? setter = null;
726
727 if (setAccessor != null)
728 {
729 setter = (value, p, pi) =>
730 {
731 BadExecutionContext? setCtx =
732 new BadExecutionContext(caller.Scope.CreateChild($"set {name}", caller.Scope, null));
733
734 setCtx.Scope.DefineVariable("value",
735 value,
736 setCtx.Scope,
738 );
739
740 foreach (BadObject o in setCtx.Execute(setAccessor))
741 {
742 //Execute
743 }
744 };
745 }
746
747 m_ScopeVariables.PropertyInfos.Add(name, new BadPropertyInfo(type, setter == null));
748
750 BadObjectReference.Make($"property {name}",
751 (p) =>
752 {
753 BadExecutionContext? getCtx =
754 new BadExecutionContext(caller.Scope
755 .CreateChild($"get {name}",
756 caller.Scope,
757 null
758 )
759 );
760 BadObject? get = Null;
761
762 foreach (BadObject o in getCtx.Execute(getAccessor))
763 {
764 get = o;
765 }
766
767 return get.Dereference(p);
768 },
769 setter
770 )
771 );
772
773 m_Attributes[name] = attributes;
774 }
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:923
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 784 of file BadScope.cs.

789 {
790 if (HasLocal(name, caller ?? this, false))
791 {
792 throw new BadRuntimeException($"Variable {name} is already defined");
793 }
794
795 m_Attributes[name] = attributes ?? [];
796
797 m_ScopeVariables.GetProperty(name, false, caller ?? this)
798 .Set(value, null, info, true);
799 }
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 231 of file BadScope.cs.

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

◆ GetExports()

BadObject BadScript2.Runtime.BadScope.GetExports ( )

Returns the exported key value pairs of the scope.

Returns
BadTable with all exported variables

Definition at line 522 of file BadScope.cs.

523 {
524 return m_Exports ?? Null;
525 }
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 250 of file BadScope.cs.

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

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

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

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

380 {
381 return Prototype;
382 }
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 304 of file BadScope.cs.

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

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

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

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

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

◆ GetStackTrace() [1/2]

string BadScript2.Runtime.BadScope.GetStackTrace ( )

Returns the Stack Trace of the Current scope.

Returns
Stack Trace

Definition at line 419 of file BadScope.cs.

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

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

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

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

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

◆ GetTable()

BadTable BadScript2.Runtime.BadScope.GetTable ( )

Returns the Variable Table of the current scope.

Returns
BadTable with all local variables

Definition at line 583 of file BadScope.cs.

584 {
585 return m_ScopeVariables;
586 }

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

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

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

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

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

808 {
809 if (HasLocal(name, this))
810 {
812 }
813
814 if (Parent == null)
815 {
816 throw new BadRuntimeException($"Variable '{name}' is not defined");
817 }
818
819 return Parent!.GetVariableInfo(name);
820 }
BadPropertyInfo GetVariableInfo(string name)
Returns the variable info of the specified variable.
Definition BadScope.cs:807
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 923 of file BadScope.cs.

924 {
925 return !useExtensions
926 ? m_ScopeVariables.InnerTable.ContainsKey(name)
927 : m_ScopeVariables.HasProperty(name, caller);
928 }
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 951 of file BadScope.cs.

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

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

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

◆ InitializeAttributes()

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

Definition at line 683 of file BadScope.cs.

684 {
685 if (ClassObject == null)
686 {
687 throw new BadRuntimeException("Scope is not a class scope");
688 }
689
690 foreach (KeyValuePair<string, BadObject[]> kvp in m_Attributes)
691 {
692 BadMemberInfo? member = new BadMemberInfo(kvp.Key, this);
693
694 foreach (BadClass attribute in kvp.Value.OfType<BadClass>())
695 {
697 {
698 BadFunction? invoke = (BadFunction)attribute.GetProperty("Initialize")
699 .Dereference(null);
700
701 foreach (BadObject o in invoke.Invoke(new BadObject[] { ClassObject!, member },
702 new BadExecutionContext(this)
703 ))
704 {
705 //Execute
706 yield return o;
707 }
708 }
709 }
710 }
711 }
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 844 of file BadScope.cs.

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

◆ OnChange()

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

Definition at line 642 of file BadScope.cs.

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

◆ OnChanged()

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

Definition at line 610 of file BadScope.cs.

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

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

485 {
486 if ((Flags & BadScopeFlags.AllowBreak) == 0)
487 {
488 throw new BadRuntimeException("Break not allowed in this scope");
489 }
490
491 IsBreak = true;
492
493 if ((Flags & BadScopeFlags.CaptureBreak) == 0)
494 {
495 Parent?.SetBreak();
496 }
497 }
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:484

◆ SetCaller()

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

Sets the Caller of the Scope.

Parameters
callerThe Caller

Definition at line 295 of file BadScope.cs.

296 {
297 m_Caller = caller;
298 }

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

504 {
505 if ((Flags & BadScopeFlags.AllowContinue) == 0)
506 {
507 throw new BadRuntimeException("Continue not allowed in this scope");
508 }
509
510 IsContinue = true;
511
512 if ((Flags & BadScopeFlags.CaptureContinue) == 0)
513 {
515 }
516 }
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:503

◆ SetExports()

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

Definition at line 527 of file BadScope.cs.

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

◆ SetFlags()

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

Sets the Scope Flags.

Parameters
flagsScope Flags

Definition at line 410 of file BadScope.cs.

411 {
412 Flags = flags;
413 }

◆ SetRegisteredApi()

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

Registers an API.

Parameters
api

Definition at line 261 of file BadScope.cs.

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

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

565 {
566 if ((Flags & BadScopeFlags.AllowReturn) == 0)
567 {
568 throw new BadRuntimeException("Return not allowed in this scope");
569 }
570
571 ReturnValue = value;
572
573 if ((Flags & BadScopeFlags.CaptureReturn) == 0)
574 {
575 Parent?.SetReturnValue(value);
576 }
577 }
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:564

◆ ToSafeString()

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

Definition at line 958 of file BadScope.cs.

959 {
960 done.Add(this);
961
962 return m_ScopeVariables.ToSafeString(done);
963 }
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: