node.js-nan https://www.e-learn.cn/tag/nodejs-nan zh-hans Invoking some callback function twice leads to Segmentation fault: Nan https://www.e-learn.cn/topic/2184296 <span>Invoking some callback function twice leads to Segmentation fault: Nan</span> <span><span lang="" about="/user/98" typeof="schema:Person" property="schema:name" datatype="">六眼飞鱼酱①</span></span> <span>2019-12-11 03:35:16</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am writing C++ addon using nbind - GitHub link for most thing and Nan - GitHub link for calling callbacks asynchronous. When I invoke callback only once, it works perfect. But When I invoke callback twice it gives <code>Segmentation fault (core dumped)</code>. Couldn't find error using <code>gdb</code>. Here is JS and C++ codes(compiling using <code>node-gyp configure build</code>):</p> <pre><code>//main.js code var nbind = require('nbind'); var lib = nbind.init().lib; lib.HeaderExample.callJS(function(a) { console.log("result" + a); }); lib.HeaderExample.startThread(); lib.HeaderExample.startThread(); </code></pre> <p>C++ addon's code </p> <pre><code>//c++ code class CallbackRunner : public Nan::AsyncWorker { public: CallbackRunner(Nan::Callback *callback) : AsyncWorker(callback) {} void Execute () {} void HandleOKCallback () { std::cout &lt;&lt; "running HandleOKCallback in thread " &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl; Nan::HandleScope scope; v8::Local&lt;v8::Value&gt; argv[] = { Nan::New&lt;v8::Number&gt;(10) }; callback-&gt;Call(1, argv); } }; class HeaderExample { public: static void callJS(nbind::cbFunction &amp;callback) { std::cout &lt;&lt; "running callJS in thread " &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl; m_onInitialisationStarted = new nbind::cbFunction(callback); Nan::Callback *callbackNan = new Nan::Callback(m_onInitialisationStarted-&gt;getJsFunction()); runner = new CallbackRunner(callbackNan); } static void startThread() { std::cout &lt;&lt; "it is here"; std::thread threadS(some); threadS.join(); } static void some() { std::cout &lt;&lt; "running some in thread: " &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl; if(runner){ AsyncQueueWorker(runner); } } inline static nbind::cbFunction *m_onInitialisationStarted = 0; inline static CallbackRunner *runner; }; </code></pre> <br /><h3>回答1:</h3><br /><p>Your class uses <code>AsyncQueueWorker</code> to invoke the <code>CallbackRunner</code>, but <code>AsyncQueueWorker</code> calls <code>AsyncExecuteComplete</code> after the callback is done, which in turn calls <code>worker-&gt;Destroy()</code>. See the <code>AsyncQueueWorker</code> code from <code>nan.h</code>:</p> <pre><code>inline void AsyncExecute (uv_work_t* req) { AsyncWorker *worker = static_cast&lt;AsyncWorker*&gt;(req-&gt;data); worker-&gt;Execute(); } inline void AsyncExecuteComplete (uv_work_t* req) { AsyncWorker* worker = static_cast&lt;AsyncWorker*&gt;(req-&gt;data); worker-&gt;WorkComplete(); worker-&gt;Destroy(); } inline void AsyncQueueWorker (AsyncWorker* worker) { uv_queue_work( uv_default_loop() , &amp;worker-&gt;request , AsyncExecute , reinterpret_cast&lt;uv_after_work_cb&gt;(AsyncExecuteComplete) ); } </code></pre> <p><code>worker-&gt;Destroy()</code> will delete the <code>CallbackRunner</code> class, along with the <code>Nan::Callback</code> that you fed to its constructor. That is the reason why you get a segmentation fault when attempting to call this callback a second time.</p> <p>You might be better off basing your class on <code>Nan::AsyncProgressQueueWorker</code> instead of <code>Nan::AsyncWorker</code>. <code>AsyncProgressQueueWorker</code> inherits <code>AsyncWorker</code> and it allows you to schedule work from the main thread just as <code>AsyncWorker</code> does, but it provides you with an <code>ExecutionProgress</code> class that allows you to use any thread to call back into the main thread any number of times while the original scheduled job is running.</p> <p><code>Nan::AsyncProgressQueueWorker</code> was added to <em>NAN</em> in version <code>2.8.0</code></p> <br /><br /><br /><h3>回答2:</h3><br /><p>You can't have two threads calling into the same V8 instance concurrently. You'll need careful locking to make sure that only one thread interacts with V8 at any point in time.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/45589254/invoking-some-callback-function-twice-leads-to-segmentation-fault-nan</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/nodejs" hreflang="zh-hans">node.js</a></div> <div class="field--item"><a href="/tag/v8" hreflang="zh-hans">v8</a></div> <div class="field--item"><a href="/tag/libuv" hreflang="zh-hans">libuv</a></div> <div class="field--item"><a href="/tag/nodejs-nan" hreflang="zh-hans">node.js-nan</a></div> </div> </div> Tue, 10 Dec 2019 19:35:16 +0000 六眼飞鱼酱① 2184296 at https://www.e-learn.cn Return a Nan::ObjectWrap from another Nan::ObjectWrap https://www.e-learn.cn/topic/1908202 <span>Return a Nan::ObjectWrap from another Nan::ObjectWrap</span> <span><span lang="" about="/user/75" typeof="schema:Person" property="schema:name" datatype="">六月ゝ 毕业季﹏</span></span> <span>2019-12-07 08:22:07</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have two subclasses of <code>Nan::ObjectWrap</code></p> <pre><code>class Zyre: public Nan::ObjectWrap {...} class ZyreEvent: public Nan::ObjectWrap {...} </code></pre> <p>How can I return a <code>ZyreEvent</code> javascript object from a method in <code>Zyre</code>?</p> <p>I have the following method, in which I create a <code>ZyreEvent</code>:</p> <pre><code>NAN_METHOD (Zyre::_recv) { Zyre *node = Nan::ObjectWrap::Unwrap &lt;Zyre&gt; (info.Holder ()); ZyreEvent *zyre_event = new ZyreEvent (node-&gt;self); info.GetReturnValue().Set(zyre_event-&gt;Wrap(info.This())); } </code></pre> <p>But I can't Wrap the zyre_event because <code>Wrap</code> is a protected member.</p> <br /><h3>回答1:</h3><br /><p>If I understand correctly, you want to return from (subclass of) <code>Nan::ObjectWrap</code>'s method instance of another (subclass of) <code>Nan::ObjectWrap</code>.</p> <p>Note: I'm not experienced so this may have faults or be wrong. I've put my sources in brackets where are examples how it's is done I guess.</p> <ol><li>Create <code>static NewInstance</code> method in a first class which receives pointer of itself (NewInstance)</li> <li>Use <code>v8::External</code> to wrap first class' C++ object and pass it as an argument for <code>New</code> with <code>argc</code> and <code>argv</code> to first class' constructor (using v8::External) (v8::External doc)</li> <li>Edit first class' <code>New</code> method and handle <code>info.Length() == 1 &amp;&amp; info[0]-&gt;IsExternal()</code> case which is basically copy constructor in this case (copying passed pointer)</li> <li>Use <code>...::NewInstance()</code> in second class to set return value</li> </ol><br /><br /><p>来源:<code>https://stackoverflow.com/questions/38794789/return-a-nanobjectwrap-from-another-nanobjectwrap</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/nodejs" hreflang="zh-hans">node.js</a></div> <div class="field--item"><a href="/tag/nodejs-addon" hreflang="zh-hans">node.js-addon</a></div> <div class="field--item"><a href="/tag/nodejs-nan" hreflang="zh-hans">node.js-nan</a></div> </div> </div> Sat, 07 Dec 2019 00:22:07 +0000 六月ゝ 毕业季﹏ 1908202 at https://www.e-learn.cn Error: Cannot find module 'nan' https://www.e-learn.cn/topic/1790585 <span>Error: Cannot find module &#039;nan&#039;</span> <span><span lang="" about="/user/163" typeof="schema:Person" property="schema:name" datatype="">荒凉一梦</span></span> <span>2019-12-06 05:01:31</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm working on native Node.js addon and following nan docs</p> <p>I included nan into binding.gyp like: <code>"include_dirs" : [ "&lt;!(node -e \"require('nan')\")" ]</code></p> <p>Also nan is in npm dependencies.</p> <p>But when I install the package inside the another node module node-gyp is failed with error</p> <pre><code>&gt; nnb@1.0.2 install /Users/Shopgate/sandbox/stress/node_modules/nnb &gt; node-gyp rebuild module.js:338 throw err; ^ Error: Cannot find module 'nan' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at [eval]:1:1 at Object.exports.runInThisContext (vm.js:74:17) at Object.&lt;anonymous&gt; ([eval]-wrapper:6:22) at Module._compile (module.js:460:26) at evalScript (node.js:431:25) at startup (node.js:90:7) gyp: Call to 'node -e "require('nan')"' returned exit status 1. while trying to load binding.gyp </code></pre> <br /><h3>回答1:</h3><br /><p>Not ideal but it works if you install nan first.</p> <pre><code>$ npm install nan </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>Install <code>nan</code> globally: </p> <p><code>npm i -g nan</code></p> <p>And then, </p> <p><code>export NODE_PATH=$(npm root -g)</code></p> <br /><br /><br /><h3>回答3:</h3><br /><h3>Unfortunately I had this issue, anything that had 'npm' in it was immediately returned with thrown error.</h3> <pre><code>module.js:471 throw err; ^ Error: Cannot find module 'number-is-nan' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object.&lt;anonymous&gt; (/usr/local/lib/node_modules/npm/node_modules/npmlog/node_modules/gauge/node_modules/string-width/node_modules/code-point-at/index.js:2:19) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) cd: no such file or directory: /npm </code></pre> <h3>I had to remove my node &amp; npm files</h3> <p>uninstall and reinstall instruction I followed were here (stackoverflow):</p> <p>How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)</p> <p>I then followed the rabbit trail all the way through every issue!</p> <p>Don't immediately jump to SO (stackoverflow) at the first sign of friction, the command window (cli) will "oddly enough" give you the right answers to everything (I did however hit about 2 hurdles I referred to SO for, mainly to be on the safe side).</p> <ul><li>When going through the install of node, npm, &amp; grunt via Install Node.js and npm with Homebrew. </li> </ul><p>-- I ran into some friction with a python framework file being in a place that made homebrew throw a warning resolve with.</p> <p>-- and a kegs link warning resolved with my own write up here.</p> <ul><li>Then when I needed to run: <code>brew install node</code></li> </ul><p>-- I had another <code>brew link issue</code>, easy enough, as my write-up above for kegs link warning would just need to be walked through again, so I thought. I then was getting an error saying I can not <code>brew link --overwrite node</code> that file (privileges or something if I recall, none-the-less resolved here).</p> <h3>Finally</h3> <ul><li><p>It was at this point everything was working well. -- Remember! there were simple steps inbetween that I literally just obeyed the command prompts in the cli, such as:</p></li> <li><p>brew wanting me to delete files via a given path:</p></li> </ul><p>-- <em>open finder&gt; [at top of MacBook "monitor tool bar"] Go&gt;go to folder...&gt;copy&amp;paste the path supplied by brew in cli window&gt;it will find that director/file then delete appropriate directory/file</em></p> <p>-- continue these steps until <code>brew update</code> is completely satisfied.</p> <ul><li><p>As I said this final part was just simple following of direction from brew responses directly.</p></li> <li><p>I hope this walkthrough helps someone bypass the issues I had a bit faster, and thank you to the community that spent their time helping me with solutions that I have linked to in this answer.</p></li> <li><p>don't cry over spilt code, keep supporting the community.</p></li> </ul><br /><br /><br /><h3>回答4:</h3><br /><p>I am little bit late for a party. But what worked for me was to re-install all <code>node_modules</code> using <code>rm -rf node_modules &amp;&amp; npm install</code></p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/34464673/error-cannot-find-module-nan</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/nodejs" hreflang="zh-hans">node.js</a></div> <div class="field--item"><a href="/tag/npm" hreflang="zh-hans">npm</a></div> <div class="field--item"><a href="/tag/node-gyp" hreflang="zh-hans">node-gyp</a></div> <div class="field--item"><a href="/tag/nodejs-addon" hreflang="zh-hans">node.js-addon</a></div> <div class="field--item"><a href="/tag/nodejs-nan" hreflang="zh-hans">node.js-nan</a></div> </div> </div> Thu, 05 Dec 2019 21:01:31 +0000 荒凉一梦 1790585 at https://www.e-learn.cn Return a Nan::ObjectWrap from another Nan::ObjectWrap https://www.e-learn.cn/topic/1728882 <span>Return a Nan::ObjectWrap from another Nan::ObjectWrap</span> <span><span lang="" about="/user/49" typeof="schema:Person" property="schema:name" datatype="">帅比萌擦擦*</span></span> <span>2019-12-05 17:10:25</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><div class="alert alert-danger" role="alert"> <p>I have two subclasses of <code>Nan::ObjectWrap</code></p> <pre><code>class Zyre: public Nan::ObjectWrap {...} class ZyreEvent: public Nan::ObjectWrap {...} </code></pre> <p>How can I return a <code>ZyreEvent</code> javascript object from a method in <code>Zyre</code>?</p> <p>I have the following method, in which I create a <code>ZyreEvent</code>:</p> <pre><code>NAN_METHOD (Zyre::_recv) { Zyre *node = Nan::ObjectWrap::Unwrap &lt;Zyre&gt; (info.Holder ()); ZyreEvent *zyre_event = new ZyreEvent (node-&gt;self); info.GetReturnValue().Set(zyre_event-&gt;Wrap(info.This())); } </code></pre> <p>But I can't Wrap the zyre_event because <code>Wrap</code> is a protected member.</p> </div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>If I understand correctly, you want to return from (subclass of) <code>Nan::ObjectWrap</code>'s method instance of another (subclass of) <code>Nan::ObjectWrap</code>.</p> <p>Note: I'm not experienced so this may have faults or be wrong. I've put my sources in brackets where are examples how it's is done I guess.</p> <ol><li>Create <code>static NewInstance</code> method in a first class which receives pointer of itself (<a href="https://github.com/tracelytics/node-traceview-bindings/blob/master/src/metadata.cc#L18" rel="nofollow">NewInstance</a>)</li> <li>Use <code>v8::External</code> to wrap first class' C++ object and pass it as an argument for <code>New</code> with <code>argc</code> and <code>argv</code> to first class' constructor (<a href="https://github.com/tracelytics/node-traceview-bindings/blob/master/src/metadata.cc#L22" rel="nofollow">using v8::External</a>) (<a href="http://bespin.cz/~ondras/html/classv8_1_1External.html" rel="nofollow">v8::External doc</a>)</li> <li>Edit first class' <code>New</code> method and handle <code>info.Length() == 1 &amp;&amp; info[0]-&gt;IsExternal()</code> case which is basically copy constructor in this case (<a href="https://github.com/tracelytics/node-traceview-bindings/blob/master/src/metadata.cc#L112" rel="nofollow">copying passed pointer</a>)</li> <li>Use <code>...::NewInstance()</code> in second class to set return value</li> </ol></div></div><div class="alert alert-warning" role="alert"><p>来源:<code>https://stackoverflow.com/questions/38794789/return-a-nanobjectwrap-from-another-nanobjectwrap</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-0" hreflang="zh-hans">c++</a></div> <div class="field--item"><a href="/tag/nodejs" hreflang="zh-hans">node.js</a></div> <div class="field--item"><a href="/tag/nodejs-addon" hreflang="zh-hans">node.js-addon</a></div> <div class="field--item"><a href="/tag/nodejs-nan" hreflang="zh-hans">node.js-nan</a></div> </div> </div> Thu, 05 Dec 2019 09:10:25 +0000 帅比萌擦擦* 1728882 at https://www.e-learn.cn Error: Cannot find module 'nan' https://www.e-learn.cn/topic/1564806 <span>Error: Cannot find module &#039;nan&#039;</span> <span><span lang="" about="/user/83" typeof="schema:Person" property="schema:name" datatype="">蓝咒</span></span> <span>2019-12-04 11:36:09</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><div class="alert alert-danger" role="alert"> <p>I'm working on native Node.js addon and following <a href="https://github.com/nodejs/nan#usage" rel="nofollow">nan docs</a></p> <p>I included nan into binding.gyp like: <code>"include_dirs" : [ "&lt;!(node -e \"require('nan')\")" ]</code></p> <p>Also nan is in npm dependencies.</p> <p>But when I install the package inside the another node module node-gyp is failed with error</p> <pre><code>&gt; nnb@1.0.2 install /Users/Shopgate/sandbox/stress/node_modules/nnb &gt; node-gyp rebuild module.js:338 throw err; ^ Error: Cannot find module 'nan' at Function.Module._resolveFilename (module.js:336:15) at Function.Module._load (module.js:278:25) at Module.require (module.js:365:17) at require (module.js:384:17) at [eval]:1:1 at Object.exports.runInThisContext (vm.js:74:17) at Object.&lt;anonymous&gt; ([eval]-wrapper:6:22) at Module._compile (module.js:460:26) at evalScript (node.js:431:25) at startup (node.js:90:7) gyp: Call to 'node -e "require('nan')"' returned exit status 1. while trying to load binding.gyp </code></pre> </div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>Not ideal but it works if you install nan first.</p> <pre><code>$ npm install nan </code></pre> </div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>Install <code>nan</code> globally: </p> <p><code>npm i -g nan</code></p> <p>And then, </p> <p><code>export NODE_PATH=$(npm root -g)</code></p> </div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <h3>Unfortunately I had this issue, anything that had 'npm' in it was immediately returned with thrown error.</h3> <pre><code>module.js:471 throw err; ^ Error: Cannot find module 'number-is-nan' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object.&lt;anonymous&gt; (/usr/local/lib/node_modules/npm/node_modules/npmlog/node_modules/gauge/node_modules/string-width/node_modules/code-point-at/index.js:2:19) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) cd: no such file or directory: /npm </code></pre> <h3>I had to remove my node &amp; npm files</h3> <p>uninstall and reinstall instruction I followed were here (stackoverflow):</p> <p><a href="https://stackoverflow.com/a/11178106/6627119" rel="nofollow">How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)</a></p> <p>I then followed the rabbit trail all the way through every issue!</p> <p>Don't immediately jump to SO (stackoverflow) at the first sign of friction, the command window (cli) will "oddly enough" give you the right answers to everything (I did however hit about 2 hurdles I referred to SO for, mainly to be on the safe side).</p> <ul><li>When going through the install of node, npm, &amp; grunt via <a href="https://changelog.com/posts/install-node-js-with-homebrew-on-os-x" rel="nofollow">Install Node.js and npm with Homebrew</a>. </li> </ul><p>-- I ran into some friction with a python framework file being in a place that made homebrew throw a warning <a href="https://stackoverflow.com/a/22265200/6627119" rel="nofollow">resolve with</a>.</p> <p>-- and a kegs link warning <a href="https://stackoverflow.com/a/47253541/6627119" rel="nofollow">resolved with my own write up here</a>.</p> <ul><li>Then when I needed to run: <code>brew install node</code></li> </ul><p>-- I had another <code>brew link issue</code>, easy enough, as my write-up above for kegs link warning would just need to be walked through again, so I thought. I then was getting an error saying I can not <code>brew link --overwrite node</code> that file (privileges or something if I recall, none-the-less <a href="https://stackoverflow.com/a/33904961/6627119" rel="nofollow">resolved here</a>).</p> <h3>Finally</h3> <ul><li><p>It was at this point everything was working well. -- Remember! there were simple steps inbetween that I literally just obeyed the command prompts in the cli, such as:</p></li> <li><p>brew wanting me to delete files via a given path:</p></li> </ul><p>-- <em>open finder&gt; [at top of MacBook "monitor tool bar"] Go&gt;go to folder...&gt;copy&amp;paste the path supplied by brew in cli window&gt;it will find that director/file then delete appropriate directory/file</em></p> <p>-- continue these steps until <code>brew update</code> is completely satisfied.</p> <ul><li><p>As I said this final part was just simple following of direction from brew responses directly.</p></li> <li><p>I hope this walkthrough helps someone bypass the issues I had a bit faster, and thank you to the community that spent their time helping me with solutions that I have linked to in this answer.</p></li> <li><p>don't cry over spilt code, keep supporting the community.</p></li> </ul></div></div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>I am little bit late for a party. But what worked for me was to re-install all <code>node_modules</code> using <code>rm -rf node_modules &amp;&amp; npm install</code></p> </div></div><div class="alert alert-warning" role="alert"><p>来源:<code>https://stackoverflow.com/questions/34464673/error-cannot-find-module-nan</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/nodejs" hreflang="zh-hans">node.js</a></div> <div class="field--item"><a href="/tag/npm" hreflang="zh-hans">npm</a></div> <div class="field--item"><a href="/tag/node-gyp" hreflang="zh-hans">node-gyp</a></div> <div class="field--item"><a href="/tag/nodejs-addon" hreflang="zh-hans">node.js-addon</a></div> <div class="field--item"><a href="/tag/nodejs-nan" hreflang="zh-hans">node.js-nan</a></div> </div> </div> Wed, 04 Dec 2019 03:36:09 +0000 蓝咒 1564806 at https://www.e-learn.cn