Search code examples
duktape

duktape, modify variable argument in native C code


I'm trying to modify a variable passed in an argument in a native function like this:

var MyVar = 'foo';
myNativeFunc(MyVar);

and inside my native, I can read the content of MyVar, with :

std::string(duk_to_string(ctx, 0));

but, I need to modify the value of this variable inside native function. what is the best way for does it? (I can't use the return statement) thanks.


Solution

  • That's not possible as it would implement pass-by-reference (which is not supported in JS). Read also the answer to the question Pass Variables by Reference in Javascript

    So, either pass in an object reference and change the object properties or use a return value.