Skip to content

Commit c25e6ed

Browse files
committed
Trying to solve more issues from nodejs.
1 parent 85ae3de commit c25e6ed

File tree

4 files changed

+76
-51
lines changed

4 files changed

+76
-51
lines changed

.github/workflows/docker-hub.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
push:
99
branches:
1010
- master
11+
- develop
1112
tags:
1213
- 'v*.*.*'
1314

@@ -44,9 +45,6 @@ jobs:
4445
bash ./docker-compose.sh push
4546
elif [[ "${{ contains(github.ref, 'refs/tags/') }}" = true ]]; then
4647
bash ./docker-compose.sh version
47-
else
48-
echo "Failed to push the docker images"
49-
exit 1
5048
fi
5149
5250
- name: Logout from DockerHub

source/loaders/node_loader/bootstrap/lib/bootstrap.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ const path = require('path');
66
const util = require('util');
77
const fs = require('fs');
88

9-
/* Require the JavaScript parser */
9+
// Require the JavaScript parser
1010
const espree = require(path.join(__dirname, 'node_modules', 'espree'));
1111

1212
const node_require = Module.prototype.require;
1313
const node_resolve = require.resolve;
1414
const node_cache = require.cache;
1515

16-
/* Store in the module prototype the original functions for future use in derived loaders like TypeScript */
16+
// Store in the module prototype the original functions for future use in derived loaders like TypeScript
1717
Module.prototype.node_require = node_require;
1818
Module.prototype.node_resolve = node_resolve;
1919
Module.prototype.node_cache = node_cache;
2020

2121
function node_loader_trampoline_initialize(loader_library_path) {
22+
// Restore the argv (this is used for tricking node::Start method)
23+
process.argv = [ process.argv[0] ];
24+
2225
// Add current execution directory to the execution paths
2326
node_loader_trampoline_execution_path(process.cwd());
2427

2528
const paths = [
26-
/* Local version of MetaCall NodeJS Port */
29+
// Local version of MetaCall NodeJS Port
2730
'metacall',
28-
/* Optionally, use loader library path for global installed NodeJS Port */
31+
// Optionally, use loader library path for global installed NodeJS Port
2932
...loader_library_path ? [ path.join(loader_library_path, 'node_modules', 'metacall', 'index.js') ] : [],
3033
];
3134

@@ -333,7 +336,7 @@ function node_loader_trampoline_discover(handle) {
333336
}
334337

335338
function node_loader_trampoline_test(obj) {
336-
/* Imporant: never trigger an async resource in this function */
339+
// Imporant: never trigger an async resource in this function
337340
if (obj !== undefined) {
338341
fs.writeSync(process.stdout.fd, `${util.inspect(obj, false, null, true)}\n`);
339342
}
@@ -411,7 +414,7 @@ module.exports = ((impl, ptr) => {
411414
throw new Error('Process arguments (process.argv[2], process.argv[3]) not defined.');
412415
}
413416

414-
/* Get trampoline from list of linked bindings */
417+
// Get trampoline from list of linked bindings
415418
const trampoline = process._linkedBinding('node_loader_trampoline_module');
416419

417420
const node_loader_ptr = trampoline.register(impl, ptr, {

source/loaders/node_loader/source/node_loader_impl.cpp

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,15 @@ extern char **environ;
9999
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
100100
#endif
101101

102-
#include <node.h>
102+
/* NodeJS Includes */
103103
#include <node_api.h>
104104

105+
#ifdef NAPI_VERSION
106+
#undef NAPI_VERSION
107+
#endif
108+
109+
#include <node.h>
110+
105111
#include <v8.h> /* version: 6.2.414.50 */
106112

107113
#ifdef ENABLE_DEBUGGER_SUPPORT
@@ -650,7 +656,7 @@ struct loader_impl_async_handle_promise_safe_type
650656
loader_impl_async_handle_promise_safe_type(loader_impl_node node_impl, napi_env env) :
651657
node_impl(node_impl), env(env), result(NULL) {}
652658

653-
~loader_impl_async_handle_promise_safe_type()
659+
void destroy()
654660
{
655661
threadsafe_async.close([](uv_handle_t *handle) {
656662
loader_impl_async_handle_promise_safe_type *handle_promise_safe = static_cast<loader_impl_async_handle_promise_safe_type *>(handle->data);
@@ -659,6 +665,8 @@ struct loader_impl_async_handle_promise_safe_type
659665
{
660666
metacall_value_destroy(handle_promise_safe->result);
661667
}
668+
669+
delete handle_promise_safe;
662670
});
663671
}
664672
};
@@ -772,6 +780,40 @@ static HMODULE (*get_module_handle_a_ptr)(_In_opt_ LPCSTR) = NULL; /* TODO: Impl
772780

773781
/* -- Methods -- */
774782

783+
#if 1 // NODE_MAJOR_VERSION < 18
784+
#if NODE_MAJOR_VERSION >= 12
785+
#define node_loader_impl_register_module_id node::ModuleFlags::kLinked
786+
#else
787+
#define node_loader_impl_register_module_id 0x02 /* NM_F_LINKED */
788+
#endif
789+
790+
#define node_loader_impl_register_module(name, fn) \
791+
do \
792+
{ \
793+
static napi_module node_loader_module = { \
794+
NAPI_MODULE_VERSION, \
795+
node_loader_impl_register_module_id, \
796+
__FILE__, \
797+
fn, \
798+
name, \
799+
NULL, \
800+
{ 0 } \
801+
}; \
802+
napi_module_register(&node_loader_module); \
803+
} while (0)
804+
805+
void node_loader_impl_register_linked_bindings()
806+
{
807+
/* Initialize Node Loader Trampoline */
808+
node_loader_impl_register_module("node_loader_trampoline_module", node_loader_trampoline_initialize);
809+
810+
/* Initialize Node Loader Port */
811+
node_loader_impl_register_module("node_loader_port_module", node_loader_port_initialize);
812+
}
813+
#else
814+
// TODO: New register implementation
815+
#endif
816+
775817
void node_loader_impl_exception(napi_env env, napi_status status)
776818
{
777819
if (status != napi_ok)
@@ -3385,7 +3427,7 @@ void node_loader_impl_handle_promise_safe(napi_env env, loader_impl_async_handle
33853427
}
33863428

33873429
/* Close the handle */
3388-
delete handle_promise_safe;
3430+
handle_promise_safe->destroy();
33893431

33903432
/* Close scope */
33913433
status = napi_close_handle_scope(env, handle_scope);
@@ -3445,6 +3487,7 @@ void *node_loader_impl_register(void *node_impl_ptr, void *env_ptr, void *functi
34453487
napi_value function_table_object;
34463488
napi_value global;
34473489
napi_status status;
3490+
napi_handle_scope handle_scope;
34483491

34493492
/* Lock node implementation mutex */
34503493
uv_mutex_lock(&node_impl->mutex);
@@ -3456,6 +3499,11 @@ void *node_loader_impl_register(void *node_impl_ptr, void *env_ptr, void *functi
34563499
env = static_cast<napi_env>(env_ptr);
34573500
function_table_object = static_cast<napi_value>(function_table_object_ptr);
34583501

3502+
/* Create scope */
3503+
status = napi_open_handle_scope(env, &handle_scope);
3504+
3505+
node_loader_impl_exception(env, status);
3506+
34593507
/* Make global object persistent */
34603508
status = napi_get_global(env, &global);
34613509

@@ -3548,6 +3596,11 @@ void *node_loader_impl_register(void *node_impl_ptr, void *env_ptr, void *functi
35483596
get_module_handle_a_ptr = (HMODULE(*)(_In_opt_ LPCSTR))node_loader_hook_import_address_table("kernel32.dll", "GetModuleHandleA", &get_module_handle_a_hook);
35493597
#endif
35503598

3599+
/* Close scope */
3600+
status = napi_close_handle_scope(env, handle_scope);
3601+
3602+
node_loader_impl_exception(env, status);
3603+
35513604
/* Signal start condition */
35523605
uv_cond_signal(&node_impl->cond);
35533606

@@ -3780,9 +3833,15 @@ void node_loader_impl_thread(void *data)
37803833
#endif
37813834
*/
37823835

3836+
// #if NODE_MAJOR_VERSION < 18
3837+
node_loader_impl_register_linked_bindings();
3838+
// #endif
3839+
37833840
/* Unlock node implementation mutex */
37843841
uv_mutex_unlock(&node_impl->mutex);
37853842

3843+
/* Register bindings for versions older than 18 */
3844+
37863845
/* Start NodeJS runtime */
37873846
int result = node::Start(argc, reinterpret_cast<char **>(argv));
37883847

@@ -3824,44 +3883,6 @@ loader_impl_data node_loader_impl_initialize(loader_impl impl, configuration con
38243883

38253884
(void)impl;
38263885

3827-
/* Initialize Node Loader Trampoline */
3828-
{
3829-
static napi_module node_loader_trampoline_module = {
3830-
NAPI_MODULE_VERSION,
3831-
#if NODE_MAJOR_VERSION >= 12
3832-
node::ModuleFlags::kLinked,
3833-
#else
3834-
0x02, /* NM_F_LINKED */
3835-
#endif
3836-
__FILE__,
3837-
node_loader_trampoline_initialize,
3838-
"node_loader_trampoline_module",
3839-
NULL,
3840-
{ 0 }
3841-
};
3842-
3843-
napi_module_register(&node_loader_trampoline_module);
3844-
}
3845-
3846-
/* Initialize Node Loader Port */
3847-
{
3848-
static napi_module node_loader_port_module = {
3849-
NAPI_MODULE_VERSION,
3850-
#if NODE_MAJOR_VERSION >= 12
3851-
node::ModuleFlags::kLinked,
3852-
#else
3853-
0x02, /* NM_F_LINKED */
3854-
#endif
3855-
__FILE__,
3856-
node_loader_port_initialize,
3857-
"node_loader_port_module",
3858-
NULL,
3859-
{ 0 }
3860-
};
3861-
3862-
napi_module_register(&node_loader_port_module);
3863-
}
3864-
38653886
node_impl = new loader_impl_node_type();
38663887

38673888
if (node_impl == nullptr)

source/tests/metacall_node_python_await_extended_test/source/metacall_node_python_await_extended_test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ TEST_F(metacall_node_python_await_extended_test, DefaultConstructor)
4646
/* Python */
4747
"import sys\n"
4848
"import threading\n"
49+
"counter = 0\n"
4950
"async def python_simple(n):\n"
50-
"\tprint('inside python_simple', threading.current_thread().ident, ':', n)\n"
51+
"\tglobal counter\n"
52+
"\tprint('inside python_simple', threading.current_thread().ident, counter, ':', n)\n"
53+
"\tcounter = counter + 1\n"
5154
"\tsys.stdout.flush()\n"
5255
"\treturn n\n"
5356
// "import asyncio\n"

0 commit comments

Comments
 (0)