Permalink
Browse files

add enif_make_string{_len} NIF functions

  • Loading branch information...
1 parent 2f5e73b commit ada16e27fccc65bdd9cb5fbe199f00e175b01889 @krestenkrab krestenkrab committed Oct 8, 2013
Showing with 35 additions and 1 deletion.
  1. +35 −1 jnif/jnif_string.cc
View
@@ -4,6 +4,9 @@
static jmethodID m_eobject__testString;
static jmethodID m_estring__stringValue;
+static jmethodID m_estring__make;
+
+static jclass estring_class;
void initialize_jnif_string(JavaVM* vm, JNIEnv *je)
@@ -13,11 +16,42 @@ void initialize_jnif_string(JavaVM* vm, JNIEnv *je)
"testString",
"()Lerjang/EString;");
- jclass estring_class = je->FindClass("erjang/EString");
+ estring_class = je->FindClass("erjang/EString");
+ estring_class = (jclass) je->NewGlobalRef(estring_class);
m_estring__stringValue = je->GetMethodID(estring_class,
"stringValue",
"()Ljava/lang/String;");
+ m_estring__make = je->GetStaticMethodID(estring_class,
+ "make",
+ "([BII)Lerjang/EString;");
+
+}
+
+ERL_NIF_TERM enif_make_string(ErlNifEnv* ee, const char* string, ErlNifCharEncoding encoding)
+{
+ return enif_make_string_len(ee, string, strlen(string), encoding);
+}
+
+ERL_NIF_TERM enif_make_string_len(ErlNifEnv* ee, const char* string, size_t size, ErlNifCharEncoding encoding)
+{
+ JNIEnv *je = ee->je;
+
+ if (encoding == ERL_NIF_LATIN1) {
+ jbyteArray barr = je->NewByteArray(size);
+ jbyte* arr = (jbyte*)je->GetPrimitiveArrayCritical(barr, 0);
+ memcpy(arr, string, size);
+ je->ReleasePrimitiveArrayCritical(barr, arr, JNI_COMMIT);
+
+ jobject result = je->CallStaticObjectMethod(estring_class,
+ m_estring__make,
+ barr, (jint)0, size);
+
+ return jnif_retain( ee, result );
+ } else {
+ // TODO: what do do?
+ abort();
+ }
}
int enif_get_string(ErlNifEnv* ee,

0 comments on commit ada16e2

Please sign in to comment.