/// <summary> /// Initializes a new instance of the <see cref="Script"/> class. /// </summary> /// <param name="coreModules">The core modules to be pre-registered in the default global table.</param> public Script(CoreModules coreModules) { Options = new ScriptOptions(DefaultOptions); PerformanceStats = new PerformanceStatistics(); Registry = new Table(this); m_ByteCode = new ByteCode(this); m_MainProcessor = new Processor(this, m_GlobalTable, m_ByteCode); m_GlobalTable = new Table(this).RegisterCoreModules(coreModules); foreach (var t in TypeDescriptorRegistry.RegisteredTypes) { Type valType = t.Value.Type; if (valType.GetTypeInfo().Assembly.FullName != typeof(Script).GetTypeInfo().Assembly.FullName) { // m_GlobalTable.RegisterModuleType(t.Value.Type); if (t.Value is StandardUserDataDescriptor) { StandardUserDataDescriptor desc = (StandardUserDataDescriptor)t.Value; foreach (var member in desc.Members) { if (member.Value is MethodMemberDescriptor) { MethodMemberDescriptor methDesc = (MethodMemberDescriptor)member.Value; if (methDesc.IsConstructor) { m_GlobalTable.Set(methDesc.Name, methDesc.GetCallbackAsDynValue(this)); } } else if (member.Value is OverloadedMethodMemberDescriptor) { OverloadedMethodMemberDescriptor methDesc = (OverloadedMethodMemberDescriptor)member.Value; foreach (var overloadDesc in methDesc.m_Overloads) { if (overloadDesc is MethodMemberDescriptor) { MethodMemberDescriptor actualDesc = (MethodMemberDescriptor)overloadDesc; if (actualDesc.IsConstructor) { //m_GlobalTable.Set(desc.FriendlyName, actualDesc.GetCallbackAsDynValue(this)); m_GlobalTable.Set(desc.FriendlyName, DynValue.NewCallback(methDesc.GetCallbackFunction(this))); break; } } } } } } } } }
public static Table RegisterModuleType(this Table gtable, Type t) { Table table = CreateModuleNamespace(gtable, t); foreach (MethodInfo mi in t.GetMethods(BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic)) { if (mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).Length > 0) { MoonSharpMethodAttribute attr = (MoonSharpMethodAttribute)mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).First(); if (!ConversionHelper.CheckCallbackSignature(mi)) { throw new ArgumentException(string.Format("Method {0} does not have the right signature.", mi.Name)); } Func <ScriptExecutionContext, CallbackArguments, DynValue> func = (Func <ScriptExecutionContext, CallbackArguments, DynValue>)Delegate.CreateDelegate(typeof(Func <ScriptExecutionContext, CallbackArguments, DynValue>), mi); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : mi.Name; table.Set(name, DynValue.NewCallback(func, name)); } else if (mi.Name == "MoonSharpInit") { object[] args = new object[2] { gtable, table }; mi.Invoke(null, args); } } foreach (FieldInfo fi in t.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic).Where(_mi => _mi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).Length > 0)) { MoonSharpMethodAttribute attr = (MoonSharpMethodAttribute)fi.GetCustomAttributes(typeof(MoonSharpMethodAttribute), false).First(); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : fi.Name; RegisterScriptField(fi, null, table, t, name); } foreach (FieldInfo fi in t.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic).Where(_mi => _mi.GetCustomAttributes(typeof(MoonSharpConstantAttribute), false).Length > 0)) { MoonSharpConstantAttribute attr = (MoonSharpConstantAttribute)fi.GetCustomAttributes(typeof(MoonSharpConstantAttribute), false).First(); string name = (!string.IsNullOrEmpty(attr.Name)) ? attr.Name : fi.Name; RegisterScriptFieldAsConst(fi, null, table, t, name); } return(gtable); }