示例#1
0
        public void NativeFunction()
        {
            var state = new MondState();

            state["function"] = new MondFunction((_, args) => args[0]);

            var result = state.Run(@"
                return global.function('arg');
            ");

            Assert.True(result == "arg");
        }
示例#2
0
文件: Program.cs 项目: krixalis/Mond
        static void Main()
        {
            /*const string source1 = @"
                var a = [1, 2, 3];
                return a.length();
            ";*/

            const string source1 = @"
                fun fibonacci(n) {
                    fun inner(m, a, b) {
                        if (m == 0)
                            return a;

                        return inner(m - 1, b, a + b);
                    }

                    return inner(n, 0, 1);
                }

                return fibonacci(50);
            ";

            try
            {
                var state = new MondState();
                state["call"] = new MondFunction((_, args) => state.Call(args[0], args[1]));

                var program1 = MondProgram.Compile(source1, "test1.mnd");
                //var program2 = MondProgram.Compile(source2, "test2.mnd");

                var result1 = state.Load(program1);
                //var result2 = state.Load(program2);

                Console.WriteLine(result1.ToString());
                //Console.WriteLine(result2.ToString());
            }
            catch (MondException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }
示例#3
0
        private MondValue CreateLocalObject()
        {
            var obj = new MondValue(_state);
            obj.Prototype = MondValue.Null;

            obj["__get"] = new MondFunction((_, args) =>
            {
                if (args.Length != 2)
                    throw new MondRuntimeException("LocalObject.__get: requires 2 parameters");

                var name = (string)args[1];

                Func<MondValue> getter;
                Action<MondValue> setter;

                if (!TryGetLocalAccessor(name, out getter, out setter))
                    throw new MondRuntimeException("`{0}` is not defined", name);

                return getter();
            });

            obj["__set"] = new MondFunction((_, args) =>
            {
                if (args.Length != 3)
                    throw new MondRuntimeException("LocalObject.__set: requires 3 parameters");

                var name = (string)args[1];
                var value = args[2];

                Func<MondValue> getter;
                Action<MondValue> setter;

                if (!TryGetLocalAccessor(name, out getter, out setter))
                    throw new MondRuntimeException("`{0}` is not defined", name);

                if (setter == null)
                    throw new MondRuntimeException("`{0}` is read-only", name);

                setter(value);
                return value;
            });

            return obj;
        }
示例#4
0
文件: Closure.cs 项目: krixalis/Mond
        public Closure(MondFunction function)
        {
            Type = ClosureType.Native;

            NativeFunction = function;
        }
示例#5
0
        public static MondValue GetEnumerator([MondInstance] MondValue instance)
        {
            var enumerator = new MondValue(MondValueType.Object);
            var str = (string)instance;
            var i = 0;

            enumerator["current"] = MondValue.Undefined;
            enumerator["moveNext"] = new MondValue((_, args) =>
            {
                if (i >= str.Length)
                    return false;

                enumerator["current"] = new string(str[i++], 1);
                return true;
            });

            enumerator["dispose"] = new MondFunction((_, args) => MondValue.Undefined);

            return enumerator;
        }
示例#6
0
        public static MondValue GetEnumerator([MondInstance] MondValue instance)
        {
            EnsureObject("getEnumerator", instance);

            var enumerator = new MondValue(MondValueType.Object);
            var keys = instance.ObjectValue.Values.Keys.ToList();
            var i = 0;

            enumerator["current"] = MondValue.Undefined;
            enumerator["moveNext"] = new MondValue((_, args) =>
            {
                if (i >= keys.Count)
                    return false;

                var pair = new MondValue(MondValueType.Object);
                pair["key"] = keys[i];
                pair["value"] = instance.ObjectValue.Values[keys[i]];

                enumerator["current"] = pair;
                i++;
                return true;
            });

            enumerator["dispose"] = new MondFunction((_, args) => MondValue.Undefined);

            return enumerator;
        }