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")]
63 [BadMethod(description:
"Evaluates a BadScript Source String")]
64 [return: BadReturn(
"An Awaitable Task")]
67 [BadParameter(description:
"The Source of the Script")]
69 [BadParameter(description:
"An (optional but recommended) file path, it will be used to determine the working directory of the script.")]
71 [BadParameter(description:
"If true, any optimizations that are activated in the settings will be applied.")]
75 "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."
80 "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."
82 bool setLastAsReturn = false)
87 bool optimizeConstantFolding =
88 BadNativeOptimizationSettings.Instance.UseConstantFoldingOptimization && optimize;
89 bool optimizeConstantSubstitution =
90 BadNativeOptimizationSettings.Instance.UseConstantSubstitutionOptimization && optimize;
94 if (runtime !=
null && scope ==
null)
98 ctx = runtime.
CreateContext(Path.GetDirectoryName(file) ??
"/");
107 caller.Scope.GetRootScope()
108 .CreateChild(
"<EvaluateAsync>", caller.Scope,
null,
BadScopeFlags.CaptureThrow)
115 if (optimizeConstantFolding)
120 if (optimizeConstantSubstitution)
127 IEnumerable<BadObject> executor;
140 SafeExecute(executor, ctx, () => task).GetEnumerator(),
149 [BadMethod(description:
"Returns the Stack Trace of the current Execution Context")]
150 [return: BadReturn(
"The Stack Trace")]
153 return ctx.Scope.GetStackTrace();
164 [BadMethod(description:
"Returns all Native Types")]
165 [return: BadReturn(
"An Array containing all Native Types")]
171 [BadMethod(description:
"Returns the Assembly Path of the current Runtime")]
172 [return: BadReturn(
"The Assembly Path")]
175 string path = Path.ChangeExtension(Assembly.GetEntryAssembly()!.Location, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
"exe" :
"");
180 [BadMethod(description:
"Returns a new Guid")]
181 [return: BadReturn(
"A new Guid")]
184 return Guid.NewGuid().ToString();
187 [BadMethod(description:
"Returns true if an api with that name is registered")]
188 [return: BadReturn(
"True if the api is registered")]
191 return ctx.Scope.RegisteredApis.Contains(api);
194 [BadMethod(description:
"Creates a new Reference Object")]
195 [return: BadReturn(
"The Reference Object")]
198 [BadParameter(description:
"The Text for the Reference")] string refText,
199 [BadParameter(description:
"The getter Function")]
BadFunction get,
200 [BadParameter(description:
"The setter Function")]
BadFunction? set = null,
201 [BadParameter(description:
"The delete Function")]
BadFunction? delete = null)
203 if(
set ==
null &&
delete ==
null)
208 if (
delete ==
null &&
set !=
null)
215 if (
delete !=
null &&
set ==
null)
219 (Action<BadObject, BadPropertyInfo?>?)
null,
242 return result.Dereference();
246 foreach (
BadObject? o
in func.Invoke(
new []{value}, ctx))
251 [BadMethod(description:
"Returns all registered apis")]
252 [return: BadReturn(
"An Array containing all registered apis")]
255 return new BadArray(ctx.Scope.RegisteredApis.Select(x => (
BadObject)x).ToList());
265 [BadMethod(description:
"Registers an Import Handler")]
268 [BadParameter(description:
"An Import handler implementing the IImportHandler Interface", nativeType:
"IImportHandler")]
278 if (importer ==
null)
293 [BadMethod(
"Validate",
"Validates a source string")]
294 [
return: BadReturn(
"Validation Result")]
295 private static BadTable ValidateSource([BadParameter(description:
"The Source to Validate")] string source, [BadParameter(description:
"The File Name")] string file)
300 ret.SetProperty(
"IsError", result.
IsError);
307 BadTable msg = new BadTable();
308 msg.SetProperty(
"Message", x.Message);
309 msg.SetProperty(
"Validator", x.Validator.ToString());
310 msg.SetProperty(
"Type", x.Type.ToString());
311 msg.SetProperty(
"Position", x.Expression.Position.ToString());
313 return (BadObject)msg;
329 [BadMethod(description:
"Parses a date string")]
330 [return: BadReturn(
"Bad Table with the parsed date")]
333 DateTimeOffset d = DateTimeOffset.Parse(date);
355 var c = CultureInfo.InvariantCulture;
356 if (args.Length == 1 && args[0] is
IBadString str)
358 c = new CultureInfo(str.Value);
361 return c.Calendar.GetWeekOfYear(time.DateTime, c.DateTimeFormat.CalendarWeekRule,
362 c.DateTimeFormat.FirstDayOfWeek);
379 args.Length < 1 ?
null : ((
IBadString)args[0]).Value
381 .ToShortTimeString(),
401 args.Length < 1 ?
null : ((
IBadString)args[0]).Value
403 .ToShortDateString(),
423 args.Length < 1 ?
null : ((
IBadString)args[0]).Value
445 args.Length < 1 ?
null : ((
IBadString)args[0]).Value
469 var c = CultureInfo.InvariantCulture;
470 if (args.Length == 3 && args[2] is
IBadString str)
472 c =
new CultureInfo(str.Value);
475 string? timeZone = args.Length < 2 ? null : (args[1] as
IBadString)?.Value;
481 .ToString(format, c);
513 table.SetFunction<decimal>(
"AddYears", d =>
GetDateTime(time.AddYears((
int)d)));
514 table.SetFunction<decimal>(
"AddMonths", d =>
GetDateTime(time.AddMonths((
int)d)));
515 table.SetFunction<decimal>(
"AddDays", d =>
GetDateTime(time.AddDays((
double)d)));
516 table.SetFunction<decimal>(
"AddHours", d =>
GetDateTime(time.AddHours((
double)d)));
517 table.SetFunction<decimal>(
"AddMinutes", d =>
GetDateTime(time.AddMinutes((
double)d)));
518 table.SetFunction<decimal>(
"AddSeconds", d =>
GetDateTime(time.AddSeconds((
double)d)));
519 table.SetFunction<decimal>(
"AddMilliseconds", d =>
GetDateTime(time.AddMilliseconds((
double)d)));
520 table.SetFunction<decimal>(
"AddTicks", d =>
GetDateTime(time.AddTicks((
long)d)));
544 DateTimeOffset dtOffset =
545 new DateTimeOffset(year, month, day, hour, minute, second, millisecond, TimeSpan.Parse(offset));
547 DateTime dateTime = dtOffset.DateTime;
549 if (timeZone !=
null)
551 dateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, timeZone);
561 [BadMethod(description:
"Returns the Current Time")]
562 [return: BadReturn(
"The Current Time")]
574 [BadMethod(description:
"Lists all extension names of the given object")]
575 [return: BadReturn(
"An Array containing all Extension Names")]
578 return new BadArray(ctx.Scope.Provider.GetExtensionNames(o).ToList());
585 [BadMethod(description:
"Lists all global extension names")]
586 [return: BadReturn(
"An Array containing all Extension Names")]
589 return new BadArray(ctx.Scope.Provider.GetExtensionNames().ToList());
596 [BadMethod(description:
"Gets the Commandline Arguments")]
597 [return: BadReturn(
"An Array containing all Commandline Arguments")]
604 [BadMethod(description:
"Gets the Attributes of the given objects members")]
605 [return: BadReturn(
"A Table containing the Attributes of the given objects members.")]
610 return cls.Scope.GetMemberInfos();
624 foreach (
BadObject o
in ctx.Execute(exprs))
629 yield
return ctx.Scope.GetExports();
640 IEnumerable<BadObject> script,
642 Func<BadTask> getTask)
644 using IEnumerator<
BadObject>? enumerator = script.GetEnumerator();
649 if (!enumerator.MoveNext())
656 getTask().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()
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)
static BadObject ParseDate([BadParameter(description:"The date string")] string date)
Parses a date string.
BadArray GetRegisteredApis(BadExecutionContext ctx)
static DateTime CreateDate(BadTable dateTable, string? timeZone=null)
Creates a DateTime from a BadTable.
static BadTable GetDateTime(DateTimeOffset time)
Converts a DateTimeOffset to a Bad Table.
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.
static BadTable GetTimeNow()
Returns the Current Time.
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)
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 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.
Interop Function taking an array of arguments.
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< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
Stores Meta Information about a Property.
Implements a Table Structure for the BadScript Language.
Dictionary< string, BadObject > InnerTable
The Inner Table for this Object.
Implements a function that can be called from the script.
static readonly BadClassPrototype Prototype
The Prototype for the Function Object.
Provides function parameter info.
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.
Implements the Interface for Native Numbers.
Implements the Interface for Native Strings.
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.