1using System.Globalization;
2using System.Reflection;
3using System.Runtime.InteropServices;
56 [BadMethod(description:
"Creates a default scope based off of the root scope of the caller")]
57 [return: BadReturn(
"The created scope")]
64 [BadMethod(description:
"Evaluates a BadScript Source String")]
65 [return: BadReturn(
"An Awaitable Task")]
67 [BadParameter(description:
"The Source of the Script")]
69 [BadParameter(description:
70 "An (optional but recommended) file path, it will be used to determine the working directory of the script."
73 [BadParameter(description:
74 "If true, any optimizations that are activated in the settings will be applied."
77 [BadParameter(description:
78 "An (optional) scope that the execution takes place in, if not specified, an Instance of BadRuntime will get searched and a scope will be created from it, if its not found, a scope will be created from the root scope of the caller."
81 [BadParameter(description:
82 "If true, the last element that was returned from the enumeration will be the result of the task. Otherwise the result will be the exported objects of the script."
84 bool setLastAsReturn = false)
88 bool optimizeConstantFolding =
89 BadNativeOptimizationSettings.Instance.UseConstantFoldingOptimization && optimize;
91 bool optimizeConstantSubstitution =
92 BadNativeOptimizationSettings.Instance.UseConstantSubstitutionOptimization && optimize;
97 if (runtime !=
null && scope ==
null)
101 ctx = runtime.
CreateContext(Path.GetDirectoryName(file) ??
"/");
110 .CreateChild(
"<EvaluateAsync>",
122 if (optimizeConstantFolding)
127 if (optimizeConstantSubstitution)
134 IEnumerable<BadObject> executor;
157 [BadMethod(description:
"Returns the Stack Trace of the current Execution Context")]
158 [return: BadReturn(
"The Stack Trace")]
161 return ctx.Scope.GetStackTrace();
172 [BadMethod(description:
"Returns all Native Types")]
173 [return: BadReturn(
"An Array containing all Native Types")]
181 [BadMethod(description:
"Returns the Assembly Path of the current Runtime")]
182 [return: BadReturn(
"The Assembly Path")]
185 string path = Path.ChangeExtension(Assembly.GetEntryAssembly()!.Location,
186 RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
"exe" :
""
192 [BadMethod(description:
"Returns a new Guid")]
193 [return: BadReturn(
"A new Guid")]
196 return Guid.NewGuid()
200 [BadMethod(description:
"Returns true if an api with that name is registered")]
201 [return: BadReturn(
"True if the api is registered")]
204 return ctx.Scope.RegisteredApis.Contains(api);
207 [BadMethod(description:
"Creates a new Reference Object")]
208 [return: BadReturn(
"The Reference Object")]
210 [BadParameter(description:
"The Text for the Reference")]
212 [BadParameter(description:
"The getter Function")]
214 [BadParameter(description:
"The setter Function")]
216 [BadParameter(description:
"The delete Function")]
219 if (
set ==
null &&
delete ==
null)
224 if (
delete ==
null &&
set !=
null)
232 if (
delete !=
null &&
set ==
null)
236 (Action<BadObject, BadSourcePosition?, BadPropertyInfo?>?)
null,
262 return result.Dereference(
null);
267 foreach (
BadObject? o
in func.Invoke(
new[] { value },
272 [BadMethod(description:
"Returns all registered apis")]
273 [return: BadReturn(
"An Array containing all registered apis")]
288 [BadMethod(description:
"Registers an Import Handler")]
290 [BadParameter(description:
291 "An Import handler implementing the IImportHandler Interface",
292 nativeType:
"IImportHandler"
305 if (importer ==
null)
320 [BadMethod(
"Validate",
"Validates a source string")]
321 [
return: BadReturn(
"Validation Result")]
323 [BadParameter(description:
"The File Name")] string file)
328 ret.SetProperty(
"IsError", result.
IsError);
330 ret.SetProperty(
"Messages",
333 BadTable msg = new BadTable();
334 msg.SetProperty(
"Message", x.Message);
335 msg.SetProperty(
"Validator", x.Validator.ToString());
336 msg.SetProperty(
"Type", x.Type.ToString());
338 msg.SetProperty(
"Position",
339 x.Expression.Position.ToString()
342 return (BadObject)msg;
358 [BadMethod(description:
"Parses a date string")]
359 [return: BadReturn(
"Bad Table with the parsed date")]
362 DateTimeOffset d = DateTimeOffset.Parse(date);
371 [BadMethod(description:
"Returns the Current Time")]
372 [return: BadReturn(
"The Current Time")]
384 [BadMethod(description:
"Lists all extension names of the given object")]
385 [return: BadReturn(
"An Array containing all Extension Names")]
387 [BadParameter(description:
"Object")]
BadObject o)
389 return new BadArray(ctx.Scope.Provider.GetExtensionNames(o)
399 [BadMethod(description:
"Returns all Extensions of the given object(will create the objects based on the supplied object)")]
400 [return: BadReturn(
"A Table containing all Extensions")]
402 [BadParameter(description:
"Object")]
BadObject o)
404 return ctx.Scope.Provider.GetExtensions(o);
411 [BadMethod(description:
"Lists all global extension names")]
412 [return: BadReturn(
"An Array containing all Extension Names")]
415 return new BadArray(ctx.Scope.Provider.GetExtensionNames()
424 [BadMethod(description:
"Gets the Commandline Arguments")]
425 [return: BadReturn(
"An Array containing all Commandline Arguments")]
436 [BadMethod(description:
"Gets the Attributes of the given objects members")]
437 [return: BadReturn(
"A Table containing the Attributes of the given objects members.")]
442 return cls.Scope.GetMemberInfos();
456 foreach (
BadObject o
in ctx.Execute(exprs))
461 yield
return ctx.Scope.GetExports();
471 private static IEnumerable<BadObject>
SafeExecute(IEnumerable<BadObject> script,
473 Func<BadTask> getTask)
475 using IEnumerator<
BadObject>? enumerator = script.GetEnumerator();
481 if (!enumerator.MoveNext())
489 .Runnable.SetError(e.
Error);
Exposes the BadScript Runtime Functionality to Consumers.
BadExecutionContext CreateContext(string workingDirectory)
Creates a new Context with the configured Options.
Public interface for the filesystem abstraction of the BadScript Engine.
static IFileSystem Instance
File System implementation.
Implements the "Runtime" API.
static BadArray GetMembers(BadObject obj)
BadScope GetRootScope(BadExecutionContext ctx)
static IEnumerable< BadObject > SafeExecute(IEnumerable< BadObject > script, BadExecutionContext ctx, Func< BadTask > getTask)
Wrapper that will execute a script and catch any errors that occur.
BadArray GetNativeTypes()
static BadObject ParseDate([BadParameter(description:"The date string")] string date)
Parses a date string.
BadArray GetRegisteredApis(BadExecutionContext ctx)
IEnumerable< BadObject > ExecuteScriptWithExports(BadExecutionContext ctx, IEnumerable< BadExpression > exprs)
Executes a script and returns the exported variables.
string GetRuntimeAssemblyPath()
static BadTable ValidateSource([BadParameter(description:"The Source to Validate")] string source, [BadParameter(description:"The File Name")] string file)
Validates a source string.
BadObject IsApiRegistered(BadExecutionContext ctx, [BadParameter(description:"The Api Name")] string api)
void DeleteReference(BadExecutionContext ctx, BadFunction func)
static ? IEnumerable< string > StartupArguments
The Startup Arguments that were passed to the Runtime.
string GetStackTrace(BadExecutionContext ctx)
void SetReferenceValue(BadExecutionContext ctx, BadFunction func, BadObject value)
static BadTable GetExtensions(BadExecutionContext ctx, [BadParameter(description:"Object")] BadObject o)
Returns all Extensions of the given object(will create the objects based on the supplied object)
BadObject MakeReference(BadExecutionContext ctx, [BadParameter(description:"The Text for the Reference")] string refText, [BadParameter(description:"The getter Function")] BadFunction get, [BadParameter(description:"The setter Function")] BadFunction? set=null, [BadParameter(description:"The delete Function")] BadFunction? delete=null)
BadObject GetReferenceValue(BadExecutionContext ctx, BadFunction func)
override void AdditionalData(BadTable target)
BadScope CreateDefaultScope(BadExecutionContext ctx)
static void RegisterImportHandler(BadExecutionContext ctx, [BadParameter(description:"An Import handler implementing the IImportHandler Interface", nativeType:"IImportHandler")] BadClass cls)
Registers an Import Handler.
BadTask EvaluateAsync(BadExecutionContext caller, [BadParameter(description:"The Source of the Script")] string source, [BadParameter(description:"An (optional but recommended) file path, it will be used to determine the working directory of the script.")] string? file=null, [BadParameter(description:"If true, any optimizations that are activated in the settings will be applied.")] bool optimize=true, [BadParameter(description:"An (optional) scope that the execution takes place in, if not specified, an Instance of BadRuntime will get searched and a scope will be created from it, if its not found, a scope will be created from the root scope of the caller.")] BadScope? scope=null, [BadParameter(description:"If true, the last element that was returned from the enumeration will be the result of the task. Otherwise the result will be the exported objects of the script.")] bool setLastAsReturn=false)
static BadArray GetExtensionNames(BadExecutionContext ctx, [BadParameter(description:"Object")] BadObject o)
Returns all Extension Names of the given object.
static BadArray GetArguments()
Returns the arguments passed to the script.
BadTable MakeNative()
Creates the "Native" Table.
static BadObject GetTimeNow()
Returns the Current Time.
static BadObject GetGlobalExtensionNames(BadExecutionContext ctx)
Lists all global extension names.
Implements a Runnable that can return a value.
Implements a Task Object.
Implements a simple constant folding optimization.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
Contains the Implementation of the Constant Substitution Optimization This optimization replaces expr...
static IEnumerable< BadExpression > Optimize(BadConstantSubstitutionOptimizerScope scope, IEnumerable< BadExpression > expressions)
Substitutes all variables in the expressions with their constant value.
The Parser of the Language. It turns Source Code into an Expression Tree.
static BadSourceParser Create(string fileName, string source)
Creates a BadSourceParser Instance based on the source and filename provided.
static IEnumerable< BadExpression > Parse(string fileName, string source)
Parses a BadExpression from the Source Reader.
The Execution Context. Every execution of a script needs a context the script is running in....
IEnumerable< BadObject > Execute(IEnumerable< BadExpression > expressions)
Executes an enumeration of expressions.
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
BadScope GetRootScope()
Returns the Root Scope of the Scope.
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
string GetStackTrace()
Returns the Stack Trace of the Current scope.
Gets thrown if the runtime encounters an error.
Implements the Error Object Type.
static BadRuntimeError FromException(Exception e, string? scriptStackTrace=null)
Creates a BadRuntimeError from an Exception.
Gets thrown by the runtime.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implements an Interop API for the BS2 Language.
The Class that manages the importing of modules.
BadModuleImporter AddHandler(BadImportHandler handler)
Adds a new Import Handler to the Importer.
Implements a Dynamic List/Array for the BadScript Language.
The Base Class for all BadScript Objects.
static readonly BadObject Null
The Null Value for the BadScript Language.
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? delete=null)
Creates a new Reference Object.
Implements a Table Structure for the BadScript Language.
Implements a function that can be called from the script.
Implements a Type Instance in the BadScript Language.
Implements a Class Prototype for the BadScript Language.
override BadClassPrototype GetPrototype()
Helper Class that Builds a Native Class from a Prototype.
static readonly BadInterfacePrototype ImportHandler
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
static IEnumerable< BadClassPrototype > NativeTypes
Enumeration of all Native Class Prototypes.
string GetFullPath(string path)
Returns the full path of the given path.
Contains Shared Data Structures and Functionality.
Contains IO Implementation for the BadScript2 Runtime.
Contains Common Interop APIs for the BadScript2 Runtime.
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains the BadScript2 Constant Folding Optimizations.
Contains the BadScript2 Constant Substitution Optimizations.
Contains the Expressions for the BadScript2 Language.
Contains the Comparison Operators for the BadScript2 Language.
Contains the Parser for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Extension Classes for Functions.
Contains the Interop Function Classes for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Contains Runtime Type Objects.
Contains the Runtime Objects.
Contains Runtime Settings Objects.
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.
Implements a context for expression validation.
bool IsError
Indicates whether there are any messages of type Error.
static BadExpressionValidatorContext Validate(IEnumerable< BadExpression > expressions)
Validates the given expressions.
override string ToString()
Returns a string representation of the validation results.
IReadOnlyList< BadExpressionValidatorMessage > Messages
The messages generated by the validators.