Skip to content

Commit

Permalink
Added workaround for function template memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Viatcheslav Gachkaylo committed Feb 16, 2011
1 parent 7f8ef53 commit 7961762
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
19 changes: 0 additions & 19 deletions v8-bindings/v8c-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include <stdlib.h>

typedef struct _V8InvocationCallbackData V8InvocationCallbackData;

class V8CHandleScope : public v8::HandleScope
{
public:
Expand All @@ -14,21 +12,4 @@ class V8CHandleScope : public v8::HandleScope
void operator delete(void* p, size_t size) { free(p); }
};

/*
class V8CFunctionTemplate : public v8::FunctionTemplate
{
public:
static v8::Local<FunctionTemplate> NewWithOptions(
v8::InvocationCallback callback = 0,
v8::Handle<v8::Value> data = v8::Handle<v8::Value>(),
v8::Handle<v8::Signature> signature = v8::Handle<v8::Signature>(),
V8InvocationCallbackData *deleteAfterUsing);
~V8CFunctionTemplate()
void SetCallHandler(v8::InvocationCallback callback,
v8::Handle<v8::Value> data = v8::Handle<v8::Value>());
void Set
private:
V8CFunctionTemplate() : v8::FunctionTemplate() {}
};
*/
#endif // #ifndef __V8C_PRIVATE_H__
26 changes: 15 additions & 11 deletions v8-bindings/v8c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,29 @@ V8Handle v8_arguments_get(const V8Arguments* args, int i) {
return unwrap_handle((*args)[i]);
}

struct _V8InvocationCallbackData {
V8InvocationCallback callback;
// TODO: void* data.
};
static v8::Handle<v8::Value> v8_invocation_callback_no_data(const v8::Arguments& args) {
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(args.Data());
V8InvocationCallback callback =
reinterpret_cast<V8InvocationCallback>(data->Value());
return wrap_handle<v8::Value>(callback(&args));
}

static v8::Handle<v8::Value> v8_invocation_callback(const v8::Arguments& args) {
static v8::Handle<v8::Value> v8_invocation_callback_with_data(const v8::Arguments& args) {
v8::Local<v8::External> data = v8::Local<v8::External>::Cast(args.Data());
V8InvocationCallbackData* callback_data =
static_cast<V8InvocationCallbackData*>(data->Value());
return wrap_handle<v8::Value>(callback_data->callback(&args));
return wrap_handle<v8::Value>(callback_data->callback(&args, callback_data->data));
}

V8Handle v8_function_template_new(V8InvocationCallback callback) {
// XXX this leaks. We need to somehow tie this lifetime to the
// function template's lifetime?
V8InvocationCallbackData* callback_data = new V8InvocationCallbackData;
callback_data->callback = callback;
return unwrap_handle(
v8::FunctionTemplate::New(v8_invocation_callback,
v8::FunctionTemplate::New(v8_invocation_callback_no_data,
v8::External::New((void*)callback)));
}

V8Handle v8_function_template_new_with_data(V8InvocationCallbackData *callback_data) {
return unwrap_handle(
v8::FunctionTemplate::New(v8_invocation_callback_with_data,
v8::External::New(callback_data)));
}

Expand Down
10 changes: 10 additions & 0 deletions v8-bindings/v8c.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,18 @@ V8Handle v8_arguments_get(const V8Arguments* args, int i);
it internally. We could still provide extra-data-like
functionality if needed, though. */
typedef V8Handle (*V8InvocationCallback)(const V8Arguments*);
typedef V8Handle (*V8InvocationCallbackWithData)(const V8Arguments*, void*);
typedef struct _V8InvocationCallbackData {
V8InvocationCallbackWithData callback;
void *data;
} V8InvocationCallbackData;

V8Handle v8_function_template_new(V8InvocationCallback callback);

/* Creates JavaScript callback with custom data passed to the C callback function */
/* callbackData pointer must be valid until function template handle is disposed */
V8Handle v8_function_template_new_with_data(V8InvocationCallbackData *callbackData);

/* ObjectTemplate */
V8Handle v8_object_template_new();

Expand Down

0 comments on commit 7961762

Please sign in to comment.