public void StartFunction(string functionName, LuaArgs args) { try { var function = m_machine.GetGlobal(functionName).GetFunction(); var coroutine = m_machine.CreateCoroutine(function); coroutine.Resume(args); if (!coroutine.IsFinished) { m_activeCoroutines.Add(coroutine); } } catch (LuaError e) { App.Log("Lua Error: {0}", e.Message); } }
private CPUUpdateResult ReallyBoot(LuaMount bootMount) { if (m_machine != null) { throw new InvalidOperationException(); } // Check if the system has any RAM if (m_computer.Memory.TotalMemory == 0) { m_computer.ErrorOutput.WriteLine("Error starting CPU: No RAM"); return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue); } // Mount the ROM m_fileSystem.Mount(bootMount, FilePath.Empty, FilePath.Empty, true); // Check if the boot path exists var bootPath = new FilePath("boot.lua"); if (!m_fileSystem.Exists(bootPath) || m_fileSystem.IsDir(bootPath)) { m_computer.ErrorOutput.WriteLine("Error starting CPU: ROM does not countain {0}", bootPath); m_fileSystem.UnmountAll(); return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue); } // Init lua machine m_machine = new LuaMachine(m_computer.Memory); m_machine.AllowByteCodeLoading = false; m_machine.EnforceTimeLimits = true; try { // Remove default APIs we don't want m_machine.RemoveUnsafeGlobals(); // Install basic APIs if (m_computer.Host != null) { m_machine.SetGlobal("_HOST", m_computer.Host); } InstallAPI(new IOAPI(m_computer, m_fileSystem)); InstallAPI(new OSAPI(m_computer, this, m_fileSystem)); InstallAPI(new PackageAPI(m_computer, m_fileSystem)); // Install custom APIs PreloadAPI(new SystemAPI(m_computer, this)); PreloadAPI(new FSAPI(m_computer, m_fileSystem)); } catch (LuaError e) { m_computer.ErrorOutput.WriteLine("Error starting CPU: {0}", e.Message); Halt(); return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue); } // Load the boot script try { string bootScript; using (var reader = m_fileSystem.OpenForRead(bootPath)) { bootScript = reader.ReadToEnd(); } var bootFunction = m_machine.LoadString(bootScript, "@" + bootPath); m_mainRoutine = m_machine.CreateCoroutine(bootFunction); m_eventFilter.Clear(); } catch (IOException e) { m_computer.ErrorOutput.WriteLine("Error loading {0}: {1}", bootPath, e.Message); Halt(); return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue); } catch (LuaError e) { m_computer.ErrorOutput.WriteLine("Error parsing {0}: {1}", bootPath, e.Message); Halt(); return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue); } // Start the boot script m_eventFilter.Clear(); return(Resume(LuaArgs.Empty)); }