Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

389
Visualizações
node.js: How to yield from within a C++ module?

Background: v8 supports yield (old news, I know), and that is great to do away with callbacks in javascript code such as what is used in node.js (see https://wingolog.org/archives/2013/05/08/generators-in-v8 )

The question: since a javascript co-routine can call C++ code (via a module), how can the invoked C++ perform a yield operation? To illustrate:

// javascript
function* values()
{
    yield 27;
    mycppmodule.someFunction();
}

// c++
mycppmodule::someFunction()
{
    __somehow_yield( 28 ); // how can we make this happen?
}

// user of the code above
var o = values();
o.next(); //  returns 27 - came from javascript above
o.next(); //  returns 28  - came from c++ above, which is invoked by js

I suspect the answer is somewhere in the V8 API (https://v8docs.nodesource.com/node-7.4/) , but my search did not yield (pun intended) any results...

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your mycppmodule.someFunction(); couldn't yield the value from the generator returned by the generator function values() even if it was written in JavaScript. If you want to do something in C++ that works like some other code in JavaScript then you must make sure that it would work in JavaScript in the first place.

If you want to use generator-based coroutines (like with co module or Bluebirds's coroutine) then the situation is a little bit different - any coroutine just returns a promise and what you yield from a generator is actually a promise that you want resolved and injected in the next run of the generator, but it doesn't seem to be the case here.

That having been said, first make sure that you idea can be implemented in JsvaScript and I argue that this:

function* values() {
    yield 27;
    someFunction();
}

let someFunction = // fill the blanks

var o = values();
o.next(); // returns 27 - came from generator
o.next(); // returns 28 - came from someFunction()

cannot be implemented without changing the values() generator function to something like:

function* values() {
    yield 27;
    let gen = someFunction();
    yield gen.next();
}

or:

function* values() {
    yield 27;
    yield* someFunction();
}

If you are OK with changing the original values() function then read on.

Now, all that a generator function does is it returns an object that is a generator. That generator has methods like .next(), .throw() and .return(). If you create an object in C++ that has the right interface then you might be able to use it as a generator in JavaScript with keywords like yield* but I would have to test it to make sure about it.

See the docs:

  • yield
  • yield*
  • function*
  • Generator
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda