Add base for monkey patch in nodejs port.
'use strict';
const Path = require('path');
+const Module = require('module');
+/* Load MetaCall addon */
const addon = (() => {
const LIBRARY_PATH = process.env.LOADER_LIBRARY_PATH || '';
@@ -41,8 +43,57 @@ const addon = (() => {
return addon;
})();
-/* TODO: Override require and monkey patch the functions */
+/* Monkey patch require for simplifying load */
+Module.prototype.require = new Proxy(Module.prototype.require, {
+ apply(target, module, args) {
+ const node_require = () => {
+ return Reflect.apply(target, module, args);
+ };
+
+ const metacall_require = (tag, name) => {
+ addon.metacall_load_from_file(tag, [ name ]);
+ // TODO: Inspect the current handle and append it to an object mocking the function calls with metacall
+ return 1;
+ };
+
+ const loaders = {
+ py: metacall_require,
+ rb: metacall_require,
+ cs: metacall_require,
+ js: node_require,
+ node: node_require,
+ };
+
+ const loadersTag = {
+ py: 'py',
+ rb: 'rb',
+ cs: 'cs',
+ /*dll: 'cs',*/
+ };
+
+ const extensions = Object.keys(loaders);
+
+ const name = args[0];
+ const index = name.lastIndexOf('.');
+
+ if (index === -1) {
+ return node_require();
+ } else {
+ // Load the module
+ const ext = name.substr(index + 1);
+ const loader = loaders[ext];
+
+ if (loader) {
+ return loader(loadersTag[ext], name);
+ } else {
+ throw new Error(`Extension ${ext} is not suported by MetaCall.`);
+ }
+ }
+ }
+});
+
+/* Export the API */
module.exports = {
metacall: (name, ...args) => {
if (Object.prototype.toString.call(name) !== '[object String]') {
