Repository navigation
Description
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a feature request that matches the one I want to file, without success.
Electron Version
13.1.4
What operating system are you using?
Windows
Operating System Version
Windows 10 Home (19042)
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
The native module in the testcase gist should be able to link.
The problematic part is the function v8::ArrayBuffer::GetBackingStore()
Actual Behavior
Linking fails with the error
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::shared_ptr<class v8::BackingStore> __cd ecl v8::ArrayBuffer::GetBackingStore(void)" (__imp_?GetBackingStore@ArrayBuffer@v8@@QEAA?AV?$shared_ptr@VBackingStore@v8@@@std@@XZ) re ferenced in function "void __cdecl foobar(class v8::FunctionCallbackInfo<class v8::Value> const &)" (?foobar@@YAXAEBV?$FunctionCallbac kInfo@VValue@v8@@@v8@@@Z) [c:\Work\repro_bug_arraybuffer\build\foobar.vcxproj]
Testcase Gist URL
Additional Information
Building the same module for node 14.17.1 works fine.
The error message, although cryptic, is correct: the header and link library shipped do not seem to match. the header declares GetBackingStore like this:
std::shared_ptr<BackingStore> GetBackingStore();
but the library exports this:
?GetBackingStore@ArrayBuffer@v8@@QEAA?AV?$shared_ptr@VBackingStore@v8@@@__1@std@@XZ
which demangles to
public: class std::__1::shared_ptr<class v8::BackingStore> __cdecl v8::ArrayBuffer::GetBackingStore(void) __ptr64
so as far as I understand it the namespace is different and thus the linker doesn't find the function.
node.exe version 14.17.1 otoh exports
?GetBackingStore@ArrayBuffer@v8@@QEAA?AV?$shared_ptr@VBackingStore@v8@@@std@@XZ
aka
public: class std::shared_ptr<class v8::BackingStore> __cdecl v8::ArrayBuffer::GetBackingStore(void) __ptr64
which matches the declaration in v8.h exactly.
With prior versions of electron (up to 12 I think) we could use the deprecated function GetContent to get at the content of an ArrayBuffer but that function has been removed so until this is fixed, if I'm not completely mistaken, any native module that requires access to the content of an ArrayBuffer, including helper libraries like nan or nbind and all modules that depend on those will be broken.
Activity
miniak commentedon Jun 25, 2021
I ran into the same issue. I ended up changing my API to use node::Buffer instead.
Bufferisinstanceof Uint8Arrayminiak commentedon Jun 25, 2021
https://github.com/nodejs/node/blob/master/src/node_buffer.h
MarshallOfSound commentedon Jun 25, 2021
This is a deliberate change that I think we're still in the process of fully documenting / supporting.
Basically for this one API you have to build against libc++ rather than whatever standard library you happen to be using for your native module. There's a long story for why this is the case but it comes down to standard library's not actually being standard.
See electron/rebuild#703 for a draft of supporting this in electron-rebuild.
nornagon commentedon Jun 28, 2021
Just a note: I'd recommend that all native modules use N-API and avoid nan, for this kind of reason. C++ ABIs are very difficult to stabilize. N-API is explicitly designed as a stable ABI target. https://nodejs.org/api/n-api.html
TanninOne commentedon Jun 29, 2021
@miniak Thank you for the suggestion, unfortunately this particular use of ArrayBuffer is in a foreign library (nbind) which I'd rather not get too deep into
@MarshallOfSound that makes sense, I found a bit after I posted this that the __1 namespace is appended to prevent accidentally linking to the wrong std implementation. Still weird that it works linking against node.
Also: I'm working with windows and msvc, I don't expect I can just use libc++ instead of the shipped standard libraries?
Is the solution here to switch to clang?
@nornagon I'm definitively moving my modules to N-API piece by piece but it takes time as I can't invest too much time into it.
MarshallOfSound commentedon Jun 29, 2021
Yep, using the same clang + std library that Electron uses to build is the safest way to build native modules that interact with the std library.
lygstate commentedon Aug 27, 2021
How to fixes this?
kjvalencik commentedon Aug 31, 2021
@miniak Thanks for sharing that
Bufferworks. As a workaround (at a performance cost), anifdefcould be used when compiling for Windows and Electron to first construct a Buffer from theArrayBufferand then usenode::Buffer::Datato get the contents.Is there a
#defthat can be used for checking the Electron version?Edit: It looks like
ELECTRON_MAJOR_VERSIONcould be used.kjvalencik commentedon Aug 31, 2021
I ended up adding the following to my
node-gypconfig:And using the follow
#ifNot the most elegant or low cost solution, but hopefully that works as a stop gap while my users migrate to Node-API.
Is there a
#defthat can be used to tell if the module is being built for electron? My solution was a bit convoluted.somanuell commentedon Sep 14, 2021
I don't understand that code. What is Neon_Buffer_Data? Where does go the node::Buffer::New result?
kjvalencik commentedon Sep 14, 2021
@somanuell That's a sample of the neon project source.
Neon_Buffer_Datais a very small helper that gets the contents of the buffer:The result of
node::Buffer::Newgoes intolocaland thennode::Buffer::Datacan be used to get the buffer data out.mmomtchev commentedon Mar 11, 2022
@VerteDinde, I find the decision to build Electron on Windows using a C++ runtime that it just out of beta instead of the MS C++ runtime highly unusual - but there is probably a good reason for it. But in this case, I suggest that you also include this runtime along the
node.libDLL import library so that native modules can link with it. Currently, if I want to port my native Node.js addon to Electron I have only two choices and both are horrible:std::shared_ptras some people in this thread didlibc++for WindowsAll of this renders the port of a native Node.js addon to Electron a huge undertaking and completely destroys the dream of simply launching
electron-rebuildand going for a cup of coffee.mmomtchev commentedon Mar 11, 2022
@VerteDinde Or as a plan B, I can suggest you a very ugly kludge that gets the job done with almost no modifications:
Patch
v8/src/api/api.ccto include a second form ofGetBackingStorethat returns a raw pointer then makeNANuse this second form. You don't need ashared_ptrfor the use thatNANmakes of it inTypedArrayContents. It is horrible, but it will allow you get working a ton of native Node.js addons while changing two lines of code.github-actions commentedon Oct 5, 2022
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
github-actions commentedon Nov 7, 2022
This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.
dogeFu commentedon Nov 7, 2022
I use node::Buffer::new api to replace v8 api to solve this problem.But it's not easy when you have third library that use those api.Hope there will be more easy way to do it.
lygstate commentedon Nov 7, 2022
A possible solution is just diable this v8 api and forcing using to always using node::Buffer::new instead, or
mock this v8 api to use node::Buffer::new instead in released v8 header, so the issue will be gone
GitMurf commentedon Nov 24, 2022
@dogeFu / @lygstate could you please guide me in how I could replace to use
node::Buffer::newinstead of v8 api? I have been trying to figure this out for weeks! Thank you so much!GitMurf commentedon Nov 24, 2022
@mmomtchev could you explain this a little further on how I could do this? I am simply trying to get node-canvas to work with Electron and having issues with NAN and
GetBackingStore. Thanks so much!dogeFu commentedon Nov 25, 2022
@GitMurf for example, replace v8::ArrayBuffer::GetBackingStore()->Data() with node::Buffer::Data(). Just Replace API that has link error when you build project.
GitMurf commentedon Nov 25, 2022
@dogeFu thank you so much for the quick reply! I couldn't get what you suggested to work but I am very very inexperienced at trying to mess with dependencies (especially native ones as I am not a c++ guy). The following code is the one giving me the problem and I am not sure how to update this in
nanaccordingly. The culprit is:data = static_cast<char*>(buffer->GetBackingStore()->Data()) + byte_offset;Thanks so much for your help!