{"action":"created","issue":{"url":"https://api.github.com/repos/svaarala/duktape/issues/692","repository_url":"https://api.github.com/repos/svaarala/duktape","labels_url":"https://api.github.com/repos/svaarala/duktape/issues/692/labels{/name}","comments_url":"https://api.github.com/repos/svaarala/duktape/issues/692/comments","events_url":"https://api.github.com/repos/svaarala/duktape/issues/692/events","html_url":"https://github.com/svaarala/duktape/pull/692","id":149779408,"number":692,"title":"Minimal operator overloading test","user":{"login":"svaarala","id":6377415,"avatar_url":"https://avatars.githubusercontent.com/u/6377415?v=3","gravatar_id":"","url":"https://api.github.com/users/svaarala","html_url":"https://github.com/svaarala","followers_url":"https://api.github.com/users/svaarala/followers","following_url":"https://api.github.com/users/svaarala/following{/other_user}","gists_url":"https://api.github.com/users/svaarala/gists{/gist_id}","starred_url":"https://api.github.com/users/svaarala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/svaarala/subscriptions","organizations_url":"https://api.github.com/users/svaarala/orgs","repos_url":"https://api.github.com/users/svaarala/repos","events_url":"https://api.github.com/users/svaarala/events{/privacy}","received_events_url":"https://api.github.com/users/svaarala/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-20T14:11:55Z","updated_at":"2016-04-20T14:14:50Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/svaarala/duktape/pulls/692","html_url":"https://github.com/svaarala/duktape/pull/692","diff_url":"https://github.com/svaarala/duktape/pull/692.diff","patch_url":"https://github.com/svaarala/duktape/pull/692.patch"},"body":"Operator overloading has been requested multiple times. The basic downside of adding it to Duktape is that it's not available in (all) other engines which causes a lock-in which would be nice to avoid.\r\n\r\nOn the other hand, Duktape is used in some environments where the ability to manipulate domain specific objects using operator overloading is convenient (for example, vector and matrix calculation).\r\n\r\nThis pull contains an example of how operator overloading could be added to Duktape with a minimal impact. The example approach is to add an operator overload check to arithmetic bytecode opcodes (ADD, etc) which is done after fast path checks (e.g. number+number addition for ADD) and only if operator overloading config option is enabled (disabled by default).\r\n\r\nTo avoid getting drawn into the specifics of how operator overloading is handled (for which there are multiple options) the example simply checks if `Duktape.add()` exists and if so, offloads the operation to that function.\r\n\r\nThis pull is mostly for discussion, it's not merge ready but provides some concreteness on a possible approach."},"comment":{"url":"https://api.github.com/repos/svaarala/duktape/issues/comments/212442955","html_url":"https://github.com/svaarala/duktape/pull/692#issuecomment-212442955","issue_url":"https://api.github.com/repos/svaarala/duktape/issues/692","id":212442955,"user":{"login":"svaarala","id":6377415,"avatar_url":"https://avatars.githubusercontent.com/u/6377415?v=3","gravatar_id":"","url":"https://api.github.com/users/svaarala","html_url":"https://github.com/svaarala","followers_url":"https://api.github.com/users/svaarala/followers","following_url":"https://api.github.com/users/svaarala/following{/other_user}","gists_url":"https://api.github.com/users/svaarala/gists{/gist_id}","starred_url":"https://api.github.com/users/svaarala/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/svaarala/subscriptions","organizations_url":"https://api.github.com/users/svaarala/orgs","repos_url":"https://api.github.com/users/svaarala/repos","events_url":"https://api.github.com/users/svaarala/events{/privacy}","received_events_url":"https://api.github.com/users/svaarala/received_events","type":"User","site_admin":false},"created_at":"2016-04-20T14:14:50Z","updated_at":"2016-04-20T14:14:50Z","body":"At this point the branch just demonstrates how to hook overloading into the executor:\r\n\r\n```\r\n((o) Duktape [linenoise] 1.4.99 (v1.4.0-397-g4435a65-dirty)\r\nduk> \"foo\" + 1\r\n= foo1\r\nduk> Duktape.add = function add(x,y) { print('ADD', x, y); return Number(x) + Number(y); };\r\n= function add() {\"ecmascript\"}\r\nduk> \"foo\" + 1\r\nADD foo 1\r\n= NaN\r\nduk> \"123\" + 1\r\nADD 123 1\r\n= 124\r\n```\r\n\r\nIt would be nice if the overload callback could somehow indicate it's not interested in the arguments given so that Duktape would fall back to standard semantics. As things are now in the example, if an overload function is present it must implement the entire semantics."}}