Skip to content

Commit

Permalink
Add metacall inspect to node port.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Dec 2, 2019
1 parent d60165b commit d0aae06
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
12 changes: 12 additions & 0 deletions source/ports/node_port/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,16 @@ module.exports = {

return addon.metacall_load_from_file(tag, paths);
},

metacall_inspect: () => {
const json_data = addon.metacall_inspect();

if (json_data !== undefined) {
const json = JSON.parse(json_data);

delete json['__metacall_host__'];

return json;
}
},
};
59 changes: 55 additions & 4 deletions source/ports/node_port/source/node_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@
#include <metacall/metacall.h>
#include <cstring>

#define napi_call(env, call) \
do { \
napi_status status = (call); \
\
if (status != napi_ok) \
{ \
const napi_extended_error_info * error_info = NULL; \
bool is_pending; \
napi_get_last_error_info((env), &error_info); \
napi_is_exception_pending((env), &is_pending); \
if (!is_pending) \
{ \
const char * message = (error_info->error_message == NULL) \
? "empty error message" \
: error_info->error_message; \
napi_throw_error((env), NULL, message); \
return NULL; \
} \
} \
} while(0)

/* TODO: Remove this? */
#define FUNCTION_NAME_LENGTH 50
#define GENERAL_STRING_LENGTH 256
Expand Down Expand Up @@ -336,18 +357,48 @@ napi_value metacall_node_load_from_file(napi_env env, napi_callback_info info)

/* END-TODO */

/* TODO: Add documentation */
napi_value metacall_node_inspect(napi_env env, napi_callback_info)
{
napi_value result;

size_t size = 0;

struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };

void * allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);

char * inspect_str = metacall_inspect(&size, allocator);

if (!(inspect_str != NULL && size != 0))
{
napi_throw_error(env, NULL, "Invalid MetaCall inspect string");
}

napi_call(env, napi_create_string_utf8(env, inspect_str, size - 1, &result));

metacall_allocator_free(allocator, inspect_str);

metacall_allocator_destroy(allocator);

return result;
}

/* TODO: Review documentation */
// This functions sets the necessary js functions that could be called in NodeJs
void metacall_node_exports(napi_env env, napi_value exports)
{
const char function_metacall_str[] = "metacall";
const char function_metacall_load_file_str[] = "metacall_load_from_file";
napi_value function_metacall_load_file, function_metacall;
const char function_load_from_file_str[] = "metacall_load_from_file";
const char function_inspect_str[] = "metacall_inspect";
napi_value function_metacall, function_load_from_file, function_inspect;

napi_create_function(env, function_metacall_str, sizeof(function_metacall_str) - 1, metacall_node, NULL, &function_metacall);
napi_create_function(env, function_metacall_load_file_str, sizeof(function_metacall_load_file_str) - 1, metacall_node_load_from_file, NULL, &function_metacall_load_file);
napi_create_function(env, function_load_from_file_str, sizeof(function_load_from_file_str) - 1, metacall_node_load_from_file, NULL, &function_load_from_file);
napi_create_function(env, function_inspect_str, sizeof(function_inspect_str) - 1, metacall_node_inspect, NULL, &function_inspect);
napi_set_named_property(env, exports, function_metacall_str, function_metacall);
napi_set_named_property(env, exports, function_metacall_load_file_str, function_metacall_load_file);
napi_set_named_property(env, exports, function_load_from_file_str, function_load_from_file);
napi_set_named_property(env, exports, function_inspect_str, function_inspect);
}

/* TODO: Review documentation */
Expand Down
11 changes: 10 additions & 1 deletion source/ports/node_port/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const assert = require('assert');

const { metacall, metacall_load_from_file } = require('../index.js');
const { metacall, metacall_load_from_file, metacall_inspect } = require('../index.js');

describe('metacall', () => {
describe('require', () => {
it('functions metacall and metacall_load_from_file must be defined', () => {
assert.notStrictEqual(metacall, undefined);
assert.notStrictEqual(metacall_load_from_file, undefined);
assert.notStrictEqual(metacall_inspect, undefined);
});
});

Expand All @@ -24,6 +25,14 @@ describe('metacall', () => {
});
});

describe('inspect', () => {
it('metacall_inspect', () => {
const json = metacall_inspect();
console.log(JSON.stringify(json));
assert.notStrictEqual(json, undefined);
});
});

describe('call', () => {
it('metacall (mock)', () => {
assert.strictEqual(metacall('my_empty_func'), 1234);
Expand Down

0 comments on commit d0aae06

Please sign in to comment.