public static void LoadLibrary(WrenVM vm) { vm.Interpret("scheduler", "scheduler", _schedulerSource); ObjClass scheduler = (ObjClass)vm.FindVariable("scheduler", "Scheduler"); vm.Primitive(scheduler.ClassObj, "captureMethods_()", CaptureMethods); vm.Interpret("scheduler", "scheduler", "Scheduler.captureMethods_()"); }
static void RunRepl() { WrenVM vm = new WrenVM(); LibraryLoader.LoadLibraries(vm); Console.WriteLine("-- wren v0.0.0"); string line = ""; for (; ;) { Console.Write("> "); line += Console.ReadLine() + "\n"; if (OpenBrackets(line) > 0) { continue; } // TODO: Handle failure. vm.Interpret("Prompt", "Prompt", line); line = ""; } }
public static void LoadLibrary(WrenVM vm) { vm.Interpret("timer", "timer", _timerSource); ObjClass timer = (ObjClass)vm.FindVariable("timer", "Timer"); vm.Primitive(timer.ClassObj, "startTimer_(_,_)", StartTimer); }
public static void LoadLibrary(WrenVM vm) { vm.Interpret("", "", MetaLibSource); ObjClass meta = (ObjClass)vm.FindVariable("Meta"); vm.Primitive(meta.ClassObj, "eval(_)", Eval); }
public void WithArguments() { Assert.AreEqual(WrenInterpretResult.Success, vm.Interpret("var test = Fn.new { |text|\n" + "System.print(text)\n" + "}")); var fnHandle = vm.MakeCallHandle("call(_)"); vm.EnsureSlots(2); vm.GetVariable(WrenVM.InterpetModule, "test", 0); vm.SetSlotString(1, "testing!"); Assert.AreEqual(WrenInterpretResult.Success, vm.Call(fnHandle)); Assert.AreEqual("testing!", output[0]); fnHandle.Release(); }
public static void LoadLibrary(WrenVM vm) { vm.Interpret("io", "io", IoSource); ObjClass file = (ObjClass)vm.FindVariable("io", "File"); vm.Primitive(file.ClassObj, "open_(_,_)", Open); vm.Primitive(file.ClassObj, "sizePath_(_,_)", SizePath); vm.Primitive(file, "close_(_)", Close); vm.Primitive(file, "descriptor", Descriptor); vm.Primitive(file, "readBytes_(_,_)", ReadBytes); vm.Primitive(file, "size_(_)", Size); }
static void Main(string[] args) { Console.Title = "Wren.NET Test"; WrenConfig Cfg = new WrenConfig(BindForeignMethod, LoadModule); WrenVM WVM = Cfg.NewVM(); string Test = File.ReadAllText("test.wren"); Console.WriteLine("{0}\n\n----------", Test); WVM.Interpret("Test", Test); Console.ReadLine(); }
static int RunFile(string path) { if (!File.Exists(path)) { return(66); // File Not Found } _loadedFile = path; string source = File.ReadAllText(path); WrenVM vm = new WrenVM { LoadModuleFn = LoadModule }; LibraryLoader.LoadLibraries(vm); return((int)vm.Interpret("main", path, source)); }
/// <summary> /// Automatically maps the given types to make their marked interfaces accessible from Wren. /// If the module name is the <see cref="WrenVM.InterpetModule"/>, the code will be interpreted immediately. /// </summary> /// <param name="vm">The <see cref="WrenVM"/> to make the types available to.</param> /// <param name="moduleName">The name of the module to place the types into.</param> /// <param name="targets">The types to map.</param> public static void AutoMap(this WrenVM vm, string moduleName, params Type[] targets) { checkInitialization(vm); if (moduleName == WrenVM.InterpetModule) { var classes = mainModuleClasses.GetValue(vm, _ => null); foreach (var target in targets) { var foreignClass = new ForeignClass(target); classes.Add(foreignClass.Name, foreignClass); vm.Interpret(foreignClass.Source); } return; } var modules = generatedModules.GetValue(vm, _ => null); ForeignModule module; if (modules.ContainsKey(moduleName)) { module = modules[moduleName]; if (module.Used && TreatModificationAfterLoadAsError) { throw new LoadedModuleModifiedException(moduleName); } } else { module = new ForeignModule(); modules.Add(moduleName, module); } foreach (var target in targets) { module.Add(target); } }
private static void Main(string[] args) { var config = new WrenConfig(); config.Write += (vm, text) => Console.Write(text); config.Error += (type, module, line, message) => Console.WriteLine($"Error [{type}] in module [{module}] at line {line}:{Environment.NewLine}{message}"); config.LoadModule += (vm, module) => $"System.print(\"Module [{module}] loaded!\")"; config.BindForeignMethod += (vm, module, className, isStatic, signature) => { Console.WriteLine($"BindForeignMethod called: It's called {signature}, is part of {className} and is {(isStatic ? "static" : "not static")}."); return(signature == "sayHi(_)" ? sayHi : (WrenForeignMethod)null); }; config.BindForeignClass += (vm, module, className) => className == "Test" ? new WrenForeignClassMethods { Allocate = alloc } : null; using (var vm = new WrenVM(config)) { var result = vm.Interpret("System.print(\"Hi from Wren!\")"); result = vm.Interpret("var helloTo = Fn.new { |name|\n" + "System.print(\"Hello, %(name)!\")\n" + "}"); result = vm.Interpret("helloTo.call(\"IronWren\")"); var someFnHandle = vm.MakeCallHandle("call(_)"); vm.EnsureSlots(2); vm.GetVariable(WrenVM.InterpetModule, "helloTo", 0); vm.SetSlotString(1, "foreign method"); result = vm.Call(someFnHandle); result = vm.Interpret("foreign class Test {\n" + "construct new() { }\n" + "isForeign { true }\n" + "foreign sayHi(to)\n" + "}\n" + "var test = Test.new()\n" + "test.sayHi(\"wren\")\n" + "\n" + "import \"TestModule\"\n"); vm.EnsureSlots(1); vm.GetVariable(WrenVM.InterpetModule, "test", 0); result = vm.Call(vm.MakeCallHandle("isForeign")); var isTestClassForeign = vm.GetSlotBool(0); Console.WriteLine("Test class is foreign: " + isTestClassForeign); vm.AutoMap(typeof(WrenMath)); vm.Interpret("System.print(\"The sine of pi is: %(Math.sin(Math.pi))!\")"); Console.WriteLine($"And C# says it's: {Math.Sin(Math.PI)}"); Console.WriteLine(); var sw = new Stopwatch(); for (var i = 0; i < 3; ++i) { sw.Restart(); vm.Interpret("for (i in 1..1000000) Math.sin(Math.pi)"); sw.Stop(); Console.WriteLine("1000000 iterations took " + sw.ElapsedMilliseconds + "ms."); } var results = new double[1000000]; sw.Restart(); for (var i = 0; i < 1000000; ++i) { results[i] = Math.Sin(Math.PI); } sw.Stop(); Console.WriteLine("1000000 iterations in C# took " + sw.ElapsedMilliseconds + "ms."); vm.AutoMap <WrenVector>(); vm.Interpret("var vec = Vector.new(1, 2)"); vm.Interpret("vec.print()"); vm.Interpret("System.print(\"Vector's X is: %(vec.x)\")"); vm.Interpret("System.print(\"Vector's Y is: %(vec.y)\")"); Console.ReadLine(); Console.Clear(); Console.WriteLine("You may now write Wren code that will be interpreted!"); Console.WriteLine("Use file:[path] to interpret a file!"); Console.WriteLine(); while (true) { var input = Console.ReadLine(); if (input.StartsWith("file:")) { vm.Interpret(File.ReadAllText(input.Substring(5))); } else { vm.Interpret(input); } } } }
public static void LoadLibrary <T>(WrenVM vm) where T : IEnumerator { vm.Interpret("", "", WrenScriptLibSource); ObjClass scriptClass = (ObjClass)vm.FindVariable("Script"); ObjClass scriptRefClass = (ObjClass)vm.FindVariable("ScriptRef"); List <string> underscores = new List <string>(); List <int> registeredArities = new List <int>(); foreach (var type in Assembly.GetCallingAssembly().GetTypes()) { if (typeof(T).IsAssignableFrom(type)) { var attrs = type.GetCustomAttributes(true); foreach (var attr in attrs) { var scriptName = attr as WrenNameAttribute; if (scriptName != null) { registeredArities.Clear(); foreach (var ctor in type.GetConstructors()) { bool ignore = false; foreach (var ctorAttr in ctor.GetCustomAttributes(true)) { if (ctorAttr is WrenIgnoreAttribute) { ignore = true; } } if (!ignore) { var constructor = ctor; // capture a local cuz we're in a closure var parameters = constructor.GetParameters(); if (registeredArities.Contains(parameters.Length)) { throw new Exception("Wren doesn't understand typed signatures so wren constructable classes shouldn't contain constructors with the same number of signatures"); } registeredArities.Add(parameters.Length); underscores.Clear(); foreach (var parameter in parameters) { underscores.Add("_"); } var signature = scriptName.name + "(" + string.Join(",", underscores) + ")"; vm.Primitive(scriptRefClass.ClassObj, signature, (primVM, args, stackStart) => { try { object[] constructorArgs = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { // Convert.ChangeType is mostly because numbers in wren are always doubles, but it might catch other cool stuff var arg = args[stackStart + i + 1].Unbox(); constructorArgs[i] = arg is IConvertible ? Convert.ChangeType(arg, parameters[i].ParameterType) : arg; } args[stackStart] = new ObjForeign() { foreign = constructor.Invoke(constructorArgs) }; return(true); } catch (Exception e) { primVM.Fiber.Error = Obj.MakeString("Couldn't construct " + constructor.Name + ": " + e.Message); return(false); } }); vm.Primitive(scriptClass.ClassObj, signature, (primVM, args, stackStart, success) => { try { object[] constructorArgs = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { var arg = args[stackStart + i + 1].Unbox(); constructorArgs[i] = arg is IConvertible ? Convert.ChangeType(arg, parameters[i].ParameterType) : arg; } var script = constructor.Invoke(constructorArgs); success.value = true; return(script as IEnumerator); } catch (Exception e) { primVM.Fiber.Error = Obj.MakeString("Couldn't construct " + constructor.Name + ": " + e.Message); success.value = false; return(Noop()); } }); } } } } } } }