ModuleRegister.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using MoonSharp.Interpreter.CoreLib;
  7. using MoonSharp.Interpreter.Execution;
  8. namespace MoonSharp.Interpreter
  9. {
  10. public static class ModuleRegister
  11. {
  12. public static Table RegisterCoreModules(this Table table, CoreModules modules)
  13. {
  14. if (modules.Has(CoreModules.GlobalConsts)) RegisterConstants(table);
  15. if (modules.Has(CoreModules.TableIterators)) RegisterModuleType<TableIterators>(table);
  16. if (modules.Has(CoreModules.Metatables)) RegisterModuleType<MetaTableMethods>(table);
  17. return table;
  18. }
  19. public static Table RegisterConstants(this Table table)
  20. {
  21. table["_G"] = DynValue.NewTable(table);
  22. table["_VERSION"] = DynValue.NewString(string.Format("Moon# {0}",
  23. Assembly.GetExecutingAssembly().GetName().Version.Major,
  24. Assembly.GetExecutingAssembly().GetName().Version.Minor));
  25. table["_MOONSHARP"] = DynValue.NewString(Assembly.GetExecutingAssembly().GetName().Version.ToString());
  26. return table;
  27. }
  28. public static Table RegisterModuleType(this Table table, Type t)
  29. {
  30. foreach (MethodInfo mi in t.GetMethods(BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic).Where(_mi => _mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).Length > 0))
  31. {
  32. MoonSharpMethodAttribute attr = (MoonSharpMethodAttribute)mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).First();
  33. ParameterInfo[] pi = mi.GetParameters();
  34. if (pi.Length != 2 || pi[0].ParameterType != typeof(IExecutionContext)
  35. || pi[1].ParameterType != typeof(CallbackArguments) || mi.ReturnType != typeof(DynValue))
  36. {
  37. throw new ArgumentException(string.Format("Method {0} does not have the right signature.", mi.Name));
  38. }
  39. Func<IExecutionContext, CallbackArguments, DynValue> func = (Func<IExecutionContext, CallbackArguments, DynValue>)Delegate.CreateDelegate(typeof(Func<IExecutionContext, CallbackArguments, DynValue>), mi);
  40. string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : mi.Name;
  41. table[name] = DynValue.NewCallback(func);
  42. }
  43. return table;
  44. }
  45. public static Table RegisterModuleType<T>(this Table table)
  46. {
  47. return RegisterModuleType(table, typeof(T));
  48. }
  49. public static Table RegisterModuleObject(this Table table, object o)
  50. {
  51. Type t = o.GetType();
  52. foreach (MethodInfo mi in t.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic).Where(_mi => _mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).Length > 0))
  53. {
  54. MoonSharpMethodAttribute attr = (MoonSharpMethodAttribute)mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).First();
  55. ParameterInfo[] pi = mi.GetParameters();
  56. if (pi.Length != 2 || pi[0].ParameterType != typeof(IExecutionContext)
  57. || pi[1].ParameterType != typeof(CallbackArguments) || mi.ReturnType != typeof(DynValue))
  58. {
  59. throw new ArgumentException(string.Format("Method {0} does not have the right signature.", mi.Name));
  60. }
  61. Func<IExecutionContext, CallbackArguments, DynValue> func = (Func<IExecutionContext, CallbackArguments, DynValue>)
  62. Delegate.CreateDelegate(typeof(Func<IExecutionContext, CallbackArguments, DynValue>), o, mi);
  63. string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : mi.Name;
  64. table[name] = DynValue.NewCallback(func);
  65. }
  66. return table;
  67. }
  68. }
  69. }