{"action":"created","comment":{"url":"https://api.github.com/repos/square/duktape-android/pulls/comments/52184321","id":52184321,"diff_hunk":"@@ -0,0 +1,160 @@\n+/*\n+ * Copyright (C) 2016 Square, Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *      http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+#include \"DuktapeContext.h\"\n+#include \"JString.h\"\n+#include \"JavaMethod.h\"\n+\n+namespace {\n+\n+// Internal names used to properties in the duktape context's global stash and bound variables.\n+// The \\xff\\xff part keeps the variable hidden from JavaScript (visible through C API only).\n+const char* JAVAVM_PROP_NAME = \"\\xff\\xffjavaVM\";\n+const char* JAVA_THIS_PROP_NAME = \"\\xff\\xffjava_this\";\n+const char* JAVA_METHOD_PROP_NAME = \"\\xff\\xffjava_method\";\n+\n+JNIEnv* getJNIEnv(duk_context *ctx) {\n+  duk_push_global_stash(ctx);\n+  duk_get_prop_string(ctx, -1, JAVAVM_PROP_NAME);\n+  JavaVM* javaVM = static_cast<JavaVM*>(duk_require_pointer(ctx, -1));\n+  duk_pop_2(ctx);\n+  JNIEnv* env;\n+  javaVM->AttachCurrentThread(&env, nullptr);\n+  return env;\n+}\n+\n+jobject getJavaThis(duk_context* ctx) {\n+  duk_push_this(ctx);\n+  duk_get_prop_string(ctx, -1, JAVA_THIS_PROP_NAME);\n+  jobject thisObject = static_cast<jobject>(duk_require_pointer(ctx, -1));\n+  duk_pop_2(ctx);\n+  return thisObject;\n+}\n+\n+JavaMethod* getJavaMethod(duk_context* ctx) {\n+  duk_push_current_function(ctx);\n+  duk_get_prop_string(ctx, -1, JAVA_METHOD_PROP_NAME);\n+  JavaMethod* method = static_cast<JavaMethod*>(duk_require_pointer(ctx, -1));\n+  duk_pop_2(ctx);\n+  return method;\n+}\n+\n+duk_int_t eval_string_with_filename(duk_context *ctx, const char *src,\n+                                    const char *fileName) {\n+  duk_push_string(ctx, fileName);\n+  return duk_eval_raw(ctx, src, 0, DUK_COMPILE_EVAL | DUK_COMPILE_SAFE |\n+                                   DUK_COMPILE_NOSOURCE | DUK_COMPILE_STRLEN);\n+}\n+\n+duk_ret_t handler(duk_context *ctx) {\n+  JavaMethod* method = getJavaMethod(ctx);\n+  return method ? method->Invoke(ctx, getJNIEnv(ctx), getJavaThis(ctx)) : DUK_RET_INTERNAL_ERROR;\n+}\n+\n+duk_ret_t javaObjectFinalizer(duk_context *ctx) {\n+  jobject thisObject = static_cast<jobject>(duk_require_pointer(ctx, -1));\n+  getJNIEnv(ctx)->DeleteGlobalRef(thisObject);\n+  duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);\n+  while (duk_next(ctx, -1, true)) {\n+    if (duk_get_prop_string(ctx, -1, JAVA_METHOD_PROP_NAME)) {\n+      delete static_cast<JavaMethod*>(duk_require_pointer(ctx, -1));\n+    }\n+    duk_pop_3(ctx);\n+  }\n+  duk_pop_2(ctx);\n+  return 0;\n+}\n+\n+} // anonymous namespace\n+\n+Duktape::Duktape(JavaVM* javaVM)\n+    : m_context(duk_create_heap_default()) {\n+  // Stash the JVM object in the context, so we can find our way back from a duktape C callback.\n+  duk_push_global_stash(m_context);\n+  duk_push_pointer(m_context, javaVM);\n+  duk_put_prop_string(m_context, -2, JAVAVM_PROP_NAME);\n+  duk_pop(m_context);\n+}\n+\n+Duktape::~Duktape() {\n+  duk_destroy_heap(m_context);\n+}\n+\n+jstring Duktape::evaluate(JNIEnv *env, jstring code, jstring fname) {\n+  const JString sourceCode(env, code);\n+  const JString fileName(env, fname);\n+\n+  jstring result = nullptr;\n+\n+  if (eval_string_with_filename(m_context, sourceCode, fileName) != 0) {\n+    jclass exceptionClass = env->FindClass(\"com/squareup/duktape/DuktapeException\");\n+\n+    // If it's a duktape error object, try to pull out the full stacktrace.\n+    if (duk_is_error(m_context, -1) && duk_get_prop_string(m_context, -1, \"stack\")) {\n+      const char* stack = duk_safe_to_string(m_context, -1);\n+\n+      // Is there an exception thrown from a Java method?\n+      if (duk_get_prop_string(m_context, -2, JavaMethod::JAVA_EXCEPTION_PROP_NAME)) {\n+        // TODO: add the duktape stack to this exception.\n+        jthrowable ex = static_cast<jthrowable>(duk_get_pointer(m_context, -1));\n+        env->Throw(ex);\n+        duk_pop(m_context);\n+      } else {\n+        env->ThrowNew(exceptionClass, stack);\n+      }\n+      duk_pop(m_context);\n+    } else {\n+      // Not an error or no stacktrace, just convert to a string.\n+      env->ThrowNew(exceptionClass, duk_safe_to_string(m_context, -1));\n+    }\n+  } else {\n+    result = env->NewStringUTF(duk_get_string(m_context, -1));\n+  }\n+\n+  duk_pop(m_context);\n+\n+  return result;\n+}\n+\n+void Duktape::bindInstance(JNIEnv *env, jstring instance, jobject object, jobjectArray methods) {\n+  duk_push_global_object(m_context);\n+  const duk_idx_t objIndex = duk_require_normalize_index(m_context, duk_push_object(m_context));\n+\n+  // Keep a reference in JavaScript to the object being bound.\n+  duk_push_pointer(m_context, env->NewGlobalRef(object));\n+  duk_put_prop_string(m_context, objIndex, JAVA_THIS_PROP_NAME);\n+  duk_push_c_function(m_context, javaObjectFinalizer, 1);\n+  duk_set_finalizer(m_context, objIndex);","path":"duktape/src/main/jni/DuktapeContext.cpp","position":139,"original_position":139,"commit_id":"7373ff371c388d4b5251a2c3627241501c37cfc6","original_commit_id":"7373ff371c388d4b5251a2c3627241501c37cfc6","user":{"login":"swankjesse","id":133019,"avatar_url":"https://avatars.githubusercontent.com/u/133019?v=3","gravatar_id":"","url":"https://api.github.com/users/swankjesse","html_url":"https://github.com/swankjesse","followers_url":"https://api.github.com/users/swankjesse/followers","following_url":"https://api.github.com/users/swankjesse/following{/other_user}","gists_url":"https://api.github.com/users/swankjesse/gists{/gist_id}","starred_url":"https://api.github.com/users/swankjesse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swankjesse/subscriptions","organizations_url":"https://api.github.com/users/swankjesse/orgs","repos_url":"https://api.github.com/users/swankjesse/repos","events_url":"https://api.github.com/users/swankjesse/events{/privacy}","received_events_url":"https://api.github.com/users/swankjesse/received_events","type":"User","site_admin":false},"body":"this is really cool.","created_at":"2016-02-08T15:44:01Z","updated_at":"2016-02-08T15:44:01Z","html_url":"https://github.com/square/duktape-android/pull/31#discussion_r52184321","pull_request_url":"https://api.github.com/repos/square/duktape-android/pulls/31","_links":{"self":{"href":"https://api.github.com/repos/square/duktape-android/pulls/comments/52184321"},"html":{"href":"https://github.com/square/duktape-android/pull/31#discussion_r52184321"},"pull_request":{"href":"https://api.github.com/repos/square/duktape-android/pulls/31"}}},"pull_request":{"url":"https://api.github.com/repos/square/duktape-android/pulls/31","id":58570511,"html_url":"https://github.com/square/duktape-android/pull/31","diff_url":"https://github.com/square/duktape-android/pull/31.diff","patch_url":"https://github.com/square/duktape-android/pull/31.patch","issue_url":"https://api.github.com/repos/square/duktape-android/issues/31","number":31,"state":"open","locked":false,"title":"Add function that binds a Java object to a named JavaScript global an…","user":{"login":"szurbrigg","id":13789594,"avatar_url":"https://avatars.githubusercontent.com/u/13789594?v=3","gravatar_id":"","url":"https://api.github.com/users/szurbrigg","html_url":"https://github.com/szurbrigg","followers_url":"https://api.github.com/users/szurbrigg/followers","following_url":"https://api.github.com/users/szurbrigg/following{/other_user}","gists_url":"https://api.github.com/users/szurbrigg/gists{/gist_id}","starred_url":"https://api.github.com/users/szurbrigg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/szurbrigg/subscriptions","organizations_url":"https://api.github.com/users/szurbrigg/orgs","repos_url":"https://api.github.com/users/szurbrigg/repos","events_url":"https://api.github.com/users/szurbrigg/events{/privacy}","received_events_url":"https://api.github.com/users/szurbrigg/received_events","type":"User","site_admin":false},"body":"…d allow it to be called from JavaScript.  Supports Strings and void argument/return types (will add more in a follow up).","created_at":"2016-02-08T04:44:19Z","updated_at":"2016-02-08T15:44:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"63b6524db979d9cb1dfe1d272f14838f03cdb1fa","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/square/duktape-android/pulls/31/commits","review_comments_url":"https://api.github.com/repos/square/duktape-android/pulls/31/comments","review_comment_url":"https://api.github.com/repos/square/duktape-android/pulls/comments{/number}","comments_url":"https://api.github.com/repos/square/duktape-android/issues/31/comments","statuses_url":"https://api.github.com/repos/square/duktape-android/statuses/7373ff371c388d4b5251a2c3627241501c37cfc6","head":{"label":"square:szurbrigg/0207-duktape-calls-java","ref":"szurbrigg/0207-duktape-calls-java","sha":"7373ff371c388d4b5251a2c3627241501c37cfc6","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":41445081,"name":"duktape-android","full_name":"square/duktape-android","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/duktape-android","description":"The Duktape embeddable Javascript engine packaged for Android.","fork":false,"url":"https://api.github.com/repos/square/duktape-android","forks_url":"https://api.github.com/repos/square/duktape-android/forks","keys_url":"https://api.github.com/repos/square/duktape-android/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/duktape-android/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/duktape-android/teams","hooks_url":"https://api.github.com/repos/square/duktape-android/hooks","issue_events_url":"https://api.github.com/repos/square/duktape-android/issues/events{/number}","events_url":"https://api.github.com/repos/square/duktape-android/events","assignees_url":"https://api.github.com/repos/square/duktape-android/assignees{/user}","branches_url":"https://api.github.com/repos/square/duktape-android/branches{/branch}","tags_url":"https://api.github.com/repos/square/duktape-android/tags","blobs_url":"https://api.github.com/repos/square/duktape-android/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/duktape-android/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/duktape-android/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/duktape-android/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/duktape-android/statuses/{sha}","languages_url":"https://api.github.com/repos/square/duktape-android/languages","stargazers_url":"https://api.github.com/repos/square/duktape-android/stargazers","contributors_url":"https://api.github.com/repos/square/duktape-android/contributors","subscribers_url":"https://api.github.com/repos/square/duktape-android/subscribers","subscription_url":"https://api.github.com/repos/square/duktape-android/subscription","commits_url":"https://api.github.com/repos/square/duktape-android/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/duktape-android/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/duktape-android/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/duktape-android/issues/comments{/number}","contents_url":"https://api.github.com/repos/square/duktape-android/contents/{+path}","compare_url":"https://api.github.com/repos/square/duktape-android/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/duktape-android/merges","archive_url":"https://api.github.com/repos/square/duktape-android/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/duktape-android/downloads","issues_url":"https://api.github.com/repos/square/duktape-android/issues{/number}","pulls_url":"https://api.github.com/repos/square/duktape-android/pulls{/number}","milestones_url":"https://api.github.com/repos/square/duktape-android/milestones{/number}","notifications_url":"https://api.github.com/repos/square/duktape-android/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/duktape-android/labels{/name}","releases_url":"https://api.github.com/repos/square/duktape-android/releases{/id}","deployments_url":"https://api.github.com/repos/square/duktape-android/deployments","created_at":"2015-08-26T19:17:25Z","updated_at":"2016-02-06T20:25:16Z","pushed_at":"2016-02-08T04:44:19Z","git_url":"git://github.com/square/duktape-android.git","ssh_url":"git@github.com:square/duktape-android.git","clone_url":"https://github.com/square/duktape-android.git","svn_url":"https://github.com/square/duktape-android","homepage":"","size":2275,"stargazers_count":234,"watchers_count":234,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":13,"mirror_url":null,"open_issues_count":3,"forks":13,"open_issues":3,"watchers":234,"default_branch":"master"}},"base":{"label":"square:master","ref":"master","sha":"79e78412b6cc10b05d59d1930cab3c3f1e24cc26","user":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"repo":{"id":41445081,"name":"duktape-android","full_name":"square/duktape-android","owner":{"login":"square","id":82592,"avatar_url":"https://avatars.githubusercontent.com/u/82592?v=3","gravatar_id":"","url":"https://api.github.com/users/square","html_url":"https://github.com/square","followers_url":"https://api.github.com/users/square/followers","following_url":"https://api.github.com/users/square/following{/other_user}","gists_url":"https://api.github.com/users/square/gists{/gist_id}","starred_url":"https://api.github.com/users/square/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/square/subscriptions","organizations_url":"https://api.github.com/users/square/orgs","repos_url":"https://api.github.com/users/square/repos","events_url":"https://api.github.com/users/square/events{/privacy}","received_events_url":"https://api.github.com/users/square/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/square/duktape-android","description":"The Duktape embeddable Javascript engine packaged for Android.","fork":false,"url":"https://api.github.com/repos/square/duktape-android","forks_url":"https://api.github.com/repos/square/duktape-android/forks","keys_url":"https://api.github.com/repos/square/duktape-android/keys{/key_id}","collaborators_url":"https://api.github.com/repos/square/duktape-android/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/square/duktape-android/teams","hooks_url":"https://api.github.com/repos/square/duktape-android/hooks","issue_events_url":"https://api.github.com/repos/square/duktape-android/issues/events{/number}","events_url":"https://api.github.com/repos/square/duktape-android/events","assignees_url":"https://api.github.com/repos/square/duktape-android/assignees{/user}","branches_url":"https://api.github.com/repos/square/duktape-android/branches{/branch}","tags_url":"https://api.github.com/repos/square/duktape-android/tags","blobs_url":"https://api.github.com/repos/square/duktape-android/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/square/duktape-android/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/square/duktape-android/git/refs{/sha}","trees_url":"https://api.github.com/repos/square/duktape-android/git/trees{/sha}","statuses_url":"https://api.github.com/repos/square/duktape-android/statuses/{sha}","languages_url":"https://api.github.com/repos/square/duktape-android/languages","stargazers_url":"https://api.github.com/repos/square/duktape-android/stargazers","contributors_url":"https://api.github.com/repos/square/duktape-android/contributors","subscribers_url":"https://api.github.com/repos/square/duktape-android/subscribers","subscription_url":"https://api.github.com/repos/square/duktape-android/subscription","commits_url":"https://api.github.com/repos/square/duktape-android/commits{/sha}","git_commits_url":"https://api.github.com/repos/square/duktape-android/git/commits{/sha}","comments_url":"https://api.github.com/repos/square/duktape-android/comments{/number}","issue_comment_url":"https://api.github.com/repos/square/duktape-android/issues/comments{/number}","contents_url":"https://api.github.com/repos/square/duktape-android/contents/{+path}","compare_url":"https://api.github.com/repos/square/duktape-android/compare/{base}...{head}","merges_url":"https://api.github.com/repos/square/duktape-android/merges","archive_url":"https://api.github.com/repos/square/duktape-android/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/square/duktape-android/downloads","issues_url":"https://api.github.com/repos/square/duktape-android/issues{/number}","pulls_url":"https://api.github.com/repos/square/duktape-android/pulls{/number}","milestones_url":"https://api.github.com/repos/square/duktape-android/milestones{/number}","notifications_url":"https://api.github.com/repos/square/duktape-android/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/square/duktape-android/labels{/name}","releases_url":"https://api.github.com/repos/square/duktape-android/releases{/id}","deployments_url":"https://api.github.com/repos/square/duktape-android/deployments","created_at":"2015-08-26T19:17:25Z","updated_at":"2016-02-06T20:25:16Z","pushed_at":"2016-02-08T04:44:19Z","git_url":"git://github.com/square/duktape-android.git","ssh_url":"git@github.com:square/duktape-android.git","clone_url":"https://github.com/square/duktape-android.git","svn_url":"https://github.com/square/duktape-android","homepage":"","size":2275,"stargazers_count":234,"watchers_count":234,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":13,"mirror_url":null,"open_issues_count":3,"forks":13,"open_issues":3,"watchers":234,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/square/duktape-android/pulls/31"},"html":{"href":"https://github.com/square/duktape-android/pull/31"},"issue":{"href":"https://api.github.com/repos/square/duktape-android/issues/31"},"comments":{"href":"https://api.github.com/repos/square/duktape-android/issues/31/comments"},"review_comments":{"href":"https://api.github.com/repos/square/duktape-android/pulls/31/comments"},"review_comment":{"href":"https://api.github.com/repos/square/duktape-android/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/square/duktape-android/pulls/31/commits"},"statuses":{"href":"https://api.github.com/repos/square/duktape-android/statuses/7373ff371c388d4b5251a2c3627241501c37cfc6"}}}}