This is an automated email from the ASF dual-hosted git repository.

ronny pushed a commit to branch spidermonkey_version_nif
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit ee02a0dc29cdb2581319e35c41eb7e750a6e644e
Author: Ronny Berndt <[email protected]>
AuthorDate: Fri Oct 14 13:20:38 2022 +0200

    Introduce Spidermonkey NIF
    
    An Erlang NIF to get the Spidermonky version from the included lib.
---
 .../priv/couch_spidermonkey/couch_spidermonkey.cpp | 50 ++++++++++++++++++++++
 .../priv/couch_spidermonkey/couch_spidermonkey.erl | 19 ++++++++
 2 files changed, 69 insertions(+)

diff --git a/src/couch/priv/couch_spidermonkey/couch_spidermonkey.cpp 
b/src/couch/priv/couch_spidermonkey/couch_spidermonkey.cpp
new file mode 100644
index 000000000..15b1e1d71
--- /dev/null
+++ b/src/couch/priv/couch_spidermonkey/couch_spidermonkey.cpp
@@ -0,0 +1,50 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy 
of
+ * the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+
+#include <string.h>
+#include <jsapi.h>
+#include "erl_nif.h"
+
+using namespace std;
+
+ERL_NIF_TERM
+get_spidermonkey_version(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+    const string JAVASCRIPT = "JavaScript-C";
+    int js_len = JAVASCRIPT.length();
+
+    // JS_GetImplementationVersion()
+    // returns "JavaScript-CMAJOR.MINOR.PATCH"
+    const string FULLVERSION = JS_GetImplementationVersion();
+    int full_len = FULLVERSION.length();
+
+    //ignore "JavaScript-C"
+    string result = FULLVERSION.substr(js_len, full_len-js_len);
+
+    return enif_make_string(env, result.c_str(), ERL_NIF_LATIN1);
+}
+
+static ErlNifFunc nif_functions[] = {
+    {"get_spidermonkey_version", 0, get_spidermonkey_version}
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ERL_NIF_INIT(couch_spidermonkey, nif_functions, NULL, NULL, NULL, NULL);
+
+#ifdef __cplusplus
+}
+#endif
\ No newline at end of file
diff --git a/src/couch/priv/couch_spidermonkey/couch_spidermonkey.erl 
b/src/couch/priv/couch_spidermonkey/couch_spidermonkey.erl
new file mode 100644
index 000000000..801d46111
--- /dev/null
+++ b/src/couch/priv/couch_spidermonkey/couch_spidermonkey.erl
@@ -0,0 +1,19 @@
+%%%-------------------------------------------------------------------
+%%% @author big-r
+%%% @copyright (C) 2022, <COMPANY>
+%%% @doc
+%%%
+%%% @end
+%%% Created : 13. Okt 2022 19:40
+%%%-------------------------------------------------------------------
+-module(couch_spidermonkey).
+
+-export([get_spidermonkey_version/0]).
+-nifs([get_spidermonkey_version/0]).
+-on_load(init/0).
+
+init() ->
+  ok = erlang:load_nif("./couch_spidermonkey", 0).
+
+get_spidermonkey_version() ->
+  exit(nif_library_not_loaded).
\ No newline at end of file

Reply via email to