Permalink
Browse files

Implement enif_get_atom{_length} NIF functions

  • Loading branch information...
krestenkrab committed Oct 8, 2013
1 parent ada16e2 commit db2dda2d4f8b9c701c465bc0204d897185f41622
Showing with 53 additions and 0 deletions.
  1. +42 −0 jnif/jnif_atom.cc
  2. +11 −0 src/main/java/erjang/EAtom.java
View
@@ -6,6 +6,8 @@ static jmethodID m_eobject__testAtom;
static jclass eatom_class;
static jmethodID m_eatom__intern;
static jmethodID m_eatom__existing_atom_or_null;
+static jmethodID m_eatom__latin1_bytes;
+static jmethodID m_eatom__latin1_length;
void initialize_jnif_atom(JavaVM* vm, JNIEnv *je)
{
@@ -22,9 +24,49 @@ void initialize_jnif_atom(JavaVM* vm, JNIEnv *je)
m_eatom__existing_atom_or_null = je->GetStaticMethodID(eatom_class,
"existing_atom_or_null",
"(Ljava/lang/String;)Lerjang/EAtom;");
+ m_eatom__latin1_bytes = je->GetMethodID(eatom_class,
+ "latin1_bytes",
+ "()[B");
+ m_eatom__latin1_length = je->GetMethodID(eatom_class,
+ "latin1_length",
+ "()I");
}
+int enif_get_atom_length(ErlNifEnv* ee, ERL_NIF_TERM term, unsigned* len, ErlNifCharEncoding encoding)
+{
+ JNIEnv *je = ee->je;
+ jobject atom = je->CallObjectMethod(E2J(term), m_eobject__testAtom);
+ if (atom == JVM_NULL || encoding != ERL_NIF_LATIN1)
+ return NIF_FALSE;
+
+ *len = je->CallIntMethod(atom, m_eatom__latin1_length);
+
+ return NIF_TRUE;
+}
+
+int enif_get_atom(ErlNifEnv* ee, ERL_NIF_TERM term, char* buf, unsigned size, ErlNifCharEncoding encoding)
+{
+ JNIEnv *je = ee->je;
+ jobject atom = je->CallObjectMethod(E2J(term), m_eobject__testAtom);
+ if (atom == JVM_NULL || encoding != ERL_NIF_LATIN1)
+ return 0;
+
+ jbyteArray barr = (jbyteArray) je->CallObjectMethod(atom, m_eatom__latin1_bytes);
+ jint byte_size = je->GetArrayLength(barr);
+ if (byte_size+1 > size)
+ return 0;
+
+ jbyte* arr = (jbyte*)je->GetPrimitiveArrayCritical(barr, 0);
+ memcpy(buf, arr, byte_size);
+ je->ReleasePrimitiveArrayCritical(barr, arr, JNI_ABORT);
+ buf[byte_size] = 0;
+
+ return byte_size+1;
+}
+
+
+
int enif_is_atom(ErlNifEnv* ee, ERL_NIF_TERM term)
{
JNIEnv *je = ee->je;
@@ -19,6 +19,7 @@
package erjang;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
@@ -141,6 +142,16 @@ public int hashCode() {
return hash;
}
+ // used by NIF API
+ public byte[] latin1_bytes() {
+ return value.getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ // used by NIF API
+ public int latin1_length() {
+ return value.length();
+ }
+
public static EAtom intern(String name) {

0 comments on commit db2dda2

Please sign in to comment.