BadScript 2
Loading...
Searching...
No Matches
BadNumberExtension.cs
Go to the documentation of this file.
1using System.Globalization;
2
11
13
18{
22 private static readonly Dictionary<string, CultureInfo> s_Cultures = new Dictionary<string, CultureInfo>();
23
31 private static BadObject NumberToString(BadExecutionContext ctx, decimal d, IReadOnlyList<BadObject> args)
32 {
33 if (args.Count == 0)
34 {
35 return d.ToString(CultureInfo.InvariantCulture);
36 }
37
38 IBadString format = (IBadString)args[0]; //Type is checked in the function builder
39
40 if (args.Count == 1)
41 {
42 CultureInfo? defaultCulture = ctx.Scope.GetSingleton<BadRuntime>()
43 ?.Culture ??
44 CultureInfo.InvariantCulture;
45
46 return d.ToString(format.Value, defaultCulture);
47 }
48
49 if (args.Count != 2)
50 {
51 throw new BadRuntimeException("Invalid number of arguments");
52 }
53
54 IBadString culture = (IBadString)args[1]; //Type is checked in the function builder
55
56 if (s_Cultures.TryGetValue(culture.Value,
57 out CultureInfo? cultureInfo
58 )) //Cache Culture info to avoid creating it every time
59 {
60 return d.ToString(format.Value, cultureInfo);
61 }
62
63 cultureInfo = CultureInfo.CreateSpecificCulture(culture.Value);
64 s_Cultures.Add(culture.Value, cultureInfo);
65
66 return d.ToString(format.Value, cultureInfo);
67 }
68
70 protected override void AddExtensions(BadInteropExtensionProvider provider)
71 {
72 provider.RegisterObject<decimal>("ToString",
73 d => new BadInteropFunction("ToString",
74 (c, a) => NumberToString(c, d, a),
75 false,
77 new BadFunctionParameter("format",
78 true,
79 true,
80 false,
81 null,
83 ),
84 new BadFunctionParameter("culture",
85 true,
86 true,
87 false,
88 null,
90 )
91 )
92 );
93 }
94}
Exposes the BadScript Runtime Functionality to Consumers.
Definition BadRuntime.cs:30
override void AddExtensions(BadInteropExtensionProvider provider)
static readonly Dictionary< string, CultureInfo > s_Cultures
Culture Info Cache.
static BadObject NumberToString(BadExecutionContext ctx, decimal d, IReadOnlyList< BadObject > args)
Formats a number to a string.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Public Extension API for the BS2 Runtime.
void RegisterObject(Type t, string propName, BadObject obj)
Registers the specified extension for the specified type.
Interop Function taking an array of arguments.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:224
Helper Class that Builds a Native Class from a Prototype.
static BadClassPrototype GetNative(string name)
Returns a Native Class Prototype for the given Native Type.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
new string Value
The String Value.
Definition IBadString.cs:11
Contains Common Interop Extensions for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
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.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.