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"); }
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(); }
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; }
public Closure(MondFunction function) { Type = ClosureType.Native; NativeFunction = function; }
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; }
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; }