-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
454 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.SmallBasic.Library; | ||
using System; | ||
|
||
/// <summary> | ||
/// The Arguments object provides operations to access the command-line arguments that were passed at the start of this program. | ||
/// This class has been taken from Small Basic version 0.2. | ||
/// </summary> | ||
[SmallBasicType] | ||
public static class ZSArguments | ||
{ | ||
private static string[] args = Environment.GetCommandLineArgs(); | ||
|
||
/// <summary> | ||
/// Gets the number of command-line arguments passed to this program. | ||
/// </summary> | ||
public static Primitive Count | ||
{ | ||
get { return (args.Length != 0) ? (args.Length - 1) : 0; } | ||
} | ||
|
||
/// <summary> | ||
/// Returns the specified argument. | ||
/// </summary> | ||
/// <param name="index"> | ||
/// Index of the argument. | ||
/// </param> | ||
/// <returns> | ||
/// The command-line argument at the specified index. | ||
/// </returns> | ||
public static Primitive GetArgument(Primitive index) | ||
{ | ||
int num = index; | ||
if (num >= 1 && num < args.Length) | ||
{ | ||
return new Primitive(args[num]); | ||
} | ||
return new Primitive(""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using Microsoft.SmallBasic.Library; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
/// <summary> | ||
/// The Platform object provides a way to generically invoke other .NET libraries. | ||
/// This class has been taken from Small Basic version 0.2. | ||
/// </summary> | ||
[SmallBasicType] | ||
public static class ZSPlatform | ||
{ | ||
private static Dictionary<string, int> _nameGenerationMap = new Dictionary<string, int>(); | ||
|
||
private static Dictionary<string, object> _objectMap = new Dictionary<string, object>(); | ||
|
||
/// <summary> | ||
/// Creates an instance of a specified .NET type. | ||
/// For Example : System.DateTime | ||
/// </summary> | ||
/// <param name="typeName">The fully qualified name of the type.</param> | ||
/// <returns>A unique identifier for the created instance or "ERROR" if creation fails.</returns> | ||
public static Primitive CreateInstance(Primitive typeName) | ||
{ | ||
try | ||
{ | ||
Type type = Type.GetType(typeName); | ||
object value = Activator.CreateInstance(type); | ||
string text = GenerateNewName("Instance"); | ||
_objectMap[text] = value; | ||
return text; | ||
} | ||
catch (Exception ex) | ||
{ | ||
return "ERROR : " + ex.Message; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Invokes a method on an instance of a .NET type. | ||
/// </summary> | ||
/// <param name="instanceId">The unique identifier of the instance.</param> | ||
/// <param name="methodName">The name of the method to invoke.</param> | ||
/// <param name="argumentsStackName">The name of the stack containing the method arguments.</param> | ||
/// <returns>The result of the method invocation or "ERROR" if invocation fails.</returns> | ||
public static Primitive InvokeInstanceMethod(Primitive instanceId, Primitive methodName, Primitive argumentsStackName) | ||
{ | ||
try | ||
{ | ||
object obj = _objectMap[instanceId]; | ||
MethodInfo method = obj.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); | ||
int num = Stack.GetCount(argumentsStackName); | ||
ParameterInfo[] parameters = method.GetParameters(); | ||
if (parameters.Length == num) | ||
{ | ||
List<object> list = new List<object>(); | ||
for (int num2 = num - 1; num2 >= 0; num2--) | ||
{ | ||
list[num2] = Stack.PopValue(argumentsStackName); | ||
} | ||
return new Primitive(method.Invoke(obj, list.ToArray())); | ||
} | ||
return "ERROR"; | ||
} | ||
catch (Exception ex) | ||
{ | ||
return "ERROR : " + ex.Message; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Invokes a static method on a .NET type. | ||
/// </summary> | ||
/// <param name="typeName">The fully qualified name of the type.</param> | ||
/// <param name="methodName">The name of the method to invoke.</param> | ||
/// <param name="argumentsStackName">The name of the stack containing the method arguments.</param> | ||
/// <returns>The result of the method invocation or "ERROR" if invocation fails.</returns> | ||
public static Primitive InvokeStaticMethod(Primitive typeName, Primitive methodName, Primitive argumentsStackName) | ||
{ | ||
try | ||
{ | ||
Type type = Type.GetType(typeName); | ||
MethodInfo method = type.GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.Public); | ||
int num = Stack.GetCount(argumentsStackName); | ||
ParameterInfo[] parameters = method.GetParameters(); | ||
if (parameters.Length == num) | ||
{ | ||
List<object> list = new List<object>(); | ||
for (int num2 = num - 1; num2 >= 0; num2--) | ||
{ | ||
list[num2] = Stack.PopValue(argumentsStackName); | ||
} | ||
return new Primitive(method.Invoke(null, list.ToArray())); | ||
} | ||
return "ERROR"; | ||
} | ||
catch (Exception ex) | ||
{ | ||
return "ERROR : " + ex.Message; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Generates a new unique name with a specified prefix. | ||
/// </summary> | ||
/// <param name="prefix">The prefix for the generated name.</param> | ||
/// <returns>A new unique name.</returns> | ||
internal static string GenerateNewName(string prefix) | ||
{ | ||
int num = 0; | ||
_nameGenerationMap.TryGetValue(prefix, out num); | ||
num++; | ||
_nameGenerationMap[prefix] = num; | ||
return prefix + num.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.