Skip to content

Instantly share code, notes, and snippets.

@kovasb
Created June 24, 2014 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kovasb/6a9bb54316ff0e0e051e to your computer and use it in GitHub Desktop.
Save kovasb/6a9bb54316ff0e0e051e to your computer and use it in GitHub Desktop.
/*static*/ v8::Handle<v8::Value> Java::bufferFromDirect(const v8::Arguments& args) {
v8::HandleScope scope;
Java* self = node::ObjectWrap::Unwrap<Java>(args.This());
v8::Handle<v8::Value> ensureJvmResults = self->ensureJvm();
if(!ensureJvmResults->IsUndefined()) {
return v8::False();
}
JNIEnv* env = self->getJavaEnv();
JavaScope javaScope(env);
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(args[0]);
jobject instance = v8ToJava(env, obj);
if (!instance) {
// not even a Java object
return v8::False();
}
char* data = (char *) env->GetDirectBufferAddress(instance);
jlong size = env->GetDirectBufferCapacity(instance);
Buffer* buffer = Buffer::New(data, size, Unmap, (void *) size);
return scope.Close(buffer->handle_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment