Skip to content

Commit

Permalink
Implement preload of metacall package for node loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Jan 20, 2021
1 parent 8d99bcb commit fc86e09
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions source/loaders/node_loader/bootstrap/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ const util = require('util');

const cherow = require('./node_modules/cherow');

// eslint-disable-next-line no-empty-function
function node_loader_trampoline_initialize() {
// Nothing to initialize yet
try {
// Preload MetaCall Monkey Patch
require('metacall');
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
// console.log('NodeJS Warning: MetaCall could not be preloaded');
} else {
console.log(`NodeJS Error (while preloading MetaCall): ${e.message}`);
}
}
}

function node_loader_trampoline_is_callable(value) {
Expand Down
12 changes: 10 additions & 2 deletions source/ports/node_port/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ mod.prototype.require = function (name) {
/* Probably in the future we can differenciate between them, but it is not trivial */
};

/* Try to load it with NodeJS first */
try {
return node_require.apply(this, [ name ]);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

const index = name.lastIndexOf('.');

if (index !== -1) {
Expand Down Expand Up @@ -204,8 +213,7 @@ mod.prototype.require = function (name) {
};

/* Debug logs */
if (process.env['NODE_ENV'] === 'debug' && addon !== undefined)
{
if (process.env['NODE_ENV'] === 'debug' && addon !== undefined) {
addon.metacall_logs();
}

Expand Down

0 comments on commit fc86e09

Please sign in to comment.