Example #1
0
        public void OperatorEqualTo()
        {
            _left  = 10;
            _right = 10;
            Assert.True(_left == _right);

            _left  = 10;
            _right = 11;
            Assert.False(_left == _right);

            _left  = "10";
            _right = 10;
            Assert.False(_left == _right); // no ty

            _left  = MondValue.Null;
            _right = MondValue.Undefined;
            Assert.False(_left == _right);

            _left  = MondValue.Object();
            _right = _left;
            Assert.True(_left == _right);

            _left  = 0;
            _right = MondValue.Null;
            Assert.False(_left == _right, "type check");
        }
Example #2
0
        public void ClassArgument()
        {
            var person = new ClassTests.Person(MondValue.Object(), "Rohan");

            var personValue = MondValue.Object(_state);

            personValue.UserData = person;

            _state["rohan"] = personValue;

            Assert.True(_state.Run(@"
                return global.Greet(global.rohan);
            ") == "hello Rohan!");

            personValue.UserData = "something";

            Assert.Throws <MondRuntimeException>(() => _state.Run(@"
                global.Greet(global.rohan);
            "));

            personValue.UserData = null;

            Assert.Throws <MondRuntimeException>(() => _state.Run(@"
                global.Greet(global.rohan);
            "));
        }
Example #3
0
            private MondValue ParseObject()
            {
                var obj   = MondValue.Object();
                var first = true;

                while (!Match(TokenType.ObjectEnd))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        Require(TokenType.Comma);
                    }

                    var key = Require(TokenType.String);

                    Require(TokenType.Colon);

                    var value = ParseValue();

                    obj[key.Value] = value;
                }

                Require(TokenType.ObjectEnd);
                return(obj);
            }
Example #4
0
        public static MondValue GetEnumerator([MondInstance] MondValue instance)
        {
            EnsureObject("getEnumerator", instance);

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

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

                var pair      = MondValue.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);
        }
Example #5
0
        public void OnOpen(WebSocket socket)
        {
            _socket = socket;
            _socket.DataReceived += OnMessage;

            _debugger.GetState(
                out var isRunning, out var programs, out var position, out var watches, out var callStack);

            var message = MondValue.Object();

            message["Type"]        = "InitialState";
            message["Programs"]    = MondValue.Array(programs.Select(Utility.JsonProgram));
            message["Running"]     = isRunning;
            message["Id"]          = position.Id;
            message["StartLine"]   = position.StartLine;
            message["StartColumn"] = position.StartColumn;
            message["EndLine"]     = position.EndLine;
            message["EndColumn"]   = position.EndColumn;
            message["Watches"]     = MondValue.Array(watches.Select(Utility.JsonWatch));

            if (callStack != null)
            {
                message["CallStack"] = _debugger.BuildCallStackArray(callStack);
            }

            Send(Json.Serialize(message));
        }
Example #6
0
        public MondValue Serialize(MondState state, params MondValue[] args)
        {
            var result = MondValue.Object(state);

            result["$ctor"] = "Color";
            result["$args"] = MondValue.Array(new MondValue[] { Red, Green, Blue, Alpha });
            return(result);
        }
Example #7
0
        public MondValue Serialize(MondState state, params MondValue[] args)
        {
            var result = MondValue.Object(state);

            result["$ctor"] = "Regex";
            result["$args"] = MondValue.Array(new MondValue[] { _pattern, _ignoreCase, _multiline });
            return(result);
        }
Example #8
0
        public static MondValue JsonWatch(Watch watch)
        {
            var obj = MondValue.Object();

            obj["Id"]         = watch.Id;
            obj["Expression"] = watch.Expression;
            obj["Value"]      = watch.Value;
            return(obj);
        }
Example #9
0
        public static MondValue JsonProgram(ProgramInfo program)
        {
            var obj = MondValue.Object();

            obj["FileName"]    = program.FileName;
            obj["SourceCode"]  = program.DebugInfo.SourceCode;
            obj["FirstLine"]   = FirstLineNumber(program.DebugInfo);
            obj["Breakpoints"] = MondValue.Array(program.Breakpoints.Select(e => MondValue.Number(e)));
            return(obj);
        }
Example #10
0
        private static MondValue ToMond(Match match)
        {
            var result = MondValue.Object();

            result["index"]   = match.Index;
            result["length"]  = match.Length;
            result["success"] = match.Success;
            result["value"]   = match.Value;
            return(result);
        }
Example #11
0
        private static MondValue JsonValueProperty(MondValue key, MondValue value)
        {
            var property = MondValue.Object();

            property["name"]      = key.ToString();
            property["nameType"]  = key.Type.GetName();
            property["value"]     = value.ToString();
            property["valueType"] = value.Type.GetName();
            return(property);
        }
Example #12
0
        public static MondValue JsonCallStackEntry(int programId, MondDebugContext.CallStackEntry callStackEntry)
        {
            var obj = MondValue.Object();

            obj["ProgramId"]    = programId;
            obj["FileName"]     = callStackEntry.FileName;
            obj["Function"]     = callStackEntry.Function;
            obj["LineNumber"]   = callStackEntry.LineNumber;
            obj["ColumnNumber"] = callStackEntry.ColumnNumber;
            return(obj);
        }
Example #13
0
        public static MondValue JsonBreakpoint(MondDebugInfo.Statement statement)
        {
            var obj = MondValue.Object();

            obj["address"]   = statement.Address;
            obj["line"]      = statement.StartLineNumber;
            obj["column"]    = statement.StartColumnNumber;
            obj["endLine"]   = statement.EndLineNumber;
            obj["endColumn"] = statement.EndColumnNumber;
            return(obj);
        }
Example #14
0
        public static MondValue Bind(Type type)
        {
            var obj = MondValue.Object();

            foreach (var m in BindImpl(type))
            {
                obj.AsDictionary.Add(m.Item1, m.Item2);
            }

            return(obj);
        }
Example #15
0
        public void OnOpen()
        {
            _debugger.GetState(out var isRunning, out _);

            var message = MondValue.Object();

            message["type"]      = "initialState";
            message["version"]   = MondRemoteDebugger.ProtocolVersion;
            message["isRunning"] = isRunning;

            Send(Json.Serialize(message));
        }
Example #16
0
        public Machine(MondState state)
            : this()
        {
            _state = state;
            Global = MondValue.Object(state);

            _debugAction = MondDebugAction.Run;
            _debugSkip   = false;
            _debugAlign  = false;
            _debugDepth  = 0;
            Debugger     = null;
        }
Example #17
0
        /// <summary>
        /// Generates a class binding for the given type. Returns the constructor
        /// function and sets prototype to the generated prototype.
        /// </summary>
        public static MondFunction Bind(Type type, MondState state, out MondValue prototype)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            ClassBinding binding;

            lock (Cache)
            {
                if (!Cache.TryGetValue(type, out binding))
                {
                    binding     = BindImpl(type);
                    Cache[type] = binding;
                }
            }

            var prototypeObj = state.FindPrototype(binding.Name);

            if (prototypeObj == null)
            {
                prototypeObj = CopyToObject(binding.PrototypeFunctions, state);

                if (!state.TryAddPrototype(binding.Name, prototypeObj))
                {
                    throw new MondBindingException(BindingError.DuplicatePrototype, binding.Name);
                }
            }

            prototype = prototypeObj;

            var constructor = binding.Constructor;

            if (constructor == null)
            {
                return(null);
            }

            return((_, arguments) =>
            {
                var instance = MondValue.Object(_);
                instance.Prototype = prototypeObj;
                instance.UserData = constructor(_, instance, arguments);
                return instance;
            });
        }
Example #18
0
        public static MondValue Create(MondState state)
        {
            MondClassBinder.Bind <AsyncClass>(state, out var prototype);

            var instance = new AsyncClass();

            var obj = MondValue.Object();

            obj.UserData  = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return(obj);
        }
Example #19
0
        internal static ReturnConverter MakeReturnConversion(Type returnType)
        {
            if (returnType == typeof(void))
            {
                return((e, s, o) => MondValue.Undefined);
            }

            if (returnType == typeof(MondValue))
            {
                return((e, s, o) => ReferenceEquals(o, null) ? MondValue.Null : (MondValue)o);
            }

            if (returnType == typeof(string))
            {
                return((e, s, o) => ReferenceEquals(o, null) ? MondValue.Null : (MondValue)(string)o);
            }

            if (returnType == typeof(bool))
            {
                return((e, s, o) => (bool)o);
            }

            if (NumberTypes.Contains(returnType))
            {
                return((e, s, o) => Convert.ToDouble(o));
            }

            var classAttrib = returnType.Attribute <MondClassAttribute>();

            if (classAttrib != null && classAttrib.AllowReturn)
            {
                var className = classAttrib.Name ?? returnType.Name;

                return((e, s, o) =>
                {
                    var prototype = s.FindPrototype(className);
                    if (prototype == null)
                    {
                        throw new MondRuntimeException(e + string.Format(BindingError.PrototypeNotFound, className));
                    }

                    var obj = MondValue.Object(s);
                    obj.Prototype = prototype;
                    obj.UserData = o;
                    return obj;
                });
            }

            throw new MondBindingException(BindingError.UnsupportedReturnType, returnType);
        }
Example #20
0
        public static MondValue JsonCallStackEntry(int programId, MondDebugContext.CallStackEntry callStackEntry)
        {
            var obj = MondValue.Object();

            obj["programId"] = programId;
            obj["address"]   = callStackEntry.Address;
            obj["fileName"]  = callStackEntry.FileName;
            obj["function"]  = callStackEntry.Function;
            obj["line"]      = callStackEntry.StartLineNumber;
            obj["column"]    = callStackEntry.StartColumnNumber;
            obj["endLine"]   = callStackEntry.EndLineNumber ?? MondValue.Undefined;
            obj["endColumn"] = callStackEntry.EndColumnNumber ?? MondValue.Undefined;
            return(obj);
        }
Example #21
0
        private static MondValue CopyToObject(Dictionary <string, MondFunction> functions, MondState state)
        {
            var obj = MondValue.Object(state);

            obj.Prototype = MondValue.Null;

            foreach (var func in functions)
            {
                obj[func.Key] = func.Value;
            }

            obj.Prototype = ValuePrototype.Value;
            return(obj);
        }
Example #22
0
        public void ObjectFieldIndexer()
        {
            var obj = MondValue.Object();

            Assert.True(obj["undef"] == MondValue.Undefined);

            Assert.True(obj["setPrototype"] != MondValue.Undefined);

            obj["test"] = 123;
            Assert.True(obj["test"] == 123);

            obj[123] = "test";
            Assert.True(obj[123] == "test");
        }
Example #23
0
        private MondValue CreateLocalObject()
        {
            var obj = MondValue.Object(_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];

                if (!TryGetLocalAccessor(name, out var getter, out var 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];

                if (!TryGetLocalAccessor(name, out var getter, out var 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);
        }
Example #24
0
        public void WrappedInstanceFunction()
        {
            var obj = MondValue.Object();

            var func = MondValue.Function((state, instance, args) => MondValue.Undefined);

            Assert.True(func.FunctionValue.Type == ClosureType.InstanceNative);

            obj["test"] = func;
            var closure = obj["test"];

            Assert.True(closure.FunctionValue.Type == ClosureType.Native);

            Assert.True(new MondState().Call(obj["test"]) == MondValue.Undefined);
        }
Example #25
0
        public static MondValue Create(MondState state, RequireLibrary require)
        {
            MondClassBinder.Bind <RequireClass>(state, out var prototype);

            var instance = new RequireClass();

            instance._require = require;

            var obj = MondValue.Object();

            obj.UserData  = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return(obj);
        }
Example #26
0
        public static MondValue Create(MondState state, ConsoleOutputLibrary consoleOutput)
        {
            MondValue prototype;

            MondClassBinder.Bind <ConsoleOutputClass>(state, out prototype);

            var instance = new ConsoleOutputClass();

            instance._consoleOutput = consoleOutput;

            var obj = MondValue.Object();

            obj.UserData  = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return(obj);
        }
Example #27
0
        private MondDebugAction WaitForAction()
        {
            var result = _breaker.Task.Result;

            lock (_sync)
            {
                _context = null;
                _breaker = null;
            }

            var message = MondValue.Object();

            message["Type"]    = "State";
            message["Running"] = true;

            Broadcast(message);

            return(result);
        }
Example #28
0
        public void UserData()
        {
            const string data = "test";

            var value = MondValue.Object();

            value.UserData = data;

            Assert.True(ReferenceEquals(data, value.UserData));

            value.UserData = null;

            Assert.True(ReferenceEquals(null, value.UserData));

            Assert.Throws <MondRuntimeException>(() =>
            {
                var a = MondValue.Null.UserData;
            });
        }
Example #29
0
        internal void RemoveWatch(int id)
        {
            int removed;

            lock (_sync)
                removed = _watches.RemoveAll(w => w.Id == id);

            if (removed == 0)
            {
                return;
            }

            var message = MondValue.Object();

            message["Type"] = "RemovedWatch";
            message["Id"]   = id;

            Broadcast(message);
        }
Example #30
0
            private MondValue ParseObject()
            {
                var obj   = MondValue.Object();
                var first = true;

                while (!Match(TokenType.ObjectEnd))
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        Require(TokenType.Comma);
                    }

                    var key = ParseValue();

                    Require(TokenType.Colon);

                    var value = ParseValue();

                    obj[key] = value;
                }

                Require(TokenType.ObjectEnd);

                var ctorName = obj["$ctor"];

                if (ctorName.Type == MondValueType.String)
                {
                    var ctor = _state[ctorName];
                    if (ctor.Type == MondValueType.Function || (ctor.Type == MondValueType.Object && (bool)ctor["__call"]))
                    {
                        var ctorArgs = obj["$args"];
                        var args     = ctorArgs.Type == MondValueType.Array ? ctorArgs.AsList : new MondValue[0];
                        obj = _state.Call(ctor, args.ToArray());
                    }
                }

                return(obj);
            }