BadScript 2
Loading...
Searching...
No Matches
BadNumberExtension.cs
Go to the documentation of this file.
1using System.Globalization;
2
10
12
17{
21 private static readonly Dictionary<string, CultureInfo> s_Cultures = new Dictionary<string, CultureInfo>();
22
30 private static BadObject NumberToString(decimal d, IReadOnlyList<BadObject> args)
31 {
32 if (args.Count == 0)
33 {
34 return d.ToString(CultureInfo.InvariantCulture);
35 }
36
37 IBadString format = (IBadString)args[0]; //Type is checked in the function builder
38
39 if (args.Count == 1)
40 {
41 return d.ToString(format.Value);
42 }
43
44 if (args.Count != 2)
45 {
46 throw new BadRuntimeException("Invalid number of arguments");
47 }
48
49 IBadString culture = (IBadString)args[1]; //Type is checked in the function builder
50
51 if (s_Cultures.TryGetValue(
52 culture.Value,
53 out CultureInfo? cultureInfo
54 )) //Cache Culture info to avoid creating it every time
55 {
56 return d.ToString(format.Value, cultureInfo);
57 }
58
59 cultureInfo = CultureInfo.CreateSpecificCulture(culture.Value);
60 s_Cultures.Add(culture.Value, cultureInfo);
61
62 return d.ToString(format.Value, cultureInfo);
63 }
64
66 protected override void AddExtensions(BadInteropExtensionProvider provider)
67 {
68 provider.RegisterObject<decimal>(
69 "ToString",
70 d => new BadInteropFunction(
71 "ToString",
72 a => NumberToString(d, a),
73 false,
75 new BadFunctionParameter("format", true, true, false, null, BadNativeClassBuilder.GetNative("string")),
77 "culture",
78 true,
79 true,
80 false,
81 null,
83 )
84 )
85 );
86 }
87}
override void AddExtensions(BadInteropExtensionProvider provider)
static readonly Dictionary< string, CultureInfo > s_Cultures
Culture Info Cache.
static BadObject NumberToString(decimal d, IReadOnlyList< BadObject > args)
Formats a number to a string.
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:210
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