<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Hello Luca,</div><div class=""><br class=""></div><div class="">I would not give you a straight answer about NIF but I know for sure it is not enough just to load Erlang module in cases of lists. You need to load native library as well.</div><div class=""><br class=""></div><div class="">Typically NIF module has an association with library <a href="http://erlang.org/doc/tutorial/nif.html" class="">http://erlang.org/doc/tutorial/nif.html</a>.</div><div class="">Please note that lists is BIF module, you might not be able to re-bind it with other lib.</div><div class=""><br class=""></div><div class="">I would suggest you to study meck implementation. It deals a lot with module re-loading</div><div class=""><a href="https://github.com/eproxus/meck" class="">https://github.com/eproxus/meck</a></div><div class=""><a href="https://github.com/eproxus/meck/blob/master/src/meck_code.erl" class="">https://github.com/eproxus/meck/blob/master/src/meck_code.erl</a></div><div class=""><br class=""></div><div class="">Best Regards,</div><div class="">Dmitry</div><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 29 Jul 2019, at 11.28, Luca Tansini <<a href="mailto:luca.tansini95@gmail.com" class="">luca.tansini95@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">I’m trying to load into the VM a copy of a module under a different name.</div><div class="">The way I’m trying to do it is:</div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span></div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>- get the binary of the module with code:get_object_code(mod_name)</div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>- replace the module name inside the binary (otherwise the code loader won’t accept it)</div><div class=""><span class="Apple-tab-span" style="white-space:pre"> </span>- reload it with code:load_binary(new_name, “”, RenamedBinary)</div><div class=""><br class=""></div><div class="">here is some sample code showing the approach: <a href="https://pastebin.com/HpdUR0eU" class="">https://pastebin.com/HpdUR0eU</a>.</div><div class=""><br class=""></div><div class="">This approach seems to work as long as the original module doesn’t contain any NIFs.</div><div class="">In that case the module is still loaded (not always, I’ve found at least one exception, more below), but then gives runtime errors when NIFs are trying to be run.</div><div class=""><br class=""></div><div class="">For instance this is what happens with the lists module:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">>> module_cp:load_module_copy(lists).</div></div><div class=""><div class="">{module,xists}</div></div><div class="">>> xists:seq(1,10).</div><div class="">[1,2,3,4,5,6,7,8,9,10]</div><div class=""><div class="">>> xists:reverse([1,2,3,4]).</div></div><div class=""><div class="">** exception error: undefined function erlang:nif_error/1</div></div><div class=""><div class=""> in function xists:reverse/2 (xists.erl, line 95)</div></div></blockquote><div class=""><br class=""></div><div class="">For the erlang module instead, even the code loading fails:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">>> module_cp:load_module_copy(erlang).</div></div><div class=""><div class="">{error,badfile}</div></div><div class=""><div class=""><br class=""></div></div><div class=""><div class="">=ERROR REPORT==== 26-Jul-2019::13:10:11 ===</div></div><div class=""><div class="">Loading of failed: badfile</div></div><div class=""><div class=""><br class=""></div></div><div class=""><div class="">=ERROR REPORT==== 26-Jul-2019::13:10:11 ===</div></div><div class=""><div class="">beam/beam_load.c(2284): Error loading function xrlang:garbage_collect/2: op bif0 u x:</div></div><div class=""><div class=""> no specific operation found</div></div><div class=""><br class=""></div></blockquote><br class=""><div class="">I’m not very familiar with how NIFs work and are loaded, but is there a workaround for this?</div><div class="">Maybe renaming only the module attribute is not enough.</div><div class="">I’m on MacOS running Erlang/OTP 20 [erts-9.3].</div><div class=""><br class=""></div><div class="">Thank you</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div>_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></blockquote></div><br class=""></body></html>