calling C++ from javascript: how to modify an input argument?

42 views
Skip to first unread message

SimonHF

unread,
Apr 22, 2014, 1:08:44 PM4/22/14
to nod...@googlegroups.com
For example, I can get a uint like this in a C++ function: uint32_t myuint32 = args[0]->Int32Value();

But is it also possible to change the value somehow from C++ land, so that in javascript the variable passed into the function will reflect the changed value?

If this is possible with some C++ argument types and not others, then which types allow modification?

Thanks.

mscdex

unread,
Apr 22, 2014, 2:28:59 PM4/22/14
to nod...@googlegroups.com
On Tuesday, April 22, 2014 1:08:44 PM UTC-4, SimonHF wrote:
But is it also possible to change the value somehow from C++ land, so that in javascript the variable passed into the function will reflect the changed value?

No.

SimonHF

unread,
Apr 22, 2014, 3:51:11 PM4/22/14
to nod...@googlegroups.com
For example, so far the closest I have got is finding String::MakeExternal [1] which appears to modify an existing external string, correct?

And I have a suspicion that it's possible to use external memory with node buffer. Is this true? And is there example code for this?

I'm trying to figure out ways to do zero copy between C++ and node / V8.

[1] http://izs.me/v8-docs/classv8_1_1String.html#a6419e6b87e73bf03e326dd862fdca495 

Floby

unread,
Apr 23, 2014, 4:40:17 AM4/23/14
to nod...@googlegroups.com
In javascript, scalar values (number, string, boolean and null) are immutable. It means you can't modify the value of a variable, you can only reassign another value to a variable (there's a slight difference).
The point is, the v8 tries very hard to keep these immutable as well.

Furthermore, zero-copy can't be done with Javascript variables because they live in the v8-managed memory. The closest thing you can do is to use Buffers in node. Buffers are small JS Objects referring to an allocated memory space outside of the V8-managed memory.

If you're gonna do funky stuff with memory, do it with buffers.
Reply all
Reply to author
Forward
0 new messages