nodejs
This commit is contained in:
parent
593985dad3
commit
9ec7eb1c3b
5 changed files with 91 additions and 39 deletions
48
.gitignore
vendored
48
.gitignore
vendored
|
@ -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
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
set(TARGET main)
|
set(TARGET main)
|
||||||
add_executable(${TARGET} main.cpp)
|
add_executable(${TARGET} main.cpp)
|
||||||
install(TARGETS ${TARGET} RUNTIME)
|
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)
|
target_compile_features(${TARGET} PRIVATE cxx_std_20)
|
||||||
|
|
||||||
|
|
|
@ -138,6 +138,7 @@ int main(int argc, char ** argv) {
|
||||||
// save choice to use color for later
|
// save choice to use color for later
|
||||||
// (note for later: this is a slightly awkward choice)
|
// (note for later: this is a slightly awkward choice)
|
||||||
console::init(params.simple_io, params.use_color);
|
console::init(params.simple_io, params.use_color);
|
||||||
|
process_output_plugin_node_init();
|
||||||
atexit([]() { console::cleanup(); });
|
atexit([]() { console::cleanup(); });
|
||||||
|
|
||||||
if (params.logits_all) {
|
if (params.logits_all) {
|
||||||
|
@ -910,5 +911,6 @@ int main(int argc, char ** argv) {
|
||||||
LOG_TEE("Log end\n");
|
LOG_TEE("Log end\n");
|
||||||
#endif // LOG_DISABLE_LOGS
|
#endif // LOG_DISABLE_LOGS
|
||||||
|
|
||||||
|
process_output_plugin_node_destroy();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,17 +7,31 @@
|
||||||
#include <libnode/node_api.h>
|
#include <libnode/node_api.h>
|
||||||
#include <libnode/js_native_api.h>
|
#include <libnode/js_native_api.h>
|
||||||
#include <libnode/js_native_api_types.h>
|
#include <libnode/js_native_api_types.h>
|
||||||
|
|
||||||
|
|
||||||
|
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,
|
std::string process_output_plugin_node(const std::string start,
|
||||||
const std::string state,
|
const std::string state,
|
||||||
const std::string input) {
|
const std::string input) {
|
||||||
|
|
||||||
// !!! All napi calls for one given environment must
|
// This is a V8 isolate, there may be multiple
|
||||||
// !!! 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
|
|
||||||
napi_env env;
|
napi_env env;
|
||||||
// This holds local references, when it is closed
|
// This holds local references, when it is closed
|
||||||
// they become available to the GC
|
// they become available to the GC
|
||||||
|
@ -29,25 +43,14 @@ std::string process_output_plugin_node(const std::string start,
|
||||||
napi_value result;
|
napi_value result;
|
||||||
|
|
||||||
const char *main_script = "console.log('hello world'); "
|
const char *main_script = "console.log('hello world'); "
|
||||||
"function callMe() { console.log('called you'); }"
|
"function callMe() { console.log('called you'); }"
|
||||||
// or you can use vm.runInThisContext
|
"global.callMe = callMe;";
|
||||||
"global.callMe = callMe;";
|
|
||||||
|
|
||||||
// Do only once
|
if (napi_create_environment(context.platform, NULL, main_script, &env) != napi_ok) {
|
||||||
if (napi_create_platform(0, NULL, 0, NULL, NULL, 0, &platform) != napi_ok) {
|
fprintf(stderr, "Failed running JS\n");
|
||||||
fprintf(stderr, "Failed creating the platform\n");
|
return "error1";
|
||||||
return "error";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
if (napi_get_global(env, &global) != napi_ok) {
|
||||||
fprintf(stderr, "Failed accessing the global object\n");
|
fprintf(stderr, "Failed accessing the global object\n");
|
||||||
return "Failed accessing the global object";
|
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");
|
fprintf(stderr, "Failed accessing the global object\n");
|
||||||
return "Failed accessing the global object";
|
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) {
|
if (napi_call_function(env, global, cb, 0, NULL, &result) != napi_ok) {
|
||||||
fprintf(stderr, "Failed calling JS callback\n");
|
fprintf(stderr, "Failed calling JS callback\n");
|
||||||
return "Failed calling JS callback";
|
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) {
|
if (napi_run_environment(env) != napi_ok) {
|
||||||
fprintf(stderr, "Failed flushing pending JS callbacks\n");
|
fprintf(stderr, "Failed flushing pending JS callbacks\n");
|
||||||
return "Failed flushing pending JS callbacks";
|
return "Failed flushing pending JS callbacks";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown everyhing
|
|
||||||
napi_close_handle_scope(env, scope);
|
napi_close_handle_scope(env, scope);
|
||||||
|
|
||||||
if (napi_destroy_environment(env, NULL) != napi_ok) {
|
if (napi_destroy_environment(env, NULL) != napi_ok) {
|
||||||
return "destroy";
|
return "destroy";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (napi_destroy_platform(platform) != napi_ok) {
|
|
||||||
fprintf(stderr, "Failed destroying the platform\n");
|
|
||||||
return "Failed destroying the platform";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "OK";
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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,
|
std::string process_output_plugin_node(const std::string start,
|
||||||
const std::string state,
|
const std::string state,
|
||||||
const std::string input);
|
const std::string input);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue