{"action":"created","comment":{"url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/comments/47873511","id":47873511,"diff_hunk":"@@ -0,0 +1,154 @@\n+﻿using System.Collections.Generic;\n+using kOS.Safe.Encapsulation;\n+using kOS.Safe.Encapsulation.Suffixes;\n+using kOS.Safe.Exceptions;\n+using System;\n+using System.Text;\n+\n+namespace kOS.Safe.Execution\n+{\n+    /// <summary>\n+    /// Any situation where a function of any type is referenced as the\n+    /// value of a variable or suffix, it will be a variant of this class,\n+    /// which is the base class underneath references to user functions,\n+    /// built-in functions, or suffix methods.\n+    /// </summary>\n+    public abstract class KOSDelegate : Structure\n+    {\n+        public List<object> preBoundArgs = new List<object>();\n+        protected readonly ICpu cpu;\n+        \n+        public KOSDelegate(ICpu cpu)\n+        {\n+            this.cpu = cpu;\n+            InitializeSuffixes();\n+        }\n+        \n+        public KOSDelegate(KOSDelegate oldCopy)\n+        {\n+            cpu = oldCopy.cpu;\n+            InitializeSuffixes();\n+            foreach (object ca in oldCopy.preBoundArgs)\n+                preBoundArgs.Add(ca);\n+        }\n+\n+        private void InitializeSuffixes()\n+        {\n+            AddSuffix(\"CALL\", new VarArgsSuffix<object, object>(Call));\n+            AddSuffix(\"BIND\", new VarArgsSuffix<KOSDelegate, object>(Bind));\n+        }\n+\n+        public void AddPreBoundArg(object arg)\n+        {\n+            preBoundArgs.Add(arg);\n+        }\n+\n+        public object Call(params object[] args)\n+        {\n+            PushUnderArgs();\n+            cpu.PushStack(new KOSArgMarkerType());\n+            foreach (object arg in preBoundArgs)\n+            {\n+                cpu.PushStack(arg);\n+            }\n+            foreach (object arg in args)\n+            {\n+                cpu.PushStack(arg);\n+            }\n+            return Call();\n+        }\n+        \n+        /// <summary>\n+        /// Assuming normal args are already on the stack,\n+        /// then the prebound args would have to be inserted\n+        /// underneath them, which slightly violates the\n+        /// stack access rules.  So do it through this method\n+        /// if it's needed.  (If Call() was used, then it's not needed\n+        /// because Call() does this for you.  But if Call() was\n+        /// not used and someone just used bare parentheses like so:\n+        ///    function y { parameter a,b,c. print a+b+c. }\n+        ///    set x to y@:bind(1,2).\n+        ///    x(). // instead of saying x:call().\n+        /// then the prebound args don't get pushed by doing that, and they\n+        /// have to get pushed under the top by calling this.)\n+        /// </summary>\n+        public void InsertPreBoundArgs()\n+        {\n+            Stack<object> aboveArgs = new Stack<object>();\n+            object arg = \"\"; // doesn't matter what it is as long as it's non-null for the while check below.\n+            while (arg != null && !(arg is KOSArgMarkerType))\n+            {\n+                arg = cpu.PopStack();\n+                if (! (arg is KOSArgMarkerType))\n+                    aboveArgs.Push(arg);\n+            }\n+            if (arg == null)\n+                throw new KOSException(\"KOSDelegate.InsertPreBoundArgs: Stack arg bottom missing.\\n\" +\n+                                       \"Contact the kOS devs.  This message should 'never' happen.\");\n+            // Now re-push the args back, putting the preBound ones at the bottom\n+            // where they belong:\n+            cpu.PushStack(new KOSArgMarkerType());\n+            foreach (object item in preBoundArgs)\n+            {\n+                cpu.PushStack(item);\n+            }\n+            foreach (object item in aboveArgs) // Because this was pushed to a stack, this should show in reverse order.\n+            {\n+                cpu.PushStack(item);\n+            }\n+        }","path":"src/kOS.Safe/Encapsulation/KOSDelegate.cs","position":99,"original_position":99,"commit_id":"04880b377f9f1b74c1a209b23c751bd8f8bc6510","original_commit_id":"04880b377f9f1b74c1a209b23c751bd8f8bc6510","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":"Nope, that wasn't the problem.  never mind","created_at":"2015-12-17T06:27:24Z","updated_at":"2015-12-17T06:27:24Z","html_url":"https://github.com/KSP-KOS/KOS/pull/1315#discussion_r47873511","pull_request_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315","_links":{"self":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/comments/47873511"},"html":{"href":"https://github.com/KSP-KOS/KOS/pull/1315#discussion_r47873511"},"pull_request":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315"}}},"pull_request":{"url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315","id":53829264,"html_url":"https://github.com/KSP-KOS/KOS/pull/1315","diff_url":"https://github.com/KSP-KOS/KOS/pull/1315.diff","patch_url":"https://github.com/KSP-KOS/KOS/pull/1315.patch","issue_url":"https://api.github.com/repos/KSP-KOS/KOS/issues/1315","number":1315,"state":"open","locked":false,"title":"Delegation","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":"Phew.  Documenting this took more words than the actual code changes.  It's not an easy concept to ELIK to the new programmers out there.\r\n\r\nImplements the creation of delegates in kerboscript programs for at least user functions and builtin functions, but not for suffixes.\r\n","created_at":"2015-12-16T11:28:54Z","updated_at":"2015-12-17T06:27:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bfced17a2c91f0d538dbf870b3ee0addf5c9bc3c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315/commits","review_comments_url":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315/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/1315/comments","statuses_url":"https://api.github.com/repos/KSP-KOS/KOS/statuses/04880b377f9f1b74c1a209b23c751bd8f8bc6510","head":{"label":"Dunbaratu:delegation","ref":"delegation","sha":"04880b377f9f1b74c1a209b23c751bd8f8bc6510","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-12-16T11:37:22Z","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":46486,"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":3,"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"base":{"label":"KSP-KOS:develop","ref":"develop","sha":"026ceaacdf7bab1e5f3f2bb09fd089c30e7cfc44","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-12-13T13:14:55Z","pushed_at":"2015-12-16T11:37:22Z","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":49439,"stargazers_count":166,"watchers_count":166,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":80,"mirror_url":null,"open_issues_count":236,"forks":80,"open_issues":236,"watchers":166,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315"},"html":{"href":"https://github.com/KSP-KOS/KOS/pull/1315"},"issue":{"href":"https://api.github.com/repos/KSP-KOS/KOS/issues/1315"},"comments":{"href":"https://api.github.com/repos/KSP-KOS/KOS/issues/1315/comments"},"review_comments":{"href":"https://api.github.com/repos/KSP-KOS/KOS/pulls/1315/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/1315/commits"},"statuses":{"href":"https://api.github.com/repos/KSP-KOS/KOS/statuses/04880b377f9f1b74c1a209b23c751bd8f8bc6510"}}}}