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"); }
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); ")); }
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); }
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); }
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)); }
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); }
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); }
public static MondValue JsonWatch(Watch watch) { var obj = MondValue.Object(); obj["Id"] = watch.Id; obj["Expression"] = watch.Expression; obj["Value"] = watch.Value; return(obj); }
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); }
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); }
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); }
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); }
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); }
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); }
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)); }
public Machine(MondState state) : this() { _state = state; Global = MondValue.Object(state); _debugAction = MondDebugAction.Run; _debugSkip = false; _debugAlign = false; _debugDepth = 0; Debugger = null; }
/// <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; }); }
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); }
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); }
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); }
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); }
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"); }
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); }
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); }
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); }
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); }
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); }
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; }); }
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); }
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); }