From 1696237a3f444f0e33a8947749b4d70f6feb511b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 22 May 2015 21:23:16 +0800 Subject: [PATCH] Pass Handle to upper HandleScope when returning a V8 Handle --- native_mate/callback.h | 117 ++++++++++++++++++++++++++++++++++++ native_mate/callback.h.pump | 23 +++++++ 2 files changed, 140 insertions(+) diff --git a/native_mate/callback.h b/native_mate/callback.h index 28e572f0e780..310964196ff8 100644 --- a/native_mate/callback.h +++ b/native_mate/callback.h @@ -22,6 +22,17 @@ typedef scoped_refptr > SafeV8Function; template struct V8FunctionInvoker; +template +struct V8FunctionInvoker()> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local val(holder->Call(holder, 0, NULL)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function) { @@ -45,6 +56,20 @@ struct V8FunctionInvoker { } }; +template +struct V8FunctionInvoker(P1)> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function, P1 a1) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local args[] = { + ConvertToV8(isolate, a1), + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1) { @@ -74,6 +99,22 @@ struct V8FunctionInvoker { } }; +template +struct V8FunctionInvoker(P1, P2)> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, + P2 a2) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local args[] = { + ConvertToV8(isolate, a1), + ConvertToV8(isolate, a2), + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2) { @@ -105,6 +146,23 @@ struct V8FunctionInvoker { } }; +template +struct V8FunctionInvoker(P1, P2, P3)> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, + P2 a2, P3 a3) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local args[] = { + ConvertToV8(isolate, a1), + ConvertToV8(isolate, a2), + ConvertToV8(isolate, a3), + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2, @@ -140,6 +198,24 @@ struct V8FunctionInvoker { } }; +template +struct V8FunctionInvoker(P1, P2, P3, P4)> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, + P2 a2, P3 a3, P4 a4) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local args[] = { + ConvertToV8(isolate, a1), + ConvertToV8(isolate, a2), + ConvertToV8(isolate, a3), + ConvertToV8(isolate, a4), + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2, @@ -177,6 +253,26 @@ struct V8FunctionInvoker { } }; +template +struct V8FunctionInvoker(P1, P2, P3, P4, P5)> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, + P2 a2, P3 a3, P4 a4, P5 a5) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local args[] = { + ConvertToV8(isolate, a1), + ConvertToV8(isolate, a2), + ConvertToV8(isolate, a3), + ConvertToV8(isolate, a4), + ConvertToV8(isolate, a5), + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { @@ -217,6 +313,27 @@ struct V8FunctionInvoker { } }; +template +struct V8FunctionInvoker(P1, P2, P3, P4, P5, P6)> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, + P2 a2, P3 a3, P4 a4, P5 a5, P6 a6) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + v8::Local args[] = { + ConvertToV8(isolate, a1), + ConvertToV8(isolate, a2), + ConvertToV8(isolate, a3), + ConvertToV8(isolate, a4), + ConvertToV8(isolate, a5), + ConvertToV8(isolate, a6), + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { diff --git a/native_mate/callback.h.pump b/native_mate/callback.h.pump index 1286fd0a579a..675391385564 100644 --- a/native_mate/callback.h.pump +++ b/native_mate/callback.h.pump @@ -29,6 +29,29 @@ $range ARITY 0..MAX_ARITY $for ARITY [[ $range ARG 1..ARITY +template +struct V8FunctionInvoker($for ARG , [[P$(ARG)]])> { + static v8::Local Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) { + Locker locker(isolate); + v8::EscapableHandleScope handle_scope(isolate); + v8::Local holder = function->NewHandle(); + +$if ARITY == 0 [[ + v8::Local val(holder->Call(holder, 0, NULL)); +]] $else [[ + v8::Local args[] = { +$for ARG [[ + + ConvertToV8(isolate, a$(ARG)), +]] + + }; + v8::Local val(holder->Call(holder, arraysize(args), args)); +]] + return handle_scope.Escape(val); + } +}; + template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) {