Permalink
Browse files

update lame binding

The "gfp" is a regular Buffer instance, instead of a V8 object.
Fix some bugs.
  • Loading branch information...
1 parent e02ee35 commit 3d61c56f42186e607ce09ca52f94d186073f0f5d @TooTallNate committed Oct 14, 2012
Showing with 35 additions and 27 deletions.
  1. +35 −27 src/node_lame.cc
View
@@ -26,15 +26,13 @@ using namespace node;
namespace nodelame {
-#define UNWRAP_GFP \
- HandleScope scope; \
- lame_global_flags *gfp = NULL; \
- //Local<Object> wrapper = args[0]->ToObject(); \
- //lame_global_flags *gfp = (lame_global_flags *)wrapper->GetPointerFromInternalField(0);
-
#define PASTE2(a, b) a##b
#define PASTE(a, b) PASTE2(a, b)
+#define UNWRAP_GFP \
+ HandleScope scope; \
+ lame_global_flags *gfp = reinterpret_cast<lame_global_flags *>(Buffer::Data(args[0].As<Object>()));
+
#define FN(type, v8type, fn) \
Handle<Value> PASTE(node_lame_get_, fn) (const Arguments& args) { \
UNWRAP_GFP; \
@@ -43,15 +41,12 @@ Handle<Value> PASTE(node_lame_get_, fn) (const Arguments& args) { \
} \
Handle<Value> PASTE(node_lame_set_, fn) (const Arguments& args) { \
UNWRAP_GFP; \
- type input = (type)args[0]->PASTE(v8type, Value)(); \
+ type input = (type)args[1]->PASTE(v8type, Value)(); \
int output = PASTE(lame_set_, fn)(gfp, input); \
return scope.Close(Number::New(output)); \
}
-/* Wrapper ObjectTemplate to hold `lame_t` instances */
-Persistent<ObjectTemplate> gfpClass;
-
/* struct that's used for async encoding */
struct encode_req {
lame_global_flags *gfp;
@@ -64,6 +59,28 @@ struct encode_req {
};
+/*
+ * Called when the wrapped pointer is garbage collected.
+ * We never have to do anything here...
+ */
+
+void wrap_pointer_cb(char *data, void *hint) {
+ //fprintf(stderr, "wrap_pointer_cb\n");
+}
+
+Handle<Value> WrapPointer(char *ptr, size_t length) {
+ HandleScope scope;
+ void *user_data = NULL;
+ Buffer *buf = Buffer::New(ptr, length, wrap_pointer_cb, user_data);
+ return scope.Close(buf->handle_);
+}
+
+Handle<Value> WrapPointer(char *ptr) {
+ size_t size = 0;
+ return WrapPointer(ptr, size);
+}
+
+
/* get_lame_version() */
Handle<Value> node_get_lame_version (const Arguments& args) {
HandleScope scope;
@@ -78,33 +95,32 @@ Handle<Value> node_lame_close (const Arguments& args) {
}
/* called when a 'gfp' wrapper Object get's GC'd from JS-land */
+/*
void gfp_weak_callback (Persistent<Value> wrapper, void *arg) {
HandleScope scope;
lame_global_flags *gfp = (lame_global_flags *)arg;
lame_close(gfp);
wrapper.Dispose();
}
-
+*/
/* malloc()'s a `lame_t` struct and returns it to JS land */
Handle<Value> node_lame_init (const Arguments& args) {
HandleScope scope;
lame_global_flags *gfp = lame_init();
- // TODO: Check for NULL here
+ if (gfp == NULL) return Null();
// disable id3v2 in the encode stream;
// user must call lame_getid3v2_tag() manually
// TODO: Move to it's own binding function
lame_set_write_id3tag_automatic(gfp, 0);
- Persistent<Object> wrapper = Persistent<Object>::New(gfpClass->NewInstance());
- wrapper->SetPointerInInternalField(0, gfp);
- wrapper.MakeWeak(gfp, gfp_weak_callback);
-
+ Handle<Value> wrapper = WrapPointer((char *)gfp);
return scope.Close(wrapper);
}
+
/* encode a buffer on the thread pool. */
async_rtn
EIO_encode_buffer_interleaved (uv_work_t *req) {
@@ -148,13 +164,12 @@ Handle<Value> node_lame_encode_buffer_interleaved (const Arguments& args) {
UNWRAP_GFP;
// the input buffer
- char *input = Buffer::Data(args[1]->ToObject());
+ char *input = Buffer::Data(args[1].As<Object>());
int num_samples = args[2]->Int32Value();
// the output buffer
- Local<Object> outbuf = args[3]->ToObject();
int out_offset = args[4]->Int32Value();
- char *output = Buffer::Data(outbuf) + out_offset;
+ char *output = Buffer::Data(args[3].As<Object>()) + out_offset;
int output_size = args[5]->Int32Value();
encode_req *request = new encode_req;
@@ -240,10 +255,7 @@ Handle<Value> node_lame_get_id3v2_tag (const Arguments& args) {
/* lame_init_params(gfp) */
Handle<Value> node_lame_init_params (const Arguments& args) {
UNWRAP_GFP;
- if (lame_init_params(gfp) < 0) {
- return ThrowException(String::New("lame_init_params() failed"));
- }
- return Undefined();
+ return scope.Close(Number::New(lame_init_params(gfp)));
}
@@ -303,10 +315,6 @@ FN(int, Int32, highpasswidth);
void InitLame(Handle<Object> target) {
HandleScope scope;
- gfpClass = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
- gfpClass->SetInternalFieldCount(1);
-
-
#define CONST_INT(value) \
target->Set(String::NewSymbol(#value), Integer::New(value), \
static_cast<PropertyAttribute>(ReadOnly|DontDelete));

0 comments on commit 3d61c56

Please sign in to comment.