Skip to content

Commit

Permalink
Mitigated issue #132
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed May 10, 2021
1 parent 8a3e90a commit 1bc80ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion source/loaders/node_loader/source/node_loader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions source/ports/node_port/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
45 changes: 30 additions & 15 deletions source/ports/node_port/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down

0 comments on commit 1bc80ba

Please sign in to comment.