From 4ac67cf3435b3e15f95fadc20c98e37abf706ea4 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 13 Jun 2018 19:38:22 +0200 Subject: Demo protobufs in deno2. Adds deno_set_response() to allow stack allocated responses. --- deno2/deno.cc | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'deno2/deno.cc') diff --git a/deno2/deno.cc b/deno2/deno.cc index 27dfefb8e..d59f49065 100644 --- a/deno2/deno.cc +++ b/deno2/deno.cc @@ -154,15 +154,12 @@ void Pub(const v8::FunctionCallbackInfo& args) { const_cast(reinterpret_cast(contents.Data())); deno_buf buf{data, contents.ByteLength()}; - auto retbuf = d->cb(d, channel, buf); - if (retbuf.data) { - // TODO(ry) Support zero-copy. - auto ab = v8::ArrayBuffer::New(d->isolate, retbuf.len); - memcpy(ab->GetContents().Data(), retbuf.data, retbuf.len); - args.GetReturnValue().Set(handle_scope.Escape(ab)); - } else { - args.GetReturnValue().Set(v8::Null(d->isolate)); - } + assert(d->currentArgs == nullptr); + d->currentArgs = &args; + + d->cb(d, channel, buf); + + d->currentArgs = nullptr; } bool Execute(v8::Local context, const char* js_filename, @@ -334,6 +331,13 @@ bool deno_pub(Deno* d, const char* channel, deno_buf buf) { return true; } +void deno_set_response(Deno* d, deno_buf buf) { + // TODO(ry) Support zero-copy. + auto ab = v8::ArrayBuffer::New(d->isolate, buf.len); + memcpy(ab->GetContents().Data(), buf.data, buf.len); + d->currentArgs->GetReturnValue().Set(ab); +} + void deno_delete(Deno* d) { d->isolate->Dispose(); delete d; -- cgit v1.2.3