{"action":"created","comment":{"url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/comments/28471650","id":28471650,"diff_hunk":"@@ -238,7 +238,7 @@ public ProgramContext GetInterpreterContext()\n             return contexts[0];\n         }\n         \n-        public ProgramContext GetProgramContext()\n+        public ProgramContext SwitchToProgramContext()","path":"src/kOS/Execution/CPU.cs","position":14,"original_position":14,"commit_id":"a97a09b79a2b32e0daae580a17b1c810b4073bb3","original_commit_id":"a97a09b79a2b32e0daae580a17b1c810b4073bb3","user":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"body":"I renamed this because it took me a while to find the problem, and part of the reason it took so long was that the original naming convention of \"Get\" ProgramContext hid the fact that actually there's a major side effect hiding inside this \"get\" method: Not only does it GET the program context, but it also tells the cpu to switch to starting to USE that program context as its next instruction it's going to execute after the current one is done.\r\n\r\nThat is crucial to how run() works.  I kept running around in circles trying to find \"yes I see that run() loads the program, but where on earth does it tell the CPU to jump to start running it?  I can't find it anywhere!\"\r\n\r\nAnd the reason I couldn't find it is that it was hidden inside this \"Get\" method as a secret side-effect.  Thus I figured we really should rename it to make it clear it's not just doing a \"Get\".\r\n","created_at":"2015-04-15T23:03:33Z","updated_at":"2015-04-15T23:03:33Z","html_url":"https://github.com/KSP-KOS/KOS/pull/832#discussion_r28471650","pull_request_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832","_links":{"self":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/comments/28471650"},"html":{"href":"https://github.com/KSP-KOS/KOS/pull/832#discussion_r28471650"},"pull_request":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832"}}},"pull_request":{"url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832","id":33404896,"html_url":"https://github.com/KSP-KOS/KOS/pull/832","diff_url":"https://github.com/KSP-KOS/KOS/pull/832.diff","patch_url":"https://github.com/KSP-KOS/KOS/pull/832.patch","issue_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/832","number":832,"state":"closed","locked":false,"title":"Fixes #676 no more second error on syntax errors.","user":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"body":"When I examined it, I saw that this is how it's\r\nmeant to work in normal cases:\r\n\r\nThere are two instruction pointers - one for where\r\nyou are in the interpreter context and another for\r\nwhere you are in the program context.\r\n\r\nWhen you run a program, it does this:\r\n\r\n  1. Interpreter context has an OpcodeCall that calls built-in function run().\r\n  2. Function run() tells CPU it should switch to program context on the\r\n     next instruction.\r\n  3. Function run() calls compiler, and populates the CPU's program context\r\n     with the resulting program.\r\n  4. Function run() finishes.\r\n  5. CPU, like it does for all opcode executions, updates the instruction\r\n     pointer of the current context to the next opcode.\r\n  6. CPU finishes loop iteration, and begins new loop.\r\n  7. CPU now is in the new program context, and is running the program.\r\n  8. When program finishes or errors out, CPU returns to interpreter context and picks up where it left the instruction pointer for the interpreter.\r\n\r\nThe problem is when step 3 barfs and throws an exception, the CPU was\r\n*already* told in step 2 that it is no longer going to be in interpreter\r\ncontext, so when it handles the error it's handling it as if it had come\r\nfrom a program instead of from the interpreter, and thus is does NOT advance\r\nthe interpreter's instruction pointer, thinking that had already been\r\ndone before in step 5 of the previous iteration just before it had\r\njumped into the \"program\" it thought it was running.\r\n\r\nThus the interpreter tries to run the same Opcode a second time, and gets\r\nthe new error because the stack was already popped in the first attempt\r\nto call the run() function.  The second time it tries to call run(), the\r\nargs it needs aren't there on the stack anymore, thus the null ref error.\r\n\r\nThe fix was to detect when the compiler failed in step 3, and when it did,\r\nto undo the \"you are now in program context\" setup it did in step 2, so\r\nthe CPU knows it's still in interpreter context and it does therefore need\r\nto advance the interpreter's instruction pointer.","created_at":"2015-04-15T22:59:38Z","updated_at":"2015-04-15T23:03:33Z","closed_at":"2015-04-15T23:02:40Z","merged_at":"2015-04-15T23:02:40Z","merge_commit_sha":"07a574978c55112f01c6ba8d63a7fd7af7958181","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832/commits","review_comments_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832/comments","review_comment_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/comments{/number}","comments_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/832/comments","statuses_url":"https://api.github.com/repos/KSP-KOS/KOS/statuses/a97a09b79a2b32e0daae580a17b1c810b4073bb3","head":{"label":"Dunbaratu:Fixes_676","ref":"Fixes_676","sha":"a97a09b79a2b32e0daae580a17b1c810b4073bb3","user":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"repo":{"id":18195960,"name":"KOS-1","full_name":"Dunbaratu/KOS-1","owner":{"login":"Dunbaratu","id":5216848,"avatar_url":"https://avatars.githubusercontent.com/u/5216848?v=3","gravatar_id":"","url":"https://api.github.com/users/Dunbaratu","html_url":"https://github.com/Dunbaratu","followers_url":"https://api.github.com/users/Dunbaratu/followers","following_url":"https://api.github.com/users/Dunbaratu/following{/other_user}","gists_url":"https://api.github.com/users/Dunbaratu/gists{/gist_id}","starred_url":"https://api.github.com/users/Dunbaratu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dunbaratu/subscriptions","organizations_url":"https://api.github.com/users/Dunbaratu/orgs","repos_url":"https://api.github.com/users/Dunbaratu/repos","events_url":"https://api.github.com/users/Dunbaratu/events{/privacy}","received_events_url":"https://api.github.com/users/Dunbaratu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Dunbaratu/KOS-1","description":"Fully programmable autopilot mod for KSP. Originally By Nivekk","fork":true,"url":"https://api.github.com/repos/Dunbaratu/KOS-1","forks_url":"https://api.github.com/repos/Dunbaratu/KOS-1/forks","keys_url":"https://api.github.com/repos/Dunbaratu/KOS-1/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Dunbaratu/KOS-1/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Dunbaratu/KOS-1/teams","hooks_url":"https://api.github.com/repos/Dunbaratu/KOS-1/hooks","issue_events_url":"https://api.github.com/repos/Dunbaratu/KOS-1/issues/events{/number}","events_url":"https://api.github.com/repos/Dunbaratu/KOS-1/events","assignees_url":"https://api.github.com/repos/Dunbaratu/KOS-1/assignees{/user}","branches_url":"https://api.github.com/repos/Dunbaratu/KOS-1/branches{/branch}","tags_url":"https://api.github.com/repos/Dunbaratu/KOS-1/tags","blobs_url":"https://api.github.com/repos/Dunbaratu/KOS-1/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Dunbaratu/KOS-1/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Dunbaratu/KOS-1/git/refs{/sha}","trees_url":"https://api.github.com/repos/Dunbaratu/KOS-1/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Dunbaratu/KOS-1/statuses/{sha}","languages_url":"https://api.github.com/repos/Dunbaratu/KOS-1/languages","stargazers_url":"https://api.github.com/repos/Dunbaratu/KOS-1/stargazers","contributors_url":"https://api.github.com/repos/Dunbaratu/KOS-1/contributors","subscribers_url":"https://api.github.com/repos/Dunbaratu/KOS-1/subscribers","subscription_url":"https://api.github.com/repos/Dunbaratu/KOS-1/subscription","commits_url":"https://api.github.com/repos/Dunbaratu/KOS-1/commits{/sha}","git_commits_url":"https://api.github.com/repos/Dunbaratu/KOS-1/git/commits{/sha}","comments_url":"https://api.github.com/repos/Dunbaratu/KOS-1/comments{/number}","issue_comment_url":"https://api.github.com/repos/Dunbaratu/KOS-1/issues/comments{/number}","contents_url":"https://api.github.com/repos/Dunbaratu/KOS-1/contents/{+path}","compare_url":"https://api.github.com/repos/Dunbaratu/KOS-1/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Dunbaratu/KOS-1/merges","archive_url":"https://api.github.com/repos/Dunbaratu/KOS-1/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Dunbaratu/KOS-1/downloads","issues_url":"https://api.github.com/repos/Dunbaratu/KOS-1/issues{/number}","pulls_url":"https://api.github.com/repos/Dunbaratu/KOS-1/pulls{/number}","milestones_url":"https://api.github.com/repos/Dunbaratu/KOS-1/milestones{/number}","notifications_url":"https://api.github.com/repos/Dunbaratu/KOS-1/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Dunbaratu/KOS-1/labels{/name}","releases_url":"https://api.github.com/repos/Dunbaratu/KOS-1/releases{/id}","created_at":"2014-03-28T00:09:16Z","updated_at":"2015-01-04T19:29:21Z","pushed_at":"2015-04-15T22:59:07Z","git_url":"git://github.com/Dunbaratu/KOS-1.git","ssh_url":"git@github.com:Dunbaratu/KOS-1.git","clone_url":"https://github.com/Dunbaratu/KOS-1.git","svn_url":"https://github.com/Dunbaratu/KOS-1","homepage":null,"size":28121,"stargazers_count":0,"watchers_count":0,"language":"C#","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"KSP-KOS:develop","ref":"develop","sha":"3aa175984a4bc098ca2726a21a0b81fb88fbc952","user":{"login":"KSP-KOS","id":6767371,"avatar_url":"https://avatars.githubusercontent.com/u/6767371?v=3","gravatar_id":"","url":"https://api.github.com/users/KSP-KOS","html_url":"https://github.com/KSP-KOS","followers_url":"https://api.github.com/users/KSP-KOS/followers","following_url":"https://api.github.com/users/KSP-KOS/following{/other_user}","gists_url":"https://api.github.com/users/KSP-KOS/gists{/gist_id}","starred_url":"https://api.github.com/users/KSP-KOS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KSP-KOS/subscriptions","organizations_url":"https://api.github.com/users/KSP-KOS/orgs","repos_url":"https://api.github.com/users/KSP-KOS/repos","events_url":"https://api.github.com/users/KSP-KOS/events{/privacy}","received_events_url":"https://api.github.com/users/KSP-KOS/received_events","type":"Organization","site_admin":false},"repo":{"id":17217530,"name":"KOS","full_name":"KSP-KOS/KOS","owner":{"login":"KSP-KOS","id":6767371,"avatar_url":"https://avatars.githubusercontent.com/u/6767371?v=3","gravatar_id":"","url":"https://api.github.com/users/KSP-KOS","html_url":"https://github.com/KSP-KOS","followers_url":"https://api.github.com/users/KSP-KOS/followers","following_url":"https://api.github.com/users/KSP-KOS/following{/other_user}","gists_url":"https://api.github.com/users/KSP-KOS/gists{/gist_id}","starred_url":"https://api.github.com/users/KSP-KOS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KSP-KOS/subscriptions","organizations_url":"https://api.github.com/users/KSP-KOS/orgs","repos_url":"https://api.github.com/users/KSP-KOS/repos","events_url":"https://api.github.com/users/KSP-KOS/events{/privacy}","received_events_url":"https://api.github.com/users/KSP-KOS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/KSP-KOS/KOS","description":"Fully programmable autopilot mod for KSP. Originally By Nivekk","fork":false,"url":"https://api.github.com/repos/KSP-KOS/KOS","forks_url":"https://api.github.com/repos/KSP-KOS/KOS/forks","keys_url":"https://api.github.com/repos/KSP-KOS/KOS/keys{/key_id}","collaborators_url":"https://api.github.com/repos/KSP-KOS/KOS/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/KSP-KOS/KOS/teams","hooks_url":"https://api.github.com/repos/KSP-KOS/KOS/hooks","issue_events_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/events{/number}","events_url":"https://api.github.com/repos/KSP-KOS/KOS/events","assignees_url":"https://api.github.com/repos/KSP-KOS/KOS/assignees{/user}","branches_url":"https://api.github.com/repos/KSP-KOS/KOS/branches{/branch}","tags_url":"https://api.github.com/repos/KSP-KOS/KOS/tags","blobs_url":"https://api.github.com/repos/KSP-KOS/KOS/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/KSP-KOS/KOS/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/KSP-KOS/KOS/git/refs{/sha}","trees_url":"https://api.github.com/repos/KSP-KOS/KOS/git/trees{/sha}","statuses_url":"https://api.github.com/repos/KSP-KOS/KOS/statuses/{sha}","languages_url":"https://api.github.com/repos/KSP-KOS/KOS/languages","stargazers_url":"https://api.github.com/repos/KSP-KOS/KOS/stargazers","contributors_url":"https://api.github.com/repos/KSP-KOS/KOS/contributors","subscribers_url":"https://api.github.com/repos/KSP-KOS/KOS/subscribers","subscription_url":"https://api.github.com/repos/KSP-KOS/KOS/subscription","commits_url":"https://api.github.com/repos/KSP-KOS/KOS/commits{/sha}","git_commits_url":"https://api.github.com/repos/KSP-KOS/KOS/git/commits{/sha}","comments_url":"https://api.github.com/repos/KSP-KOS/KOS/comments{/number}","issue_comment_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/comments{/number}","contents_url":"https://api.github.com/repos/KSP-KOS/KOS/contents/{+path}","compare_url":"https://api.github.com/repos/KSP-KOS/KOS/compare/{base}...{head}","merges_url":"https://api.github.com/repos/KSP-KOS/KOS/merges","archive_url":"https://api.github.com/repos/KSP-KOS/KOS/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/KSP-KOS/KOS/downloads","issues_url":"https://api.github.com/repos/KSP-KOS/KOS/issues{/number}","pulls_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls{/number}","milestones_url":"https://api.github.com/repos/KSP-KOS/KOS/milestones{/number}","notifications_url":"https://api.github.com/repos/KSP-KOS/KOS/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/KSP-KOS/KOS/labels{/name}","releases_url":"https://api.github.com/repos/KSP-KOS/KOS/releases{/id}","created_at":"2014-02-26T16:43:31Z","updated_at":"2015-04-15T23:02:57Z","pushed_at":"2015-04-15T23:02:57Z","git_url":"git://github.com/KSP-KOS/KOS.git","ssh_url":"git@github.com:KSP-KOS/KOS.git","clone_url":"https://github.com/KSP-KOS/KOS.git","svn_url":"https://github.com/KSP-KOS/KOS","homepage":null,"size":29612,"stargazers_count":99,"watchers_count":99,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":56,"mirror_url":null,"open_issues_count":157,"forks":56,"open_issues":157,"watchers":99,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832"},"html":{"href":"https://github.com/KSP-KOS/KOS/pull/832"},"issue":{"href":"https://api.github.com/repos/KSP-KOS/KOS/issues/832"},"comments":{"href":"https://api.github.com/repos/KSP-KOS/KOS/issues/832/comments"},"review_comments":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832/comments"},"review_comment":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/832/commits"},"statuses":{"href":"https://api.github.com/repos/KSP-KOS/KOS/statuses/a97a09b79a2b32e0daae580a17b1c810b4073bb3"}}}}