From 9ec7eb1c3bbee1296356ca3859e0debc64905a06 Mon Sep 17 00:00:00 2001 From: mike dupont Date: Fri, 8 Dec 2023 13:44:39 -0500 Subject: [PATCH] nodejs --- .gitignore | 48 +++++++++++++++++++++++ examples/main/CMakeLists.txt | 2 +- examples/main/main.cpp | 2 + plugin_nodejs.cpp | 75 ++++++++++++++++++------------------ plugin_nodejs.hpp | 3 ++ 5 files changed, 91 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 14f0c136b..a6f3abcb6 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,51 @@ tests/test-tokenizer-1-bpe *~ .#* #* +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data diff --git a/examples/main/CMakeLists.txt b/examples/main/CMakeLists.txt index ed0eebb12..d4b96cd8a 100644 --- a/examples/main/CMakeLists.txt +++ b/examples/main/CMakeLists.txt @@ -1,6 +1,6 @@ set(TARGET main) add_executable(${TARGET} main.cpp) install(TARGETS ${TARGET} RUNTIME) -target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} libnode.so plugin_nodejs.o) +target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} libnode.so ) target_compile_features(${TARGET} PRIVATE cxx_std_20) diff --git a/examples/main/main.cpp b/examples/main/main.cpp index 44af3fe6d..3bf134774 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -138,6 +138,7 @@ int main(int argc, char ** argv) { // save choice to use color for later // (note for later: this is a slightly awkward choice) console::init(params.simple_io, params.use_color); + process_output_plugin_node_init(); atexit([]() { console::cleanup(); }); if (params.logits_all) { @@ -910,5 +911,6 @@ int main(int argc, char ** argv) { LOG_TEE("Log end\n"); #endif // LOG_DISABLE_LOGS + process_output_plugin_node_destroy(); return 0; } diff --git a/plugin_nodejs.cpp b/plugin_nodejs.cpp index 3a21c905a..1b5beab81 100644 --- a/plugin_nodejs.cpp +++ b/plugin_nodejs.cpp @@ -7,17 +7,31 @@ #include #include #include + + +class Context { +public: + napi_platform platform; + +}; + +static Context context; + +void process_output_plugin_node_init() +{ + if (napi_create_platform(0, NULL, 0, NULL, NULL, 0, &context.platform) != napi_ok) { + fprintf(stderr, "Failed creating the platform\n"); + return "error"; + } + + +} + std::string process_output_plugin_node(const std::string start, const std::string state, const std::string input) { - // !!! All napi calls for one given environment must - // !!! be made from the same thread that created it - // (except everything napi_threadsafe_function related) - - // This the V8 engine, there must be only one - napi_platform platform; - // This is a V8 isolate, there may be multiple + // This is a V8 isolate, there may be multiple napi_env env; // This holds local references, when it is closed // they become available to the GC @@ -29,25 +43,14 @@ std::string process_output_plugin_node(const std::string start, napi_value result; const char *main_script = "console.log('hello world'); " - "function callMe() { console.log('called you'); }" - // or you can use vm.runInThisContext - "global.callMe = callMe;"; + "function callMe() { console.log('called you'); }" + "global.callMe = callMe;"; - // Do only once - if (napi_create_platform(0, NULL, 0, NULL, NULL, 0, &platform) != napi_ok) { - fprintf(stderr, "Failed creating the platform\n"); - return "error"; + if (napi_create_environment(context.platform, NULL, main_script, &env) != napi_ok) { + fprintf(stderr, "Failed running JS\n"); + return "error1"; } - // Do for each environment (V8 isolate) - // 'hello world' will be printed here - if (napi_create_environment(platform, NULL, main_script, &env) != napi_ok) { - fprintf(stderr, "Failed running JS\n"); - return "error1"; - } - - // Here you can interact with the environment through Node-API env - // (refer to the Node-API doc) if (napi_get_global(env, &global) != napi_ok) { fprintf(stderr, "Failed accessing the global object\n"); return "Failed accessing the global object"; @@ -57,34 +60,30 @@ std::string process_output_plugin_node(const std::string start, fprintf(stderr, "Failed accessing the global object\n"); return "Failed accessing the global object"; } - - // This cycle can be repeated { - // Call a JS function - // V8 will run in this thread if (napi_call_function(env, global, cb, 0, NULL, &result) != napi_ok) { fprintf(stderr, "Failed calling JS callback\n"); return "Failed calling JS callback"; } - // (optional) Call this to flush all pending async callbacks - // V8 will run in this thread if (napi_run_environment(env) != napi_ok) { fprintf(stderr, "Failed flushing pending JS callbacks\n"); return "Failed flushing pending JS callbacks"; } } - - // Shutdown everyhing napi_close_handle_scope(env, scope); - if (napi_destroy_environment(env, NULL) != napi_ok) { return "destroy"; } - - if (napi_destroy_platform(platform) != napi_ok) { - fprintf(stderr, "Failed destroying the platform\n"); - return "Failed destroying the platform"; - } - return "OK"; } + + +void process_output_plugin_node_destroy(); +void process_output_plugin_node_destroy() +{ + + if (napi_destroy_platform(context.platform) != napi_ok) { + fprintf(stderr, "Failed destroying the platform\n"); + //return "Failed destroying the platform"; + } +} diff --git a/plugin_nodejs.hpp b/plugin_nodejs.hpp index b8ee4b262..f20f6028d 100644 --- a/plugin_nodejs.hpp +++ b/plugin_nodejs.hpp @@ -1,3 +1,6 @@ +void process_output_plugin_node_init(); +void process_output_plugin_node_destroy(); + std::string process_output_plugin_node(const std::string start, const std::string state, const std::string input);