diff --git a/source/loaders/node_loader/source/node_loader_impl.cpp b/source/loaders/node_loader/source/node_loader_impl.cpp index aab61531e..b583a9330 100644 --- a/source/loaders/node_loader/source/node_loader_impl.cpp +++ b/source/loaders/node_loader/source/node_loader_impl.cpp @@ -1100,7 +1100,7 @@ napi_value node_loader_impl_value_to_napi(loader_impl_node node_impl, napi_env e else if (id == TYPE_CLASS) { /* TODO */ - napi_throw_error(env, NULL, "NodeJS Loader class is not implemented"); + /* napi_throw_error(env, NULL, "NodeJS Loader class is not implemented"); */ /* klass cls = value_to_class(arg_value); diff --git a/source/ports/node_port/index.js b/source/ports/node_port/index.js index 582354b99..15e610303 100644 --- a/source/ports/node_port/index.js +++ b/source/ports/node_port/index.js @@ -228,22 +228,22 @@ mod.prototype.require = function (name) { const index = name.lastIndexOf('.'); if (index !== -1) { - /* If there is extension, load the module depending on the tag */ - const extension = name.substr(index + 1); - const tag = tags[extension]; + /* If there is extension, load the module depending on the tag */ + const extension = name.substr(index + 1); + const tag = tags[extension]; - if (tag && tag !== 'node') { - /* Load with MetaCall if we found a tag and it is not NodeJS */ - return metacall_require(tag, name); - } + if (tag && tag !== 'node') { + /* Load with MetaCall if we found a tag and it is not NodeJS */ + return metacall_require(tag, name); + } } try { - return node_require.apply(this, [ name ]); + return node_require.apply(this, [ name ]); } catch (e) { - if (e.code !== 'MODULE_NOT_FOUND') { - throw e; - } + if (e.code !== 'MODULE_NOT_FOUND') { + throw e; + } } /* If there is no extension or the extension is not supported or it is 'node', load it with NodeJS require */ diff --git a/source/ports/node_port/test/index.js b/source/ports/node_port/test/index.js index 9dc7ac006..6b245dabe 100644 --- a/source/ports/node_port/test/index.js +++ b/source/ports/node_port/test/index.js @@ -123,16 +123,33 @@ describe('metacall', () => { assert.strictEqual(asd.mixed_args('a', 3, 4, 3.4, 'NOT IMPLEMENTED'), 65); }); it('require (py)', () => { - // TODO: Both methods work, should we disable the commented out style to be NodeJS compilant? - // const example = require('example.py'); - const example = require('./example.py'); - assert.notStrictEqual(example, undefined); - assert.strictEqual(example.multiply(2, 2), 4); - assert.strictEqual(example.divide(4.0, 2.0), 2.0); - assert.strictEqual(example.sum(2, 2), 4); - assert.strictEqual(example.strcat('2', '2'), '22'); - assert.deepStrictEqual(example.return_array(), [1, 2, 3]); - assert.deepStrictEqual(example.return_same_array([1, 2, 3]), [1, 2, 3]); + const verify = (example) => { + assert.notStrictEqual(example, undefined); + assert.strictEqual(example.multiply(2, 2), 4); + assert.strictEqual(example.divide(4.0, 2.0), 2.0); + assert.strictEqual(example.sum(2, 2), 4); + assert.strictEqual(example.strcat('2', '2'), '22'); + assert.deepStrictEqual(example.return_array(), [1, 2, 3]); + assert.deepStrictEqual(example.return_same_array([1, 2, 3]), [1, 2, 3]); + }; + + verify(require('./example.py')); + + // TODO: Should we enable this format? I think it should work right now but it does not, we must review it + // verify(require('example.py')); + }); + it('require (py class)', () => { + const classname = require('./classname.py'); + assert.notStrictEqual(classname.function_returns_object_new_local_variable, undefined); + assert.notStrictEqual(classname.return_bound_method_param, undefined); + assert.notStrictEqual(classname.return_object_function, undefined); + assert.notStrictEqual(classname.return_itself, undefined); + assert.notStrictEqual(classname.return_object_bound_method_call, undefined); + assert.notStrictEqual(classname.return_class_function, undefined); + assert.notStrictEqual(classname.return_object_bound_method_new_object, undefined); + + // TODO: Implement classes + // assert.notStrictEqual(classname.MyClass, undefined); }); it('require (py module)', () => { // This code loads directly a module without extension from Python @@ -145,11 +162,9 @@ describe('metacall', () => { const { find_library } = require('ctypes.util'); assert.notStrictEqual(find_library, undefined); - // TODO: This fails because the submodule imports a class, which - // is not yet supported by the NodeJS loader - //const { py_encode_basestring_ascii } = require('json.encoder'); - //assert.notStrictEqual(py_encode_basestring_ascii, undefined); - //assert.strictEqual(py_encode_basestring_ascii('asd'), '"asd"'); + const { py_encode_basestring_ascii } = require('json.encoder'); + assert.notStrictEqual(py_encode_basestring_ascii, undefined); + assert.strictEqual(py_encode_basestring_ascii('asd'), '"asd"'); }); it('require (rb)', () => { // TODO: Both methods work, should we disable the commented out style to be NodeJS compilant?