Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
Load native C modules in duktape 2.1.0 to

I am still lost after reading the how-to article.

  • https://github.com/svaarala/duktape/tree/master/extras/module-node

It is said that

The load callback is a Duktape/C function which takes the resolved module ID and: (1) returns the Ecmascript source code for the module or undefined if there's no source code, e.g. for pure C modules, (2) can populate module.exports itself, and (3) can replace module.exports.

But when loading a native C module,

  • what should be pushed into the value stack? duk_push_undefined(ctx) instead of duk_push_string(ctx, module_source)?
  • what should be returned by the load callback to its caller? return 0 instead of return 1?

I tried to call myobject_init (using the default instance in http://wiki.duktape.org/HowtoNativeConstructor.html) in the load callback cb_load_module. But duktape complains

TypeError: [object Object] not constructable

when I evaluate var MyObject = require("MyObject"), no matter if I

  • push undefined into the value stack and return 1,
  • or push nothing into the value stack and return 0.
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Problem solved. There are a few more details scattered in

  • https://github.com/svaarala/duktape/blob/master/doc/c-module-convention.rst
  • http://wiki.duktape.org/HowtoModules.html It is about duktape 1.x module loading framework, but it helps understanding 2.x framework.

The most important tricks are:

  • The module init function should return a function (constructor is also a function). Do NOT register the function using duk_put_global_string.

    duk_ret_t module_init(duk_context* ctx)
    {
        duk_push_c_function(ctx, module_constructor, num_arguments_for_constructor);
        // set properties for the constructor
        return 1;    // stack: [ ... constructor ]
    }
    
  • The function cb_load_module must push the module init function into the value stack, then use duk_call to call it and set module.exports. Do NOT call the module init function directly.

    duk_ret_t cb_load_module(duk_context *ctx)
    {
        // stack: [ resolved_id exports module ]
        ...
        duk_push_c_function(ctx, module_init, 0);
        duk_call(ctx, 0);
        duk_put_prop_string(ctx, 2, "exports");  // obj_idx(module) = 2
        return 0;  // no .js source for native modules
    }
    
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda