Skip to content

Commit d0aae06

Browse files
committed
Add metacall inspect to node port.
1 parent d60165b commit d0aae06

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

source/ports/node_port/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,16 @@ module.exports = {
104104

105105
return addon.metacall_load_from_file(tag, paths);
106106
},
107+
108+
metacall_inspect: () => {
109+
const json_data = addon.metacall_inspect();
110+
111+
if (json_data !== undefined) {
112+
const json = JSON.parse(json_data);
113+
114+
delete json['__metacall_host__'];
115+
116+
return json;
117+
}
118+
},
107119
};

source/ports/node_port/source/node_port.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@
2424
#include <metacall/metacall.h>
2525
#include <cstring>
2626

27+
#define napi_call(env, call) \
28+
do { \
29+
napi_status status = (call); \
30+
\
31+
if (status != napi_ok) \
32+
{ \
33+
const napi_extended_error_info * error_info = NULL; \
34+
bool is_pending; \
35+
napi_get_last_error_info((env), &error_info); \
36+
napi_is_exception_pending((env), &is_pending); \
37+
if (!is_pending) \
38+
{ \
39+
const char * message = (error_info->error_message == NULL) \
40+
? "empty error message" \
41+
: error_info->error_message; \
42+
napi_throw_error((env), NULL, message); \
43+
return NULL; \
44+
} \
45+
} \
46+
} while(0)
47+
2748
/* TODO: Remove this? */
2849
#define FUNCTION_NAME_LENGTH 50
2950
#define GENERAL_STRING_LENGTH 256
@@ -336,18 +357,48 @@ napi_value metacall_node_load_from_file(napi_env env, napi_callback_info info)
336357

337358
/* END-TODO */
338359

360+
/* TODO: Add documentation */
361+
napi_value metacall_node_inspect(napi_env env, napi_callback_info)
362+
{
363+
napi_value result;
364+
365+
size_t size = 0;
366+
367+
struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };
368+
369+
void * allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);
370+
371+
char * inspect_str = metacall_inspect(&size, allocator);
372+
373+
if (!(inspect_str != NULL && size != 0))
374+
{
375+
napi_throw_error(env, NULL, "Invalid MetaCall inspect string");
376+
}
377+
378+
napi_call(env, napi_create_string_utf8(env, inspect_str, size - 1, &result));
379+
380+
metacall_allocator_free(allocator, inspect_str);
381+
382+
metacall_allocator_destroy(allocator);
383+
384+
return result;
385+
}
386+
339387
/* TODO: Review documentation */
340388
// This functions sets the necessary js functions that could be called in NodeJs
341389
void metacall_node_exports(napi_env env, napi_value exports)
342390
{
343391
const char function_metacall_str[] = "metacall";
344-
const char function_metacall_load_file_str[] = "metacall_load_from_file";
345-
napi_value function_metacall_load_file, function_metacall;
392+
const char function_load_from_file_str[] = "metacall_load_from_file";
393+
const char function_inspect_str[] = "metacall_inspect";
394+
napi_value function_metacall, function_load_from_file, function_inspect;
346395

347396
napi_create_function(env, function_metacall_str, sizeof(function_metacall_str) - 1, metacall_node, NULL, &function_metacall);
348-
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);
397+
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);
398+
napi_create_function(env, function_inspect_str, sizeof(function_inspect_str) - 1, metacall_node_inspect, NULL, &function_inspect);
349399
napi_set_named_property(env, exports, function_metacall_str, function_metacall);
350-
napi_set_named_property(env, exports, function_metacall_load_file_str, function_metacall_load_file);
400+
napi_set_named_property(env, exports, function_load_from_file_str, function_load_from_file);
401+
napi_set_named_property(env, exports, function_inspect_str, function_inspect);
351402
}
352403

353404
/* TODO: Review documentation */

source/ports/node_port/test/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
const assert = require('assert');
44

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

77
describe('metacall', () => {
88
describe('require', () => {
99
it('functions metacall and metacall_load_from_file must be defined', () => {
1010
assert.notStrictEqual(metacall, undefined);
1111
assert.notStrictEqual(metacall_load_from_file, undefined);
12+
assert.notStrictEqual(metacall_inspect, undefined);
1213
});
1314
});
1415

@@ -24,6 +25,14 @@ describe('metacall', () => {
2425
});
2526
});
2627

28+
describe('inspect', () => {
29+
it('metacall_inspect', () => {
30+
const json = metacall_inspect();
31+
console.log(JSON.stringify(json));
32+
assert.notStrictEqual(json, undefined);
33+
});
34+
});
35+
2736
describe('call', () => {
2837
it('metacall (mock)', () => {
2938
assert.strictEqual(metacall('my_empty_func'), 1234);

0 commit comments

Comments
 (0)