{"action":"created","comment":{"url":"https://api.github.com/repos/Samsung/jerryscript/pulls/comments/63184412","id":63184412,"diff_hunk":"@@ -369,7 +369,222 @@ main (int argc, char * argv[])\n \n The application inputs commands and evaluates them, one after another.\n \n+## Step 6. Creating JS object in global context\n+\n+In this example we demonstrate how to use native function and structures in JavaScript.\n+\n+```c\n+#include <string.h>\n+#include \"jerry.h\"\n+\n+struct my_struct\n+{\n+  const char *msg;\n+} my_struct;\n+\n+/**\n+ * Get a string from a native object\n+ */\n+static bool\n+get_msg_handler (const jerry_api_object_t *function_obj_p, /**< function object */\n+                 const jerry_api_value_t *this_p, /**< this arg */\n+                 jerry_api_value_t *ret_val_p, /**< return argument */\n+                 const jerry_api_value_t *args_p, /**< function arguments */\n+                 const jerry_api_length_t args_cnt) /**< number of function arguments */\n+{\n+  jerry_api_string_t *msg_str_p = jerry_api_create_string ((const jerry_api_char_t *) my_struct.msg);\n+  *ret_val_p = jerry_api_create_string_value (msg_str_p);\n+\n+  return true;\n+} /* get_msg_handler */\n+\n+int\n+main (int argc, char * argv[])\n+{\n+  jerry_completion_code_t status = JERRY_COMPLETION_CODE_OK;\n+\n+  /* Initialize engine */\n+  jerry_init (JERRY_FLAG_EMPTY);\n+\n+  /* Do something with the native object */\n+  my_struct.msg = \"Hello World\";\n+\n+  /* Create an empty JS object */\n+  jerry_api_object_t *object_p = jerry_api_create_object ();\n+\n+  /* Create a JS function object and wrap into a jerry value */\n+  jerry_api_value_t object_value;\n+  object_value.type = JERRY_API_DATA_TYPE_OBJECT;\n+  object_value.u.v_object = jerry_api_create_external_function (get_msg_handler);\n+\n+  /* Set the native function as a property of the empty JS object */\n+  jerry_api_set_object_field_value (object_p,\n+                                    (const jerry_api_char_t *) \"myFunc\",\n+                                    &object_value);\n+  jerry_api_release_value (&object_value);\n+\n+  /* Wrap the (now it is not empty) JS object into a jerry api value */\n+  object_value.type = JERRY_API_DATA_TYPE_OBJECT;\n+  object_value.u.v_object = object_p;\n+  jerry_api_object_t *global_obj_p = jerry_api_get_global ();\n+\n+  /* Add the JS object to the global context */\n+  jerry_api_set_object_field_value (global_obj_p,\n+                                    (const jerry_api_char_t *) \"MyObject\",\n+                                    &object_value);\n+  jerry_api_release_value (&object_value);\n+  jerry_api_release_object (global_obj_p);\n+\n+  /* Now we have a \"builtin\" object called MyObject with a function called myFunc()\n+   *\n+   * Equivalent JS code:\n+   *                    var MyObject = { myFunc() { return \"some string value\"; } }\n+   */\n+  const jerry_api_char_t script[] = \"var str = MyObject.myFunc(); \\\n+                                     print (str);\";\n+  size_t script_size = strlen ((const char *) script);\n+\n+  jerry_api_value_t eval_ret;\n+\n+  /* Evaluate script */\n+  status = jerry_api_eval (script,\n+                           script_size,\n+                           false,\n+                           false,\n+                           &eval_ret);\n+\n+  /* Free JavaScript value, returned by eval */\n+  jerry_api_release_value (&eval_ret);\n+\n+  /* Cleanup engine */\n+  jerry_cleanup ();\n+\n+  return status;\n+}\n+```\n+\n+The application will generate the following output:\n+\n+```bash\n+Hello World\n+```\n+\n+## Step 7. Extending JS Objects with native functions\n+\n+Here we create a JS Object with `jerry_api_eval`, then extend it with a native function. This function shows how to get a property value from the object and how to manipulate it.\n+\n+```c\n+#include <string.h>\n+#include \"jerry.h\"\n+\n+/**\n+ * Add param to 'this.x'\n+ */\n+static bool\n+add_handler (const jerry_api_object_t *function_obj_p, /**< function object */\n+             const jerry_api_value_t *this_p, /**< this arg */\n+             jerry_api_value_t *ret_val_p, /**< return argument */\n+             const jerry_api_value_t *args_p, /**< function arguments */\n+             const jerry_api_length_t args_cnt) /**< number of function arguments */\n+{\n+  jerry_api_value_t x_val;\n+\n+  /* Get 'this.x' */\n+  if (jerry_api_get_object_field_value (jerry_api_get_object_value (this_p),\n+                                        (const jerry_api_char_t *) \"x\",\n+                                        &x_val))\n+  {\n+    /* Convert Jerry API values to double */\n+    double x = jerry_api_get_number_value (&x_val);\n+    double d = jerry_api_get_number_value (args_p);\n+\n+    /* Add the parameter to 'x' */\n+    jerry_api_value_t res_val = jerry_api_create_number_value (x + d);\n+\n+    /* Set the new value of 'this.x' */\n+    jerry_api_set_object_field_value (jerry_api_get_object_value (this_p),\n+                                      (const jerry_api_char_t *) \"x\",\n+                                      &res_val);\n+\n+  }\n+\n+  return true;\n+} /* add_handler */\n+\n+int\n+main (int argc, char * argv[])\n+{\n+  jerry_completion_code_t status = JERRY_COMPLETION_CODE_OK;\n+\n+  /* Initialize engine */\n+  jerry_init (JERRY_FLAG_EMPTY);\n+\n+  /* Create a JS object */\n+  const jerry_api_char_t my_js_object[] = \"MyObject = \\\n+  { x : 12, \\\n+    y : 'Value of x is ', \\\n+    foo: function () \\\n+    { \\\n+      return this.y + this.x; \\\n+    } \\\n+  }\";\n+\n+  jerry_api_value_t my_js_obj_val;\n+  /* Evaluate script */\n+  status = jerry_api_eval (my_js_object,\n+                           strlen ((const char *) my_js_object),\n+                           false,\n+                           false,\n+                           &my_js_obj_val);\n+\n+  jerry_api_object_t *object_p = jerry_api_get_object_value (&my_js_obj_val);\n+\n+  /* Create a JS function object and wrap into a jerry value */\n+  jerry_api_value_t object_value;\n+  object_value.type = JERRY_API_DATA_TYPE_OBJECT;\n+  object_value.u.v_object = jerry_api_create_external_function (add_handler);\n+\n+  /* Set the native function as a property of previously created MyObject */\n+  jerry_api_set_object_field_value (object_p,\n+                                    (const jerry_api_char_t *) \"add2x\",\n+                                    &object_value);\n+  jerry_api_release_value (&object_value);\n+\n+  /* Free JavaScript value, returned by eval (my_js_object) */\n+  jerry_api_release_value (&my_js_obj_val);\n+\n+  const jerry_api_char_t script[] = \"var str = MyObject.foo (); \\\n+                                     print (str); \\\n+                                     MyObject.add2x (5); \\\n+                                     print (MyObject.foo());\";","path":"docs/API-EXAMPLE.md","position":191,"original_position":191,"commit_id":"76895f4b7fa704e2d7be5f75001c54e21ee42587","original_commit_id":"76895f4b7fa704e2d7be5f75001c54e21ee42587","user":{"login":"akiss77","id":9976482,"avatar_url":"https://avatars.githubusercontent.com/u/9976482?v=3","gravatar_id":"","url":"https://api.github.com/users/akiss77","html_url":"https://github.com/akiss77","followers_url":"https://api.github.com/users/akiss77/followers","following_url":"https://api.github.com/users/akiss77/following{/other_user}","gists_url":"https://api.github.com/users/akiss77/gists{/gist_id}","starred_url":"https://api.github.com/users/akiss77/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akiss77/subscriptions","organizations_url":"https://api.github.com/users/akiss77/orgs","repos_url":"https://api.github.com/users/akiss77/repos","events_url":"https://api.github.com/users/akiss77/events{/privacy}","received_events_url":"https://api.github.com/users/akiss77/received_events","type":"User","site_admin":false},"body":"Not bad. Perhaps break the closing quotes in a new line as well?","created_at":"2016-05-13T13:43:55Z","updated_at":"2016-05-13T13:43:55Z","html_url":"https://github.com/Samsung/jerryscript/pull/1055#discussion_r63184412","pull_request_url":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055","_links":{"self":{"href":"https://api.github.com/repos/Samsung/jerryscript/pulls/comments/63184412"},"html":{"href":"https://github.com/Samsung/jerryscript/pull/1055#discussion_r63184412"},"pull_request":{"href":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055"}}},"pull_request":{"url":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055","id":69796599,"html_url":"https://github.com/Samsung/jerryscript/pull/1055","diff_url":"https://github.com/Samsung/jerryscript/pull/1055.diff","patch_url":"https://github.com/Samsung/jerryscript/pull/1055.patch","issue_url":"https://api.github.com/repos/Samsung/jerryscript/issues/1055","number":1055,"state":"open","locked":false,"title":"Add new API examples","user":{"login":"LaszloLango","id":10030017,"avatar_url":"https://avatars.githubusercontent.com/u/10030017?v=3","gravatar_id":"","url":"https://api.github.com/users/LaszloLango","html_url":"https://github.com/LaszloLango","followers_url":"https://api.github.com/users/LaszloLango/followers","following_url":"https://api.github.com/users/LaszloLango/following{/other_user}","gists_url":"https://api.github.com/users/LaszloLango/gists{/gist_id}","starred_url":"https://api.github.com/users/LaszloLango/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LaszloLango/subscriptions","organizations_url":"https://api.github.com/users/LaszloLango/orgs","repos_url":"https://api.github.com/users/LaszloLango/repos","events_url":"https://api.github.com/users/LaszloLango/events{/privacy}","received_events_url":"https://api.github.com/users/LaszloLango/received_events","type":"User","site_admin":false},"body":"JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com","created_at":"2016-05-12T08:23:05Z","updated_at":"2016-05-13T13:43:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0053079260c64bb491e1477fb23654256ed330db","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055/commits","review_comments_url":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055/comments","review_comment_url":"https://api.github.com/repos/Samsung/jerryscript/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Samsung/jerryscript/issues/1055/comments","statuses_url":"https://api.github.com/repos/Samsung/jerryscript/statuses/76895f4b7fa704e2d7be5f75001c54e21ee42587","head":{"label":"LaszloLango:update-api-examples","ref":"update-api-examples","sha":"76895f4b7fa704e2d7be5f75001c54e21ee42587","user":{"login":"LaszloLango","id":10030017,"avatar_url":"https://avatars.githubusercontent.com/u/10030017?v=3","gravatar_id":"","url":"https://api.github.com/users/LaszloLango","html_url":"https://github.com/LaszloLango","followers_url":"https://api.github.com/users/LaszloLango/followers","following_url":"https://api.github.com/users/LaszloLango/following{/other_user}","gists_url":"https://api.github.com/users/LaszloLango/gists{/gist_id}","starred_url":"https://api.github.com/users/LaszloLango/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LaszloLango/subscriptions","organizations_url":"https://api.github.com/users/LaszloLango/orgs","repos_url":"https://api.github.com/users/LaszloLango/repos","events_url":"https://api.github.com/users/LaszloLango/events{/privacy}","received_events_url":"https://api.github.com/users/LaszloLango/received_events","type":"User","site_admin":false},"repo":{"id":38432323,"name":"jerryscript","full_name":"LaszloLango/jerryscript","owner":{"login":"LaszloLango","id":10030017,"avatar_url":"https://avatars.githubusercontent.com/u/10030017?v=3","gravatar_id":"","url":"https://api.github.com/users/LaszloLango","html_url":"https://github.com/LaszloLango","followers_url":"https://api.github.com/users/LaszloLango/followers","following_url":"https://api.github.com/users/LaszloLango/following{/other_user}","gists_url":"https://api.github.com/users/LaszloLango/gists{/gist_id}","starred_url":"https://api.github.com/users/LaszloLango/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LaszloLango/subscriptions","organizations_url":"https://api.github.com/users/LaszloLango/orgs","repos_url":"https://api.github.com/users/LaszloLango/repos","events_url":"https://api.github.com/users/LaszloLango/events{/privacy}","received_events_url":"https://api.github.com/users/LaszloLango/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/LaszloLango/jerryscript","description":"","fork":true,"url":"https://api.github.com/repos/LaszloLango/jerryscript","forks_url":"https://api.github.com/repos/LaszloLango/jerryscript/forks","keys_url":"https://api.github.com/repos/LaszloLango/jerryscript/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LaszloLango/jerryscript/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LaszloLango/jerryscript/teams","hooks_url":"https://api.github.com/repos/LaszloLango/jerryscript/hooks","issue_events_url":"https://api.github.com/repos/LaszloLango/jerryscript/issues/events{/number}","events_url":"https://api.github.com/repos/LaszloLango/jerryscript/events","assignees_url":"https://api.github.com/repos/LaszloLango/jerryscript/assignees{/user}","branches_url":"https://api.github.com/repos/LaszloLango/jerryscript/branches{/branch}","tags_url":"https://api.github.com/repos/LaszloLango/jerryscript/tags","blobs_url":"https://api.github.com/repos/LaszloLango/jerryscript/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LaszloLango/jerryscript/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LaszloLango/jerryscript/git/refs{/sha}","trees_url":"https://api.github.com/repos/LaszloLango/jerryscript/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LaszloLango/jerryscript/statuses/{sha}","languages_url":"https://api.github.com/repos/LaszloLango/jerryscript/languages","stargazers_url":"https://api.github.com/repos/LaszloLango/jerryscript/stargazers","contributors_url":"https://api.github.com/repos/LaszloLango/jerryscript/contributors","subscribers_url":"https://api.github.com/repos/LaszloLango/jerryscript/subscribers","subscription_url":"https://api.github.com/repos/LaszloLango/jerryscript/subscription","commits_url":"https://api.github.com/repos/LaszloLango/jerryscript/commits{/sha}","git_commits_url":"https://api.github.com/repos/LaszloLango/jerryscript/git/commits{/sha}","comments_url":"https://api.github.com/repos/LaszloLango/jerryscript/comments{/number}","issue_comment_url":"https://api.github.com/repos/LaszloLango/jerryscript/issues/comments{/number}","contents_url":"https://api.github.com/repos/LaszloLango/jerryscript/contents/{+path}","compare_url":"https://api.github.com/repos/LaszloLango/jerryscript/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LaszloLango/jerryscript/merges","archive_url":"https://api.github.com/repos/LaszloLango/jerryscript/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LaszloLango/jerryscript/downloads","issues_url":"https://api.github.com/repos/LaszloLango/jerryscript/issues{/number}","pulls_url":"https://api.github.com/repos/LaszloLango/jerryscript/pulls{/number}","milestones_url":"https://api.github.com/repos/LaszloLango/jerryscript/milestones{/number}","notifications_url":"https://api.github.com/repos/LaszloLango/jerryscript/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LaszloLango/jerryscript/labels{/name}","releases_url":"https://api.github.com/repos/LaszloLango/jerryscript/releases{/id}","deployments_url":"https://api.github.com/repos/LaszloLango/jerryscript/deployments","created_at":"2015-07-02T12:47:42Z","updated_at":"2016-02-17T14:18:30Z","pushed_at":"2016-05-13T13:30:16Z","git_url":"git://github.com/LaszloLango/jerryscript.git","ssh_url":"git@github.com:LaszloLango/jerryscript.git","clone_url":"https://github.com/LaszloLango/jerryscript.git","svn_url":"https://github.com/LaszloLango/jerryscript","homepage":null,"size":7718,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Samsung:master","ref":"master","sha":"210ba6dc851949132403e865005f24d7d445027f","user":{"login":"Samsung","id":6210390,"avatar_url":"https://avatars.githubusercontent.com/u/6210390?v=3","gravatar_id":"","url":"https://api.github.com/users/Samsung","html_url":"https://github.com/Samsung","followers_url":"https://api.github.com/users/Samsung/followers","following_url":"https://api.github.com/users/Samsung/following{/other_user}","gists_url":"https://api.github.com/users/Samsung/gists{/gist_id}","starred_url":"https://api.github.com/users/Samsung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Samsung/subscriptions","organizations_url":"https://api.github.com/users/Samsung/orgs","repos_url":"https://api.github.com/users/Samsung/repos","events_url":"https://api.github.com/users/Samsung/events{/privacy}","received_events_url":"https://api.github.com/users/Samsung/received_events","type":"Organization","site_admin":false},"repo":{"id":33339660,"name":"jerryscript","full_name":"Samsung/jerryscript","owner":{"login":"Samsung","id":6210390,"avatar_url":"https://avatars.githubusercontent.com/u/6210390?v=3","gravatar_id":"","url":"https://api.github.com/users/Samsung","html_url":"https://github.com/Samsung","followers_url":"https://api.github.com/users/Samsung/followers","following_url":"https://api.github.com/users/Samsung/following{/other_user}","gists_url":"https://api.github.com/users/Samsung/gists{/gist_id}","starred_url":"https://api.github.com/users/Samsung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Samsung/subscriptions","organizations_url":"https://api.github.com/users/Samsung/orgs","repos_url":"https://api.github.com/users/Samsung/repos","events_url":"https://api.github.com/users/Samsung/events{/privacy}","received_events_url":"https://api.github.com/users/Samsung/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Samsung/jerryscript","description":"Ultra-lightweight JavaScript engine for Internet of Things. http://www.jerryscript.net","fork":false,"url":"https://api.github.com/repos/Samsung/jerryscript","forks_url":"https://api.github.com/repos/Samsung/jerryscript/forks","keys_url":"https://api.github.com/repos/Samsung/jerryscript/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Samsung/jerryscript/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Samsung/jerryscript/teams","hooks_url":"https://api.github.com/repos/Samsung/jerryscript/hooks","issue_events_url":"https://api.github.com/repos/Samsung/jerryscript/issues/events{/number}","events_url":"https://api.github.com/repos/Samsung/jerryscript/events","assignees_url":"https://api.github.com/repos/Samsung/jerryscript/assignees{/user}","branches_url":"https://api.github.com/repos/Samsung/jerryscript/branches{/branch}","tags_url":"https://api.github.com/repos/Samsung/jerryscript/tags","blobs_url":"https://api.github.com/repos/Samsung/jerryscript/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Samsung/jerryscript/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Samsung/jerryscript/git/refs{/sha}","trees_url":"https://api.github.com/repos/Samsung/jerryscript/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Samsung/jerryscript/statuses/{sha}","languages_url":"https://api.github.com/repos/Samsung/jerryscript/languages","stargazers_url":"https://api.github.com/repos/Samsung/jerryscript/stargazers","contributors_url":"https://api.github.com/repos/Samsung/jerryscript/contributors","subscribers_url":"https://api.github.com/repos/Samsung/jerryscript/subscribers","subscription_url":"https://api.github.com/repos/Samsung/jerryscript/subscription","commits_url":"https://api.github.com/repos/Samsung/jerryscript/commits{/sha}","git_commits_url":"https://api.github.com/repos/Samsung/jerryscript/git/commits{/sha}","comments_url":"https://api.github.com/repos/Samsung/jerryscript/comments{/number}","issue_comment_url":"https://api.github.com/repos/Samsung/jerryscript/issues/comments{/number}","contents_url":"https://api.github.com/repos/Samsung/jerryscript/contents/{+path}","compare_url":"https://api.github.com/repos/Samsung/jerryscript/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Samsung/jerryscript/merges","archive_url":"https://api.github.com/repos/Samsung/jerryscript/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Samsung/jerryscript/downloads","issues_url":"https://api.github.com/repos/Samsung/jerryscript/issues{/number}","pulls_url":"https://api.github.com/repos/Samsung/jerryscript/pulls{/number}","milestones_url":"https://api.github.com/repos/Samsung/jerryscript/milestones{/number}","notifications_url":"https://api.github.com/repos/Samsung/jerryscript/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Samsung/jerryscript/labels{/name}","releases_url":"https://api.github.com/repos/Samsung/jerryscript/releases{/id}","deployments_url":"https://api.github.com/repos/Samsung/jerryscript/deployments","created_at":"2015-04-03T01:10:31Z","updated_at":"2016-05-13T00:38:06Z","pushed_at":"2016-05-13T13:43:12Z","git_url":"git://github.com/Samsung/jerryscript.git","ssh_url":"git@github.com:Samsung/jerryscript.git","clone_url":"https://github.com/Samsung/jerryscript.git","svn_url":"https://github.com/Samsung/jerryscript","homepage":"","size":26274,"stargazers_count":1265,"watchers_count":1265,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":140,"mirror_url":null,"open_issues_count":67,"forks":140,"open_issues":67,"watchers":1265,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055"},"html":{"href":"https://github.com/Samsung/jerryscript/pull/1055"},"issue":{"href":"https://api.github.com/repos/Samsung/jerryscript/issues/1055"},"comments":{"href":"https://api.github.com/repos/Samsung/jerryscript/issues/1055/comments"},"review_comments":{"href":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055/comments"},"review_comment":{"href":"https://api.github.com/repos/Samsung/jerryscript/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Samsung/jerryscript/pulls/1055/commits"},"statuses":{"href":"https://api.github.com/repos/Samsung/jerryscript/statuses/76895f4b7fa704e2d7be5f75001c54e21ee42587"}}}}