public void TestTemplateOneOf()
 {
     Random random = new Random();
     var document = new SimpleDocument("The letter is {OneOf(\"a\", \"b\", \"c\", \"d\", null)}.");
     var store = new BuiltinStore();
     store["OneOf"] = new NativeFunction((values) =>
     {
         return values[random.Next(values.Count)];
     });
     store["system"] = "Alrai";
     List<string> results = new List<string>();
     for (int i = 0; i < 1000; i++)
     {
         results.Add(document.Render(store));
     }
     Assert.IsTrue(results.Contains(@"The letter is a."));
     results.RemoveAll(result => result == @"The letter is a.");
     Assert.IsTrue(results.Contains(@"The letter is b."));
     results.RemoveAll(result => result == @"The letter is b.");
     Assert.IsTrue(results.Contains(@"The letter is c."));
     results.RemoveAll(result => result == @"The letter is c.");
     Assert.IsTrue(results.Contains(@"The letter is d."));
     results.RemoveAll(result => result == @"The letter is d.");
     Assert.IsTrue(results.Contains(@"The letter is ."));
     results.RemoveAll(result => result == @"The letter is .");
     Assert.IsTrue(results.Count == 0);
 }
Example #2
0
 public static Continuation Create(object self, NativeFunction function)
 {
     Continuation c = new Continuation();
     c.ExecutionPointer = 1;
     c.ParentContinuation = null;
     c.Frame = new Frame(self, function);
     return c;
 }
 public void TestTemplateFunctional()
 {
     var document = new SimpleDocument(@"You are entering the {System(system)} system.");
     var store = new BuiltinStore();
     store["System"] = new NativeFunction((values) =>
     {
         return Translations.StarSystem(values[0].AsString);
     }, 1);
     store["system"] = "Alrai";
     var result = document.Render(store);
     Assert.AreEqual("You are entering the <phoneme alphabet=\"ipa\" ph=\"ˈalraɪ\">Alrai</phoneme> system.", result);
 }
Example #4
0
        public static void Boot(NativeFunction initFunction)
        {
            if (_booting)
            {
                throw new NativeError("XaeiOS already booting.");
            }
            if (_booted)
            {
                throw new NativeError("XaeiOS already booted.");
            }
            if (initFunction == null)
            {
                throw new NativeError("Cannot boot with null init function.");
            }
            _booting = true;
            Logging.Trace("Initializing kernel");
            SystemCalls.InitializeKernel();
            Logging.Trace("Initializing OSCorlib");
            OSCorlib.Initialize();

            // create kernel thread and sip
            Logging.Trace("Starting kernel");
            TaskHandle kernelTask = SystemCalls.GetCurrentTask();
            SIP kernelSIP = SIP.CreateSystemSIP("XaeiOS.Kernel");
            Thread kernelThread = Thread.CreateSystemThread(ThreadPriority.Realtime, kernelSIP, kernelTask, "XaeiOS.Kernel");
            kernelSIP.InitializeThread(kernelThread);
            SIPManager.RegisterSIP(kernelSIP);
            ThreadManager.RegisterThread(kernelThread);
            ResourceManager.RegisterSIP(kernelSIP);
            kernelSIP.AddPrincipal(new RootPrincipal(new RootIdentity()));
            SystemCalls.StartKernel();

            // create external thread and sip
            Logging.Trace("Creating the External process");
            TaskHandle externalTask = SystemCalls.GetExternalTask();
            string externalProcessName = "External";
            SIP externalSIP = SIP.CreateSystemSIP(externalProcessName);
            Thread externalThread = Thread.CreateSystemThread(ThreadPriority.Realtime, externalSIP, externalTask, externalProcessName);
            SIPManager.RegisterSIP(externalSIP);
            ResourceManager.RegisterSIP(externalSIP);
            ThreadManager.RegisterThread(externalThread);
            externalSIP.AddPrincipal(new ExternalPrincipal(new ExternalIdentity()));

            Logging.Trace("Starting boot loader");
            _initFunction = initFunction;
            TaskHandle task = SystemCalls.CreateTask(null, new TaskFunction(CreateInitProcess), new TaskCallback(BootCallback), TaskPriority.Normal, "XaeiOS.Boot");
            SystemCalls.StartTask(task);
        }
Example #5
0
 public static extern var SetTimeout(NativeFunction f, long timeout);
Example #6
0
 public static extern var SetInterval(NativeFunction f, long timeout);
Example #7
0
 public Frame(object self, NativeFunction function)
 {
     This = self;
     Function = function;
 }
Example #8
0
 public static extern NativeFunction GenerateCodeForBoundNativeFunction(NativeFunction fn, var self, var[] args);
Example #9
0
 public static extern NativeFunction GenerateCodeForBoundNativeFunctionSimple(NativeFunction fn, var self);
Example #10
0
 private static void InternalCreate(Delegate d, object target, NativeFunction function)
 {
     d._target = target;
     d._function = function;
 }
 public OptimisticStackRebuildingContext(object self, NativeFunction function)
 {
     This = self;
     Function = function;
 }
Example #12
0
 internal static MulticastDelegate InternalCreate(object target, NativeFunction function)
 {
     return new MulticastDelegate(target, function);
 }
Example #13
0
 protected MulticastDelegate(object target, NativeFunction function)
     : base(target, function)
 {
 }
Example #14
0
        public void ExpressionInvoke(string symbol, string expected)
        {
            Action<IStore>	populate;

            populate = (scope) =>
            {
                scope[symbol] = expected;
                scope["f"] = new NativeFunction ((a, s, o) =>
                {
                    string	value;

                    value = s[a[0]].AsString;

                    o.Write (value);

                    return value;
                }, 1);
            };

            this.AssertRender ("{f('" + symbol + "')}", ((Value)expected).AsString + ((Value)expected).AsString, DefaultSetting.Instance, populate, (d) => {});
            this.AssertReturn ("{return f('" + symbol + "')}", ((Value)expected).ToString (), DefaultSetting.Instance, populate, (d) => {});
        }
Example #15
0
 protected Delegate(object target, NativeFunction function)
 {
     InternalCreate(this, target, function);
 }
        /// <summary>
        /// Build a store from a list of variables
        /// </summary>
        private BuiltinStore buildStore(Dictionary<string, Cottle.Value> vars)
        {
            BuiltinStore store = new BuiltinStore();

            // Function to call another script
            store["F"] = new NativeFunction((values) =>
            {
                return new ScriptResolver(scripts).resolve(values[0].AsString, store);
            }, 1);

            // Translation functions
            store["P"] = new NativeFunction((values) =>
            {
                string val = values[0].AsString;
                string translation = val;
                if (translation == val)
                {
                    translation = Translations.Faction(val);
                }
                if (translation == val)
                {
                    translation = Translations.Power(val);
                }
                if (translation == val)
                {
                    translation = Translations.StarSystem(val);
                }
                return translation;
            }, 1);

            // Helper functions
            store["OneOf"] = new NativeFunction((values) =>
            {
                return new ScriptResolver(scripts).resolveScript(values[random.Next(values.Count)].AsString, store);
            });

            store["Occasionally"] = new NativeFunction((values) =>
            {
                if (random.Next((int)values[0].AsNumber) == 0)
                {
                    return new ScriptResolver(scripts).resolveScript(values[1].AsString, store);
                }
                else
                {
                    return "";
                }
            }, 2);

            store["Humanise"] = new NativeFunction((values) =>
            {
                return Translations.Humanize(values[0].AsNumber);
            }, 1);

            store["Pause"] = new NativeFunction((values) =>
            {
                return @"<break time =""" + values[0].AsNumber + @"ms"" />";
            }, 1);

            //
            // Commander-specific functions
            //
            store["ShipName"] = new NativeFunction((values) =>
            {
                int? localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                string model = (values.Count == 2 ? values[1].AsString : null);
                Ship ship = findShip(localId, model);
                string result = (ship == null ? "your ship" : ship.SpokenName());
                return result;
            }, 0, 2);

            store["ShipCallsign"] = new NativeFunction((values) =>
            {
                int? localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                Ship ship = findShip(localId, null);

                string result;
                if (ship != null)
                {
                    if (EDDI.Instance.Cmdr != null && EDDI.Instance.Cmdr.name != null)
                    {
                        // Obtain the first three characters
                        string chars = new Regex("[^a-zA-Z0-9]").Replace(EDDI.Instance.Cmdr.name, "").ToUpperInvariant().Substring(0, 3);
                        result = ship.SpokenManufacturer() + " " + Translations.CallSign(chars);
                    }
                    else
                    {
                        if (ship.SpokenManufacturer() == null)
                        {
                            result = "unidentified ship";
                        }
                        else
                        {
                            result = "unidentified " + ship.SpokenManufacturer() + " " + ship.SpokenModel();
                        }
                    }
                }
                else
                {
                    result = "unidentified ship";
                }
                return result;
            }, 0, 1);

            //
            // Obtain definition objects for various items
            //

            store["ShipDetails"] = new NativeFunction((values) =>
            {
                int? localId = (values.Count == 0 ? (int?)null : (int)values[0].AsNumber);
                Ship result = findShip(localId, null);
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 0, 1);

            store["CombatRatingDetails"] = new NativeFunction((values) =>
            {
                CombatRating result = CombatRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = CombatRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["TradeRatingDetails"] = new NativeFunction((values) =>
            {
                TradeRating result = TradeRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = TradeRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["ExplorationRatingDetails"] = new NativeFunction((values) =>
            {
                ExplorationRating result = ExplorationRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = ExplorationRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["EmpireRatingDetails"] = new NativeFunction((values) =>
            {
                EmpireRating result = EmpireRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = EmpireRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["FederationRatingDetails"] = new NativeFunction((values) =>
            {
                FederationRating result = FederationRating.FromName(values[0].AsString);
                if (result == null)
                {
                    result = FederationRating.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["SystemDetails"] = new NativeFunction((values) =>
            {
                StarSystem result = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[0].AsString, true);
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["StationDetails"] = new NativeFunction((values) =>
            {
                if (values.Count == 0)
                {
                    return null;
                }
                StarSystem system;
                if (values.Count == 1)
                {
                    // Current system
                    system = EDDI.Instance.CurrentStarSystem;
                }
                else
                {
                    system = StarSystemSqLiteRepository.Instance.GetOrCreateStarSystem(values[1].AsString, true);
                }
                Station result = system != null && system.stations != null ? system.stations.FirstOrDefault(v => v.name == values[0].AsString) : null;
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1, 2);

            store["SuperpowerDetails"] = new NativeFunction((values) =>
            {
                Superpower result = Superpower.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Superpower.From(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["StateDetails"] = new NativeFunction((values) =>
            {
                State result = State.FromName(values[0].AsString);
                if (result == null)
                {
                    result = State.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["EconomyDetails"] = new NativeFunction((values) =>
            {
                Economy result = Economy.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Economy.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["GovernmentDetails"] = new NativeFunction((values) =>
            {
                Government result = Government.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Government.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["SecurityLevelDetails"] = new NativeFunction((values) =>
            {
                SecurityLevel result = SecurityLevel.FromName(values[0].AsString);
                if (result == null)
                {
                    result = SecurityLevel.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            store["MaterialDetails"] = new NativeFunction((values) =>
            {
                Material result = Material.FromName(values[0].AsString);
                if (result == null)
                {
                    result = Material.FromEDName(values[0].AsString);
                }
                return (result == null ? new ReflectionValue(new object()) : new ReflectionValue(result));
            }, 1);

            // Variables
            foreach (KeyValuePair<string, Cottle.Value> entry in vars)
            {
                store[entry.Key] = entry.Value;
            }

            return store;
        }
Example #17
0
 internal static Delegate Create(object target, NativeFunction function)
 {
     return new Delegate(target, function);
 }