|
24 | 24 | #include <metacall/metacall.h> |
25 | 25 | #include <cstring> |
26 | 26 |
|
| 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 | + |
27 | 48 | /* TODO: Remove this? */ |
28 | 49 | #define FUNCTION_NAME_LENGTH 50 |
29 | 50 | #define GENERAL_STRING_LENGTH 256 |
@@ -336,18 +357,48 @@ napi_value metacall_node_load_from_file(napi_env env, napi_callback_info info) |
336 | 357 |
|
337 | 358 | /* END-TODO */ |
338 | 359 |
|
| 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 | + |
339 | 387 | /* TODO: Review documentation */ |
340 | 388 | // This functions sets the necessary js functions that could be called in NodeJs |
341 | 389 | void metacall_node_exports(napi_env env, napi_value exports) |
342 | 390 | { |
343 | 391 | 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; |
346 | 395 |
|
347 | 396 | 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); |
349 | 399 | 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); |
351 | 402 | } |
352 | 403 |
|
353 | 404 | /* TODO: Review documentation */ |
|
0 commit comments