{"action":"created","issue":{"url":"https://api.github.com/repos/Codewars/codewars.com/issues/279","repository_url":"https://api.github.com/repos/Codewars/codewars.com","labels_url":"https://api.github.com/repos/Codewars/codewars.com/issues/279/labels{/name}","comments_url":"https://api.github.com/repos/Codewars/codewars.com/issues/279/comments","events_url":"https://api.github.com/repos/Codewars/codewars.com/issues/279/events","html_url":"https://github.com/Codewars/codewars.com/issues/279","id":147323388,"number":279,"title":"Redefining Function.prototype.toString causes typed array constructors to throw TypeError","user":{"login":"paul-calvelage","id":5090280,"avatar_url":"https://avatars.githubusercontent.com/u/5090280?v=3","gravatar_id":"","url":"https://api.github.com/users/paul-calvelage","html_url":"https://github.com/paul-calvelage","followers_url":"https://api.github.com/users/paul-calvelage/followers","following_url":"https://api.github.com/users/paul-calvelage/following{/other_user}","gists_url":"https://api.github.com/users/paul-calvelage/gists{/gist_id}","starred_url":"https://api.github.com/users/paul-calvelage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paul-calvelage/subscriptions","organizations_url":"https://api.github.com/users/paul-calvelage/orgs","repos_url":"https://api.github.com/users/paul-calvelage/repos","events_url":"https://api.github.com/users/paul-calvelage/events{/privacy}","received_events_url":"https://api.github.com/users/paul-calvelage/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-11T05:55:07Z","updated_at":"2016-04-12T18:20:00Z","closed_at":null,"body":"This is a bug I discovered while working on a Javascript kata, where the author had redefined `Function.prototype.toString` in the preloaded code section, I am guessing in an attempt to prevent cheating.  I then attempted a solution using typed arrays and stumbled onto this.  Luckily I have enough honor points to poke around the beta kata to see what is happening.\r\n\r\nAnyway, I can reproduce the problem on other kata, which is why I am reporting it here rather than just to the author.  The steps below are sufficient to duplicate the issue on any kata.\r\n\r\nFor some reason I am not able to grasp, redefining `Function.prototype.toString` causes the typed array constructors, such as Int16Array, to throw a TypeError (not directly, but via the backend of Codewars somewhere).  This is unexpected, and I'm sure the author was not trying to block the use of typed arrays by hiding the source code of functions.\r\n\r\n### How to reproduce the problem:\r\n__Step 1__: (While logged in...) Train on any random Javascript kata (it doesn't matter, say http://www.codewars.com/kata/56dae9dc54c0acd29d00109a/train/javascript just for example).\r\n__Step 2__: In the \"Your Solution\" editor paste:\r\n```javascript\r\nFunction.prototype.toString = {};\r\nvar x = new Int16Array();\r\nvar y = new Int16Array();\r\n```\r\n__Step 3__: In the \"Your Test Cases\" editor paste:\r\n```javascript\r\nTest.expect(true);\r\n```\r\n__Step 4__: Click \"Run Tests\"\r\n\r\n__Expected behavior__: \"Test Passed\" appears in the \"Output\" window highlighted in green.  This is expected since `Test.expect(true)` should pass and `new Int16Array()` should not throw an error.\r\n__Buggy behavior__: \"TypeError: Cannot convert object to primitive value\" appears in the \"Output\" window highlighted in red.\r\n\r\n![solution](https://cloud.githubusercontent.com/assets/5090280/14417908/3be2a946-ff88-11e5-969d-361ae430bc81.PNG)\r\n![test](https://cloud.githubusercontent.com/assets/5090280/14417910/3f4bbfd2-ff88-11e5-98f1-af80c441f332.PNG)\r\n![error](https://cloud.githubusercontent.com/assets/5090280/14417911/416982f4-ff88-11e5-9a9e-86bd5cbc3851.PNG)\r\n\r\n\r\n### Clues\r\nSomething in the backend could be calling `toString` on Int16Array.  This code:\r\n```javascript\r\nFunction.prototype.toString = function() { console.log(this); };\r\nvar x = new Int16Array();\r\nvar y = new Int16Array();\r\n```\r\nproduces:\r\n```\r\n[Function: get]\r\n[Function: set]\r\n[Function: slice]\r\n[Function: subarray]\r\n[Function: slice]\r\nTest Passed\r\n```\r\nAnd this:\r\n```javascript\r\nFunction.prototype.toString = function() { Int16Array(); };\r\nvar x = new Int16Array();\r\nvar y = new Int16Array();\r\n```\r\nis enough to blow up the stack via recursion\r\n```\r\nRangeError: Maximum call stack size exceeded\r\n```\r\nSo why is `toString` being called on `Int16Array` functions?  Is noderunner doing it or something else?"},"comment":{"url":"https://api.github.com/repos/Codewars/codewars.com/issues/comments/209040363","html_url":"https://github.com/Codewars/codewars.com/issues/279#issuecomment-209040363","issue_url":"https://api.github.com/repos/Codewars/codewars.com/issues/279","id":209040363,"user":{"login":"paul-calvelage","id":5090280,"avatar_url":"https://avatars.githubusercontent.com/u/5090280?v=3","gravatar_id":"","url":"https://api.github.com/users/paul-calvelage","html_url":"https://github.com/paul-calvelage","followers_url":"https://api.github.com/users/paul-calvelage/followers","following_url":"https://api.github.com/users/paul-calvelage/following{/other_user}","gists_url":"https://api.github.com/users/paul-calvelage/gists{/gist_id}","starred_url":"https://api.github.com/users/paul-calvelage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paul-calvelage/subscriptions","organizations_url":"https://api.github.com/users/paul-calvelage/orgs","repos_url":"https://api.github.com/users/paul-calvelage/repos","events_url":"https://api.github.com/users/paul-calvelage/events{/privacy}","received_events_url":"https://api.github.com/users/paul-calvelage/received_events","type":"User","site_admin":false},"created_at":"2016-04-12T18:20:00Z","updated_at":"2016-04-12T18:20:00Z","body":"I agree that it might not be Codewars.  My first instinct was that this could be a problem with the version of Node.js and/or Babel.  But I couldn't reproduce it using Node.js on my computer, and I didn't see anything in the Node.js or Babel bug database relating to this.  It's very possible I missed something.  \r\n\r\nWhen I have more time I might try downgrading my machine to the versions that Codewars is using to see if this is something that has already been fixed upstream.\r\n\r\nAt my suggestion, the author of the kata I noticed this on had already worked around the problem yesterday by changing the definition to\r\n```javascript\r\nFunction.prototype.toString = function() { return ''; }\r\n```\r\n(http://www.codewars.com/kata/how-many-are-smaller-than-me-ii/discuss#570b448b023799af2d000a51)\r\n\r\nThere didn't seem to be any obvious ill effects.  But I'm still scratching my head about the cause."}}